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