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