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