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