Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[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, Whamcloud, Inc.
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);
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_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1033 {
1034         ENTRY;
1035         LASSERT(exp != NULL && fid != NULL);
1036         if (lmv_object_delete(exp, fid)) {
1037                 CDEBUG(D_INODE, "Object "DFID" is destroyed.\n",
1038                        PFID(fid));
1039         }
1040         RETURN(0);
1041 }
1042
1043 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1044 {
1045         struct lmv_obd             *lmv = &obd->u.lmv;
1046         struct lprocfs_static_vars  lvars;
1047         struct lmv_desc            *desc;
1048         int                         rc;
1049         int                         i = 0;
1050         ENTRY;
1051
1052         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1053                 CERROR("LMV setup requires a descriptor\n");
1054                 RETURN(-EINVAL);
1055         }
1056
1057         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1058         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1059                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1060                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1061                 RETURN(-EINVAL);
1062         }
1063
1064         lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1065
1066         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1067         if (lmv->tgts == NULL)
1068                 RETURN(-ENOMEM);
1069
1070         for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1071                 mutex_init(&lmv->tgts[i].ltd_fid_mutex);
1072                 lmv->tgts[i].ltd_idx = i;
1073         }
1074
1075         lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1076
1077         OBD_ALLOC(lmv->datas, lmv->datas_size);
1078         if (lmv->datas == NULL)
1079                 GOTO(out_free_tgts, rc = -ENOMEM);
1080
1081         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1082         lmv->desc.ld_tgt_count = 0;
1083         lmv->desc.ld_active_tgt_count = 0;
1084         lmv->max_cookiesize = 0;
1085         lmv->max_def_easize = 0;
1086         lmv->max_easize = 0;
1087         lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1088
1089         spin_lock_init(&lmv->lmv_lock);
1090         mutex_init(&lmv->init_mutex);
1091
1092         rc = lmv_object_setup(obd);
1093         if (rc) {
1094                 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1095                 GOTO(out_free_datas, rc);
1096         }
1097
1098         lprocfs_lmv_init_vars(&lvars);
1099         lprocfs_obd_setup(obd, lvars.obd_vars);
1100 #ifdef LPROCFS
1101         {
1102                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1103                                         0444, &lmv_proc_target_fops, obd);
1104                 if (rc)
1105                         CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1106                                obd->obd_name, rc);
1107        }
1108 #endif
1109         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1110                              LUSTRE_CLI_FLD_HASH_DHT);
1111         if (rc) {
1112                 CERROR("Can't init FLD, err %d\n", rc);
1113                 GOTO(out_free_datas, rc);
1114         }
1115
1116         RETURN(0);
1117
1118 out_free_datas:
1119         OBD_FREE(lmv->datas, lmv->datas_size);
1120         lmv->datas = NULL;
1121 out_free_tgts:
1122         OBD_FREE(lmv->tgts, lmv->tgts_size);
1123         lmv->tgts = NULL;
1124         return rc;
1125 }
1126
1127 static int lmv_cleanup(struct obd_device *obd)
1128 {
1129         struct lmv_obd   *lmv = &obd->u.lmv;
1130         ENTRY;
1131
1132         fld_client_fini(&lmv->lmv_fld);
1133         lmv_object_cleanup(obd);
1134         OBD_FREE(lmv->datas, lmv->datas_size);
1135         OBD_FREE(lmv->tgts, lmv->tgts_size);
1136
1137         RETURN(0);
1138 }
1139
1140 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1141 {
1142         struct lustre_cfg     *lcfg = buf;
1143         struct obd_uuid        tgt_uuid;
1144         int                    rc;
1145         ENTRY;
1146
1147         switch(lcfg->lcfg_command) {
1148         case LCFG_ADD_MDC:
1149                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1150                         GOTO(out, rc = -EINVAL);
1151
1152                 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1153                 rc = lmv_add_target(obd, &tgt_uuid);
1154                 GOTO(out, rc);
1155         default: {
1156                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1157                 GOTO(out, rc = -EINVAL);
1158         }
1159         }
1160 out:
1161         RETURN(rc);
1162 }
1163
1164 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1165                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1166 {
1167         struct obd_device     *obd = class_exp2obd(exp);
1168         struct lmv_obd        *lmv = &obd->u.lmv;
1169         struct obd_statfs     *temp;
1170         int                    rc = 0;
1171         int                    i;
1172         ENTRY;
1173
1174         rc = lmv_check_connect(obd);
1175         if (rc)
1176                 RETURN(rc);
1177
1178         OBD_ALLOC(temp, sizeof(*temp));
1179         if (temp == NULL)
1180                 RETURN(-ENOMEM);
1181
1182         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1183                 if (lmv->tgts[i].ltd_exp == NULL)
1184                         continue;
1185
1186                 rc = obd_statfs(env, lmv->tgts[i].ltd_exp, temp,
1187                                 max_age, flags);
1188                 if (rc) {
1189                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1190                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1191                                rc);
1192                         GOTO(out_free_temp, rc);
1193                 }
1194                 if (i == 0) {
1195                         *osfs = *temp;
1196                 } else {
1197                         osfs->os_bavail += temp->os_bavail;
1198                         osfs->os_blocks += temp->os_blocks;
1199                         osfs->os_ffree += temp->os_ffree;
1200                         osfs->os_files += temp->os_files;
1201                 }
1202         }
1203
1204         EXIT;
1205 out_free_temp:
1206         OBD_FREE(temp, sizeof(*temp));
1207         return rc;
1208 }
1209
1210 static int lmv_getstatus(struct obd_export *exp,
1211                          struct lu_fid *fid,
1212                          struct obd_capa **pc)
1213 {
1214         struct obd_device    *obd = exp->exp_obd;
1215         struct lmv_obd       *lmv = &obd->u.lmv;
1216         int                   rc;
1217         ENTRY;
1218
1219         rc = lmv_check_connect(obd);
1220         if (rc)
1221                 RETURN(rc);
1222
1223         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1224         RETURN(rc);
1225 }
1226
1227 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1228                         struct obd_capa *oc, obd_valid valid, const char *name,
1229                         const char *input, int input_size, int output_size,
1230                         int flags, struct ptlrpc_request **request)
1231 {
1232         struct obd_device      *obd = exp->exp_obd;
1233         struct lmv_obd         *lmv = &obd->u.lmv;
1234         struct lmv_tgt_desc    *tgt;
1235         int                     rc;
1236         ENTRY;
1237
1238         rc = lmv_check_connect(obd);
1239         if (rc)
1240                 RETURN(rc);
1241
1242         tgt = lmv_find_target(lmv, fid);
1243         if (IS_ERR(tgt))
1244                 RETURN(PTR_ERR(tgt));
1245
1246         rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1247                          input_size, output_size, flags, request);
1248
1249         RETURN(rc);
1250 }
1251
1252 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1253                         struct obd_capa *oc, obd_valid valid, const char *name,
1254                         const char *input, int input_size, int output_size,
1255                         int flags, __u32 suppgid,
1256                         struct ptlrpc_request **request)
1257 {
1258         struct obd_device      *obd = exp->exp_obd;
1259         struct lmv_obd         *lmv = &obd->u.lmv;
1260         struct lmv_tgt_desc    *tgt;
1261         int                     rc;
1262         ENTRY;
1263
1264         rc = lmv_check_connect(obd);
1265         if (rc)
1266                 RETURN(rc);
1267
1268         tgt = lmv_find_target(lmv, fid);
1269         if (IS_ERR(tgt))
1270                 RETURN(PTR_ERR(tgt));
1271
1272         rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1273                          input_size, output_size, flags, suppgid,
1274                          request);
1275
1276         RETURN(rc);
1277 }
1278
1279 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1280                        struct ptlrpc_request **request)
1281 {
1282         struct obd_device       *obd = exp->exp_obd;
1283         struct lmv_obd          *lmv = &obd->u.lmv;
1284         struct lmv_tgt_desc     *tgt;
1285         struct lmv_object       *obj;
1286         int                      rc;
1287         int                      i;
1288         ENTRY;
1289
1290         rc = lmv_check_connect(obd);
1291         if (rc)
1292                 RETURN(rc);
1293
1294         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1295         if (IS_ERR(tgt))
1296                 RETURN(PTR_ERR(tgt));
1297
1298         if (op_data->op_flags & MF_GET_MDT_IDX) {
1299                 op_data->op_mds = tgt->ltd_idx;
1300                 RETURN(0);
1301         }
1302
1303         rc = md_getattr(tgt->ltd_exp, op_data, request);
1304         if (rc)
1305                 RETURN(rc);
1306
1307         obj = lmv_object_find_lock(obd, &op_data->op_fid1);
1308
1309         CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(&op_data->op_fid1),
1310                obj ? "(split)" : "");
1311
1312         /*
1313          * If object is split, then we loop over all the slaves and gather size
1314          * attribute. In ideal world we would have to gather also mds field from
1315          * all slaves, as object is spread over the cluster and this is
1316          * definitely interesting information and it is not good to loss it,
1317          * but...
1318          */
1319         if (obj) {
1320                 struct mdt_body *body;
1321
1322                 if (*request == NULL) {
1323                         lmv_object_put(obj);
1324                         RETURN(rc);
1325                 }
1326
1327                 body = req_capsule_server_get(&(*request)->rq_pill,
1328                                               &RMF_MDT_BODY);
1329                 LASSERT(body != NULL);
1330
1331                 for (i = 0; i < obj->lo_objcount; i++) {
1332                         if (lmv->tgts[i].ltd_exp == NULL) {
1333                                 CWARN("%s: NULL export for %d\n",
1334                                       obd->obd_name, i);
1335                                 continue;
1336                         }
1337
1338                         /*
1339                          * Skip master object.
1340                          */
1341                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1342                                 continue;
1343
1344                         body->size += obj->lo_stripes[i].ls_size;
1345                 }
1346
1347                 lmv_object_put_unlock(obj);
1348         }
1349
1350         RETURN(rc);
1351 }
1352
1353 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1354                              ldlm_iterator_t it, void *data)
1355 {
1356         struct obd_device   *obd = exp->exp_obd;
1357         struct lmv_obd      *lmv = &obd->u.lmv;
1358         int                  i;
1359         int                  rc;
1360         ENTRY;
1361
1362         rc = lmv_check_connect(obd);
1363         if (rc)
1364                 RETURN(rc);
1365
1366         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1367
1368         /*
1369          * With CMD every object can have two locks in different namespaces:
1370          * lookup lock in space of mds storing direntry and update/open lock in
1371          * space of mds storing inode.
1372          */
1373         for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1374                 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1375
1376         RETURN(0);
1377 }
1378
1379 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1380                            ldlm_iterator_t it, void *data)
1381 {
1382         struct obd_device   *obd = exp->exp_obd;
1383         struct lmv_obd      *lmv = &obd->u.lmv;
1384         int                  i;
1385         int                  rc;
1386         ENTRY;
1387
1388         rc = lmv_check_connect(obd);
1389         if (rc)
1390                 RETURN(rc);
1391
1392         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1393
1394         /*
1395          * With CMD every object can have two locks in different namespaces:
1396          * lookup lock in space of mds storing direntry and update/open lock in
1397          * space of mds storing inode.
1398          */
1399         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1400                 rc = md_find_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1401                 if (rc)
1402                         RETURN(rc);
1403         }
1404
1405         RETURN(rc);
1406 }
1407
1408
1409 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1410                      struct md_open_data *mod, struct ptlrpc_request **request)
1411 {
1412         struct obd_device     *obd = exp->exp_obd;
1413         struct lmv_obd        *lmv = &obd->u.lmv;
1414         struct lmv_tgt_desc   *tgt;
1415         int                    rc;
1416         ENTRY;
1417
1418         rc = lmv_check_connect(obd);
1419         if (rc)
1420                 RETURN(rc);
1421
1422         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1423         if (IS_ERR(tgt))
1424                 RETURN(PTR_ERR(tgt));
1425
1426         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1427         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1428         RETURN(rc);
1429 }
1430
1431 /**
1432  * Called in the case MDS returns -ERESTART on create on open, what means that
1433  * directory is split and its LMV presentation object has to be updated.
1434  */
1435 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1436 {
1437         struct obd_device       *obd = exp->exp_obd;
1438         struct lmv_obd          *lmv = &obd->u.lmv;
1439         struct ptlrpc_request   *req = NULL;
1440         struct lmv_tgt_desc     *tgt;
1441         struct lmv_object       *obj;
1442         struct lustre_md         md;
1443         struct md_op_data       *op_data;
1444         int                      mealen;
1445         int                      rc;
1446         __u64                    valid;
1447         ENTRY;
1448
1449         md.mea = NULL;
1450         mealen = lmv_get_easize(lmv);
1451
1452         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1453
1454         tgt = lmv_find_target(lmv, fid);
1455         if (IS_ERR(tgt))
1456                 RETURN(PTR_ERR(tgt));
1457
1458         /*
1459          * Time to update mea of parent fid.
1460          */
1461
1462         OBD_ALLOC_PTR(op_data);
1463         if (op_data == NULL) 
1464                 RETURN(-ENOMEM);
1465
1466         op_data->op_fid1 = *fid;
1467         op_data->op_mode = mealen;
1468         op_data->op_valid = valid;
1469
1470         rc = md_getattr(tgt->ltd_exp, op_data, &req);
1471         OBD_FREE_PTR(op_data);
1472         if (rc) {
1473                 CERROR("md_getattr() failed, error %d\n", rc);
1474                 GOTO(cleanup, rc);
1475         }
1476
1477         rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1478         if (rc) {
1479                 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1480                 GOTO(cleanup, rc);
1481         }
1482
1483         if (md.mea == NULL)
1484                 GOTO(cleanup, rc = -ENODATA);
1485
1486         obj = lmv_object_create(exp, fid, md.mea);
1487         if (IS_ERR(obj))
1488                 rc = PTR_ERR(obj);
1489         else
1490                 lmv_object_put(obj);
1491
1492         obd_free_memmd(exp, (void *)&md.mea);
1493         EXIT;
1494 cleanup:
1495         if (req)
1496                 ptlrpc_req_finished(req);
1497         return rc;
1498 }
1499
1500 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1501                const void *data, int datalen, int mode, __u32 uid,
1502                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1503                struct ptlrpc_request **request)
1504 {
1505         struct obd_device       *obd = exp->exp_obd;
1506         struct lmv_obd          *lmv = &obd->u.lmv;
1507         struct lmv_tgt_desc     *tgt;
1508         struct lmv_object       *obj;
1509         int                      rc;
1510         int                      loop = 0;
1511         int                      sidx;
1512         ENTRY;
1513
1514         rc = lmv_check_connect(obd);
1515         if (rc)
1516                 RETURN(rc);
1517
1518         if (!lmv->desc.ld_active_tgt_count)
1519                 RETURN(-EIO);
1520 repeat:
1521         ++loop;
1522         LASSERT(loop <= 2);
1523
1524         obj = lmv_object_find(obd, &op_data->op_fid1);
1525         if (obj) {
1526                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1527                                        op_data->op_name, op_data->op_namelen);
1528                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1529                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1530                 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1531                 tgt = lmv_get_target(lmv, op_data->op_mds);
1532                 lmv_object_put(obj);
1533         } else {
1534                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1535                 op_data->op_bias |= MDS_CHECK_SPLIT;
1536                 op_data->op_mds = tgt->ltd_idx;
1537         }
1538
1539         if (IS_ERR(tgt))
1540                 RETURN(PTR_ERR(tgt));
1541
1542         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1543         if (rc == -ERESTART)
1544                 goto repeat;
1545         else if (rc)
1546                 RETURN(rc);
1547
1548         CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1549                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1550                op_data->op_mds);
1551
1552         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1553         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1554                        cap_effective, rdev, request);
1555         if (rc == 0) {
1556                 if (*request == NULL)
1557                         RETURN(rc);
1558                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1559         } else if (rc == -ERESTART) {
1560                 LASSERT(*request != NULL);
1561                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1562                           "Got -ERESTART during create!\n");
1563                 ptlrpc_req_finished(*request);
1564                 *request = NULL;
1565
1566                 /*
1567                  * Directory got split. Time to update local object and repeat
1568                  * the request with proper MDS.
1569                  */
1570                 rc = lmv_handle_split(exp, &op_data->op_fid1);
1571                 if (rc == 0) {
1572                         rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1573                                                  op_data, &op_data->op_fid2);
1574                         if (rc)
1575                                 RETURN(rc);
1576                         goto repeat;
1577                 }
1578         }
1579         RETURN(rc);
1580 }
1581
1582 static int lmv_done_writing(struct obd_export *exp,
1583                             struct md_op_data *op_data,
1584                             struct md_open_data *mod)
1585 {
1586         struct obd_device     *obd = exp->exp_obd;
1587         struct lmv_obd        *lmv = &obd->u.lmv;
1588         struct lmv_tgt_desc   *tgt;
1589         int                    rc;
1590         ENTRY;
1591
1592         rc = lmv_check_connect(obd);
1593         if (rc)
1594                 RETURN(rc);
1595
1596         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1597         if (IS_ERR(tgt))
1598                 RETURN(PTR_ERR(tgt));
1599
1600         rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1601         RETURN(rc);
1602 }
1603
1604 static int
1605 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1606                    struct lookup_intent *it, struct md_op_data *op_data,
1607                    struct lustre_handle *lockh, void *lmm, int lmmsize)
1608 {
1609         struct obd_device     *obd = exp->exp_obd;
1610         struct lmv_obd        *lmv = &obd->u.lmv;
1611         struct lmv_stripe_md  *mea = op_data->op_mea1;
1612         struct md_op_data     *op_data2;
1613         struct lmv_tgt_desc   *tgt;
1614         int                    i;
1615         int                    rc = 0;
1616         ENTRY;
1617
1618         OBD_ALLOC_PTR(op_data2);
1619         if (op_data2 == NULL)
1620                 RETURN(-ENOMEM);
1621
1622         LASSERT(mea != NULL);
1623         for (i = 0; i < mea->mea_count; i++) {
1624                 memset(op_data2, 0, sizeof(*op_data2));
1625                 op_data2->op_fid1 = mea->mea_ids[i];
1626                 op_data2->op_bias = 0;
1627
1628                 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1629                 if (IS_ERR(tgt))
1630                         GOTO(cleanup, rc = PTR_ERR(tgt));
1631
1632                 if (tgt->ltd_exp == NULL)
1633                         continue;
1634
1635                 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1636                                 lockh + i, lmm, lmmsize, NULL, 0);
1637
1638                 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1639                        PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1640
1641                 if (rc)
1642                         GOTO(cleanup, rc);
1643
1644                 if (it->d.lustre.it_data) {
1645                         struct ptlrpc_request *req;
1646                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1647                         ptlrpc_req_finished(req);
1648                 }
1649
1650                 if (it->d.lustre.it_status)
1651                         GOTO(cleanup, rc = it->d.lustre.it_status);
1652         }
1653
1654         EXIT;
1655 cleanup:
1656         OBD_FREE_PTR(op_data2);
1657
1658         if (rc != 0) {
1659                 /*
1660                  * Drop all taken locks.
1661                  */
1662                 while (--i >= 0) {
1663                         if (lockh[i].cookie)
1664                                 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1665                         lockh[i].cookie = 0;
1666                 }
1667         }
1668         return rc;
1669 }
1670
1671 static int
1672 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1673                    struct lookup_intent *it, struct md_op_data *op_data,
1674                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1675                    int extra_lock_flags)
1676 {
1677         struct ptlrpc_request      *req = it->d.lustre.it_data;
1678         struct obd_device          *obd = exp->exp_obd;
1679         struct lmv_obd             *lmv = &obd->u.lmv;
1680         struct lustre_handle        plock;
1681         struct lmv_tgt_desc        *tgt;
1682         struct md_op_data          *rdata;
1683         struct lu_fid               fid1;
1684         struct mdt_body            *body;
1685         int                         rc = 0;
1686         int                         pmode;
1687         ENTRY;
1688
1689         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1690         LASSERT(body != NULL);
1691
1692         if (!(body->valid & OBD_MD_MDS))
1693                 RETURN(0);
1694
1695         CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1696                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1697
1698         /*
1699          * We got LOOKUP lock, but we really need attrs.
1700          */
1701         pmode = it->d.lustre.it_lock_mode;
1702         LASSERT(pmode != 0);
1703         memcpy(&plock, lockh, sizeof(plock));
1704         it->d.lustre.it_lock_mode = 0;
1705         it->d.lustre.it_data = NULL;
1706         fid1 = body->fid1;
1707
1708         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1709         ptlrpc_req_finished(req);
1710
1711         tgt = lmv_find_target(lmv, &fid1);
1712         if (IS_ERR(tgt))
1713                 GOTO(out, rc = PTR_ERR(tgt));
1714
1715         OBD_ALLOC_PTR(rdata);
1716         if (rdata == NULL)
1717                 GOTO(out, rc = -ENOMEM);
1718
1719         rdata->op_fid1 = fid1;
1720         rdata->op_bias = MDS_CROSS_REF;
1721
1722         rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1723                         lmm, lmmsize, NULL, extra_lock_flags);
1724         OBD_FREE_PTR(rdata);
1725         EXIT;
1726 out:
1727         ldlm_lock_decref(&plock, pmode);
1728         return rc;
1729 }
1730
1731 static int
1732 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1733             struct lookup_intent *it, struct md_op_data *op_data,
1734             struct lustre_handle *lockh, void *lmm, int lmmsize,
1735             struct ptlrpc_request **req, __u64 extra_lock_flags)
1736 {
1737         struct obd_device        *obd = exp->exp_obd;
1738         struct lmv_obd           *lmv = &obd->u.lmv;
1739         struct lmv_tgt_desc      *tgt;
1740         struct lmv_object        *obj;
1741         int                       sidx;
1742         int                       rc;
1743         ENTRY;
1744
1745         rc = lmv_check_connect(obd);
1746         if (rc)
1747                 RETURN(rc);
1748
1749         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1750                LL_IT2STR(it), PFID(&op_data->op_fid1));
1751
1752         if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1753                 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1754                                         lockh, lmm, lmmsize);
1755                 RETURN(rc);
1756         }
1757
1758         obj = lmv_object_find(obd, &op_data->op_fid1);
1759         if (obj && op_data->op_namelen) {
1760                 sidx = raw_name2idx(obj->lo_hashtype,
1761                                        obj->lo_objcount,
1762                                        (char *)op_data->op_name,
1763                                        op_data->op_namelen);
1764                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1765                 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1766         } else {
1767                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1768         }
1769         if (obj)
1770                 lmv_object_put(obj);
1771
1772         if (IS_ERR(tgt))
1773                 RETURN(PTR_ERR(tgt));
1774
1775         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1776                LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1777
1778         rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1779                         lmm, lmmsize, req, extra_lock_flags);
1780
1781         if (rc == 0 && it && it->it_op == IT_OPEN) {
1782                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1783                                         lmm, lmmsize, extra_lock_flags);
1784         }
1785         RETURN(rc);
1786 }
1787
1788 static int
1789 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1790                  struct ptlrpc_request **request)
1791 {
1792         struct ptlrpc_request   *req = NULL;
1793         struct obd_device       *obd = exp->exp_obd;
1794         struct lmv_obd          *lmv = &obd->u.lmv;
1795         struct lu_fid            rid = op_data->op_fid1;
1796         struct lmv_tgt_desc     *tgt;
1797         struct mdt_body         *body;
1798         struct lmv_object       *obj;
1799         obd_valid                valid = op_data->op_valid;
1800         int                      rc;
1801         int                      loop = 0;
1802         int                      sidx;
1803         ENTRY;
1804
1805         rc = lmv_check_connect(obd);
1806         if (rc)
1807                 RETURN(rc);
1808
1809 repeat:
1810         ++loop;
1811         LASSERT(loop <= 2);
1812         obj = lmv_object_find(obd, &rid);
1813         if (obj) {
1814                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1815                                     op_data->op_name, op_data->op_namelen);
1816                 rid = obj->lo_stripes[sidx].ls_fid;
1817                 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1818                 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1819                 valid &= ~OBD_MD_FLCKSPLIT;
1820                 lmv_object_put(obj);
1821         } else {
1822                 tgt = lmv_find_target(lmv, &rid);
1823                 valid |= OBD_MD_FLCKSPLIT;
1824                 op_data->op_mds = tgt->ltd_idx;
1825         }
1826         if (IS_ERR(tgt))
1827                 RETURN(PTR_ERR(tgt));
1828
1829         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1830                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1831                PFID(&rid), tgt->ltd_idx);
1832
1833         op_data->op_valid = valid;
1834         op_data->op_fid1 = rid;
1835         rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1836         if (rc == 0) {
1837                 body = req_capsule_server_get(&(*request)->rq_pill,
1838                                               &RMF_MDT_BODY);
1839                 LASSERT(body != NULL);
1840
1841                 if (body->valid & OBD_MD_MDS) {
1842                         rid = body->fid1;
1843                         CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1844                                PFID(&rid));
1845
1846                         tgt = lmv_find_target(lmv, &rid);
1847                         if (IS_ERR(tgt)) {
1848                                 ptlrpc_req_finished(*request);
1849                                 RETURN(PTR_ERR(tgt));
1850                         }
1851
1852                         op_data->op_fid1 = rid;
1853                         op_data->op_valid |= OBD_MD_FLCROSSREF;
1854                         op_data->op_namelen = 0;
1855                         op_data->op_name = NULL;
1856                         rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1857                         ptlrpc_req_finished(*request);
1858                         *request = req;
1859                 }
1860         } else if (rc == -ERESTART) {
1861                 LASSERT(*request != NULL);
1862                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1863                           "Got -ERESTART during getattr!\n");
1864                 ptlrpc_req_finished(*request);
1865                 *request = NULL;
1866
1867                 /*
1868                  * Directory got split. Time to update local object and repeat
1869                  * the request with proper MDS.
1870                  */
1871                 rc = lmv_handle_split(exp, &rid);
1872                 if (rc == 0)
1873                         goto repeat;
1874         }
1875         RETURN(rc);
1876 }
1877
1878 #define md_op_data_fid(op_data, fl)                     \
1879         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1880          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1881          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1882          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1883          NULL)
1884
1885 static int lmv_early_cancel_slaves(struct obd_export *exp,
1886                                    struct md_op_data *op_data, int op_tgt,
1887                                    ldlm_mode_t mode, int bits, int flag)
1888 {
1889         struct obd_device      *obd = exp->exp_obd;
1890         struct lmv_obd         *lmv = &obd->u.lmv;
1891         ldlm_policy_data_t      policy = {{0}};
1892         struct lu_fid          *op_fid;
1893         struct lu_fid          *st_fid;
1894         struct lmv_tgt_desc    *tgt;
1895         struct lmv_object      *obj;
1896         int                     rc = 0;
1897         int                     i;
1898         ENTRY;
1899
1900         op_fid = md_op_data_fid(op_data, flag);
1901         if (!fid_is_sane(op_fid))
1902                 RETURN(0);
1903
1904         obj = lmv_object_find(obd, op_fid);
1905         if (obj == NULL)
1906                 RETURN(-EALREADY);
1907
1908         policy.l_inodebits.bits = bits;
1909         for (i = 0; i < obj->lo_objcount; i++) {
1910                 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1911                 st_fid = &obj->lo_stripes[i].ls_fid;
1912                 if (op_tgt != tgt->ltd_idx) {
1913                         CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1914                                PFID(st_fid), tgt->ltd_idx);
1915                         rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1916                                               mode, LCF_ASYNC, NULL);
1917                         if (rc)
1918                                 GOTO(out_put_obj, rc);
1919                 } else {
1920                         CDEBUG(D_INODE,
1921                                "EARLY_CANCEL skip operation target %d on "DFID"\n",
1922                                op_tgt, PFID(st_fid));
1923                         /*
1924                          * Do not cancel locks for operation target, they will
1925                          * be handled later in underlaying layer when calling
1926                          * function we run on behalf of.
1927                          */
1928                         *op_fid = *st_fid;
1929                         op_data->op_flags |= flag;
1930                 }
1931         }
1932         EXIT;
1933 out_put_obj:
1934         lmv_object_put(obj);
1935         return rc;
1936 }
1937
1938 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1939                             int op_tgt, ldlm_mode_t mode, int bits, int flag)
1940 {
1941         struct lu_fid          *fid = md_op_data_fid(op_data, flag);
1942         struct obd_device      *obd = exp->exp_obd;
1943         struct lmv_obd         *lmv = &obd->u.lmv;
1944         struct lmv_tgt_desc    *tgt;
1945         ldlm_policy_data_t      policy = {{0}};
1946         struct lmv_object      *obj;
1947         int                     rc = 0;
1948         ENTRY;
1949
1950         if (!fid_is_sane(fid))
1951                 RETURN(0);
1952
1953         obj = lmv_object_find(obd, fid);
1954         if (obj) {
1955                 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1956                                              bits, flag);
1957                 lmv_object_put(obj);
1958         } else {
1959                 tgt = lmv_find_target(lmv, fid);
1960                 if (IS_ERR(tgt))
1961                         RETURN(PTR_ERR(tgt));
1962
1963                 if (tgt->ltd_idx != op_tgt) {
1964                         CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1965                         policy.l_inodebits.bits = bits;
1966                         rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1967                                               mode, LCF_ASYNC, NULL);
1968                 } else {
1969                         CDEBUG(D_INODE,
1970                                "EARLY_CANCEL skip operation target %d on "DFID"\n",
1971                                op_tgt, PFID(fid));
1972                         op_data->op_flags |= flag;
1973                         rc = 0;
1974                 }
1975
1976         }
1977         RETURN(rc);
1978 }
1979
1980 /*
1981  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1982  * op_data->op_fid2
1983  */
1984 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1985                     struct ptlrpc_request **request)
1986 {
1987         struct obd_device       *obd = exp->exp_obd;
1988         struct lmv_obd          *lmv = &obd->u.lmv;
1989         struct lmv_tgt_desc     *tgt;
1990         struct lmv_object       *obj;
1991         int                      rc;
1992         int                      loop = 0;
1993         mdsno_t                  mds;
1994         int                      sidx;
1995         ENTRY;
1996
1997         rc = lmv_check_connect(obd);
1998         if (rc)
1999                 RETURN(rc);
2000
2001 repeat:
2002         ++loop;
2003         LASSERT(loop <= 2);
2004         LASSERT(op_data->op_namelen != 0);
2005
2006         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2007                PFID(&op_data->op_fid2), op_data->op_namelen,
2008                op_data->op_name, PFID(&op_data->op_fid1));
2009
2010         obj = lmv_object_find(obd, &op_data->op_fid2);
2011         if (obj) {
2012                 sidx = raw_name2idx(obj->lo_hashtype,
2013                                     obj->lo_objcount,
2014                                     op_data->op_name,
2015                                     op_data->op_namelen);
2016                 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2017                 mds = obj->lo_stripes[sidx].ls_mds;
2018                 lmv_object_put(obj);
2019         } else {
2020                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
2021                 if (rc)
2022                         RETURN(rc);
2023         }
2024
2025         CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
2026                mds, PFID(&op_data->op_fid1));
2027
2028         op_data->op_fsuid = cfs_curproc_fsuid();
2029         op_data->op_fsgid = cfs_curproc_fsgid();
2030         op_data->op_cap = cfs_curproc_cap_pack();
2031         tgt = lmv_get_target(lmv, mds);
2032
2033         /*
2034          * Cancel UPDATE lock on child (fid1).
2035          */
2036         op_data->op_flags |= MF_MDC_CANCEL_FID2;
2037         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2038                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2039         if (rc == 0)
2040                 rc = md_link(tgt->ltd_exp, op_data, request);
2041         if (rc == -ERESTART) {
2042                 LASSERT(*request != NULL);
2043                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2044                           "Got -ERESTART during link!\n");
2045                 ptlrpc_req_finished(*request);
2046                 *request = NULL;
2047
2048                 /*
2049                  * Directory got split. Time to update local object and repeat
2050                  * the request with proper MDS.
2051                  */
2052                 rc = lmv_handle_split(exp, &op_data->op_fid2);
2053                 if (rc == 0)
2054                         goto repeat;
2055         }
2056
2057         RETURN(rc);
2058 }
2059
2060 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2061                       const char *old, int oldlen, const char *new, int newlen,
2062                       struct ptlrpc_request **request)
2063 {
2064         struct obd_device       *obd = exp->exp_obd;
2065         struct lmv_obd          *lmv = &obd->u.lmv;
2066         struct lmv_tgt_desc     *src_tgt;
2067         int                      rc;
2068         int                      sidx;
2069         int                      loop = 0;
2070         struct lmv_object       *obj;
2071         mdsno_t                  mds1;
2072         mdsno_t                  mds2;
2073         ENTRY;
2074
2075         LASSERT(oldlen != 0);
2076
2077         CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2078                oldlen, old, PFID(&op_data->op_fid1),
2079                newlen, new, PFID(&op_data->op_fid2));
2080
2081         rc = lmv_check_connect(obd);
2082         if (rc)
2083                 RETURN(rc);
2084
2085 repeat:
2086         ++loop;
2087         LASSERT(loop <= 2);
2088         obj = lmv_object_find(obd, &op_data->op_fid1);
2089         if (obj) {
2090                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2091                                     (char *)old, oldlen);
2092                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2093                 mds1 = obj->lo_stripes[sidx].ls_mds;
2094                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2095                 lmv_object_put(obj);
2096         } else {
2097                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2098                 if (rc)
2099                         RETURN(rc);
2100         }
2101
2102         obj = lmv_object_find(obd, &op_data->op_fid2);
2103         if (obj) {
2104                 /*
2105                  * Directory is already split, so we have to forward request to
2106                  * the right MDS.
2107                  */
2108                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2109                                        (char *)new, newlen);
2110
2111                 mds2 = obj->lo_stripes[sidx].ls_mds;
2112                 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2113                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2114                 lmv_object_put(obj);
2115         } else {
2116                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2117                 if (rc)
2118                         RETURN(rc);
2119         }
2120
2121         op_data->op_fsuid = cfs_curproc_fsuid();
2122         op_data->op_fsgid = cfs_curproc_fsgid();
2123         op_data->op_cap = cfs_curproc_cap_pack();
2124
2125         src_tgt = lmv_get_target(lmv, mds1);
2126
2127         /*
2128          * LOOKUP lock on src child (fid3) should also be cancelled for
2129          * src_tgt in mdc_rename.
2130          */
2131         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2132
2133         /*
2134          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2135          * own target.
2136          */
2137         rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2138                               LCK_EX, MDS_INODELOCK_UPDATE,
2139                               MF_MDC_CANCEL_FID2);
2140
2141         /*
2142          * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2143          */
2144         if (rc == 0) {
2145                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2146                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2147                                       MF_MDC_CANCEL_FID4);
2148         }
2149
2150         /*
2151          * Cancel all the locks on tgt child (fid4).
2152          */
2153         if (rc == 0)
2154                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2155                                       LCK_EX, MDS_INODELOCK_FULL,
2156                                       MF_MDC_CANCEL_FID4);
2157
2158         if (rc == 0)
2159                 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2160                                new, newlen, request);
2161
2162         if (rc == -ERESTART) {
2163                 LASSERT(*request != NULL);
2164                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2165                           "Got -ERESTART during rename!\n");
2166                 ptlrpc_req_finished(*request);
2167                 *request = NULL;
2168
2169                 /*
2170                  * Directory got split. Time to update local object and repeat
2171                  * the request with proper MDS.
2172                  */
2173                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2174                 if (rc == 0)
2175                         goto repeat;
2176         }
2177         RETURN(rc);
2178 }
2179
2180 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2181                        void *ea, int ealen, void *ea2, int ea2len,
2182                        struct ptlrpc_request **request,
2183                        struct md_open_data **mod)
2184 {
2185         struct obd_device       *obd = exp->exp_obd;
2186         struct lmv_obd          *lmv = &obd->u.lmv;
2187         struct ptlrpc_request   *req;
2188         struct lmv_tgt_desc     *tgt;
2189         struct lmv_object       *obj;
2190         int                      rc = 0;
2191         int                      i;
2192         ENTRY;
2193
2194         rc = lmv_check_connect(obd);
2195         if (rc)
2196                 RETURN(rc);
2197
2198         obj = lmv_object_find(obd, &op_data->op_fid1);
2199
2200         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2201                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2202                obj ? ", split" : "");
2203
2204         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2205         if (obj) {
2206                 for (i = 0; i < obj->lo_objcount; i++) {
2207                         op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2208
2209                         tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2210                         if (IS_ERR(tgt)) {
2211                                 rc = PTR_ERR(tgt);
2212                                 break;
2213                         }
2214
2215                         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2216                                         ea2, ea2len, &req, mod);
2217
2218                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2219                                 /*
2220                                  * This is master object and this request should
2221                                  * be returned back to llite.
2222                                  */
2223                                 *request = req;
2224                         } else {
2225                                 ptlrpc_req_finished(req);
2226                         }
2227
2228                         if (rc)
2229                                 break;
2230                 }
2231                 lmv_object_put(obj);
2232         } else {
2233                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2234                 if (IS_ERR(tgt))
2235                         RETURN(PTR_ERR(tgt));
2236
2237                 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2238                                 ea2len, request, mod);
2239         }
2240         RETURN(rc);
2241 }
2242
2243 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2244                     struct obd_capa *oc, struct ptlrpc_request **request)
2245 {
2246         struct obd_device         *obd = exp->exp_obd;
2247         struct lmv_obd            *lmv = &obd->u.lmv;
2248         struct lmv_tgt_desc       *tgt;
2249         int                        rc;
2250         ENTRY;
2251
2252         rc = lmv_check_connect(obd);
2253         if (rc)
2254                 RETURN(rc);
2255
2256         tgt = lmv_find_target(lmv, fid);
2257         if (IS_ERR(tgt))
2258                 RETURN(PTR_ERR(tgt));
2259
2260         rc = md_sync(tgt->ltd_exp, fid, oc, request);
2261         RETURN(rc);
2262 }
2263
2264 /**
2265  * Main purpose of LMV blocking ast is to remove split directory LMV
2266  * presentation object (struct lmv_object) attached to the lock being revoked.
2267  */
2268 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2269                      void *data, int flag)
2270 {
2271         struct lustre_handle    lockh;
2272         struct lmv_object      *obj;
2273         int                     rc;
2274         ENTRY;
2275
2276         switch (flag) {
2277         case LDLM_CB_BLOCKING:
2278                 ldlm_lock2handle(lock, &lockh);
2279                 rc = ldlm_cli_cancel(&lockh);
2280                 if (rc < 0) {
2281                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2282                         RETURN(rc);
2283                 }
2284                 break;
2285         case LDLM_CB_CANCELING:
2286                 /*
2287                  * Time to drop cached attrs for split directory object
2288                  */
2289                 obj = lock->l_ast_data;
2290                 if (obj) {
2291                         CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2292                                ", master "DFID"\n",
2293                                lock->l_resource->lr_name.name[3] == 1 ?
2294                                "LOOKUP" : "UPDATE",
2295                                lock->l_resource->lr_name.name[0],
2296                                lock->l_resource->lr_name.name[1],
2297                                PFID(&obj->lo_fid));
2298                         lmv_object_put(obj);
2299                 }
2300                 break;
2301         default:
2302                 LBUG();
2303         }
2304         RETURN(0);
2305 }
2306
2307 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2308 {
2309         __u64         val;
2310
2311         val = le64_to_cpu(*hash);
2312         if (val < hash_adj)
2313                 val += MAX_HASH_SIZE;
2314         if (val != MDS_DIR_END_OFF)
2315                 *hash = cpu_to_le64(val - hash_adj);
2316 }
2317
2318 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2319 {
2320         __u64              id;
2321         struct obd_import *imp;
2322
2323         /*
2324          * XXX: to get nid we assume that underlying obd device is mdc.
2325          */
2326         imp  = class_exp2cliimp(exp);
2327         id   = imp->imp_connection->c_self + fid_flatten(fid);
2328
2329         CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2330                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2331
2332         return id ^ (id >> 32);
2333 }
2334
2335 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2336                         struct page **pages, struct ptlrpc_request **request)
2337 {
2338         struct obd_device       *obd = exp->exp_obd;
2339         struct lmv_obd          *lmv = &obd->u.lmv;
2340         struct lmv_object       *obj;
2341         struct lu_fid            rid = op_data->op_fid1;
2342         __u64                    offset = op_data->op_offset;
2343         __u64                    hash_adj = 0;
2344         __u32                    rank = 0;
2345         __u64                    seg_size = 0;
2346         __u64                    tgt_tmp = 0;
2347         int                      tgt_idx = 0;
2348         int                      tgt0_idx = 0;
2349         int                      rc;
2350         int                      nr = 0;
2351         int                      i;
2352         /* number of pages read, in CFS_PAGE_SIZE */
2353         int                      nrdpgs;
2354         /* number of pages transferred in LU_PAGE_SIZE */
2355         int                      nlupgs;
2356         struct lmv_stripe       *los;
2357         struct lmv_tgt_desc     *tgt;
2358         struct lu_dirpage       *dp;
2359         struct lu_dirent        *ent;
2360         ENTRY;
2361
2362         rc = lmv_check_connect(obd);
2363         if (rc)
2364                 RETURN(rc);
2365
2366         CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2367
2368         /*
2369          * This case handle directory lookup in clustered metadata case (i.e.
2370          * split directory is located on multiple md servers.)
2371          * each server keeps directory entries for certain range of hashes.
2372          * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2373          * first server will keep records with hashes [ 0 ... MAX_HASH / N  - 1],
2374          * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2375          * so on....
2376          *      readdir can simply start reading entries from 0 - N server in
2377          * order but that will not scale well as all client will request dir in
2378          * to server in same order.
2379          * Following algorithm does optimization:
2380          * Instead of doing readdir in 1, 2, ...., N order, client with a
2381          * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2382          * (every client has rank R)
2383          *      But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2384          * since client ask dir from MDS{R} client has pages with offsets
2385          * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2386          * on hash  values that we get.
2387          */
2388         obj = lmv_object_find_lock(obd, &rid);
2389         if (obj) {
2390                 nr       = obj->lo_objcount;
2391                 LASSERT(nr > 0);
2392                 seg_size = MAX_HASH_SIZE;
2393                 do_div(seg_size, nr);
2394                 los      = obj->lo_stripes;
2395                 tgt      = lmv_get_target(lmv, los[0].ls_mds);
2396                 rank     = lmv_node_rank(tgt->ltd_exp, &rid) % nr;
2397                 tgt_tmp  = offset;
2398                 do_div(tgt_tmp, seg_size);
2399                 tgt0_idx = do_div(tgt_tmp,  nr);
2400                 tgt_idx  = (tgt0_idx + rank) % nr;
2401
2402                 if (tgt_idx < tgt0_idx)
2403                         /*
2404                          * Wrap around.
2405                          *
2406                          * Last segment has unusual length due to division
2407                          * rounding.
2408                          */
2409                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2410                 else
2411                         hash_adj = 0;
2412
2413                 hash_adj += rank * seg_size;
2414
2415                 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2416                        LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2417                        offset, tgt0_idx, offset + hash_adj, tgt_idx);
2418
2419                 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2420                 rid = obj->lo_stripes[tgt_idx].ls_fid;
2421                 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2422
2423                 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2424                        PFID(&rid), (unsigned long)offset, tgt_idx);
2425         } else
2426                 tgt = lmv_find_target(lmv, &rid);
2427
2428         if (IS_ERR(tgt))
2429                 GOTO(cleanup, rc = PTR_ERR(tgt));
2430
2431         op_data->op_fid1 = rid;
2432         rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2433         if (rc)
2434                 GOTO(cleanup, rc);
2435
2436         nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2437                  >> CFS_PAGE_SHIFT;
2438         nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2439         LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2440         LASSERT(nrdpgs > 0 && nrdpgs <= op_data->op_npages);
2441
2442         CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs,
2443                op_data->op_npages);
2444
2445         for (i = 0; i < nrdpgs; i++) {
2446 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2447                 struct lu_dirpage *first;
2448                 __u64 hash_end = 0;
2449                 __u32 flags = 0;
2450 #endif
2451                 struct lu_dirent *tmp = NULL;
2452
2453                 dp = cfs_kmap(pages[i]);
2454                 if (obj) {
2455                         lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2456                         lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2457                         LASSERT(le64_to_cpu(dp->ldp_hash_start) <=
2458                                 op_data->op_offset);
2459
2460                         if ((tgt0_idx != nr - 1) &&
2461                             (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2462                         {
2463                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2464                                                                (tgt0_idx + 1));
2465                                 CDEBUG(D_INODE,
2466                                        ""DFID" reset end "LPX64" tgt %d\n",
2467                                        PFID(&rid),
2468                                        (__u64)le64_to_cpu(dp->ldp_hash_end),
2469                                        tgt_idx);
2470                         }
2471                 }
2472
2473                 ent = lu_dirent_start(dp);
2474 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2475                 first = dp;
2476                 hash_end = dp->ldp_hash_end;
2477 repeat:
2478 #endif
2479                 nlupgs--;
2480                 for (tmp = ent; ent != NULL;
2481                      tmp = ent, ent = lu_dirent_next(ent)) {
2482                         if (obj)
2483                                 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2484                 }
2485
2486 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2487                 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2488                 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2489                         ent = lu_dirent_start(dp);
2490
2491                         if (obj) {
2492                                 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2493                                 if ((tgt0_idx != nr - 1) &&
2494                                     (le64_to_cpu(dp->ldp_hash_end) ==
2495                                      MDS_DIR_END_OFF)) {
2496                                         hash_end = cpu_to_le32(seg_size *
2497                                                                (tgt0_idx + 1));
2498                                         CDEBUG(D_INODE,
2499                                             ""DFID" reset end "LPX64" tgt %d\n",
2500                                             PFID(&rid),
2501                                             (__u64)le64_to_cpu(hash_end),
2502                                             tgt_idx);
2503                                 }
2504                         }
2505                         hash_end = dp->ldp_hash_end;
2506                         flags = dp->ldp_flags;
2507
2508                         if (tmp) {
2509                                 /* enlarge the end entry lde_reclen from 0 to
2510                                  * first entry of next lu_dirpage, in this way
2511                                  * several lu_dirpages can be stored into one
2512                                  * client page on client. */
2513                                 tmp = ((void *)tmp) +
2514                                       le16_to_cpu(tmp->lde_reclen);
2515                                 tmp->lde_reclen =
2516                                         cpu_to_le16((char *)(dp->ldp_entries) -
2517                                                     (char *)tmp);
2518                                 goto repeat;
2519                         }
2520                 }
2521                 first->ldp_hash_end = hash_end;
2522                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2523                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2524 #else
2525                 SET_BUT_UNUSED(tmp);
2526 #endif
2527                 cfs_kunmap(pages[i]);
2528         }
2529         EXIT;
2530 cleanup:
2531         if (obj)
2532                 lmv_object_put_unlock(obj);
2533         return rc;
2534 }
2535
2536 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2537                       struct ptlrpc_request **request)
2538 {
2539         struct obd_device       *obd = exp->exp_obd;
2540         struct lmv_obd          *lmv = &obd->u.lmv;
2541         struct lmv_tgt_desc     *tgt = NULL;
2542         struct lmv_object       *obj;
2543         int                      rc;
2544         int                      sidx;
2545         int                      loop = 0;
2546         ENTRY;
2547
2548         rc = lmv_check_connect(obd);
2549         if (rc)
2550                 RETURN(rc);
2551
2552 repeat:
2553         ++loop;
2554         LASSERT(loop <= 2);
2555         LASSERT(op_data->op_namelen != 0);
2556
2557         obj = lmv_object_find(obd, &op_data->op_fid1);
2558         if (obj) {
2559                 sidx = raw_name2idx(obj->lo_hashtype,
2560                                     obj->lo_objcount,
2561                                     op_data->op_name,
2562                                     op_data->op_namelen);
2563                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2564                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2565                 tgt = lmv_get_target(lmv,
2566                                      obj->lo_stripes[sidx].ls_mds);
2567                 lmv_object_put(obj);
2568                 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2569                        op_data->op_namelen, op_data->op_name,
2570                        PFID(&op_data->op_fid1), sidx);
2571         }
2572
2573         if (tgt == NULL) {
2574                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2575                 if (IS_ERR(tgt))
2576                         RETURN(PTR_ERR(tgt));
2577                 op_data->op_bias |= MDS_CHECK_SPLIT;
2578         }
2579
2580         op_data->op_fsuid = cfs_curproc_fsuid();
2581         op_data->op_fsgid = cfs_curproc_fsgid();
2582         op_data->op_cap = cfs_curproc_cap_pack();
2583
2584         /*
2585          * If child's fid is given, cancel unused locks for it if it is from
2586          * another export than parent.
2587          *
2588          * LOOKUP lock for child (fid3) should also be cancelled on parent
2589          * tgt_tgt in mdc_unlink().
2590          */
2591         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2592
2593         /*
2594          * Cancel FULL locks on child (fid3).
2595          */
2596         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2597                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2598
2599         if (rc == 0)
2600                 rc = md_unlink(tgt->ltd_exp, op_data, request);
2601
2602         if (rc == -ERESTART) {
2603                 LASSERT(*request != NULL);
2604                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2605                           "Got -ERESTART during unlink!\n");
2606                 ptlrpc_req_finished(*request);
2607                 *request = NULL;
2608
2609                 /*
2610                  * Directory got split. Time to update local object and repeat
2611                  * the request with proper MDS.
2612                  */
2613                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2614                 if (rc == 0)
2615                         goto repeat;
2616         }
2617         RETURN(rc);
2618 }
2619
2620 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2621 {
2622         struct lmv_obd *lmv = &obd->u.lmv;
2623         int rc = 0;
2624
2625         switch (stage) {
2626         case OBD_CLEANUP_EARLY:
2627                 /* XXX: here should be calling obd_precleanup() down to
2628                  * stack. */
2629                 break;
2630         case OBD_CLEANUP_EXPORTS:
2631                 fld_client_proc_fini(&lmv->lmv_fld);
2632                 lprocfs_obd_cleanup(obd);
2633                 break;
2634         default:
2635                 break;
2636         }
2637         RETURN(rc);
2638 }
2639
2640 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2641                         __u32 keylen, void *key, __u32 *vallen, void *val,
2642                         struct lov_stripe_md *lsm)
2643 {
2644         struct obd_device       *obd;
2645         struct lmv_obd          *lmv;
2646         int                      rc = 0;
2647         ENTRY;
2648
2649         obd = class_exp2obd(exp);
2650         if (obd == NULL) {
2651                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2652                        exp->exp_handle.h_cookie);
2653                 RETURN(-EINVAL);
2654         }
2655
2656         lmv = &obd->u.lmv;
2657         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2658                 struct lmv_tgt_desc *tgts;
2659                 int i;
2660
2661                 rc = lmv_check_connect(obd);
2662                 if (rc)
2663                         RETURN(rc);
2664
2665                 LASSERT(*vallen == sizeof(__u32));
2666                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2667                      i++, tgts++) {
2668
2669                         /*
2670                          * All tgts should be connected when this gets called.
2671                          */
2672                         if (!tgts || !tgts->ltd_exp) {
2673                                 CERROR("target not setup?\n");
2674                                 continue;
2675                         }
2676
2677                         if (!obd_get_info(env, tgts->ltd_exp, keylen, key,
2678                                           vallen, val, NULL))
2679                                 RETURN(0);
2680                 }
2681                 RETURN(-EINVAL);
2682         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2683                 rc = lmv_check_connect(obd);
2684                 if (rc)
2685                         RETURN(rc);
2686
2687                 /*
2688                  * Forwarding this request to first MDS, it should know LOV
2689                  * desc.
2690                  */
2691                 rc = obd_get_info(env, lmv->tgts[0].ltd_exp, keylen, key,
2692                                   vallen, val, NULL);
2693                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2694                         exp->exp_connect_flags =
2695                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2696                 }
2697                 RETURN(rc);
2698         } else if (KEY_IS(KEY_TGT_COUNT)) {
2699                 *((int *)val) = lmv->desc.ld_tgt_count;
2700                 RETURN(0);
2701         }
2702
2703         CDEBUG(D_IOCTL, "Invalid key\n");
2704         RETURN(-EINVAL);
2705 }
2706
2707 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2708                        obd_count keylen, void *key, obd_count vallen,
2709                        void *val, struct ptlrpc_request_set *set)
2710 {
2711         struct lmv_tgt_desc    *tgt;
2712         struct obd_device      *obd;
2713         struct lmv_obd         *lmv;
2714         int rc = 0;
2715         ENTRY;
2716
2717         obd = class_exp2obd(exp);
2718         if (obd == NULL) {
2719                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2720                        exp->exp_handle.h_cookie);
2721                 RETURN(-EINVAL);
2722         }
2723         lmv = &obd->u.lmv;
2724
2725         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2726                 int i, err = 0;
2727
2728                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2729                         tgt = &lmv->tgts[i];
2730
2731                         if (!tgt->ltd_exp)
2732                                 continue;
2733
2734                         err = obd_set_info_async(env, tgt->ltd_exp,
2735                                                  keylen, key, vallen, val, set);
2736                         if (err && rc == 0)
2737                                 rc = err;
2738                 }
2739
2740                 RETURN(rc);
2741         }
2742
2743         RETURN(-EINVAL);
2744 }
2745
2746 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2747                struct lov_stripe_md *lsm)
2748 {
2749         struct obd_device         *obd = class_exp2obd(exp);
2750         struct lmv_obd            *lmv = &obd->u.lmv;
2751         struct lmv_stripe_md      *meap;
2752         struct lmv_stripe_md      *lsmp;
2753         int                        mea_size;
2754         int                        i;
2755         ENTRY;
2756
2757         mea_size = lmv_get_easize(lmv);
2758         if (!lmmp)
2759                 RETURN(mea_size);
2760
2761         if (*lmmp && !lsm) {
2762                 OBD_FREE_LARGE(*lmmp, mea_size);
2763                 *lmmp = NULL;
2764                 RETURN(0);
2765         }
2766
2767         if (*lmmp == NULL) {
2768                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2769                 if (*lmmp == NULL)
2770                         RETURN(-ENOMEM);
2771         }
2772
2773         if (!lsm)
2774                 RETURN(mea_size);
2775
2776         lsmp = (struct lmv_stripe_md *)lsm;
2777         meap = (struct lmv_stripe_md *)*lmmp;
2778
2779         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2780             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2781                 RETURN(-EINVAL);
2782
2783         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2784         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2785         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2786
2787         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2788                 meap->mea_ids[i] = meap->mea_ids[i];
2789                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2790         }
2791
2792         RETURN(mea_size);
2793 }
2794
2795 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2796                  struct lov_mds_md *lmm, int lmm_size)
2797 {
2798         struct obd_device          *obd = class_exp2obd(exp);
2799         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2800         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2801         struct lmv_obd             *lmv = &obd->u.lmv;
2802         int                         mea_size;
2803         int                         i;
2804         __u32                       magic;
2805         ENTRY;
2806
2807         mea_size = lmv_get_easize(lmv);
2808         if (lsmp == NULL)
2809                 return mea_size;
2810
2811         if (*lsmp != NULL && lmm == NULL) {
2812                 OBD_FREE_LARGE(*tmea, mea_size);
2813                 *lsmp = NULL;
2814                 RETURN(0);
2815         }
2816
2817         LASSERT(mea_size == lmm_size);
2818
2819         OBD_ALLOC_LARGE(*tmea, mea_size);
2820         if (*tmea == NULL)
2821                 RETURN(-ENOMEM);
2822
2823         if (!lmm)
2824                 RETURN(mea_size);
2825
2826         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2827             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2828             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2829         {
2830                 magic = le32_to_cpu(mea->mea_magic);
2831         } else {
2832                 /*
2833                  * Old mea is not handled here.
2834                  */
2835                 CERROR("Old not supportable EA is found\n");
2836                 LBUG();
2837         }
2838
2839         (*tmea)->mea_magic = magic;
2840         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2841         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2842
2843         for (i = 0; i < (*tmea)->mea_count; i++) {
2844                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2845                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2846         }
2847         RETURN(mea_size);
2848 }
2849
2850 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2851                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2852                              ldlm_cancel_flags_t flags, void *opaque)
2853 {
2854         struct obd_device       *obd = exp->exp_obd;
2855         struct lmv_obd          *lmv = &obd->u.lmv;
2856         int                      rc = 0;
2857         int                      err;
2858         int                      i;
2859         ENTRY;
2860
2861         LASSERT(fid != NULL);
2862
2863         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2864                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2865                         continue;
2866
2867                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2868                                        policy, mode, flags, opaque);
2869                 if (!rc)
2870                         rc = err;
2871         }
2872         RETURN(rc);
2873 }
2874
2875 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2876                       __u64 *bits)
2877 {
2878         struct obd_device       *obd = exp->exp_obd;
2879         struct lmv_obd          *lmv = &obd->u.lmv;
2880         int                      rc;
2881         ENTRY;
2882
2883         rc =  md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2884         RETURN(rc);
2885 }
2886
2887 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2888                            const struct lu_fid *fid, ldlm_type_t type,
2889                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2890                            struct lustre_handle *lockh)
2891 {
2892         struct obd_device       *obd = exp->exp_obd;
2893         struct lmv_obd          *lmv = &obd->u.lmv;
2894         ldlm_mode_t              rc;
2895         int                      i;
2896         ENTRY;
2897
2898         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2899
2900         /*
2901          * With CMD every object can have two locks in different namespaces:
2902          * lookup lock in space of mds storing direntry and update/open lock in
2903          * space of mds storing inode. Thus we check all targets, not only that
2904          * one fid was created in.
2905          */
2906         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2907                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2908                                    type, policy, mode, lockh);
2909                 if (rc)
2910                         RETURN(rc);
2911         }
2912
2913         RETURN(0);
2914 }
2915
2916 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2917                       struct obd_export *dt_exp, struct obd_export *md_exp,
2918                       struct lustre_md *md)
2919 {
2920         struct obd_device       *obd = exp->exp_obd;
2921         struct lmv_obd          *lmv = &obd->u.lmv;
2922         int                      rc;
2923         ENTRY;
2924         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2925         RETURN(rc);
2926 }
2927
2928 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2929 {
2930         struct obd_device       *obd = exp->exp_obd;
2931         struct lmv_obd          *lmv = &obd->u.lmv;
2932         ENTRY;
2933
2934         if (md->mea)
2935                 obd_free_memmd(exp, (void *)&md->mea);
2936         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2937 }
2938
2939 int lmv_set_open_replay_data(struct obd_export *exp,
2940                              struct obd_client_handle *och,
2941                              struct ptlrpc_request *open_req)
2942 {
2943         struct obd_device       *obd = exp->exp_obd;
2944         struct lmv_obd          *lmv = &obd->u.lmv;
2945         struct lmv_tgt_desc     *tgt;
2946         ENTRY;
2947
2948         tgt = lmv_find_target(lmv, &och->och_fid);
2949         if (IS_ERR(tgt))
2950                 RETURN(PTR_ERR(tgt));
2951
2952         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2953 }
2954
2955 int lmv_clear_open_replay_data(struct obd_export *exp,
2956                                struct obd_client_handle *och)
2957 {
2958         struct obd_device       *obd = exp->exp_obd;
2959         struct lmv_obd          *lmv = &obd->u.lmv;
2960         struct lmv_tgt_desc     *tgt;
2961         ENTRY;
2962
2963         tgt = lmv_find_target(lmv, &och->och_fid);
2964         if (IS_ERR(tgt))
2965                 RETURN(PTR_ERR(tgt));
2966
2967         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2968 }
2969
2970 static int lmv_get_remote_perm(struct obd_export *exp,
2971                                const struct lu_fid *fid,
2972                                struct obd_capa *oc, __u32 suppgid,
2973                                struct ptlrpc_request **request)
2974 {
2975         struct obd_device       *obd = exp->exp_obd;
2976         struct lmv_obd          *lmv = &obd->u.lmv;
2977         struct lmv_tgt_desc     *tgt;
2978         int                      rc;
2979         ENTRY;
2980
2981         rc = lmv_check_connect(obd);
2982         if (rc)
2983                 RETURN(rc);
2984
2985         tgt = lmv_find_target(lmv, fid);
2986         if (IS_ERR(tgt))
2987                 RETURN(PTR_ERR(tgt));
2988
2989         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2990         RETURN(rc);
2991 }
2992
2993 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2994                           renew_capa_cb_t cb)
2995 {
2996         struct obd_device       *obd = exp->exp_obd;
2997         struct lmv_obd          *lmv = &obd->u.lmv;
2998         struct lmv_tgt_desc     *tgt;
2999         int                      rc;
3000         ENTRY;
3001
3002         rc = lmv_check_connect(obd);
3003         if (rc)
3004                 RETURN(rc);
3005
3006         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3007         if (IS_ERR(tgt))
3008                 RETURN(PTR_ERR(tgt));
3009
3010         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3011         RETURN(rc);
3012 }
3013
3014 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3015                     const struct req_msg_field *field, struct obd_capa **oc)
3016 {
3017         struct obd_device *obd = exp->exp_obd;
3018         struct lmv_obd *lmv = &obd->u.lmv;
3019         int rc;
3020
3021         ENTRY;
3022         rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3023         RETURN(rc);
3024 }
3025
3026 int lmv_intent_getattr_async(struct obd_export *exp,
3027                              struct md_enqueue_info *minfo,
3028                              struct ldlm_enqueue_info *einfo)
3029 {
3030         struct md_op_data       *op_data = &minfo->mi_data;
3031         struct obd_device       *obd = exp->exp_obd;
3032         struct lmv_obd          *lmv = &obd->u.lmv;
3033         struct lmv_object       *obj;
3034         struct lmv_tgt_desc     *tgt = NULL;
3035         int                      rc;
3036         int                      sidx;
3037         ENTRY;
3038
3039         rc = lmv_check_connect(obd);
3040         if (rc)
3041                 RETURN(rc);
3042
3043         if (op_data->op_namelen) {
3044                 obj = lmv_object_find(obd, &op_data->op_fid1);
3045                 if (obj) {
3046                         sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3047                                             (char *)op_data->op_name,
3048                                             op_data->op_namelen);
3049                         op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3050                         tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3051                         lmv_object_put(obj);
3052                 }
3053         }
3054
3055         if (tgt == NULL)
3056                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3057
3058         if (IS_ERR(tgt))
3059                 RETURN(PTR_ERR(tgt));
3060
3061         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3062         RETURN(rc);
3063 }
3064
3065 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3066                         struct lu_fid *fid, __u64 *bits)
3067 {
3068         struct obd_device       *obd = exp->exp_obd;
3069         struct lmv_obd          *lmv = &obd->u.lmv;
3070         struct lmv_tgt_desc     *tgt;
3071         int                      rc;
3072         ENTRY;
3073
3074         rc = lmv_check_connect(obd);
3075         if (rc)
3076                 RETURN(rc);
3077
3078         tgt = lmv_find_target(lmv, fid);
3079         if (IS_ERR(tgt))
3080                 RETURN(PTR_ERR(tgt));
3081
3082         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3083         RETURN(rc);
3084 }
3085
3086 /**
3087  * For lmv, only need to send request to master MDT, and the master MDT will
3088  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3089  * we directly fetch data from the slave MDTs.
3090  */
3091 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3092                  struct obd_quotactl *oqctl)
3093 {
3094         struct obd_device   *obd = class_exp2obd(exp);
3095         struct lmv_obd      *lmv = &obd->u.lmv;
3096         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3097         int                  rc = 0, i;
3098         __u64                curspace, curinodes;
3099         ENTRY;
3100
3101         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3102                 CERROR("master lmv inactive\n");
3103                 RETURN(-EIO);
3104         }
3105
3106         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3107                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3108                 RETURN(rc);
3109         }
3110
3111         curspace = curinodes = 0;
3112         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3113                 int err;
3114                 tgt = &lmv->tgts[i];
3115
3116                 if (tgt->ltd_exp == NULL)
3117                         continue;
3118                 if (!tgt->ltd_active) {
3119                         CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3120                         continue;
3121                 }
3122
3123                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3124                 if (err) {
3125                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3126                         if (!rc)
3127                                 rc = err;
3128                 } else {
3129                         curspace += oqctl->qc_dqblk.dqb_curspace;
3130                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3131                 }
3132         }
3133         oqctl->qc_dqblk.dqb_curspace = curspace;
3134         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3135
3136         RETURN(rc);
3137 }
3138
3139 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3140                    struct obd_quotactl *oqctl)
3141 {
3142         struct obd_device   *obd = class_exp2obd(exp);
3143         struct lmv_obd      *lmv = &obd->u.lmv;
3144         struct lmv_tgt_desc *tgt;
3145         int                  i, rc = 0;
3146         ENTRY;
3147
3148         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3149                 int err;
3150
3151                 if (!tgt->ltd_active) {
3152                         CERROR("lmv idx %d inactive\n", i);
3153                         RETURN(-EIO);
3154                 }
3155
3156                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3157                 if (err && !rc)
3158                         rc = err;
3159         }
3160
3161         RETURN(rc);
3162 }
3163
3164 struct obd_ops lmv_obd_ops = {
3165         .o_owner                = THIS_MODULE,
3166         .o_setup                = lmv_setup,
3167         .o_cleanup              = lmv_cleanup,
3168         .o_precleanup           = lmv_precleanup,
3169         .o_process_config       = lmv_process_config,
3170         .o_connect              = lmv_connect,
3171         .o_disconnect           = lmv_disconnect,
3172         .o_statfs               = lmv_statfs,
3173         .o_get_info             = lmv_get_info,
3174         .o_set_info_async       = lmv_set_info_async,
3175         .o_packmd               = lmv_packmd,
3176         .o_unpackmd             = lmv_unpackmd,
3177         .o_notify               = lmv_notify,
3178         .o_get_uuid             = lmv_get_uuid,
3179         .o_iocontrol            = lmv_iocontrol,
3180         .o_fid_delete           = lmv_fid_delete,
3181         .o_quotacheck           = lmv_quotacheck,
3182         .o_quotactl             = lmv_quotactl
3183 };
3184
3185 struct md_ops lmv_md_ops = {
3186         .m_getstatus            = lmv_getstatus,
3187         .m_change_cbdata        = lmv_change_cbdata,
3188         .m_find_cbdata          = lmv_find_cbdata,
3189         .m_close                = lmv_close,
3190         .m_create               = lmv_create,
3191         .m_done_writing         = lmv_done_writing,
3192         .m_enqueue              = lmv_enqueue,
3193         .m_getattr              = lmv_getattr,
3194         .m_getxattr             = lmv_getxattr,
3195         .m_getattr_name         = lmv_getattr_name,
3196         .m_intent_lock          = lmv_intent_lock,
3197         .m_link                 = lmv_link,
3198         .m_rename               = lmv_rename,
3199         .m_setattr              = lmv_setattr,
3200         .m_setxattr             = lmv_setxattr,
3201         .m_sync                 = lmv_sync,
3202         .m_readpage             = lmv_readpage,
3203         .m_unlink               = lmv_unlink,
3204         .m_init_ea_size         = lmv_init_ea_size,
3205         .m_cancel_unused        = lmv_cancel_unused,
3206         .m_set_lock_data        = lmv_set_lock_data,
3207         .m_lock_match           = lmv_lock_match,
3208         .m_get_lustre_md        = lmv_get_lustre_md,
3209         .m_free_lustre_md       = lmv_free_lustre_md,
3210         .m_set_open_replay_data = lmv_set_open_replay_data,
3211         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3212         .m_renew_capa           = lmv_renew_capa,
3213         .m_unpack_capa          = lmv_unpack_capa,
3214         .m_get_remote_perm      = lmv_get_remote_perm,
3215         .m_intent_getattr_async = lmv_intent_getattr_async,
3216         .m_revalidate_lock      = lmv_revalidate_lock
3217 };
3218
3219 int __init lmv_init(void)
3220 {
3221         struct lprocfs_static_vars lvars;
3222         int                        rc;
3223
3224         lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3225                                                 sizeof(struct lmv_object),
3226                                                 0, 0);
3227         if (!lmv_object_cache) {
3228                 CERROR("Error allocating lmv objects cache\n");
3229                 return -ENOMEM;
3230         }
3231
3232         lprocfs_lmv_init_vars(&lvars);
3233
3234         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3235                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3236         if (rc)
3237                 cfs_mem_cache_destroy(lmv_object_cache);
3238
3239         return rc;
3240 }
3241
3242 #ifdef __KERNEL__
3243 static void lmv_exit(void)
3244 {
3245         class_unregister_type(LUSTRE_LMV_NAME);
3246
3247         LASSERTF(cfs_atomic_read(&lmv_object_count) == 0,
3248                  "Can't free lmv objects cache, %d object(s) busy\n",
3249                  cfs_atomic_read(&lmv_object_count));
3250         cfs_mem_cache_destroy(lmv_object_cache);
3251 }
3252
3253 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3254 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3255 MODULE_LICENSE("GPL");
3256
3257 module_init(lmv_init);
3258 module_exit(lmv_exit);
3259 #endif