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