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