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