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