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