Whamcloud - gitweb
LU-1445 fid: Add DATA fid type in fid_request.
[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 /* object cache. */
63 cfs_mem_cache_t *lmv_object_cache;
64 cfs_atomic_t lmv_object_count = CFS_ATOMIC_INIT(0);
65
66 static void lmv_activate_target(struct lmv_obd *lmv,
67                                 struct lmv_tgt_desc *tgt,
68                                 int activate)
69 {
70         if (tgt->ltd_active == activate)
71                 return;
72
73         tgt->ltd_active = activate;
74         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
75 }
76
77 /**
78  * Error codes:
79  *
80  *  -EINVAL  : UUID can't be found in the LMV's target list
81  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
82  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
83  */
84 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
85                               int activate)
86 {
87         struct lmv_tgt_desc    *tgt;
88         struct obd_device      *obd;
89         int                     i;
90         int                     rc = 0;
91         ENTRY;
92
93         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
94                lmv, uuid->uuid, activate);
95
96         spin_lock(&lmv->lmv_lock);
97         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
98                 if (tgt->ltd_exp == NULL)
99                         continue;
100
101                 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n",
102                        i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
103
104                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
105                         break;
106         }
107
108         if (i == lmv->desc.ld_tgt_count)
109                 GOTO(out_lmv_lock, rc = -EINVAL);
110
111         obd = class_exp2obd(tgt->ltd_exp);
112         if (obd == NULL)
113                 GOTO(out_lmv_lock, rc = -ENOTCONN);
114
115         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
116                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
117                obd->obd_type->typ_name, i);
118         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
119
120         if (tgt->ltd_active == activate) {
121                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
122                        activate ? "" : "in");
123                 GOTO(out_lmv_lock, rc);
124         }
125
126         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
127                activate ? "" : "in");
128         lmv_activate_target(lmv, tgt, activate);
129         EXIT;
130
131  out_lmv_lock:
132         spin_unlock(&lmv->lmv_lock);
133         return rc;
134 }
135
136 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
137                             struct obd_connect_data *data)
138 {
139         struct lmv_tgt_desc    *tgt;
140         int                     i;
141         ENTRY;
142
143         LASSERT(data != NULL);
144
145         spin_lock(&lmv->lmv_lock);
146         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
147                 if (tgt->ltd_exp == NULL)
148                         continue;
149
150                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
151                         lmv->datas[tgt->ltd_idx] = *data;
152                         break;
153                 }
154         }
155         spin_unlock(&lmv->lmv_lock);
156         RETURN(0);
157 }
158
159 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
160         struct obd_device *obd = exp->exp_obd;
161         struct lmv_obd *lmv = &obd->u.lmv;
162         return obd_get_uuid(lmv->tgts[0].ltd_exp);
163 }
164
165 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
166                       enum obd_notify_event ev, void *data)
167 {
168         struct obd_connect_data *conn_data;
169         struct lmv_obd          *lmv = &obd->u.lmv;
170         struct obd_uuid         *uuid;
171         int                      rc = 0;
172         ENTRY;
173
174         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
175                 CERROR("unexpected notification of %s %s!\n",
176                        watched->obd_type->typ_name,
177                        watched->obd_name);
178                 RETURN(-EINVAL);
179         }
180
181         uuid = &watched->u.cli.cl_target_uuid;
182         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
183                 /*
184                  * Set MDC as active before notifying the observer, so the
185                  * observer can use the MDC normally.
186                  */
187                 rc = lmv_set_mdc_active(lmv, uuid,
188                                         ev == OBD_NOTIFY_ACTIVE);
189                 if (rc) {
190                         CERROR("%sactivation of %s failed: %d\n",
191                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
192                                uuid->uuid, rc);
193                         RETURN(rc);
194                 }
195         } else if (ev == OBD_NOTIFY_OCD) {
196                 conn_data = &watched->u.cli.cl_import->imp_connect_data;
197
198                 /*
199                  * Set connect data to desired target, update exp_connect_flags.
200                  */
201                 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
202                 if (rc) {
203                         CERROR("can't set connect data to target %s, rc %d\n",
204                                uuid->uuid, rc);
205                         RETURN(rc);
206                 }
207
208                 /*
209                  * XXX: Make sure that ocd_connect_flags from all targets are
210                  * the same. Otherwise one of MDTs runs wrong version or
211                  * something like this.  --umka
212                  */
213                 obd->obd_self_export->exp_connect_flags =
214                         conn_data->ocd_connect_flags;
215         }
216 #if 0
217         else if (ev == OBD_NOTIFY_DISCON) {
218                 /*
219                  * For disconnect event, flush fld cache for failout MDS case.
220                  */
221                 fld_client_flush(&lmv->lmv_fld);
222         }
223 #endif
224         /*
225          * Pass the notification up the chain.
226          */
227         if (obd->obd_observer)
228                 rc = obd_notify(obd->obd_observer, watched, ev, data);
229
230         RETURN(rc);
231 }
232
233 /**
234  * This is fake connect function. Its purpose is to initialize lmv and say
235  * caller that everything is okay. Real connection will be performed later.
236  */
237 static int lmv_connect(const struct lu_env *env,
238                        struct obd_export **exp, struct obd_device *obd,
239                        struct obd_uuid *cluuid, struct obd_connect_data *data,
240                        void *localdata)
241 {
242 #ifdef __KERNEL__
243         struct proc_dir_entry *lmv_proc_dir;
244 #endif
245         struct lmv_obd        *lmv = &obd->u.lmv;
246         struct lustre_handle  conn = { 0 };
247         int                    rc = 0;
248         ENTRY;
249
250         /*
251          * We don't want to actually do the underlying connections more than
252          * once, so keep track.
253          */
254         lmv->refcount++;
255         if (lmv->refcount > 1) {
256                 *exp = NULL;
257                 RETURN(0);
258         }
259
260         rc = class_connect(&conn, obd, cluuid);
261         if (rc) {
262                 CERROR("class_connection() returned %d\n", rc);
263                 RETURN(rc);
264         }
265
266         *exp = class_conn2export(&conn);
267         class_export_get(*exp);
268
269         lmv->exp = *exp;
270         lmv->connected = 0;
271         lmv->cluuid = *cluuid;
272
273         if (data)
274                 lmv->conn_data = *data;
275
276 #ifdef __KERNEL__
277         lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
278                                         NULL, NULL);
279         if (IS_ERR(lmv_proc_dir)) {
280                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
281                        obd->obd_type->typ_name, obd->obd_name);
282                 lmv_proc_dir = NULL;
283         }
284 #endif
285
286         /*
287          * All real clients should perform actual connection right away, because
288          * it is possible, that LMV will not have opportunity to connect targets
289          * and MDC stuff will be called directly, for instance while reading
290          * ../mdc/../kbytesfree procfs file, etc.
291          */
292         if (data->ocd_connect_flags & OBD_CONNECT_REAL)
293                 rc = lmv_check_connect(obd);
294
295 #ifdef __KERNEL__
296         if (rc) {
297                 if (lmv_proc_dir)
298                         lprocfs_remove(&lmv_proc_dir);
299         }
300 #endif
301
302         RETURN(rc);
303 }
304
305 static void lmv_set_timeouts(struct obd_device *obd)
306 {
307         struct lmv_tgt_desc   *tgts;
308         struct lmv_obd        *lmv;
309         int                    i;
310
311         lmv = &obd->u.lmv;
312         if (lmv->server_timeout == 0)
313                 return;
314
315         if (lmv->connected == 0)
316                 return;
317
318         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
319                 if (tgts->ltd_exp == NULL)
320                         continue;
321
322                 obd_set_info_async(NULL, tgts->ltd_exp, sizeof(KEY_INTERMDS),
323                                    KEY_INTERMDS, 0, NULL, NULL);
324         }
325 }
326
327 static int lmv_init_ea_size(struct obd_export *exp, int easize,
328                             int def_easize, int cookiesize)
329 {
330         struct obd_device   *obd = exp->exp_obd;
331         struct lmv_obd      *lmv = &obd->u.lmv;
332         int                  i;
333         int                  rc = 0;
334         int                  change = 0;
335         ENTRY;
336
337         if (lmv->max_easize < easize) {
338                 lmv->max_easize = easize;
339                 change = 1;
340         }
341         if (lmv->max_def_easize < def_easize) {
342                 lmv->max_def_easize = def_easize;
343                 change = 1;
344         }
345         if (lmv->max_cookiesize < cookiesize) {
346                 lmv->max_cookiesize = cookiesize;
347                 change = 1;
348         }
349         if (change == 0)
350                 RETURN(0);
351
352         if (lmv->connected == 0)
353                 RETURN(0);
354
355         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
356                 if (lmv->tgts[i].ltd_exp == NULL) {
357                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
358                         continue;
359                 }
360
361                 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
362                                      cookiesize);
363                 if (rc) {
364                         CERROR("obd_init_ea_size() failed on MDT target %d, "
365                                "error %d.\n", i, rc);
366                         break;
367                 }
368         }
369         RETURN(rc);
370 }
371
372 #define MAX_STRING_SIZE 128
373
374 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
375 {
376 #ifdef __KERNEL__
377         struct proc_dir_entry   *lmv_proc_dir;
378 #endif
379         struct lmv_obd          *lmv = &obd->u.lmv;
380         struct obd_uuid         *cluuid = &lmv->cluuid;
381         struct obd_connect_data *mdc_data = NULL;
382         struct obd_uuid          lmv_mdc_uuid = { "LMV_MDC_UUID" };
383         struct obd_device       *mdc_obd;
384         struct obd_export       *mdc_exp;
385         struct lu_fld_target     target;
386         int                      rc;
387         ENTRY;
388
389         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
390                                         &obd->obd_uuid);
391         if (!mdc_obd) {
392                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
393                 RETURN(-EINVAL);
394         }
395
396         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
397                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
398                 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
399                 cluuid->uuid);
400
401         if (!mdc_obd->obd_set_up) {
402                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
403                 RETURN(-EINVAL);
404         }
405
406         rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
407                          &lmv->conn_data, NULL);
408         if (rc) {
409                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
410                 RETURN(rc);
411         }
412
413         /*
414          * Init fid sequence client for this mdc and add new fld target.
415          */
416         rc = obd_fid_init(mdc_exp, LUSTRE_SEQ_METADATA);
417         if (rc)
418                 RETURN(rc);
419
420         target.ft_srv = NULL;
421         target.ft_exp = mdc_exp;
422         target.ft_idx = tgt->ltd_idx;
423
424         fld_client_add_target(&lmv->lmv_fld, &target);
425
426         mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
427
428         rc = obd_register_observer(mdc_obd, obd);
429         if (rc) {
430                 obd_disconnect(mdc_exp);
431                 CERROR("target %s register_observer error %d\n",
432                        tgt->ltd_uuid.uuid, rc);
433                 RETURN(rc);
434         }
435
436         if (obd->obd_observer) {
437                 /*
438                  * Tell the observer about the new target.
439                  */
440                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
441                                 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
442                 if (rc) {
443                         obd_disconnect(mdc_exp);
444                         RETURN(rc);
445                 }
446         }
447
448         tgt->ltd_active = 1;
449         tgt->ltd_exp = mdc_exp;
450         lmv->desc.ld_active_tgt_count++;
451
452         /*
453          * Copy connect data, it may be used later.
454          */
455         lmv->datas[tgt->ltd_idx] = *mdc_data;
456
457         md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
458                         lmv->max_def_easize, lmv->max_cookiesize);
459
460         CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
461                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
462                 cfs_atomic_read(&obd->obd_refcount));
463
464 #ifdef __KERNEL__
465         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
466         if (lmv_proc_dir) {
467                 struct proc_dir_entry *mdc_symlink;
468
469                 LASSERT(mdc_obd->obd_type != NULL);
470                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
471                 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
472                                                   lmv_proc_dir,
473                                                   "../../../%s/%s",
474                                                   mdc_obd->obd_type->typ_name,
475                                                   mdc_obd->obd_name);
476                 if (mdc_symlink == NULL) {
477                         CERROR("Could not register LMV target "
478                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
479                                obd->obd_type->typ_name, obd->obd_name,
480                                mdc_obd->obd_name);
481                         lprocfs_remove(&lmv_proc_dir);
482                         lmv_proc_dir = NULL;
483                 }
484         }
485 #endif
486         RETURN(0);
487 }
488
489 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
490 {
491         struct lmv_obd      *lmv = &obd->u.lmv;
492         struct lmv_tgt_desc *tgt;
493         int                  rc = 0;
494         ENTRY;
495
496         CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
497
498         lmv_init_lock(lmv);
499
500         if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
501                 lmv_init_unlock(lmv);
502                 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
503                        "That many MDCs already configured.\n",
504                        tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
505                 RETURN(-EINVAL);
506         }
507         if (lmv->desc.ld_tgt_count == 0) {
508                 struct obd_device *mdc_obd;
509
510                 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
511                                                 &obd->obd_uuid);
512                 if (!mdc_obd) {
513                         lmv_init_unlock(lmv);
514                         CERROR("Target %s not attached\n", tgt_uuid->uuid);
515                         RETURN(-EINVAL);
516                 }
517         }
518         spin_lock(&lmv->lmv_lock);
519         tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
520         tgt->ltd_uuid = *tgt_uuid;
521         spin_unlock(&lmv->lmv_lock);
522
523         if (lmv->connected) {
524                 rc = lmv_connect_mdc(obd, tgt);
525                 if (rc) {
526                         spin_lock(&lmv->lmv_lock);
527                         lmv->desc.ld_tgt_count--;
528                         memset(tgt, 0, sizeof(*tgt));
529                         spin_unlock(&lmv->lmv_lock);
530                 } else {
531                         int easize = sizeof(struct lmv_stripe_md) +
532                                      lmv->desc.ld_tgt_count *
533                                      sizeof(struct lu_fid);
534                         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
535                 }
536         }
537
538         lmv_init_unlock(lmv);
539         RETURN(rc);
540 }
541
542 int lmv_check_connect(struct obd_device *obd)
543 {
544         struct lmv_obd       *lmv = &obd->u.lmv;
545         struct lmv_tgt_desc  *tgt;
546         int                   i;
547         int                   rc;
548         int                   easize;
549         ENTRY;
550
551         if (lmv->connected)
552                 RETURN(0);
553
554         lmv_init_lock(lmv);
555         if (lmv->connected) {
556                 lmv_init_unlock(lmv);
557                 RETURN(0);
558         }
559
560         if (lmv->desc.ld_tgt_count == 0) {
561                 lmv_init_unlock(lmv);
562                 CERROR("%s: no targets configured.\n", obd->obd_name);
563                 RETURN(-EINVAL);
564         }
565
566         CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
567                lmv->cluuid.uuid, obd->obd_name);
568
569         LASSERT(lmv->tgts != NULL);
570
571         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
572                 rc = lmv_connect_mdc(obd, tgt);
573                 if (rc)
574                         GOTO(out_disc, rc);
575         }
576
577         lmv_set_timeouts(obd);
578         class_export_put(lmv->exp);
579         lmv->connected = 1;
580         easize = lmv_get_easize(lmv);
581         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
582         lmv_init_unlock(lmv);
583         RETURN(0);
584
585  out_disc:
586         while (i-- > 0) {
587                 int rc2;
588                 --tgt;
589                 tgt->ltd_active = 0;
590                 if (tgt->ltd_exp) {
591                         --lmv->desc.ld_active_tgt_count;
592                         rc2 = obd_disconnect(tgt->ltd_exp);
593                         if (rc2) {
594                                 CERROR("LMV target %s disconnect on "
595                                        "MDC idx %d: error %d\n",
596                                        tgt->ltd_uuid.uuid, i, rc2);
597                         }
598                 }
599         }
600         class_disconnect(lmv->exp);
601         lmv_init_unlock(lmv);
602         RETURN(rc);
603 }
604
605 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
606 {
607 #ifdef __KERNEL__
608         struct proc_dir_entry  *lmv_proc_dir;
609 #endif
610         struct lmv_obd         *lmv = &obd->u.lmv;
611         struct obd_device      *mdc_obd;
612         int                     rc;
613         ENTRY;
614
615         LASSERT(tgt != NULL);
616         LASSERT(obd != NULL);
617
618         mdc_obd = class_exp2obd(tgt->ltd_exp);
619
620         if (mdc_obd) {
621                 mdc_obd->obd_force = obd->obd_force;
622                 mdc_obd->obd_fail = obd->obd_fail;
623                 mdc_obd->obd_no_recov = obd->obd_no_recov;
624         }
625
626 #ifdef __KERNEL__
627         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
628         if (lmv_proc_dir) {
629                 struct proc_dir_entry *mdc_symlink;
630
631                 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
632                 if (mdc_symlink) {
633                         lprocfs_remove(&mdc_symlink);
634                 } else {
635                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
636                                obd->obd_type->typ_name, obd->obd_name,
637                                mdc_obd->obd_name);
638                 }
639         }
640 #endif
641         rc = obd_fid_fini(tgt->ltd_exp);
642         if (rc)
643                 CERROR("Can't finanize fids factory\n");
644
645         CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
646                tgt->ltd_exp->exp_obd->obd_name,
647                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
648
649         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
650         rc = obd_disconnect(tgt->ltd_exp);
651         if (rc) {
652                 if (tgt->ltd_active) {
653                         CERROR("Target %s disconnect error %d\n",
654                                tgt->ltd_uuid.uuid, rc);
655                 }
656         }
657
658         lmv_activate_target(lmv, tgt, 0);
659         tgt->ltd_exp = NULL;
660         RETURN(0);
661 }
662
663 static int lmv_disconnect(struct obd_export *exp)
664 {
665         struct obd_device     *obd = class_exp2obd(exp);
666 #ifdef __KERNEL__
667         struct proc_dir_entry *lmv_proc_dir;
668 #endif
669         struct lmv_obd        *lmv = &obd->u.lmv;
670         int                    rc;
671         int                    i;
672         ENTRY;
673
674         if (!lmv->tgts)
675                 goto out_local;
676
677         /*
678          * Only disconnect the underlying layers on the final disconnect.
679          */
680         lmv->refcount--;
681         if (lmv->refcount != 0)
682                 goto out_local;
683
684         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
685                 if (lmv->tgts[i].ltd_exp == NULL)
686                         continue;
687                 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
688         }
689
690 #ifdef __KERNEL__
691         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
692         if (lmv_proc_dir) {
693                 lprocfs_remove(&lmv_proc_dir);
694         } else {
695                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
696                        obd->obd_type->typ_name, obd->obd_name);
697         }
698 #endif
699
700 out_local:
701         /*
702          * This is the case when no real connection is established by
703          * lmv_check_connect().
704          */
705         if (!lmv->connected)
706                 class_export_put(exp);
707         rc = class_disconnect(exp);
708         if (lmv->refcount == 0)
709                 lmv->connected = 0;
710         RETURN(rc);
711 }
712
713 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
714                          int len, void *karg, void *uarg)
715 {
716         struct obd_device    *obddev = class_exp2obd(exp);
717         struct lmv_obd       *lmv = &obddev->u.lmv;
718         int                   i = 0;
719         int                   rc = 0;
720         int                   set = 0;
721         int                   count = lmv->desc.ld_tgt_count;
722         ENTRY;
723
724         if (count == 0)
725                 RETURN(-ENOTTY);
726
727         switch (cmd) {
728         case IOC_OBD_STATFS: {
729                 struct obd_ioctl_data *data = karg;
730                 struct obd_device *mdc_obd;
731                 struct obd_statfs stat_buf = {0};
732                 __u32 index;
733
734                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
735                 if ((index >= count))
736                         RETURN(-ENODEV);
737
738                 if (!lmv->tgts[index].ltd_active)
739                         RETURN(-ENODATA);
740
741                 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
742                 if (!mdc_obd)
743                         RETURN(-EINVAL);
744
745                 /* copy UUID */
746                 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
747                                      min((int) data->ioc_plen2,
748                                          (int) sizeof(struct obd_uuid))))
749                         RETURN(-EFAULT);
750
751                 rc = obd_statfs(NULL, lmv->tgts[index].ltd_exp, &stat_buf,
752                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
753                                 0);
754                 if (rc)
755                         RETURN(rc);
756                 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
757                                      min((int) data->ioc_plen1,
758                                          (int) sizeof(stat_buf))))
759                         RETURN(-EFAULT);
760                 break;
761         }
762         case OBD_IOC_QUOTACTL: {
763                 struct if_quotactl *qctl = karg;
764                 struct lmv_tgt_desc *tgt = NULL;
765                 struct obd_quotactl *oqctl;
766
767                 if (qctl->qc_valid == QC_MDTIDX) {
768                         if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
769                                 RETURN(-EINVAL);
770
771                         tgt = &lmv->tgts[qctl->qc_idx];
772                         if (!tgt->ltd_exp)
773                                 RETURN(-EINVAL);
774                 } else if (qctl->qc_valid == QC_UUID) {
775                         for (i = 0; i < count; i++) {
776                                 tgt = &lmv->tgts[i];
777                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
778                                                      &qctl->obd_uuid))
779                                         continue;
780
781                                 if (tgt->ltd_exp == NULL)
782                                         RETURN(-EINVAL);
783
784                                 break;
785                         }
786                 } else {
787                         RETURN(-EINVAL);
788                 }
789
790                 if (i >= count)
791                         RETURN(-EAGAIN);
792
793                 LASSERT(tgt && tgt->ltd_exp);
794                 OBD_ALLOC_PTR(oqctl);
795                 if (!oqctl)
796                         RETURN(-ENOMEM);
797
798                 QCTL_COPY(oqctl, qctl);
799                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
800                 if (rc == 0) {
801                         QCTL_COPY(qctl, oqctl);
802                         qctl->qc_valid = QC_MDTIDX;
803                         qctl->obd_uuid = tgt->ltd_uuid;
804                 }
805                 OBD_FREE_PTR(oqctl);
806                 break;
807         }
808         case OBD_IOC_CHANGELOG_SEND:
809         case OBD_IOC_CHANGELOG_CLEAR: {
810                 struct ioc_changelog *icc = karg;
811
812                 if (icc->icc_mdtindex >= count)
813                         RETURN(-ENODEV);
814
815                 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex].ltd_exp,
816                                    sizeof(*icc), icc, NULL);
817                 break;
818         }
819         case LL_IOC_GET_CONNECT_FLAGS: {
820                 rc = obd_iocontrol(cmd, lmv->tgts[0].ltd_exp, len, karg, uarg);
821                 break;
822         }
823
824         default : {
825                 for (i = 0; i < count; i++) {
826                         int err;
827                         struct obd_device *mdc_obd;
828
829                         if (lmv->tgts[i].ltd_exp == NULL)
830                                 continue;
831                         /* ll_umount_begin() sets force flag but for lmv, not
832                          * mdc. Let's pass it through */
833                         mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
834                         mdc_obd->obd_force = obddev->obd_force;
835                         err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
836                                             karg, uarg);
837                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
838                                 RETURN(err);
839                         } else if (err) {
840                                 if (lmv->tgts[i].ltd_active) {
841                                         CERROR("error: iocontrol MDC %s on MDT"
842                                                "idx %d cmd %x: err = %d\n",
843                                                 lmv->tgts[i].ltd_uuid.uuid,
844                                                 i, cmd, err);
845                                         if (!rc)
846                                                 rc = err;
847                                 }
848                         } else
849                                 set = 1;
850                 }
851                 if (!set && !rc)
852                         rc = -EIO;
853         }
854         }
855         RETURN(rc);
856 }
857
858 static int lmv_all_chars_policy(int count, const char *name,
859                                 int len)
860 {
861         unsigned int c = 0;
862
863         while (len > 0)
864                 c += name[--len];
865         c = c % count;
866         return c;
867 }
868
869 static int lmv_nid_policy(struct lmv_obd *lmv)
870 {
871         struct obd_import *imp;
872         __u32              id;
873
874         /*
875          * XXX: To get nid we assume that underlying obd device is mdc.
876          */
877         imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
878         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
879         return id % lmv->desc.ld_tgt_count;
880 }
881
882 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
883                           placement_policy_t placement)
884 {
885         switch (placement) {
886         case PLACEMENT_CHAR_POLICY:
887                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
888                                             op_data->op_name,
889                                             op_data->op_namelen);
890         case PLACEMENT_NID_POLICY:
891                 return lmv_nid_policy(lmv);
892
893         default:
894                 break;
895         }
896
897         CERROR("Unsupported placement policy %x\n", placement);
898         return -EINVAL;
899 }
900
901 /**
902  * This is _inode_ placement policy function (not name).
903  */
904 static int lmv_placement_policy(struct obd_device *obd,
905                                 struct md_op_data *op_data,
906                                 mdsno_t *mds)
907 {
908         struct lmv_obd          *lmv = &obd->u.lmv;
909         struct lmv_object       *obj;
910         int                      rc;
911         ENTRY;
912
913         LASSERT(mds != NULL);
914
915         if (lmv->desc.ld_tgt_count == 1) {
916                 *mds = 0;
917                 RETURN(0);
918         }
919
920         /*
921          * Allocate new fid on target according to operation type and parent
922          * home mds.
923          */
924         obj = lmv_object_find(obd, &op_data->op_fid1);
925         if (obj != NULL || op_data->op_name == NULL ||
926             op_data->op_opc != LUSTRE_OPC_MKDIR) {
927                 /*
928                  * Allocate fid for non-dir or for null name or for case parent
929                  * dir is split.
930                  */
931                 if (obj) {
932                         lmv_object_put(obj);
933
934                         /*
935                          * If we have this flag turned on, and we see that
936                          * parent dir is split, this means, that caller did not
937                          * notice split yet. This is race and we would like to
938                          * let caller know that.
939                          */
940                         if (op_data->op_bias & MDS_CHECK_SPLIT)
941                                 RETURN(-ERESTART);
942                 }
943
944                 /*
945                  * Allocate new fid on same mds where parent fid is located and
946                  * where operation will be sent. In case of split dir, ->op_fid1
947                  * and ->op_mds here will contain fid and mds of slave directory
948                  * object (assigned by caller).
949                  */
950                 *mds = op_data->op_mds;
951                 rc = 0;
952         } else {
953                 /*
954                  * Parent directory is not split and we want to create a
955                  * directory in it. Let's calculate where to place it according
956                  * to operation data @op_data.
957                  */
958                 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
959                 rc = 0;
960         }
961
962         if (rc) {
963                 CERROR("Can't choose MDS, err = %d\n", rc);
964         } else {
965                 LASSERT(*mds < lmv->desc.ld_tgt_count);
966         }
967
968         RETURN(rc);
969 }
970
971 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
972                     mdsno_t mds)
973 {
974         struct lmv_tgt_desc *tgt;
975         int                  rc;
976         ENTRY;
977
978         tgt = lmv_get_target(lmv, mds);
979
980         /*
981          * New seq alloc and FLD setup should be atomic. Otherwise we may find
982          * on server that seq in new allocated fid is not yet known.
983          */
984         mutex_lock(&tgt->ltd_fid_mutex);
985
986         if (!tgt->ltd_active)
987                 GOTO(out, rc = -ENODEV);
988
989         /*
990          * Asking underlaying tgt layer to allocate new fid.
991          */
992         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
993         if (rc > 0) {
994                 LASSERT(fid_is_sane(fid));
995                 rc = 0;
996         }
997
998         EXIT;
999 out:
1000         mutex_unlock(&tgt->ltd_fid_mutex);
1001         return rc;
1002 }
1003
1004 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1005                   struct md_op_data *op_data)
1006 {
1007         struct obd_device     *obd = class_exp2obd(exp);
1008         struct lmv_obd        *lmv = &obd->u.lmv;
1009         mdsno_t                mds = 0;
1010         int                    rc;
1011         ENTRY;
1012
1013         LASSERT(op_data != NULL);
1014         LASSERT(fid != NULL);
1015
1016         rc = lmv_placement_policy(obd, op_data, &mds);
1017         if (rc) {
1018                 CERROR("Can't get target for allocating fid, "
1019                        "rc %d\n", rc);
1020                 RETURN(rc);
1021         }
1022
1023         rc = __lmv_fid_alloc(lmv, fid, mds);
1024         if (rc) {
1025                 CERROR("Can't alloc new fid, rc %d\n", rc);
1026                 RETURN(rc);
1027         }
1028
1029         RETURN(rc);
1030 }
1031
1032 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1033 {
1034         struct lmv_obd             *lmv = &obd->u.lmv;
1035         struct lprocfs_static_vars  lvars;
1036         struct lmv_desc            *desc;
1037         int                         rc;
1038         int                         i = 0;
1039         ENTRY;
1040
1041         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1042                 CERROR("LMV setup requires a descriptor\n");
1043                 RETURN(-EINVAL);
1044         }
1045
1046         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1047         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1048                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1049                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1050                 RETURN(-EINVAL);
1051         }
1052
1053         lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1054
1055         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1056         if (lmv->tgts == NULL)
1057                 RETURN(-ENOMEM);
1058
1059         for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1060                 mutex_init(&lmv->tgts[i].ltd_fid_mutex);
1061                 lmv->tgts[i].ltd_idx = i;
1062         }
1063
1064         lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1065
1066         OBD_ALLOC(lmv->datas, lmv->datas_size);
1067         if (lmv->datas == NULL)
1068                 GOTO(out_free_tgts, rc = -ENOMEM);
1069
1070         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1071         lmv->desc.ld_tgt_count = 0;
1072         lmv->desc.ld_active_tgt_count = 0;
1073         lmv->max_cookiesize = 0;
1074         lmv->max_def_easize = 0;
1075         lmv->max_easize = 0;
1076         lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1077
1078         spin_lock_init(&lmv->lmv_lock);
1079         mutex_init(&lmv->init_mutex);
1080
1081         rc = lmv_object_setup(obd);
1082         if (rc) {
1083                 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1084                 GOTO(out_free_datas, rc);
1085         }
1086
1087         lprocfs_lmv_init_vars(&lvars);
1088         lprocfs_obd_setup(obd, lvars.obd_vars);
1089 #ifdef LPROCFS
1090         {
1091                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1092                                         0444, &lmv_proc_target_fops, obd);
1093                 if (rc)
1094                         CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1095                                obd->obd_name, rc);
1096        }
1097 #endif
1098         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1099                              LUSTRE_CLI_FLD_HASH_DHT);
1100         if (rc) {
1101                 CERROR("Can't init FLD, err %d\n", rc);
1102                 GOTO(out_free_datas, rc);
1103         }
1104
1105         RETURN(0);
1106
1107 out_free_datas:
1108         OBD_FREE(lmv->datas, lmv->datas_size);
1109         lmv->datas = NULL;
1110 out_free_tgts:
1111         OBD_FREE(lmv->tgts, lmv->tgts_size);
1112         lmv->tgts = NULL;
1113         return rc;
1114 }
1115
1116 static int lmv_cleanup(struct obd_device *obd)
1117 {
1118         struct lmv_obd   *lmv = &obd->u.lmv;
1119         ENTRY;
1120
1121         fld_client_fini(&lmv->lmv_fld);
1122         lmv_object_cleanup(obd);
1123         OBD_FREE(lmv->datas, lmv->datas_size);
1124         OBD_FREE(lmv->tgts, lmv->tgts_size);
1125
1126         RETURN(0);
1127 }
1128
1129 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1130 {
1131         struct lustre_cfg     *lcfg = buf;
1132         struct obd_uuid        tgt_uuid;
1133         int                    rc;
1134         ENTRY;
1135
1136         switch(lcfg->lcfg_command) {
1137         case LCFG_ADD_MDC:
1138                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1139                         GOTO(out, rc = -EINVAL);
1140
1141                 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1142                 rc = lmv_add_target(obd, &tgt_uuid);
1143                 GOTO(out, rc);
1144         default: {
1145                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1146                 GOTO(out, rc = -EINVAL);
1147         }
1148         }
1149 out:
1150         RETURN(rc);
1151 }
1152
1153 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1154                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1155 {
1156         struct obd_device     *obd = class_exp2obd(exp);
1157         struct lmv_obd        *lmv = &obd->u.lmv;
1158         struct obd_statfs     *temp;
1159         int                    rc = 0;
1160         int                    i;
1161         ENTRY;
1162
1163         rc = lmv_check_connect(obd);
1164         if (rc)
1165                 RETURN(rc);
1166
1167         OBD_ALLOC(temp, sizeof(*temp));
1168         if (temp == NULL)
1169                 RETURN(-ENOMEM);
1170
1171         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1172                 if (lmv->tgts[i].ltd_exp == NULL)
1173                         continue;
1174
1175                 rc = obd_statfs(env, lmv->tgts[i].ltd_exp, temp,
1176                                 max_age, flags);
1177                 if (rc) {
1178                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1179                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1180                                rc);
1181                         GOTO(out_free_temp, rc);
1182                 }
1183                 if (i == 0) {
1184                         *osfs = *temp;
1185                 } else {
1186                         osfs->os_bavail += temp->os_bavail;
1187                         osfs->os_blocks += temp->os_blocks;
1188                         osfs->os_ffree += temp->os_ffree;
1189                         osfs->os_files += temp->os_files;
1190                 }
1191         }
1192
1193         EXIT;
1194 out_free_temp:
1195         OBD_FREE(temp, sizeof(*temp));
1196         return rc;
1197 }
1198
1199 static int lmv_getstatus(struct obd_export *exp,
1200                          struct lu_fid *fid,
1201                          struct obd_capa **pc)
1202 {
1203         struct obd_device    *obd = exp->exp_obd;
1204         struct lmv_obd       *lmv = &obd->u.lmv;
1205         int                   rc;
1206         ENTRY;
1207
1208         rc = lmv_check_connect(obd);
1209         if (rc)
1210                 RETURN(rc);
1211
1212         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1213         RETURN(rc);
1214 }
1215
1216 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1217                         struct obd_capa *oc, obd_valid valid, const char *name,
1218                         const char *input, int input_size, int output_size,
1219                         int flags, struct ptlrpc_request **request)
1220 {
1221         struct obd_device      *obd = exp->exp_obd;
1222         struct lmv_obd         *lmv = &obd->u.lmv;
1223         struct lmv_tgt_desc    *tgt;
1224         int                     rc;
1225         ENTRY;
1226
1227         rc = lmv_check_connect(obd);
1228         if (rc)
1229                 RETURN(rc);
1230
1231         tgt = lmv_find_target(lmv, fid);
1232         if (IS_ERR(tgt))
1233                 RETURN(PTR_ERR(tgt));
1234
1235         rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1236                          input_size, output_size, flags, request);
1237
1238         RETURN(rc);
1239 }
1240
1241 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1242                         struct obd_capa *oc, obd_valid valid, const char *name,
1243                         const char *input, int input_size, int output_size,
1244                         int flags, __u32 suppgid,
1245                         struct ptlrpc_request **request)
1246 {
1247         struct obd_device      *obd = exp->exp_obd;
1248         struct lmv_obd         *lmv = &obd->u.lmv;
1249         struct lmv_tgt_desc    *tgt;
1250         int                     rc;
1251         ENTRY;
1252
1253         rc = lmv_check_connect(obd);
1254         if (rc)
1255                 RETURN(rc);
1256
1257         tgt = lmv_find_target(lmv, fid);
1258         if (IS_ERR(tgt))
1259                 RETURN(PTR_ERR(tgt));
1260
1261         rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1262                          input_size, output_size, flags, suppgid,
1263                          request);
1264
1265         RETURN(rc);
1266 }
1267
1268 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1269                        struct ptlrpc_request **request)
1270 {
1271         struct obd_device       *obd = exp->exp_obd;
1272         struct lmv_obd          *lmv = &obd->u.lmv;
1273         struct lmv_tgt_desc     *tgt;
1274         struct lmv_object       *obj;
1275         int                      rc;
1276         int                      i;
1277         ENTRY;
1278
1279         rc = lmv_check_connect(obd);
1280         if (rc)
1281                 RETURN(rc);
1282
1283         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1284         if (IS_ERR(tgt))
1285                 RETURN(PTR_ERR(tgt));
1286
1287         if (op_data->op_flags & MF_GET_MDT_IDX) {
1288                 op_data->op_mds = tgt->ltd_idx;
1289                 RETURN(0);
1290         }
1291
1292         rc = md_getattr(tgt->ltd_exp, op_data, request);
1293         if (rc)
1294                 RETURN(rc);
1295
1296         obj = lmv_object_find_lock(obd, &op_data->op_fid1);
1297
1298         CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(&op_data->op_fid1),
1299                obj ? "(split)" : "");
1300
1301         /*
1302          * If object is split, then we loop over all the slaves and gather size
1303          * attribute. In ideal world we would have to gather also mds field from
1304          * all slaves, as object is spread over the cluster and this is
1305          * definitely interesting information and it is not good to loss it,
1306          * but...
1307          */
1308         if (obj) {
1309                 struct mdt_body *body;
1310
1311                 if (*request == NULL) {
1312                         lmv_object_put(obj);
1313                         RETURN(rc);
1314                 }
1315
1316                 body = req_capsule_server_get(&(*request)->rq_pill,
1317                                               &RMF_MDT_BODY);
1318                 LASSERT(body != NULL);
1319
1320                 for (i = 0; i < obj->lo_objcount; i++) {
1321                         if (lmv->tgts[i].ltd_exp == NULL) {
1322                                 CWARN("%s: NULL export for %d\n",
1323                                       obd->obd_name, i);
1324                                 continue;
1325                         }
1326
1327                         /*
1328                          * Skip master object.
1329                          */
1330                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1331                                 continue;
1332
1333                         body->size += obj->lo_stripes[i].ls_size;
1334                 }
1335
1336                 lmv_object_put_unlock(obj);
1337         }
1338
1339         RETURN(rc);
1340 }
1341
1342 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1343                              ldlm_iterator_t it, void *data)
1344 {
1345         struct obd_device   *obd = exp->exp_obd;
1346         struct lmv_obd      *lmv = &obd->u.lmv;
1347         int                  i;
1348         int                  rc;
1349         ENTRY;
1350
1351         rc = lmv_check_connect(obd);
1352         if (rc)
1353                 RETURN(rc);
1354
1355         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1356
1357         /*
1358          * With CMD every object can have two locks in different namespaces:
1359          * lookup lock in space of mds storing direntry and update/open lock in
1360          * space of mds storing inode.
1361          */
1362         for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1363                 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1364
1365         RETURN(0);
1366 }
1367
1368 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1369                            ldlm_iterator_t it, void *data)
1370 {
1371         struct obd_device   *obd = exp->exp_obd;
1372         struct lmv_obd      *lmv = &obd->u.lmv;
1373         int                  i;
1374         int                  rc;
1375         ENTRY;
1376
1377         rc = lmv_check_connect(obd);
1378         if (rc)
1379                 RETURN(rc);
1380
1381         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1382
1383         /*
1384          * With CMD every object can have two locks in different namespaces:
1385          * lookup lock in space of mds storing direntry and update/open lock in
1386          * space of mds storing inode.
1387          */
1388         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1389                 rc = md_find_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1390                 if (rc)
1391                         RETURN(rc);
1392         }
1393
1394         RETURN(rc);
1395 }
1396
1397
1398 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1399                      struct md_open_data *mod, struct ptlrpc_request **request)
1400 {
1401         struct obd_device     *obd = exp->exp_obd;
1402         struct lmv_obd        *lmv = &obd->u.lmv;
1403         struct lmv_tgt_desc   *tgt;
1404         int                    rc;
1405         ENTRY;
1406
1407         rc = lmv_check_connect(obd);
1408         if (rc)
1409                 RETURN(rc);
1410
1411         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1412         if (IS_ERR(tgt))
1413                 RETURN(PTR_ERR(tgt));
1414
1415         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1416         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1417         RETURN(rc);
1418 }
1419
1420 /**
1421  * Called in the case MDS returns -ERESTART on create on open, what means that
1422  * directory is split and its LMV presentation object has to be updated.
1423  */
1424 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1425 {
1426         struct obd_device       *obd = exp->exp_obd;
1427         struct lmv_obd          *lmv = &obd->u.lmv;
1428         struct ptlrpc_request   *req = NULL;
1429         struct lmv_tgt_desc     *tgt;
1430         struct lmv_object       *obj;
1431         struct lustre_md         md;
1432         struct md_op_data       *op_data;
1433         int                      mealen;
1434         int                      rc;
1435         __u64                    valid;
1436         ENTRY;
1437
1438         md.mea = NULL;
1439         mealen = lmv_get_easize(lmv);
1440
1441         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1442
1443         tgt = lmv_find_target(lmv, fid);
1444         if (IS_ERR(tgt))
1445                 RETURN(PTR_ERR(tgt));
1446
1447         /*
1448          * Time to update mea of parent fid.
1449          */
1450
1451         OBD_ALLOC_PTR(op_data);
1452         if (op_data == NULL) 
1453                 RETURN(-ENOMEM);
1454
1455         op_data->op_fid1 = *fid;
1456         op_data->op_mode = mealen;
1457         op_data->op_valid = valid;
1458
1459         rc = md_getattr(tgt->ltd_exp, op_data, &req);
1460         OBD_FREE_PTR(op_data);
1461         if (rc) {
1462                 CERROR("md_getattr() failed, error %d\n", rc);
1463                 GOTO(cleanup, rc);
1464         }
1465
1466         rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1467         if (rc) {
1468                 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1469                 GOTO(cleanup, rc);
1470         }
1471
1472         if (md.mea == NULL)
1473                 GOTO(cleanup, rc = -ENODATA);
1474
1475         obj = lmv_object_create(exp, fid, md.mea);
1476         if (IS_ERR(obj))
1477                 rc = PTR_ERR(obj);
1478         else
1479                 lmv_object_put(obj);
1480
1481         obd_free_memmd(exp, (void *)&md.mea);
1482         EXIT;
1483 cleanup:
1484         if (req)
1485                 ptlrpc_req_finished(req);
1486         return rc;
1487 }
1488
1489 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1490                const void *data, int datalen, int mode, __u32 uid,
1491                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1492                struct ptlrpc_request **request)
1493 {
1494         struct obd_device       *obd = exp->exp_obd;
1495         struct lmv_obd          *lmv = &obd->u.lmv;
1496         struct lmv_tgt_desc     *tgt;
1497         struct lmv_object       *obj;
1498         int                      rc;
1499         int                      loop = 0;
1500         int                      sidx;
1501         ENTRY;
1502
1503         rc = lmv_check_connect(obd);
1504         if (rc)
1505                 RETURN(rc);
1506
1507         if (!lmv->desc.ld_active_tgt_count)
1508                 RETURN(-EIO);
1509 repeat:
1510         ++loop;
1511         LASSERT(loop <= 2);
1512
1513         obj = lmv_object_find(obd, &op_data->op_fid1);
1514         if (obj) {
1515                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1516                                        op_data->op_name, op_data->op_namelen);
1517                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1518                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1519                 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1520                 tgt = lmv_get_target(lmv, op_data->op_mds);
1521                 lmv_object_put(obj);
1522         } else {
1523                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1524                 op_data->op_bias |= MDS_CHECK_SPLIT;
1525                 op_data->op_mds = tgt->ltd_idx;
1526         }
1527
1528         if (IS_ERR(tgt))
1529                 RETURN(PTR_ERR(tgt));
1530
1531         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1532         if (rc == -ERESTART)
1533                 goto repeat;
1534         else if (rc)
1535                 RETURN(rc);
1536
1537         CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1538                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1539                op_data->op_mds);
1540
1541         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1542         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1543                        cap_effective, rdev, request);
1544         if (rc == 0) {
1545                 if (*request == NULL)
1546                         RETURN(rc);
1547                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1548         } else if (rc == -ERESTART) {
1549                 LASSERT(*request != NULL);
1550                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1551                           "Got -ERESTART during create!\n");
1552                 ptlrpc_req_finished(*request);
1553                 *request = NULL;
1554
1555                 /*
1556                  * Directory got split. Time to update local object and repeat
1557                  * the request with proper MDS.
1558                  */
1559                 rc = lmv_handle_split(exp, &op_data->op_fid1);
1560                 if (rc == 0) {
1561                         rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1562                                                  op_data, &op_data->op_fid2);
1563                         if (rc)
1564                                 RETURN(rc);
1565                         goto repeat;
1566                 }
1567         }
1568         RETURN(rc);
1569 }
1570
1571 static int lmv_done_writing(struct obd_export *exp,
1572                             struct md_op_data *op_data,
1573                             struct md_open_data *mod)
1574 {
1575         struct obd_device     *obd = exp->exp_obd;
1576         struct lmv_obd        *lmv = &obd->u.lmv;
1577         struct lmv_tgt_desc   *tgt;
1578         int                    rc;
1579         ENTRY;
1580
1581         rc = lmv_check_connect(obd);
1582         if (rc)
1583                 RETURN(rc);
1584
1585         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1586         if (IS_ERR(tgt))
1587                 RETURN(PTR_ERR(tgt));
1588
1589         rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1590         RETURN(rc);
1591 }
1592
1593 static int
1594 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1595                    struct lookup_intent *it, struct md_op_data *op_data,
1596                    struct lustre_handle *lockh, void *lmm, int lmmsize)
1597 {
1598         struct obd_device     *obd = exp->exp_obd;
1599         struct lmv_obd        *lmv = &obd->u.lmv;
1600         struct lmv_stripe_md  *mea = op_data->op_mea1;
1601         struct md_op_data     *op_data2;
1602         struct lmv_tgt_desc   *tgt;
1603         int                    i;
1604         int                    rc = 0;
1605         ENTRY;
1606
1607         OBD_ALLOC_PTR(op_data2);
1608         if (op_data2 == NULL)
1609                 RETURN(-ENOMEM);
1610
1611         LASSERT(mea != NULL);
1612         for (i = 0; i < mea->mea_count; i++) {
1613                 memset(op_data2, 0, sizeof(*op_data2));
1614                 op_data2->op_fid1 = mea->mea_ids[i];
1615                 op_data2->op_bias = 0;
1616
1617                 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1618                 if (IS_ERR(tgt))
1619                         GOTO(cleanup, rc = PTR_ERR(tgt));
1620
1621                 if (tgt->ltd_exp == NULL)
1622                         continue;
1623
1624                 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1625                                 lockh + i, lmm, lmmsize, NULL, 0);
1626
1627                 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1628                        PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1629
1630                 if (rc)
1631                         GOTO(cleanup, rc);
1632
1633                 if (it->d.lustre.it_data) {
1634                         struct ptlrpc_request *req;
1635                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1636                         ptlrpc_req_finished(req);
1637                 }
1638
1639                 if (it->d.lustre.it_status)
1640                         GOTO(cleanup, rc = it->d.lustre.it_status);
1641         }
1642
1643         EXIT;
1644 cleanup:
1645         OBD_FREE_PTR(op_data2);
1646
1647         if (rc != 0) {
1648                 /*
1649                  * Drop all taken locks.
1650                  */
1651                 while (--i >= 0) {
1652                         if (lockh[i].cookie)
1653                                 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1654                         lockh[i].cookie = 0;
1655                 }
1656         }
1657         return rc;
1658 }
1659
1660 static int
1661 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1662                    struct lookup_intent *it, struct md_op_data *op_data,
1663                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1664                    int extra_lock_flags)
1665 {
1666         struct ptlrpc_request      *req = it->d.lustre.it_data;
1667         struct obd_device          *obd = exp->exp_obd;
1668         struct lmv_obd             *lmv = &obd->u.lmv;
1669         struct lustre_handle        plock;
1670         struct lmv_tgt_desc        *tgt;
1671         struct md_op_data          *rdata;
1672         struct lu_fid               fid1;
1673         struct mdt_body            *body;
1674         int                         rc = 0;
1675         int                         pmode;
1676         ENTRY;
1677
1678         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1679         LASSERT(body != NULL);
1680
1681         if (!(body->valid & OBD_MD_MDS))
1682                 RETURN(0);
1683
1684         CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1685                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1686
1687         /*
1688          * We got LOOKUP lock, but we really need attrs.
1689          */
1690         pmode = it->d.lustre.it_lock_mode;
1691         LASSERT(pmode != 0);
1692         memcpy(&plock, lockh, sizeof(plock));
1693         it->d.lustre.it_lock_mode = 0;
1694         it->d.lustre.it_data = NULL;
1695         fid1 = body->fid1;
1696
1697         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1698         ptlrpc_req_finished(req);
1699
1700         tgt = lmv_find_target(lmv, &fid1);
1701         if (IS_ERR(tgt))
1702                 GOTO(out, rc = PTR_ERR(tgt));
1703
1704         OBD_ALLOC_PTR(rdata);
1705         if (rdata == NULL)
1706                 GOTO(out, rc = -ENOMEM);
1707
1708         rdata->op_fid1 = fid1;
1709         rdata->op_bias = MDS_CROSS_REF;
1710
1711         rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1712                         lmm, lmmsize, NULL, extra_lock_flags);
1713         OBD_FREE_PTR(rdata);
1714         EXIT;
1715 out:
1716         ldlm_lock_decref(&plock, pmode);
1717         return rc;
1718 }
1719
1720 static int
1721 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1722             struct lookup_intent *it, struct md_op_data *op_data,
1723             struct lustre_handle *lockh, void *lmm, int lmmsize,
1724             struct ptlrpc_request **req, __u64 extra_lock_flags)
1725 {
1726         struct obd_device        *obd = exp->exp_obd;
1727         struct lmv_obd           *lmv = &obd->u.lmv;
1728         struct lmv_tgt_desc      *tgt;
1729         struct lmv_object        *obj;
1730         int                       sidx;
1731         int                       rc;
1732         ENTRY;
1733
1734         rc = lmv_check_connect(obd);
1735         if (rc)
1736                 RETURN(rc);
1737
1738         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1739                LL_IT2STR(it), PFID(&op_data->op_fid1));
1740
1741         if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1742                 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1743                                         lockh, lmm, lmmsize);
1744                 RETURN(rc);
1745         }
1746
1747         obj = lmv_object_find(obd, &op_data->op_fid1);
1748         if (obj && op_data->op_namelen) {
1749                 sidx = raw_name2idx(obj->lo_hashtype,
1750                                        obj->lo_objcount,
1751                                        (char *)op_data->op_name,
1752                                        op_data->op_namelen);
1753                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1754                 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1755         } else {
1756                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1757         }
1758         if (obj)
1759                 lmv_object_put(obj);
1760
1761         if (IS_ERR(tgt))
1762                 RETURN(PTR_ERR(tgt));
1763
1764         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1765                LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1766
1767         rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1768                         lmm, lmmsize, req, extra_lock_flags);
1769
1770         if (rc == 0 && it && it->it_op == IT_OPEN) {
1771                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1772                                         lmm, lmmsize, extra_lock_flags);
1773         }
1774         RETURN(rc);
1775 }
1776
1777 static int
1778 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1779                  struct ptlrpc_request **request)
1780 {
1781         struct ptlrpc_request   *req = NULL;
1782         struct obd_device       *obd = exp->exp_obd;
1783         struct lmv_obd          *lmv = &obd->u.lmv;
1784         struct lu_fid            rid = op_data->op_fid1;
1785         struct lmv_tgt_desc     *tgt;
1786         struct mdt_body         *body;
1787         struct lmv_object       *obj;
1788         obd_valid                valid = op_data->op_valid;
1789         int                      rc;
1790         int                      loop = 0;
1791         int                      sidx;
1792         ENTRY;
1793
1794         rc = lmv_check_connect(obd);
1795         if (rc)
1796                 RETURN(rc);
1797
1798 repeat:
1799         ++loop;
1800         LASSERT(loop <= 2);
1801         obj = lmv_object_find(obd, &rid);
1802         if (obj) {
1803                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1804                                     op_data->op_name, op_data->op_namelen);
1805                 rid = obj->lo_stripes[sidx].ls_fid;
1806                 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1807                 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1808                 valid &= ~OBD_MD_FLCKSPLIT;
1809                 lmv_object_put(obj);
1810         } else {
1811                 tgt = lmv_find_target(lmv, &rid);
1812                 valid |= OBD_MD_FLCKSPLIT;
1813                 op_data->op_mds = tgt->ltd_idx;
1814         }
1815         if (IS_ERR(tgt))
1816                 RETURN(PTR_ERR(tgt));
1817
1818         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1819                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1820                PFID(&rid), tgt->ltd_idx);
1821
1822         op_data->op_valid = valid;
1823         op_data->op_fid1 = rid;
1824         rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1825         if (rc == 0) {
1826                 body = req_capsule_server_get(&(*request)->rq_pill,
1827                                               &RMF_MDT_BODY);
1828                 LASSERT(body != NULL);
1829
1830                 if (body->valid & OBD_MD_MDS) {
1831                         rid = body->fid1;
1832                         CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1833                                PFID(&rid));
1834
1835                         tgt = lmv_find_target(lmv, &rid);
1836                         if (IS_ERR(tgt)) {
1837                                 ptlrpc_req_finished(*request);
1838                                 RETURN(PTR_ERR(tgt));
1839                         }
1840
1841                         op_data->op_fid1 = rid;
1842                         op_data->op_valid |= OBD_MD_FLCROSSREF;
1843                         op_data->op_namelen = 0;
1844                         op_data->op_name = NULL;
1845                         rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1846                         ptlrpc_req_finished(*request);
1847                         *request = req;
1848                 }
1849         } else if (rc == -ERESTART) {
1850                 LASSERT(*request != NULL);
1851                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1852                           "Got -ERESTART during getattr!\n");
1853                 ptlrpc_req_finished(*request);
1854                 *request = NULL;
1855
1856                 /*
1857                  * Directory got split. Time to update local object and repeat
1858                  * the request with proper MDS.
1859                  */
1860                 rc = lmv_handle_split(exp, &rid);
1861                 if (rc == 0)
1862                         goto repeat;
1863         }
1864         RETURN(rc);
1865 }
1866
1867 #define md_op_data_fid(op_data, fl)                     \
1868         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1869          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1870          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1871          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1872          NULL)
1873
1874 static int lmv_early_cancel_slaves(struct obd_export *exp,
1875                                    struct md_op_data *op_data, int op_tgt,
1876                                    ldlm_mode_t mode, int bits, int flag)
1877 {
1878         struct obd_device      *obd = exp->exp_obd;
1879         struct lmv_obd         *lmv = &obd->u.lmv;
1880         ldlm_policy_data_t      policy = {{0}};
1881         struct lu_fid          *op_fid;
1882         struct lu_fid          *st_fid;
1883         struct lmv_tgt_desc    *tgt;
1884         struct lmv_object      *obj;
1885         int                     rc = 0;
1886         int                     i;
1887         ENTRY;
1888
1889         op_fid = md_op_data_fid(op_data, flag);
1890         if (!fid_is_sane(op_fid))
1891                 RETURN(0);
1892
1893         obj = lmv_object_find(obd, op_fid);
1894         if (obj == NULL)
1895                 RETURN(-EALREADY);
1896
1897         policy.l_inodebits.bits = bits;
1898         for (i = 0; i < obj->lo_objcount; i++) {
1899                 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1900                 st_fid = &obj->lo_stripes[i].ls_fid;
1901                 if (op_tgt != tgt->ltd_idx) {
1902                         CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1903                                PFID(st_fid), tgt->ltd_idx);
1904                         rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1905                                               mode, LCF_ASYNC, NULL);
1906                         if (rc)
1907                                 GOTO(out_put_obj, rc);
1908                 } else {
1909                         CDEBUG(D_INODE,
1910                                "EARLY_CANCEL skip operation target %d on "DFID"\n",
1911                                op_tgt, PFID(st_fid));
1912                         /*
1913                          * Do not cancel locks for operation target, they will
1914                          * be handled later in underlaying layer when calling
1915                          * function we run on behalf of.
1916                          */
1917                         *op_fid = *st_fid;
1918                         op_data->op_flags |= flag;
1919                 }
1920         }
1921         EXIT;
1922 out_put_obj:
1923         lmv_object_put(obj);
1924         return rc;
1925 }
1926
1927 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1928                             int op_tgt, ldlm_mode_t mode, int bits, int flag)
1929 {
1930         struct lu_fid          *fid = md_op_data_fid(op_data, flag);
1931         struct obd_device      *obd = exp->exp_obd;
1932         struct lmv_obd         *lmv = &obd->u.lmv;
1933         struct lmv_tgt_desc    *tgt;
1934         ldlm_policy_data_t      policy = {{0}};
1935         struct lmv_object      *obj;
1936         int                     rc = 0;
1937         ENTRY;
1938
1939         if (!fid_is_sane(fid))
1940                 RETURN(0);
1941
1942         obj = lmv_object_find(obd, fid);
1943         if (obj) {
1944                 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1945                                              bits, flag);
1946                 lmv_object_put(obj);
1947         } else {
1948                 tgt = lmv_find_target(lmv, fid);
1949                 if (IS_ERR(tgt))
1950                         RETURN(PTR_ERR(tgt));
1951
1952                 if (tgt->ltd_idx != op_tgt) {
1953                         CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1954                         policy.l_inodebits.bits = bits;
1955                         rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1956                                               mode, LCF_ASYNC, NULL);
1957                 } else {
1958                         CDEBUG(D_INODE,
1959                                "EARLY_CANCEL skip operation target %d on "DFID"\n",
1960                                op_tgt, PFID(fid));
1961                         op_data->op_flags |= flag;
1962                         rc = 0;
1963                 }
1964
1965         }
1966         RETURN(rc);
1967 }
1968
1969 /*
1970  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1971  * op_data->op_fid2
1972  */
1973 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1974                     struct ptlrpc_request **request)
1975 {
1976         struct obd_device       *obd = exp->exp_obd;
1977         struct lmv_obd          *lmv = &obd->u.lmv;
1978         struct lmv_tgt_desc     *tgt;
1979         struct lmv_object       *obj;
1980         int                      rc;
1981         int                      loop = 0;
1982         mdsno_t                  mds;
1983         int                      sidx;
1984         ENTRY;
1985
1986         rc = lmv_check_connect(obd);
1987         if (rc)
1988                 RETURN(rc);
1989
1990 repeat:
1991         ++loop;
1992         LASSERT(loop <= 2);
1993         LASSERT(op_data->op_namelen != 0);
1994
1995         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1996                PFID(&op_data->op_fid2), op_data->op_namelen,
1997                op_data->op_name, PFID(&op_data->op_fid1));
1998
1999         obj = lmv_object_find(obd, &op_data->op_fid2);
2000         if (obj) {
2001                 sidx = raw_name2idx(obj->lo_hashtype,
2002                                     obj->lo_objcount,
2003                                     op_data->op_name,
2004                                     op_data->op_namelen);
2005                 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2006                 mds = obj->lo_stripes[sidx].ls_mds;
2007                 lmv_object_put(obj);
2008         } else {
2009                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
2010                 if (rc)
2011                         RETURN(rc);
2012         }
2013
2014         CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
2015                mds, PFID(&op_data->op_fid1));
2016
2017         op_data->op_fsuid = cfs_curproc_fsuid();
2018         op_data->op_fsgid = cfs_curproc_fsgid();
2019         op_data->op_cap = cfs_curproc_cap_pack();
2020         tgt = lmv_get_target(lmv, mds);
2021
2022         /*
2023          * Cancel UPDATE lock on child (fid1).
2024          */
2025         op_data->op_flags |= MF_MDC_CANCEL_FID2;
2026         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2027                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2028         if (rc == 0)
2029                 rc = md_link(tgt->ltd_exp, op_data, request);
2030         if (rc == -ERESTART) {
2031                 LASSERT(*request != NULL);
2032                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2033                           "Got -ERESTART during link!\n");
2034                 ptlrpc_req_finished(*request);
2035                 *request = NULL;
2036
2037                 /*
2038                  * Directory got split. Time to update local object and repeat
2039                  * the request with proper MDS.
2040                  */
2041                 rc = lmv_handle_split(exp, &op_data->op_fid2);
2042                 if (rc == 0)
2043                         goto repeat;
2044         }
2045
2046         RETURN(rc);
2047 }
2048
2049 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2050                       const char *old, int oldlen, const char *new, int newlen,
2051                       struct ptlrpc_request **request)
2052 {
2053         struct obd_device       *obd = exp->exp_obd;
2054         struct lmv_obd          *lmv = &obd->u.lmv;
2055         struct lmv_tgt_desc     *src_tgt;
2056         int                      rc;
2057         int                      sidx;
2058         int                      loop = 0;
2059         struct lmv_object       *obj;
2060         mdsno_t                  mds1;
2061         mdsno_t                  mds2;
2062         ENTRY;
2063
2064         LASSERT(oldlen != 0);
2065
2066         CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2067                oldlen, old, PFID(&op_data->op_fid1),
2068                newlen, new, PFID(&op_data->op_fid2));
2069
2070         rc = lmv_check_connect(obd);
2071         if (rc)
2072                 RETURN(rc);
2073
2074 repeat:
2075         ++loop;
2076         LASSERT(loop <= 2);
2077         obj = lmv_object_find(obd, &op_data->op_fid1);
2078         if (obj) {
2079                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2080                                     (char *)old, oldlen);
2081                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2082                 mds1 = obj->lo_stripes[sidx].ls_mds;
2083                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2084                 lmv_object_put(obj);
2085         } else {
2086                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2087                 if (rc)
2088                         RETURN(rc);
2089         }
2090
2091         obj = lmv_object_find(obd, &op_data->op_fid2);
2092         if (obj) {
2093                 /*
2094                  * Directory is already split, so we have to forward request to
2095                  * the right MDS.
2096                  */
2097                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2098                                        (char *)new, newlen);
2099
2100                 mds2 = obj->lo_stripes[sidx].ls_mds;
2101                 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2102                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2103                 lmv_object_put(obj);
2104         } else {
2105                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2106                 if (rc)
2107                         RETURN(rc);
2108         }
2109
2110         op_data->op_fsuid = cfs_curproc_fsuid();
2111         op_data->op_fsgid = cfs_curproc_fsgid();
2112         op_data->op_cap = cfs_curproc_cap_pack();
2113
2114         src_tgt = lmv_get_target(lmv, mds1);
2115
2116         /*
2117          * LOOKUP lock on src child (fid3) should also be cancelled for
2118          * src_tgt in mdc_rename.
2119          */
2120         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2121
2122         /*
2123          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2124          * own target.
2125          */
2126         rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2127                               LCK_EX, MDS_INODELOCK_UPDATE,
2128                               MF_MDC_CANCEL_FID2);
2129
2130         /*
2131          * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2132          */
2133         if (rc == 0) {
2134                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2135                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2136                                       MF_MDC_CANCEL_FID4);
2137         }
2138
2139         /*
2140          * Cancel all the locks on tgt child (fid4).
2141          */
2142         if (rc == 0)
2143                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2144                                       LCK_EX, MDS_INODELOCK_FULL,
2145                                       MF_MDC_CANCEL_FID4);
2146
2147         if (rc == 0)
2148                 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2149                                new, newlen, request);
2150
2151         if (rc == -ERESTART) {
2152                 LASSERT(*request != NULL);
2153                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2154                           "Got -ERESTART during rename!\n");
2155                 ptlrpc_req_finished(*request);
2156                 *request = NULL;
2157
2158                 /*
2159                  * Directory got split. Time to update local object and repeat
2160                  * the request with proper MDS.
2161                  */
2162                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2163                 if (rc == 0)
2164                         goto repeat;
2165         }
2166         RETURN(rc);
2167 }
2168
2169 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2170                        void *ea, int ealen, void *ea2, int ea2len,
2171                        struct ptlrpc_request **request,
2172                        struct md_open_data **mod)
2173 {
2174         struct obd_device       *obd = exp->exp_obd;
2175         struct lmv_obd          *lmv = &obd->u.lmv;
2176         struct ptlrpc_request   *req;
2177         struct lmv_tgt_desc     *tgt;
2178         struct lmv_object       *obj;
2179         int                      rc = 0;
2180         int                      i;
2181         ENTRY;
2182
2183         rc = lmv_check_connect(obd);
2184         if (rc)
2185                 RETURN(rc);
2186
2187         obj = lmv_object_find(obd, &op_data->op_fid1);
2188
2189         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2190                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2191                obj ? ", split" : "");
2192
2193         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2194         if (obj) {
2195                 for (i = 0; i < obj->lo_objcount; i++) {
2196                         op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2197
2198                         tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2199                         if (IS_ERR(tgt)) {
2200                                 rc = PTR_ERR(tgt);
2201                                 break;
2202                         }
2203
2204                         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2205                                         ea2, ea2len, &req, mod);
2206
2207                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2208                                 /*
2209                                  * This is master object and this request should
2210                                  * be returned back to llite.
2211                                  */
2212                                 *request = req;
2213                         } else {
2214                                 ptlrpc_req_finished(req);
2215                         }
2216
2217                         if (rc)
2218                                 break;
2219                 }
2220                 lmv_object_put(obj);
2221         } else {
2222                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2223                 if (IS_ERR(tgt))
2224                         RETURN(PTR_ERR(tgt));
2225
2226                 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2227                                 ea2len, request, mod);
2228         }
2229         RETURN(rc);
2230 }
2231
2232 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2233                     struct obd_capa *oc, struct ptlrpc_request **request)
2234 {
2235         struct obd_device         *obd = exp->exp_obd;
2236         struct lmv_obd            *lmv = &obd->u.lmv;
2237         struct lmv_tgt_desc       *tgt;
2238         int                        rc;
2239         ENTRY;
2240
2241         rc = lmv_check_connect(obd);
2242         if (rc)
2243                 RETURN(rc);
2244
2245         tgt = lmv_find_target(lmv, fid);
2246         if (IS_ERR(tgt))
2247                 RETURN(PTR_ERR(tgt));
2248
2249         rc = md_sync(tgt->ltd_exp, fid, oc, request);
2250         RETURN(rc);
2251 }
2252
2253 /**
2254  * Main purpose of LMV blocking ast is to remove split directory LMV
2255  * presentation object (struct lmv_object) attached to the lock being revoked.
2256  */
2257 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2258                      void *data, int flag)
2259 {
2260         struct lustre_handle    lockh;
2261         struct lmv_object      *obj;
2262         int                     rc;
2263         ENTRY;
2264
2265         switch (flag) {
2266         case LDLM_CB_BLOCKING:
2267                 ldlm_lock2handle(lock, &lockh);
2268                 rc = ldlm_cli_cancel(&lockh);
2269                 if (rc < 0) {
2270                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2271                         RETURN(rc);
2272                 }
2273                 break;
2274         case LDLM_CB_CANCELING:
2275                 /*
2276                  * Time to drop cached attrs for split directory object
2277                  */
2278                 obj = lock->l_ast_data;
2279                 if (obj) {
2280                         CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2281                                ", master "DFID"\n",
2282                                lock->l_resource->lr_name.name[3] == 1 ?
2283                                "LOOKUP" : "UPDATE",
2284                                lock->l_resource->lr_name.name[0],
2285                                lock->l_resource->lr_name.name[1],
2286                                PFID(&obj->lo_fid));
2287                         lmv_object_put(obj);
2288                 }
2289                 break;
2290         default:
2291                 LBUG();
2292         }
2293         RETURN(0);
2294 }
2295
2296 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2297 {
2298         __u64         val;
2299
2300         val = le64_to_cpu(*hash);
2301         if (val < hash_adj)
2302                 val += MAX_HASH_SIZE;
2303         if (val != MDS_DIR_END_OFF)
2304                 *hash = cpu_to_le64(val - hash_adj);
2305 }
2306
2307 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2308 {
2309         __u64              id;
2310         struct obd_import *imp;
2311
2312         /*
2313          * XXX: to get nid we assume that underlying obd device is mdc.
2314          */
2315         imp  = class_exp2cliimp(exp);
2316         id   = imp->imp_connection->c_self + fid_flatten(fid);
2317
2318         CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2319                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2320
2321         return id ^ (id >> 32);
2322 }
2323
2324 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2325                         struct page **pages, struct ptlrpc_request **request)
2326 {
2327         struct obd_device       *obd = exp->exp_obd;
2328         struct lmv_obd          *lmv = &obd->u.lmv;
2329         struct lmv_object       *obj;
2330         struct lu_fid            rid = op_data->op_fid1;
2331         __u64                    offset = op_data->op_offset;
2332         __u64                    hash_adj = 0;
2333         __u32                    rank = 0;
2334         __u64                    seg_size = 0;
2335         __u64                    tgt_tmp = 0;
2336         int                      tgt_idx = 0;
2337         int                      tgt0_idx = 0;
2338         int                      rc;
2339         int                      nr = 0;
2340         int                      i;
2341         /* number of pages read, in CFS_PAGE_SIZE */
2342         int                      nrdpgs;
2343         /* number of pages transferred in LU_PAGE_SIZE */
2344         int                      nlupgs;
2345         struct lmv_stripe       *los;
2346         struct lmv_tgt_desc     *tgt;
2347         struct lu_dirpage       *dp;
2348         struct lu_dirent        *ent;
2349         ENTRY;
2350
2351         rc = lmv_check_connect(obd);
2352         if (rc)
2353                 RETURN(rc);
2354
2355         CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2356
2357         /*
2358          * This case handle directory lookup in clustered metadata case (i.e.
2359          * split directory is located on multiple md servers.)
2360          * each server keeps directory entries for certain range of hashes.
2361          * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2362          * first server will keep records with hashes [ 0 ... MAX_HASH / N  - 1],
2363          * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2364          * so on....
2365          *      readdir can simply start reading entries from 0 - N server in
2366          * order but that will not scale well as all client will request dir in
2367          * to server in same order.
2368          * Following algorithm does optimization:
2369          * Instead of doing readdir in 1, 2, ...., N order, client with a
2370          * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2371          * (every client has rank R)
2372          *      But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2373          * since client ask dir from MDS{R} client has pages with offsets
2374          * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2375          * on hash  values that we get.
2376          */
2377         obj = lmv_object_find_lock(obd, &rid);
2378         if (obj) {
2379                 nr       = obj->lo_objcount;
2380                 LASSERT(nr > 0);
2381                 seg_size = MAX_HASH_SIZE;
2382                 do_div(seg_size, nr);
2383                 los      = obj->lo_stripes;
2384                 tgt      = lmv_get_target(lmv, los[0].ls_mds);
2385                 rank     = lmv_node_rank(tgt->ltd_exp, &rid) % nr;
2386                 tgt_tmp  = offset;
2387                 do_div(tgt_tmp, seg_size);
2388                 tgt0_idx = do_div(tgt_tmp,  nr);
2389                 tgt_idx  = (tgt0_idx + rank) % nr;
2390
2391                 if (tgt_idx < tgt0_idx)
2392                         /*
2393                          * Wrap around.
2394                          *
2395                          * Last segment has unusual length due to division
2396                          * rounding.
2397                          */
2398                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2399                 else
2400                         hash_adj = 0;
2401
2402                 hash_adj += rank * seg_size;
2403
2404                 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2405                        LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2406                        offset, tgt0_idx, offset + hash_adj, tgt_idx);
2407
2408                 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2409                 rid = obj->lo_stripes[tgt_idx].ls_fid;
2410                 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2411
2412                 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2413                        PFID(&rid), (unsigned long)offset, tgt_idx);
2414         } else
2415                 tgt = lmv_find_target(lmv, &rid);
2416
2417         if (IS_ERR(tgt))
2418                 GOTO(cleanup, rc = PTR_ERR(tgt));
2419
2420         op_data->op_fid1 = rid;
2421         rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2422         if (rc)
2423                 GOTO(cleanup, rc);
2424
2425         nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2426                  >> CFS_PAGE_SHIFT;
2427         nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2428         LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2429         LASSERT(nrdpgs > 0 && nrdpgs <= op_data->op_npages);
2430
2431         CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs,
2432                op_data->op_npages);
2433
2434         for (i = 0; i < nrdpgs; i++) {
2435 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2436                 struct lu_dirpage *first;
2437                 __u64 hash_end = 0;
2438                 __u32 flags = 0;
2439 #endif
2440                 struct lu_dirent *tmp = NULL;
2441
2442                 dp = cfs_kmap(pages[i]);
2443                 if (obj) {
2444                         lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2445                         lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2446                         LASSERT(le64_to_cpu(dp->ldp_hash_start) <=
2447                                 op_data->op_offset);
2448
2449                         if ((tgt0_idx != nr - 1) &&
2450                             (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2451                         {
2452                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2453                                                                (tgt0_idx + 1));
2454                                 CDEBUG(D_INODE,
2455                                        ""DFID" reset end "LPX64" tgt %d\n",
2456                                        PFID(&rid),
2457                                        (__u64)le64_to_cpu(dp->ldp_hash_end),
2458                                        tgt_idx);
2459                         }
2460                 }
2461
2462                 ent = lu_dirent_start(dp);
2463 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2464                 first = dp;
2465                 hash_end = dp->ldp_hash_end;
2466 repeat:
2467 #endif
2468                 nlupgs--;
2469                 for (tmp = ent; ent != NULL;
2470                      tmp = ent, ent = lu_dirent_next(ent)) {
2471                         if (obj)
2472                                 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2473                 }
2474
2475 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2476                 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2477                 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2478                         ent = lu_dirent_start(dp);
2479
2480                         if (obj) {
2481                                 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2482                                 if ((tgt0_idx != nr - 1) &&
2483                                     (le64_to_cpu(dp->ldp_hash_end) ==
2484                                      MDS_DIR_END_OFF)) {
2485                                         hash_end = cpu_to_le32(seg_size *
2486                                                                (tgt0_idx + 1));
2487                                         CDEBUG(D_INODE,
2488                                             ""DFID" reset end "LPX64" tgt %d\n",
2489                                             PFID(&rid),
2490                                             (__u64)le64_to_cpu(hash_end),
2491                                             tgt_idx);
2492                                 }
2493                         }
2494                         hash_end = dp->ldp_hash_end;
2495                         flags = dp->ldp_flags;
2496
2497                         if (tmp) {
2498                                 /* enlarge the end entry lde_reclen from 0 to
2499                                  * first entry of next lu_dirpage, in this way
2500                                  * several lu_dirpages can be stored into one
2501                                  * client page on client. */
2502                                 tmp = ((void *)tmp) +
2503                                       le16_to_cpu(tmp->lde_reclen);
2504                                 tmp->lde_reclen =
2505                                         cpu_to_le16((char *)(dp->ldp_entries) -
2506                                                     (char *)tmp);
2507                                 goto repeat;
2508                         }
2509                 }
2510                 first->ldp_hash_end = hash_end;
2511                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2512                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2513 #else
2514                 SET_BUT_UNUSED(tmp);
2515 #endif
2516                 cfs_kunmap(pages[i]);
2517         }
2518         EXIT;
2519 cleanup:
2520         if (obj)
2521                 lmv_object_put_unlock(obj);
2522         return rc;
2523 }
2524
2525 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2526                       struct ptlrpc_request **request)
2527 {
2528         struct obd_device       *obd = exp->exp_obd;
2529         struct lmv_obd          *lmv = &obd->u.lmv;
2530         struct lmv_tgt_desc     *tgt = NULL;
2531         struct lmv_object       *obj;
2532         int                      rc;
2533         int                      sidx;
2534         int                      loop = 0;
2535         ENTRY;
2536
2537         rc = lmv_check_connect(obd);
2538         if (rc)
2539                 RETURN(rc);
2540
2541 repeat:
2542         ++loop;
2543         LASSERT(loop <= 2);
2544         LASSERT(op_data->op_namelen != 0);
2545
2546         obj = lmv_object_find(obd, &op_data->op_fid1);
2547         if (obj) {
2548                 sidx = raw_name2idx(obj->lo_hashtype,
2549                                     obj->lo_objcount,
2550                                     op_data->op_name,
2551                                     op_data->op_namelen);
2552                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2553                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2554                 tgt = lmv_get_target(lmv,
2555                                      obj->lo_stripes[sidx].ls_mds);
2556                 lmv_object_put(obj);
2557                 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2558                        op_data->op_namelen, op_data->op_name,
2559                        PFID(&op_data->op_fid1), sidx);
2560         }
2561
2562         if (tgt == NULL) {
2563                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2564                 if (IS_ERR(tgt))
2565                         RETURN(PTR_ERR(tgt));
2566                 op_data->op_bias |= MDS_CHECK_SPLIT;
2567         }
2568
2569         op_data->op_fsuid = cfs_curproc_fsuid();
2570         op_data->op_fsgid = cfs_curproc_fsgid();
2571         op_data->op_cap = cfs_curproc_cap_pack();
2572
2573         /*
2574          * If child's fid is given, cancel unused locks for it if it is from
2575          * another export than parent.
2576          *
2577          * LOOKUP lock for child (fid3) should also be cancelled on parent
2578          * tgt_tgt in mdc_unlink().
2579          */
2580         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2581
2582         /*
2583          * Cancel FULL locks on child (fid3).
2584          */
2585         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2586                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2587
2588         if (rc == 0)
2589                 rc = md_unlink(tgt->ltd_exp, op_data, request);
2590
2591         if (rc == -ERESTART) {
2592                 LASSERT(*request != NULL);
2593                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2594                           "Got -ERESTART during unlink!\n");
2595                 ptlrpc_req_finished(*request);
2596                 *request = NULL;
2597
2598                 /*
2599                  * Directory got split. Time to update local object and repeat
2600                  * the request with proper MDS.
2601                  */
2602                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2603                 if (rc == 0)
2604                         goto repeat;
2605         }
2606         RETURN(rc);
2607 }
2608
2609 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2610 {
2611         struct lmv_obd *lmv = &obd->u.lmv;
2612         int rc = 0;
2613
2614         switch (stage) {
2615         case OBD_CLEANUP_EARLY:
2616                 /* XXX: here should be calling obd_precleanup() down to
2617                  * stack. */
2618                 break;
2619         case OBD_CLEANUP_EXPORTS:
2620                 fld_client_proc_fini(&lmv->lmv_fld);
2621                 lprocfs_obd_cleanup(obd);
2622                 break;
2623         default:
2624                 break;
2625         }
2626         RETURN(rc);
2627 }
2628
2629 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2630                         __u32 keylen, void *key, __u32 *vallen, void *val,
2631                         struct lov_stripe_md *lsm)
2632 {
2633         struct obd_device       *obd;
2634         struct lmv_obd          *lmv;
2635         int                      rc = 0;
2636         ENTRY;
2637
2638         obd = class_exp2obd(exp);
2639         if (obd == NULL) {
2640                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2641                        exp->exp_handle.h_cookie);
2642                 RETURN(-EINVAL);
2643         }
2644
2645         lmv = &obd->u.lmv;
2646         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2647                 struct lmv_tgt_desc *tgts;
2648                 int i;
2649
2650                 rc = lmv_check_connect(obd);
2651                 if (rc)
2652                         RETURN(rc);
2653
2654                 LASSERT(*vallen == sizeof(__u32));
2655                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2656                      i++, tgts++) {
2657
2658                         /*
2659                          * All tgts should be connected when this gets called.
2660                          */
2661                         if (!tgts || !tgts->ltd_exp) {
2662                                 CERROR("target not setup?\n");
2663                                 continue;
2664                         }
2665
2666                         if (!obd_get_info(env, tgts->ltd_exp, keylen, key,
2667                                           vallen, val, NULL))
2668                                 RETURN(0);
2669                 }
2670                 RETURN(-EINVAL);
2671         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2672                 rc = lmv_check_connect(obd);
2673                 if (rc)
2674                         RETURN(rc);
2675
2676                 /*
2677                  * Forwarding this request to first MDS, it should know LOV
2678                  * desc.
2679                  */
2680                 rc = obd_get_info(env, lmv->tgts[0].ltd_exp, keylen, key,
2681                                   vallen, val, NULL);
2682                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2683                         exp->exp_connect_flags =
2684                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2685                 }
2686                 RETURN(rc);
2687         } else if (KEY_IS(KEY_TGT_COUNT)) {
2688                 *((int *)val) = lmv->desc.ld_tgt_count;
2689                 RETURN(0);
2690         }
2691
2692         CDEBUG(D_IOCTL, "Invalid key\n");
2693         RETURN(-EINVAL);
2694 }
2695
2696 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2697                        obd_count keylen, void *key, obd_count vallen,
2698                        void *val, struct ptlrpc_request_set *set)
2699 {
2700         struct lmv_tgt_desc    *tgt;
2701         struct obd_device      *obd;
2702         struct lmv_obd         *lmv;
2703         int rc = 0;
2704         ENTRY;
2705
2706         obd = class_exp2obd(exp);
2707         if (obd == NULL) {
2708                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2709                        exp->exp_handle.h_cookie);
2710                 RETURN(-EINVAL);
2711         }
2712         lmv = &obd->u.lmv;
2713
2714         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2715                 int i, err = 0;
2716
2717                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2718                         tgt = &lmv->tgts[i];
2719
2720                         if (!tgt->ltd_exp)
2721                                 continue;
2722
2723                         err = obd_set_info_async(env, tgt->ltd_exp,
2724                                                  keylen, key, vallen, val, set);
2725                         if (err && rc == 0)
2726                                 rc = err;
2727                 }
2728
2729                 RETURN(rc);
2730         }
2731
2732         RETURN(-EINVAL);
2733 }
2734
2735 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2736                struct lov_stripe_md *lsm)
2737 {
2738         struct obd_device         *obd = class_exp2obd(exp);
2739         struct lmv_obd            *lmv = &obd->u.lmv;
2740         struct lmv_stripe_md      *meap;
2741         struct lmv_stripe_md      *lsmp;
2742         int                        mea_size;
2743         int                        i;
2744         ENTRY;
2745
2746         mea_size = lmv_get_easize(lmv);
2747         if (!lmmp)
2748                 RETURN(mea_size);
2749
2750         if (*lmmp && !lsm) {
2751                 OBD_FREE_LARGE(*lmmp, mea_size);
2752                 *lmmp = NULL;
2753                 RETURN(0);
2754         }
2755
2756         if (*lmmp == NULL) {
2757                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2758                 if (*lmmp == NULL)
2759                         RETURN(-ENOMEM);
2760         }
2761
2762         if (!lsm)
2763                 RETURN(mea_size);
2764
2765         lsmp = (struct lmv_stripe_md *)lsm;
2766         meap = (struct lmv_stripe_md *)*lmmp;
2767
2768         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2769             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2770                 RETURN(-EINVAL);
2771
2772         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2773         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2774         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2775
2776         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2777                 meap->mea_ids[i] = meap->mea_ids[i];
2778                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2779         }
2780
2781         RETURN(mea_size);
2782 }
2783
2784 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2785                  struct lov_mds_md *lmm, int lmm_size)
2786 {
2787         struct obd_device          *obd = class_exp2obd(exp);
2788         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2789         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2790         struct lmv_obd             *lmv = &obd->u.lmv;
2791         int                         mea_size;
2792         int                         i;
2793         __u32                       magic;
2794         ENTRY;
2795
2796         mea_size = lmv_get_easize(lmv);
2797         if (lsmp == NULL)
2798                 return mea_size;
2799
2800         if (*lsmp != NULL && lmm == NULL) {
2801                 OBD_FREE_LARGE(*tmea, mea_size);
2802                 *lsmp = NULL;
2803                 RETURN(0);
2804         }
2805
2806         LASSERT(mea_size == lmm_size);
2807
2808         OBD_ALLOC_LARGE(*tmea, mea_size);
2809         if (*tmea == NULL)
2810                 RETURN(-ENOMEM);
2811
2812         if (!lmm)
2813                 RETURN(mea_size);
2814
2815         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2816             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2817             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2818         {
2819                 magic = le32_to_cpu(mea->mea_magic);
2820         } else {
2821                 /*
2822                  * Old mea is not handled here.
2823                  */
2824                 CERROR("Old not supportable EA is found\n");
2825                 LBUG();
2826         }
2827
2828         (*tmea)->mea_magic = magic;
2829         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2830         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2831
2832         for (i = 0; i < (*tmea)->mea_count; i++) {
2833                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2834                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2835         }
2836         RETURN(mea_size);
2837 }
2838
2839 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2840                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2841                              ldlm_cancel_flags_t flags, void *opaque)
2842 {
2843         struct obd_device       *obd = exp->exp_obd;
2844         struct lmv_obd          *lmv = &obd->u.lmv;
2845         int                      rc = 0;
2846         int                      err;
2847         int                      i;
2848         ENTRY;
2849
2850         LASSERT(fid != NULL);
2851
2852         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2853                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2854                         continue;
2855
2856                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2857                                        policy, mode, flags, opaque);
2858                 if (!rc)
2859                         rc = err;
2860         }
2861         RETURN(rc);
2862 }
2863
2864 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2865                       __u64 *bits)
2866 {
2867         struct obd_device       *obd = exp->exp_obd;
2868         struct lmv_obd          *lmv = &obd->u.lmv;
2869         int                      rc;
2870         ENTRY;
2871
2872         rc =  md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2873         RETURN(rc);
2874 }
2875
2876 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2877                            const struct lu_fid *fid, ldlm_type_t type,
2878                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2879                            struct lustre_handle *lockh)
2880 {
2881         struct obd_device       *obd = exp->exp_obd;
2882         struct lmv_obd          *lmv = &obd->u.lmv;
2883         ldlm_mode_t              rc;
2884         int                      i;
2885         ENTRY;
2886
2887         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2888
2889         /*
2890          * With CMD every object can have two locks in different namespaces:
2891          * lookup lock in space of mds storing direntry and update/open lock in
2892          * space of mds storing inode. Thus we check all targets, not only that
2893          * one fid was created in.
2894          */
2895         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2896                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2897                                    type, policy, mode, lockh);
2898                 if (rc)
2899                         RETURN(rc);
2900         }
2901
2902         RETURN(0);
2903 }
2904
2905 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2906                       struct obd_export *dt_exp, struct obd_export *md_exp,
2907                       struct lustre_md *md)
2908 {
2909         struct obd_device       *obd = exp->exp_obd;
2910         struct lmv_obd          *lmv = &obd->u.lmv;
2911         int                      rc;
2912         ENTRY;
2913         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2914         RETURN(rc);
2915 }
2916
2917 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2918 {
2919         struct obd_device       *obd = exp->exp_obd;
2920         struct lmv_obd          *lmv = &obd->u.lmv;
2921         ENTRY;
2922
2923         if (md->mea)
2924                 obd_free_memmd(exp, (void *)&md->mea);
2925         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2926 }
2927
2928 int lmv_set_open_replay_data(struct obd_export *exp,
2929                              struct obd_client_handle *och,
2930                              struct ptlrpc_request *open_req)
2931 {
2932         struct obd_device       *obd = exp->exp_obd;
2933         struct lmv_obd          *lmv = &obd->u.lmv;
2934         struct lmv_tgt_desc     *tgt;
2935         ENTRY;
2936
2937         tgt = lmv_find_target(lmv, &och->och_fid);
2938         if (IS_ERR(tgt))
2939                 RETURN(PTR_ERR(tgt));
2940
2941         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2942 }
2943
2944 int lmv_clear_open_replay_data(struct obd_export *exp,
2945                                struct obd_client_handle *och)
2946 {
2947         struct obd_device       *obd = exp->exp_obd;
2948         struct lmv_obd          *lmv = &obd->u.lmv;
2949         struct lmv_tgt_desc     *tgt;
2950         ENTRY;
2951
2952         tgt = lmv_find_target(lmv, &och->och_fid);
2953         if (IS_ERR(tgt))
2954                 RETURN(PTR_ERR(tgt));
2955
2956         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2957 }
2958
2959 static int lmv_get_remote_perm(struct obd_export *exp,
2960                                const struct lu_fid *fid,
2961                                struct obd_capa *oc, __u32 suppgid,
2962                                struct ptlrpc_request **request)
2963 {
2964         struct obd_device       *obd = exp->exp_obd;
2965         struct lmv_obd          *lmv = &obd->u.lmv;
2966         struct lmv_tgt_desc     *tgt;
2967         int                      rc;
2968         ENTRY;
2969
2970         rc = lmv_check_connect(obd);
2971         if (rc)
2972                 RETURN(rc);
2973
2974         tgt = lmv_find_target(lmv, fid);
2975         if (IS_ERR(tgt))
2976                 RETURN(PTR_ERR(tgt));
2977
2978         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2979         RETURN(rc);
2980 }
2981
2982 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2983                           renew_capa_cb_t cb)
2984 {
2985         struct obd_device       *obd = exp->exp_obd;
2986         struct lmv_obd          *lmv = &obd->u.lmv;
2987         struct lmv_tgt_desc     *tgt;
2988         int                      rc;
2989         ENTRY;
2990
2991         rc = lmv_check_connect(obd);
2992         if (rc)
2993                 RETURN(rc);
2994
2995         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2996         if (IS_ERR(tgt))
2997                 RETURN(PTR_ERR(tgt));
2998
2999         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3000         RETURN(rc);
3001 }
3002
3003 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3004                     const struct req_msg_field *field, struct obd_capa **oc)
3005 {
3006         struct obd_device *obd = exp->exp_obd;
3007         struct lmv_obd *lmv = &obd->u.lmv;
3008         int rc;
3009
3010         ENTRY;
3011         rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3012         RETURN(rc);
3013 }
3014
3015 int lmv_intent_getattr_async(struct obd_export *exp,
3016                              struct md_enqueue_info *minfo,
3017                              struct ldlm_enqueue_info *einfo)
3018 {
3019         struct md_op_data       *op_data = &minfo->mi_data;
3020         struct obd_device       *obd = exp->exp_obd;
3021         struct lmv_obd          *lmv = &obd->u.lmv;
3022         struct lmv_object       *obj;
3023         struct lmv_tgt_desc     *tgt = NULL;
3024         int                      rc;
3025         int                      sidx;
3026         ENTRY;
3027
3028         rc = lmv_check_connect(obd);
3029         if (rc)
3030                 RETURN(rc);
3031
3032         if (op_data->op_namelen) {
3033                 obj = lmv_object_find(obd, &op_data->op_fid1);
3034                 if (obj) {
3035                         sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3036                                             (char *)op_data->op_name,
3037                                             op_data->op_namelen);
3038                         op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3039                         tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3040                         lmv_object_put(obj);
3041                 }
3042         }
3043
3044         if (tgt == NULL)
3045                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3046
3047         if (IS_ERR(tgt))
3048                 RETURN(PTR_ERR(tgt));
3049
3050         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3051         RETURN(rc);
3052 }
3053
3054 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3055                         struct lu_fid *fid, __u64 *bits)
3056 {
3057         struct obd_device       *obd = exp->exp_obd;
3058         struct lmv_obd          *lmv = &obd->u.lmv;
3059         struct lmv_tgt_desc     *tgt;
3060         int                      rc;
3061         ENTRY;
3062
3063         rc = lmv_check_connect(obd);
3064         if (rc)
3065                 RETURN(rc);
3066
3067         tgt = lmv_find_target(lmv, fid);
3068         if (IS_ERR(tgt))
3069                 RETURN(PTR_ERR(tgt));
3070
3071         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3072         RETURN(rc);
3073 }
3074
3075 /**
3076  * For lmv, only need to send request to master MDT, and the master MDT will
3077  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3078  * we directly fetch data from the slave MDTs.
3079  */
3080 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3081                  struct obd_quotactl *oqctl)
3082 {
3083         struct obd_device   *obd = class_exp2obd(exp);
3084         struct lmv_obd      *lmv = &obd->u.lmv;
3085         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3086         int                  rc = 0, i;
3087         __u64                curspace, curinodes;
3088         ENTRY;
3089
3090         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3091                 CERROR("master lmv inactive\n");
3092                 RETURN(-EIO);
3093         }
3094
3095         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3096                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3097                 RETURN(rc);
3098         }
3099
3100         curspace = curinodes = 0;
3101         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3102                 int err;
3103                 tgt = &lmv->tgts[i];
3104
3105                 if (tgt->ltd_exp == NULL)
3106                         continue;
3107                 if (!tgt->ltd_active) {
3108                         CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3109                         continue;
3110                 }
3111
3112                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3113                 if (err) {
3114                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3115                         if (!rc)
3116                                 rc = err;
3117                 } else {
3118                         curspace += oqctl->qc_dqblk.dqb_curspace;
3119                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3120                 }
3121         }
3122         oqctl->qc_dqblk.dqb_curspace = curspace;
3123         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3124
3125         RETURN(rc);
3126 }
3127
3128 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3129                    struct obd_quotactl *oqctl)
3130 {
3131         struct obd_device   *obd = class_exp2obd(exp);
3132         struct lmv_obd      *lmv = &obd->u.lmv;
3133         struct lmv_tgt_desc *tgt;
3134         int                  i, rc = 0;
3135         ENTRY;
3136
3137         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3138                 int err;
3139
3140                 if (!tgt->ltd_active) {
3141                         CERROR("lmv idx %d inactive\n", i);
3142                         RETURN(-EIO);
3143                 }
3144
3145                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3146                 if (err && !rc)
3147                         rc = err;
3148         }
3149
3150         RETURN(rc);
3151 }
3152
3153 struct obd_ops lmv_obd_ops = {
3154         .o_owner                = THIS_MODULE,
3155         .o_setup                = lmv_setup,
3156         .o_cleanup              = lmv_cleanup,
3157         .o_precleanup           = lmv_precleanup,
3158         .o_process_config       = lmv_process_config,
3159         .o_connect              = lmv_connect,
3160         .o_disconnect           = lmv_disconnect,
3161         .o_statfs               = lmv_statfs,
3162         .o_get_info             = lmv_get_info,
3163         .o_set_info_async       = lmv_set_info_async,
3164         .o_packmd               = lmv_packmd,
3165         .o_unpackmd             = lmv_unpackmd,
3166         .o_notify               = lmv_notify,
3167         .o_get_uuid             = lmv_get_uuid,
3168         .o_iocontrol            = lmv_iocontrol,
3169         .o_quotacheck           = lmv_quotacheck,
3170         .o_quotactl             = lmv_quotactl
3171 };
3172
3173 struct md_ops lmv_md_ops = {
3174         .m_getstatus            = lmv_getstatus,
3175         .m_change_cbdata        = lmv_change_cbdata,
3176         .m_find_cbdata          = lmv_find_cbdata,
3177         .m_close                = lmv_close,
3178         .m_create               = lmv_create,
3179         .m_done_writing         = lmv_done_writing,
3180         .m_enqueue              = lmv_enqueue,
3181         .m_getattr              = lmv_getattr,
3182         .m_getxattr             = lmv_getxattr,
3183         .m_getattr_name         = lmv_getattr_name,
3184         .m_intent_lock          = lmv_intent_lock,
3185         .m_link                 = lmv_link,
3186         .m_rename               = lmv_rename,
3187         .m_setattr              = lmv_setattr,
3188         .m_setxattr             = lmv_setxattr,
3189         .m_sync                 = lmv_sync,
3190         .m_readpage             = lmv_readpage,
3191         .m_unlink               = lmv_unlink,
3192         .m_init_ea_size         = lmv_init_ea_size,
3193         .m_cancel_unused        = lmv_cancel_unused,
3194         .m_set_lock_data        = lmv_set_lock_data,
3195         .m_lock_match           = lmv_lock_match,
3196         .m_get_lustre_md        = lmv_get_lustre_md,
3197         .m_free_lustre_md       = lmv_free_lustre_md,
3198         .m_set_open_replay_data = lmv_set_open_replay_data,
3199         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3200         .m_renew_capa           = lmv_renew_capa,
3201         .m_unpack_capa          = lmv_unpack_capa,
3202         .m_get_remote_perm      = lmv_get_remote_perm,
3203         .m_intent_getattr_async = lmv_intent_getattr_async,
3204         .m_revalidate_lock      = lmv_revalidate_lock
3205 };
3206
3207 int __init lmv_init(void)
3208 {
3209         struct lprocfs_static_vars lvars;
3210         int                        rc;
3211
3212         lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3213                                                 sizeof(struct lmv_object),
3214                                                 0, 0);
3215         if (!lmv_object_cache) {
3216                 CERROR("Error allocating lmv objects cache\n");
3217                 return -ENOMEM;
3218         }
3219
3220         lprocfs_lmv_init_vars(&lvars);
3221
3222         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3223                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3224         if (rc)
3225                 cfs_mem_cache_destroy(lmv_object_cache);
3226
3227         return rc;
3228 }
3229
3230 #ifdef __KERNEL__
3231 static void lmv_exit(void)
3232 {
3233         class_unregister_type(LUSTRE_LMV_NAME);
3234
3235         LASSERTF(cfs_atomic_read(&lmv_object_count) == 0,
3236                  "Can't free lmv objects cache, %d object(s) busy\n",
3237                  cfs_atomic_read(&lmv_object_count));
3238         cfs_mem_cache_destroy(lmv_object_cache);
3239 }