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