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