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