Whamcloud - gitweb
LU-8403 obd: remove unused data parameter from obd_notify()
[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 <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 OBD_IOC_CHANGELOG_SEND:
989         case OBD_IOC_CHANGELOG_CLEAR: {
990                 struct ioc_changelog *icc = karg;
991
992                 if (icc->icc_mdtindex >= count)
993                         RETURN(-ENODEV);
994
995                 tgt = lmv->tgts[icc->icc_mdtindex];
996                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
997                         RETURN(-ENODEV);
998                 rc = obd_iocontrol(cmd, tgt->ltd_exp, sizeof(*icc), icc, NULL);
999                 break;
1000         }
1001         case LL_IOC_GET_CONNECT_FLAGS: {
1002                 tgt = lmv->tgts[0];
1003                 if (tgt == NULL || tgt->ltd_exp == NULL)
1004                         RETURN(-ENODATA);
1005                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1006                 break;
1007         }
1008         case LL_IOC_FID2MDTIDX: {
1009                 struct lu_fid *fid = karg;
1010                 int             mdt_index;
1011
1012                 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1013                 if (rc != 0)
1014                         RETURN(rc);
1015
1016                 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1017                  * point to user space memory for FID2MDTIDX. */
1018                 *(__u32 *)uarg = mdt_index;
1019                 break;
1020         }
1021         case OBD_IOC_FID2PATH: {
1022                 rc = lmv_fid2path(exp, len, karg, uarg);
1023                 break;
1024         }
1025         case LL_IOC_HSM_STATE_GET:
1026         case LL_IOC_HSM_STATE_SET:
1027         case LL_IOC_HSM_ACTION: {
1028                 struct md_op_data       *op_data = karg;
1029
1030                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1031                 if (IS_ERR(tgt))
1032                         RETURN(PTR_ERR(tgt));
1033
1034                 if (tgt->ltd_exp == NULL)
1035                         RETURN(-EINVAL);
1036
1037                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1038                 break;
1039         }
1040         case LL_IOC_HSM_PROGRESS: {
1041                 const struct hsm_progress_kernel *hpk = karg;
1042
1043                 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1044                 if (IS_ERR(tgt))
1045                         RETURN(PTR_ERR(tgt));
1046                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1047                 break;
1048         }
1049         case LL_IOC_HSM_REQUEST: {
1050                 struct hsm_user_request *hur = karg;
1051                 unsigned int reqcount = hur->hur_request.hr_itemcount;
1052
1053                 if (reqcount == 0)
1054                         RETURN(0);
1055
1056                 /* if the request is about a single fid
1057                  * or if there is a single MDS, no need to split
1058                  * the request. */
1059                 if (reqcount == 1 || count == 1) {
1060                         tgt = lmv_find_target(lmv,
1061                                               &hur->hur_user_item[0].hui_fid);
1062                         if (IS_ERR(tgt))
1063                                 RETURN(PTR_ERR(tgt));
1064                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1065                 } else {
1066                         /* split fid list to their respective MDS */
1067                         for (i = 0; i < count; i++) {
1068                                 int nr, rc1;
1069                                 size_t reqlen;
1070                                 struct hsm_user_request *req;
1071
1072                                 tgt = lmv->tgts[i];
1073                                 if (tgt == NULL || tgt->ltd_exp == NULL)
1074                                         continue;
1075
1076                                 nr = lmv_hsm_req_count(lmv, hur, tgt);
1077                                 if (nr < 0)
1078                                         RETURN(nr);
1079                                 if (nr == 0) /* nothing for this MDS */
1080                                         continue;
1081
1082                                 /* build a request with fids for this MDS */
1083                                 reqlen = offsetof(typeof(*hur),
1084                                                   hur_user_item[nr])
1085                                                 + hur->hur_request.hr_data_len;
1086                                 OBD_ALLOC_LARGE(req, reqlen);
1087                                 if (req == NULL)
1088                                         RETURN(-ENOMEM);
1089                                 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1090                                 if (rc1 < 0)
1091                                         GOTO(hsm_req_err, rc1);
1092                                 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1093                                                     req, uarg);
1094 hsm_req_err:
1095                                 if (rc1 != 0 && rc == 0)
1096                                         rc = rc1;
1097                                 OBD_FREE_LARGE(req, reqlen);
1098                         }
1099                 }
1100                 break;
1101         }
1102         case LL_IOC_LOV_SWAP_LAYOUTS: {
1103                 struct md_op_data       *op_data = karg;
1104                 struct lmv_tgt_desc     *tgt1, *tgt2;
1105
1106                 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1107                 if (IS_ERR(tgt1))
1108                         RETURN(PTR_ERR(tgt1));
1109
1110                 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1111                 if (IS_ERR(tgt2))
1112                         RETURN(PTR_ERR(tgt2));
1113
1114                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1115                         RETURN(-EINVAL);
1116
1117                 /* only files on same MDT can have their layouts swapped */
1118                 if (tgt1->ltd_idx != tgt2->ltd_idx)
1119                         RETURN(-EPERM);
1120
1121                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1122                 break;
1123         }
1124         case LL_IOC_HSM_CT_START: {
1125                 struct lustre_kernelcomm *lk = karg;
1126                 if (lk->lk_flags & LK_FLG_STOP)
1127                         rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1128                 else
1129                         rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1130                 break;
1131         }
1132         default:
1133                 for (i = 0; i < count; i++) {
1134                         struct obd_device *mdc_obd;
1135                         int err;
1136
1137                         tgt = lmv->tgts[i];
1138                         if (tgt == NULL || tgt->ltd_exp == NULL)
1139                                 continue;
1140                         /* ll_umount_begin() sets force flag but for lmv, not
1141                          * mdc. Let's pass it through */
1142                         mdc_obd = class_exp2obd(tgt->ltd_exp);
1143                         mdc_obd->obd_force = obddev->obd_force;
1144                         err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1145                         if (err) {
1146                                 if (tgt->ltd_active) {
1147                                         CERROR("error: iocontrol MDC %s on MDT"
1148                                                " idx %d cmd %x: err = %d\n",
1149                                                tgt->ltd_uuid.uuid, i, cmd, err);
1150                                         if (!rc)
1151                                                 rc = err;
1152                                 }
1153                         } else
1154                                 set = 1;
1155                 }
1156                 if (!set && !rc)
1157                         rc = -EIO;
1158         }
1159         RETURN(rc);
1160 }
1161
1162 /**
1163  * This is _inode_ placement policy function (not name).
1164  */
1165 static int lmv_placement_policy(struct obd_device *obd,
1166                                 struct md_op_data *op_data, u32 *mds)
1167 {
1168         struct lmv_obd     *lmv = &obd->u.lmv;
1169         struct lmv_user_md *lum;
1170
1171         ENTRY;
1172
1173         LASSERT(mds != NULL);
1174
1175         if (lmv->desc.ld_tgt_count == 1) {
1176                 *mds = 0;
1177                 RETURN(0);
1178         }
1179
1180         lum = op_data->op_data;
1181         /* Choose MDS by
1182          * 1. See if the stripe offset is specified by lum.
1183          * 2. Then check if there is default stripe offset.
1184          * 3. Finally choose MDS by name hash if the parent
1185          *    is striped directory. (see lmv_locate_mds()). */
1186         if (op_data->op_cli_flags & CLI_SET_MEA && lum != NULL &&
1187             le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1188                 *mds = le32_to_cpu(lum->lum_stripe_offset);
1189         } else if (op_data->op_default_stripe_offset != (__u32)-1) {
1190                 *mds = op_data->op_default_stripe_offset;
1191                 op_data->op_mds = *mds;
1192                 /* Correct the stripe offset in lum */
1193                 if (lum != NULL)
1194                         lum->lum_stripe_offset = cpu_to_le32(*mds);
1195         } else {
1196                 *mds = op_data->op_mds;
1197         }
1198
1199         RETURN(0);
1200 }
1201
1202 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1203 {
1204         struct lmv_tgt_desc     *tgt;
1205         int                      rc;
1206         ENTRY;
1207
1208         tgt = lmv_get_target(lmv, mds, NULL);
1209         if (IS_ERR(tgt))
1210                 RETURN(PTR_ERR(tgt));
1211
1212         /*
1213          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1214          * on server that seq in new allocated fid is not yet known.
1215          */
1216         mutex_lock(&tgt->ltd_fid_mutex);
1217
1218         if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1219                 GOTO(out, rc = -ENODEV);
1220
1221         /*
1222          * Asking underlying tgt layer to allocate new fid.
1223          */
1224         rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1225         if (rc > 0) {
1226                 LASSERT(fid_is_sane(fid));
1227                 rc = 0;
1228         }
1229
1230         EXIT;
1231 out:
1232         mutex_unlock(&tgt->ltd_fid_mutex);
1233         return rc;
1234 }
1235
1236 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1237                   struct lu_fid *fid, struct md_op_data *op_data)
1238 {
1239         struct obd_device     *obd = class_exp2obd(exp);
1240         struct lmv_obd        *lmv = &obd->u.lmv;
1241         u32                    mds = 0;
1242         int                    rc;
1243         ENTRY;
1244
1245         LASSERT(op_data != NULL);
1246         LASSERT(fid != NULL);
1247
1248         rc = lmv_placement_policy(obd, op_data, &mds);
1249         if (rc) {
1250                 CERROR("Can't get target for allocating fid, "
1251                        "rc %d\n", rc);
1252                 RETURN(rc);
1253         }
1254
1255         rc = __lmv_fid_alloc(lmv, fid, mds);
1256         if (rc) {
1257                 CERROR("Can't alloc new fid, rc %d\n", rc);
1258                 RETURN(rc);
1259         }
1260
1261         RETURN(rc);
1262 }
1263
1264 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1265 {
1266         struct lmv_obd  *lmv = &obd->u.lmv;
1267         struct lmv_desc *desc;
1268         int             rc;
1269         ENTRY;
1270
1271         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1272                 CERROR("LMV setup requires a descriptor\n");
1273                 RETURN(-EINVAL);
1274         }
1275
1276         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1277         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1278                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1279                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1280                 RETURN(-EINVAL);
1281         }
1282
1283         lmv->tgts_size = 32U;
1284         OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1285         if (lmv->tgts == NULL)
1286                 RETURN(-ENOMEM);
1287
1288         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1289         lmv->desc.ld_tgt_count = 0;
1290         lmv->desc.ld_active_tgt_count = 0;
1291         lmv->max_def_easize = 0;
1292         lmv->max_easize = 0;
1293
1294         spin_lock_init(&lmv->lmv_lock);
1295         mutex_init(&lmv->lmv_init_mutex);
1296
1297 #ifdef CONFIG_PROC_FS
1298         obd->obd_vars = lprocfs_lmv_obd_vars;
1299         lprocfs_obd_setup(obd);
1300         lprocfs_alloc_md_stats(obd, 0);
1301         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1302                                 0444, &lmv_proc_target_fops, obd);
1303         if (rc)
1304                 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1305                       obd->obd_name, rc);
1306 #endif
1307         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1308                              LUSTRE_CLI_FLD_HASH_DHT);
1309         if (rc) {
1310                 CERROR("Can't init FLD, err %d\n", rc);
1311                 GOTO(out, rc);
1312         }
1313
1314         RETURN(0);
1315
1316 out:
1317         return rc;
1318 }
1319
1320 static int lmv_cleanup(struct obd_device *obd)
1321 {
1322         struct lmv_obd   *lmv = &obd->u.lmv;
1323         ENTRY;
1324
1325         fld_client_fini(&lmv->lmv_fld);
1326         if (lmv->tgts != NULL) {
1327                 int i;
1328                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1329                         if (lmv->tgts[i] == NULL)
1330                                 continue;
1331                         lmv_del_target(lmv, i);
1332                 }
1333                 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1334                 lmv->tgts_size = 0;
1335         }
1336         RETURN(0);
1337 }
1338
1339 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1340 {
1341         struct lustre_cfg       *lcfg = buf;
1342         struct obd_uuid         obd_uuid;
1343         int                     gen;
1344         __u32                   index;
1345         int                     rc;
1346         ENTRY;
1347
1348         switch (lcfg->lcfg_command) {
1349         case LCFG_ADD_MDC:
1350                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1351                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
1352                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1353                         GOTO(out, rc = -EINVAL);
1354
1355                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1356
1357                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1358                         GOTO(out, rc = -EINVAL);
1359                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1360                         GOTO(out, rc = -EINVAL);
1361                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1362                 GOTO(out, rc);
1363         default:
1364                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1365                 GOTO(out, rc = -EINVAL);
1366         }
1367 out:
1368         RETURN(rc);
1369 }
1370
1371 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1372                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1373 {
1374         struct obd_device       *obd = class_exp2obd(exp);
1375         struct lmv_obd          *lmv = &obd->u.lmv;
1376         struct obd_statfs       *temp;
1377         int                      rc = 0;
1378         __u32                    i;
1379         ENTRY;
1380
1381         OBD_ALLOC(temp, sizeof(*temp));
1382         if (temp == NULL)
1383                 RETURN(-ENOMEM);
1384
1385         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1386                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1387                         continue;
1388
1389                 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1390                                 max_age, flags);
1391                 if (rc) {
1392                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1393                                lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1394                                rc);
1395                         GOTO(out_free_temp, rc);
1396                 }
1397
1398                 if (i == 0) {
1399                         *osfs = *temp;
1400                         /* If the statfs is from mount, it will needs
1401                          * retrieve necessary information from MDT0.
1402                          * i.e. mount does not need the merged osfs
1403                          * from all of MDT.
1404                          * And also clients can be mounted as long as
1405                          * MDT0 is in service*/
1406                         if (flags & OBD_STATFS_FOR_MDT0)
1407                                 GOTO(out_free_temp, rc);
1408                 } else {
1409                         osfs->os_bavail += temp->os_bavail;
1410                         osfs->os_blocks += temp->os_blocks;
1411                         osfs->os_ffree += temp->os_ffree;
1412                         osfs->os_files += temp->os_files;
1413                 }
1414         }
1415
1416         EXIT;
1417 out_free_temp:
1418         OBD_FREE(temp, sizeof(*temp));
1419         return rc;
1420 }
1421
1422 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1423                         struct lu_fid *fid)
1424 {
1425         struct obd_device    *obd = exp->exp_obd;
1426         struct lmv_obd       *lmv = &obd->u.lmv;
1427         int                   rc;
1428         ENTRY;
1429
1430         rc = md_get_root(lmv->tgts[0]->ltd_exp, fileset, fid);
1431         RETURN(rc);
1432 }
1433
1434 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1435                         u64 valid, const char *name,
1436                         const char *input, int input_size, int output_size,
1437                         int flags, struct ptlrpc_request **request)
1438 {
1439         struct obd_device      *obd = exp->exp_obd;
1440         struct lmv_obd         *lmv = &obd->u.lmv;
1441         struct lmv_tgt_desc    *tgt;
1442         int                     rc;
1443         ENTRY;
1444
1445         tgt = lmv_find_target(lmv, fid);
1446         if (IS_ERR(tgt))
1447                 RETURN(PTR_ERR(tgt));
1448
1449         rc = md_getxattr(tgt->ltd_exp, fid, valid, name, input,
1450                          input_size, output_size, flags, request);
1451
1452         RETURN(rc);
1453 }
1454
1455 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1456                         u64 valid, const char *name,
1457                         const char *input, int input_size, int output_size,
1458                         int flags, __u32 suppgid,
1459                         struct ptlrpc_request **request)
1460 {
1461         struct obd_device      *obd = exp->exp_obd;
1462         struct lmv_obd         *lmv = &obd->u.lmv;
1463         struct lmv_tgt_desc    *tgt;
1464         int                     rc;
1465         ENTRY;
1466
1467         tgt = lmv_find_target(lmv, fid);
1468         if (IS_ERR(tgt))
1469                 RETURN(PTR_ERR(tgt));
1470
1471         rc = md_setxattr(tgt->ltd_exp, fid, valid, name, input,
1472                          input_size, output_size, flags, suppgid,
1473                          request);
1474
1475         RETURN(rc);
1476 }
1477
1478 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1479                        struct ptlrpc_request **request)
1480 {
1481         struct obd_device       *obd = exp->exp_obd;
1482         struct lmv_obd          *lmv = &obd->u.lmv;
1483         struct lmv_tgt_desc     *tgt;
1484         int                      rc;
1485         ENTRY;
1486
1487         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1488         if (IS_ERR(tgt))
1489                 RETURN(PTR_ERR(tgt));
1490
1491         if (op_data->op_flags & MF_GET_MDT_IDX) {
1492                 op_data->op_mds = tgt->ltd_idx;
1493                 RETURN(0);
1494         }
1495
1496         rc = md_getattr(tgt->ltd_exp, op_data, request);
1497
1498         RETURN(rc);
1499 }
1500
1501 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1502 {
1503         struct obd_device   *obd = exp->exp_obd;
1504         struct lmv_obd      *lmv = &obd->u.lmv;
1505         __u32                i;
1506         ENTRY;
1507
1508         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1509
1510         /*
1511          * With DNE every object can have two locks in different namespaces:
1512          * lookup lock in space of MDT storing direntry and update/open lock in
1513          * space of MDT storing inode.
1514          */
1515         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1516                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1517                         continue;
1518                 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1519         }
1520
1521         RETURN(0);
1522 }
1523
1524 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1525                      struct md_open_data *mod, struct ptlrpc_request **request)
1526 {
1527         struct obd_device     *obd = exp->exp_obd;
1528         struct lmv_obd        *lmv = &obd->u.lmv;
1529         struct lmv_tgt_desc   *tgt;
1530         int                    rc;
1531         ENTRY;
1532
1533         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1534         if (IS_ERR(tgt))
1535                 RETURN(PTR_ERR(tgt));
1536
1537         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1538         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1539         RETURN(rc);
1540 }
1541
1542 /**
1543  * Choosing the MDT by name or FID in @op_data.
1544  * For non-striped directory, it will locate MDT by fid.
1545  * For striped-directory, it will locate MDT by name. And also
1546  * it will reset op_fid1 with the FID of the choosen stripe.
1547  **/
1548 struct lmv_tgt_desc *
1549 lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1550                            const char *name, int namelen, struct lu_fid *fid,
1551                            u32 *mds)
1552 {
1553         struct lmv_tgt_desc     *tgt;
1554         const struct lmv_oinfo  *oinfo;
1555
1556         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1557                 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1558                         RETURN(ERR_PTR(-EBADF));
1559                 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1560         } else {
1561                 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
1562                 if (IS_ERR(oinfo))
1563                         RETURN(ERR_CAST(oinfo));
1564         }
1565
1566         if (fid != NULL)
1567                 *fid = oinfo->lmo_fid;
1568         if (mds != NULL)
1569                 *mds = oinfo->lmo_mds;
1570
1571         tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1572
1573         CDEBUG(D_INFO, "locate on mds %u "DFID"\n", oinfo->lmo_mds,
1574                PFID(&oinfo->lmo_fid));
1575         return tgt;
1576 }
1577
1578 /**
1579  * Locate mds by fid or name
1580  *
1581  * For striped directory (lsm != NULL), it will locate the stripe
1582  * by name hash (see lsm_name_to_stripe_info()). Note: if the hash_type
1583  * is unknown, it will return -EBADFD, and lmv_intent_lookup might need
1584  * walk through all of stripes to locate the entry.
1585  *
1586  * For normal direcotry, it will locate MDS by FID directly.
1587  * \param[in] lmv       LMV device
1588  * \param[in] op_data   client MD stack parameters, name, namelen
1589  *                      mds_num etc.
1590  * \param[in] fid       object FID used to locate MDS.
1591  *
1592  * retval               pointer to the lmv_tgt_desc if succeed.
1593  *                      ERR_PTR(errno) if failed.
1594  */
1595 struct lmv_tgt_desc*
1596 lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1597                struct lu_fid *fid)
1598 {
1599         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1600         struct lmv_tgt_desc     *tgt;
1601
1602         /* During creating VOLATILE file, it should honor the mdt
1603          * index if the file under striped dir is being restored, see
1604          * ct_restore(). */
1605         if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1606             (int)op_data->op_mds != -1) {
1607                 int i;
1608                 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1609                 if (IS_ERR(tgt))
1610                         return tgt;
1611
1612                 if (lsm != NULL) {
1613                         /* refill the right parent fid */
1614                         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1615                                 struct lmv_oinfo *oinfo;
1616
1617                                 oinfo = &lsm->lsm_md_oinfo[i];
1618                                 if (oinfo->lmo_mds == op_data->op_mds) {
1619                                         *fid = oinfo->lmo_fid;
1620                                         break;
1621                                 }
1622                         }
1623
1624                         if (i == lsm->lsm_md_stripe_count)
1625                                 *fid = lsm->lsm_md_oinfo[0].lmo_fid;
1626                 }
1627
1628                 return tgt;
1629         }
1630
1631         if (lsm == NULL || op_data->op_namelen == 0) {
1632                 tgt = lmv_find_target(lmv, fid);
1633                 if (IS_ERR(tgt))
1634                         return tgt;
1635
1636                 op_data->op_mds = tgt->ltd_idx;
1637                 return tgt;
1638         }
1639
1640         return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1641                                           op_data->op_namelen, fid,
1642                                           &op_data->op_mds);
1643 }
1644
1645 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1646                 const void *data, size_t datalen, umode_t mode, uid_t uid,
1647                 gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
1648                 struct ptlrpc_request **request)
1649 {
1650         struct obd_device       *obd = exp->exp_obd;
1651         struct lmv_obd          *lmv = &obd->u.lmv;
1652         struct lmv_tgt_desc     *tgt;
1653         int                      rc;
1654         ENTRY;
1655
1656         if (!lmv->desc.ld_active_tgt_count)
1657                 RETURN(-EIO);
1658
1659         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1660         if (IS_ERR(tgt))
1661                 RETURN(PTR_ERR(tgt));
1662
1663         CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1664                 (int)op_data->op_namelen, op_data->op_name,
1665                 PFID(&op_data->op_fid1), op_data->op_mds);
1666
1667         rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1668         if (rc)
1669                 RETURN(rc);
1670         if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1671                 /* Send the create request to the MDT where the object
1672                  * will be located */
1673                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1674                 if (IS_ERR(tgt))
1675                         RETURN(PTR_ERR(tgt));
1676
1677                 op_data->op_mds = tgt->ltd_idx;
1678         } else {
1679                 CDEBUG(D_CONFIG, "Server doesn't support striped dirs\n");
1680         }
1681
1682         CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1683                PFID(&op_data->op_fid2), op_data->op_mds);
1684
1685         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1686         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1687                        cap_effective, rdev, request);
1688         if (rc == 0) {
1689                 if (*request == NULL)
1690                         RETURN(rc);
1691                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1692         }
1693         RETURN(rc);
1694 }
1695
1696 static int
1697 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1698             const union ldlm_policy_data *policy, struct md_op_data *op_data,
1699             struct lustre_handle *lockh, __u64 extra_lock_flags)
1700 {
1701         struct obd_device        *obd = exp->exp_obd;
1702         struct lmv_obd           *lmv = &obd->u.lmv;
1703         struct lmv_tgt_desc      *tgt;
1704         int                       rc;
1705         ENTRY;
1706
1707         CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
1708
1709         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1710         if (IS_ERR(tgt))
1711                 RETURN(PTR_ERR(tgt));
1712
1713         CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
1714                PFID(&op_data->op_fid1), tgt->ltd_idx);
1715
1716         rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
1717                         extra_lock_flags);
1718
1719         RETURN(rc);
1720 }
1721
1722 static int
1723 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1724                  struct ptlrpc_request **preq)
1725 {
1726         struct ptlrpc_request   *req = NULL;
1727         struct obd_device       *obd = exp->exp_obd;
1728         struct lmv_obd          *lmv = &obd->u.lmv;
1729         struct lmv_tgt_desc     *tgt;
1730         struct mdt_body         *body;
1731         int                      rc;
1732         ENTRY;
1733
1734         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1735         if (IS_ERR(tgt))
1736                 RETURN(PTR_ERR(tgt));
1737
1738         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1739                 (int)op_data->op_namelen, op_data->op_name,
1740                 PFID(&op_data->op_fid1), tgt->ltd_idx);
1741
1742         rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1743         if (rc != 0)
1744                 RETURN(rc);
1745
1746         body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1747         LASSERT(body != NULL);
1748
1749         if (body->mbo_valid & OBD_MD_MDS) {
1750                 struct lu_fid rid = body->mbo_fid1;
1751                 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1752                        PFID(&rid));
1753
1754                 tgt = lmv_find_target(lmv, &rid);
1755                 if (IS_ERR(tgt)) {
1756                         ptlrpc_req_finished(*preq);
1757                         preq = NULL;
1758                         RETURN(PTR_ERR(tgt));
1759                 }
1760
1761                 op_data->op_fid1 = rid;
1762                 op_data->op_valid |= OBD_MD_FLCROSSREF;
1763                 op_data->op_namelen = 0;
1764                 op_data->op_name = NULL;
1765                 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1766                 ptlrpc_req_finished(*preq);
1767                 *preq = req;
1768         }
1769
1770         RETURN(rc);
1771 }
1772
1773 #define md_op_data_fid(op_data, fl)                     \
1774         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1775          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1776          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1777          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1778          NULL)
1779
1780 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1781                             struct md_op_data *op_data, __u32 op_tgt,
1782                             enum ldlm_mode mode, int bits, int flag)
1783 {
1784         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1785         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
1786         union ldlm_policy_data policy = { { 0 } };
1787         int rc = 0;
1788         ENTRY;
1789
1790         if (!fid_is_sane(fid))
1791                 RETURN(0);
1792
1793         if (tgt == NULL) {
1794                 tgt = lmv_find_target(lmv, fid);
1795                 if (IS_ERR(tgt))
1796                         RETURN(PTR_ERR(tgt));
1797         }
1798
1799         if (tgt->ltd_idx != op_tgt) {
1800                 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1801                 policy.l_inodebits.bits = bits;
1802                 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1803                                       mode, LCF_ASYNC, NULL);
1804         } else {
1805                 CDEBUG(D_INODE,
1806                        "EARLY_CANCEL skip operation target %d on "DFID"\n",
1807                        op_tgt, PFID(fid));
1808                 op_data->op_flags |= flag;
1809                 rc = 0;
1810         }
1811
1812         RETURN(rc);
1813 }
1814
1815 /*
1816  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1817  * op_data->op_fid2
1818  */
1819 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1820                     struct ptlrpc_request **request)
1821 {
1822         struct obd_device       *obd = exp->exp_obd;
1823         struct lmv_obd          *lmv = &obd->u.lmv;
1824         struct lmv_tgt_desc     *tgt;
1825         int                      rc;
1826         ENTRY;
1827
1828         LASSERT(op_data->op_namelen != 0);
1829
1830         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1831                PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1832                op_data->op_name, PFID(&op_data->op_fid1));
1833
1834         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1835         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1836         op_data->op_cap = cfs_curproc_cap_pack();
1837         if (op_data->op_mea2 != NULL) {
1838                 struct lmv_stripe_md    *lsm = op_data->op_mea2;
1839                 const struct lmv_oinfo  *oinfo;
1840
1841                 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
1842                                                 op_data->op_namelen);
1843                 if (IS_ERR(oinfo))
1844                         RETURN(PTR_ERR(oinfo));
1845
1846                 op_data->op_fid2 = oinfo->lmo_fid;
1847         }
1848
1849         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1850         if (IS_ERR(tgt))
1851                 RETURN(PTR_ERR(tgt));
1852
1853         /*
1854          * Cancel UPDATE lock on child (fid1).
1855          */
1856         op_data->op_flags |= MF_MDC_CANCEL_FID2;
1857         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
1858                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1859         if (rc != 0)
1860                 RETURN(rc);
1861
1862         rc = md_link(tgt->ltd_exp, op_data, request);
1863
1864         RETURN(rc);
1865 }
1866
1867 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1868                       const char *old, size_t oldlen,
1869                       const char *new, size_t newlen,
1870                       struct ptlrpc_request **request)
1871 {
1872         struct obd_device       *obd = exp->exp_obd;
1873         struct lmv_obd          *lmv = &obd->u.lmv;
1874         struct lmv_tgt_desc     *src_tgt;
1875         struct lmv_tgt_desc     *tgt_tgt;
1876         struct obd_export       *target_exp;
1877         struct mdt_body         *body;
1878         int                     rc;
1879         ENTRY;
1880
1881         LASSERT(oldlen != 0);
1882
1883         CDEBUG(D_INODE, "RENAME %.*s in "DFID":%d to %.*s in "DFID":%d\n",
1884                (int)oldlen, old, PFID(&op_data->op_fid1),
1885                op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
1886                (int)newlen, new, PFID(&op_data->op_fid2),
1887                op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
1888
1889         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1890         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1891         op_data->op_cap = cfs_curproc_cap_pack();
1892         if (op_data->op_cli_flags & CLI_MIGRATE) {
1893                 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
1894                          PFID(&op_data->op_fid3));
1895
1896                 if (op_data->op_mea1 != NULL) {
1897                         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1898                         struct lmv_tgt_desc     *tmp;
1899
1900                         /* Fix the parent fid for striped dir */
1901                         tmp = lmv_locate_target_for_name(lmv, lsm, old,
1902                                                          oldlen,
1903                                                          &op_data->op_fid1,
1904                                                          NULL);
1905                         if (IS_ERR(tmp))
1906                                 RETURN(PTR_ERR(tmp));
1907                 }
1908
1909                 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1910                 if (rc != 0)
1911                         RETURN(rc);
1912
1913                 src_tgt = lmv_find_target(lmv, &op_data->op_fid3);
1914                 if (IS_ERR(src_tgt))
1915                         RETURN(PTR_ERR(src_tgt));
1916
1917                 target_exp = src_tgt->ltd_exp;
1918         } else {
1919                 if (op_data->op_mea1 != NULL) {
1920                         struct lmv_stripe_md    *lsm = op_data->op_mea1;
1921
1922                         src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
1923                                                              oldlen,
1924                                                              &op_data->op_fid1,
1925                                                              &op_data->op_mds);
1926                 } else {
1927                         src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
1928                 }
1929                 if (IS_ERR(src_tgt))
1930                         RETURN(PTR_ERR(src_tgt));
1931
1932
1933                 if (op_data->op_mea2 != NULL) {
1934                         struct lmv_stripe_md    *lsm = op_data->op_mea2;
1935
1936                         tgt_tgt = lmv_locate_target_for_name(lmv, lsm, new,
1937                                                              newlen,
1938                                                              &op_data->op_fid2,
1939                                                              &op_data->op_mds);
1940                 } else {
1941                         tgt_tgt = lmv_find_target(lmv, &op_data->op_fid2);
1942
1943                 }
1944                 if (IS_ERR(tgt_tgt))
1945                         RETURN(PTR_ERR(tgt_tgt));
1946
1947                 target_exp = tgt_tgt->ltd_exp;
1948         }
1949
1950         /*
1951          * LOOKUP lock on src child (fid3) should also be cancelled for
1952          * src_tgt in mdc_rename.
1953          */
1954         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1955
1956         /*
1957          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
1958          * own target.
1959          */
1960         rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1961                               LCK_EX, MDS_INODELOCK_UPDATE,
1962                               MF_MDC_CANCEL_FID2);
1963
1964         if (rc != 0)
1965                 RETURN(rc);
1966         /*
1967          * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
1968          */
1969         if (fid_is_sane(&op_data->op_fid3)) {
1970                 struct lmv_tgt_desc *tgt;
1971
1972                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1973                 if (IS_ERR(tgt))
1974                         RETURN(PTR_ERR(tgt));
1975
1976                 /* Cancel LOOKUP lock on its parent */
1977                 rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
1978                                       LCK_EX, MDS_INODELOCK_LOOKUP,
1979                                       MF_MDC_CANCEL_FID3);
1980                 if (rc != 0)
1981                         RETURN(rc);
1982
1983                 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1984                                       LCK_EX, MDS_INODELOCK_FULL,
1985                                       MF_MDC_CANCEL_FID3);
1986                 if (rc != 0)
1987                         RETURN(rc);
1988         }
1989
1990 retry_rename:
1991         /*
1992          * Cancel all the locks on tgt child (fid4).
1993          */
1994         if (fid_is_sane(&op_data->op_fid4)) {
1995                 struct lmv_tgt_desc *tgt;
1996
1997                 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1998                                       LCK_EX, MDS_INODELOCK_FULL,
1999                                       MF_MDC_CANCEL_FID4);
2000                 if (rc != 0)
2001                         RETURN(rc);
2002
2003                 tgt = lmv_find_target(lmv, &op_data->op_fid4);
2004                 if (IS_ERR(tgt))
2005                         RETURN(PTR_ERR(tgt));
2006
2007                 /* Since the target child might be destroyed, and it might
2008                  * become orphan, and we can only check orphan on the local
2009                  * MDT right now, so we send rename request to the MDT where
2010                  * target child is located. If target child does not exist,
2011                  * then it will send the request to the target parent */
2012                 target_exp = tgt->ltd_exp;
2013         }
2014
2015         rc = md_rename(target_exp, op_data, old, oldlen, new, newlen,
2016                        request);
2017
2018         if (rc != 0 && rc != -EXDEV)
2019                 RETURN(rc);
2020
2021         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2022         if (body == NULL)
2023                 RETURN(-EPROTO);
2024
2025         /* Not cross-ref case, just get out of here. */
2026         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2027                 RETURN(rc);
2028
2029         CDEBUG(D_INODE, "%s: try rename to another MDT for "DFID"\n",
2030                exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2031
2032         op_data->op_fid4 = body->mbo_fid1;
2033         ptlrpc_req_finished(*request);
2034         *request = NULL;
2035         goto retry_rename;
2036 }
2037
2038 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2039                        void *ea, size_t ealen, struct ptlrpc_request **request)
2040 {
2041         struct obd_device       *obd = exp->exp_obd;
2042         struct lmv_obd          *lmv = &obd->u.lmv;
2043         struct lmv_tgt_desc     *tgt;
2044         int                      rc = 0;
2045         ENTRY;
2046
2047         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2048                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2049
2050         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2051         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2052         if (IS_ERR(tgt))
2053                 RETURN(PTR_ERR(tgt));
2054
2055         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2056
2057         RETURN(rc);
2058 }
2059
2060 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2061                      struct ptlrpc_request **request)
2062 {
2063         struct obd_device       *obd = exp->exp_obd;
2064         struct lmv_obd          *lmv = &obd->u.lmv;
2065         struct lmv_tgt_desc     *tgt;
2066         int                      rc;
2067         ENTRY;
2068
2069         tgt = lmv_find_target(lmv, fid);
2070         if (IS_ERR(tgt))
2071                 RETURN(PTR_ERR(tgt));
2072
2073         rc = md_fsync(tgt->ltd_exp, fid, request);
2074         RETURN(rc);
2075 }
2076
2077 /**
2078  * Get current minimum entry from striped directory
2079  *
2080  * This function will search the dir entry, whose hash value is the
2081  * closest(>=) to @hash_offset, from all of sub-stripes, and it is
2082  * only being called for striped directory.
2083  *
2084  * \param[in] exp               export of LMV
2085  * \param[in] op_data           parameters transferred beween client MD stack
2086  *                              stripe_information will be included in this
2087  *                              parameter
2088  * \param[in] cb_op             ldlm callback being used in enqueue in
2089  *                              mdc_read_page
2090  * \param[in] hash_offset       the hash value, which is used to locate
2091  *                              minum(closet) dir entry
2092  * \param[in|out] stripe_offset the caller use this to indicate the stripe
2093  *                              index of last entry, so to avoid hash conflict
2094  *                              between stripes. It will also be used to
2095  *                              return the stripe index of current dir entry.
2096  * \param[in|out] entp          the minum entry and it also is being used
2097  *                              to input the last dir entry to resolve the
2098  *                              hash conflict
2099  *
2100  * \param[out] ppage            the page which holds the minum entry
2101  *
2102  * \retval                      = 0 get the entry successfully
2103  *                              negative errno (< 0) does not get the entry
2104  */
2105 static int lmv_get_min_striped_entry(struct obd_export *exp,
2106                                      struct md_op_data *op_data,
2107                                      struct md_callback *cb_op,
2108                                      __u64 hash_offset, int *stripe_offset,
2109                                      struct lu_dirent **entp,
2110                                      struct page **ppage)
2111 {
2112         struct obd_device       *obd = exp->exp_obd;
2113         struct lmv_obd          *lmv = &obd->u.lmv;
2114         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2115         struct lmv_tgt_desc     *tgt;
2116         int                     stripe_count;
2117         struct lu_dirent        *min_ent = NULL;
2118         struct page             *min_page = NULL;
2119         int                     min_idx = 0;
2120         int                     i;
2121         int                     rc = 0;
2122         ENTRY;
2123
2124         stripe_count = lsm->lsm_md_stripe_count;
2125         for (i = 0; i < stripe_count; i++) {
2126                 struct lu_dirent        *ent = NULL;
2127                 struct page             *page = NULL;
2128                 struct lu_dirpage       *dp;
2129                 __u64                   stripe_hash = hash_offset;
2130
2131                 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds, NULL);
2132                 if (IS_ERR(tgt))
2133                         GOTO(out, rc = PTR_ERR(tgt));
2134
2135                 /* op_data will be shared by each stripe, so we need
2136                  * reset these value for each stripe */
2137                 op_data->op_fid1 = lsm->lsm_md_oinfo[i].lmo_fid;
2138                 op_data->op_fid2 = lsm->lsm_md_oinfo[i].lmo_fid;
2139                 op_data->op_data = lsm->lsm_md_oinfo[i].lmo_root;
2140 next:
2141                 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, stripe_hash,
2142                                   &page);
2143                 if (rc != 0)
2144                         GOTO(out, rc);
2145
2146                 dp = page_address(page);
2147                 for (ent = lu_dirent_start(dp); ent != NULL;
2148                      ent = lu_dirent_next(ent)) {
2149                         /* Skip dummy entry */
2150                         if (le16_to_cpu(ent->lde_namelen) == 0)
2151                                 continue;
2152
2153                         if (le64_to_cpu(ent->lde_hash) < hash_offset)
2154                                 continue;
2155
2156                         if (le64_to_cpu(ent->lde_hash) == hash_offset &&
2157                             (*entp == ent || i < *stripe_offset))
2158                                 continue;
2159
2160                         /* skip . and .. for other stripes */
2161                         if (i != 0 &&
2162                             (strncmp(ent->lde_name, ".",
2163                                      le16_to_cpu(ent->lde_namelen)) == 0 ||
2164                              strncmp(ent->lde_name, "..",
2165                                      le16_to_cpu(ent->lde_namelen)) == 0))
2166                                 continue;
2167                         break;
2168                 }
2169
2170                 if (ent == NULL) {
2171                         stripe_hash = le64_to_cpu(dp->ldp_hash_end);
2172
2173                         kunmap(page);
2174                         put_page(page);
2175                         page = NULL;
2176
2177                         /* reach the end of current stripe, go to next stripe */
2178                         if (stripe_hash == MDS_DIR_END_OFF)
2179                                 continue;
2180                         else
2181                                 goto next;
2182                 }
2183
2184                 if (min_ent != NULL) {
2185                         if (le64_to_cpu(min_ent->lde_hash) >
2186                             le64_to_cpu(ent->lde_hash)) {
2187                                 min_ent = ent;
2188                                 kunmap(min_page);
2189                                 put_page(min_page);
2190                                 min_idx = i;
2191                                 min_page = page;
2192                         } else {
2193                                 kunmap(page);
2194                                 put_page(page);
2195                                 page = NULL;
2196                         }
2197                 } else {
2198                         min_ent = ent;
2199                         min_page = page;
2200                         min_idx = i;
2201                 }
2202         }
2203
2204 out:
2205         if (*ppage != NULL) {
2206                 kunmap(*ppage);
2207                 put_page(*ppage);
2208         }
2209         *stripe_offset = min_idx;
2210         *entp = min_ent;
2211         *ppage = min_page;
2212         RETURN(rc);
2213 }
2214
2215 /**
2216  * Build dir entry page from a striped directory
2217  *
2218  * This function gets one entry by @offset from a striped directory. It will
2219  * read entries from all of stripes, and choose one closest to the required
2220  * offset(&offset). A few notes
2221  * 1. skip . and .. for non-zero stripes, because there can only have one .
2222  * and .. in a directory.
2223  * 2. op_data will be shared by all of stripes, instead of allocating new
2224  * one, so need to restore before reusing.
2225  * 3. release the entry page if that is not being chosen.
2226  *
2227  * \param[in] exp       obd export refer to LMV
2228  * \param[in] op_data   hold those MD parameters of read_entry
2229  * \param[in] cb_op     ldlm callback being used in enqueue in mdc_read_entry
2230  * \param[out] ldp      the entry being read
2231  * \param[out] ppage    the page holding the entry. Note: because the entry
2232  *                      will be accessed in upper layer, so we need hold the
2233  *                      page until the usages of entry is finished, see
2234  *                      ll_dir_entry_next.
2235  *
2236  * retval               =0 if get entry successfully
2237  *                      <0 cannot get entry
2238  */
2239 static int lmv_read_striped_page(struct obd_export *exp,
2240                                  struct md_op_data *op_data,
2241                                  struct md_callback *cb_op,
2242                                  __u64 offset, struct page **ppage)
2243 {
2244         struct lu_fid           master_fid = op_data->op_fid1;
2245         struct inode            *master_inode = op_data->op_data;
2246         __u64                   hash_offset = offset;
2247         struct lu_dirpage       *dp;
2248         struct page             *min_ent_page = NULL;
2249         struct page             *ent_page = NULL;
2250         struct lu_dirent        *ent;
2251         void                    *area;
2252         int                     ent_idx = 0;
2253         struct lu_dirent        *min_ent = NULL;
2254         struct lu_dirent        *last_ent;
2255         size_t                  left_bytes;
2256         int                     rc;
2257         ENTRY;
2258
2259         /* Allocate a page and read entries from all of stripes and fill
2260          * the page by hash order */
2261         ent_page = alloc_page(GFP_KERNEL);
2262         if (ent_page == NULL)
2263                 RETURN(-ENOMEM);
2264
2265         /* Initialize the entry page */
2266         dp = kmap(ent_page);
2267         memset(dp, 0, sizeof(*dp));
2268         dp->ldp_hash_start = cpu_to_le64(offset);
2269         dp->ldp_flags |= LDF_COLLIDE;
2270
2271         area = dp + 1;
2272         left_bytes = PAGE_SIZE - sizeof(*dp);
2273         ent = area;
2274         last_ent = ent;
2275         do {
2276                 __u16   ent_size;
2277
2278                 /* Find the minum entry from all sub-stripes */
2279                 rc = lmv_get_min_striped_entry(exp, op_data, cb_op, hash_offset,
2280                                                &ent_idx, &min_ent,
2281                                                &min_ent_page);
2282                 if (rc != 0)
2283                         GOTO(out, rc);
2284
2285                 /* If it can not get minum entry, it means it already reaches
2286                  * the end of this directory */
2287                 if (min_ent == NULL) {
2288                         last_ent->lde_reclen = 0;
2289                         hash_offset = MDS_DIR_END_OFF;
2290                         GOTO(out, rc);
2291                 }
2292
2293                 ent_size = le16_to_cpu(min_ent->lde_reclen);
2294
2295                 /* the last entry lde_reclen is 0, but it might not
2296                  * the end of this entry of this temporay entry */
2297                 if (ent_size == 0)
2298                         ent_size = lu_dirent_calc_size(
2299                                         le16_to_cpu(min_ent->lde_namelen),
2300                                         le32_to_cpu(min_ent->lde_attrs));
2301                 if (ent_size > left_bytes) {
2302                         last_ent->lde_reclen = cpu_to_le16(0);
2303                         hash_offset = le64_to_cpu(min_ent->lde_hash);
2304                         GOTO(out, rc);
2305                 }
2306
2307                 memcpy(ent, min_ent, ent_size);
2308
2309                 /* Replace . with master FID and Replace .. with the parent FID
2310                  * of master object */
2311                 if (strncmp(ent->lde_name, ".",
2312                             le16_to_cpu(ent->lde_namelen)) == 0 &&
2313                     le16_to_cpu(ent->lde_namelen) == 1)
2314                         fid_cpu_to_le(&ent->lde_fid, &master_fid);
2315                 else if (strncmp(ent->lde_name, "..",
2316                                    le16_to_cpu(ent->lde_namelen)) == 0 &&
2317                            le16_to_cpu(ent->lde_namelen) == 2)
2318                         fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2319
2320                 left_bytes -= ent_size;
2321                 ent->lde_reclen = cpu_to_le16(ent_size);
2322                 last_ent = ent;
2323                 ent = (void *)ent + ent_size;
2324                 hash_offset = le64_to_cpu(min_ent->lde_hash);
2325                 if (hash_offset == MDS_DIR_END_OFF) {
2326                         last_ent->lde_reclen = 0;
2327                         break;
2328                 }
2329         } while (1);
2330 out:
2331         if (min_ent_page != NULL) {
2332                 kunmap(min_ent_page);
2333                 put_page(min_ent_page);
2334         }
2335
2336         if (unlikely(rc != 0)) {
2337                 __free_page(ent_page);
2338                 ent_page = NULL;
2339         } else {
2340                 if (ent == area)
2341                         dp->ldp_flags |= LDF_EMPTY;
2342                 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2343                 dp->ldp_hash_end = cpu_to_le64(hash_offset);
2344         }
2345
2346         /* We do not want to allocate md_op_data during each
2347          * dir entry reading, so op_data will be shared by every stripe,
2348          * then we need to restore it back to original value before
2349          * return to the upper layer */
2350         op_data->op_fid1 = master_fid;
2351         op_data->op_fid2 = master_fid;
2352         op_data->op_data = master_inode;
2353
2354         *ppage = ent_page;
2355
2356         RETURN(rc);
2357 }
2358
2359 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2360                   struct md_callback *cb_op, __u64 offset,
2361                   struct page **ppage)
2362 {
2363         struct obd_device       *obd = exp->exp_obd;
2364         struct lmv_obd          *lmv = &obd->u.lmv;
2365         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2366         struct lmv_tgt_desc     *tgt;
2367         int                     rc;
2368         ENTRY;
2369
2370         if (unlikely(lsm != NULL)) {
2371                 rc = lmv_read_striped_page(exp, op_data, cb_op, offset, ppage);
2372                 RETURN(rc);
2373         }
2374
2375         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2376         if (IS_ERR(tgt))
2377                 RETURN(PTR_ERR(tgt));
2378
2379         rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2380
2381         RETURN(rc);
2382 }
2383
2384 /**
2385  * Unlink a file/directory
2386  *
2387  * Unlink a file or directory under the parent dir. The unlink request
2388  * usually will be sent to the MDT where the child is located, but if
2389  * the client does not have the child FID then request will be sent to the
2390  * MDT where the parent is located.
2391  *
2392  * If the parent is a striped directory then it also needs to locate which
2393  * stripe the name of the child is located, and replace the parent FID
2394  * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2395  * it will walk through all of sub-stripes until the child is being
2396  * unlinked finally.
2397  *
2398  * \param[in] exp       export refer to LMV
2399  * \param[in] op_data   different parameters transferred beween client
2400  *                      MD stacks, name, namelen, FIDs etc.
2401  *                      op_fid1 is the parent FID, op_fid2 is the child
2402  *                      FID.
2403  * \param[out] request  point to the request of unlink.
2404  *
2405  * retval               0 if succeed
2406  *                      negative errno if failed.
2407  */
2408 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2409                       struct ptlrpc_request **request)
2410 {
2411         struct obd_device       *obd = exp->exp_obd;
2412         struct lmv_obd          *lmv = &obd->u.lmv;
2413         struct lmv_tgt_desc     *tgt = NULL;
2414         struct lmv_tgt_desc     *parent_tgt = NULL;
2415         struct mdt_body         *body;
2416         int                     rc;
2417         int                     stripe_index = 0;
2418         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2419         ENTRY;
2420
2421 retry_unlink:
2422         /* For striped dir, we need to locate the parent as well */
2423         if (lsm != NULL) {
2424                 struct lmv_tgt_desc *tmp;
2425
2426                 LASSERT(op_data->op_name != NULL &&
2427                         op_data->op_namelen != 0);
2428
2429                 tmp = lmv_locate_target_for_name(lmv, lsm,
2430                                                  op_data->op_name,
2431                                                  op_data->op_namelen,
2432                                                  &op_data->op_fid1,
2433                                                  &op_data->op_mds);
2434
2435                 /* return -EBADFD means unknown hash type, might
2436                  * need try all sub-stripe here */
2437                 if (IS_ERR(tmp) && PTR_ERR(tmp) != -EBADFD)
2438                         RETURN(PTR_ERR(tmp));
2439
2440                 /* Note: both migrating dir and unknown hash dir need to
2441                  * try all of sub-stripes, so we need start search the
2442                  * name from stripe 0, but migrating dir is already handled
2443                  * inside lmv_locate_target_for_name(), so we only check
2444                  * unknown hash type directory here */
2445                 if (!lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
2446                         struct lmv_oinfo *oinfo;
2447
2448                         oinfo = &lsm->lsm_md_oinfo[stripe_index];
2449
2450                         op_data->op_fid1 = oinfo->lmo_fid;
2451                         op_data->op_mds = oinfo->lmo_mds;
2452                 }
2453         }
2454
2455 try_next_stripe:
2456         /* Send unlink requests to the MDT where the child is located */
2457         if (likely(!fid_is_zero(&op_data->op_fid2)))
2458                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2459         else if (lsm != NULL)
2460                 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
2461         else
2462                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2463
2464         if (IS_ERR(tgt))
2465                 RETURN(PTR_ERR(tgt));
2466
2467         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2468         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2469         op_data->op_cap = cfs_curproc_cap_pack();
2470
2471         /*
2472          * If child's fid is given, cancel unused locks for it if it is from
2473          * another export than parent.
2474          *
2475          * LOOKUP lock for child (fid3) should also be cancelled on parent
2476          * tgt_tgt in mdc_unlink().
2477          */
2478         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2479
2480         /*
2481          * Cancel FULL locks on child (fid3).
2482          */
2483         parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2484         if (IS_ERR(parent_tgt))
2485                 RETURN(PTR_ERR(parent_tgt));
2486
2487         if (parent_tgt != tgt) {
2488                 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2489                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2490                                       MF_MDC_CANCEL_FID3);
2491         }
2492
2493         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2494                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2495         if (rc != 0)
2496                 RETURN(rc);
2497
2498         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2499                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2500
2501         rc = md_unlink(tgt->ltd_exp, op_data, request);
2502         if (rc != 0 && rc != -EREMOTE && rc != -ENOENT)
2503                 RETURN(rc);
2504
2505         /* Try next stripe if it is needed. */
2506         if (rc == -ENOENT && lsm != NULL && lmv_need_try_all_stripes(lsm)) {
2507                 struct lmv_oinfo *oinfo;
2508
2509                 stripe_index++;
2510                 if (stripe_index >= lsm->lsm_md_stripe_count)
2511                         RETURN(rc);
2512
2513                 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2514
2515                 op_data->op_fid1 = oinfo->lmo_fid;
2516                 op_data->op_mds = oinfo->lmo_mds;
2517
2518                 ptlrpc_req_finished(*request);
2519                 *request = NULL;
2520
2521                 goto try_next_stripe;
2522         }
2523
2524         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2525         if (body == NULL)
2526                 RETURN(-EPROTO);
2527
2528         /* Not cross-ref case, just get out of here. */
2529         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2530                 RETURN(rc);
2531
2532         CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2533                exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2534
2535         /* This is a remote object, try remote MDT, Note: it may
2536          * try more than 1 time here, Considering following case
2537          * /mnt/lustre is root on MDT0, remote1 is on MDT1
2538          * 1. Initially A does not know where remote1 is, it send
2539          *    unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2540          *    resend unlink RPC to MDT1 (retry 1st time).
2541          *
2542          * 2. During the unlink RPC in flight,
2543          *    client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2544          *    and create new remote1, but on MDT0
2545          *
2546          * 3. MDT1 get unlink RPC(from A), then do remote lock on
2547          *    /mnt/lustre, then lookup get fid of remote1, and find
2548          *    it is remote dir again, and replay -EREMOTE again.
2549          *
2550          * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2551          *
2552          * In theory, it might try unlimited time here, but it should
2553          * be very rare case.  */
2554         op_data->op_fid2 = body->mbo_fid1;
2555         ptlrpc_req_finished(*request);
2556         *request = NULL;
2557
2558         goto retry_unlink;
2559 }
2560
2561 static int lmv_precleanup(struct obd_device *obd)
2562 {
2563         ENTRY;
2564         fld_client_proc_fini(&obd->u.lmv.lmv_fld);
2565         lprocfs_obd_cleanup(obd);
2566         lprocfs_free_md_stats(obd);
2567         RETURN(0);
2568 }
2569
2570 /**
2571  * Get by key a value associated with a LMV device.
2572  *
2573  * Dispatch request to lower-layer devices as needed.
2574  *
2575  * \param[in] env               execution environment for this thread
2576  * \param[in] exp               export for the LMV device
2577  * \param[in] keylen            length of key identifier
2578  * \param[in] key               identifier of key to get value for
2579  * \param[in] vallen            size of \a val
2580  * \param[out] val              pointer to storage location for value
2581  * \param[in] lsm               optional striping metadata of object
2582  *
2583  * \retval 0            on success
2584  * \retval negative     negated errno on failure
2585  */
2586 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2587                         __u32 keylen, void *key, __u32 *vallen, void *val)
2588 {
2589         struct obd_device       *obd;
2590         struct lmv_obd          *lmv;
2591         int                      rc = 0;
2592         ENTRY;
2593
2594         obd = class_exp2obd(exp);
2595         if (obd == NULL) {
2596                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2597                        exp->exp_handle.h_cookie);
2598                 RETURN(-EINVAL);
2599         }
2600
2601         lmv = &obd->u.lmv;
2602         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2603                 int i;
2604
2605                 LASSERT(*vallen == sizeof(__u32));
2606                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2607                         struct lmv_tgt_desc *tgt = lmv->tgts[i];
2608                         /*
2609                          * All tgts should be connected when this gets called.
2610                          */
2611                         if (tgt == NULL || tgt->ltd_exp == NULL)
2612                                 continue;
2613
2614                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2615                                           vallen, val))
2616                                 RETURN(0);
2617                 }
2618                 RETURN(-EINVAL);
2619         } else if (KEY_IS(KEY_MAX_EASIZE) ||
2620                    KEY_IS(KEY_DEFAULT_EASIZE) ||
2621                    KEY_IS(KEY_CONN_DATA)) {
2622                 /*
2623                  * Forwarding this request to first MDS, it should know LOV
2624                  * desc.
2625                  */
2626                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2627                                   vallen, val);
2628                 if (!rc && KEY_IS(KEY_CONN_DATA))
2629                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2630                 RETURN(rc);
2631         } else if (KEY_IS(KEY_TGT_COUNT)) {
2632                 *((int *)val) = lmv->desc.ld_tgt_count;
2633                 RETURN(0);
2634         }
2635
2636         CDEBUG(D_IOCTL, "Invalid key\n");
2637         RETURN(-EINVAL);
2638 }
2639
2640 /**
2641  * Asynchronously set by key a value associated with a LMV device.
2642  *
2643  * Dispatch request to lower-layer devices as needed.
2644  *
2645  * \param[in] env       execution environment for this thread
2646  * \param[in] exp       export for the LMV device
2647  * \param[in] keylen    length of key identifier
2648  * \param[in] key       identifier of key to store value for
2649  * \param[in] vallen    size of value to store
2650  * \param[in] val       pointer to data to be stored
2651  * \param[in] set       optional list of related ptlrpc requests
2652  *
2653  * \retval 0            on success
2654  * \retval negative     negated errno on failure
2655  */
2656 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2657                         __u32 keylen, void *key, __u32 vallen, void *val,
2658                         struct ptlrpc_request_set *set)
2659 {
2660         struct lmv_tgt_desc     *tgt = NULL;
2661         struct obd_device       *obd;
2662         struct lmv_obd          *lmv;
2663         int rc = 0;
2664         ENTRY;
2665
2666         obd = class_exp2obd(exp);
2667         if (obd == NULL) {
2668                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2669                        exp->exp_handle.h_cookie);
2670                 RETURN(-EINVAL);
2671         }
2672         lmv = &obd->u.lmv;
2673
2674         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2675             KEY_IS(KEY_DEFAULT_EASIZE)) {
2676                 int i, err = 0;
2677
2678                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2679                         tgt = lmv->tgts[i];
2680
2681                         if (tgt == NULL || tgt->ltd_exp == NULL)
2682                                 continue;
2683
2684                         err = obd_set_info_async(env, tgt->ltd_exp,
2685                                                  keylen, key, vallen, val, set);
2686                         if (err && rc == 0)
2687                                 rc = err;
2688                 }
2689
2690                 RETURN(rc);
2691         }
2692
2693         RETURN(-EINVAL);
2694 }
2695
2696 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2697                             const struct lmv_mds_md_v1 *lmm1)
2698 {
2699         struct lmv_obd  *lmv = &exp->exp_obd->u.lmv;
2700         int             stripe_count;
2701         int             cplen;
2702         int             i;
2703         int             rc = 0;
2704         ENTRY;
2705
2706         lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2707         lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2708         lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2709         if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2710                 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2711         else
2712                 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2713         lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2714         cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2715                         sizeof(lsm->lsm_md_pool_name));
2716
2717         if (cplen >= sizeof(lsm->lsm_md_pool_name))
2718                 RETURN(-E2BIG);
2719
2720         CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d"
2721                "layout_version %d\n", lsm->lsm_md_stripe_count,
2722                lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
2723                lsm->lsm_md_layout_version);
2724
2725         stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2726         for (i = 0; i < stripe_count; i++) {
2727                 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2728                               &lmm1->lmv_stripe_fids[i]);
2729                 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2730                                     &lsm->lsm_md_oinfo[i].lmo_mds);
2731                 if (rc != 0)
2732                         RETURN(rc);
2733                 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
2734                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2735         }
2736
2737         RETURN(rc);
2738 }
2739
2740 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2741                         const union lmv_mds_md *lmm, size_t lmm_size)
2742 {
2743         struct lmv_stripe_md     *lsm;
2744         int                      lsm_size;
2745         int                      rc;
2746         bool                     allocated = false;
2747         ENTRY;
2748
2749         LASSERT(lsmp != NULL);
2750
2751         lsm = *lsmp;
2752         /* Free memmd */
2753         if (lsm != NULL && lmm == NULL) {
2754                 int i;
2755                 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
2756                         /* For migrating inode, the master stripe and master
2757                          * object will be the same, so do not need iput, see
2758                          * ll_update_lsm_md */
2759                         if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
2760                               i == 0) && lsm->lsm_md_oinfo[i].lmo_root != NULL)
2761                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
2762                 }
2763                 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
2764                 OBD_FREE(lsm, lsm_size);
2765                 *lsmp = NULL;
2766                 RETURN(0);
2767         }
2768
2769         if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
2770                 RETURN(-EPERM);
2771
2772         /* Unpack memmd */
2773         if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2774             le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2775                 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2776                        exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2777                        -EIO);
2778                 RETURN(-EIO);
2779         }
2780
2781         if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2782                 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2783         else
2784                 /**
2785                  * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2786                  * stripecount should be 0 then.
2787                  */
2788                 lsm_size = lmv_stripe_md_size(0);
2789
2790         lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2791         if (lsm == NULL) {
2792                 OBD_ALLOC(lsm, lsm_size);
2793                 if (lsm == NULL)
2794                         RETURN(-ENOMEM);
2795                 allocated = true;
2796                 *lsmp = lsm;
2797         }
2798
2799         switch (le32_to_cpu(lmm->lmv_magic)) {
2800         case LMV_MAGIC_V1:
2801                 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
2802                 break;
2803         default:
2804                 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
2805                        le32_to_cpu(lmm->lmv_magic));
2806                 rc = -EINVAL;
2807                 break;
2808         }
2809
2810         if (rc != 0 && allocated) {
2811                 OBD_FREE(lsm, lsm_size);
2812                 *lsmp = NULL;
2813                 lsm_size = rc;
2814         }
2815         RETURN(lsm_size);
2816 }
2817
2818 void lmv_free_memmd(struct lmv_stripe_md *lsm)
2819 {
2820         lmv_unpackmd(NULL, &lsm, NULL, 0);
2821 }
2822 EXPORT_SYMBOL(lmv_free_memmd);
2823
2824 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2825                              union ldlm_policy_data *policy,
2826                              enum ldlm_mode mode, enum ldlm_cancel_flags flags,
2827                              void *opaque)
2828 {
2829         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2830         int rc = 0;
2831         __u32 i;
2832         ENTRY;
2833
2834         LASSERT(fid != NULL);
2835
2836         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2837                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2838                 int err;
2839
2840                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2841                         continue;
2842
2843                 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
2844                                        opaque);
2845                 if (!rc)
2846                         rc = err;
2847         }
2848         RETURN(rc);
2849 }
2850
2851 static int lmv_set_lock_data(struct obd_export *exp,
2852                              const struct lustre_handle *lockh,
2853                              void *data, __u64 *bits)
2854 {
2855         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2856         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2857         int                      rc;
2858         ENTRY;
2859
2860         if (tgt == NULL || tgt->ltd_exp == NULL)
2861                 RETURN(-EINVAL);
2862         rc =  md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
2863         RETURN(rc);
2864 }
2865
2866 enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
2867                               const struct lu_fid *fid, enum ldlm_type type,
2868                               union ldlm_policy_data *policy,
2869                               enum ldlm_mode mode, struct lustre_handle *lockh)
2870 {
2871         struct obd_device       *obd = exp->exp_obd;
2872         struct lmv_obd          *lmv = &obd->u.lmv;
2873         enum ldlm_mode          rc;
2874         int                     tgt;
2875         int                     i;
2876         ENTRY;
2877
2878         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2879
2880         /*
2881          * With DNE every object can have two locks in different namespaces:
2882          * lookup lock in space of MDT storing direntry and update/open lock in
2883          * space of MDT storing inode.  Try the MDT that the FID maps to first,
2884          * since this can be easily found, and only try others if that fails.
2885          */
2886         for (i = 0, tgt = lmv_find_target_index(lmv, fid);
2887              i < lmv->desc.ld_tgt_count;
2888              i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
2889                 if (tgt < 0) {
2890                         CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
2891                                obd->obd_name, PFID(fid), tgt);
2892                         tgt = 0;
2893                 }
2894
2895                 if (lmv->tgts[tgt] == NULL ||
2896                     lmv->tgts[tgt]->ltd_exp == NULL ||
2897                     lmv->tgts[tgt]->ltd_active == 0)
2898                         continue;
2899
2900                 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
2901                                    type, policy, mode, lockh);
2902                 if (rc)
2903                         RETURN(rc);
2904         }
2905
2906         RETURN(0);
2907 }
2908
2909 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2910                       struct obd_export *dt_exp, struct obd_export *md_exp,
2911                       struct lustre_md *md)
2912 {
2913         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2914         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2915
2916         if (tgt == NULL || tgt->ltd_exp == NULL)
2917                 RETURN(-EINVAL);
2918
2919         return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2920 }
2921
2922 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2923 {
2924         struct obd_device       *obd = exp->exp_obd;
2925         struct lmv_obd          *lmv = &obd->u.lmv;
2926         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2927         ENTRY;
2928
2929         if (md->lmv != NULL) {
2930                 lmv_free_memmd(md->lmv);
2931                 md->lmv = NULL;
2932         }
2933         if (tgt == NULL || tgt->ltd_exp == NULL)
2934                 RETURN(-EINVAL);
2935         RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
2936 }
2937
2938 int lmv_set_open_replay_data(struct obd_export *exp,
2939                              struct obd_client_handle *och,
2940                              struct lookup_intent *it)
2941 {
2942         struct obd_device       *obd = exp->exp_obd;
2943         struct lmv_obd          *lmv = &obd->u.lmv;
2944         struct lmv_tgt_desc     *tgt;
2945         ENTRY;
2946
2947         tgt = lmv_find_target(lmv, &och->och_fid);
2948         if (IS_ERR(tgt))
2949                 RETURN(PTR_ERR(tgt));
2950
2951         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
2952 }
2953
2954 int lmv_clear_open_replay_data(struct obd_export *exp,
2955                                struct obd_client_handle *och)
2956 {
2957         struct obd_device       *obd = exp->exp_obd;
2958         struct lmv_obd          *lmv = &obd->u.lmv;
2959         struct lmv_tgt_desc     *tgt;
2960         ENTRY;
2961
2962         tgt = lmv_find_target(lmv, &och->och_fid);
2963         if (IS_ERR(tgt))
2964                 RETURN(PTR_ERR(tgt));
2965
2966         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2967 }
2968
2969 int lmv_intent_getattr_async(struct obd_export *exp,
2970                              struct md_enqueue_info *minfo)
2971 {
2972         struct md_op_data       *op_data = &minfo->mi_data;
2973         struct obd_device       *obd = exp->exp_obd;
2974         struct lmv_obd          *lmv = &obd->u.lmv;
2975         struct lmv_tgt_desc     *ptgt = NULL;
2976         struct lmv_tgt_desc     *ctgt = NULL;
2977         int                      rc;
2978         ENTRY;
2979
2980         if (!fid_is_sane(&op_data->op_fid2))
2981                 RETURN(-EINVAL);
2982
2983         ptgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2984         if (IS_ERR(ptgt))
2985                 RETURN(PTR_ERR(ptgt));
2986
2987         ctgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2988         if (IS_ERR(ctgt))
2989                 RETURN(PTR_ERR(ctgt));
2990
2991         /*
2992          * if child is on remote MDT, we need 2 async RPCs to fetch both LOOKUP
2993          * lock on parent, and UPDATE lock on child MDT, which makes all
2994          * complicated. Considering remote dir is rare case, and not supporting
2995          * it in statahead won't cause any issue, drop its support for now.
2996          */
2997         if (ptgt != ctgt)
2998                 RETURN(-ENOTSUPP);
2999
3000         rc = md_intent_getattr_async(ptgt->ltd_exp, minfo);
3001         RETURN(rc);
3002 }
3003
3004 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3005                         struct lu_fid *fid, __u64 *bits)
3006 {
3007         struct obd_device       *obd = exp->exp_obd;
3008         struct lmv_obd          *lmv = &obd->u.lmv;
3009         struct lmv_tgt_desc     *tgt;
3010         int                      rc;
3011         ENTRY;
3012
3013         tgt = lmv_find_target(lmv, fid);
3014         if (IS_ERR(tgt))
3015                 RETURN(PTR_ERR(tgt));
3016
3017         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3018         RETURN(rc);
3019 }
3020
3021 int lmv_get_fid_from_lsm(struct obd_export *exp,
3022                          const struct lmv_stripe_md *lsm,
3023                          const char *name, int namelen, struct lu_fid *fid)
3024 {
3025         const struct lmv_oinfo *oinfo;
3026
3027         LASSERT(lsm != NULL);
3028         oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
3029         if (IS_ERR(oinfo))
3030                 return PTR_ERR(oinfo);
3031
3032         *fid = oinfo->lmo_fid;
3033
3034         RETURN(0);
3035 }
3036
3037 /**
3038  * For lmv, only need to send request to master MDT, and the master MDT will
3039  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3040  * we directly fetch data from the slave MDTs.
3041  */
3042 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3043                  struct obd_quotactl *oqctl)
3044 {
3045         struct obd_device   *obd = class_exp2obd(exp);
3046         struct lmv_obd      *lmv = &obd->u.lmv;
3047         struct lmv_tgt_desc *tgt = lmv->tgts[0];
3048         int                  rc = 0;
3049         __u32                i;
3050         __u64                curspace, curinodes;
3051         ENTRY;
3052
3053         if (tgt == NULL ||
3054             tgt->ltd_exp == NULL ||
3055             !tgt->ltd_active ||
3056             lmv->desc.ld_tgt_count == 0) {
3057                 CERROR("master lmv inactive\n");
3058                 RETURN(-EIO);
3059         }
3060
3061         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3062                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3063                 RETURN(rc);
3064         }
3065
3066         curspace = curinodes = 0;
3067         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3068                 int err;
3069                 tgt = lmv->tgts[i];
3070
3071                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3072                         continue;
3073
3074                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3075                 if (err) {
3076                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3077                         if (!rc)
3078                                 rc = err;
3079                 } else {
3080                         curspace += oqctl->qc_dqblk.dqb_curspace;
3081                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3082                 }
3083         }
3084         oqctl->qc_dqblk.dqb_curspace = curspace;
3085         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3086
3087         RETURN(rc);
3088 }
3089
3090 static int lmv_merge_attr(struct obd_export *exp,
3091                           const struct lmv_stripe_md *lsm,
3092                           struct cl_attr *attr,
3093                           ldlm_blocking_callback cb_blocking)
3094 {
3095         int rc;
3096         int i;
3097
3098         rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3099         if (rc < 0)
3100                 return rc;
3101
3102         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3103                 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3104
3105                 CDEBUG(D_INFO, ""DFID" size %llu, blocks %llu nlink %u,"
3106                        " atime %lu ctime %lu, mtime %lu.\n",
3107                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3108                        i_size_read(inode), (unsigned long long)inode->i_blocks,
3109                        inode->i_nlink, LTIME_S(inode->i_atime),
3110                        LTIME_S(inode->i_ctime), LTIME_S(inode->i_mtime));
3111
3112                 /* for slave stripe, it needs to subtract nlink for . and .. */
3113                 if (i != 0)
3114                         attr->cat_nlink += inode->i_nlink - 2;
3115                 else
3116                         attr->cat_nlink = inode->i_nlink;
3117
3118                 attr->cat_size += i_size_read(inode);
3119                 attr->cat_blocks += inode->i_blocks;
3120
3121                 if (attr->cat_atime < LTIME_S(inode->i_atime))
3122                         attr->cat_atime = LTIME_S(inode->i_atime);
3123
3124                 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3125                         attr->cat_ctime = LTIME_S(inode->i_ctime);
3126
3127                 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3128                         attr->cat_mtime = LTIME_S(inode->i_mtime);
3129         }
3130         return 0;
3131 }
3132
3133 struct obd_ops lmv_obd_ops = {
3134         .o_owner                = THIS_MODULE,
3135         .o_setup                = lmv_setup,
3136         .o_cleanup              = lmv_cleanup,
3137         .o_precleanup           = lmv_precleanup,
3138         .o_process_config       = lmv_process_config,
3139         .o_connect              = lmv_connect,
3140         .o_disconnect           = lmv_disconnect,
3141         .o_statfs               = lmv_statfs,
3142         .o_get_info             = lmv_get_info,
3143         .o_set_info_async       = lmv_set_info_async,
3144         .o_notify               = lmv_notify,
3145         .o_get_uuid             = lmv_get_uuid,
3146         .o_iocontrol            = lmv_iocontrol,
3147         .o_quotactl             = lmv_quotactl
3148 };
3149
3150 struct md_ops lmv_md_ops = {
3151         .m_get_root             = lmv_get_root,
3152         .m_null_inode           = lmv_null_inode,
3153         .m_close                = lmv_close,
3154         .m_create               = lmv_create,
3155         .m_enqueue              = lmv_enqueue,
3156         .m_getattr              = lmv_getattr,
3157         .m_getxattr             = lmv_getxattr,
3158         .m_getattr_name         = lmv_getattr_name,
3159         .m_intent_lock          = lmv_intent_lock,
3160         .m_link                 = lmv_link,
3161         .m_rename               = lmv_rename,
3162         .m_setattr              = lmv_setattr,
3163         .m_setxattr             = lmv_setxattr,
3164         .m_fsync                = lmv_fsync,
3165         .m_read_page            = lmv_read_page,
3166         .m_unlink               = lmv_unlink,
3167         .m_init_ea_size         = lmv_init_ea_size,
3168         .m_cancel_unused        = lmv_cancel_unused,
3169         .m_set_lock_data        = lmv_set_lock_data,
3170         .m_lock_match           = lmv_lock_match,
3171         .m_get_lustre_md        = lmv_get_lustre_md,
3172         .m_free_lustre_md       = lmv_free_lustre_md,
3173         .m_merge_attr           = lmv_merge_attr,
3174         .m_set_open_replay_data = lmv_set_open_replay_data,
3175         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3176         .m_intent_getattr_async = lmv_intent_getattr_async,
3177         .m_revalidate_lock      = lmv_revalidate_lock,
3178         .m_get_fid_from_lsm     = lmv_get_fid_from_lsm,
3179         .m_unpackmd             = lmv_unpackmd,
3180 };
3181
3182 static int __init lmv_init(void)
3183 {
3184         return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3185                                    LUSTRE_LMV_NAME, NULL);
3186 }
3187
3188 static void __exit lmv_exit(void)
3189 {
3190         class_unregister_type(LUSTRE_LMV_NAME);
3191 }
3192
3193 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3194 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3195 MODULE_VERSION(LUSTRE_VERSION_STRING);
3196 MODULE_LICENSE("GPL");
3197
3198 module_init(lmv_init);
3199 module_exit(lmv_exit);