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