Whamcloud - gitweb
LU-819 utils: Fix lfs getstripe -M
[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",
1114                                         0444, &lmv_proc_target_fops, obd);
1115                 if (rc)
1116                         CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1117                                obd->obd_name, rc);
1118        }
1119 #endif
1120         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1121                              LUSTRE_CLI_FLD_HASH_DHT);
1122         if (rc) {
1123                 CERROR("Can't init FLD, err %d\n", rc);
1124                 GOTO(out_free_datas, rc);
1125         }
1126
1127         RETURN(0);
1128
1129 out_free_datas:
1130         OBD_FREE(lmv->datas, lmv->datas_size);
1131         lmv->datas = NULL;
1132 out_free_tgts:
1133         OBD_FREE(lmv->tgts, lmv->tgts_size);
1134         lmv->tgts = NULL;
1135         return rc;
1136 }
1137
1138 static int lmv_cleanup(struct obd_device *obd)
1139 {
1140         struct lmv_obd   *lmv = &obd->u.lmv;
1141         ENTRY;
1142
1143         fld_client_fini(&lmv->lmv_fld);
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         int                      rc;
2078         int                      sidx;
2079         int                      loop = 0;
2080         struct lmv_object       *obj;
2081         mdsno_t                  mds1;
2082         mdsno_t                  mds2;
2083         ENTRY;
2084
2085         LASSERT(oldlen != 0);
2086
2087         CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2088                oldlen, old, PFID(&op_data->op_fid1),
2089                newlen, new, PFID(&op_data->op_fid2));
2090
2091         rc = lmv_check_connect(obd);
2092         if (rc)
2093                 RETURN(rc);
2094
2095 repeat:
2096         ++loop;
2097         LASSERT(loop <= 2);
2098         obj = lmv_object_find(obd, &op_data->op_fid1);
2099         if (obj) {
2100                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2101                                     (char *)old, oldlen);
2102                 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2103                 mds1 = obj->lo_stripes[sidx].ls_mds;
2104                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2105                 lmv_object_put(obj);
2106         } else {
2107                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2108                 if (rc)
2109                         RETURN(rc);
2110         }
2111
2112         obj = lmv_object_find(obd, &op_data->op_fid2);
2113         if (obj) {
2114                 /*
2115                  * Directory is already split, so we have to forward request to
2116                  * the right MDS.
2117                  */
2118                 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2119                                        (char *)new, newlen);
2120
2121                 mds2 = obj->lo_stripes[sidx].ls_mds;
2122                 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2123                 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2124                 lmv_object_put(obj);
2125         } else {
2126                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2127                 if (rc)
2128                         RETURN(rc);
2129         }
2130
2131         op_data->op_fsuid = cfs_curproc_fsuid();
2132         op_data->op_fsgid = cfs_curproc_fsgid();
2133         op_data->op_cap = cfs_curproc_cap_pack();
2134
2135         src_tgt = lmv_get_target(lmv, mds1);
2136
2137         /*
2138          * LOOKUP lock on src child (fid3) should also be cancelled for
2139          * src_tgt in mdc_rename.
2140          */
2141         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2142
2143         /*
2144          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2145          * own target.
2146          */
2147         rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2148                               LCK_EX, MDS_INODELOCK_UPDATE,
2149                               MF_MDC_CANCEL_FID2);
2150
2151         /*
2152          * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2153          */
2154         if (rc == 0) {
2155                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2156                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2157                                       MF_MDC_CANCEL_FID4);
2158         }
2159
2160         /*
2161          * Cancel all the locks on tgt child (fid4).
2162          */
2163         if (rc == 0)
2164                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2165                                       LCK_EX, MDS_INODELOCK_FULL,
2166                                       MF_MDC_CANCEL_FID4);
2167
2168         if (rc == 0)
2169                 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2170                                new, newlen, request);
2171
2172         if (rc == -ERESTART) {
2173                 LASSERT(*request != NULL);
2174                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2175                           "Got -ERESTART during rename!\n");
2176                 ptlrpc_req_finished(*request);
2177                 *request = NULL;
2178
2179                 /*
2180                  * Directory got split. Time to update local object and repeat
2181                  * the request with proper MDS.
2182                  */
2183                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2184                 if (rc == 0)
2185                         goto repeat;
2186         }
2187         RETURN(rc);
2188 }
2189
2190 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2191                        void *ea, int ealen, void *ea2, int ea2len,
2192                        struct ptlrpc_request **request,
2193                        struct md_open_data **mod)
2194 {
2195         struct obd_device       *obd = exp->exp_obd;
2196         struct lmv_obd          *lmv = &obd->u.lmv;
2197         struct ptlrpc_request   *req;
2198         struct lmv_tgt_desc     *tgt;
2199         struct lmv_object       *obj;
2200         int                      rc = 0;
2201         int                      i;
2202         ENTRY;
2203
2204         rc = lmv_check_connect(obd);
2205         if (rc)
2206                 RETURN(rc);
2207
2208         obj = lmv_object_find(obd, &op_data->op_fid1);
2209
2210         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2211                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2212                obj ? ", split" : "");
2213
2214         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2215         if (obj) {
2216                 for (i = 0; i < obj->lo_objcount; i++) {
2217                         op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2218
2219                         tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2220                         if (IS_ERR(tgt)) {
2221                                 rc = PTR_ERR(tgt);
2222                                 break;
2223                         }
2224
2225                         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2226                                         ea2, ea2len, &req, mod);
2227
2228                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2229                                 /*
2230                                  * This is master object and this request should
2231                                  * be returned back to llite.
2232                                  */
2233                                 *request = req;
2234                         } else {
2235                                 ptlrpc_req_finished(req);
2236                         }
2237
2238                         if (rc)
2239                                 break;
2240                 }
2241                 lmv_object_put(obj);
2242         } else {
2243                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2244                 if (IS_ERR(tgt))
2245                         RETURN(PTR_ERR(tgt));
2246
2247                 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2248                                 ea2len, request, mod);
2249         }
2250         RETURN(rc);
2251 }
2252
2253 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2254                     struct obd_capa *oc, struct ptlrpc_request **request)
2255 {
2256         struct obd_device         *obd = exp->exp_obd;
2257         struct lmv_obd            *lmv = &obd->u.lmv;
2258         struct lmv_tgt_desc       *tgt;
2259         int                        rc;
2260         ENTRY;
2261
2262         rc = lmv_check_connect(obd);
2263         if (rc)
2264                 RETURN(rc);
2265
2266         tgt = lmv_find_target(lmv, fid);
2267         if (IS_ERR(tgt))
2268                 RETURN(PTR_ERR(tgt));
2269
2270         rc = md_sync(tgt->ltd_exp, fid, oc, request);
2271         RETURN(rc);
2272 }
2273
2274 /**
2275  * Main purpose of LMV blocking ast is to remove split directory LMV
2276  * presentation object (struct lmv_object) attached to the lock being revoked.
2277  */
2278 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2279                      void *data, int flag)
2280 {
2281         struct lustre_handle    lockh;
2282         struct lmv_object      *obj;
2283         int                     rc;
2284         ENTRY;
2285
2286         switch (flag) {
2287         case LDLM_CB_BLOCKING:
2288                 ldlm_lock2handle(lock, &lockh);
2289                 rc = ldlm_cli_cancel(&lockh);
2290                 if (rc < 0) {
2291                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2292                         RETURN(rc);
2293                 }
2294                 break;
2295         case LDLM_CB_CANCELING:
2296                 /*
2297                  * Time to drop cached attrs for split directory object
2298                  */
2299                 obj = lock->l_ast_data;
2300                 if (obj) {
2301                         CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2302                                ", master "DFID"\n",
2303                                lock->l_resource->lr_name.name[3] == 1 ?
2304                                "LOOKUP" : "UPDATE",
2305                                lock->l_resource->lr_name.name[0],
2306                                lock->l_resource->lr_name.name[1],
2307                                PFID(&obj->lo_fid));
2308                         lmv_object_put(obj);
2309                 }
2310                 break;
2311         default:
2312                 LBUG();
2313         }
2314         RETURN(0);
2315 }
2316
2317 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2318 {
2319         __u64         val;
2320
2321         val = le64_to_cpu(*hash);
2322         if (val < hash_adj)
2323                 val += MAX_HASH_SIZE;
2324         if (val != MDS_DIR_END_OFF)
2325                 *hash = cpu_to_le64(val - hash_adj);
2326 }
2327
2328 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2329 {
2330         __u64              id;
2331         struct obd_import *imp;
2332
2333         /*
2334          * XXX: to get nid we assume that underlying obd device is mdc.
2335          */
2336         imp  = class_exp2cliimp(exp);
2337         id   = imp->imp_connection->c_self + fid_flatten(fid);
2338
2339         CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2340                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2341
2342         return id ^ (id >> 32);
2343 }
2344
2345 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2346                         struct obd_capa *oc, __u64 offset64,
2347                         struct page **pages, unsigned npages,
2348                         struct ptlrpc_request **request)
2349 {
2350         struct obd_device       *obd = exp->exp_obd;
2351         struct lmv_obd          *lmv = &obd->u.lmv;
2352         struct lu_fid            rid = *fid;
2353         struct lmv_object       *obj;
2354         __u64                    offset;
2355         __u64                    hash_adj = 0;
2356         __u32                    rank = 0;
2357         __u64                    seg_size = 0;
2358         __u64                    tgt_tmp = 0;
2359         int                      tgt_idx = 0;
2360         int                      tgt0_idx = 0;
2361         int                      rc;
2362         int                      nr = 0;
2363         int                      i;
2364         /* number of pages read, in CFS_PAGE_SIZE */
2365         int                      nrdpgs;
2366         /* number of pages transferred in LU_PAGE_SIZE */
2367         int                      nlupgs;
2368         struct lmv_stripe       *los;
2369         struct lmv_tgt_desc     *tgt;
2370         struct lu_dirpage       *dp;
2371         struct lu_dirent        *ent;
2372         ENTRY;
2373
2374         offset = offset64;
2375
2376         rc = lmv_check_connect(obd);
2377         if (rc)
2378                 RETURN(rc);
2379
2380         CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2381
2382         /*
2383          * This case handle directory lookup in clustered metadata case (i.e.
2384          * split directory is located on multiple md servers.)
2385          * each server keeps directory entries for certain range of hashes.
2386          * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2387          * first server will keep records with hashes [ 0 ... MAX_HASH / N  - 1],
2388          * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2389          * so on....
2390          *      readdir can simply start reading entries from 0 - N server in
2391          * order but that will not scale well as all client will request dir in
2392          * to server in same order.
2393          * Following algorithm does optimization:
2394          * Instead of doing readdir in 1, 2, ...., N order, client with a
2395          * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2396          * (every client has rank R)
2397          *      But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2398          * since client ask dir from MDS{R} client has pages with offsets
2399          * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2400          * on hash  values that we get.
2401          */
2402         obj = lmv_object_find_lock(obd, fid);
2403         if (obj) {
2404                 nr       = obj->lo_objcount;
2405                 LASSERT(nr > 0);
2406                 seg_size = MAX_HASH_SIZE;
2407                 do_div(seg_size, nr);
2408                 los      = obj->lo_stripes;
2409                 tgt      = lmv_get_target(lmv, los[0].ls_mds);
2410                 rank     = lmv_node_rank(tgt->ltd_exp, fid) % nr;
2411                 tgt_tmp  = offset;
2412                 do_div(tgt_tmp, seg_size);
2413                 tgt0_idx = do_div(tgt_tmp,  nr);
2414                 tgt_idx  = (tgt0_idx + rank) % nr;
2415
2416                 if (tgt_idx < tgt0_idx)
2417                         /*
2418                          * Wrap around.
2419                          *
2420                          * Last segment has unusual length due to division
2421                          * rounding.
2422                          */
2423                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2424                 else
2425                         hash_adj = 0;
2426
2427                 hash_adj += rank * seg_size;
2428
2429                 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2430                        LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2431                        offset, tgt0_idx, offset + hash_adj, tgt_idx);
2432
2433                 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2434                 rid = obj->lo_stripes[tgt_idx].ls_fid;
2435                 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2436
2437                 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2438                        PFID(&rid), (unsigned long)offset, tgt_idx);
2439         } else
2440                 tgt = lmv_find_target(lmv, &rid);
2441
2442         if (IS_ERR(tgt))
2443                 GOTO(cleanup, rc = PTR_ERR(tgt));
2444
2445         rc = md_readpage(tgt->ltd_exp, &rid, oc, offset, pages, npages,
2446                          request);
2447         if (rc)
2448                 GOTO(cleanup, rc);
2449
2450         nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2451                  >> CFS_PAGE_SHIFT;
2452         nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2453         LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2454         LASSERT(nrdpgs > 0 && nrdpgs <= npages);
2455
2456         CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs, npages);
2457
2458         for (i = 0; i < nrdpgs; i++) {
2459 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2460                 struct lu_dirpage *first;
2461                 __u64 hash_end = 0;
2462                 __u32 flags = 0;
2463 #endif
2464                 struct lu_dirent *tmp = NULL;
2465
2466                 dp = cfs_kmap(pages[i]);
2467                 if (obj) {
2468                         lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2469                         lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2470                         LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2471
2472                         if ((tgt0_idx != nr - 1) &&
2473                             (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2474                         {
2475                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2476                                                                (tgt0_idx + 1));
2477                                 CDEBUG(D_INODE,
2478                                        ""DFID" reset end "LPX64" tgt %d\n",
2479                                        PFID(&rid),
2480                                        (__u64)le64_to_cpu(dp->ldp_hash_end),
2481                                        tgt_idx);
2482                         }
2483                 }
2484
2485                 ent = lu_dirent_start(dp);
2486 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2487                 first = dp;
2488                 hash_end = dp->ldp_hash_end;
2489 repeat:
2490 #endif
2491                 nlupgs--;
2492                 for (tmp = ent; ent != NULL;
2493                      tmp = ent, ent = lu_dirent_next(ent)) {
2494                         if (obj)
2495                                 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2496                 }
2497
2498 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2499                 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2500                 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2501                         ent = lu_dirent_start(dp);
2502
2503                         if (obj) {
2504                                 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2505                                 if ((tgt0_idx != nr - 1) &&
2506                                     (le64_to_cpu(dp->ldp_hash_end) ==
2507                                      MDS_DIR_END_OFF)) {
2508                                         hash_end = cpu_to_le32(seg_size *
2509                                                                (tgt0_idx + 1));
2510                                         CDEBUG(D_INODE,
2511                                             ""DFID" reset end "LPX64" tgt %d\n",
2512                                             PFID(&rid),
2513                                             (__u64)le64_to_cpu(hash_end),
2514                                             tgt_idx);
2515                                 }
2516                         }
2517                         hash_end = dp->ldp_hash_end;
2518                         flags = dp->ldp_flags;
2519
2520                         if (tmp) {
2521                                 /* enlarge the end entry lde_reclen from 0 to
2522                                  * first entry of next lu_dirpage, in this way
2523                                  * several lu_dirpages can be stored into one
2524                                  * client page on client. */
2525                                 tmp = ((void *)tmp) +
2526                                       le16_to_cpu(tmp->lde_reclen);
2527                                 tmp->lde_reclen =
2528                                         cpu_to_le16((char *)(dp->ldp_entries) -
2529                                                     (char *)tmp);
2530                                 goto repeat;
2531                         }
2532                 }
2533                 first->ldp_hash_end = hash_end;
2534                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2535                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2536 #else
2537                 SET_BUT_UNUSED(tmp);
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         struct lmv_obd *lmv = &obd->u.lmv;
2635         int rc = 0;
2636
2637         switch (stage) {
2638         case OBD_CLEANUP_EARLY:
2639                 /* XXX: here should be calling obd_precleanup() down to
2640                  * stack. */
2641                 break;
2642         case OBD_CLEANUP_EXPORTS:
2643                 fld_client_proc_fini(&lmv->lmv_fld);
2644                 lprocfs_obd_cleanup(obd);
2645                 rc = obd_llog_finish(obd, 0);
2646                 if (rc != 0)
2647                         CERROR("failed to cleanup llogging subsystems\n");
2648                 break;
2649         default:
2650                 break;
2651         }
2652         RETURN(rc);
2653 }
2654
2655 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2656                         void *key, __u32 *vallen, void *val,
2657                         struct lov_stripe_md *lsm)
2658 {
2659         struct obd_device       *obd;
2660         struct lmv_obd          *lmv;
2661         int                      rc = 0;
2662         ENTRY;
2663
2664         obd = class_exp2obd(exp);
2665         if (obd == NULL) {
2666                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2667                        exp->exp_handle.h_cookie);
2668                 RETURN(-EINVAL);
2669         }
2670
2671         lmv = &obd->u.lmv;
2672         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2673                 struct lmv_tgt_desc *tgts;
2674                 int i;
2675
2676                 rc = lmv_check_connect(obd);
2677                 if (rc)
2678                         RETURN(rc);
2679
2680                 LASSERT(*vallen == sizeof(__u32));
2681                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2682                      i++, tgts++) {
2683
2684                         /*
2685                          * All tgts should be connected when this gets called.
2686                          */
2687                         if (!tgts || !tgts->ltd_exp) {
2688                                 CERROR("target not setup?\n");
2689                                 continue;
2690                         }
2691
2692                         if (!obd_get_info(tgts->ltd_exp, keylen, key,
2693                                           vallen, val, NULL))
2694                                 RETURN(0);
2695                 }
2696                 RETURN(-EINVAL);
2697         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2698                 rc = lmv_check_connect(obd);
2699                 if (rc)
2700                         RETURN(rc);
2701
2702                 /*
2703                  * Forwarding this request to first MDS, it should know LOV
2704                  * desc.
2705                  */
2706                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2707                                   vallen, val, NULL);
2708                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2709                         exp->exp_connect_flags =
2710                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2711                 }
2712                 RETURN(rc);
2713         } else if (KEY_IS(KEY_TGT_COUNT)) {
2714                 *((int *)val) = lmv->desc.ld_tgt_count;
2715                 RETURN(0);
2716         }
2717
2718         CDEBUG(D_IOCTL, "Invalid key\n");
2719         RETURN(-EINVAL);
2720 }
2721
2722 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2723                        void *key, obd_count vallen, void *val,
2724                        struct ptlrpc_request_set *set)
2725 {
2726         struct lmv_tgt_desc    *tgt;
2727         struct obd_device      *obd;
2728         struct lmv_obd         *lmv;
2729         int rc = 0;
2730         ENTRY;
2731
2732         obd = class_exp2obd(exp);
2733         if (obd == NULL) {
2734                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2735                        exp->exp_handle.h_cookie);
2736                 RETURN(-EINVAL);
2737         }
2738         lmv = &obd->u.lmv;
2739
2740         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2741                 int i, err = 0;
2742
2743                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2744                         tgt = &lmv->tgts[i];
2745
2746                         if (!tgt->ltd_exp)
2747                                 continue;
2748
2749                         err = obd_set_info_async(tgt->ltd_exp,
2750                                                  keylen, key, vallen, val, set);
2751                         if (err && rc == 0)
2752                                 rc = err;
2753                 }
2754
2755                 RETURN(rc);
2756         }
2757
2758         RETURN(-EINVAL);
2759 }
2760
2761 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2762                struct lov_stripe_md *lsm)
2763 {
2764         struct obd_device         *obd = class_exp2obd(exp);
2765         struct lmv_obd            *lmv = &obd->u.lmv;
2766         struct lmv_stripe_md      *meap;
2767         struct lmv_stripe_md      *lsmp;
2768         int                        mea_size;
2769         int                        i;
2770         ENTRY;
2771
2772         mea_size = lmv_get_easize(lmv);
2773         if (!lmmp)
2774                 RETURN(mea_size);
2775
2776         if (*lmmp && !lsm) {
2777                 OBD_FREE_LARGE(*lmmp, mea_size);
2778                 *lmmp = NULL;
2779                 RETURN(0);
2780         }
2781
2782         if (*lmmp == NULL) {
2783                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2784                 if (*lmmp == NULL)
2785                         RETURN(-ENOMEM);
2786         }
2787
2788         if (!lsm)
2789                 RETURN(mea_size);
2790
2791         lsmp = (struct lmv_stripe_md *)lsm;
2792         meap = (struct lmv_stripe_md *)*lmmp;
2793
2794         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2795             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2796                 RETURN(-EINVAL);
2797
2798         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2799         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2800         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2801
2802         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2803                 meap->mea_ids[i] = meap->mea_ids[i];
2804                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2805         }
2806
2807         RETURN(mea_size);
2808 }
2809
2810 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2811                  struct lov_mds_md *lmm, int lmm_size)
2812 {
2813         struct obd_device          *obd = class_exp2obd(exp);
2814         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2815         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2816         struct lmv_obd             *lmv = &obd->u.lmv;
2817         int                         mea_size;
2818         int                         i;
2819         __u32                       magic;
2820         ENTRY;
2821
2822         mea_size = lmv_get_easize(lmv);
2823         if (lsmp == NULL)
2824                 return mea_size;
2825
2826         if (*lsmp != NULL && lmm == NULL) {
2827                 OBD_FREE_LARGE(*tmea, mea_size);
2828                 *lsmp = NULL;
2829                 RETURN(0);
2830         }
2831
2832         LASSERT(mea_size == lmm_size);
2833
2834         OBD_ALLOC_LARGE(*tmea, mea_size);
2835         if (*tmea == NULL)
2836                 RETURN(-ENOMEM);
2837
2838         if (!lmm)
2839                 RETURN(mea_size);
2840
2841         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2842             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2843             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2844         {
2845                 magic = le32_to_cpu(mea->mea_magic);
2846         } else {
2847                 /*
2848                  * Old mea is not handled here.
2849                  */
2850                 CERROR("Old not supportable EA is found\n");
2851                 LBUG();
2852         }
2853
2854         (*tmea)->mea_magic = magic;
2855         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2856         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2857
2858         for (i = 0; i < (*tmea)->mea_count; i++) {
2859                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2860                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2861         }
2862         RETURN(mea_size);
2863 }
2864
2865 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2866                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2867                              ldlm_cancel_flags_t flags, void *opaque)
2868 {
2869         struct obd_device       *obd = exp->exp_obd;
2870         struct lmv_obd          *lmv = &obd->u.lmv;
2871         int                      rc = 0;
2872         int                      err;
2873         int                      i;
2874         ENTRY;
2875
2876         LASSERT(fid != NULL);
2877
2878         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2879                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2880                         continue;
2881
2882                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2883                                        policy, mode, flags, opaque);
2884                 if (!rc)
2885                         rc = err;
2886         }
2887         RETURN(rc);
2888 }
2889
2890 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2891                       __u64 *bits)
2892 {
2893         struct obd_device       *obd = exp->exp_obd;
2894         struct lmv_obd          *lmv = &obd->u.lmv;
2895         int                      rc;
2896         ENTRY;
2897
2898         rc =  md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2899         RETURN(rc);
2900 }
2901
2902 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2903                            const struct lu_fid *fid, ldlm_type_t type,
2904                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2905                            struct lustre_handle *lockh)
2906 {
2907         struct obd_device       *obd = exp->exp_obd;
2908         struct lmv_obd          *lmv = &obd->u.lmv;
2909         ldlm_mode_t              rc;
2910         int                      i;
2911         ENTRY;
2912
2913         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2914
2915         /*
2916          * With CMD every object can have two locks in different namespaces:
2917          * lookup lock in space of mds storing direntry and update/open lock in
2918          * space of mds storing inode. Thus we check all targets, not only that
2919          * one fid was created in.
2920          */
2921         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2922                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2923                                    type, policy, mode, lockh);
2924                 if (rc)
2925                         RETURN(rc);
2926         }
2927
2928         RETURN(0);
2929 }
2930
2931 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2932                       struct obd_export *dt_exp, struct obd_export *md_exp,
2933                       struct lustre_md *md)
2934 {
2935         struct obd_device       *obd = exp->exp_obd;
2936         struct lmv_obd          *lmv = &obd->u.lmv;
2937         int                      rc;
2938         ENTRY;
2939         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2940         RETURN(rc);
2941 }
2942
2943 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2944 {
2945         struct obd_device       *obd = exp->exp_obd;
2946         struct lmv_obd          *lmv = &obd->u.lmv;
2947         ENTRY;
2948
2949         if (md->mea)
2950                 obd_free_memmd(exp, (void *)&md->mea);
2951         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2952 }
2953
2954 int lmv_set_open_replay_data(struct obd_export *exp,
2955                              struct obd_client_handle *och,
2956                              struct ptlrpc_request *open_req)
2957 {
2958         struct obd_device       *obd = exp->exp_obd;
2959         struct lmv_obd          *lmv = &obd->u.lmv;
2960         struct lmv_tgt_desc     *tgt;
2961         ENTRY;
2962
2963         tgt = lmv_find_target(lmv, &och->och_fid);
2964         if (IS_ERR(tgt))
2965                 RETURN(PTR_ERR(tgt));
2966
2967         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2968 }
2969
2970 int lmv_clear_open_replay_data(struct obd_export *exp,
2971                                struct obd_client_handle *och)
2972 {
2973         struct obd_device       *obd = exp->exp_obd;
2974         struct lmv_obd          *lmv = &obd->u.lmv;
2975         struct lmv_tgt_desc     *tgt;
2976         ENTRY;
2977
2978         tgt = lmv_find_target(lmv, &och->och_fid);
2979         if (IS_ERR(tgt))
2980                 RETURN(PTR_ERR(tgt));
2981
2982         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2983 }
2984
2985 static int lmv_get_remote_perm(struct obd_export *exp,
2986                                const struct lu_fid *fid,
2987                                struct obd_capa *oc, __u32 suppgid,
2988                                struct ptlrpc_request **request)
2989 {
2990         struct obd_device       *obd = exp->exp_obd;
2991         struct lmv_obd          *lmv = &obd->u.lmv;
2992         struct lmv_tgt_desc     *tgt;
2993         int                      rc;
2994         ENTRY;
2995
2996         rc = lmv_check_connect(obd);
2997         if (rc)
2998                 RETURN(rc);
2999
3000         tgt = lmv_find_target(lmv, fid);
3001         if (IS_ERR(tgt))
3002                 RETURN(PTR_ERR(tgt));
3003
3004         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
3005         RETURN(rc);
3006 }
3007
3008 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3009                           renew_capa_cb_t cb)
3010 {
3011         struct obd_device       *obd = exp->exp_obd;
3012         struct lmv_obd          *lmv = &obd->u.lmv;
3013         struct lmv_tgt_desc     *tgt;
3014         int                      rc;
3015         ENTRY;
3016
3017         rc = lmv_check_connect(obd);
3018         if (rc)
3019                 RETURN(rc);
3020
3021         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3022         if (IS_ERR(tgt))
3023                 RETURN(PTR_ERR(tgt));
3024
3025         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3026         RETURN(rc);
3027 }
3028
3029 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3030                     const struct req_msg_field *field, struct obd_capa **oc)
3031 {
3032         struct obd_device *obd = exp->exp_obd;
3033         struct lmv_obd *lmv = &obd->u.lmv;
3034         int rc;
3035
3036         ENTRY;
3037         rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3038         RETURN(rc);
3039 }
3040
3041 int lmv_intent_getattr_async(struct obd_export *exp,
3042                              struct md_enqueue_info *minfo,
3043                              struct ldlm_enqueue_info *einfo)
3044 {
3045         struct md_op_data       *op_data = &minfo->mi_data;
3046         struct obd_device       *obd = exp->exp_obd;
3047         struct lmv_obd          *lmv = &obd->u.lmv;
3048         struct lmv_object       *obj;
3049         struct lmv_tgt_desc     *tgt = NULL;
3050         int                      rc;
3051         int                      sidx;
3052         ENTRY;
3053
3054         rc = lmv_check_connect(obd);
3055         if (rc)
3056                 RETURN(rc);
3057
3058         if (op_data->op_namelen) {
3059                 obj = lmv_object_find(obd, &op_data->op_fid1);
3060                 if (obj) {
3061                         sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3062                                             (char *)op_data->op_name,
3063                                             op_data->op_namelen);
3064                         op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3065                         tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3066                         lmv_object_put(obj);
3067                 }
3068         }
3069
3070         if (tgt == NULL)
3071                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3072
3073         if (IS_ERR(tgt))
3074                 RETURN(PTR_ERR(tgt));
3075
3076         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3077         RETURN(rc);
3078 }
3079
3080 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3081                         struct lu_fid *fid, __u64 *bits)
3082 {
3083         struct obd_device       *obd = exp->exp_obd;
3084         struct lmv_obd          *lmv = &obd->u.lmv;
3085         struct lmv_tgt_desc     *tgt;
3086         int                      rc;
3087         ENTRY;
3088
3089         rc = lmv_check_connect(obd);
3090         if (rc)
3091                 RETURN(rc);
3092
3093         tgt = lmv_find_target(lmv, fid);
3094         if (IS_ERR(tgt))
3095                 RETURN(PTR_ERR(tgt));
3096
3097         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3098         RETURN(rc);
3099 }
3100
3101 /**
3102  * For lmv, only need to send request to master MDT, and the master MDT will
3103  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3104  * we directly fetch data from the slave MDTs.
3105  */
3106 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3107                  struct obd_quotactl *oqctl)
3108 {
3109         struct obd_device   *obd = class_exp2obd(exp);
3110         struct lmv_obd      *lmv = &obd->u.lmv;
3111         struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3112         int                  rc = 0, i;
3113         __u64                curspace, curinodes;
3114         ENTRY;
3115
3116         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3117                 CERROR("master lmv inactive\n");
3118                 RETURN(-EIO);
3119         }
3120
3121         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3122                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3123                 RETURN(rc);
3124         }
3125
3126         curspace = curinodes = 0;
3127         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3128                 int err;
3129                 tgt = &lmv->tgts[i];
3130
3131                 if (tgt->ltd_exp == NULL)
3132                         continue;
3133                 if (!tgt->ltd_active) {
3134                         CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3135                         continue;
3136                 }
3137
3138                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3139                 if (err) {
3140                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3141                         if (!rc)
3142                                 rc = err;
3143                 } else {
3144                         curspace += oqctl->qc_dqblk.dqb_curspace;
3145                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3146                 }
3147         }
3148         oqctl->qc_dqblk.dqb_curspace = curspace;
3149         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3150
3151         RETURN(rc);
3152 }
3153
3154 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3155                    struct obd_quotactl *oqctl)
3156 {
3157         struct obd_device   *obd = class_exp2obd(exp);
3158         struct lmv_obd      *lmv = &obd->u.lmv;
3159         struct lmv_tgt_desc *tgt;
3160         int                  i, rc = 0;
3161         ENTRY;
3162
3163         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3164                 int err;
3165
3166                 if (!tgt->ltd_active) {
3167                         CERROR("lmv idx %d inactive\n", i);
3168                         RETURN(-EIO);
3169                 }
3170
3171                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3172                 if (err && !rc)
3173                         rc = err;
3174         }
3175
3176         RETURN(rc);
3177 }
3178
3179 struct obd_ops lmv_obd_ops = {
3180         .o_owner                = THIS_MODULE,
3181         .o_setup                = lmv_setup,
3182         .o_cleanup              = lmv_cleanup,
3183         .o_precleanup           = lmv_precleanup,
3184         .o_process_config       = lmv_process_config,
3185         .o_connect              = lmv_connect,
3186         .o_disconnect           = lmv_disconnect,
3187         .o_statfs               = lmv_statfs,
3188         .o_get_info             = lmv_get_info,
3189         .o_set_info_async       = lmv_set_info_async,
3190         .o_packmd               = lmv_packmd,
3191         .o_unpackmd             = lmv_unpackmd,
3192         .o_notify               = lmv_notify,
3193         .o_get_uuid             = lmv_get_uuid,
3194         .o_iocontrol            = lmv_iocontrol,
3195         .o_fid_delete           = lmv_fid_delete,
3196         .o_quotacheck           = lmv_quotacheck,
3197         .o_quotactl             = lmv_quotactl
3198 };
3199
3200 struct md_ops lmv_md_ops = {
3201         .m_getstatus            = lmv_getstatus,
3202         .m_change_cbdata        = lmv_change_cbdata,
3203         .m_find_cbdata          = lmv_find_cbdata,
3204         .m_close                = lmv_close,
3205         .m_create               = lmv_create,
3206         .m_done_writing         = lmv_done_writing,
3207         .m_enqueue              = lmv_enqueue,
3208         .m_getattr              = lmv_getattr,
3209         .m_getxattr             = lmv_getxattr,
3210         .m_getattr_name         = lmv_getattr_name,
3211         .m_intent_lock          = lmv_intent_lock,
3212         .m_link                 = lmv_link,
3213         .m_rename               = lmv_rename,
3214         .m_setattr              = lmv_setattr,
3215         .m_setxattr             = lmv_setxattr,
3216         .m_sync                 = lmv_sync,
3217         .m_readpage             = lmv_readpage,
3218         .m_unlink               = lmv_unlink,
3219         .m_init_ea_size         = lmv_init_ea_size,
3220         .m_cancel_unused        = lmv_cancel_unused,
3221         .m_set_lock_data        = lmv_set_lock_data,
3222         .m_lock_match           = lmv_lock_match,
3223         .m_get_lustre_md        = lmv_get_lustre_md,
3224         .m_free_lustre_md       = lmv_free_lustre_md,
3225         .m_set_open_replay_data = lmv_set_open_replay_data,
3226         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3227         .m_renew_capa           = lmv_renew_capa,
3228         .m_unpack_capa          = lmv_unpack_capa,
3229         .m_get_remote_perm      = lmv_get_remote_perm,
3230         .m_intent_getattr_async = lmv_intent_getattr_async,
3231         .m_revalidate_lock      = lmv_revalidate_lock
3232 };
3233
3234 int __init lmv_init(void)
3235 {
3236         struct lprocfs_static_vars lvars;
3237         int                        rc;
3238
3239         lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3240                       &n