Whamcloud - gitweb
LU-9859 libcfs: delete libcfs/linux/libcfs.h
[fs/lustre-release.git] / lustre / lmv / lmv_obd.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #define DEBUG_SUBSYSTEM S_LMV
34
35 #include <linux/file.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/user_namespace.h>
39 #ifdef HAVE_UIDGID_HEADER
40 # include <linux/uidgid.h>
41 #endif
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
44 #include <linux/mm.h>
45 #include <linux/math64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
48
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_lmv.h>
54 #include <lprocfs_status.h>
55 #include <cl_object.h>
56 #include <lustre_fid.h>
57 #include <uapi/linux/lustre/lustre_ioctl.h>
58 #include <lustre_kernelcomm.h>
59 #include "lmv_internal.h"
60
61 static int lmv_check_connect(struct obd_device *obd);
62
63 static void lmv_activate_target(struct lmv_obd *lmv,
64                                 struct lmv_tgt_desc *tgt,
65                                 int activate)
66 {
67         if (tgt->ltd_active == activate)
68                 return;
69
70         tgt->ltd_active = activate;
71         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
72
73         tgt->ltd_exp->exp_obd->obd_inactive = !activate;
74 }
75
76 /**
77  * Error codes:
78  *
79  *  -EINVAL  : UUID can't be found in the LMV's target list
80  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
81  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
82  */
83 static int lmv_set_mdc_active(struct lmv_obd *lmv,
84                               const struct obd_uuid *uuid,
85                               int activate)
86 {
87         struct lmv_tgt_desc     *tgt = NULL;
88         struct obd_device       *obd;
89         __u32                    i;
90         int                      rc = 0;
91         ENTRY;
92
93         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
94                         lmv, uuid->uuid, activate);
95
96         spin_lock(&lmv->lmv_lock);
97         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
98                 tgt = lmv->tgts[i];
99                 if (tgt == NULL || tgt->ltd_exp == NULL)
100                         continue;
101
102                 CDEBUG(D_INFO, "Target idx %d is %s conn %#llx\n", i,
103                        tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
104
105                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
106                         break;
107         }
108
109         if (i == lmv->desc.ld_tgt_count)
110                 GOTO(out_lmv_lock, rc = -EINVAL);
111
112         obd = class_exp2obd(tgt->ltd_exp);
113         if (obd == NULL)
114                 GOTO(out_lmv_lock, rc = -ENOTCONN);
115
116         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
117                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
118                obd->obd_type->typ_name, i);
119         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
120
121         if (tgt->ltd_active == activate) {
122                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
123                        activate ? "" : "in");
124                 GOTO(out_lmv_lock, rc);
125         }
126
127         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
128                activate ? "" : "in");
129         lmv_activate_target(lmv, tgt, activate);
130         EXIT;
131
132  out_lmv_lock:
133         spin_unlock(&lmv->lmv_lock);
134         return rc;
135 }
136
137 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
138 {
139         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
140         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
141
142         return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
143 }
144
145 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
146                       enum obd_notify_event ev)
147 {
148         struct obd_connect_data *conn_data;
149         struct lmv_obd          *lmv = &obd->u.lmv;
150         struct obd_uuid         *uuid;
151         int                      rc = 0;
152         ENTRY;
153
154         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
155                 CERROR("unexpected notification of %s %s!\n",
156                        watched->obd_type->typ_name,
157                        watched->obd_name);
158                 RETURN(-EINVAL);
159         }
160
161         uuid = &watched->u.cli.cl_target_uuid;
162         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
163                 /*
164                  * Set MDC as active before notifying the observer, so the
165                  * observer can use the MDC normally.
166                  */
167                 rc = lmv_set_mdc_active(lmv, uuid,
168                                         ev == OBD_NOTIFY_ACTIVE);
169                 if (rc) {
170                         CERROR("%sactivation of %s failed: %d\n",
171                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
172                                uuid->uuid, rc);
173                         RETURN(rc);
174                 }
175         } else if (ev == OBD_NOTIFY_OCD) {
176                 conn_data = &watched->u.cli.cl_import->imp_connect_data;
177                 /*
178                  * XXX: Make sure that ocd_connect_flags from all targets are
179                  * the same. Otherwise one of MDTs runs wrong version or
180                  * something like this.  --umka
181                  */
182                 obd->obd_self_export->exp_connect_data = *conn_data;
183         }
184
185         /*
186          * Pass the notification up the chain.
187          */
188         if (obd->obd_observer)
189                 rc = obd_notify(obd->obd_observer, watched, ev);
190
191         RETURN(rc);
192 }
193
194 static int lmv_connect(const struct lu_env *env,
195                        struct obd_export **pexp, struct obd_device *obd,
196                        struct obd_uuid *cluuid, struct obd_connect_data *data,
197                        void *localdata)
198 {
199         struct lmv_obd *lmv = &obd->u.lmv;
200         struct lustre_handle conn = { 0 };
201         struct obd_export *exp;
202         int rc;
203         ENTRY;
204
205         rc = class_connect(&conn, obd, cluuid);
206         if (rc) {
207                 CERROR("class_connection() returned %d\n", rc);
208                 RETURN(rc);
209         }
210
211         exp = class_conn2export(&conn);
212
213         lmv->connected = 0;
214         lmv->conn_data = *data;
215
216         if (lmv->targets_proc_entry == NULL) {
217                 lmv->targets_proc_entry = lprocfs_register("target_obds",
218                                                            obd->obd_proc_entry,
219                                                            NULL, NULL);
220                 if (IS_ERR(lmv->targets_proc_entry)) {
221                         CERROR("%s: cannot register "
222                                "/proc/fs/lustre/%s/%s/target_obds\n",
223                                obd->obd_name, obd->obd_type->typ_name,
224                                obd->obd_name);
225                         lmv->targets_proc_entry = NULL;
226                 }
227         }
228
229         rc = lmv_check_connect(obd);
230         if (rc != 0)
231                 GOTO(out_proc, rc);
232
233         *pexp = exp;
234
235         RETURN(rc);
236
237 out_proc:
238         if (lmv->targets_proc_entry != NULL)
239                 lprocfs_remove(&lmv->targets_proc_entry);
240
241         class_disconnect(exp);
242
243         return rc;
244 }
245
246 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
247                             __u32 def_easize)
248 {
249         struct obd_device       *obd = exp->exp_obd;
250         struct lmv_obd          *lmv = &obd->u.lmv;
251         __u32                    i;
252         int                      rc = 0;
253         int                      change = 0;
254         ENTRY;
255
256         if (lmv->max_easize < easize) {
257                 lmv->max_easize = easize;
258                 change = 1;
259         }
260         if (lmv->max_def_easize < def_easize) {
261                 lmv->max_def_easize = def_easize;
262                 change = 1;
263         }
264
265         if (change == 0)
266                 RETURN(0);
267
268         if (lmv->connected == 0)
269                 RETURN(0);
270
271         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
272                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
273
274                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
275                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
276                         continue;
277                 }
278
279                 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
280                 if (rc) {
281                         CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
282                                " rc = %d\n", obd->obd_name, i, rc);
283                         break;
284                 }
285         }
286         RETURN(rc);
287 }
288
289 #define MAX_STRING_SIZE 128
290
291 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
292 {
293         struct lmv_obd          *lmv = &obd->u.lmv;
294         struct obd_device       *mdc_obd;
295         struct obd_export       *mdc_exp;
296         struct lu_fld_target     target;
297         int                      rc;
298         ENTRY;
299
300         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
301                                         &obd->obd_uuid);
302         if (!mdc_obd) {
303                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
304                 RETURN(-EINVAL);
305         }
306
307         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s\n",
308                mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
309                tgt->ltd_uuid.uuid, obd->obd_uuid.uuid);
310
311         if (!mdc_obd->obd_set_up) {
312                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
313                 RETURN(-EINVAL);
314         }
315
316         rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
317                          &lmv->conn_data, NULL);
318         if (rc) {
319                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
320                 RETURN(rc);
321         }
322
323         /*
324          * Init fid sequence client for this mdc and add new fld target.
325          */
326         rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
327         if (rc)
328                 RETURN(rc);
329
330         target.ft_srv = NULL;
331         target.ft_exp = mdc_exp;
332         target.ft_idx = tgt->ltd_idx;
333
334         fld_client_add_target(&lmv->lmv_fld, &target);
335
336         rc = obd_register_observer(mdc_obd, obd);
337         if (rc) {
338                 obd_disconnect(mdc_exp);
339                 CERROR("target %s register_observer error %d\n",
340                        tgt->ltd_uuid.uuid, rc);
341                 RETURN(rc);
342         }
343
344         if (obd->obd_observer) {
345                 /*
346                  * Tell the observer about the new target.
347                  */
348                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
349                                 OBD_NOTIFY_ACTIVE);
350                 if (rc) {
351                         obd_disconnect(mdc_exp);
352                         RETURN(rc);
353                 }
354         }
355
356         tgt->ltd_active = 1;
357         tgt->ltd_exp = mdc_exp;
358         lmv->desc.ld_active_tgt_count++;
359
360         md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
361
362         CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
363                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
364                 atomic_read(&obd->obd_refcount));
365
366         if (lmv->targets_proc_entry != NULL) {
367                 struct proc_dir_entry *mdc_symlink;
368
369                 LASSERT(mdc_obd->obd_type != NULL);
370                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
371                 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
372                                                   lmv->targets_proc_entry,
373                                                   "../../../%s/%s",
374                                                   mdc_obd->obd_type->typ_name,
375                                                   mdc_obd->obd_name);
376                 if (mdc_symlink == NULL) {
377                         CERROR("cannot register LMV target "
378                                "/proc/fs/lustre/%s/%s/target_obds/%s\n",
379                                obd->obd_type->typ_name, obd->obd_name,
380                                mdc_obd->obd_name);
381                 }
382         }
383         RETURN(0);
384 }
385
386 static void lmv_del_target(struct lmv_obd *lmv, int index)
387 {
388         if (lmv->tgts[index] == NULL)
389                 return;
390
391         OBD_FREE_PTR(lmv->tgts[index]);
392         lmv->tgts[index] = NULL;
393         return;
394 }
395
396 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
397                            __u32 index, int gen)
398 {
399         struct obd_device *mdc_obd;
400         struct lmv_obd      *lmv = &obd->u.lmv;
401         struct lmv_tgt_desc *tgt;
402         int                  orig_tgt_count = 0;
403         int                  rc = 0;
404         ENTRY;
405
406         CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
407         mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
408                                         &obd->obd_uuid);
409         if (!mdc_obd) {
410                 CERROR("%s: Target %s not attached: rc = %d\n",
411                        obd->obd_name, uuidp->uuid, -EINVAL);
412                 RETURN(-EINVAL);
413         }
414
415         mutex_lock(&lmv->lmv_init_mutex);
416         if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
417                 tgt = lmv->tgts[index];
418                 CERROR("%s: UUID %s already assigned at LMV target index %d:"
419                        " rc = %d\n", obd->obd_name,
420                        obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
421                 mutex_unlock(&lmv->lmv_init_mutex);
422                 RETURN(-EEXIST);
423         }
424
425         if (index >= lmv->tgts_size) {
426                 /* We need to reallocate the lmv target array. */
427                 struct lmv_tgt_desc **newtgts, **old = NULL;
428                 __u32 newsize = 1;
429                 __u32 oldsize = 0;
430
431                 while (newsize < index + 1)
432                         newsize = newsize << 1;
433                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
434                 if (newtgts == NULL) {
435                         mutex_unlock(&lmv->lmv_init_mutex);
436                         RETURN(-ENOMEM);
437                 }
438
439                 if (lmv->tgts_size) {
440                         memcpy(newtgts, lmv->tgts,
441                                sizeof(*newtgts) * lmv->tgts_size);
442                         old = lmv->tgts;
443                         oldsize = lmv->tgts_size;
444                 }
445
446                 lmv->tgts = newtgts;
447                 lmv->tgts_size = newsize;
448                 smp_rmb();
449                 if (old)
450                         OBD_FREE(old, sizeof(*old) * oldsize);
451
452                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
453                        lmv->tgts_size);
454         }
455
456         OBD_ALLOC_PTR(tgt);
457         if (!tgt) {
458                 mutex_unlock(&lmv->lmv_init_mutex);
459                 RETURN(-ENOMEM);
460         }
461
462         mutex_init(&tgt->ltd_fid_mutex);
463         tgt->ltd_idx = index;
464         tgt->ltd_uuid = *uuidp;
465         tgt->ltd_active = 0;
466         lmv->tgts[index] = tgt;
467         if (index >= lmv->desc.ld_tgt_count) {
468                 orig_tgt_count = lmv->desc.ld_tgt_count;
469                 lmv->desc.ld_tgt_count = index + 1;
470         }
471
472         if (lmv->connected == 0) {
473                 /* lmv_check_connect() will connect this target. */
474                 mutex_unlock(&lmv->lmv_init_mutex);
475                 RETURN(0);
476         }
477
478         /* Otherwise let's connect it ourselves */
479         mutex_unlock(&lmv->lmv_init_mutex);
480         rc = lmv_connect_mdc(obd, tgt);
481         if (rc != 0) {
482                 spin_lock(&lmv->lmv_lock);
483                 if (lmv->desc.ld_tgt_count == index + 1)
484                         lmv->desc.ld_tgt_count = orig_tgt_count;
485                 memset(tgt, 0, sizeof(*tgt));
486                 spin_unlock(&lmv->lmv_lock);
487         } else {
488                 int easize = sizeof(struct lmv_stripe_md) +
489                         lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
490                 lmv_init_ea_size(obd->obd_self_export, easize, 0);
491         }
492
493         RETURN(rc);
494 }
495
496 static int lmv_check_connect(struct obd_device *obd)
497 {
498         struct lmv_obd          *lmv = &obd->u.lmv;
499         struct lmv_tgt_desc     *tgt;
500         __u32                    i;
501         int                      rc;
502         int                      easize;
503         ENTRY;
504
505         if (lmv->connected)
506                 RETURN(0);
507
508         mutex_lock(&lmv->lmv_init_mutex);
509         if (lmv->connected) {
510                 mutex_unlock(&lmv->lmv_init_mutex);
511                 RETURN(0);
512         }
513
514         if (lmv->desc.ld_tgt_count == 0) {
515                 mutex_unlock(&lmv->lmv_init_mutex);
516                 CERROR("%s: no targets configured.\n", obd->obd_name);
517                 RETURN(-EINVAL);
518         }
519
520         LASSERT(lmv->tgts != NULL);
521
522         if (lmv->tgts[0] == NULL) {
523                 mutex_unlock(&lmv->lmv_init_mutex);
524                 CERROR("%s: no target configured for index 0.\n",
525                        obd->obd_name);
526                 RETURN(-EINVAL);
527         }
528
529         CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
530                obd->obd_uuid.uuid, obd->obd_name);
531
532         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
533                 tgt = lmv->tgts[i];
534                 if (tgt == NULL)
535                         continue;
536                 rc = lmv_connect_mdc(obd, tgt);
537                 if (rc)
538                         GOTO(out_disc, rc);
539         }
540
541         lmv->connected = 1;
542         easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
543         lmv_init_ea_size(obd->obd_self_export, easize, 0);
544         mutex_unlock(&lmv->lmv_init_mutex);
545         RETURN(0);
546
547  out_disc:
548         while (i-- > 0) {
549                 int rc2;
550                 tgt = lmv->tgts[i];
551                 if (tgt == NULL)
552                         continue;
553                 tgt->ltd_active = 0;
554                 if (tgt->ltd_exp) {
555                         --lmv->desc.ld_active_tgt_count;
556                         rc2 = obd_disconnect(tgt->ltd_exp);
557                         if (rc2) {
558                                 CERROR("LMV target %s disconnect on "
559                                        "MDC idx %d: error %d\n",
560                                        tgt->ltd_uuid.uuid, i, rc2);
561                         }
562                 }
563         }
564
565         mutex_unlock(&lmv->lmv_init_mutex);
566
567         RETURN(rc);
568 }
569
570 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
571 {
572         struct lmv_obd         *lmv = &obd->u.lmv;
573         struct obd_device      *mdc_obd;
574         int                     rc;
575         ENTRY;
576
577         LASSERT(tgt != NULL);
578         LASSERT(obd != NULL);
579
580         mdc_obd = class_exp2obd(tgt->ltd_exp);
581
582         if (mdc_obd) {
583                 mdc_obd->obd_force = obd->obd_force;
584                 mdc_obd->obd_fail = obd->obd_fail;
585                 mdc_obd->obd_no_recov = obd->obd_no_recov;
586
587                 if (lmv->targets_proc_entry != NULL)
588                         lprocfs_remove_proc_entry(mdc_obd->obd_name,
589                                                   lmv->targets_proc_entry);
590         }
591
592         rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
593         if (rc)
594                 CERROR("Can't finanize fids factory\n");
595
596         CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
597                tgt->ltd_exp->exp_obd->obd_name,
598                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
599
600         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
601         rc = obd_disconnect(tgt->ltd_exp);
602         if (rc) {
603                 if (tgt->ltd_active) {
604                         CERROR("Target %s disconnect error %d\n",
605                                tgt->ltd_uuid.uuid, rc);
606                 }
607         }
608
609         lmv_activate_target(lmv, tgt, 0);
610         tgt->ltd_exp = NULL;
611         RETURN(0);
612 }
613
614 static int lmv_disconnect(struct obd_export *exp)
615 {
616         struct obd_device       *obd = class_exp2obd(exp);
617         struct lmv_obd          *lmv = &obd->u.lmv;
618         int                      rc;
619         __u32                    i;
620         ENTRY;
621
622         if (!lmv->tgts)
623                 goto out_local;
624
625         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
626                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
627                         continue;
628
629                 lmv_disconnect_mdc(obd, lmv->tgts[i]);
630         }
631
632         if (lmv->targets_proc_entry != NULL)
633                 lprocfs_remove(&lmv->targets_proc_entry);
634         else
635                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
636                        obd->obd_type->typ_name, obd->obd_name);
637
638 out_local:
639         /*
640          * This is the case when no real connection is established by
641          * lmv_check_connect().
642          */
643         if (!lmv->connected)
644                 class_export_put(exp);
645         rc = class_disconnect(exp);
646         lmv->connected = 0;
647
648         RETURN(rc);
649 }
650
651 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
652                         void __user *uarg)
653 {
654         struct obd_device       *obddev = class_exp2obd(exp);
655         struct lmv_obd          *lmv = &obddev->u.lmv;
656         struct getinfo_fid2path *gf;
657         struct lmv_tgt_desc     *tgt;
658         struct getinfo_fid2path *remote_gf = NULL;
659         struct lu_fid           root_fid;
660         int                     remote_gf_size = 0;
661         int                     rc;
662
663         gf = karg;
664         tgt = lmv_find_target(lmv, &gf->gf_fid);
665         if (IS_ERR(tgt))
666                 RETURN(PTR_ERR(tgt));
667
668         root_fid = *gf->gf_u.gf_root_fid;
669         LASSERT(fid_is_sane(&root_fid));
670
671 repeat_fid2path:
672         rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
673         if (rc != 0 && rc != -EREMOTE)
674                 GOTO(out_fid2path, rc);
675
676         /* If remote_gf != NULL, it means just building the
677          * path on the remote MDT, copy this path segement to gf */
678         if (remote_gf != NULL) {
679                 struct getinfo_fid2path *ori_gf;
680                 char *ptr;
681
682                 ori_gf = (struct getinfo_fid2path *)karg;
683                 if (strlen(ori_gf->gf_u.gf_path) + 1 +
684                     strlen(gf->gf_u.gf_path) + 1 > ori_gf->gf_pathlen)
685                         GOTO(out_fid2path, rc = -EOVERFLOW);
686
687                 ptr = ori_gf->gf_u.gf_path;
688
689                 memmove(ptr + strlen(gf->gf_u.gf_path) + 1, ptr,
690                         strlen(ori_gf->gf_u.gf_path));
691
692                 strncpy(ptr, gf->gf_u.gf_path,
693                         strlen(gf->gf_u.gf_path));
694                 ptr += strlen(gf->gf_u.gf_path);
695                 *ptr = '/';
696         }
697
698         CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
699                tgt->ltd_exp->exp_obd->obd_name,
700                gf->gf_u.gf_path, PFID(&gf->gf_fid), gf->gf_recno,
701                gf->gf_linkno);
702
703         if (rc == 0)
704                 GOTO(out_fid2path, rc);
705
706         /* sigh, has to go to another MDT to do path building further */
707         if (remote_gf == NULL) {
708                 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
709                 OBD_ALLOC(remote_gf, remote_gf_size);
710                 if (remote_gf == NULL)
711                         GOTO(out_fid2path, rc = -ENOMEM);
712                 remote_gf->gf_pathlen = PATH_MAX;
713         }
714
715         if (!fid_is_sane(&gf->gf_fid)) {
716                 CERROR("%s: invalid FID "DFID": rc = %d\n",
717                        tgt->ltd_exp->exp_obd->obd_name,
718                        PFID(&gf->gf_fid), -EINVAL);
719                 GOTO(out_fid2path, rc = -EINVAL);
720         }
721
722         tgt = lmv_find_target(lmv, &gf->gf_fid);
723         if (IS_ERR(tgt))
724                 GOTO(out_fid2path, rc = -EINVAL);
725
726         remote_gf->gf_fid = gf->gf_fid;
727         remote_gf->gf_recno = -1;
728         remote_gf->gf_linkno = -1;
729         memset(remote_gf->gf_u.gf_path, 0, remote_gf->gf_pathlen);
730         *remote_gf->gf_u.gf_root_fid = root_fid;
731         gf = remote_gf;
732         goto repeat_fid2path;
733
734 out_fid2path:
735         if (remote_gf != NULL)
736                 OBD_FREE(remote_gf, remote_gf_size);
737         RETURN(rc);
738 }
739
740 static int lmv_hsm_req_count(struct lmv_obd *lmv,
741                              const struct hsm_user_request *hur,
742                              const struct lmv_tgt_desc *tgt_mds)
743 {
744         __u32                    i;
745         int                      nr = 0;
746         struct lmv_tgt_desc     *curr_tgt;
747
748         /* count how many requests must be sent to the given target */
749         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
750                 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
751                 if (IS_ERR(curr_tgt))
752                         RETURN(PTR_ERR(curr_tgt));
753                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
754                         nr++;
755         }
756         return nr;
757 }
758
759 static int lmv_hsm_req_build(struct lmv_obd *lmv,
760                               struct hsm_user_request *hur_in,
761                               const struct lmv_tgt_desc *tgt_mds,
762                               struct hsm_user_request *hur_out)
763 {
764         __u32                    i, nr_out;
765         struct lmv_tgt_desc     *curr_tgt;
766
767         /* build the hsm_user_request for the given target */
768         hur_out->hur_request = hur_in->hur_request;
769         nr_out = 0;
770         for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
771                 curr_tgt = lmv_find_target(lmv,
772                                            &hur_in->hur_user_item[i].hui_fid);
773                 if (IS_ERR(curr_tgt))
774                         RETURN(PTR_ERR(curr_tgt));
775                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
776                         hur_out->hur_user_item[nr_out] =
777                                                 hur_in->hur_user_item[i];
778                         nr_out++;
779                 }
780         }
781         hur_out->hur_request.hr_itemcount = nr_out;
782         memcpy(hur_data(hur_out), hur_data(hur_in),
783                hur_in->hur_request.hr_data_len);
784
785         RETURN(0);
786 }
787
788 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
789                                  int len, struct lustre_kernelcomm *lk,
790                                  void __user *uarg)
791 {
792         struct lmv_obd *lmv = &obd->u.lmv;
793         __u32   i;
794         int     rc;
795         ENTRY;
796
797         /* unregister request (call from llapi_hsm_copytool_fini) */
798         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
799                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
800
801                 if (tgt == NULL || tgt->ltd_exp == NULL)
802                         continue;
803                 /* best effort: try to clean as much as possible
804                  * (continue on error) */
805                 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
806         }
807
808         /* Whatever the result, remove copytool from kuc groups.
809          * Unreached coordinators will get EPIPE on next requests
810          * and will unregister automatically.
811          */
812         rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
813
814         RETURN(rc);
815 }
816
817 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
818                                int len, struct lustre_kernelcomm *lk,
819                                void __user *uarg)
820 {
821         struct lmv_obd *lmv = &obd->u.lmv;
822         struct file             *filp;
823         __u32                    i, j;
824         int                      err;
825         bool                     any_set = false;
826         struct kkuc_ct_data      kcd = {
827                 .kcd_magic   = KKUC_CT_DATA_MAGIC,
828                 .kcd_archive = lk->lk_data,
829         };
830         int                      rc = 0;
831         ENTRY;
832
833         filp = fget(lk->lk_wfd);
834         if (!filp)
835                 RETURN(-EBADF);
836
837         rc = libcfs_kkuc_group_add(filp, &obd->obd_uuid, lk->lk_uid,
838                                    lk->lk_group, &kcd, sizeof(kcd));
839         if (rc)
840                 GOTO(err_fput, rc);
841
842         /* All or nothing: try to register to all MDS.
843          * In case of failure, unregister from previous MDS,
844          * except if it because of inactive target. */
845         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
846                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
847
848                 if (tgt == NULL || tgt->ltd_exp == NULL)
849                         continue;
850
851                 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
852                 if (err) {
853                         if (tgt->ltd_active) {
854                                 /* permanent error */
855                                 CERROR("%s: iocontrol MDC %s on MDT"
856                                        " idx %d cmd %x: err = %d\n",
857                                        lmv2obd_dev(lmv)->obd_name,
858                                        tgt->ltd_uuid.uuid, i, cmd, err);
859                                 rc = err;
860                                 lk->lk_flags |= LK_FLG_STOP;
861                                 /* unregister from previous MDS */
862                                 for (j = 0; j < i; j++) {
863                                         tgt = lmv->tgts[j];
864                                         if (tgt == NULL || tgt->ltd_exp == NULL)
865                                                 continue;
866                                         obd_iocontrol(cmd, tgt->ltd_exp, len,
867                                                       lk, uarg);
868                                 }
869                                 GOTO(err_kkuc_rem, rc);
870                         }
871                         /* else: transient error.
872                          * kuc will register to the missing MDT
873                          * when it is back */
874                 } else {
875                         any_set = true;
876                 }
877         }
878
879         if (!any_set)
880                 /* no registration done: return error */
881                 GOTO(err_kkuc_rem, rc = -ENOTCONN);
882
883         RETURN(0);
884
885 err_kkuc_rem:
886         libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
887
888 err_fput:
889         fput(filp);
890         return rc;
891 }
892
893
894
895
896 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
897                          int len, void *karg, void __user *uarg)
898 {
899         struct obd_device       *obddev = class_exp2obd(exp);
900         struct lmv_obd          *lmv = &obddev->u.lmv;
901         struct lmv_tgt_desc     *tgt = NULL;
902         __u32                    i = 0;
903         int                      rc = 0;
904         int                      set = 0;
905         __u32                    count = lmv->desc.ld_tgt_count;
906         ENTRY;
907
908         if (count == 0)
909                 RETURN(-ENOTTY);
910
911         switch (cmd) {
912         case IOC_OBD_STATFS: {
913                 struct obd_ioctl_data *data = karg;
914                 struct obd_device *mdc_obd;
915                 struct obd_statfs stat_buf = {0};
916                 __u32 index;
917
918                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
919                 if ((index >= count))
920                         RETURN(-ENODEV);
921
922                 tgt = lmv->tgts[index];
923                 if (tgt == NULL || !tgt->ltd_active)
924                         RETURN(-ENODATA);
925
926                 mdc_obd = class_exp2obd(tgt->ltd_exp);
927                 if (!mdc_obd)
928                         RETURN(-EINVAL);
929
930                 /* copy UUID */
931                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
932                                  min((int) data->ioc_plen2,
933                                      (int) sizeof(struct obd_uuid))))
934                         RETURN(-EFAULT);
935
936                 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
937                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
938                                 0);
939                 if (rc)
940                         RETURN(rc);
941                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
942                                  min((int) data->ioc_plen1,
943                                      (int) sizeof(stat_buf))))
944                         RETURN(-EFAULT);
945                 break;
946         }
947         case OBD_IOC_QUOTACTL: {
948                 struct if_quotactl *qctl = karg;
949                 struct obd_quotactl *oqctl;
950
951                 if (qctl->qc_valid == QC_MDTIDX) {
952                         if (count <= qctl->qc_idx)
953                                 RETURN(-EINVAL);
954
955                         tgt = lmv->tgts[qctl->qc_idx];
956                         if (tgt == NULL || tgt->ltd_exp == NULL)
957                                 RETURN(-EINVAL);
958                 } else if (qctl->qc_valid == QC_UUID) {
959                         for (i = 0; i < count; i++) {
960                                 tgt = lmv->tgts[i];
961                                 if (tgt == NULL)
962                                         continue;
963                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
964                                                      &qctl->obd_uuid))
965                                         continue;
966
967                                 if (tgt->ltd_exp == NULL)
968                                         RETURN(-EINVAL);
969
970                                 break;
971                         }
972                 } else {
973                         RETURN(-EINVAL);
974                 }
975
976                 if (i >= count)
977                         RETURN(-EAGAIN);
978
979                 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
980                 OBD_ALLOC_PTR(oqctl);
981                 if (!oqctl)
982                         RETURN(-ENOMEM);
983
984                 QCTL_COPY(oqctl, qctl);
985                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
986                 if (rc == 0) {
987                         QCTL_COPY(qctl, oqctl);
988                         qctl->qc_valid = QC_MDTIDX;
989                         qctl->obd_uuid = tgt->ltd_uuid;
990                 }
991                 OBD_FREE_PTR(oqctl);
992                 break;
993         }
994         case LL_IOC_GET_CONNECT_FLAGS: {
995                 tgt = lmv->tgts[0];
996                 if (tgt == NULL || tgt->ltd_exp == NULL)
997                         RETURN(-ENODATA);
998                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
999                 break;
1000         }
1001         case LL_IOC_FID2MDTIDX: {
1002                 struct lu_fid *fid = karg;
1003                 int             mdt_index;
1004
1005                 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1006                 if (rc != 0)
1007                         RETURN(rc);
1008
1009                 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1010                  * point to user space memory for FID2MDTIDX. */
1011                 *(__u32 *)uarg = mdt_index;
1012                 break;
1013         }
1014         case OBD_IOC_FID2PATH: {
1015                 rc = lmv_fid2path(exp, len, karg, uarg);
1016                 break;
1017         }
1018         case LL_IOC_HSM_STATE_GET:
1019         case LL_IOC_HSM_STATE_SET:
1020         case LL_IOC_HSM_ACTION: {
1021                 struct md_op_data       *op_data = karg;
1022
1023                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1024                 if (IS_ERR(tgt))
1025                         RETURN(PTR_ERR(tgt));
1026
1027                 if (tgt->ltd_exp == NULL)
1028                         RETURN(-EINVAL);
1029
1030                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1031                 break;
1032         }
1033         case LL_IOC_HSM_PROGRESS: {
1034                 const struct hsm_progress_kernel *hpk = karg;
1035
1036                 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1037                 if (IS_ERR(tgt))
1038                         RETURN(PTR_ERR(tgt));
1039                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1040                 break;
1041         }
1042         case LL_IOC_HSM_REQUEST: {
1043                 struct hsm_user_request *hur = karg;
1044                 unsigned int reqcount = hur->hur_request.hr_itemcount;
1045
1046                 if (reqcount == 0)
1047                         RETURN(0);
1048
1049                 /* if the request is about a single fid
1050                  * or if there is a single MDS, no need to split
1051                  * the request. */
1052                 if (reqcount == 1 || count == 1) {
1053                         tgt = lmv_find_target(lmv,
1054                                               &hur->hur_user_item[0].hui_fid);
1055                         if (IS_ERR(tgt))
1056                                 RETURN(PTR_ERR(tgt));
1057                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1058                 } else {
1059                         /* split fid list to their respective MDS */
1060                         for (i = 0; i < count; i++) {
1061                                 int nr, rc1;
1062                                 size_t reqlen;
1063                                 struct hsm_user_request *req;
1064
1065                                 tgt = lmv->tgts[i];
1066                                 if (tgt == NULL || tgt->ltd_exp == NULL)
1067                                         continue;
1068
1069                                 nr = lmv_hsm_req_count(lmv, hur, tgt);
1070                                 if (nr < 0)
1071                                         RETURN(nr);
1072                                 if (nr == 0) /* nothing for this MDS */
1073                                         continue;
1074
1075                                 /* build a request with fids for this MDS */
1076                                 reqlen = offsetof(typeof(*hur),
1077                                                   hur_user_item[nr])
1078                                                 + hur->hur_request.hr_data_len;
1079                                 OBD_ALLOC_LARGE(req, reqlen);
1080                                 if (req == NULL)
1081                                         RETURN(-ENOMEM);
1082                                 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1083                                 if (rc1 < 0)
1084                                         GOTO(hsm_req_err, rc1);
1085                                 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1086                                                     req, uarg);
1087 hsm_req_err:
1088                                 if (rc1 != 0 && rc == 0)
1089                                         rc = rc1;
1090                                 OBD_FREE_LARGE(req, reqlen);
1091                         }
1092                 }
1093                 break;
1094         }
1095         case LL_IOC_LOV_SWAP_LAYOUTS: {
1096                 struct md_op_data       *op_data = karg;
1097                 struct lmv_tgt_desc     *tgt1, *tgt2;
1098
1099                 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1100                 if (IS_ERR(tgt1))
1101                         RETURN(PTR_ERR(tgt1));
1102
1103                 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1104                 if (IS_ERR(tgt2))
1105                         RETURN(PTR_ERR(tgt2));
1106
1107                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1108                         RETURN(-EINVAL);
1109
1110                 /* only files on same MDT can have their layouts swapped */
1111                 if (tgt1->ltd_idx != tgt2->ltd_idx)
1112                         RETURN(-EPERM);
1113
1114                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1115                 break;
1116         }
1117         case LL_IOC_HSM_CT_START: {
1118                 struct lustre_kernelcomm *lk = karg;
1119                 if (lk->lk_flags & LK_FLG_STOP)
1120                         rc = lmv_hsm_ct_unregister(obddev, cmd, len, lk, uarg);
1121                 else
1122                         rc = lmv_hsm_ct_register(obddev, cmd, len, lk, uarg);
1123                 break;
1124         }
1125         default:
1126                 for (i = 0; i < count; i++) {
1127                         struct obd_device *mdc_obd;
1128                         int err;
1129
1130                         tgt = lmv->tgts[i];
1131                         if (tgt == NULL || tgt->ltd_exp == NULL)
1132                                 continue;
1133                         /* ll_umount_begin() sets force flag but for lmv, not
1134                          * mdc. Let's pass it through */
1135                         mdc_obd = class_exp2obd(tgt->ltd_exp);
1136                         mdc_obd->obd_force = obddev->obd_force;
1137                         err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1138                         if (err) {
1139                                 if (tgt->ltd_active) {
1140                                         CERROR("error: iocontrol MDC %s on MDT"
1141                                                " idx %d cmd %x: err = %d\n",
1142                                                tgt->ltd_uuid.uuid, i, cmd, err);
1143                                         if (!rc)
1144                                                 rc = err;
1145                                 }
1146                         } else
1147                                 set = 1;
1148                 }
1149                 if (!set && !rc)
1150                         rc = -EIO;
1151         }
1152         RETURN(rc);
1153 }
1154
1155 /**
1156  * This is _inode_ placement policy function (not name).
1157  */
1158 static int lmv_placement_policy(struct obd_device *obd,
1159                                 struct md_op_data *op_data, u32 *mds)
1160 {
1161         struct lmv_obd     *lmv = &obd->u.lmv;
1162         struct lmv_user_md *lum;
1163
1164         ENTRY;
1165
1166         LASSERT(mds != NULL);
1167
1168         if (lmv->desc.ld_tgt_count == 1) {
1169                 *mds = 0;
1170                 RETURN(0);
1171         }
1172
1173         lum = op_data->op_data;
1174         /* Choose MDS by
1175          * 1. See if the stripe offset is specified by lum.
1176          * 2. Then check if there is default stripe offset.
1177          * 3. Finally choose MDS by name hash if the parent
1178          *    is striped directory. (see lmv_locate_mds()). */
1179         if (op_data->op_cli_flags & CLI_SET_MEA && lum != NULL &&
1180             le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1181                 *mds = le32_to_cpu(lum->lum_stripe_offset);
1182         } else if (op_data->op_default_stripe_offset != (__u32)-1) {
1183                 *mds = op_data->op_default_stripe_offset;
1184                 op_data->op_mds = *mds;
1185                 /* Correct the stripe offset in lum */
1186                 if (lum != NULL)
1187                         lum->lum_stripe_offset = cpu_to_le32(*mds);
1188         } else {
1189                 *mds = op_data->op_mds;
1190         }
1191
1192         RETURN(0);
1193 }
1194
1195 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1196 {
1197         struct lmv_tgt_desc     *tgt;
1198         int                      rc;
1199         ENTRY;
1200
1201         tgt = lmv_get_target(lmv, mds, NULL);
1202         if (IS_ERR(tgt))
1203                 RETURN(PTR_ERR(tgt));
1204
1205         /*
1206          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1207          * on server that seq in new allocated fid is not yet known.
1208          */
1209         mutex_lock(&tgt->ltd_fid_mutex);
1210
1211         if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1212                 GOTO(out, rc = -ENODEV);
1213
1214         /*
1215          * Asking underlying tgt layer to allocate new fid.
1216          */
1217         rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1218         if (rc > 0) {
1219                 LASSERT(fid_is_sane(fid));
1220                 rc = 0;
1221         }
1222
1223         EXIT;
1224 out:
1225         mutex_unlock(&tgt->ltd_fid_mutex);
1226         return rc;
1227 }
1228
1229 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1230                   struct lu_fid *fid, struct md_op_data *op_data)
1231 {
1232         struct obd_device     *obd = class_exp2obd(exp);
1233         struct lmv_obd        *lmv = &obd->u.lmv;
1234         u32                    mds = 0;
1235         int                    rc;
1236         ENTRY;
1237
1238         LASSERT(op_data != NULL);
1239         LASSERT(fid != NULL);
1240
1241         rc = lmv_placement_policy(obd, op_data, &mds);
1242         if (rc) {
1243                 CERROR("Can't get target for allocating fid, "
1244                        "rc %d\n", rc);
1245                 RETURN(rc);
1246         }
1247
1248         rc = __lmv_fid_alloc(lmv, fid, mds);
1249         if (rc) {
1250                 CERROR("Can't alloc new fid, rc %d\n", rc);
1251                 RETURN(rc);
1252         }
1253
1254         RETURN(rc);
1255 }
1256
1257 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1258 {
1259         struct lmv_obd  *lmv = &obd->u.lmv;
1260         struct lmv_desc *desc;
1261         int             rc;
1262         ENTRY;
1263
1264         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1265                 CERROR("LMV setup requires a descriptor\n");
1266                 RETURN(-EINVAL);
1267         }
1268
1269         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1270         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1271                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1272                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1273                 RETURN(-EINVAL);
1274         }
1275
1276         lmv->tgts_size = 32U;
1277         OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1278         if (lmv->tgts == NULL)
1279                 RETURN(-ENOMEM);
1280
1281         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1282         lmv->desc.ld_tgt_count = 0;
1283         lmv->desc.ld_active_tgt_count = 0;
1284         lmv->max_def_easize = 0;
1285         lmv->max_easize = 0;
1286
1287         spin_lock_init(&lmv->lmv_lock);
1288         mutex_init(&lmv->lmv_init_mutex);
1289
1290 #ifdef CONFIG_PROC_FS
1291         obd->obd_vars = lprocfs_lmv_obd_vars;
1292         lprocfs_obd_setup(obd, true);
1293         lprocfs_alloc_md_stats(obd, 0);
1294         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1295                                 0444, &lmv_proc_target_fops, obd);
1296         if (rc)
1297                 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1298                       obd->obd_name, rc);
1299 #endif
1300         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1301                              LUSTRE_CLI_FLD_HASH_DHT);
1302         if (rc) {
1303                 CERROR("Can't init FLD, err %d\n", rc);
1304                 GOTO(out, rc);
1305         }
1306
1307         RETURN(0);
1308
1309 out:
1310         return rc;
1311 }
1312
1313 static int lmv_cleanup(struct obd_device *obd)
1314 {
1315         struct lmv_obd   *lmv = &obd->u.lmv;
1316         ENTRY;
1317
1318         fld_client_fini(&lmv->lmv_fld);
1319         if (lmv->tgts != NULL) {
1320                 int i;
1321                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1322                         if (lmv->tgts[i] == NULL)
1323                                 continue;
1324                         lmv_del_target(lmv, i);
1325                 }
1326                 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1327                 lmv->tgts_size = 0;
1328         }
1329         RETURN(0);
1330 }
1331
1332 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1333 {
1334         struct lustre_cfg       *lcfg = buf;
1335         struct obd_uuid         obd_uuid;
1336         int                     gen;
1337         __u32                   index;
1338         int                     rc;
1339         ENTRY;
1340
1341         switch (lcfg->lcfg_command) {
1342         case LCFG_ADD_MDC:
1343                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1344                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
1345                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1346                         GOTO(out, rc = -EINVAL);
1347
1348                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1349
1350                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1351                         GOTO(out, rc = -EINVAL);
1352                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1353                         GOTO(out, rc = -EINVAL);
1354                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1355                 GOTO(out, rc);
1356         default:
1357                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1358                 GOTO(out, rc = -EINVAL);
1359         }
1360 out:
1361         RETURN(rc);
1362 }
1363
1364 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1365                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1366 {
1367         struct obd_device       *obd = class_exp2obd(exp);
1368         struct lmv_obd          *lmv = &obd->u.lmv;
1369         struct obd_statfs       *temp;
1370         int                      rc = 0;
1371         __u32                    i;
1372         ENTRY;
1373
1374         OBD_ALLOC(temp, sizeof(*temp));
1375         if (temp == NULL)
1376                 RETURN(-ENOMEM);
1377
1378         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1379                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1380                         continue;
1381
1382                 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1383                                 max_age, flags);
1384                 if (rc) {
1385                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1386                                lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1387                                rc);
1388                         GOTO(out_free_temp, rc);
1389                 }
1390
1391                 if (i == 0) {
1392                         *osfs = *temp;
1393                         /* If the statfs is from mount, it will needs
1394                          * retrieve necessary information from MDT0.
1395                          * i.e. mount does not need the merged osfs
1396                          * from all of MDT.
1397                          * And also clients can be mounted as long as
1398                          * MDT0 is in service*/
1399                         if (flags & OBD_STATFS_FOR_MDT0)
1400                                 GOTO(out_free_temp, rc);
1401                 } else {
1402                         osfs->os_bavail += temp->os_bavail;
1403                         osfs->os_blocks += temp->os_blocks;
1404                         osfs->os_ffree += temp->os_ffree;
1405                         osfs->os_files += temp->os_files;
1406                 }
1407         }
1408
1409         EXIT;
1410 out_free_temp:
1411         OBD_FREE(temp, sizeof(*temp));
1412         return rc;
1413 }
1414
1415 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1416                         struct lu_fid *fid)
1417 {
1418         struct obd_device    *obd = exp->exp_obd;
1419         struct lmv_obd       *lmv = &obd->u.lmv;
1420         int                   rc;
1421         ENTRY;
1422
1423         rc = md_get_root(lmv->tgts[0]->ltd_exp, fileset, fid);
1424         RETURN(rc);
1425 }
1426
1427 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1428                         u64 valid, const char *name,
1429                         const char *input, int input_size, int output_size,
1430                         int flags, struct ptlrpc_request **request)
1431 {
1432         struct obd_device      *obd = exp->exp_obd;
1433         struct lmv_obd         *lmv = &obd->u.lmv;
1434         struct lmv_tgt_desc    *tgt;
1435         int                     rc;
1436         ENTRY;
1437
1438         tgt = lmv_find_target(lmv, fid);
1439         if (IS_ERR(tgt))
1440                 RETURN(PTR_ERR(tgt));
1441
1442         rc = md_getxattr(tgt->ltd_exp, fid, valid, name, input,
1443                          input_size, output_size, flags, request);
1444
1445         RETURN(rc);
1446 }
1447
1448 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1449                         u64 valid, const char *name,
1450                         const char *input, int input_size, int output_size,
1451                         int flags, __u32 suppgid,
1452                         struct ptlrpc_request **request)
1453 {
1454         struct obd_device      *obd = exp->exp_obd;
1455         struct lmv_obd         *lmv = &obd->u.lmv;
1456         struct lmv_tgt_desc    *tgt;
1457         int                     rc;
1458         ENTRY;
1459
1460         tgt = lmv_find_target(lmv, fid);
1461         if (IS_ERR(tgt))
1462                 RETURN(PTR_ERR(tgt));
1463
1464         rc = md_setxattr(tgt->ltd_exp, fid, valid, name, input,
1465                          input_size, output_size, flags, suppgid,
1466                          request);
1467
1468         RETURN(rc);
1469 }
1470
1471 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1472                        struct ptlrpc_request **request)
1473 {
1474         struct obd_device       *obd = exp->exp_obd;
1475         struct lmv_obd          *lmv = &obd->u.lmv;
1476         struct lmv_tgt_desc     *tgt;
1477         int                      rc;
1478         ENTRY;
1479
1480         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1481         if (IS_ERR(tgt))
1482                 RETURN(PTR_ERR(tgt));
1483
1484         if (op_data->op_flags & MF_GET_MDT_IDX) {
1485                 op_data->op_mds = tgt->ltd_idx;
1486                 RETURN(0);
1487         }
1488
1489         rc = md_getattr(tgt->ltd_exp, op_data, request);
1490
1491         RETURN(rc);
1492 }
1493
1494 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1495 {
1496         struct obd_device   *obd = exp->exp_obd;
1497         struct lmv_obd      *lmv = &obd->u.lmv;
1498         __u32                i;
1499         ENTRY;
1500
1501         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1502
1503         /*
1504          * With DNE every object can have two locks in different namespaces:
1505          * lookup lock in space of MDT storing direntry and update/open lock in
1506          * space of MDT storing inode.
1507          */
1508         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1509                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1510                         continue;
1511                 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1512         }
1513
1514         RETURN(0);
1515 }
1516
1517 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1518                      struct md_open_data *mod, struct ptlrpc_request **request)
1519 {
1520         struct obd_device     *obd = exp->exp_obd;
1521         struct lmv_obd        *lmv = &obd->u.lmv;
1522         struct lmv_tgt_desc   *tgt;
1523         int                    rc;
1524         ENTRY;
1525
1526         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1527         if (IS_ERR(tgt))
1528                 RETURN(PTR_ERR(tgt));
1529
1530         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1531         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1532         RETURN(rc);
1533 }
1534
1535 /**
1536  * Choosing the MDT by name or FID in @op_data.
1537  * For non-striped directory, it will locate MDT by fid.
1538  * For striped-directory, it will locate MDT by name. And also
1539  * it will reset op_fid1 with the FID of the choosen stripe.
1540  **/
1541 struct lmv_tgt_desc *
1542 lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1543                            const char *name, int namelen, struct lu_fid *fid,
1544                            u32 *mds)
1545 {
1546         struct lmv_tgt_desc     *tgt;
1547         const struct lmv_oinfo  *oinfo;
1548
1549         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1550                 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1551                         RETURN(ERR_PTR(-EBADF));
1552                 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1553         } else {
1554                 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
1555                 if (IS_ERR(oinfo))
1556                         RETURN(ERR_CAST(oinfo));
1557         }
1558
1559         if (fid != NULL)
1560                 *fid = oinfo->lmo_fid;
1561         if (mds != NULL)
1562                 *mds = oinfo->lmo_mds;
1563
1564         tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1565
1566         CDEBUG(D_INFO, "locate on mds %u "DFID"\n", oinfo->lmo_mds,
1567                PFID(&oinfo->lmo_fid));
1568         return tgt;
1569 }
1570
1571 /**
1572  * Locate mds by fid or name
1573  *
1574  * For striped directory (lsm != NULL), it will locate the stripe
1575  * by name hash (see lsm_name_to_stripe_info()). Note: if the hash_type
1576  * is unknown, it will return -EBADFD, and lmv_intent_lookup might need
1577  * walk through all of stripes to locate the entry.
1578  *
1579  * For normal direcotry, it will locate MDS by FID directly.
1580  * \param[in] lmv       LMV device
1581  * \param[in] op_data   client MD stack parameters, name, namelen
1582  *                      mds_num etc.
1583  * \param[in] fid       object FID used to locate MDS.
1584  *
1585  * retval               pointer to the lmv_tgt_desc if succeed.
1586  *                      ERR_PTR(errno) if failed.
1587  */
1588 struct lmv_tgt_desc*
1589 lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1590                struct lu_fid *fid)
1591 {
1592         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1593         struct lmv_tgt_desc     *tgt;
1594
1595         /* During creating VOLATILE file, it should honor the mdt
1596          * index if the file under striped dir is being restored, see
1597          * ct_restore(). */
1598         if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1599             (int)op_data->op_mds != -1) {
1600                 int i;
1601                 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1602                 if (IS_ERR(tgt))
1603                         return tgt;
1604
1605                 if (lsm != NULL) {
1606                         /* refill the right parent fid */
1607                         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1608                                 struct lmv_oinfo *oinfo;
1609
1610                                 oinfo = &lsm->lsm_md_oinfo[i];
1611                                 if (oinfo->lmo_mds == op_data->op_mds) {
1612                                         *fid = oinfo->lmo_fid;
1613                                         break;
1614                                 }
1615                         }
1616
1617                         if (i == lsm->lsm_md_stripe_count)
1618                                 *fid = lsm->lsm_md_oinfo[0].lmo_fid;
1619                 }
1620
1621                 return tgt;
1622         }
1623
1624         if (lsm == NULL || op_data->op_namelen == 0) {
1625                 tgt = lmv_find_target(lmv, fid);
1626                 if (IS_ERR(tgt))
1627                         return tgt;
1628
1629                 op_data->op_mds = tgt->ltd_idx;
1630                 return tgt;
1631         }
1632
1633         return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1634                                           op_data->op_namelen, fid,
1635                                           &op_data->op_mds);
1636 }
1637
1638 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1639                 const void *data, size_t datalen, umode_t mode, uid_t uid,
1640                 gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
1641                 struct ptlrpc_request **request)
1642 {
1643         struct obd_device       *obd = exp->exp_obd;
1644         struct lmv_obd          *lmv = &obd->u.lmv;
1645         struct lmv_tgt_desc     *tgt;
1646         int                      rc;
1647         ENTRY;
1648
1649         if (!lmv->desc.ld_active_tgt_count)
1650                 RETURN(-EIO);
1651
1652         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1653         if (IS_ERR(tgt))
1654                 RETURN(PTR_ERR(tgt));
1655
1656         CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1657                 (int)op_data->op_namelen, op_data->op_name,
1658                 PFID(&op_data->op_fid1), op_data->op_mds);
1659
1660         rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1661         if (rc)
1662                 RETURN(rc);
1663         if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1664                 /* Send the create request to the MDT where the object
1665                  * will be located */
1666                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1667                 if (IS_ERR(tgt))
1668                         RETURN(PTR_ERR(tgt));
1669
1670                 op_data->op_mds = tgt->ltd_idx;
1671         } else {
1672                 CDEBUG(D_CONFIG, "Server doesn't support striped dirs\n");
1673         }
1674
1675         CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1676                PFID(&op_data->op_fid2), op_data->op_mds);
1677
1678         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1679         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1680                        cap_effective, rdev, request);
1681         if (rc == 0) {
1682                 if (*request == NULL)
1683                         RETURN(rc);
1684                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1685         }
1686         RETURN(rc);
1687 }
1688
1689 static int
1690 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1691             const union ldlm_policy_data *policy, struct md_op_data *op_data,
1692             struct lustre_handle *lockh, __u64 extra_lock_flags)
1693 {
1694         struct obd_device        *obd = exp->exp_obd;
1695         struct lmv_obd           *lmv = &obd->u.lmv;
1696         struct lmv_tgt_desc      *tgt;
1697         int                       rc;
1698         ENTRY;
1699
1700         CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
1701
1702         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1703         if (IS_ERR(tgt))
1704                 RETURN(PTR_ERR(tgt));
1705
1706         CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
1707                PFID(&op_data->op_fid1), tgt->ltd_idx);
1708
1709         rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
1710                         extra_lock_flags);
1711
1712         RETURN(rc);
1713 }
1714
1715 static int
1716 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1717                  struct ptlrpc_request **preq)
1718 {
1719         struct ptlrpc_request   *req = NULL;
1720         struct obd_device       *obd = exp->exp_obd;
1721         struct lmv_obd          *lmv = &obd->u.lmv;
1722         struct lmv_tgt_desc     *tgt;
1723         struct mdt_body         *body;
1724         int                      rc;
1725         ENTRY;
1726
1727         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1728         if (IS_ERR(tgt))
1729                 RETURN(PTR_ERR(tgt));
1730
1731         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1732                 (int)op_data->op_namelen, op_data->op_name,
1733                 PFID(&op_data->op_fid1), tgt->ltd_idx);
1734
1735         rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1736         if (rc != 0)
1737                 RETURN(rc);
1738
1739         body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1740         LASSERT(body != NULL);
1741
1742         if (body->mbo_valid & OBD_MD_MDS) {
1743                 struct lu_fid rid = body->mbo_fid1;
1744                 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1745                        PFID(&rid));
1746
1747                 tgt = lmv_find_target(lmv, &rid);
1748                 if (IS_ERR(tgt)) {
1749                         ptlrpc_req_finished(*preq);
1750                         preq = NULL;
1751                         RETURN(PTR_ERR(tgt));
1752                 }
1753
1754                 op_data->op_fid1 = rid;
1755                 op_data->op_valid |= OBD_MD_FLCROSSREF;
1756                 op_data->op_namelen = 0;
1757                 op_data->op_name = NULL;
1758                 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1759                 ptlrpc_req_finished(*preq);
1760                 *preq = req;
1761         }
1762
1763         RETURN(rc);
1764 }
1765
1766 #define md_op_data_fid(op_data, fl)                     \
1767         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1768          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1769          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1770          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1771          NULL)
1772
1773 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1774                             struct md_op_data *op_data, __u32 op_tgt,
1775                             enum ldlm_mode mode, int bits, int flag)
1776 {
1777         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1778         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
1779         union ldlm_policy_data policy = { { 0 } };
1780         int rc = 0;
1781         ENTRY;
1782
1783         if (!fid_is_sane(fid))
1784                 RETURN(0);
1785
1786         if (tgt == NULL) {
1787                 tgt = lmv_find_target(lmv, fid);
1788                 if (IS_ERR(tgt))
1789                         RETURN(PTR_ERR(tgt));
1790         }
1791
1792         if (tgt->ltd_idx != op_tgt) {
1793                 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1794                 policy.l_inodebits.bits = bits;
1795                 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1796                                       mode, LCF_ASYNC, NULL);
1797         } else {
1798                 CDEBUG(D_INODE,
1799                        "EARLY_CANCEL skip operation target %d on "DFID"\n",
1800                        op_tgt, PFID(fid));
1801                 op_data->op_flags |= flag;
1802                 rc = 0;
1803         }
1804
1805         RETURN(rc);
1806 }
1807
1808 /*
1809  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1810  * op_data->op_fid2
1811  */
1812 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1813                     struct ptlrpc_request **request)
1814 {
1815         struct obd_device       *obd = exp->exp_obd;
1816         struct lmv_obd          *lmv = &obd->u.lmv;
1817         struct lmv_tgt_desc     *tgt;
1818         int                      rc;
1819         ENTRY;
1820
1821         LASSERT(op_data->op_namelen != 0);
1822
1823         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1824                PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1825                op_data->op_name, PFID(&op_data->op_fid1));
1826
1827         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1828         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1829         op_data->op_cap = cfs_curproc_cap_pack();
1830         if (op_data->op_mea2 != NULL) {
1831                 struct lmv_stripe_md    *lsm = op_data->op_mea2;
1832                 const struct lmv_oinfo  *oinfo;
1833
1834                 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
1835                                                 op_data->op_namelen);
1836                 if (IS_ERR(oinfo))
1837                         RETURN(PTR_ERR(oinfo));
1838
1839                 op_data->op_fid2 = oinfo->lmo_fid;
1840         }
1841
1842         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1843         if (IS_ERR(tgt))
1844                 RETURN(PTR_ERR(tgt));
1845
1846         /*
1847          * Cancel UPDATE lock on child (fid1).
1848          */
1849         op_data->op_flags |= MF_MDC_CANCEL_FID2;
1850         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
1851                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1852         if (rc != 0)
1853                 RETURN(rc);
1854
1855         rc = md_link(tgt->ltd_exp, op_data, request);
1856
1857         RETURN(rc);
1858 }
1859
1860 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1861                       const char *old, size_t oldlen,
1862                       const char *new, size_t newlen,
1863                       struct ptlrpc_request **request)
1864 {
1865         struct obd_device       *obd = exp->exp_obd;
1866         struct lmv_obd          *lmv = &obd->u.lmv;
1867         struct lmv_tgt_desc     *src_tgt;
1868         struct lmv_tgt_desc     *tgt_tgt;
1869         struct obd_export       *target_exp;
1870         struct mdt_body         *body;
1871         int                     rc;
1872         ENTRY;
1873
1874         LASSERT(oldlen != 0);
1875
1876         CDEBUG(D_INODE, "RENAME %.*s in "DFID":%d to %.*s in "DFID":%d\n",
1877                (int)oldlen, old, PFID(&op_data->op_fid1),
1878                op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
1879                (int)newlen, new, PFID(&op_data->op_fid2),
1880                op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
1881
1882         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1883         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1884         op_data->op_cap = cfs_curproc_cap_pack();
1885         if (op_data->op_cli_flags & CLI_MIGRATE) {
1886                 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
1887                          PFID(&op_data->op_fid3));
1888
1889                 if (op_data->op_mea1 != NULL) {
1890                         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1891                         struct lmv_tgt_desc     *tmp;
1892
1893                         /* Fix the parent fid for striped dir */
1894                         tmp = lmv_locate_target_for_name(lmv, lsm, old,
1895                                                          oldlen,
1896                                                          &op_data->op_fid1,
1897                                                          NULL);
1898                         if (IS_ERR(tmp))
1899                                 RETURN(PTR_ERR(tmp));
1900                 }
1901
1902                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1903                 if (rc != 0)
1904                         RETURN(rc);
1905
1906                 src_tgt = lmv_find_target(lmv, &op_data->op_fid3);
1907                 if (IS_ERR(src_tgt))
1908                         RETURN(PTR_ERR(src_tgt));
1909
1910                 target_exp = src_tgt->ltd_exp;
1911         } else {
1912                 if (op_data->op_mea1 != NULL) {
1913                         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1914
1915                         src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
1916                                                              oldlen,
1917                                                              &op_data->op_fid1,
1918                                                              &op_data->op_mds);
1919                 } else {
1920                         src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
1921                 }
1922                 if (IS_ERR(src_tgt))
1923                         RETURN(PTR_ERR(src_tgt));
1924
1925
1926                 if (op_data->op_mea2 != NULL) {
1927                         struct lmv_stripe_md    *lsm = op_data->op_mea2;
1928
1929                         tgt_tgt = lmv_locate_target_for_name(lmv, lsm, new,
1930                                                              newlen,
1931                                                              &op_data->op_fid2,
1932                                                              &op_data->op_mds);
1933                 } else {
1934                         tgt_tgt = lmv_find_target(lmv, &op_data->op_fid2);
1935
1936                 }
1937                 if (IS_ERR(tgt_tgt))
1938                         RETURN(PTR_ERR(tgt_tgt));
1939
1940                 target_exp = tgt_tgt->ltd_exp;
1941         }
1942
1943         /*
1944          * LOOKUP lock on src child (fid3) should also be cancelled for
1945          * src_tgt in mdc_rename.
1946          */
1947         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1948
1949         /*
1950          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
1951          * own target.
1952          */
1953         rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1954                               LCK_EX, MDS_INODELOCK_UPDATE,
1955                               MF_MDC_CANCEL_FID2);
1956
1957         if (rc != 0)
1958                 RETURN(rc);
1959         /*
1960          * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
1961          */
1962         if (fid_is_sane(&op_data->op_fid3)) {
1963                 struct lmv_tgt_desc *tgt;
1964
1965                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1966                 if (IS_ERR(tgt))
1967                         RETURN(PTR_ERR(tgt));
1968
1969                 /* Cancel LOOKUP lock on its parent */
1970                 rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
1971                                       LCK_EX, MDS_INODELOCK_LOOKUP,
1972                                       MF_MDC_CANCEL_FID3);
1973                 if (rc != 0)
1974                         RETURN(rc);
1975
1976                 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1977                                       LCK_EX, MDS_INODELOCK_ELC,
1978                                       MF_MDC_CANCEL_FID3);
1979                 if (rc != 0)
1980                         RETURN(rc);
1981         }
1982
1983 retry_rename:
1984         /*
1985          * Cancel all the locks on tgt child (fid4).
1986          */
1987         if (fid_is_sane(&op_data->op_fid4)) {
1988                 struct lmv_tgt_desc *tgt;
1989
1990                 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1991                                       LCK_EX, MDS_INODELOCK_ELC,
1992                                       MF_MDC_CANCEL_FID4);
1993                 if (rc != 0)
1994                         RETURN(rc);
1995
1996                 tgt = lmv_find_target(lmv, &op_data->op_fid4);
1997                 if (IS_ERR(tgt))
1998                         RETURN(PTR_ERR(tgt));
1999
2000                 /* Since the target child might be destroyed, and it might
2001                  * become orphan, and we can only check orphan on the local
2002                  * MDT right now, so we send rename request to the MDT where
2003                  * target child is located. If target child does not exist,
2004                  * then it will send the request to the target parent */
2005                 target_exp = tgt->ltd_exp;
2006         }
2007
2008         rc = md_rename(target_exp, op_data, old, oldlen, new, newlen,
2009                        request);
2010
2011         if (rc != 0 && rc != -EXDEV)
2012                 RETURN(rc);
2013
2014         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2015         if (body == NULL)
2016                 RETURN(-EPROTO);
2017
2018         /* Not cross-ref case, just get out of here. */
2019         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2020                 RETURN(rc);
2021
2022         CDEBUG(D_INODE, "%s: try rename to another MDT for "DFID"\n",
2023                exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2024
2025         op_data->op_fid4 = body->mbo_fid1;
2026         ptlrpc_req_finished(*request);
2027         *request = NULL;
2028         goto retry_rename;
2029 }
2030
2031 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2032                        void *ea, size_t ealen, struct ptlrpc_request **request)
2033 {
2034         struct obd_device       *obd = exp->exp_obd;
2035         struct lmv_obd          *lmv = &obd->u.lmv;
2036         struct lmv_tgt_desc     *tgt;
2037         int                      rc = 0;
2038         ENTRY;
2039
2040         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2041                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2042
2043         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2044         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2045         if (IS_ERR(tgt))
2046                 RETURN(PTR_ERR(tgt));
2047
2048         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2049
2050         RETURN(rc);
2051 }
2052
2053 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2054                      struct ptlrpc_request **request)
2055 {
2056         struct obd_device       *obd = exp->exp_obd;
2057         struct lmv_obd          *lmv = &obd->u.lmv;
2058         struct lmv_tgt_desc     *tgt;
2059         int                      rc;
2060         ENTRY;
2061
2062         tgt = lmv_find_target(lmv, fid);
2063         if (IS_ERR(tgt))
2064                 RETURN(PTR_ERR(tgt));
2065
2066         rc = md_fsync(tgt->ltd_exp, fid, request);
2067         RETURN(rc);
2068 }
2069
2070 struct stripe_dirent {
2071         struct page             *sd_page;
2072         struct lu_dirpage       *sd_dp;
2073         struct lu_dirent        *sd_ent;
2074         bool                     sd_eof;
2075 };
2076
2077 struct lmv_dir_ctxt {
2078         struct lmv_obd          *ldc_lmv;
2079         struct md_op_data       *ldc_op_data;
2080         struct md_callback      *ldc_cb_op;
2081         __u64                    ldc_hash;
2082         int                      ldc_count;
2083         struct stripe_dirent     ldc_stripes[0];
2084 };
2085
2086 static inline void put_stripe_dirent(struct stripe_dirent *stripe)
2087 {
2088         if (stripe->sd_page) {
2089                 kunmap(stripe->sd_page);
2090                 put_page(stripe->sd_page);
2091                 stripe->sd_page = NULL;
2092                 stripe->sd_ent = NULL;
2093         }
2094 }
2095
2096 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2097 {
2098         int i;
2099
2100         for (i = 0; i < ctxt->ldc_count; i++)
2101                 put_stripe_dirent(&ctxt->ldc_stripes[i]);
2102 }
2103
2104 static struct lu_dirent *stripe_dirent_next(struct lmv_dir_ctxt *ctxt,
2105                                             struct stripe_dirent *stripe,
2106                                             int stripe_index)
2107 {
2108         struct lu_dirent *ent = stripe->sd_ent;
2109         __u64 hash = ctxt->ldc_hash;
2110         __u64 end;
2111         int rc = 0;
2112         ENTRY;
2113
2114         LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2115
2116         if (stripe->sd_eof)
2117                 RETURN(NULL);
2118
2119         if (ent) {
2120                 ent = lu_dirent_next(ent);
2121                 if (!ent) {
2122 check_eof:
2123                         end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2124                         LASSERTF(hash <= end, "hash %llx end %llx\n",
2125                                  hash, end);
2126                         if (end == MDS_DIR_END_OFF) {
2127                                 stripe->sd_ent = NULL;
2128                                 stripe->sd_eof = true;
2129                                 RETURN(NULL);
2130                         }
2131
2132                         put_stripe_dirent(stripe);
2133                         hash = end;
2134                 }
2135         }
2136
2137         if (!ent) {
2138                 struct md_op_data *op_data = ctxt->ldc_op_data;
2139                 struct lmv_oinfo *oinfo;
2140                 struct lu_fid fid = op_data->op_fid1;
2141                 struct inode *inode = op_data->op_data;
2142                 struct lmv_tgt_desc *tgt;
2143
2144                 LASSERT(!stripe->sd_page);
2145
2146                 oinfo = &op_data->op_mea1->lsm_md_oinfo[stripe_index];
2147                 tgt = lmv_get_target(ctxt->ldc_lmv, oinfo->lmo_mds, NULL);
2148                 if (IS_ERR(tgt))
2149                         GOTO(out, rc = PTR_ERR(tgt));
2150
2151                 /* op_data will be shared by each stripe, so we need
2152                  * reset these value for each stripe */
2153                 op_data->op_fid1 = oinfo->lmo_fid;
2154                 op_data->op_fid2 = oinfo->lmo_fid;
2155                 op_data->op_data = oinfo->lmo_root;
2156
2157                 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_cb_op, hash,
2158                                   &stripe->sd_page);
2159
2160                 op_data->op_fid1 = fid;
2161                 op_data->op_fid2 = fid;
2162                 op_data->op_data = inode;
2163
2164                 if (rc)
2165                         GOTO(out, rc);
2166
2167                 stripe->sd_dp = page_address(stripe->sd_page);
2168                 ent = lu_dirent_start(stripe->sd_dp);
2169         }
2170
2171         for (; ent; ent = lu_dirent_next(ent)) {
2172                 /* Skip dummy entry */
2173                 if (le16_to_cpu(ent->lde_namelen) == 0)
2174                         continue;
2175
2176                 /* skip . and .. for other stripes */
2177                 if (stripe_index &&
2178                     (strncmp(ent->lde_name, ".",
2179                              le16_to_cpu(ent->lde_namelen)) == 0 ||
2180                      strncmp(ent->lde_name, "..",
2181                              le16_to_cpu(ent->lde_namelen)) == 0))
2182                         continue;
2183
2184                 if (le64_to_cpu(ent->lde_hash) >= hash)
2185                         break;
2186         }
2187
2188         if (!ent)
2189                 goto check_eof;
2190         EXIT;
2191
2192 out:
2193         stripe->sd_ent = ent;
2194         /* treat error as eof, so dir can be partially accessed */
2195         if (rc) {
2196                 put_stripe_dirent(stripe);
2197                 stripe->sd_eof = true;
2198                 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, "
2199                               "directory is partially accessed!\n",
2200                               PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
2201                               rc);
2202         }
2203         return ent;
2204 }
2205
2206 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
2207 {
2208         struct obd_device       *obd = exp->exp_obd;
2209         struct lmv_obd          *lmv = &obd->u.lmv;
2210         struct lmv_tgt_desc     *tgt;
2211         int                      rc;
2212         ENTRY;
2213
2214         rc = lmv_check_connect(obd);
2215         if (rc != 0)
2216                 RETURN(rc);
2217
2218         tgt = lmv_find_target(lmv, &data->op_fid1);
2219         if (IS_ERR(tgt))
2220                 RETURN(PTR_ERR(tgt));
2221
2222         data->op_flags |= MF_MDC_CANCEL_FID1;
2223         rc = md_file_resync(tgt->ltd_exp, data);
2224         RETURN(rc);
2225 }
2226
2227 /**
2228  * Get dirent with the closest hash for striped directory
2229  *
2230  * This function will search the dir entry, whose hash value is the
2231  * closest(>=) to hash from all of sub-stripes, and it is only being called
2232  * for striped directory.
2233  *
2234  * \param[in] ctxt              dir read context
2235  *
2236  * \retval                      dirent get the entry successfully
2237  *                              NULL does not get the entry, normally it means
2238  *                              it reaches the end of the directory, while read
2239  *                              stripe dirent error is ignored to allow partial
2240  *                              access.
2241  */
2242 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
2243 {
2244         struct stripe_dirent *stripe;
2245         struct lu_dirent *ent = NULL;
2246         int i;
2247         int min = -1;
2248
2249         /* TODO: optimize with k-way merge sort */
2250         for (i = 0; i < ctxt->ldc_count; i++) {
2251                 stripe = &ctxt->ldc_stripes[i];
2252                 if (stripe->sd_eof)
2253                         continue;
2254
2255                 if (!stripe->sd_ent) {
2256                         /* locate starting entry */
2257                         stripe_dirent_next(ctxt, stripe, i);
2258                         if (!stripe->sd_ent) {
2259                                 LASSERT(stripe->sd_eof);
2260                                 continue;
2261                         }
2262                 }
2263
2264                 if (min == -1 ||
2265                     le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
2266                     le64_to_cpu(stripe->sd_ent->lde_hash)) {
2267                         min = i;
2268                         if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
2269                             ctxt->ldc_hash)
2270                                 break;
2271                 }
2272         }
2273
2274         if (min != -1) {
2275                 stripe = &ctxt->ldc_stripes[min];
2276                 ent = stripe->sd_ent;
2277                 /* pop found dirent */
2278                 stripe_dirent_next(ctxt, stripe, min);
2279         }
2280
2281         return ent;
2282 }
2283
2284 /**
2285  * Build dir entry page for striped directory
2286  *
2287  * This function gets one entry by @offset from a striped directory. It will
2288  * read entries from all of stripes, and choose one closest to the required
2289  * offset(&offset). A few notes
2290  * 1. skip . and .. for non-zero stripes, because there can only have one .
2291  * and .. in a directory.
2292  * 2. op_data will be shared by all of stripes, instead of allocating new
2293  * one, so need to restore before reusing.
2294  *
2295  * \param[in] exp       obd export refer to LMV
2296  * \param[in] op_data   hold those MD parameters of read_entry
2297  * \param[in] cb_op     ldlm callback being used in enqueue in mdc_read_entry
2298  * \param[in] offset    starting hash offset
2299  * \param[out] ppage    the page holding the entry. Note: because the entry
2300  *                      will be accessed in upper layer, so we need hold the
2301  *                      page until the usages of entry is finished, see
2302  *                      ll_dir_entry_next.
2303  *
2304  * retval               =0 if get entry successfully
2305  *                      <0 cannot get entry
2306  */
2307 static int lmv_striped_read_page(struct obd_export *exp,
2308                                  struct md_op_data *op_data,
2309                                  struct md_callback *cb_op,
2310                                  __u64 offset, struct page **ppage)
2311 {
2312         struct page *page = NULL;
2313         struct lu_dirpage *dp;
2314         void *start;
2315         struct lu_dirent *ent;
2316         struct lu_dirent *last_ent;
2317         int stripe_count;
2318         struct lmv_dir_ctxt *ctxt;
2319         struct lu_dirent *next = NULL;
2320         __u16 ent_size;
2321         size_t left_bytes;
2322         int rc = 0;
2323         ENTRY;
2324
2325         /* Allocate a page and read entries from all of stripes and fill
2326          * the page by hash order */
2327         page = alloc_page(GFP_KERNEL);
2328         if (!page)
2329                 RETURN(-ENOMEM);
2330
2331         /* Initialize the entry page */
2332         dp = kmap(page);
2333         memset(dp, 0, sizeof(*dp));
2334         dp->ldp_hash_start = cpu_to_le64(offset);
2335
2336         start = dp + 1;
2337         left_bytes = PAGE_SIZE - sizeof(*dp);
2338         ent = start;
2339         last_ent = ent;
2340
2341         /* initalize dir read context */
2342         stripe_count = op_data->op_mea1->lsm_md_stripe_count;
2343         OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2344         if (!ctxt)
2345                 GOTO(free_page, rc = -ENOMEM);
2346         ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
2347         ctxt->ldc_op_data = op_data;
2348         ctxt->ldc_cb_op = cb_op;
2349         ctxt->ldc_hash = offset;
2350         ctxt->ldc_count = stripe_count;
2351
2352         while (1) {
2353                 next = lmv_dirent_next(ctxt);
2354
2355                 /* end of directory */
2356                 if (!next) {
2357                         ctxt->ldc_hash = MDS_DIR_END_OFF;
2358                         break;
2359                 }
2360                 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
2361
2362                 ent_size = le16_to_cpu(next->lde_reclen);
2363
2364                 /* the last entry lde_reclen is 0, but it might not be the last
2365                  * one of this temporay dir page */
2366                 if (!ent_size)
2367                         ent_size = lu_dirent_calc_size(
2368                                         le16_to_cpu(next->lde_namelen),
2369                                         le32_to_cpu(next->lde_attrs));
2370                 /* page full */
2371                 if (ent_size > left_bytes)
2372                         break;
2373
2374                 memcpy(ent, next, ent_size);
2375
2376                 /* Replace . with master FID and Replace .. with the parent FID
2377                  * of master object */
2378                 if (strncmp(ent->lde_name, ".",
2379                             le16_to_cpu(ent->lde_namelen)) == 0 &&
2380                     le16_to_cpu(ent->lde_namelen) == 1)
2381                         fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
2382                 else if (strncmp(ent->lde_name, "..",
2383                                    le16_to_cpu(ent->lde_namelen)) == 0 &&
2384                            le16_to_cpu(ent->lde_namelen) == 2)
2385                         fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2386
2387                 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
2388                        le16_to_cpu(ent->lde_namelen), ent->lde_name,
2389                        le64_to_cpu(ent->lde_hash));
2390
2391                 left_bytes -= ent_size;
2392                 ent->lde_reclen = cpu_to_le16(ent_size);
2393                 last_ent = ent;
2394                 ent = (void *)ent + ent_size;
2395         };
2396
2397         last_ent->lde_reclen = 0;
2398
2399         if (ent == start)
2400                 dp->ldp_flags |= LDF_EMPTY;
2401         else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
2402                 dp->ldp_flags |= LDF_COLLIDE;
2403         dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2404         dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
2405
2406         put_lmv_dir_ctxt(ctxt);
2407         OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2408
2409         *ppage = page;
2410
2411         RETURN(0);
2412
2413 free_page:
2414         kunmap(page);
2415         __free_page(page);
2416
2417         return rc;
2418 }
2419
2420 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2421                   struct md_callback *cb_op, __u64 offset,
2422                   struct page **ppage)
2423 {
2424         struct obd_device       *obd = exp->exp_obd;
2425         struct lmv_obd          *lmv = &obd->u.lmv;
2426         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2427         struct lmv_tgt_desc     *tgt;
2428         int                     rc;
2429         ENTRY;
2430
2431         if (unlikely(lsm != NULL)) {
2432                 rc = lmv_striped_read_page(exp, op_data, cb_op, offset, ppage);
2433                 RETURN(rc);
2434         }
2435
2436         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2437         if (IS_ERR(tgt))
2438                 RETURN(PTR_ERR(tgt));
2439
2440         rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2441
2442         RETURN(rc);
2443 }
2444
2445 /**
2446  * Unlink a file/directory
2447  *
2448  * Unlink a file or directory under the parent dir. The unlink request
2449  * usually will be sent to the MDT where the child is located, but if
2450  * the client does not have the child FID then request will be sent to the
2451  * MDT where the parent is located.
2452  *
2453  * If the parent is a striped directory then it also needs to locate which
2454  * stripe the name of the child is located, and replace the parent FID
2455  * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2456  * it will walk through all of sub-stripes until the child is being
2457  * unlinked finally.
2458  *
2459  * \param[in] exp       export refer to LMV
2460  * \param[in] op_data   different parameters transferred beween client
2461  *                      MD stacks, name, namelen, FIDs etc.
2462  *                      op_fid1 is the parent FID, op_fid2 is the child
2463  *                      FID.
2464  * \param[out] request  point to the request of unlink.
2465  *
2466  * retval               0 if succeed
2467  *                      negative errno if failed.
2468  */
2469 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2470                       struct ptlrpc_request **request)
2471 {
2472         struct obd_device       *obd = exp->exp_obd;
2473         struct lmv_obd          *lmv = &obd->u.lmv;
2474         struct lmv_tgt_desc     *tgt = NULL;
2475         struct lmv_tgt_desc     *parent_tgt = NULL;
2476         struct mdt_body         *body;
2477         int                     rc;
2478         int                     stripe_index = 0;
2479         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2480         ENTRY;
2481
2482 retry_unlink:
2483         /* For striped dir, we need to locate the parent as well */
2484         if (lsm != NULL) {
2485                 struct lmv_tgt_desc *tmp;
2486
2487                 LASSERT(op_data->op_name != NULL &&
2488                         op_data->op_namelen != 0);
2489
2490                 tmp = lmv_locate_target_for_name(lmv, lsm,
2491                                                  op_data->op_name,
2492                                                  op_data->op_namelen,
2493                                                  &op_data->op_fid1,
2494                                                  &op_data->op_mds);
2495
2496                 /* return -EBADFD means unknown hash type, might
2497                  * need try all sub-stripe here */
2498                 if (IS_ERR(tmp) && PTR_ERR(tmp) != -EBADFD)
2499                         RETURN(PTR_ERR(tmp));
2500
2501                 /* Note: both migrating dir and unknown hash dir need to
2502                  * try all of sub-stripes, so we need start search the
2503                  * name from stripe 0, but migrating dir is already handled
2504                  * inside lmv_locate_target_for_name(), so we only check
2505                  * unknown hash type directory here */
2506                 if (!lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
2507                         struct lmv_oinfo *oinfo;
2508
2509                         oinfo = &lsm->lsm_md_oinfo[stripe_index];
2510
2511                         op_data->op_fid1 = oinfo->lmo_fid;
2512                         op_data->op_mds = oinfo->lmo_mds;
2513                 }
2514         }
2515
2516 try_next_stripe:
2517         /* Send unlink requests to the MDT where the child is located */
2518         if (likely(!fid_is_zero(&op_data->op_fid2)))
2519                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2520         else if (lsm != NULL)
2521                 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
2522         else
2523                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2524
2525         if (IS_ERR(tgt))
2526                 RETURN(PTR_ERR(tgt));
2527
2528         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2529         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2530         op_data->op_cap = cfs_curproc_cap_pack();
2531
2532         /*
2533          * If child's fid is given, cancel unused locks for it if it is from
2534          * another export than parent.
2535          *
2536          * LOOKUP lock for child (fid3) should also be cancelled on parent
2537          * tgt_tgt in mdc_unlink().
2538          */
2539         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2540
2541         /*
2542          * Cancel FULL locks on child (fid3).
2543          */
2544         parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2545         if (IS_ERR(parent_tgt))
2546                 RETURN(PTR_ERR(parent_tgt));
2547
2548         if (parent_tgt != tgt) {
2549                 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2550                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2551                                       MF_MDC_CANCEL_FID3);
2552         }
2553
2554         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2555                               MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2556         if (rc != 0)
2557                 RETURN(rc);
2558
2559         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2560                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2561
2562         rc = md_unlink(tgt->ltd_exp, op_data, request);
2563         if (rc != 0 && rc != -EREMOTE && rc != -ENOENT)
2564                 RETURN(rc);
2565
2566         /* Try next stripe if it is needed. */
2567         if (rc == -ENOENT && lsm != NULL && lmv_need_try_all_stripes(lsm)) {
2568                 struct lmv_oinfo *oinfo;
2569
2570                 stripe_index++;
2571                 if (stripe_index >= lsm->lsm_md_stripe_count)
2572                         RETURN(rc);
2573
2574                 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2575
2576                 op_data->op_fid1 = oinfo->lmo_fid;
2577                 op_data->op_mds = oinfo->lmo_mds;
2578
2579                 ptlrpc_req_finished(*request);
2580                 *request = NULL;
2581
2582                 goto try_next_stripe;
2583         }
2584
2585         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2586         if (body == NULL)
2587                 RETURN(-EPROTO);
2588
2589         /* Not cross-ref case, just get out of here. */
2590         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2591                 RETURN(rc);
2592
2593         CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2594                exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2595
2596         /* This is a remote object, try remote MDT, Note: it may
2597          * try more than 1 time here, Considering following case
2598          * /mnt/lustre is root on MDT0, remote1 is on MDT1
2599          * 1. Initially A does not know where remote1 is, it send
2600          *    unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2601          *    resend unlink RPC to MDT1 (retry 1st time).
2602          *
2603          * 2. During the unlink RPC in flight,
2604          *    client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2605          *    and create new remote1, but on MDT0
2606          *
2607          * 3. MDT1 get unlink RPC(from A), then do remote lock on
2608          *    /mnt/lustre, then lookup get fid of remote1, and find
2609          *    it is remote dir again, and replay -EREMOTE again.
2610          *
2611          * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2612          *
2613          * In theory, it might try unlimited time here, but it should
2614          * be very rare case.  */
2615         op_data->op_fid2 = body->mbo_fid1;
2616         ptlrpc_req_finished(*request);
2617         *request = NULL;
2618
2619         goto retry_unlink;
2620 }
2621
2622 static int lmv_precleanup(struct obd_device *obd)
2623 {
2624         ENTRY;
2625         libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
2626         fld_client_proc_fini(&obd->u.lmv.lmv_fld);
2627         lprocfs_obd_cleanup(obd);
2628         lprocfs_free_md_stats(obd);
2629         RETURN(0);
2630 }
2631
2632 /**
2633  * Get by key a value associated with a LMV device.
2634  *
2635  * Dispatch request to lower-layer devices as needed.
2636  *
2637  * \param[in] env               execution environment for this thread
2638  * \param[in] exp               export for the LMV device
2639  * \param[in] keylen            length of key identifier
2640  * \param[in] key               identifier of key to get value for
2641  * \param[in] vallen            size of \a val
2642  * \param[out] val              pointer to storage location for value
2643  * \param[in] lsm               optional striping metadata of object
2644  *
2645  * \retval 0            on success
2646  * \retval negative     negated errno on failure
2647  */
2648 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2649                         __u32 keylen, void *key, __u32 *vallen, void *val)
2650 {
2651         struct obd_device       *obd;
2652         struct lmv_obd          *lmv;
2653         int                      rc = 0;
2654         ENTRY;
2655
2656         obd = class_exp2obd(exp);
2657         if (obd == NULL) {
2658                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2659                        exp->exp_handle.h_cookie);
2660                 RETURN(-EINVAL);
2661         }
2662
2663         lmv = &obd->u.lmv;
2664         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2665                 int i;
2666
2667                 LASSERT(*vallen == sizeof(__u32));
2668                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2669                         struct lmv_tgt_desc *tgt = lmv->tgts[i];
2670                         /*
2671                          * All tgts should be connected when this gets called.
2672                          */
2673                         if (tgt == NULL || tgt->ltd_exp == NULL)
2674                                 continue;
2675
2676                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2677                                           vallen, val))
2678                                 RETURN(0);
2679                 }
2680                 RETURN(-EINVAL);
2681         } else if (KEY_IS(KEY_MAX_EASIZE) ||
2682                    KEY_IS(KEY_DEFAULT_EASIZE) ||
2683                    KEY_IS(KEY_CONN_DATA)) {
2684                 /*
2685                  * Forwarding this request to first MDS, it should know LOV
2686                  * desc.
2687                  */
2688                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2689                                   vallen, val);
2690                 if (!rc && KEY_IS(KEY_CONN_DATA))
2691                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2692                 RETURN(rc);
2693         } else if (KEY_IS(KEY_TGT_COUNT)) {
2694                 *((int *)val) = lmv->desc.ld_tgt_count;
2695                 RETURN(0);
2696         }
2697
2698         CDEBUG(D_IOCTL, "Invalid key\n");
2699         RETURN(-EINVAL);
2700 }
2701
2702 /**
2703  * Asynchronously set by key a value associated with a LMV device.
2704  *
2705  * Dispatch request to lower-layer devices as needed.
2706  *
2707  * \param[in] env       execution environment for this thread
2708  * \param[in] exp       export for the LMV device
2709  * \param[in] keylen    length of key identifier
2710  * \param[in] key       identifier of key to store value for
2711  * \param[in] vallen    size of value to store
2712  * \param[in] val       pointer to data to be stored
2713  * \param[in] set       optional list of related ptlrpc requests
2714  *
2715  * \retval 0            on success
2716  * \retval negative     negated errno on failure
2717  */
2718 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2719                         __u32 keylen, void *key, __u32 vallen, void *val,
2720                         struct ptlrpc_request_set *set)
2721 {
2722         struct lmv_tgt_desc     *tgt = NULL;
2723         struct obd_device       *obd;
2724         struct lmv_obd          *lmv;
2725         int rc = 0;
2726         ENTRY;
2727
2728         obd = class_exp2obd(exp);
2729         if (obd == NULL) {
2730                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2731                        exp->exp_handle.h_cookie);
2732                 RETURN(-EINVAL);
2733         }
2734         lmv = &obd->u.lmv;
2735
2736         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2737             KEY_IS(KEY_DEFAULT_EASIZE)) {
2738                 int i, err = 0;
2739
2740                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2741                         tgt = lmv->tgts[i];
2742
2743                         if (tgt == NULL || tgt->ltd_exp == NULL)
2744                                 continue;
2745
2746                         err = obd_set_info_async(env, tgt->ltd_exp,
2747                                                  keylen, key, vallen, val, set);
2748                         if (err && rc == 0)
2749                                 rc = err;
2750                 }
2751
2752                 RETURN(rc);
2753         }
2754
2755         RETURN(-EINVAL);
2756 }
2757
2758 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2759                             const struct lmv_mds_md_v1 *lmm1)
2760 {
2761         struct lmv_obd  *lmv = &exp->exp_obd->u.lmv;
2762         int             stripe_count;
2763         int             cplen;
2764         int             i;
2765         int             rc = 0;
2766         ENTRY;
2767
2768         lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2769         lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2770         lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2771         if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2772                 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2773         else
2774                 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2775         lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2776         cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2777                         sizeof(lsm->lsm_md_pool_name));
2778
2779         if (cplen >= sizeof(lsm->lsm_md_pool_name))
2780                 RETURN(-E2BIG);
2781
2782         CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d"
2783                "layout_version %d\n", lsm->lsm_md_stripe_count,
2784                lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
2785                lsm->lsm_md_layout_version);
2786
2787         stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2788         for (i = 0; i < stripe_count; i++) {
2789                 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2790                               &lmm1->lmv_stripe_fids[i]);
2791                 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2792                                     &lsm->lsm_md_oinfo[i].lmo_mds);
2793                 if (rc != 0)
2794                         RETURN(rc);
2795                 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
2796                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2797         }
2798
2799         RETURN(rc);
2800 }
2801
2802 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2803                         const union lmv_mds_md *lmm, size_t lmm_size)
2804 {
2805         struct lmv_stripe_md     *lsm;
2806         int                      lsm_size;
2807         int                      rc;
2808         bool                     allocated = false;
2809         ENTRY;
2810
2811         LASSERT(lsmp != NULL);
2812
2813         lsm = *lsmp;
2814         /* Free memmd */
2815         if (lsm != NULL && lmm == NULL) {
2816                 int i;
2817                 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
2818                         /* For migrating inode, the master stripe and master
2819                          * object will be the same, so do not need iput, see
2820                          * ll_update_lsm_md */
2821                         if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
2822                               i == 0) && lsm->lsm_md_oinfo[i].lmo_root != NULL)
2823                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
2824                 }
2825                 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
2826                 OBD_FREE(lsm, lsm_size);
2827                 *lsmp = NULL;
2828                 RETURN(0);
2829         }
2830
2831         if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
2832                 RETURN(-EPERM);
2833
2834         /* Unpack memmd */
2835         if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2836             le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2837                 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2838                        exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2839                        -EIO);
2840                 RETURN(-EIO);
2841         }
2842
2843         if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2844                 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2845         else
2846                 /**
2847                  * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2848                  * stripecount should be 0 then.
2849                  */
2850                 lsm_size = lmv_stripe_md_size(0);
2851
2852         lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2853         if (lsm == NULL) {
2854                 OBD_ALLOC(lsm, lsm_size);
2855                 if (lsm == NULL)
2856                         RETURN(-ENOMEM);
2857                 allocated = true;
2858                 *lsmp = lsm;
2859         }
2860
2861         switch (le32_to_cpu(lmm->lmv_magic)) {
2862         case LMV_MAGIC_V1:
2863                 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
2864                 break;
2865         default:
2866                 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
2867                        le32_to_cpu(lmm->lmv_magic));
2868                 rc = -EINVAL;
2869                 break;
2870         }
2871
2872         if (rc != 0 && allocated) {
2873                 OBD_FREE(lsm, lsm_size);
2874                 *lsmp = NULL;
2875                 lsm_size = rc;
2876         }
2877         RETURN(lsm_size);
2878 }
2879
2880 void lmv_free_memmd(struct lmv_stripe_md *lsm)
2881 {
2882         lmv_unpackmd(NULL, &lsm, NULL, 0);
2883 }
2884 EXPORT_SYMBOL(lmv_free_memmd);
2885
2886 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2887                              union ldlm_policy_data *policy,
2888                              enum ldlm_mode mode, enum ldlm_cancel_flags flags,
2889                              void *opaque)
2890 {
2891         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2892         int rc = 0;
2893         __u32 i;
2894         ENTRY;
2895
2896         LASSERT(fid != NULL);
2897
2898         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2899                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2900                 int err;
2901
2902                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2903                         continue;
2904
2905                 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
2906                                        opaque);
2907                 if (!rc)
2908                         rc = err;
2909         }
2910         RETURN(rc);
2911 }
2912
2913 static int lmv_set_lock_data(struct obd_export *exp,
2914                              const struct lustre_handle *lockh,
2915                              void *data, __u64 *bits)
2916 {
2917         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2918         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2919         int                      rc;
2920         ENTRY;
2921
2922         if (tgt == NULL || tgt->ltd_exp == NULL)
2923                 RETURN(-EINVAL);
2924         rc =  md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
2925         RETURN(rc);
2926 }
2927
2928 enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
2929                               const struct lu_fid *fid, enum ldlm_type type,
2930                               union ldlm_policy_data *policy,
2931                               enum ldlm_mode mode, struct lustre_handle *lockh)
2932 {
2933         struct obd_device       *obd = exp->exp_obd;
2934         struct lmv_obd          *lmv = &obd->u.lmv;
2935         enum ldlm_mode          rc;
2936         int                     tgt;
2937         int                     i;
2938         ENTRY;
2939
2940         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2941
2942         /*
2943          * With DNE every object can have two locks in different namespaces:
2944          * lookup lock in space of MDT storing direntry and update/open lock in
2945          * space of MDT storing inode.  Try the MDT that the FID maps to first,
2946          * since this can be easily found, and only try others if that fails.
2947          */
2948         for (i = 0, tgt = lmv_find_target_index(lmv, fid);
2949              i < lmv->desc.ld_tgt_count;
2950              i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
2951                 if (tgt < 0) {
2952                         CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
2953                                obd->obd_name, PFID(fid), tgt);
2954                         tgt = 0;
2955                 }
2956
2957                 if (lmv->tgts[tgt] == NULL ||
2958                     lmv->tgts[tgt]->ltd_exp == NULL ||
2959                     lmv->tgts[tgt]->ltd_active == 0)
2960                         continue;
2961
2962                 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
2963                                    type, policy, mode, lockh);
2964                 if (rc)
2965                         RETURN(rc);
2966         }
2967
2968         RETURN(0);
2969 }
2970
2971 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2972                       struct obd_export *dt_exp, struct obd_export *md_exp,
2973                       struct lustre_md *md)
2974 {
2975         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2976         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2977
2978         if (tgt == NULL || tgt->ltd_exp == NULL)
2979                 RETURN(-EINVAL);
2980
2981         return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2982 }
2983
2984 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2985 {
2986         struct obd_device       *obd = exp->exp_obd;
2987         struct lmv_obd          *lmv = &obd->u.lmv;
2988         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2989         ENTRY;
2990
2991         if (md->lmv != NULL) {
2992                 lmv_free_memmd(md->lmv);
2993                 md->lmv = NULL;
2994         }
2995         if (tgt == NULL || tgt->ltd_exp == NULL)
2996                 RETURN(-EINVAL);
2997         RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
2998 }
2999
3000 int lmv_set_open_replay_data(struct obd_export *exp,
3001                              struct obd_client_handle *och,
3002                              struct lookup_intent *it)
3003 {
3004         struct obd_device       *obd = exp->exp_obd;
3005         struct lmv_obd          *lmv = &obd->u.lmv;
3006         struct lmv_tgt_desc     *tgt;
3007         ENTRY;
3008
3009         tgt = lmv_find_target(lmv, &och->och_fid);
3010         if (IS_ERR(tgt))
3011                 RETURN(PTR_ERR(tgt));
3012
3013         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3014 }
3015
3016 int lmv_clear_open_replay_data(struct obd_export *exp,
3017                                struct obd_client_handle *och)
3018 {
3019         struct obd_device       *obd = exp->exp_obd;
3020         struct lmv_obd          *lmv = &obd->u.lmv;
3021         struct lmv_tgt_desc     *tgt;
3022         ENTRY;
3023
3024         tgt = lmv_find_target(lmv, &och->och_fid);
3025         if (IS_ERR(tgt))
3026                 RETURN(PTR_ERR(tgt));
3027
3028         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3029 }
3030
3031 int lmv_intent_getattr_async(struct obd_export *exp,
3032                              struct md_enqueue_info *minfo)
3033 {
3034         struct md_op_data *op_data = &minfo->mi_data;
3035         struct obd_device *obd = exp->exp_obd;
3036         struct lmv_obd *lmv = &obd->u.lmv;
3037         struct lmv_tgt_desc *tgt = NULL;
3038         int rc;
3039         ENTRY;
3040
3041         if (!fid_is_sane(&op_data->op_fid2))
3042                 RETURN(-EINVAL);
3043
3044         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
3045         if (IS_ERR(tgt))
3046                 RETURN(PTR_ERR(tgt));
3047
3048         /*
3049          * no special handle for remote dir, which needs to fetch both LOOKUP
3050          * lock on parent, and then UPDATE lock on child MDT, which makes all
3051          * complicated because this is done async. So only LOOKUP lock is
3052          * fetched for remote dir, but considering remote dir is rare case,
3053          * and not supporting it in statahead won't cause any issue, just leave
3054          * it as is.
3055          */
3056
3057         rc = md_intent_getattr_async(tgt->ltd_exp, minfo);
3058         RETURN(rc);
3059 }
3060
3061 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3062                         struct lu_fid *fid, __u64 *bits)
3063 {
3064         struct obd_device       *obd = exp->exp_obd;
3065         struct lmv_obd          *lmv = &obd->u.lmv;
3066         struct lmv_tgt_desc     *tgt;
3067         int                      rc;
3068         ENTRY;
3069
3070         tgt = lmv_find_target(lmv, fid);
3071         if (IS_ERR(tgt))
3072                 RETURN(PTR_ERR(tgt));
3073
3074         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3075         RETURN(rc);
3076 }
3077
3078 int lmv_get_fid_from_lsm(struct obd_export *exp,
3079                          const struct lmv_stripe_md *lsm,
3080                          const char *name, int namelen, struct lu_fid *fid)
3081 {
3082         const struct lmv_oinfo *oinfo;
3083
3084         LASSERT(lsm != NULL);
3085         oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
3086         if (IS_ERR(oinfo))
3087                 return PTR_ERR(oinfo);
3088
3089         *fid = oinfo->lmo_fid;
3090
3091         RETURN(0);
3092 }
3093
3094 /**
3095  * For lmv, only need to send request to master MDT, and the master MDT will
3096  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3097  * we directly fetch data from the slave MDTs.
3098  */
3099 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3100                  struct obd_quotactl *oqctl)
3101 {
3102         struct obd_device   *obd = class_exp2obd(exp);
3103         struct lmv_obd      *lmv = &obd->u.lmv;
3104         struct lmv_tgt_desc *tgt = lmv->tgts[0];
3105         int                  rc = 0;
3106         __u32                i;
3107         __u64                curspace, curinodes;
3108         ENTRY;
3109
3110         if (tgt == NULL ||
3111             tgt->ltd_exp == NULL ||
3112             !tgt->ltd_active ||
3113             lmv->desc.ld_tgt_count == 0) {
3114                 CERROR("master lmv inactive\n");
3115                 RETURN(-EIO);
3116         }
3117
3118         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3119                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3120                 RETURN(rc);
3121         }
3122
3123         curspace = curinodes = 0;
3124         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3125                 int err;
3126                 tgt = lmv->tgts[i];
3127
3128                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3129                         continue;
3130
3131                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3132                 if (err) {
3133                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3134                         if (!rc)
3135                                 rc = err;
3136                 } else {
3137                         curspace += oqctl->qc_dqblk.dqb_curspace;
3138                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3139                 }
3140         }
3141         oqctl->qc_dqblk.dqb_curspace = curspace;
3142         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3143
3144         RETURN(rc);
3145 }
3146
3147 static int lmv_merge_attr(struct obd_export *exp,
3148                           const struct lmv_stripe_md *lsm,
3149                           struct cl_attr *attr,
3150                           ldlm_blocking_callback cb_blocking)
3151 {
3152         int rc;
3153         int i;
3154
3155         rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3156         if (rc < 0)
3157                 return rc;
3158
3159         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3160                 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3161
3162                 CDEBUG(D_INFO, ""DFID" size %llu, blocks %llu nlink %u,"
3163                        " atime %lu ctime %lu, mtime %lu.\n",
3164                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3165                        i_size_read(inode), (unsigned long long)inode->i_blocks,
3166                        inode->i_nlink, LTIME_S(inode->i_atime),
3167                        LTIME_S(inode->i_ctime), LTIME_S(inode->i_mtime));
3168
3169                 /* for slave stripe, it needs to subtract nlink for . and .. */
3170                 if (i != 0)
3171                         attr->cat_nlink += inode->i_nlink - 2;
3172                 else
3173                         attr->cat_nlink = inode->i_nlink;
3174
3175                 attr->cat_size += i_size_read(inode);
3176                 attr->cat_blocks += inode->i_blocks;
3177
3178                 if (attr->cat_atime < LTIME_S(inode->i_atime))
3179                         attr->cat_atime = LTIME_S(inode->i_atime);
3180
3181                 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3182                         attr->cat_ctime = LTIME_S(inode->i_ctime);
3183
3184                 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3185                         attr->cat_mtime = LTIME_S(inode->i_mtime);
3186         }
3187         return 0;
3188 }
3189
3190 struct obd_ops lmv_obd_ops = {
3191         .o_owner                = THIS_MODULE,
3192         .o_setup                = lmv_setup,
3193         .o_cleanup              = lmv_cleanup,
3194         .o_precleanup           = lmv_precleanup,
3195         .o_process_config       = lmv_process_config,
3196         .o_connect              = lmv_connect,
3197         .o_disconnect           = lmv_disconnect,
3198         .o_statfs               = lmv_statfs,
3199         .o_get_info             = lmv_get_info,
3200         .o_set_info_async       = lmv_set_info_async,
3201         .o_notify               = lmv_notify,
3202         .o_get_uuid             = lmv_get_uuid,
3203         .o_iocontrol            = lmv_iocontrol,
3204         .o_quotactl             = lmv_quotactl
3205 };
3206
3207 struct md_ops lmv_md_ops = {
3208         .m_get_root             = lmv_get_root,
3209         .m_null_inode           = lmv_null_inode,
3210         .m_close                = lmv_close,
3211         .m_create               = lmv_create,
3212         .m_enqueue              = lmv_enqueue,
3213         .m_getattr              = lmv_getattr,
3214         .m_getxattr             = lmv_getxattr,
3215         .m_getattr_name         = lmv_getattr_name,
3216         .m_intent_lock          = lmv_intent_lock,
3217         .m_link                 = lmv_link,
3218         .m_rename               = lmv_rename,
3219         .m_setattr              = lmv_setattr,
3220         .m_setxattr             = lmv_setxattr,
3221         .m_fsync                = lmv_fsync,
3222         .m_file_resync          = lmv_file_resync,
3223         .m_read_page            = lmv_read_page,
3224         .m_unlink               = lmv_unlink,
3225         .m_init_ea_size         = lmv_init_ea_size,
3226         .m_cancel_unused        = lmv_cancel_unused,
3227         .m_set_lock_data        = lmv_set_lock_data,
3228         .m_lock_match           = lmv_lock_match,
3229         .m_get_lustre_md        = lmv_get_lustre_md,
3230         .m_free_lustre_md       = lmv_free_lustre_md,
3231         .m_merge_attr           = lmv_merge_attr,
3232         .m_set_open_replay_data = lmv_set_open_replay_data,
3233         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3234         .m_intent_getattr_async = lmv_intent_getattr_async,
3235         .m_revalidate_lock      = lmv_revalidate_lock,
3236         .m_get_fid_from_lsm     = lmv_get_fid_from_lsm,
3237         .m_unpackmd             = lmv_unpackmd,
3238 };
3239
3240 static int __init lmv_init(void)
3241 {
3242         return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3243                                    LUSTRE_LMV_NAME, NULL);
3244 }
3245
3246 static void __exit lmv_exit(void)
3247 {
3248         class_unregister_type(LUSTRE_LMV_NAME);
3249 }
3250
3251 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3252 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3253 MODULE_VERSION(LUSTRE_VERSION_STRING);
3254 MODULE_LICENSE("GPL");
3255
3256 module_init(lmv_init);
3257 module_exit(lmv_exit);