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