Whamcloud - gitweb
LU-3182 lmv: fix duplicate directory entries
[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, 2012, 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 #ifdef __KERNEL__
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
44 #include <linux/mm.h>
45 #include <asm/div64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
48 #else
49 #include <liblustre.h>
50 #endif
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 <lprocfs_status.h>
58 #include <lustre_lite.h>
59 #include <lustre_fid.h>
60 #include "lmv_internal.h"
61
62 static void lmv_activate_target(struct lmv_obd *lmv,
63                                 struct lmv_tgt_desc *tgt,
64                                 int activate)
65 {
66         if (tgt->ltd_active == activate)
67                 return;
68
69         tgt->ltd_active = activate;
70         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
71 }
72
73 /**
74  * Error codes:
75  *
76  *  -EINVAL  : UUID can't be found in the LMV's target list
77  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
78  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
79  */
80 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
81                               int activate)
82 {
83         struct lmv_tgt_desc    *tgt;
84         struct obd_device      *obd;
85         int                     i;
86         int                     rc = 0;
87         ENTRY;
88
89         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
90                lmv, uuid->uuid, activate);
91
92         spin_lock(&lmv->lmv_lock);
93         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
94                 tgt = lmv->tgts[i];
95                 if (tgt == NULL || tgt->ltd_exp == NULL)
96                         continue;
97
98                 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n", i,
99                        tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
100
101                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
102                         break;
103         }
104
105         if (i == lmv->desc.ld_tgt_count)
106                 GOTO(out_lmv_lock, rc = -EINVAL);
107
108         obd = class_exp2obd(tgt->ltd_exp);
109         if (obd == NULL)
110                 GOTO(out_lmv_lock, rc = -ENOTCONN);
111
112         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
113                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
114                obd->obd_type->typ_name, i);
115         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
116
117         if (tgt->ltd_active == activate) {
118                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
119                        activate ? "" : "in");
120                 GOTO(out_lmv_lock, rc);
121         }
122
123         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
124                activate ? "" : "in");
125         lmv_activate_target(lmv, tgt, activate);
126         EXIT;
127
128  out_lmv_lock:
129         spin_unlock(&lmv->lmv_lock);
130         return rc;
131 }
132
133 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
134 {
135         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
136
137         return obd_get_uuid(lmv->tgts[0]->ltd_exp);
138 }
139
140 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
141                       enum obd_notify_event ev, void *data)
142 {
143         struct obd_connect_data *conn_data;
144         struct lmv_obd          *lmv = &obd->u.lmv;
145         struct obd_uuid         *uuid;
146         int                      rc = 0;
147         ENTRY;
148
149         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
150                 CERROR("unexpected notification of %s %s!\n",
151                        watched->obd_type->typ_name,
152                        watched->obd_name);
153                 RETURN(-EINVAL);
154         }
155
156         uuid = &watched->u.cli.cl_target_uuid;
157         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
158                 /*
159                  * Set MDC as active before notifying the observer, so the
160                  * observer can use the MDC normally.
161                  */
162                 rc = lmv_set_mdc_active(lmv, uuid,
163                                         ev == OBD_NOTIFY_ACTIVE);
164                 if (rc) {
165                         CERROR("%sactivation of %s failed: %d\n",
166                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
167                                uuid->uuid, rc);
168                         RETURN(rc);
169                 }
170         } else if (ev == OBD_NOTIFY_OCD) {
171                 conn_data = &watched->u.cli.cl_import->imp_connect_data;
172                 /*
173                  * XXX: Make sure that ocd_connect_flags from all targets are
174                  * the same. Otherwise one of MDTs runs wrong version or
175                  * something like this.  --umka
176                  */
177                 obd->obd_self_export->exp_connect_data = *conn_data;
178         }
179 #if 0
180         else if (ev == OBD_NOTIFY_DISCON) {
181                 /*
182                  * For disconnect event, flush fld cache for failout MDS case.
183                  */
184                 fld_client_flush(&lmv->lmv_fld);
185         }
186 #endif
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 #ifdef __KERNEL__
206         struct proc_dir_entry *lmv_proc_dir;
207 #endif
208         struct lmv_obd        *lmv = &obd->u.lmv;
209         struct lustre_handle  conn = { 0 };
210         int                    rc = 0;
211         ENTRY;
212
213         /*
214          * We don't want to actually do the underlying connections more than
215          * once, so keep track.
216          */
217         lmv->refcount++;
218         if (lmv->refcount > 1) {
219                 *exp = NULL;
220                 RETURN(0);
221         }
222
223         rc = class_connect(&conn, obd, cluuid);
224         if (rc) {
225                 CERROR("class_connection() returned %d\n", rc);
226                 RETURN(rc);
227         }
228
229         *exp = class_conn2export(&conn);
230         class_export_get(*exp);
231
232         lmv->exp = *exp;
233         lmv->connected = 0;
234         lmv->cluuid = *cluuid;
235
236         if (data)
237                 lmv->conn_data = *data;
238
239 #ifdef __KERNEL__
240         lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
241                                         NULL, NULL);
242         if (IS_ERR(lmv_proc_dir)) {
243                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
244                        obd->obd_type->typ_name, obd->obd_name);
245                 lmv_proc_dir = NULL;
246         }
247 #endif
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->ocd_connect_flags & OBD_CONNECT_REAL)
256                 rc = lmv_check_connect(obd);
257
258 #ifdef __KERNEL__
259         if (rc) {
260                 if (lmv_proc_dir)
261                         lprocfs_remove(&lmv_proc_dir);
262         }
263 #endif
264
265         RETURN(rc);
266 }
267
268 static void lmv_set_timeouts(struct obd_device *obd)
269 {
270         struct lmv_tgt_desc   *tgt;
271         struct lmv_obd        *lmv;
272         int                    i;
273
274         lmv = &obd->u.lmv;
275         if (lmv->server_timeout == 0)
276                 return;
277
278         if (lmv->connected == 0)
279                 return;
280
281         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
282                 tgt = lmv->tgts[i];
283                 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
284                         continue;
285
286                 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
287                                    KEY_INTERMDS, 0, NULL, NULL);
288         }
289 }
290
291 static int lmv_init_ea_size(struct obd_export *exp, int easize,
292                             int def_easize, int cookiesize)
293 {
294         struct obd_device   *obd = exp->exp_obd;
295         struct lmv_obd      *lmv = &obd->u.lmv;
296         int                  i;
297         int                  rc = 0;
298         int                  change = 0;
299         ENTRY;
300
301         if (lmv->max_easize < easize) {
302                 lmv->max_easize = easize;
303                 change = 1;
304         }
305         if (lmv->max_def_easize < def_easize) {
306                 lmv->max_def_easize = def_easize;
307                 change = 1;
308         }
309         if (lmv->max_cookiesize < cookiesize) {
310                 lmv->max_cookiesize = cookiesize;
311                 change = 1;
312         }
313         if (change == 0)
314                 RETURN(0);
315
316         if (lmv->connected == 0)
317                 RETURN(0);
318
319         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
320                 if (lmv->tgts[i] == NULL ||
321                     lmv->tgts[i]->ltd_exp == NULL ||
322                     lmv->tgts[i]->ltd_active == 0) {
323                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
324                         continue;
325                 }
326
327                 rc = md_init_ea_size(lmv->tgts[i]->ltd_exp, easize, def_easize,
328                                      cookiesize);
329                 if (rc) {
330                         CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
331                                " rc = %d.\n", obd->obd_name, i, rc);
332                         break;
333                 }
334         }
335         RETURN(rc);
336 }
337
338 #define MAX_STRING_SIZE 128
339
340 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
341 {
342 #ifdef __KERNEL__
343         struct proc_dir_entry   *lmv_proc_dir;
344 #endif
345         struct lmv_obd          *lmv = &obd->u.lmv;
346         struct obd_uuid         *cluuid = &lmv->cluuid;
347         struct obd_uuid          lmv_mdc_uuid = { "LMV_MDC_UUID" };
348         struct obd_device       *mdc_obd;
349         struct obd_export       *mdc_exp;
350         struct lu_fld_target     target;
351         int                      rc;
352         ENTRY;
353
354         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
355                                         &obd->obd_uuid);
356         if (!mdc_obd) {
357                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
358                 RETURN(-EINVAL);
359         }
360
361         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
362                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
363                 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
364                 cluuid->uuid);
365
366         if (!mdc_obd->obd_set_up) {
367                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
368                 RETURN(-EINVAL);
369         }
370
371         rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
372                          &lmv->conn_data, NULL);
373         if (rc) {
374                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
375                 RETURN(rc);
376         }
377
378         /*
379          * Init fid sequence client for this mdc and add new fld target.
380          */
381         rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
382         if (rc)
383                 RETURN(rc);
384
385         target.ft_srv = NULL;
386         target.ft_exp = mdc_exp;
387         target.ft_idx = tgt->ltd_idx;
388
389         fld_client_add_target(&lmv->lmv_fld, &target);
390
391         rc = obd_register_observer(mdc_obd, obd);
392         if (rc) {
393                 obd_disconnect(mdc_exp);
394                 CERROR("target %s register_observer error %d\n",
395                        tgt->ltd_uuid.uuid, rc);
396                 RETURN(rc);
397         }
398
399         if (obd->obd_observer) {
400                 /*
401                  * Tell the observer about the new target.
402                  */
403                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
404                                 OBD_NOTIFY_ACTIVE,
405                                 (void *)(tgt - lmv->tgts[0]));
406                 if (rc) {
407                         obd_disconnect(mdc_exp);
408                         RETURN(rc);
409                 }
410         }
411
412         tgt->ltd_active = 1;
413         tgt->ltd_exp = mdc_exp;
414         lmv->desc.ld_active_tgt_count++;
415
416         md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
417                         lmv->max_def_easize, lmv->max_cookiesize);
418
419         CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
420                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
421                 cfs_atomic_read(&obd->obd_refcount));
422
423 #ifdef __KERNEL__
424         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
425         if (lmv_proc_dir) {
426                 struct proc_dir_entry *mdc_symlink;
427
428                 LASSERT(mdc_obd->obd_type != NULL);
429                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
430                 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
431                                                   lmv_proc_dir,
432                                                   "../../../%s/%s",
433                                                   mdc_obd->obd_type->typ_name,
434                                                   mdc_obd->obd_name);
435                 if (mdc_symlink == NULL) {
436                         CERROR("Could not register LMV target "
437                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
438                                obd->obd_type->typ_name, obd->obd_name,
439                                mdc_obd->obd_name);
440                         lprocfs_remove(&lmv_proc_dir);
441                         lmv_proc_dir = NULL;
442                 }
443         }
444 #endif
445         RETURN(0);
446 }
447
448 static void lmv_del_target(struct lmv_obd *lmv, int index)
449 {
450         if (lmv->tgts[index] == NULL)
451                 return;
452
453         OBD_FREE_PTR(lmv->tgts[index]);
454         lmv->tgts[index] = NULL;
455         return;
456 }
457
458 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
459                            __u32 index, int gen)
460 {
461         struct lmv_obd      *lmv = &obd->u.lmv;
462         struct lmv_tgt_desc *tgt;
463         int                  rc = 0;
464         ENTRY;
465
466         CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
467
468         lmv_init_lock(lmv);
469
470         if (lmv->desc.ld_tgt_count == 0) {
471                 struct obd_device *mdc_obd;
472
473                 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
474                                                 &obd->obd_uuid);
475                 if (!mdc_obd) {
476                         lmv_init_unlock(lmv);
477                         CERROR("%s: Target %s not attached: rc = %d\n",
478                                obd->obd_name, uuidp->uuid, -EINVAL);
479                         RETURN(-EINVAL);
480                 }
481         }
482
483         if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
484                 tgt = lmv->tgts[index];
485                 CERROR("%s: UUID %s already assigned at LOV target index %d:"
486                        " rc = %d\n", obd->obd_name,
487                        obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
488                 lmv_init_unlock(lmv);
489                 RETURN(-EEXIST);
490         }
491
492         if (index >= lmv->tgts_size) {
493                 /* We need to reallocate the lmv target array. */
494                 struct lmv_tgt_desc **newtgts, **old = NULL;
495                 __u32 newsize = 1;
496                 __u32 oldsize = 0;
497
498                 while (newsize < index + 1)
499                         newsize = newsize << 1;
500                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
501                 if (newtgts == NULL) {
502                         lmv_init_unlock(lmv);
503                         RETURN(-ENOMEM);
504                 }
505
506                 if (lmv->tgts_size) {
507                         memcpy(newtgts, lmv->tgts,
508                                sizeof(*newtgts) * lmv->tgts_size);
509                         old = lmv->tgts;
510                         oldsize = lmv->tgts_size;
511                 }
512
513                 lmv->tgts = newtgts;
514                 lmv->tgts_size = newsize;
515                 smp_rmb();
516                 if (old)
517                         OBD_FREE(old, sizeof(*old) * oldsize);
518
519                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
520                        lmv->tgts_size);
521         }
522
523         OBD_ALLOC_PTR(tgt);
524         if (!tgt) {
525                 lmv_init_unlock(lmv);
526                 RETURN(-ENOMEM);
527         }
528
529         mutex_init(&tgt->ltd_fid_mutex);
530         tgt->ltd_idx = index;
531         tgt->ltd_uuid = *uuidp;
532         tgt->ltd_active = 0;
533         lmv->tgts[index] = tgt;
534         if (index >= lmv->desc.ld_tgt_count)
535                 lmv->desc.ld_tgt_count = index + 1;
536
537         if (lmv->connected) {
538                 rc = lmv_connect_mdc(obd, tgt);
539                 if (rc) {
540                         spin_lock(&lmv->lmv_lock);
541                         lmv->desc.ld_tgt_count--;
542                         memset(tgt, 0, sizeof(*tgt));
543                         spin_unlock(&lmv->lmv_lock);
544                 } else {
545                         int easize = sizeof(struct lmv_stripe_md) +
546                                      lmv->desc.ld_tgt_count *
547                                      sizeof(struct lu_fid);
548                         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
549                 }
550         }
551
552         lmv_init_unlock(lmv);
553         RETURN(rc);
554 }
555
556 int lmv_check_connect(struct obd_device *obd)
557 {
558         struct lmv_obd       *lmv = &obd->u.lmv;
559         struct lmv_tgt_desc  *tgt;
560         int                   i;
561         int                   rc;
562         int                   easize;
563         ENTRY;
564
565         if (lmv->connected)
566                 RETURN(0);
567
568         lmv_init_lock(lmv);
569         if (lmv->connected) {
570                 lmv_init_unlock(lmv);
571                 RETURN(0);
572         }
573
574         if (lmv->desc.ld_tgt_count == 0) {
575                 lmv_init_unlock(lmv);
576                 CERROR("%s: no targets configured.\n", obd->obd_name);
577                 RETURN(-EINVAL);
578         }
579
580         CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
581                lmv->cluuid.uuid, obd->obd_name);
582
583         LASSERT(lmv->tgts != NULL);
584
585         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
586                 tgt = lmv->tgts[i];
587                 if (tgt == NULL)
588                         continue;
589                 rc = lmv_connect_mdc(obd, tgt);
590                 if (rc)
591                         GOTO(out_disc, rc);
592         }
593
594         lmv_set_timeouts(obd);
595         class_export_put(lmv->exp);
596         lmv->connected = 1;
597         easize = lmv_get_easize(lmv);
598         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
599         lmv_init_unlock(lmv);
600         RETURN(0);
601
602  out_disc:
603         while (i-- > 0) {
604                 int rc2;
605                 tgt = lmv->tgts[i];
606                 if (tgt == NULL)
607                         continue;
608                 tgt->ltd_active = 0;
609                 if (tgt->ltd_exp) {
610                         --lmv->desc.ld_active_tgt_count;
611                         rc2 = obd_disconnect(tgt->ltd_exp);
612                         if (rc2) {
613                                 CERROR("LMV target %s disconnect on "
614                                        "MDC idx %d: error %d\n",
615                                        tgt->ltd_uuid.uuid, i, rc2);
616                         }
617                 }
618         }
619         class_disconnect(lmv->exp);
620         lmv_init_unlock(lmv);
621         RETURN(rc);
622 }
623
624 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
625 {
626 #ifdef __KERNEL__
627         struct proc_dir_entry  *lmv_proc_dir;
628 #endif
629         struct lmv_obd         *lmv = &obd->u.lmv;
630         struct obd_device      *mdc_obd;
631         int                     rc;
632         ENTRY;
633
634         LASSERT(tgt != NULL);
635         LASSERT(obd != NULL);
636
637         mdc_obd = class_exp2obd(tgt->ltd_exp);
638
639         if (mdc_obd) {
640                 mdc_obd->obd_force = obd->obd_force;
641                 mdc_obd->obd_fail = obd->obd_fail;
642                 mdc_obd->obd_no_recov = obd->obd_no_recov;
643         }
644
645 #ifdef __KERNEL__
646         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
647         if (lmv_proc_dir) {
648                 struct proc_dir_entry *mdc_symlink;
649
650                 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
651                 if (mdc_symlink) {
652                         lprocfs_remove(&mdc_symlink);
653                 } else {
654                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
655                                obd->obd_type->typ_name, obd->obd_name,
656                                mdc_obd->obd_name);
657                 }
658         }
659 #endif
660         rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
661         if (rc)
662                 CERROR("Can't finanize fids factory\n");
663
664         CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
665                tgt->ltd_exp->exp_obd->obd_name,
666                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
667
668         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
669         rc = obd_disconnect(tgt->ltd_exp);
670         if (rc) {
671                 if (tgt->ltd_active) {
672                         CERROR("Target %s disconnect error %d\n",
673                                tgt->ltd_uuid.uuid, rc);
674                 }
675         }
676
677         lmv_activate_target(lmv, tgt, 0);
678         tgt->ltd_exp = NULL;
679         RETURN(0);
680 }
681
682 static int lmv_disconnect(struct obd_export *exp)
683 {
684         struct obd_device     *obd = class_exp2obd(exp);
685 #ifdef __KERNEL__
686         struct proc_dir_entry *lmv_proc_dir;
687 #endif
688         struct lmv_obd        *lmv = &obd->u.lmv;
689         int                    rc;
690         int                    i;
691         ENTRY;
692
693         if (!lmv->tgts)
694                 goto out_local;
695
696         /*
697          * Only disconnect the underlying layers on the final disconnect.
698          */
699         lmv->refcount--;
700         if (lmv->refcount != 0)
701                 goto out_local;
702
703         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
704                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
705                         continue;
706
707                 lmv_disconnect_mdc(obd, lmv->tgts[i]);
708         }
709
710 #ifdef __KERNEL__
711         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
712         if (lmv_proc_dir) {
713                 lprocfs_remove(&lmv_proc_dir);
714         } else {
715                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
716                        obd->obd_type->typ_name, obd->obd_name);
717         }
718 #endif
719
720 out_local:
721         /*
722          * This is the case when no real connection is established by
723          * lmv_check_connect().
724          */
725         if (!lmv->connected)
726                 class_export_put(exp);
727         rc = class_disconnect(exp);
728         if (lmv->refcount == 0)
729                 lmv->connected = 0;
730         RETURN(rc);
731 }
732
733 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
734 {
735         struct obd_device       *obddev = class_exp2obd(exp);
736         struct lmv_obd          *lmv = &obddev->u.lmv;
737         struct getinfo_fid2path *gf;
738         struct lmv_tgt_desc     *tgt;
739         struct getinfo_fid2path *remote_gf = NULL;
740         int                     remote_gf_size = 0;
741         int                     rc;
742
743         gf = (struct getinfo_fid2path *)karg;
744         tgt = lmv_find_target(lmv, &gf->gf_fid);
745         if (IS_ERR(tgt))
746                 RETURN(PTR_ERR(tgt));
747
748 repeat_fid2path:
749         rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
750         if (rc != 0 && rc != -EREMOTE)
751                 GOTO(out_fid2path, rc);
752
753         /* If remote_gf != NULL, it means just building the
754          * path on the remote MDT, copy this path segement to gf */
755         if (remote_gf != NULL) {
756                 struct getinfo_fid2path *ori_gf;
757                 char *ptr;
758
759                 ori_gf = (struct getinfo_fid2path *)karg;
760                 if (strlen(ori_gf->gf_path) +
761                     strlen(gf->gf_path) > ori_gf->gf_pathlen)
762                         GOTO(out_fid2path, rc = -EOVERFLOW);
763
764                 ptr = ori_gf->gf_path;
765
766                 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
767                         strlen(ori_gf->gf_path));
768
769                 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
770                 ptr += strlen(gf->gf_path);
771                 *ptr = '/';
772         }
773
774         CDEBUG(D_INFO, "%s: get path %s "DFID" rec: "LPU64" ln: %u\n",
775                tgt->ltd_exp->exp_obd->obd_name,
776                gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
777                gf->gf_linkno);
778
779         if (rc == 0)
780                 GOTO(out_fid2path, rc);
781
782         /* sigh, has to go to another MDT to do path building further */
783         if (remote_gf == NULL) {
784                 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
785                 OBD_ALLOC(remote_gf, remote_gf_size);
786                 if (remote_gf == NULL)
787                         GOTO(out_fid2path, rc = -ENOMEM);
788                 remote_gf->gf_pathlen = PATH_MAX;
789         }
790
791         if (!fid_is_sane(&gf->gf_fid)) {
792                 CERROR("%s: invalid FID "DFID": rc = %d\n",
793                        tgt->ltd_exp->exp_obd->obd_name,
794                        PFID(&gf->gf_fid), -EINVAL);
795                 GOTO(out_fid2path, rc = -EINVAL);
796         }
797
798         tgt = lmv_find_target(lmv, &gf->gf_fid);
799         if (IS_ERR(tgt))
800                 GOTO(out_fid2path, rc = -EINVAL);
801
802         remote_gf->gf_fid = gf->gf_fid;
803         remote_gf->gf_recno = -1;
804         remote_gf->gf_linkno = -1;
805         memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
806         gf = remote_gf;
807         goto repeat_fid2path;
808
809 out_fid2path:
810         if (remote_gf != NULL)
811                 OBD_FREE(remote_gf, remote_gf_size);
812         RETURN(rc);
813 }
814
815 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
816                          int len, void *karg, void *uarg)
817 {
818         struct obd_device    *obddev = class_exp2obd(exp);
819         struct lmv_obd       *lmv = &obddev->u.lmv;
820         int                   i = 0;
821         int                   rc = 0;
822         int                   set = 0;
823         int                   count = lmv->desc.ld_tgt_count;
824         ENTRY;
825
826         if (count == 0)
827                 RETURN(-ENOTTY);
828
829         switch (cmd) {
830         case IOC_OBD_STATFS: {
831                 struct obd_ioctl_data *data = karg;
832                 struct obd_device *mdc_obd;
833                 struct obd_statfs stat_buf = {0};
834                 __u32 index;
835
836                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
837                 if ((index >= count))
838                         RETURN(-ENODEV);
839
840                 if (lmv->tgts[index] == NULL ||
841                     lmv->tgts[index]->ltd_active == 0)
842                         RETURN(-ENODATA);
843
844                 mdc_obd = class_exp2obd(lmv->tgts[index]->ltd_exp);
845                 if (!mdc_obd)
846                         RETURN(-EINVAL);
847
848                 /* copy UUID */
849                 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
850                                      min((int) data->ioc_plen2,
851                                          (int) sizeof(struct obd_uuid))))
852                         RETURN(-EFAULT);
853
854                 rc = obd_statfs(NULL, lmv->tgts[index]->ltd_exp, &stat_buf,
855                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
856                                 0);
857                 if (rc)
858                         RETURN(rc);
859                 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
860                                      min((int) data->ioc_plen1,
861                                          (int) sizeof(stat_buf))))
862                         RETURN(-EFAULT);
863                 break;
864         }
865         case OBD_IOC_QUOTACTL: {
866                 struct if_quotactl *qctl = karg;
867                 struct lmv_tgt_desc *tgt = NULL;
868                 struct obd_quotactl *oqctl;
869
870                 if (qctl->qc_valid == QC_MDTIDX) {
871                         if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
872                                 RETURN(-EINVAL);
873
874                         tgt = lmv->tgts[qctl->qc_idx];
875                         if (tgt == NULL || tgt->ltd_exp == NULL)
876                                 RETURN(-EINVAL);
877                 } else if (qctl->qc_valid == QC_UUID) {
878                         for (i = 0; i < count; i++) {
879                                 tgt = lmv->tgts[i];
880                                 if (tgt == NULL)
881                                         continue;
882                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
883                                                      &qctl->obd_uuid))
884                                         continue;
885
886                                 if (tgt->ltd_exp == NULL)
887                                         RETURN(-EINVAL);
888
889                                 break;
890                         }
891                 } else {
892                         RETURN(-EINVAL);
893                 }
894
895                 if (i >= count)
896                         RETURN(-EAGAIN);
897
898                 LASSERT(tgt && tgt->ltd_exp);
899                 OBD_ALLOC_PTR(oqctl);
900                 if (!oqctl)
901                         RETURN(-ENOMEM);
902
903                 QCTL_COPY(oqctl, qctl);
904                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
905                 if (rc == 0) {
906                         QCTL_COPY(qctl, oqctl);
907                         qctl->qc_valid = QC_MDTIDX;
908                         qctl->obd_uuid = tgt->ltd_uuid;
909                 }
910                 OBD_FREE_PTR(oqctl);
911                 break;
912         }
913         case OBD_IOC_CHANGELOG_SEND:
914         case OBD_IOC_CHANGELOG_CLEAR: {
915                 struct ioc_changelog *icc = karg;
916
917                 if (icc->icc_mdtindex >= count)
918                         RETURN(-ENODEV);
919
920                 if (lmv->tgts[icc->icc_mdtindex] == NULL ||
921                     lmv->tgts[icc->icc_mdtindex]->ltd_exp == NULL ||
922                     lmv->tgts[icc->icc_mdtindex]->ltd_active == 0)
923                         RETURN(-ENODEV);
924                 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex]->ltd_exp,
925                                    sizeof(*icc), icc, NULL);
926                 break;
927         }
928         case LL_IOC_GET_CONNECT_FLAGS: {
929                 if (lmv->tgts[0] == NULL)
930                         RETURN(-ENODATA);
931                 rc = obd_iocontrol(cmd, lmv->tgts[0]->ltd_exp, len, karg, uarg);
932                 break;
933         }
934         case OBD_IOC_FID2PATH: {
935                 rc = lmv_fid2path(exp, len, karg, uarg);
936                 break;
937         }
938         case LL_IOC_HSM_STATE_GET:
939         case LL_IOC_HSM_STATE_SET:
940         case LL_IOC_HSM_ACTION:
941         case LL_IOC_LOV_SWAP_LAYOUTS: {
942                 struct md_op_data       *op_data = karg;
943                 struct lmv_tgt_desc     *tgt1, *tgt2;
944
945                 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
946                 if (IS_ERR(tgt1))
947                         RETURN(PTR_ERR(tgt1));
948
949                 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
950                 if (IS_ERR(tgt2))
951                         RETURN(PTR_ERR(tgt2));
952
953                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
954                         RETURN(-EINVAL);
955
956                 /* only files on same MDT can have their layouts swapped */
957                 if (tgt1->ltd_idx != tgt2->ltd_idx)
958                         RETURN(-EPERM);
959
960                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
961                 break;
962         }
963         default:
964                 for (i = 0; i < count; i++) {
965                         struct obd_device *mdc_obd;
966                         int err;
967
968                         if (lmv->tgts[i] == NULL ||
969                             lmv->tgts[i]->ltd_exp == NULL)
970                                 continue;
971                         /* ll_umount_begin() sets force flag but for lmv, not
972                          * mdc. Let's pass it through */
973                         mdc_obd = class_exp2obd(lmv->tgts[i]->ltd_exp);
974                         mdc_obd->obd_force = obddev->obd_force;
975                         err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len,
976                                             karg, uarg);
977                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
978                                 RETURN(err);
979                         } else if (err) {
980                                 if (lmv->tgts[i]->ltd_active) {
981                                         CERROR("error: iocontrol MDC %s on MDT"
982                                                "idx %d cmd %x: err = %d\n",
983                                                 lmv->tgts[i]->ltd_uuid.uuid,
984                                                 i, cmd, err);
985                                         if (!rc)
986                                                 rc = err;
987                                 }
988                         } else
989                                 set = 1;
990                 }
991                 if (!set && !rc)
992                         rc = -EIO;
993         }
994         RETURN(rc);
995 }
996
997 #if 0
998 static int lmv_all_chars_policy(int count, const char *name,
999                                 int len)
1000 {
1001         unsigned int c = 0;
1002
1003         while (len > 0)
1004                 c += name[--len];
1005         c = c % count;
1006         return c;
1007 }
1008
1009 static int lmv_nid_policy(struct lmv_obd *lmv)
1010 {
1011         struct obd_import *imp;
1012         __u32              id;
1013
1014         /*
1015          * XXX: To get nid we assume that underlying obd device is mdc.
1016          */
1017         imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1018         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1019         return id % lmv->desc.ld_tgt_count;
1020 }
1021
1022 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1023                           placement_policy_t placement)
1024 {
1025         switch (placement) {
1026         case PLACEMENT_CHAR_POLICY:
1027                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1028                                             op_data->op_name,
1029                                             op_data->op_namelen);
1030         case PLACEMENT_NID_POLICY:
1031                 return lmv_nid_policy(lmv);
1032
1033         default:
1034                 break;
1035         }
1036
1037         CERROR("Unsupported placement policy %x\n", placement);
1038         return -EINVAL;
1039 }
1040 #endif
1041
1042 /**
1043  * This is _inode_ placement policy function (not name).
1044  */
1045 static int lmv_placement_policy(struct obd_device *obd,
1046                                 struct md_op_data *op_data,
1047                                 mdsno_t *mds)
1048 {
1049         struct lmv_obd          *lmv = &obd->u.lmv;
1050         ENTRY;
1051
1052         LASSERT(mds != NULL);
1053
1054         if (lmv->desc.ld_tgt_count == 1) {
1055                 *mds = 0;
1056                 RETURN(0);
1057         }
1058
1059         /**
1060          * If stripe_offset is provided during setdirstripe
1061          * (setdirstripe -i xx), xx MDS will be choosen.
1062          */
1063         if (op_data->op_cli_flags & CLI_SET_MEA) {
1064                 struct lmv_user_md *lum;
1065
1066                 lum = (struct lmv_user_md *)op_data->op_data;
1067                 if (lum->lum_type == LMV_STRIPE_TYPE &&
1068                     lum->lum_stripe_offset != -1) {
1069                         if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1070                                 CERROR("%s: Stripe_offset %d > MDT count %d:"
1071                                        " rc = %d\n", obd->obd_name,
1072                                        lum->lum_stripe_offset,
1073                                        lmv->desc.ld_tgt_count, -ERANGE);
1074                                 RETURN(-ERANGE);
1075                         }
1076                         *mds = lum->lum_stripe_offset;
1077                         RETURN(0);
1078                 }
1079         }
1080
1081         /* Allocate new fid on target according to operation type and parent
1082          * home mds. */
1083         *mds = op_data->op_mds;
1084         RETURN(0);
1085 }
1086
1087 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1088                     mdsno_t mds)
1089 {
1090         struct lmv_tgt_desc     *tgt;
1091         int                      rc;
1092         ENTRY;
1093
1094         tgt = lmv_get_target(lmv, mds);
1095         if (IS_ERR(tgt))
1096                 RETURN(PTR_ERR(tgt));
1097
1098         /*
1099          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1100          * on server that seq in new allocated fid is not yet known.
1101          */
1102         mutex_lock(&tgt->ltd_fid_mutex);
1103
1104         if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1105                 GOTO(out, rc = -ENODEV);
1106
1107         /*
1108          * Asking underlaying tgt layer to allocate new fid.
1109          */
1110         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1111         if (rc > 0) {
1112                 LASSERT(fid_is_sane(fid));
1113                 rc = 0;
1114         }
1115
1116         EXIT;
1117 out:
1118         mutex_unlock(&tgt->ltd_fid_mutex);
1119         return rc;
1120 }
1121
1122 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1123                   struct md_op_data *op_data)
1124 {
1125         struct obd_device     *obd = class_exp2obd(exp);
1126         struct lmv_obd        *lmv = &obd->u.lmv;
1127         mdsno_t                mds = 0;
1128         int                    rc;
1129         ENTRY;
1130
1131         LASSERT(op_data != NULL);
1132         LASSERT(fid != NULL);
1133
1134         rc = lmv_placement_policy(obd, op_data, &mds);
1135         if (rc) {
1136                 CERROR("Can't get target for allocating fid, "
1137                        "rc %d\n", rc);
1138                 RETURN(rc);
1139         }
1140
1141         rc = __lmv_fid_alloc(lmv, fid, mds);
1142         if (rc) {
1143                 CERROR("Can't alloc new fid, rc %d\n", rc);
1144                 RETURN(rc);
1145         }
1146
1147         RETURN(rc);
1148 }
1149
1150 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1151 {
1152         struct lmv_obd             *lmv = &obd->u.lmv;
1153         struct lprocfs_static_vars  lvars;
1154         struct lmv_desc            *desc;
1155         int                         rc;
1156         ENTRY;
1157
1158         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1159                 CERROR("LMV setup requires a descriptor\n");
1160                 RETURN(-EINVAL);
1161         }
1162
1163         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1164         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1165                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1166                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1167                 RETURN(-EINVAL);
1168         }
1169
1170         OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1171         if (lmv->tgts == NULL)
1172                 RETURN(-ENOMEM);
1173         lmv->tgts_size = 32;
1174
1175         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1176         lmv->desc.ld_tgt_count = 0;
1177         lmv->desc.ld_active_tgt_count = 0;
1178         lmv->max_cookiesize = 0;
1179         lmv->max_def_easize = 0;
1180         lmv->max_easize = 0;
1181         lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1182
1183         spin_lock_init(&lmv->lmv_lock);
1184         mutex_init(&lmv->init_mutex);
1185
1186         lprocfs_lmv_init_vars(&lvars);
1187
1188         lprocfs_obd_setup(obd, lvars.obd_vars);
1189 #ifdef LPROCFS
1190         {
1191                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1192                                         0444, &lmv_proc_target_fops, obd);
1193                 if (rc)
1194                         CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1195                                obd->obd_name, rc);
1196        }
1197 #endif
1198         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1199                              LUSTRE_CLI_FLD_HASH_DHT);
1200         if (rc) {
1201                 CERROR("Can't init FLD, err %d\n", rc);
1202                 GOTO(out, rc);
1203         }
1204
1205         RETURN(0);
1206
1207 out:
1208         return rc;
1209 }
1210
1211 static int lmv_cleanup(struct obd_device *obd)
1212 {
1213         struct lmv_obd   *lmv = &obd->u.lmv;
1214         ENTRY;
1215
1216         fld_client_fini(&lmv->lmv_fld);
1217         if (lmv->tgts != NULL) {
1218                 int i;
1219                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1220                         if (lmv->tgts[i] == NULL)
1221                                 continue;
1222                         lmv_del_target(lmv, i);
1223                 }
1224                 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1225                 lmv->tgts_size = 0;
1226         }
1227         RETURN(0);
1228 }
1229
1230 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1231 {
1232         struct lustre_cfg       *lcfg = buf;
1233         struct obd_uuid         obd_uuid;
1234         int                     gen;
1235         __u32                   index;
1236         int                     rc;
1237         ENTRY;
1238
1239         switch (lcfg->lcfg_command) {
1240         case LCFG_ADD_MDC:
1241                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1242                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
1243                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1244                         GOTO(out, rc = -EINVAL);
1245
1246                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1247
1248                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1249                         GOTO(out, rc = -EINVAL);
1250                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1251                         GOTO(out, rc = -EINVAL);
1252                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1253                 GOTO(out, rc);
1254         default:
1255                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1256                 GOTO(out, rc = -EINVAL);
1257         }
1258 out:
1259         RETURN(rc);
1260 }
1261
1262 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1263                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1264 {
1265         struct obd_device     *obd = class_exp2obd(exp);
1266         struct lmv_obd        *lmv = &obd->u.lmv;
1267         struct obd_statfs     *temp;
1268         int                    rc = 0;
1269         int                    i;
1270         ENTRY;
1271
1272         rc = lmv_check_connect(obd);
1273         if (rc)
1274                 RETURN(rc);
1275
1276         OBD_ALLOC(temp, sizeof(*temp));
1277         if (temp == NULL)
1278                 RETURN(-ENOMEM);
1279
1280         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1281                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1282                         continue;
1283
1284                 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1285                                 max_age, flags);
1286                 if (rc) {
1287                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1288                                lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1289                                rc);
1290                         GOTO(out_free_temp, rc);
1291                 }
1292
1293                 if (i == 0) {
1294                         *osfs = *temp;
1295                         /* If the statfs is from mount, it will needs
1296                          * retrieve necessary information from MDT0.
1297                          * i.e. mount does not need the merged osfs
1298                          * from all of MDT.
1299                          * And also clients can be mounted as long as
1300                          * MDT0 is in service*/
1301                         if (flags & OBD_STATFS_FOR_MDT0)
1302                                 GOTO(out_free_temp, rc);
1303                 } else {
1304                         osfs->os_bavail += temp->os_bavail;
1305                         osfs->os_blocks += temp->os_blocks;
1306                         osfs->os_ffree += temp->os_ffree;
1307                         osfs->os_files += temp->os_files;
1308                 }
1309         }
1310
1311         EXIT;
1312 out_free_temp:
1313         OBD_FREE(temp, sizeof(*temp));
1314         return rc;
1315 }
1316
1317 static int lmv_getstatus(struct obd_export *exp,
1318                          struct lu_fid *fid,
1319                          struct obd_capa **pc)
1320 {
1321         struct obd_device    *obd = exp->exp_obd;
1322         struct lmv_obd       *lmv = &obd->u.lmv;
1323         int                   rc;
1324         ENTRY;
1325
1326         rc = lmv_check_connect(obd);
1327         if (rc)
1328                 RETURN(rc);
1329
1330         rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1331         RETURN(rc);
1332 }
1333
1334 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1335                         struct obd_capa *oc, obd_valid valid, const char *name,
1336                         const char *input, int input_size, int output_size,
1337                         int flags, struct ptlrpc_request **request)
1338 {
1339         struct obd_device      *obd = exp->exp_obd;
1340         struct lmv_obd         *lmv = &obd->u.lmv;
1341         struct lmv_tgt_desc    *tgt;
1342         int                     rc;
1343         ENTRY;
1344
1345         rc = lmv_check_connect(obd);
1346         if (rc)
1347                 RETURN(rc);
1348
1349         tgt = lmv_find_target(lmv, fid);
1350         if (IS_ERR(tgt))
1351                 RETURN(PTR_ERR(tgt));
1352
1353         rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1354                          input_size, output_size, flags, request);
1355
1356         RETURN(rc);
1357 }
1358
1359 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1360                         struct obd_capa *oc, obd_valid valid, const char *name,
1361                         const char *input, int input_size, int output_size,
1362                         int flags, __u32 suppgid,
1363                         struct ptlrpc_request **request)
1364 {
1365         struct obd_device      *obd = exp->exp_obd;
1366         struct lmv_obd         *lmv = &obd->u.lmv;
1367         struct lmv_tgt_desc    *tgt;
1368         int                     rc;
1369         ENTRY;
1370
1371         rc = lmv_check_connect(obd);
1372         if (rc)
1373                 RETURN(rc);
1374
1375         tgt = lmv_find_target(lmv, fid);
1376         if (IS_ERR(tgt))
1377                 RETURN(PTR_ERR(tgt));
1378
1379         rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1380                          input_size, output_size, flags, suppgid,
1381                          request);
1382
1383         RETURN(rc);
1384 }
1385
1386 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1387                        struct ptlrpc_request **request)
1388 {
1389         struct obd_device       *obd = exp->exp_obd;
1390         struct lmv_obd          *lmv = &obd->u.lmv;
1391         struct lmv_tgt_desc     *tgt;
1392         int                      rc;
1393         ENTRY;
1394
1395         rc = lmv_check_connect(obd);
1396         if (rc)
1397                 RETURN(rc);
1398
1399         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1400         if (IS_ERR(tgt))
1401                 RETURN(PTR_ERR(tgt));
1402
1403         if (op_data->op_flags & MF_GET_MDT_IDX) {
1404                 op_data->op_mds = tgt->ltd_idx;
1405                 RETURN(0);
1406         }
1407
1408         rc = md_getattr(tgt->ltd_exp, op_data, request);
1409
1410         RETURN(rc);
1411 }
1412
1413 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1414 {
1415         struct obd_device   *obd = exp->exp_obd;
1416         struct lmv_obd      *lmv = &obd->u.lmv;
1417         int                  i;
1418         int                  rc;
1419         ENTRY;
1420
1421         rc = lmv_check_connect(obd);
1422         if (rc)
1423                 RETURN(rc);
1424
1425         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1426
1427         /*
1428          * With DNE every object can have two locks in different namespaces:
1429          * lookup lock in space of MDT storing direntry and update/open lock in
1430          * space of MDT storing inode.
1431          */
1432         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1433                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1434                         continue;
1435                 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1436         }
1437
1438         RETURN(0);
1439 }
1440
1441 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1442                            ldlm_iterator_t it, void *data)
1443 {
1444         struct obd_device   *obd = exp->exp_obd;
1445         struct lmv_obd      *lmv = &obd->u.lmv;
1446         int                  i;
1447         int                  rc;
1448         ENTRY;
1449
1450         rc = lmv_check_connect(obd);
1451         if (rc)
1452                 RETURN(rc);
1453
1454         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1455
1456         /*
1457          * With DNE every object can have two locks in different namespaces:
1458          * lookup lock in space of MDT storing direntry and update/open lock in
1459          * space of MDT storing inode.
1460          */
1461         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1462                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1463                         continue;
1464                 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1465                 if (rc)
1466                         RETURN(rc);
1467         }
1468
1469         RETURN(rc);
1470 }
1471
1472
1473 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1474                      struct md_open_data *mod, struct ptlrpc_request **request)
1475 {
1476         struct obd_device     *obd = exp->exp_obd;
1477         struct lmv_obd        *lmv = &obd->u.lmv;
1478         struct lmv_tgt_desc   *tgt;
1479         int                    rc;
1480         ENTRY;
1481
1482         rc = lmv_check_connect(obd);
1483         if (rc)
1484                 RETURN(rc);
1485
1486         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1487         if (IS_ERR(tgt))
1488                 RETURN(PTR_ERR(tgt));
1489
1490         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1491         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1492         RETURN(rc);
1493 }
1494
1495 struct lmv_tgt_desc
1496 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1497                 struct lu_fid *fid)
1498 {
1499         struct lmv_tgt_desc *tgt;
1500
1501         tgt = lmv_find_target(lmv, fid);
1502         if (IS_ERR(tgt))
1503                 return tgt;
1504
1505         op_data->op_mds = tgt->ltd_idx;
1506
1507         return tgt;
1508 }
1509
1510 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1511                const void *data, int datalen, int mode, __u32 uid,
1512                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1513                struct ptlrpc_request **request)
1514 {
1515         struct obd_device       *obd = exp->exp_obd;
1516         struct lmv_obd          *lmv = &obd->u.lmv;
1517         struct lmv_tgt_desc     *tgt;
1518         int                      rc;
1519         ENTRY;
1520
1521         rc = lmv_check_connect(obd);
1522         if (rc)
1523                 RETURN(rc);
1524
1525         if (!lmv->desc.ld_active_tgt_count)
1526                 RETURN(-EIO);
1527
1528         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1529         if (IS_ERR(tgt))
1530                 RETURN(PTR_ERR(tgt));
1531
1532         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1533         if (rc)
1534                 RETURN(rc);
1535
1536         CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1537                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1538                op_data->op_mds);
1539
1540         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1541         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1542                        cap_effective, rdev, request);
1543
1544         if (rc == 0) {
1545                 if (*request == NULL)
1546                         RETURN(rc);
1547                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1548         }
1549         RETURN(rc);
1550 }
1551
1552 static int lmv_done_writing(struct obd_export *exp,
1553                             struct md_op_data *op_data,
1554                             struct md_open_data *mod)
1555 {
1556         struct obd_device     *obd = exp->exp_obd;
1557         struct lmv_obd        *lmv = &obd->u.lmv;
1558         struct lmv_tgt_desc   *tgt;
1559         int                    rc;
1560         ENTRY;
1561
1562         rc = lmv_check_connect(obd);
1563         if (rc)
1564                 RETURN(rc);
1565
1566         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1567         if (IS_ERR(tgt))
1568                 RETURN(PTR_ERR(tgt));
1569
1570         rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1571         RETURN(rc);
1572 }
1573
1574 static int
1575 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1576                    struct lookup_intent *it, struct md_op_data *op_data,
1577                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1578                    int extra_lock_flags)
1579 {
1580         struct ptlrpc_request      *req = it->d.lustre.it_data;
1581         struct obd_device          *obd = exp->exp_obd;
1582         struct lmv_obd             *lmv = &obd->u.lmv;
1583         struct lustre_handle        plock;
1584         struct lmv_tgt_desc        *tgt;
1585         struct md_op_data          *rdata;
1586         struct lu_fid               fid1;
1587         struct mdt_body            *body;
1588         int                         rc = 0;
1589         int                         pmode;
1590         ENTRY;
1591
1592         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1593         LASSERT(body != NULL);
1594
1595         if (!(body->valid & OBD_MD_MDS))
1596                 RETURN(0);
1597
1598         CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1599                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1600
1601         /*
1602          * We got LOOKUP lock, but we really need attrs.
1603          */
1604         pmode = it->d.lustre.it_lock_mode;
1605         LASSERT(pmode != 0);
1606         memcpy(&plock, lockh, sizeof(plock));
1607         it->d.lustre.it_lock_mode = 0;
1608         it->d.lustre.it_data = NULL;
1609         fid1 = body->fid1;
1610
1611         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1612         ptlrpc_req_finished(req);
1613
1614         tgt = lmv_find_target(lmv, &fid1);
1615         if (IS_ERR(tgt))
1616                 GOTO(out, rc = PTR_ERR(tgt));
1617
1618         OBD_ALLOC_PTR(rdata);
1619         if (rdata == NULL)
1620                 GOTO(out, rc = -ENOMEM);
1621
1622         rdata->op_fid1 = fid1;
1623         rdata->op_bias = MDS_CROSS_REF;
1624
1625         rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1626                         lmm, lmmsize, NULL, extra_lock_flags);
1627         OBD_FREE_PTR(rdata);
1628         EXIT;
1629 out:
1630         ldlm_lock_decref(&plock, pmode);
1631         return rc;
1632 }
1633
1634 static int
1635 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1636             struct lookup_intent *it, struct md_op_data *op_data,
1637             struct lustre_handle *lockh, void *lmm, int lmmsize,
1638             struct ptlrpc_request **req, __u64 extra_lock_flags)
1639 {
1640         struct obd_device        *obd = exp->exp_obd;
1641         struct lmv_obd           *lmv = &obd->u.lmv;
1642         struct lmv_tgt_desc      *tgt;
1643         int                       rc;
1644         ENTRY;
1645
1646         rc = lmv_check_connect(obd);
1647         if (rc)
1648                 RETURN(rc);
1649
1650         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1651                LL_IT2STR(it), PFID(&op_data->op_fid1));
1652
1653         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1654         if (IS_ERR(tgt))
1655                 RETURN(PTR_ERR(tgt));
1656
1657         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1658                LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1659
1660         rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1661                         lmm, lmmsize, req, extra_lock_flags);
1662
1663         if (rc == 0 && it && it->it_op == IT_OPEN) {
1664                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1665                                         lmm, lmmsize, extra_lock_flags);
1666         }
1667         RETURN(rc);
1668 }
1669
1670 static int
1671 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1672                  struct ptlrpc_request **request)
1673 {
1674         struct ptlrpc_request   *req = NULL;
1675         struct obd_device       *obd = exp->exp_obd;
1676         struct lmv_obd          *lmv = &obd->u.lmv;
1677         struct lmv_tgt_desc     *tgt;
1678         struct mdt_body         *body;
1679         int                      rc;
1680         ENTRY;
1681
1682         rc = lmv_check_connect(obd);
1683         if (rc)
1684                 RETURN(rc);
1685
1686         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1687         if (IS_ERR(tgt))
1688                 RETURN(PTR_ERR(tgt));
1689
1690         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1691                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1692                tgt->ltd_idx);
1693
1694         rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1695         if (rc != 0)
1696                 RETURN(rc);
1697
1698         body = req_capsule_server_get(&(*request)->rq_pill,
1699                                       &RMF_MDT_BODY);
1700         LASSERT(body != NULL);
1701
1702         if (body->valid & OBD_MD_MDS) {
1703                 struct lu_fid rid = body->fid1;
1704                 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1705                        PFID(&rid));
1706
1707                 tgt = lmv_find_target(lmv, &rid);
1708                 if (IS_ERR(tgt)) {
1709                         ptlrpc_req_finished(*request);
1710                         RETURN(PTR_ERR(tgt));
1711                 }
1712
1713                 op_data->op_fid1 = rid;
1714                 op_data->op_valid |= OBD_MD_FLCROSSREF;
1715                 op_data->op_namelen = 0;
1716                 op_data->op_name = NULL;
1717                 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1718                 ptlrpc_req_finished(*request);
1719                 *request = req;
1720         }
1721
1722         RETURN(rc);
1723 }
1724
1725 #define md_op_data_fid(op_data, fl)                     \
1726         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1727          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1728          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1729          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1730          NULL)
1731
1732 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1733                             int op_tgt, ldlm_mode_t mode, int bits, int flag)
1734 {
1735         struct lu_fid          *fid = md_op_data_fid(op_data, flag);
1736         struct obd_device      *obd = exp->exp_obd;
1737         struct lmv_obd         *lmv = &obd->u.lmv;
1738         struct lmv_tgt_desc    *tgt;
1739         ldlm_policy_data_t      policy = {{0}};
1740         int                     rc = 0;
1741         ENTRY;
1742
1743         if (!fid_is_sane(fid))
1744                 RETURN(0);
1745
1746         tgt = lmv_find_target(lmv, fid);
1747         if (IS_ERR(tgt))
1748                 RETURN(PTR_ERR(tgt));
1749
1750         if (tgt->ltd_idx != op_tgt) {
1751                 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1752                 policy.l_inodebits.bits = bits;
1753                 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1754                                       mode, LCF_ASYNC, NULL);
1755         } else {
1756                 CDEBUG(D_INODE,
1757                        "EARLY_CANCEL skip operation target %d on "DFID"\n",
1758                        op_tgt, PFID(fid));
1759                 op_data->op_flags |= flag;
1760                 rc = 0;
1761         }
1762
1763         RETURN(rc);
1764 }
1765
1766 /*
1767  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1768  * op_data->op_fid2
1769  */
1770 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1771                     struct ptlrpc_request **request)
1772 {
1773         struct obd_device       *obd = exp->exp_obd;
1774         struct lmv_obd          *lmv = &obd->u.lmv;
1775         struct lmv_tgt_desc     *tgt;
1776         int                      rc;
1777         ENTRY;
1778
1779         rc = lmv_check_connect(obd);
1780         if (rc)
1781                 RETURN(rc);
1782
1783         LASSERT(op_data->op_namelen != 0);
1784
1785         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1786                PFID(&op_data->op_fid2), op_data->op_namelen,
1787                op_data->op_name, PFID(&op_data->op_fid1));
1788
1789         op_data->op_fsuid = cfs_curproc_fsuid();
1790         op_data->op_fsgid = cfs_curproc_fsgid();
1791         op_data->op_cap = cfs_curproc_cap_pack();
1792         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1793         if (IS_ERR(tgt))
1794                 RETURN(PTR_ERR(tgt));
1795
1796         /*
1797          * Cancel UPDATE lock on child (fid1).
1798          */
1799         op_data->op_flags |= MF_MDC_CANCEL_FID2;
1800         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
1801                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1802         if (rc != 0)
1803                 RETURN(rc);
1804
1805         rc = md_link(tgt->ltd_exp, op_data, request);
1806
1807         RETURN(rc);
1808 }
1809
1810 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1811                       const char *old, int oldlen, const char *new, int newlen,
1812                       struct ptlrpc_request **request)
1813 {
1814         struct obd_device       *obd = exp->exp_obd;
1815         struct lmv_obd          *lmv = &obd->u.lmv;
1816         struct lmv_tgt_desc     *src_tgt;
1817         struct lmv_tgt_desc     *tgt_tgt;
1818         int                     rc;
1819         ENTRY;
1820
1821         LASSERT(oldlen != 0);
1822
1823         CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
1824                oldlen, old, PFID(&op_data->op_fid1),
1825                newlen, new, PFID(&op_data->op_fid2));
1826
1827         rc = lmv_check_connect(obd);
1828         if (rc)
1829                 RETURN(rc);
1830
1831         op_data->op_fsuid = cfs_curproc_fsuid();
1832         op_data->op_fsgid = cfs_curproc_fsgid();
1833         op_data->op_cap = cfs_curproc_cap_pack();
1834         src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1835         if (IS_ERR(src_tgt))
1836                 RETURN(PTR_ERR(src_tgt));
1837
1838         tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1839         if (IS_ERR(tgt_tgt))
1840                 RETURN(PTR_ERR(tgt_tgt));
1841         /*
1842          * LOOKUP lock on src child (fid3) should also be cancelled for
1843          * src_tgt in mdc_rename.
1844          */
1845         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1846
1847         /*
1848          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
1849          * own target.
1850          */
1851         rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
1852                               LCK_EX, MDS_INODELOCK_UPDATE,
1853                               MF_MDC_CANCEL_FID2);
1854
1855         /*
1856          * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
1857          */
1858         if (rc == 0) {
1859                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
1860                                       LCK_EX, MDS_INODELOCK_LOOKUP,
1861                                       MF_MDC_CANCEL_FID4);
1862         }
1863
1864         /*
1865          * Cancel all the locks on tgt child (fid4).
1866          */
1867         if (rc == 0)
1868                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
1869                                       LCK_EX, MDS_INODELOCK_FULL,
1870                                       MF_MDC_CANCEL_FID4);
1871
1872         if (rc == 0)
1873                 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
1874                                new, newlen, request);
1875         RETURN(rc);
1876 }
1877
1878 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
1879                        void *ea, int ealen, void *ea2, int ea2len,
1880                        struct ptlrpc_request **request,
1881                        struct md_open_data **mod)
1882 {
1883         struct obd_device       *obd = exp->exp_obd;
1884         struct lmv_obd          *lmv = &obd->u.lmv;
1885         struct lmv_tgt_desc     *tgt;
1886         int                      rc = 0;
1887         ENTRY;
1888
1889         rc = lmv_check_connect(obd);
1890         if (rc)
1891                 RETURN(rc);
1892
1893         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
1894                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
1895
1896         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1897         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1898         if (IS_ERR(tgt))
1899                 RETURN(PTR_ERR(tgt));
1900
1901         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
1902                         ea2len, request, mod);
1903
1904         RETURN(rc);
1905 }
1906
1907 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
1908                     struct obd_capa *oc, struct ptlrpc_request **request)
1909 {
1910         struct obd_device         *obd = exp->exp_obd;
1911         struct lmv_obd            *lmv = &obd->u.lmv;
1912         struct lmv_tgt_desc       *tgt;
1913         int                        rc;
1914         ENTRY;
1915
1916         rc = lmv_check_connect(obd);
1917         if (rc)
1918                 RETURN(rc);
1919
1920         tgt = lmv_find_target(lmv, fid);
1921         if (IS_ERR(tgt))
1922                 RETURN(PTR_ERR(tgt));
1923
1924         rc = md_sync(tgt->ltd_exp, fid, oc, request);
1925         RETURN(rc);
1926 }
1927
1928 /*
1929  * Adjust a set of pages, each page containing an array of lu_dirpages,
1930  * so that each page can be used as a single logical lu_dirpage.
1931  *
1932  * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
1933  * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
1934  * struct lu_dirent.  It has size up to LU_PAGE_SIZE. The ldp_hash_end
1935  * value is used as a cookie to request the next lu_dirpage in a
1936  * directory listing that spans multiple pages (two in this example):
1937  *   ________
1938  *  |        |
1939  * .|--------v-------   -----.
1940  * |s|e|f|p|ent|ent| ... |ent|
1941  * '--|--------------   -----'   Each CFS_PAGE contains a single
1942  *    '------.                   lu_dirpage.
1943  * .---------v-------   -----.
1944  * |s|e|f|p|ent| 0 | ... | 0 |
1945  * '-----------------   -----'
1946  *
1947  * However, on hosts where the native VM page size (CFS_PAGE_SIZE) is
1948  * larger than LU_PAGE_SIZE, a single host page may contain multiple
1949  * lu_dirpages. After reading the lu_dirpages from the MDS, the
1950  * ldp_hash_end of the first lu_dirpage refers to the one immediately
1951  * after it in the same CFS_PAGE (arrows simplified for brevity, but
1952  * in general e0==s1, e1==s2, etc.):
1953  *
1954  * .--------------------   -----.
1955  * |s0|e0|f0|p|ent|ent| ... |ent|
1956  * |---v----------------   -----|
1957  * |s1|e1|f1|p|ent|ent| ... |ent|
1958  * |---v----------------   -----|  Here, each CFS_PAGE contains
1959  *             ...                 multiple lu_dirpages.
1960  * |---v----------------   -----|
1961  * |s'|e'|f'|p|ent|ent| ... |ent|
1962  * '---|----------------   -----'
1963  *     v
1964  * .----------------------------.
1965  * |        next CFS_PAGE       |
1966  *
1967  * This structure is transformed into a single logical lu_dirpage as follows:
1968  *
1969  * - Replace e0 with e' so the request for the next lu_dirpage gets the page
1970  *   labeled 'next CFS_PAGE'.
1971  *
1972  * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
1973  *   a hash collision with the next page exists.
1974  *
1975  * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
1976  *   to the first entry of the next lu_dirpage.
1977  */
1978 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
1979 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
1980 {
1981         int i;
1982
1983         for (i = 0; i < ncfspgs; i++) {
1984                 struct lu_dirpage       *dp = cfs_kmap(pages[i]);
1985                 struct lu_dirpage       *first = dp;
1986                 struct lu_dirent        *end_dirent = NULL;
1987                 struct lu_dirent        *ent;
1988                 __u64                   hash_end = dp->ldp_hash_end;
1989                 __u32                   flags = dp->ldp_flags;
1990
1991                 for (; nlupgs > 1; nlupgs--) {
1992                         ent = lu_dirent_start(dp);
1993                         for (end_dirent = ent; ent != NULL;
1994                              end_dirent = ent, ent = lu_dirent_next(ent));
1995
1996                         /* Advance dp to next lu_dirpage. */
1997                         dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
1998
1999                         /* Check if we've reached the end of the CFS_PAGE. */
2000                         if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2001                                 break;
2002
2003                         /* Save the hash and flags of this lu_dirpage. */
2004                         hash_end = dp->ldp_hash_end;
2005                         flags = dp->ldp_flags;
2006
2007                         /* Check if lu_dirpage contains no entries. */
2008                         if (!end_dirent)
2009                                 break;
2010
2011                         /* Enlarge the end entry lde_reclen from 0 to
2012                          * first entry of next lu_dirpage. */
2013                         LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2014                         end_dirent->lde_reclen =
2015                                 cpu_to_le16((char *)(dp->ldp_entries) -
2016                                             (char *)end_dirent);
2017                 }
2018
2019                 first->ldp_hash_end = hash_end;
2020                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2021                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2022
2023                 cfs_kunmap(pages[i]);
2024         }
2025 }
2026 #else
2027 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2028 #endif  /* CFS_PAGE_SIZE > LU_PAGE_SIZE */
2029
2030 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2031                         struct page **pages, struct ptlrpc_request **request)
2032 {
2033         struct obd_device       *obd = exp->exp_obd;
2034         struct lmv_obd          *lmv = &obd->u.lmv;
2035         __u64                   offset = op_data->op_offset;
2036         int                     rc;
2037         int                     ncfspgs; /* pages read in CFS_PAGE_SIZE */
2038         int                     nlupgs; /* pages read in LU_PAGE_SIZE */
2039         struct lmv_tgt_desc     *tgt;
2040         ENTRY;
2041
2042         rc = lmv_check_connect(obd);
2043         if (rc)
2044                 RETURN(rc);
2045
2046         CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n",
2047                offset, PFID(&op_data->op_fid1));
2048
2049         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2050         if (IS_ERR(tgt))
2051                 RETURN(PTR_ERR(tgt));
2052
2053         rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2054         if (rc != 0)
2055                 RETURN(rc);
2056
2057         ncfspgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2058                  >> CFS_PAGE_SHIFT;
2059         nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2060         LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2061         LASSERT(ncfspgs > 0 && ncfspgs <= op_data->op_npages);
2062
2063         CDEBUG(D_INODE, "read %d(%d)/%d pages\n", ncfspgs, nlupgs,
2064                op_data->op_npages);
2065
2066         lmv_adjust_dirpages(pages, ncfspgs, nlupgs);
2067
2068         RETURN(rc);
2069 }
2070
2071 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2072                       struct ptlrpc_request **request)
2073 {
2074         struct obd_device       *obd = exp->exp_obd;
2075         struct lmv_obd          *lmv = &obd->u.lmv;
2076         struct lmv_tgt_desc     *tgt = NULL;
2077         struct mdt_body         *body;
2078         int                     rc;
2079         ENTRY;
2080
2081         rc = lmv_check_connect(obd);
2082         if (rc)
2083                 RETURN(rc);
2084 retry:
2085         /* Send unlink requests to the MDT where the child is located */
2086         if (likely(!fid_is_zero(&op_data->op_fid2)))
2087                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2088         else
2089                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2090         if (IS_ERR(tgt))
2091                 RETURN(PTR_ERR(tgt));
2092
2093         op_data->op_fsuid = cfs_curproc_fsuid();
2094         op_data->op_fsgid = cfs_curproc_fsgid();
2095         op_data->op_cap = cfs_curproc_cap_pack();
2096
2097         /*
2098          * If child's fid is given, cancel unused locks for it if it is from
2099          * another export than parent.
2100          *
2101          * LOOKUP lock for child (fid3) should also be cancelled on parent
2102          * tgt_tgt in mdc_unlink().
2103          */
2104         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2105
2106         /*
2107          * Cancel FULL locks on child (fid3).
2108          */
2109         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2110                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2111
2112         if (rc != 0)
2113                 RETURN(rc);
2114
2115         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2116                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2117
2118         rc = md_unlink(tgt->ltd_exp, op_data, request);
2119         if (rc != 0 && rc != -EREMOTE)
2120                 RETURN(rc);
2121
2122         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2123         if (body == NULL)
2124                 RETURN(-EPROTO);
2125
2126         /* Not cross-ref case, just get out of here. */
2127         if (likely(!(body->valid & OBD_MD_MDS)))
2128                 RETURN(0);
2129
2130         CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2131                exp->exp_obd->obd_name, PFID(&body->fid1));
2132
2133         /* This is a remote object, try remote MDT, Note: it may
2134          * try more than 1 time here, Considering following case
2135          * /mnt/lustre is root on MDT0, remote1 is on MDT1
2136          * 1. Initially A does not know where remote1 is, it send
2137          *    unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2138          *    resend unlink RPC to MDT1 (retry 1st time).
2139          *
2140          * 2. During the unlink RPC in flight,
2141          *    client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2142          *    and create new remote1, but on MDT0
2143          *
2144          * 3. MDT1 get unlink RPC(from A), then do remote lock on
2145          *    /mnt/lustre, then lookup get fid of remote1, and find
2146          *    it is remote dir again, and replay -EREMOTE again.
2147          *
2148          * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2149          *
2150          * In theory, it might try unlimited time here, but it should
2151          * be very rare case.  */
2152         op_data->op_fid2 = body->fid1;
2153         ptlrpc_req_finished(*request);
2154         *request = NULL;
2155
2156         goto retry;
2157 }
2158
2159 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2160 {
2161         struct lmv_obd *lmv = &obd->u.lmv;
2162         int rc = 0;
2163
2164         switch (stage) {
2165         case OBD_CLEANUP_EARLY:
2166                 /* XXX: here should be calling obd_precleanup() down to
2167                  * stack. */
2168                 break;
2169         case OBD_CLEANUP_EXPORTS:
2170                 fld_client_proc_fini(&lmv->lmv_fld);
2171                 lprocfs_obd_cleanup(obd);
2172                 break;
2173         default:
2174                 break;
2175         }
2176         RETURN(rc);
2177 }
2178
2179 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2180                         __u32 keylen, void *key, __u32 *vallen, void *val,
2181                         struct lov_stripe_md *lsm)
2182 {
2183         struct obd_device       *obd;
2184         struct lmv_obd          *lmv;
2185         int                      rc = 0;
2186         ENTRY;
2187
2188         obd = class_exp2obd(exp);
2189         if (obd == NULL) {
2190                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2191                        exp->exp_handle.h_cookie);
2192                 RETURN(-EINVAL);
2193         }
2194
2195         lmv = &obd->u.lmv;
2196         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2197                 struct lmv_tgt_desc *tgt;
2198                 int i;
2199
2200                 rc = lmv_check_connect(obd);
2201                 if (rc)
2202                         RETURN(rc);
2203
2204                 LASSERT(*vallen == sizeof(__u32));
2205                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2206                         tgt = lmv->tgts[i];
2207                         /*
2208                          * All tgts should be connected when this gets called.
2209                          */
2210                         if (tgt == NULL || tgt->ltd_exp == NULL)
2211                                 continue;
2212
2213                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2214                                           vallen, val, NULL))
2215                                 RETURN(0);
2216                 }
2217                 RETURN(-EINVAL);
2218         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2219                 rc = lmv_check_connect(obd);
2220                 if (rc)
2221                         RETURN(rc);
2222
2223                 /*
2224                  * Forwarding this request to first MDS, it should know LOV
2225                  * desc.
2226                  */
2227                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2228                                   vallen, val, NULL);
2229                 if (!rc && KEY_IS(KEY_CONN_DATA))
2230                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2231                 RETURN(rc);
2232         } else if (KEY_IS(KEY_TGT_COUNT)) {
2233                 *((int *)val) = lmv->desc.ld_tgt_count;
2234                 RETURN(0);
2235         }
2236
2237         CDEBUG(D_IOCTL, "Invalid key\n");
2238         RETURN(-EINVAL);
2239 }
2240
2241 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2242                        obd_count keylen, void *key, obd_count vallen,
2243                        void *val, struct ptlrpc_request_set *set)
2244 {
2245         struct lmv_tgt_desc    *tgt;
2246         struct obd_device      *obd;
2247         struct lmv_obd         *lmv;
2248         int rc = 0;
2249         ENTRY;
2250
2251         obd = class_exp2obd(exp);
2252         if (obd == NULL) {
2253                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2254                        exp->exp_handle.h_cookie);
2255                 RETURN(-EINVAL);
2256         }
2257         lmv = &obd->u.lmv;
2258
2259         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2260                 int i, err = 0;
2261
2262                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2263                         tgt = lmv->tgts[i];
2264
2265                         if (tgt == NULL || tgt->ltd_exp == NULL)
2266                                 continue;
2267
2268                         err = obd_set_info_async(env, tgt->ltd_exp,
2269                                                  keylen, key, vallen, val, set);
2270                         if (err && rc == 0)
2271                                 rc = err;
2272                 }
2273
2274                 RETURN(rc);
2275         }
2276
2277         RETURN(-EINVAL);
2278 }
2279
2280 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2281                struct lov_stripe_md *lsm)
2282 {
2283         struct obd_device         *obd = class_exp2obd(exp);
2284         struct lmv_obd            *lmv = &obd->u.lmv;
2285         struct lmv_stripe_md      *meap;
2286         struct lmv_stripe_md      *lsmp;
2287         int                        mea_size;
2288         int                        i;
2289         ENTRY;
2290
2291         mea_size = lmv_get_easize(lmv);
2292         if (!lmmp)
2293                 RETURN(mea_size);
2294
2295         if (*lmmp && !lsm) {
2296                 OBD_FREE_LARGE(*lmmp, mea_size);
2297                 *lmmp = NULL;
2298                 RETURN(0);
2299         }
2300
2301         if (*lmmp == NULL) {
2302                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2303                 if (*lmmp == NULL)
2304                         RETURN(-ENOMEM);
2305         }
2306
2307         if (!lsm)
2308                 RETURN(mea_size);
2309
2310         lsmp = (struct lmv_stripe_md *)lsm;
2311         meap = (struct lmv_stripe_md *)*lmmp;
2312
2313         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2314             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2315                 RETURN(-EINVAL);
2316
2317         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2318         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2319         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2320
2321         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2322                 meap->mea_ids[i] = lsmp->mea_ids[i];
2323                 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2324         }
2325
2326         RETURN(mea_size);
2327 }
2328
2329 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2330                  struct lov_mds_md *lmm, int lmm_size)
2331 {
2332         struct obd_device          *obd = class_exp2obd(exp);
2333         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2334         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2335         struct lmv_obd             *lmv = &obd->u.lmv;
2336         int                         mea_size;
2337         int                         i;
2338         __u32                       magic;
2339         ENTRY;
2340
2341         mea_size = lmv_get_easize(lmv);
2342         if (lsmp == NULL)
2343                 return mea_size;
2344
2345         if (*lsmp != NULL && lmm == NULL) {
2346                 OBD_FREE_LARGE(*tmea, mea_size);
2347                 *lsmp = NULL;
2348                 RETURN(0);
2349         }
2350
2351         LASSERT(mea_size == lmm_size);
2352
2353         OBD_ALLOC_LARGE(*tmea, mea_size);
2354         if (*tmea == NULL)
2355                 RETURN(-ENOMEM);
2356
2357         if (!lmm)
2358                 RETURN(mea_size);
2359
2360         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2361             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2362             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2363         {
2364                 magic = le32_to_cpu(mea->mea_magic);
2365         } else {
2366                 /*
2367                  * Old mea is not handled here.
2368                  */
2369                 CERROR("Old not supportable EA is found\n");
2370                 LBUG();
2371         }
2372
2373         (*tmea)->mea_magic = magic;
2374         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2375         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2376
2377         for (i = 0; i < (*tmea)->mea_count; i++) {
2378                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2379                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2380         }
2381         RETURN(mea_size);
2382 }
2383
2384 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2385                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2386                              ldlm_cancel_flags_t flags, void *opaque)
2387 {
2388         struct obd_device       *obd = exp->exp_obd;
2389         struct lmv_obd          *lmv = &obd->u.lmv;
2390         int                      rc = 0;
2391         int                      err;
2392         int                      i;
2393         ENTRY;
2394
2395         LASSERT(fid != NULL);
2396
2397         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2398                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL ||
2399                     lmv->tgts[i]->ltd_active == 0)
2400                         continue;
2401
2402                 err = md_cancel_unused(lmv->tgts[i]->ltd_exp, fid,
2403                                        policy, mode, flags, opaque);
2404                 if (!rc)
2405                         rc = err;
2406         }
2407         RETURN(rc);
2408 }
2409
2410 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2411                       __u64 *bits)
2412 {
2413         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2414         int                      rc;
2415         ENTRY;
2416
2417         rc =  md_set_lock_data(lmv->tgts[0]->ltd_exp, lockh, data, bits);
2418         RETURN(rc);
2419 }
2420
2421 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2422                            const struct lu_fid *fid, ldlm_type_t type,
2423                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2424                            struct lustre_handle *lockh)
2425 {
2426         struct obd_device       *obd = exp->exp_obd;
2427         struct lmv_obd          *lmv = &obd->u.lmv;
2428         ldlm_mode_t              rc;
2429         int                      i;
2430         ENTRY;
2431
2432         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2433
2434         /*
2435          * With CMD every object can have two locks in different namespaces:
2436          * lookup lock in space of mds storing direntry and update/open lock in
2437          * space of mds storing inode. Thus we check all targets, not only that
2438          * one fid was created in.
2439          */
2440         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2441                 if (lmv->tgts[i] == NULL ||
2442                     lmv->tgts[i]->ltd_exp == NULL ||
2443                     lmv->tgts[i]->ltd_active == 0)
2444                         continue;
2445
2446                 rc = md_lock_match(lmv->tgts[i]->ltd_exp, flags, fid,
2447                                    type, policy, mode, lockh);
2448                 if (rc)
2449                         RETURN(rc);
2450         }
2451
2452         RETURN(0);
2453 }
2454
2455 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2456                       struct obd_export *dt_exp, struct obd_export *md_exp,
2457                       struct lustre_md *md)
2458 {
2459         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2460
2461         return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2462 }
2463
2464 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2465 {
2466         struct obd_device       *obd = exp->exp_obd;
2467         struct lmv_obd          *lmv = &obd->u.lmv;
2468         ENTRY;
2469
2470         if (md->mea)
2471                 obd_free_memmd(exp, (void *)&md->mea);
2472         RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
2473 }
2474
2475 int lmv_set_open_replay_data(struct obd_export *exp,
2476                              struct obd_client_handle *och,
2477                              struct ptlrpc_request *open_req)
2478 {
2479         struct obd_device       *obd = exp->exp_obd;
2480         struct lmv_obd          *lmv = &obd->u.lmv;
2481         struct lmv_tgt_desc     *tgt;
2482         ENTRY;
2483
2484         tgt = lmv_find_target(lmv, &och->och_fid);
2485         if (IS_ERR(tgt))
2486                 RETURN(PTR_ERR(tgt));
2487
2488         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2489 }
2490
2491 int lmv_clear_open_replay_data(struct obd_export *exp,
2492                                struct obd_client_handle *och)
2493 {
2494         struct obd_device       *obd = exp->exp_obd;
2495         struct lmv_obd          *lmv = &obd->u.lmv;
2496         struct lmv_tgt_desc     *tgt;
2497         ENTRY;
2498
2499         tgt = lmv_find_target(lmv, &och->och_fid);
2500         if (IS_ERR(tgt))
2501                 RETURN(PTR_ERR(tgt));
2502
2503         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2504 }
2505
2506 static int lmv_get_remote_perm(struct obd_export *exp,
2507                                const struct lu_fid *fid,
2508                                struct obd_capa *oc, __u32 suppgid,
2509                                struct ptlrpc_request **request)
2510 {
2511         struct obd_device       *obd = exp->exp_obd;
2512         struct lmv_obd          *lmv = &obd->u.lmv;
2513         struct lmv_tgt_desc     *tgt;
2514         int                      rc;
2515         ENTRY;
2516
2517         rc = lmv_check_connect(obd);
2518         if (rc)
2519                 RETURN(rc);
2520
2521         tgt = lmv_find_target(lmv, fid);
2522         if (IS_ERR(tgt))
2523                 RETURN(PTR_ERR(tgt));
2524
2525         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2526         RETURN(rc);
2527 }
2528
2529 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2530                           renew_capa_cb_t cb)
2531 {
2532         struct obd_device       *obd = exp->exp_obd;
2533         struct lmv_obd          *lmv = &obd->u.lmv;
2534         struct lmv_tgt_desc     *tgt;
2535         int                      rc;
2536         ENTRY;
2537
2538         rc = lmv_check_connect(obd);
2539         if (rc)
2540                 RETURN(rc);
2541
2542         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2543         if (IS_ERR(tgt))
2544                 RETURN(PTR_ERR(tgt));
2545
2546         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2547         RETURN(rc);
2548 }
2549
2550 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2551                     const struct req_msg_field *field, struct obd_capa **oc)
2552 {
2553         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2554
2555         return md_unpack_capa(lmv->tgts[0]->ltd_exp, req, field, oc);
2556 }
2557
2558 int lmv_intent_getattr_async(struct obd_export *exp,
2559                              struct md_enqueue_info *minfo,
2560                              struct ldlm_enqueue_info *einfo)
2561 {
2562         struct md_op_data       *op_data = &minfo->mi_data;
2563         struct obd_device       *obd = exp->exp_obd;
2564         struct lmv_obd          *lmv = &obd->u.lmv;
2565         struct lmv_tgt_desc     *tgt = NULL;
2566         int                      rc;
2567         ENTRY;
2568
2569         rc = lmv_check_connect(obd);
2570         if (rc)
2571                 RETURN(rc);
2572
2573         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2574         if (IS_ERR(tgt))
2575                 RETURN(PTR_ERR(tgt));
2576
2577         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2578         RETURN(rc);
2579 }
2580
2581 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2582                         struct lu_fid *fid, __u64 *bits)
2583 {
2584         struct obd_device       *obd = exp->exp_obd;
2585         struct lmv_obd          *lmv = &obd->u.lmv;
2586         struct lmv_tgt_desc     *tgt;
2587         int                      rc;
2588         ENTRY;
2589
2590         rc = lmv_check_connect(obd);
2591         if (rc)
2592                 RETURN(rc);
2593
2594         tgt = lmv_find_target(lmv, fid);
2595         if (IS_ERR(tgt))
2596                 RETURN(PTR_ERR(tgt));
2597
2598         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2599         RETURN(rc);
2600 }
2601
2602 /**
2603  * For lmv, only need to send request to master MDT, and the master MDT will
2604  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2605  * we directly fetch data from the slave MDTs.
2606  */
2607 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2608                  struct obd_quotactl *oqctl)
2609 {
2610         struct obd_device   *obd = class_exp2obd(exp);
2611         struct lmv_obd      *lmv = &obd->u.lmv;
2612         struct lmv_tgt_desc *tgt = lmv->tgts[0];
2613         int                  rc = 0, i;
2614         __u64                curspace, curinodes;
2615         ENTRY;
2616
2617         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
2618                 CERROR("master lmv inactive\n");
2619                 RETURN(-EIO);
2620         }
2621
2622         if (oqctl->qc_cmd != Q_GETOQUOTA) {
2623                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2624                 RETURN(rc);
2625         }
2626
2627         curspace = curinodes = 0;
2628         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2629                 int err;
2630                 tgt = lmv->tgts[i];
2631
2632                 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
2633                         continue;
2634                 if (!tgt->ltd_active) {
2635                         CDEBUG(D_HA, "mdt %d is inactive.\n", i);
2636                         continue;
2637                 }
2638
2639                 err = obd_quotactl(tgt->ltd_exp, oqctl);
2640                 if (err) {
2641                         CERROR("getquota on mdt %d failed. %d\n", i, err);
2642                         if (!rc)
2643                                 rc = err;
2644                 } else {
2645                         curspace += oqctl->qc_dqblk.dqb_curspace;
2646                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
2647                 }
2648         }
2649         oqctl->qc_dqblk.dqb_curspace = curspace;
2650         oqctl->qc_dqblk.dqb_curinodes = curinodes;
2651
2652         RETURN(rc);
2653 }
2654
2655 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2656                    struct obd_quotactl *oqctl)
2657 {
2658         struct obd_device   *obd = class_exp2obd(exp);
2659         struct lmv_obd      *lmv = &obd->u.lmv;
2660         struct lmv_tgt_desc *tgt;
2661         int                  i, rc = 0;
2662         ENTRY;
2663
2664         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2665                 int err;
2666                 tgt = lmv->tgts[i];
2667                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2668                         CERROR("lmv idx %d inactive\n", i);
2669                         RETURN(-EIO);
2670                 }
2671
2672                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2673                 if (err && !rc)
2674                         rc = err;
2675         }
2676
2677         RETURN(rc);
2678 }
2679
2680 struct obd_ops lmv_obd_ops = {
2681         .o_owner                = THIS_MODULE,
2682         .o_setup                = lmv_setup,
2683         .o_cleanup              = lmv_cleanup,
2684         .o_precleanup           = lmv_precleanup,
2685         .o_process_config       = lmv_process_config,
2686         .o_connect              = lmv_connect,
2687         .o_disconnect           = lmv_disconnect,
2688         .o_statfs               = lmv_statfs,
2689         .o_get_info             = lmv_get_info,
2690         .o_set_info_async       = lmv_set_info_async,
2691         .o_packmd               = lmv_packmd,
2692         .o_unpackmd             = lmv_unpackmd,
2693         .o_notify               = lmv_notify,
2694         .o_get_uuid             = lmv_get_uuid,
2695         .o_iocontrol            = lmv_iocontrol,
2696         .o_quotacheck           = lmv_quotacheck,
2697         .o_quotactl             = lmv_quotactl
2698 };
2699
2700 struct md_ops lmv_md_ops = {
2701         .m_getstatus            = lmv_getstatus,
2702         .m_null_inode           = lmv_null_inode,
2703         .m_find_cbdata          = lmv_find_cbdata,
2704         .m_close                = lmv_close,
2705         .m_create               = lmv_create,
2706         .m_done_writing         = lmv_done_writing,
2707         .m_enqueue              = lmv_enqueue,
2708         .m_getattr              = lmv_getattr,
2709         .m_getxattr             = lmv_getxattr,
2710         .m_getattr_name         = lmv_getattr_name,
2711         .m_intent_lock          = lmv_intent_lock,
2712         .m_link                 = lmv_link,
2713         .m_rename               = lmv_rename,
2714         .m_setattr              = lmv_setattr,
2715         .m_setxattr             = lmv_setxattr,
2716         .m_sync                 = lmv_sync,
2717         .m_readpage             = lmv_readpage,
2718         .m_unlink               = lmv_unlink,
2719         .m_init_ea_size         = lmv_init_ea_size,
2720         .m_cancel_unused        = lmv_cancel_unused,
2721         .m_set_lock_data        = lmv_set_lock_data,
2722         .m_lock_match           = lmv_lock_match,
2723         .m_get_lustre_md        = lmv_get_lustre_md,
2724         .m_free_lustre_md       = lmv_free_lustre_md,
2725         .m_set_open_replay_data = lmv_set_open_replay_data,
2726         .m_clear_open_replay_data = lmv_clear_open_replay_data,
2727         .m_renew_capa           = lmv_renew_capa,
2728         .m_unpack_capa          = lmv_unpack_capa,
2729         .m_get_remote_perm      = lmv_get_remote_perm,
2730         .m_intent_getattr_async = lmv_intent_getattr_async,
2731         .m_revalidate_lock      = lmv_revalidate_lock
2732 };
2733
2734 int __init lmv_init(void)
2735 {
2736         struct lprocfs_static_vars lvars;
2737         int                        rc;
2738
2739         lprocfs_lmv_init_vars(&lvars);
2740
2741         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2742                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2743         return rc;
2744 }
2745
2746 #ifdef __KERNEL__
2747 static void lmv_exit(void)
2748 {
2749         class_unregister_type(LUSTRE_LMV_NAME);
2750 }
2751
2752 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2753 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2754 MODULE_LICENSE("GPL");
2755
2756 module_init(lmv_init);
2757 module_exit(lmv_exit);
2758 #endif