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