Whamcloud - gitweb
- mdc_op_data should not be allocated on stack. Fixes about ENTRY/GOTO/RETURN
[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  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LMV
26 #ifdef __KERNEL__
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <asm/div64.h>
33 #include <linux/seq_file.h>
34 #else
35 #include <liblustre.h>
36 #endif
37 #include <linux/ext2_fs.h>
38
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_net.h>
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_mds.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_ost.h>
47 #include <linux/lprocfs_status.h>
48 #include <linux/lustre_fsfilt.h>
49 #include <linux/obd_lmv.h>
50 #include "lmv_internal.h"
51
52 /* object cache. */
53 kmem_cache_t *obj_cache;
54 atomic_t obj_cache_count = ATOMIC_INIT(0);
55
56 static void lmv_activate_target(struct lmv_obd *lmv,
57                                 struct lmv_tgt_desc *tgt,
58                                 int activate)
59 {
60         if (tgt->active == activate)
61                 return;
62         
63         tgt->active = activate;
64         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
65 }
66
67 /* Error codes:
68  *
69  *  -EINVAL  : UUID can't be found in the LMV's target list
70  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
71  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
72  */
73 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
74                               int activate)
75 {
76         struct lmv_tgt_desc *tgt;
77         struct obd_device *obd;
78         int i, rc = 0;
79         ENTRY;
80
81         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
82                lmv, uuid->uuid, activate);
83
84         spin_lock(&lmv->lmv_lock);
85         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
86                 if (tgt->ltd_exp == NULL)
87                         continue;
88
89                 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
90                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
91
92                 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
93                         break;
94         }
95
96         if (i == lmv->desc.ld_tgt_count)
97                 GOTO(out_lmv_lock, rc = -EINVAL);
98
99         obd = class_exp2obd(tgt->ltd_exp);
100         if (obd == NULL)
101                 GOTO(out_lmv_lock, rc = -ENOTCONN);
102
103         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
104                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
105                obd->obd_type->typ_name, i);
106         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
107
108         if (tgt->active == activate) {
109                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
110                        activate ? "" : "in");
111                 GOTO(out_lmv_lock, rc);
112         }
113
114         CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
115                obd, activate ? "" : "in");
116
117         lmv_activate_target(lmv, tgt, activate);
118
119         EXIT;
120         
121  out_lmv_lock:
122         spin_unlock(&lmv->lmv_lock);
123         return rc;
124 }
125
126 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
127                       int active, void *data)
128 {
129         struct obd_uuid *uuid;
130         int rc;
131         ENTRY;
132
133         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
134                 CERROR("unexpected notification of %s %s!\n",
135                        watched->obd_type->typ_name,
136                        watched->obd_name);
137                 RETURN(-EINVAL);
138         }
139         uuid = &watched->u.cli.cl_import->imp_target_uuid;
140
141         /* Set MDC as active before notifying the observer, so the observer can
142          * use the MDC normally.
143          */
144         rc = lmv_set_mdc_active(&obd->u.lmv, uuid, active);
145         if (rc) {
146                 CERROR("%sactivation of %s failed: %d\n",
147                        active ? "" : "de", uuid->uuid, rc);
148                 RETURN(rc);
149         }
150
151         if (obd->obd_observer)
152                 /* Pass the notification up the chain. */
153                 rc = obd_notify(obd->obd_observer, watched, active, data);
154
155         RETURN(rc);
156 }
157
158 int lmv_attach(struct obd_device *dev, obd_count len, void *data)
159 {
160         struct lprocfs_static_vars lvars;
161         int rc;
162         ENTRY;
163
164         lprocfs_init_vars(lmv, &lvars);
165         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
166 #ifdef __KERNEL__
167         if (rc == 0) {
168                 struct proc_dir_entry *entry;
169                 
170                 entry = create_proc_entry("target_obd_status", 0444, 
171                                            dev->obd_proc_entry);
172                 if (entry == NULL)
173                         RETURN(-ENOMEM);
174                 entry->proc_fops = &lmv_proc_target_fops; 
175                 entry->data = dev;
176        }
177 #endif
178         RETURN (rc);
179 }
180
181 int lmv_detach(struct obd_device *dev)
182 {
183         return lprocfs_obd_detach(dev);
184 }
185
186 /* this is fake connect function. Its purpose is to initialize lmv and say
187  * caller that everything is okay. Real connection will be performed later. */
188 static int lmv_connect(struct lustre_handle *conn, struct obd_device *obd,
189                        struct obd_uuid *cluuid, unsigned long flags)
190 {
191 #ifdef __KERNEL__
192         struct proc_dir_entry *lmv_proc_dir;
193 #endif
194         struct lmv_obd *lmv = &obd->u.lmv;
195         struct obd_export *exp;
196         int rc = 0;
197         ENTRY;
198
199         rc = class_connect(conn, obd, cluuid);
200         if (rc) {
201                 CERROR("class_connection() returned %d\n", rc);
202                 RETURN(rc);
203         }
204
205         exp = class_conn2export(conn);
206         
207         /* we don't want to actually do the underlying connections more than
208          * once, so keep track. */
209         lmv->refcount++;
210         if (lmv->refcount > 1) {
211                 class_export_put(exp);
212                 RETURN(0);
213         }
214
215         lmv->exp = exp;
216         lmv->connected = 0;
217         lmv->cluuid = *cluuid;
218         lmv->connect_flags = flags;
219         sema_init(&lmv->init_sem, 1);
220
221 #ifdef __KERNEL__
222         lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
223                                         NULL, NULL);
224         if (IS_ERR(lmv_proc_dir)) {
225                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
226                        obd->obd_type->typ_name, obd->obd_name);
227                 lmv_proc_dir = NULL;
228         }
229 #endif
230
231         /* 
232          * all real clients shouls perform actual connection rightaway, because
233          * it is possible, that LMV will not have opportunity to connect
234          * targets, as MDC stuff will bit called directly, for instance while
235          * reading ../mdc/../kbytesfree procfs file, etc.
236          */
237         if (flags & OBD_OPT_REAL_CLIENT)
238                 rc = lmv_check_connect(obd);
239
240 #ifdef __KERNEL__
241         if (rc) {
242                 if (lmv_proc_dir)
243                         lprocfs_remove(lmv_proc_dir);
244         }
245 #endif
246
247         RETURN(rc);
248 }
249
250 void lmv_set_timeouts(struct obd_device *obd)
251 {
252         struct lmv_tgt_desc *tgts;
253         struct lmv_obd *lmv;
254         int i;
255
256         lmv = &obd->u.lmv;
257         if (lmv->server_timeout == 0)
258                 return;
259
260         if (lmv->connected == 0)
261                 return;
262
263         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
264                 if (tgts->ltd_exp == NULL)
265                         continue;
266                 
267                 obd_set_info(tgts->ltd_exp, strlen("inter_mds"),
268                              "inter_mds", 0, NULL);
269         }
270 }
271
272 #define MAX_STRING_SIZE 128
273
274 /* performs a check if passed obd is connected. If no - connect it. */
275 int lmv_check_connect(struct obd_device *obd)
276 {
277 #ifdef __KERNEL__
278         struct proc_dir_entry *lmv_proc_dir;
279 #endif
280         struct lmv_obd *lmv = &obd->u.lmv;
281         struct lmv_tgt_desc *tgts;
282         struct obd_uuid *cluuid;
283         struct obd_export *exp;
284         int rc, rc2, i;
285         ENTRY;
286
287         if (lmv->connected)
288                 RETURN(0);
289         
290         down(&lmv->init_sem);
291         if (lmv->connected) {
292                 up(&lmv->init_sem);
293                 RETURN(0);
294         }
295
296         cluuid = &lmv->cluuid;
297         exp = lmv->exp;
298         
299         CDEBUG(D_OTHER, "time to connect %s to %s\n",
300                cluuid->uuid, obd->obd_name);
301
302         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
303                 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
304                 struct lustre_handle conn = {0, };
305                 struct obd_device *tgt_obd;
306
307                 LASSERT(tgts != NULL);
308
309                 tgt_obd = class_find_client_obd(&tgts->uuid, LUSTRE_MDC_NAME, 
310                                                 &obd->obd_uuid);
311                 if (!tgt_obd) {
312                         CERROR("target %s not attached\n", tgts->uuid.uuid);
313                         GOTO(out_disc, rc = -EINVAL);
314                 }
315
316                 /* for MDS: don't connect to yourself */
317                 if (obd_uuid_equals(&tgts->uuid, cluuid)) {
318                         CDEBUG(D_OTHER, "don't connect back to %s\n",
319                                cluuid->uuid);
320                         tgts->ltd_exp = NULL;
321                         continue;
322                 }
323
324                 CDEBUG(D_OTHER, "connect to %s(%s) - %s, %s FOR %s\n",
325                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
326                         tgts->uuid.uuid, obd->obd_uuid.uuid,
327                         cluuid->uuid);
328
329                 if (!tgt_obd->obd_set_up) {
330                         CERROR("target %s not set up\n", tgts->uuid.uuid);
331                         GOTO(out_disc, rc = -EINVAL);
332                 }
333                 
334                 rc = obd_connect(&conn, tgt_obd, &lmv_mdc_uuid,
335                                  lmv->connect_flags);
336                 if (rc) {
337                         CERROR("target %s connect error %d\n",
338                                 tgts->uuid.uuid, rc);
339                         GOTO(out_disc, rc);
340                 }
341                 tgts->ltd_exp = class_conn2export(&conn);
342
343                 obd_init_ea_size(tgts->ltd_exp, lmv->max_easize,
344                                  lmv->max_cookiesize);
345
346                 rc = obd_register_observer(tgt_obd, obd);
347                 if (rc) {
348                         CERROR("target %s register_observer error %d\n",
349                                tgts->uuid.uuid, rc);
350                         obd_disconnect(tgts->ltd_exp, 0);
351                         GOTO(out_disc, rc);
352                 }
353
354                 lmv->desc.ld_active_tgt_count++;
355                 tgts->active = 1;
356
357                 CDEBUG(D_OTHER, "connected to %s(%s) successfully (%d)\n",
358                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
359                         atomic_read(&obd->obd_refcount));
360
361 #ifdef __KERNEL__
362                 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
363                 if (lmv_proc_dir) {
364                         struct obd_device *mdc_obd = class_conn2obd(&conn);
365                         struct proc_dir_entry *mdc_symlink;
366                         char name[MAX_STRING_SIZE + 1];
367
368                         LASSERT(mdc_obd != NULL);
369                         LASSERT(mdc_obd->obd_type != NULL);
370                         LASSERT(mdc_obd->obd_type->typ_name != NULL);
371                         name[MAX_STRING_SIZE] = '\0';
372                         snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
373                                  mdc_obd->obd_type->typ_name,
374                                  mdc_obd->obd_name);
375                         mdc_symlink = proc_symlink(mdc_obd->obd_name,
376                                                    lmv_proc_dir, name);
377                         if (mdc_symlink == NULL) {
378                                 CERROR("could not register LMV target "
379                                        "/proc/fs/lustre/%s/%s/target_obds/%s.",
380                                        obd->obd_type->typ_name, obd->obd_name,
381                                        mdc_obd->obd_name);
382                                 lprocfs_remove(lmv_proc_dir);
383                                 lmv_proc_dir = NULL;
384                         }
385                 }
386 #endif
387         }
388
389         lmv_set_timeouts(obd);
390         class_export_put(exp);
391         lmv->connected = 1;
392         up(&lmv->init_sem);
393         RETURN(0);
394
395  out_disc:
396         while (i-- > 0) {
397                 struct obd_uuid uuid;
398                 --tgts;
399                 --lmv->desc.ld_active_tgt_count;
400                 tgts->active = 0;
401                 /* save for CERROR below; (we know it's terminated) */
402                 uuid = tgts->uuid;
403                 rc2 = obd_disconnect(tgts->ltd_exp, 0);
404                 if (rc2)
405                         CERROR("error: LMV target %s disconnect on MDC idx %d: "
406                                "error %d\n", uuid.uuid, i, rc2);
407         }
408         class_disconnect(exp, 0);
409         up(&lmv->init_sem);
410         return rc;
411 }
412
413 static int lmv_disconnect(struct obd_export *exp, unsigned long flags)
414 {
415         struct obd_device *obd = class_exp2obd(exp);
416         struct lmv_obd *lmv = &obd->u.lmv;
417
418 #ifdef __KERNEL__
419         struct proc_dir_entry *lmv_proc_dir;
420 #endif
421         int rc, i;
422         ENTRY;
423
424         if (!lmv->tgts)
425                 goto out_local;
426
427         /* Only disconnect the underlying layers on the final disconnect. */
428         lmv->refcount--;
429         if (lmv->refcount != 0)
430                 goto out_local;
431
432 #ifdef __KERNEL__
433         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
434 #endif
435
436         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
437                 struct obd_device *mdc_obd; 
438                 
439                 if (lmv->tgts[i].ltd_exp == NULL)
440                         continue;
441
442                 mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
443
444 #ifdef __KERNEL__
445                 if (lmv_proc_dir) {
446                         struct proc_dir_entry *mdc_symlink;
447
448                         mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
449                         if (mdc_symlink) {
450                                 lprocfs_remove(mdc_symlink);
451                         } else {
452                                 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
453                                        obd->obd_type->typ_name, obd->obd_name,
454                                        mdc_obd->obd_name);
455                         }
456                 }
457 #endif
458                 if (obd->obd_no_recov) {
459                         if (mdc_obd)
460                                 mdc_obd->obd_no_recov = 1;
461                 }
462                 CDEBUG(D_OTHER, "disconnected from %s(%s) successfully\n",
463                         lmv->tgts[i].ltd_exp->exp_obd->obd_name,
464                         lmv->tgts[i].ltd_exp->exp_obd->obd_uuid.uuid);
465
466                 obd_register_observer(lmv->tgts[i].ltd_exp->exp_obd, NULL);
467                 rc = obd_disconnect(lmv->tgts[i].ltd_exp, flags);
468                 if (rc) {
469                         if (lmv->tgts[i].active) {
470                                 CERROR("Target %s disconnect error %d\n",
471                                        lmv->tgts[i].uuid.uuid, rc);
472                         }
473                         rc = 0;
474                 }
475                 
476                 lmv_activate_target(lmv, &lmv->tgts[i], 0);
477                 lmv->tgts[i].ltd_exp = NULL;
478         }
479
480 #ifdef __KERNEL__
481         if (lmv_proc_dir) {
482                 lprocfs_remove(lmv_proc_dir);
483         } else {
484                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
485                        obd->obd_type->typ_name, obd->obd_name);
486         }
487 #endif
488
489 out_local:
490         /* this is the case when no real connection is established by
491          * lmv_check_connect(). */
492         if (!lmv->connected)
493                 class_export_put(exp);
494         rc = class_disconnect(exp, 0);
495         if (lmv->refcount == 0)
496                 lmv->connected = 0;
497         RETURN(rc);
498 }
499
500 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
501                          int len, void *karg, void *uarg)
502 {
503         struct obd_device *obddev = class_exp2obd(exp);
504         struct lmv_obd *lmv = &obddev->u.lmv;
505         int i, rc = 0, set = 0;
506         ENTRY;
507
508         if (lmv->desc.ld_tgt_count == 0)
509                 RETURN(-ENOTTY);
510         
511         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
512                 int err;
513
514                 if (lmv->tgts[i].ltd_exp == NULL)
515                         continue;
516
517                 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len, karg, uarg);
518                 if (err) {
519                         if (lmv->tgts[i].active) {
520                                 CERROR("error: iocontrol MDC %s on MDT"
521                                        "idx %d: err = %d\n",
522                                        lmv->tgts[i].uuid.uuid, i, err);
523                                 if (!rc)
524                                         rc = err;
525                         }
526                 } else
527                         set = 1;
528         }
529         if (!set && !rc)
530                 rc = -EIO;
531
532         RETURN(rc);
533 }
534
535 static int lmv_setup(struct obd_device *obd, obd_count len, void *buf)
536 {
537         int i, rc = 0;
538         struct lmv_desc *desc;
539         struct obd_uuid *uuids;
540         struct lmv_tgt_desc *tgts;
541         struct obd_device *tgt_obd;
542         struct lustre_cfg *lcfg = buf;
543         struct lmv_obd *lmv = &obd->u.lmv;
544         ENTRY;
545
546         if (lcfg->lcfg_inllen1 < 1) {
547                 CERROR("LMV setup requires a descriptor\n");
548                 RETURN(-EINVAL);
549         }
550
551         if (lcfg->lcfg_inllen2 < 1) {
552                 CERROR("LMV setup requires an MDT UUID list\n");
553                 RETURN(-EINVAL);
554         }
555
556         desc = (struct lmv_desc *)lcfg->lcfg_inlbuf1;
557         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
558                 CERROR("descriptor size wrong: %d > %d\n",
559                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
560                 RETURN(-EINVAL);
561         }
562
563         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
564         if (sizeof(*uuids) * desc->ld_tgt_count != lcfg->lcfg_inllen2) {
565                 CERROR("UUID array size wrong: %u * %u != %u\n",
566                        sizeof(*uuids), desc->ld_tgt_count, lcfg->lcfg_inllen2);
567                 RETURN(-EINVAL);
568         }
569
570         lmv->tgts_size = sizeof(struct lmv_tgt_desc) * desc->ld_tgt_count;
571         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
572         if (lmv->tgts == NULL) {
573                 CERROR("Out of memory\n");
574                 RETURN(-ENOMEM);
575         }
576
577         lmv->desc = *desc;
578         spin_lock_init(&lmv->lmv_lock);
579         
580         for (i = 0, tgts = lmv->tgts; i < desc->ld_tgt_count; i++, tgts++)
581                 tgts->uuid = uuids[i];
582         
583         lmv->max_cookiesize = 0;
584
585         lmv->max_easize = sizeof(struct lustre_id) *
586                 desc->ld_tgt_count + sizeof(struct mea);
587         
588         rc = lmv_setup_mgr(obd);
589         if (rc) {
590                 CERROR("Can't setup LMV object manager, "
591                        "error %d.\n", rc);
592                 OBD_FREE(lmv->tgts, lmv->tgts_size);
593         }
594
595         tgt_obd = class_find_client_obd(&lmv->tgts->uuid, LUSTRE_MDC_NAME,
596                                         &obd->obd_uuid);
597         if (!tgt_obd) {
598                 CERROR("Target %s not attached\n", lmv->tgts->uuid.uuid);
599                 RETURN(-EINVAL);
600         }
601
602         RETURN(rc);
603 }
604
605 static int lmv_cleanup(struct obd_device *obd, int flags) 
606 {
607         struct lmv_obd *lmv = &obd->u.lmv;
608         ENTRY;
609
610         lmv_cleanup_mgr(obd);
611         OBD_FREE(lmv->tgts, lmv->tgts_size);
612         
613         RETURN(0);
614 }
615
616 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
617                       unsigned long max_age)
618 {
619         struct lmv_obd *lmv = &obd->u.lmv;
620         struct obd_statfs *temp;
621         int rc = 0, i;
622         ENTRY;
623         
624         rc = lmv_check_connect(obd);
625         if (rc)
626                 RETURN(rc);
627
628         OBD_ALLOC(temp, sizeof(*temp));
629         if (temp == NULL)
630                 RETURN(-ENOMEM);
631                 
632         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
633                 if (lmv->tgts[i].ltd_exp == NULL) {
634                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
635                         continue;
636                 }
637
638                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp, max_age);
639                 if (rc) {
640                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
641                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
642                                rc);
643                         GOTO(out_free_temp, rc);
644                 }
645                 if (i == 0) {
646                         memcpy(osfs, temp, sizeof(*temp));
647                 } else {
648                         osfs->os_bavail += temp->os_bavail;
649                         osfs->os_blocks += temp->os_blocks;
650                         osfs->os_ffree += temp->os_ffree;
651                         osfs->os_files += temp->os_files;
652                 }
653         }
654
655         EXIT;
656 out_free_temp:
657         OBD_FREE(temp, sizeof(*temp));
658         return rc;
659 }
660
661 static int lmv_getstatus(struct obd_export *exp, struct lustre_id *id)
662 {
663         struct obd_device *obd = exp->exp_obd;
664         struct lmv_obd *lmv = &obd->u.lmv;
665         int rc;
666         ENTRY;
667
668         rc = lmv_check_connect(obd);
669         if (rc)
670                 RETURN(rc);
671
672         rc = md_getstatus(lmv->tgts[0].ltd_exp, id);
673         id_group(id) = 0;
674         
675         RETURN(rc);
676 }
677
678 static int lmv_getattr(struct obd_export *exp, struct lustre_id *id,
679                        __u64 valid, unsigned int ea_size,
680                        struct ptlrpc_request **request)
681 {
682         struct obd_device *obd = exp->exp_obd;
683         struct lmv_obd *lmv = &obd->u.lmv;
684         int rc, i = id_group(id);
685         struct lmv_obj *obj;
686         ENTRY;
687
688         rc = lmv_check_connect(obd);
689         if (rc)
690                 RETURN(rc);
691
692         LASSERT(i < lmv->desc.ld_tgt_count);
693
694         rc = md_getattr(lmv->tgts[i].ltd_exp, id, valid,
695                         ea_size, request);
696         if (rc)
697                 RETURN(rc);
698         
699         obj = lmv_grab_obj(obd, id);
700         
701         CDEBUG(D_OTHER, "GETATTR for "DLID4" %s\n",
702                OLID4(id), obj ? "(splitted)" : "");
703
704         /*
705          * if object is splitted, then we loop over all the slaves and gather
706          * size attribute. In ideal world we would have to gather also mds field
707          * from all slaves, as object is spread over the cluster and this is
708          * definitely interesting information and it is not good to loss it,
709          * but...
710          */
711         if (obj) {
712                 struct mds_body *body;
713
714                 if (*request == NULL) {
715                         lmv_put_obj(obj);
716                         RETURN(rc);
717                 }
718                         
719                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
720                                       sizeof(*body));
721                 LASSERT(body != NULL);
722
723                 lmv_lock_obj(obj);
724         
725                 for (i = 0; i < obj->objcount; i++) {
726
727                         if (lmv->tgts[i].ltd_exp == NULL) {
728                                 CWARN("%s: NULL export for %d\n",
729                                       obd->obd_name, i);
730                                 continue;
731                         }
732
733                         /* skip master obj. */
734                         if (id_equal_fid(&obj->id, &obj->objs[i].id))
735                                 continue;
736                         
737                         body->size += obj->objs[i].size;
738                 }
739
740                 lmv_unlock_obj(obj);
741                 lmv_put_obj(obj);
742         }
743         
744         RETURN(rc);
745 }
746
747 static int lmv_change_cbdata(struct obd_export *exp,
748                              struct lustre_id *id, 
749                              ldlm_iterator_t it,
750                              void *data)
751 {
752         struct obd_device *obd = exp->exp_obd;
753         struct lmv_obd *lmv = &obd->u.lmv;
754         int rc = 0;
755         ENTRY;
756         
757         rc = lmv_check_connect(obd);
758         if (rc)
759                 RETURN(rc);
760         
761         CDEBUG(D_OTHER, "CBDATA for "DLID4"\n", OLID4(id));
762         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
763
764         rc = md_change_cbdata(lmv->tgts[id_group(id)].ltd_exp,
765                               id, it, data);
766         
767         RETURN(rc);
768 }
769
770 static int lmv_change_cbdata_name(struct obd_export *exp,
771                                   struct lustre_id *pid,
772                                   char *name, int len,
773                                   struct lustre_id *cid,
774                                   ldlm_iterator_t it,
775                                   void *data)
776 {
777         struct obd_device *obd = exp->exp_obd;
778         struct lmv_obd *lmv = &obd->u.lmv;
779         struct lustre_id rcid = *cid;
780         struct lmv_obj *obj;
781         int rc = 0, mds;
782         ENTRY;
783
784         rc = lmv_check_connect(obd);
785         if (rc)
786                 RETURN(rc);
787
788         LASSERT(id_group(pid) < lmv->desc.ld_tgt_count);
789         LASSERT(id_group(cid) < lmv->desc.ld_tgt_count);
790         
791         CDEBUG(D_OTHER, "CBDATA for "DLID4":%*s -> "DLID4"\n",
792                OLID4(pid), len, name, OLID4(cid));
793
794         /* this is default mds for directory name belongs to. */
795         mds = id_group(pid);
796         obj = lmv_grab_obj(obd, pid);
797         if (obj) {
798                 /* directory is splitted. look for right mds for this name. */
799                 mds = raw_name2idx(obj->hashtype, obj->objcount, name, len);
800                 rcid = obj->objs[mds].id;
801                 mds = id_group(&rcid);
802                 lmv_put_obj(obj);
803         }
804         rc = md_change_cbdata(lmv->tgts[mds].ltd_exp, &rcid, it, data);
805         RETURN(rc);
806 }
807
808 static int lmv_valid_attrs(struct obd_export *exp, struct lustre_id *id) 
809 {
810         struct obd_device *obd = exp->exp_obd;
811         struct lmv_obd *lmv = &obd->u.lmv;
812         int rc = 0;
813         ENTRY;
814
815         rc = lmv_check_connect(obd);
816         if (rc)
817                 RETURN(rc);
818
819         CDEBUG(D_OTHER, "validate "DLID4"\n", OLID4(id));
820         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
821         rc = md_valid_attrs(lmv->tgts[id_group(id)].ltd_exp, id);
822         RETURN(rc);
823 }
824
825 int lmv_close(struct obd_export *exp, struct obdo *obdo,
826               struct obd_client_handle *och,
827               struct ptlrpc_request **request)
828 {
829         struct obd_device *obd = exp->exp_obd;
830         struct lmv_obd *lmv = &obd->u.lmv;
831         int rc, i = obdo->o_mds;
832         ENTRY;
833         
834         rc = lmv_check_connect(obd);
835         if (rc)
836                 RETURN(rc);
837
838         LASSERT(i < lmv->desc.ld_tgt_count);
839         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long)obdo->o_mds,
840                (unsigned long)obdo->o_id, (unsigned long)obdo->o_generation);
841         rc = md_close(lmv->tgts[i].ltd_exp, obdo, och, request);
842         RETURN(rc);
843 }
844
845 int lmv_get_mea_and_update_object(struct obd_export *exp, 
846                                   struct lustre_id *id)
847 {
848         struct obd_device *obd = exp->exp_obd;
849         struct lmv_obd *lmv = &obd->u.lmv;
850         struct ptlrpc_request *req = NULL;
851         struct lmv_obj *obj;
852         struct lustre_md md;
853         int mealen, rc;
854         __u64 valid;
855         ENTRY;
856
857         md.mea = NULL;
858         mealen = MEA_SIZE_LMV(lmv);
859         
860         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
861
862         /* time to update mea of parent id */
863         rc = md_getattr(lmv->tgts[id_group(id)].ltd_exp,
864                         id, valid, mealen, &req);
865         if (rc) {
866                 CERROR("md_getattr() failed, error %d\n", rc);
867                 GOTO(cleanup, rc);
868         }
869
870         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
871         if (rc) {
872                 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
873                 GOTO(cleanup, rc);
874         }
875
876         if (md.mea == NULL)
877                 GOTO(cleanup, rc = -ENODATA);
878
879         obj = lmv_create_obj(exp, id, md.mea);
880         if (IS_ERR(obj))
881                 rc = PTR_ERR(obj);
882         
883         lmv_put_obj(obj);
884         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
885
886         EXIT;
887 cleanup:
888         if (req)
889                 ptlrpc_req_finished(req);
890         return rc;
891 }
892
893 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
894                const void *data, int datalen, int mode, __u32 uid,
895                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
896 {
897         struct obd_device *obd = exp->exp_obd;
898         struct lmv_obd *lmv = &obd->u.lmv;
899         struct mds_body *body;
900         struct lmv_obj *obj;
901         int rc, mds, loop = 0;
902         ENTRY;
903
904         rc = lmv_check_connect(obd);
905         if (rc)
906                 RETURN(rc);
907
908         if (!lmv->desc.ld_active_tgt_count)
909                 RETURN(-EIO);
910 repeat:
911         LASSERT(++loop <= 2);
912         obj = lmv_grab_obj(obd, &op_data->id1);
913         if (obj) {
914                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
915                                    op_data->name, op_data->namelen);
916                 op_data->id1 = obj->objs[mds].id;
917                 lmv_put_obj(obj);
918         }
919
920         CDEBUG(D_OTHER, "CREATE '%*s' on "DLID4"\n", op_data->namelen,
921                op_data->name, OLID4(&op_data->id1));
922         
923         rc = md_create(lmv->tgts[id_group(&op_data->id1)].ltd_exp, 
924                        op_data, data, datalen, mode, uid, gid, rdev,
925                        request);
926         if (rc == 0) {
927                 if (*request == NULL)
928                         RETURN(rc);
929
930                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
931                                       sizeof(*body));
932                 if (body == NULL)
933                         RETURN(-ENOMEM);
934                 
935                 CDEBUG(D_OTHER, "created. "DLID4"\n", OLID4(&op_data->id1));
936         } else if (rc == -ERESTART) {
937                 /*
938                  * directory got splitted. time to update local object and
939                  * repeat the request with proper MDS.
940                  */
941                 rc = lmv_get_mea_and_update_object(exp, &op_data->id1);
942                 if (rc == 0) {
943                         ptlrpc_req_finished(*request);
944                         goto repeat;
945                 }
946         }
947         RETURN(rc);
948 }
949
950 int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
951 {
952         struct obd_device *obd = exp->exp_obd;
953         struct lmv_obd *lmv = &obd->u.lmv;
954         int rc;
955         ENTRY;
956         
957         rc = lmv_check_connect(obd);
958         if (rc)
959                 RETURN(rc);
960
961         /* FIXME: choose right MDC here */
962         CWARN("this method isn't implemented yet\n");
963         rc = md_done_writing(lmv->tgts[0].ltd_exp, obdo);
964         RETURN(rc);
965 }
966
967 int lmv_enqueue_slaves(struct obd_export *exp, int locktype,
968                        struct lookup_intent *it, int lockmode,
969                        struct mdc_op_data *data, struct lustre_handle *lockh,
970                        void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
971                        ldlm_blocking_callback cb_blocking, void *cb_data)
972 {
973         struct obd_device *obd = exp->exp_obd;
974         struct lmv_obd *lmv = &obd->u.lmv;
975         struct mea *mea = data->mea1;
976         struct mdc_op_data *data2;
977         int i, rc, mds;
978         ENTRY;
979
980         OBD_ALLOC(data2, sizeof(*data2));
981         if (data2 == NULL)
982                 RETURN(-ENOMEM);
983         
984         LASSERT(mea != NULL);
985         for (i = 0; i < mea->mea_count; i++) {
986                 memset(data2, 0, sizeof(*data2));
987                 data2->id1 = mea->mea_ids[i];
988                 mds = id_group(&data2->id1);
989                 
990                 if (lmv->tgts[mds].ltd_exp == NULL)
991                         continue;
992
993                 rc = md_enqueue(lmv->tgts[mds].ltd_exp, locktype, it, 
994                                 lockmode, data2, lockh + i, lmm, lmmsize, 
995                                 cb_compl, cb_blocking, cb_data);
996                 
997                 CDEBUG(D_OTHER, "take lock on slave "DLID4" -> %d/%d\n",
998                        OLID4(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
999                 if (rc)
1000                         GOTO(cleanup, rc);
1001                 if (it->d.lustre.it_data) {
1002                         struct ptlrpc_request *req;
1003                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1004                         ptlrpc_req_finished(req);
1005                 }
1006                 
1007                 if (it->d.lustre.it_status)
1008                         GOTO(cleanup, rc = it->d.lustre.it_status);
1009         }
1010         
1011         OBD_FREE(data2, sizeof(*data2));
1012         RETURN(0);
1013 cleanup:
1014         OBD_FREE(data2, sizeof(*data2));
1015         
1016         /* drop all taken locks */
1017         while (--i >= 0) {
1018                 if (lockh[i].cookie)
1019                         ldlm_lock_decref(lockh + i, lockmode);
1020                 lockh[i].cookie = 0;
1021         }
1022         return rc;
1023 }
1024
1025 int lmv_enqueue(struct obd_export *exp, int lock_type,
1026                 struct lookup_intent *it, int lock_mode,
1027                 struct mdc_op_data *data, struct lustre_handle *lockh,
1028                 void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1029                 ldlm_blocking_callback cb_blocking, void *cb_data)
1030 {
1031         struct obd_device *obd = exp->exp_obd;
1032         struct lmv_obd *lmv = &obd->u.lmv;
1033         struct lmv_obj *obj;
1034         int rc, mds;
1035         ENTRY;
1036
1037         rc = lmv_check_connect(obd);
1038         if (rc)
1039                 RETURN(rc);
1040
1041         if (data->mea1 && it->it_op == IT_UNLINK) {
1042                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
1043                                         data, lockh, lmm, lmmsize,
1044                                         cb_compl, cb_blocking, cb_data);
1045                 RETURN(rc);
1046         }
1047
1048         if (data->namelen) {
1049                 obj = lmv_grab_obj(obd, &data->id1);
1050                 if (obj) {
1051                         /* directory is splitted. look for right mds for this
1052                          * name */
1053                         mds = raw_name2idx(obj->hashtype, obj->objcount,
1054                                            (char *)data->name, data->namelen);
1055                         data->id1 = obj->objs[mds].id;
1056                         lmv_put_obj(obj);
1057                 }
1058         }
1059         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DLID4"\n", LL_IT2STR(it),
1060                OLID4(&data->id1));
1061         
1062         rc = md_enqueue(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1063                         lock_type, it, lock_mode, data, lockh, lmm, 
1064                         lmmsize, cb_compl, cb_blocking, cb_data);
1065         RETURN(rc);
1066 }
1067
1068 int lmv_getattr_lock(struct obd_export *exp, struct lustre_id *id,
1069                      char *filename, int namelen, __u64 valid,
1070                      unsigned int ea_size, struct ptlrpc_request **request)
1071 {
1072         int rc, mds = id_group(id), loop = 0;
1073         struct obd_device *obd = exp->exp_obd;
1074         struct lmv_obd *lmv = &obd->u.lmv;
1075         struct lustre_id rid = *id;
1076         struct mds_body *body;
1077         struct lmv_obj *obj;
1078         __u64 old_valid;
1079         ENTRY;
1080         
1081         rc = lmv_check_connect(obd);
1082         if (rc)
1083                 RETURN(rc);
1084 repeat:
1085         LASSERT(++loop <= 2);
1086         obj = lmv_grab_obj(obd, id);
1087         if (obj) {
1088                 /* directory is splitted. look for right mds for this name */
1089                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1090                                    filename, namelen - 1);
1091                 rid = obj->objs[mds].id;
1092                 lmv_put_obj(obj);
1093         }
1094         
1095         CDEBUG(D_OTHER, "getattr_lock for %*s on "DLID4" -> "DLID4"\n",
1096                namelen, filename, OLID4(id), OLID4(&rid));
1097
1098         old_valid = valid;
1099
1100         /*
1101          * here should be applied OBD_MD_FID to ->valid, because otherwise,
1102          * mds_getattr_lock() will not fetch fid component of lustre_id and
1103          * thus, next call to md_getattr_lock() will be performed to wrong mds.
1104          */
1105         if (!(old_valid & OBD_MD_FID))
1106                 valid |= OBD_MD_FID;
1107         
1108         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp, 
1109                              &rid, filename, namelen, valid,
1110                              ea_size, request);
1111         if (rc == 0) {
1112                 /*
1113                  * this could be cross-node reference. in this case all we have
1114                  * right now is lustre_id triple. we'd like to find other
1115                  * attributes.
1116                  */
1117                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
1118                 LASSERT(body != NULL);
1119                 if (body->valid & OBD_MD_MDS) {
1120                         struct ptlrpc_request *req = NULL;
1121                         
1122                         rid = body->id1;
1123                         CDEBUG(D_OTHER, "request attrs for "DLID4"\n", OLID4(&rid));
1124
1125                         /* 
1126                          * turning OBD_MD_FID fetching off, as we already have
1127                          * full lustre_id and do need to fetch fid component
1128                          * again. This will help to make thing slightly faster.
1129                          */
1130                         if (!(old_valid & OBD_MD_FID))
1131                                 valid &= ~OBD_MD_FID;
1132                         
1133                         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp, 
1134                                              &rid, NULL, 1, valid, ea_size, &req);
1135                         ptlrpc_req_finished(*request);
1136                         *request = req;
1137                 }
1138         } else if (rc == -ERESTART) {
1139                 /* directory got splitted. time to update local object and
1140                  * repeat the request with proper MDS */
1141                 rc = lmv_get_mea_and_update_object(exp, &rid);
1142                 if (rc == 0) {
1143                         ptlrpc_req_finished(*request);
1144                         goto repeat;
1145                 }
1146         }
1147         RETURN(rc);
1148 }
1149
1150 /*
1151  * llite passes id of an target inode in data->id1 and id of directory in
1152  * data->id2
1153  */
1154 int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
1155              struct ptlrpc_request **request)
1156 {
1157         struct obd_device *obd = exp->exp_obd;
1158         struct lmv_obd *lmv = &obd->u.lmv;
1159         struct lmv_obj *obj;
1160         int rc;
1161         ENTRY;
1162         
1163         rc = lmv_check_connect(obd);
1164         if (rc)
1165                 RETURN(rc);
1166
1167         if (data->namelen != 0) {
1168                 /* usual link request */
1169                 obj = lmv_grab_obj(obd, &data->id1);
1170                 if (obj) {
1171                         rc = raw_name2idx(obj->hashtype, obj->objcount, 
1172                                           data->name, data->namelen);
1173                         data->id1 = obj->objs[rc].id;
1174                         lmv_put_obj(obj);
1175                 }
1176                 
1177                 CDEBUG(D_OTHER,"link "DLID4":%*s to "DLID4"\n",
1178                        OLID4(&data->id2), data->namelen, data->name,
1179                        OLID4(&data->id1));
1180         } else {
1181                 /* request from MDS to acquire i_links for inode by id1 */
1182                 CDEBUG(D_OTHER, "inc i_nlinks for "DLID4"\n",
1183                        OLID4(&data->id1));
1184         }
1185                         
1186         rc = md_link(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1187                      data, request);
1188         RETURN(rc);
1189 }
1190
1191 int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
1192                const char *old, int oldlen, const char *new, int newlen,
1193                struct ptlrpc_request **request)
1194 {
1195         struct obd_device *obd = exp->exp_obd;
1196         struct lmv_obd *lmv = &obd->u.lmv;
1197         struct lmv_obj *obj;
1198         int rc, mds;
1199         ENTRY;
1200
1201         CDEBUG(D_OTHER, "rename %*s in "DLID4" to %*s in "DLID4"\n",
1202                oldlen, old, OLID4(&data->id1), newlen, new,
1203                OLID4(&data->id2));
1204
1205         rc = lmv_check_connect(obd);
1206         if (rc)
1207                 RETURN(rc);
1208
1209         if (oldlen == 0) {
1210                 /*
1211                  * MDS with old dir entry is asking another MDS to create name
1212                  * there.
1213                  */
1214                 CDEBUG(D_OTHER,
1215                        "create %*s(%d/%d) in "DLID4" pointing "
1216                        "to "DLID4"\n", newlen, new, oldlen, newlen,
1217                        OLID4(&data->id2), OLID4(&data->id1));
1218
1219                 mds = id_group(&data->id2);
1220
1221                 /* 
1222                  * target directory can be splitted, sowe should forward request
1223                  * to the right MDS.
1224                  */
1225                 obj = lmv_grab_obj(obd, &data->id2);
1226                 if (obj) {
1227                         mds = raw_name2idx(obj->hashtype, obj->objcount, 
1228                                            (char *)new, newlen);
1229                         data->id2 = obj->objs[mds].id;
1230                         CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1231                                OLID4(&data->id2));
1232                         lmv_put_obj(obj);
1233                 }
1234                 goto request;
1235         }
1236
1237         obj = lmv_grab_obj(obd, &data->id1);
1238         if (obj) {
1239                 /*
1240                  * directory is already splitted, so we have to forward request
1241                  * to the right MDS.
1242                  */
1243                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1244                                    (char *)old, oldlen);
1245                 data->id1 = obj->objs[mds].id;
1246                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1247                        OLID4(&data->id1));
1248                 lmv_put_obj(obj);
1249         }
1250
1251         obj = lmv_grab_obj(obd, &data->id2);
1252         if (obj) {
1253                 /*
1254                  * directory is already splitted, so we have to forward request
1255                  * to the right MDS.
1256                  */
1257                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1258                                    (char *)new, newlen);
1259                 
1260                 data->id2 = obj->objs[mds].id;
1261                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1262                        OLID4(&data->id2));
1263                 lmv_put_obj(obj);
1264         }
1265         
1266         mds = id_group(&data->id1);
1267
1268 request:
1269         if (id_group(&data->id1) != id_group(&data->id2)) {
1270                 CDEBUG(D_OTHER,"cross-node rename "DLID4"/%*s to "DLID4"/%*s\n",
1271                        OLID4(&data->id1), oldlen, old, OLID4(&data->id2),
1272                        newlen, new);
1273         }
1274
1275         rc = md_rename(lmv->tgts[mds].ltd_exp, data, old, oldlen,
1276                        new, newlen, request); 
1277         RETURN(rc);
1278 }
1279
1280 int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
1281                 struct iattr *iattr, void *ea, int ealen, void *ea2,
1282                 int ea2len, struct ptlrpc_request **request)
1283 {
1284         struct obd_device *obd = exp->exp_obd;
1285         struct lmv_obd *lmv = &obd->u.lmv;
1286         struct ptlrpc_request *req;
1287         struct mds_body *body;
1288         struct lmv_obj *obj;
1289         int rc = 0, i;
1290         ENTRY;
1291
1292         rc = lmv_check_connect(obd);
1293         if (rc)
1294                 RETURN(rc);
1295
1296         obj = lmv_grab_obj(obd, &data->id1);
1297         
1298         CDEBUG(D_OTHER, "SETATTR for "DLID4", valid 0x%x%s\n",
1299                OLID4(&data->id1), iattr->ia_valid, obj ? ", splitted" : "");
1300         
1301         if (obj) {
1302                 for (i = 0; i < obj->objcount; i++) {
1303                         data->id1 = obj->objs[i].id;
1304                         
1305                         rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1306                                         data, iattr, ea, ealen, ea2, ea2len, &req);
1307
1308                         if (id_equal_fid(&obj->id, &obj->objs[i].id)) {
1309                                 /*
1310                                  * this is master object and this request should
1311                                  * be returned back to llite.
1312                                  */
1313                                 *request = req;
1314                         } else {
1315                                 ptlrpc_req_finished(req);
1316                         }
1317
1318                         if (rc)
1319                                 break;
1320                 }
1321                 lmv_put_obj(obj);
1322         } else {
1323                 LASSERT(id_group(&data->id1) < lmv->desc.ld_tgt_count);
1324                 rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp,
1325                                 data, iattr, ea, ealen, ea2, ea2len, request); 
1326                 if (rc == 0) {
1327                         body = lustre_msg_buf((*request)->rq_repmsg, 0,
1328                                               sizeof(*body));
1329                         LASSERT(body != NULL);
1330                         LASSERT(id_group(&body->id1) == id_group(&data->id1));
1331                 }
1332         }
1333         RETURN(rc);
1334 }
1335
1336 int lmv_sync(struct obd_export *exp, struct lustre_id *id,
1337              struct ptlrpc_request **request)
1338 {
1339         struct obd_device *obd = exp->exp_obd;
1340         struct lmv_obd *lmv = &obd->u.lmv;
1341         int rc;
1342         ENTRY;
1343
1344         rc = lmv_check_connect(obd);
1345         if (rc)
1346                 RETURN(rc);
1347
1348         rc = md_sync(lmv->tgts[id_group(id)].ltd_exp, 
1349                      id, request);
1350         RETURN(rc);
1351 }
1352
1353 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock, 
1354                             struct ldlm_lock_desc *desc,
1355                             void *data, int flag)
1356 {
1357         struct lustre_handle lockh;
1358         struct lmv_obj *obj;
1359         int rc;
1360         ENTRY;
1361
1362         switch (flag) {
1363         case LDLM_CB_BLOCKING:
1364                 ldlm_lock2handle(lock, &lockh);
1365                 rc = ldlm_cli_cancel(&lockh);
1366                 if (rc < 0) {
1367                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1368                         RETURN(rc);
1369                 }
1370                 break;
1371         case LDLM_CB_CANCELING:
1372                 /* time to drop cached attrs for dirobj */
1373                 obj = lock->l_ast_data;
1374                 if (obj) {
1375                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
1376                                ", master "DLID4"\n",
1377                                lock->l_resource->lr_name.name[3] == 1 ?
1378                                "LOOKUP" : "UPDATE",
1379                                lock->l_resource->lr_name.name[0],
1380                                lock->l_resource->lr_name.name[1], 
1381                                OLID4(&obj->id));
1382                         lmv_put_obj(obj);
1383                 }
1384                 break;
1385         default:
1386                 LBUG();
1387         }
1388         RETURN(0);
1389 }
1390
1391 void lmv_remove_dots(struct page *page)
1392 {
1393         unsigned limit = PAGE_CACHE_SIZE;
1394         char *kaddr = page_address(page);
1395         struct ext2_dir_entry_2 *p;
1396         unsigned offs, rec_len;
1397
1398         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
1399                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
1400                 rec_len = le16_to_cpu(p->rec_len);
1401
1402                 if ((p->name_len == 1 && p->name[0] == '.') ||
1403                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
1404                         p->inode = 0;
1405         }
1406 }
1407
1408 int lmv_readpage(struct obd_export *exp, struct lustre_id *id,
1409                  __u64 offset, struct page *page,
1410                  struct ptlrpc_request **request)
1411 {
1412         struct obd_device *obd = exp->exp_obd;
1413         struct lmv_obd *lmv = &obd->u.lmv;
1414         struct lustre_id rid = *id;
1415         struct lmv_obj *obj;
1416         int rc, i;
1417         ENTRY;
1418
1419 #warning "we need well-desgined readdir() implementation"
1420         rc = lmv_check_connect(obd);
1421         if (rc)
1422                 RETURN(rc);
1423
1424         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
1425         CDEBUG(D_OTHER, "READPAGE at %llu from "DLID4"\n",
1426                offset, OLID4(&rid));
1427
1428         obj = lmv_grab_obj(obd, id);
1429         if (obj) {
1430                 lmv_lock_obj(obj);
1431
1432                 /* find dirobj containing page with requested offset. */
1433                 for (i = 0; i < obj->objcount; i++) {
1434                         if (offset < obj->objs[i].size)
1435                                 break;
1436                         offset -= obj->objs[i].size;
1437                 }
1438                 rid = obj->objs[i].id;
1439                 
1440                 lmv_unlock_obj(obj);
1441                 lmv_put_obj(obj);
1442                 
1443                 CDEBUG(D_OTHER, "forward to "DLID4" with offset %lu\n",
1444                        OLID4(&rid), (unsigned long)offset);
1445         }
1446         rc = md_readpage(lmv->tgts[id_group(&rid)].ltd_exp, &rid, 
1447                          offset, page, request);
1448         
1449         if (rc == 0 && !id_equal_fid(&rid, id))
1450                 /* this page isn't from master object. To avoid "." and ".." 
1451                  * duplication in directory, we have to remove them from all
1452                  * slave objects */
1453                 lmv_remove_dots(page);
1454         
1455         RETURN(rc);
1456 }
1457
1458 int lmv_unlink_slaves(struct obd_export *exp, struct mdc_op_data *data,
1459                       struct ptlrpc_request **req)
1460 {
1461         struct obd_device *obd = exp->exp_obd;
1462         struct lmv_obd *lmv = &obd->u.lmv;
1463         struct mea *mea = data->mea1;
1464         struct mdc_op_data *data2;
1465         int i, rc = 0;
1466         ENTRY;
1467
1468         OBD_ALLOC(data2, sizeof(*data2));
1469         if (data2 == NULL)
1470                 RETURN(-ENOMEM);
1471         
1472         LASSERT(mea != NULL);
1473         for (i = 0; i < mea->mea_count; i++) {
1474                 memset(data2, 0, sizeof(*data2));
1475                 data2->id1 = mea->mea_ids[i];
1476                 data2->create_mode = MDS_MODE_DONT_LOCK | S_IFDIR;
1477                 
1478                 if (lmv->tgts[id_group(&data2->id1)].ltd_exp == NULL)
1479                         continue;
1480
1481                 rc = md_unlink(lmv->tgts[id_group(&data2->id1)].ltd_exp,
1482                                data2, req);
1483                 
1484                 CDEBUG(D_OTHER, "unlink slave "DLID4" -> %d\n",
1485                        OLID4(&mea->mea_ids[i]), rc);
1486                 
1487                 if (*req) {
1488                         ptlrpc_req_finished(*req);
1489                         *req = NULL;
1490                 }
1491                 if (rc)
1492                         RETURN(rc);
1493         }
1494         OBD_FREE(data2, sizeof(*data2));
1495         RETURN(rc);
1496 }
1497
1498 int lmv_delete_inode(struct obd_export *exp, struct lustre_id *id)
1499 {
1500         ENTRY;
1501
1502         LASSERT(exp && id);
1503         if (lmv_delete_obj(exp, id)) {
1504                 CDEBUG(D_OTHER, "lmv object "DLID4" is destroyed.\n",
1505                        OLID4(id));
1506         }
1507         RETURN(0);
1508 }
1509
1510 int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
1511                struct ptlrpc_request **request)
1512 {
1513         struct obd_device *obd = exp->exp_obd;
1514         struct lmv_obd *lmv = &obd->u.lmv;
1515         int rc, i = 0;
1516         ENTRY;
1517         
1518         rc = lmv_check_connect(obd);
1519         if (rc)
1520                 RETURN(rc);
1521
1522         if (data->namelen == 0 && data->mea1 != NULL) {
1523                 /* mds asks to remove slave objects */
1524                 rc = lmv_unlink_slaves(exp, data, request);
1525                 RETURN(rc);
1526         }
1527
1528         if (data->namelen != 0) {
1529                 struct lmv_obj *obj;
1530                 
1531                 obj = lmv_grab_obj(obd, &data->id1);
1532                 if (obj) {
1533                         i = raw_name2idx(obj->hashtype, obj->objcount,
1534                                          data->name, data->namelen);
1535                         data->id1 = obj->objs[i].id;
1536                         lmv_put_obj(obj);
1537                 }
1538                 CDEBUG(D_OTHER, "unlink '%*s' in "DLID4" -> %u\n",
1539                        data->namelen, data->name, OLID4(&data->id1),
1540                        i);
1541         } else {
1542                 CDEBUG(D_OTHER, "drop i_nlink on "DLID4"\n",
1543                        OLID4(&data->id1));
1544         }
1545         rc = md_unlink(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1546                        data, request);
1547         RETURN(rc);
1548 }
1549
1550 struct obd_device *lmv_get_real_obd(struct obd_export *exp,
1551                                     char *name, int len)
1552 {
1553         struct obd_device *obd = exp->exp_obd;
1554         struct lmv_obd *lmv = &obd->u.lmv;
1555         int rc;
1556         ENTRY;
1557
1558         rc = lmv_check_connect(obd);
1559         if (rc)
1560                 RETURN(ERR_PTR(rc));
1561         obd = lmv->tgts[0].ltd_exp->exp_obd;
1562         EXIT;
1563         
1564         return obd;
1565 }
1566
1567 int lmv_init_ea_size(struct obd_export *exp, int easize,
1568                      int cookiesize)
1569 {
1570         struct obd_device *obd = exp->exp_obd;
1571         struct lmv_obd *lmv = &obd->u.lmv;
1572         int i, rc = 0, change = 0;
1573         ENTRY;
1574
1575         if (lmv->max_easize < easize) {
1576                 lmv->max_easize = easize;
1577                 change = 1;
1578         }
1579         if (lmv->max_cookiesize < cookiesize) {
1580                 lmv->max_cookiesize = cookiesize;
1581                 change = 1;
1582         }
1583         if (change == 0)
1584                 RETURN(0);
1585         
1586         if (lmv->connected == 0)
1587                 RETURN(0);
1588
1589         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1590                 if (lmv->tgts[i].ltd_exp == NULL) {
1591                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
1592                         continue;
1593                 }
1594
1595                 rc = obd_init_ea_size(lmv->tgts[i].ltd_exp, easize, cookiesize);
1596                 if (rc) {
1597                         CERROR("obd_init_ea_size() failed on MDT target %d, "
1598                                "error %d.\n", i, rc);
1599                         break;
1600                 }
1601         }
1602         RETURN(rc);
1603 }
1604
1605 int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1606                           struct lov_stripe_md **ea, struct obd_trans_info *oti)
1607 {
1608         struct obd_device *obd = exp->exp_obd;
1609         struct lmv_obd *lmv = &obd->u.lmv;
1610         struct lov_stripe_md obj_md;
1611         struct lov_stripe_md *obj_mdp = &obj_md;
1612         int rc = 0;
1613         ENTRY;
1614
1615         LASSERT(ea == NULL);
1616         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1617
1618         rc = obd_create(lmv->tgts[oa->o_mds].ltd_exp,
1619                         oa, &obj_mdp, oti);
1620
1621         RETURN(rc);
1622 }
1623
1624 int lmv_getready(struct obd_export *exp)
1625 {
1626         struct obd_device *obd = exp->exp_obd;
1627         int rc = 0;
1628         
1629         ENTRY;
1630         rc = lmv_check_connect(obd);
1631         RETURN(rc);
1632 }
1633
1634 /*
1635  * to be called from MDS only. @oa should have correct store cookie and o_fid
1636  * values for "master" object, as it will be used.
1637  */
1638 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1639                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1640 {
1641         struct obd_device *obd = exp->exp_obd;
1642         struct lmv_obd *lmv = &obd->u.lmv;
1643         struct lustre_id mid;
1644         int i, c, rc = 0;
1645         struct mea *mea;
1646         ENTRY;
1647
1648         rc = lmv_check_connect(obd);
1649         if (rc)
1650                 RETURN(rc);
1651
1652         LASSERT(oa != NULL);
1653         
1654         if (ea == NULL) {
1655                 rc = lmv_obd_create_single(exp, oa, NULL, oti);
1656                 if (rc)
1657                         CERROR("Can't create object, rc = %d\n", rc);
1658                 RETURN(rc);
1659         }
1660
1661         if (*ea == NULL) {
1662                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1663                 if (rc < 0) {
1664                         CERROR("obd_alloc_diskmd() failed, error %d\n",
1665                                rc);
1666                         RETURN(rc);
1667                 } else
1668                         rc = 0;
1669                 
1670                 if (*ea == NULL)
1671                         RETURN(-ENOMEM);
1672         }
1673
1674         /* 
1675          * here we should take care about splitted dir, so store cookie and fid
1676          * for "master" object should already be allocated and passed in @oa.
1677          */
1678         LASSERT(oa->o_id != 0);
1679         LASSERT(oa->o_fid != 0);
1680
1681         /* save "master" object id */
1682         obdo2id(&mid, oa);
1683
1684         mea = (struct mea *)*ea;
1685         mea->mea_master = -1;
1686         mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1687
1688         if (!mea->mea_count || mea->mea_count > lmv->desc.ld_tgt_count)
1689                 mea->mea_count = lmv->desc.ld_tgt_count;
1690
1691         for (i = 0, c = 0; c < mea->mea_count && i < lmv->desc.ld_tgt_count; i++) {
1692                 struct lov_stripe_md obj_md;
1693                 struct lov_stripe_md *obj_mdp = &obj_md;
1694                
1695                 if (lmv->tgts[i].ltd_exp == NULL) {
1696                         /* this is "master" MDS */
1697                         mea->mea_master = i;
1698                         mea->mea_ids[c] = mid;
1699                         c++;
1700                         continue;
1701                 }
1702
1703                 /*
1704                  * "master" MDS should always be part of stripped dir,
1705                  * so scan for it.
1706                  */
1707                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1708                         continue;
1709
1710                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1711                         OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1712
1713                 rc = obd_create(lmv->tgts[c].ltd_exp, oa, &obj_mdp, oti);
1714                 if (rc) {
1715                         CERROR("obd_create() failed on MDT target %d, "
1716                                "error %d\n", c, rc);
1717                         RETURN(rc);
1718                 }
1719
1720                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1721                        i, oa->o_id, oa->o_generation);
1722
1723
1724                 /*
1725                  * here, when object is created (or it is master and was passed
1726                  * from caller) on desired MDS we save its fid to local mea_ids.
1727                  */
1728                 LASSERT(oa->o_fid);
1729
1730                 /* 
1731                  * store cookie should be defined here for both cases (master
1732                  * object and not master), because master is already created.
1733                  */
1734                 LASSERT(oa->o_id);
1735
1736                 /* fill mea by store cookie and fid */
1737                 obdo2id(&mea->mea_ids[c], oa);
1738                 c++;
1739         }
1740         LASSERT(c == mea->mea_count);
1741
1742         CDEBUG(D_OTHER, "%d dirobjects created\n",
1743                (int)mea->mea_count);
1744         
1745         RETURN(rc);
1746 }
1747
1748 static int lmv_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1749                          struct obd_device *tgt, int count,
1750                          struct llog_catid *logid)
1751 {
1752         struct llog_ctxt *ctxt;
1753         int rc;
1754         ENTRY;
1755
1756         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1757                             &llog_client_ops);
1758         if (rc == 0) {
1759                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1760                 ctxt->loc_imp = tgt->u.cli.cl_import;
1761         }
1762
1763         RETURN(rc);
1764 }
1765
1766 static int lmv_llog_finish(struct obd_device *obd,
1767                            struct obd_llogs *llogs, int count)
1768 {
1769         int rc;
1770         ENTRY;
1771
1772         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1773         RETURN(rc);
1774 }
1775
1776 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1777                         void *key, __u32 *vallen, void *val)
1778 {
1779         struct obd_device *obd;
1780         struct lmv_obd *lmv;
1781         int rc = 0;
1782         ENTRY;
1783
1784         obd = class_exp2obd(exp);
1785         if (obd == NULL) {
1786                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1787                        exp->exp_handle.h_cookie);
1788                 RETURN(-EINVAL);
1789         }
1790
1791         lmv = &obd->u.lmv;
1792         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1793                 __u32 *mdsize = val;
1794                 *vallen = sizeof(__u32);
1795                 *mdsize = sizeof(struct lustre_id) * lmv->desc.ld_tgt_count
1796                         + sizeof(struct mea);
1797                 RETURN(0);
1798         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1799                 struct obd_uuid *cluuid = &lmv->cluuid;
1800                 struct lmv_tgt_desc *tgts;
1801                 __u32 *mdsnum = val;
1802                 int i;
1803
1804                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
1805                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1806                                 *vallen = sizeof(__u32);
1807                                 *mdsnum = i;
1808                                 RETURN(0);
1809                         }
1810                 }
1811                 LASSERT(0);
1812         } else if (keylen == 6 && memcmp(key, "rootid", 6) == 0) {
1813                 /* getting rootid from first MDS. */
1814                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
1815                                   vallen, val);
1816                 RETURN(rc);
1817         } else if (keylen >= strlen("lmvdesc") && strcmp(key, "lmvdesc") == 0) {
1818                 struct lmv_desc *desc_ret = val;
1819                 *desc_ret = lmv->desc;
1820                 RETURN(0);
1821         }
1822
1823         CDEBUG(D_IOCTL, "invalid key\n");
1824         RETURN(-EINVAL);
1825 }
1826
1827 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1828                  void *key, obd_count vallen, void *val)
1829 {
1830         struct obd_device *obd;
1831         struct lmv_obd *lmv;
1832         ENTRY;
1833
1834         obd = class_exp2obd(exp);
1835         if (obd == NULL) {
1836                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1837                        exp->exp_handle.h_cookie);
1838                 RETURN(-EINVAL);
1839         }
1840         lmv = &obd->u.lmv;
1841
1842         if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1843                 lmv->server_timeout = 1;
1844                 lmv_set_timeouts(obd);
1845                 RETURN(0);
1846         }
1847         
1848         RETURN(-EINVAL);
1849 }
1850
1851 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
1852                struct lov_stripe_md *lsm)
1853 {
1854         struct obd_device *obd = class_exp2obd(exp);
1855         struct lmv_obd *lmv = &obd->u.lmv;
1856         struct mea *meap, *lsmp;
1857         int mea_size, i;
1858         ENTRY;
1859
1860         mea_size = (sizeof(struct lustre_id) * 
1861                     lmv->desc.ld_tgt_count) + sizeof(struct mea);
1862         if (!lmmp)
1863                 RETURN(mea_size);
1864
1865         if (*lmmp && !lsm) {
1866                 OBD_FREE(*lmmp, mea_size);
1867                 *lmmp = NULL;
1868                 RETURN(0);
1869         }
1870
1871         if (*lmmp == NULL) {
1872                 OBD_ALLOC(*lmmp, mea_size);
1873                 if (*lmmp == NULL)
1874                         RETURN(-ENOMEM);
1875         }
1876
1877         if (!lsm)
1878                 RETURN(mea_size);
1879
1880         lsmp = (struct mea *)lsm;
1881         meap = (struct mea *)*lmmp;
1882
1883         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
1884         meap->mea_count = cpu_to_le32(lsmp->mea_count);
1885         meap->mea_master = cpu_to_le32(lsmp->mea_master);
1886
1887         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1888                 meap->mea_ids[i] = meap->mea_ids[i];
1889                 id_cpu_to_le(&meap->mea_ids[i]);
1890         }
1891
1892         RETURN(mea_size);
1893 }
1894
1895 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **mem_tgt,
1896                  struct lov_mds_md *disk_src, int mdsize)
1897 {
1898         struct obd_device *obd = class_exp2obd(exp);
1899         struct mea **tmea = (struct mea **)mem_tgt;
1900         struct mea *mea = (struct mea *)disk_src;
1901         struct lmv_obd *lmv = &obd->u.lmv;
1902         int mea_size, i;
1903         ENTRY;
1904
1905         mea_size = sizeof(struct lustre_id) * 
1906                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1907         if (mem_tgt == NULL)
1908                 return mea_size;
1909
1910         if (*mem_tgt != NULL && disk_src == NULL) {
1911                 OBD_FREE(*tmea, mea_size);
1912                 RETURN(0);
1913         }
1914
1915         LASSERT(mea_size == mdsize);
1916
1917         OBD_ALLOC(*tmea, mea_size);
1918         if (*tmea == NULL)
1919                 RETURN(-ENOMEM);
1920
1921         if (!disk_src)
1922                 RETURN(mea_size);
1923
1924         (*tmea)->mea_magic = le32_to_cpu(mea->mea_magic);
1925         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
1926         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
1927
1928         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1929                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
1930                 id_le_to_cpu(&(*tmea)->mea_ids[i]);
1931         }
1932
1933         RETURN(mea_size);
1934 }
1935
1936 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
1937             struct lov_stripe_md *ea, obd_count oa_bufs,
1938             struct brw_page *pgarr, struct obd_trans_info *oti)
1939 {
1940         struct obd_device *obd = exp->exp_obd;
1941         struct lmv_obd *lmv = &obd->u.lmv;
1942         struct mea *mea = (struct mea *) ea;
1943         int err;
1944       
1945         LASSERT(oa != NULL);
1946         LASSERT(ea != NULL);
1947         LASSERT(pgarr != NULL);
1948         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1949
1950         oa->o_gr = id_gen(&mea->mea_ids[oa->o_mds]);
1951         oa->o_id = id_ino(&mea->mea_ids[oa->o_mds]);
1952         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1953         
1954         err = obd_brw(rw, lmv->tgts[oa->o_mds].ltd_exp,
1955                       oa, NULL, oa_bufs, pgarr, oti);
1956         RETURN(err);
1957 }
1958
1959 struct obd_ops lmv_obd_ops = {
1960         .o_owner                = THIS_MODULE,
1961         .o_attach               = lmv_attach,
1962         .o_detach               = lmv_detach,
1963         .o_setup                = lmv_setup,
1964         .o_cleanup              = lmv_cleanup,
1965         .o_connect              = lmv_connect,
1966         .o_disconnect           = lmv_disconnect,
1967         .o_statfs               = lmv_statfs,
1968         .o_llog_init            = lmv_llog_init,
1969         .o_llog_finish          = lmv_llog_finish,
1970         .o_get_info             = lmv_get_info,
1971         .o_set_info             = lmv_set_info,
1972         .o_create               = lmv_obd_create,
1973         .o_packmd               = lmv_packmd,
1974         .o_unpackmd             = lmv_unpackmd,
1975         .o_brw                  = lmv_brw,
1976         .o_init_ea_size         = lmv_init_ea_size,
1977         .o_notify               = lmv_notify,
1978         .o_iocontrol            = lmv_iocontrol,
1979         .o_getready             = lmv_getready,
1980 };
1981
1982 struct md_ops lmv_md_ops = {
1983         .m_getstatus           = lmv_getstatus,
1984         .m_getattr             = lmv_getattr,
1985         .m_change_cbdata       = lmv_change_cbdata,
1986         .m_change_cbdata_name  = lmv_change_cbdata_name,
1987         .m_close               = lmv_close,
1988         .m_create              = lmv_create,
1989         .m_done_writing        = lmv_done_writing,
1990         .m_enqueue             = lmv_enqueue,
1991         .m_getattr_lock        = lmv_getattr_lock,
1992         .m_intent_lock         = lmv_intent_lock,
1993         .m_link                = lmv_link,
1994         .m_rename              = lmv_rename,
1995         .m_setattr             = lmv_setattr,
1996         .m_sync                = lmv_sync,
1997         .m_readpage            = lmv_readpage,
1998         .m_unlink              = lmv_unlink,
1999         .m_get_real_obd        = lmv_get_real_obd,
2000         .m_valid_attrs         = lmv_valid_attrs,
2001         .m_delete_inode        = lmv_delete_inode,
2002 };
2003
2004 int __init lmv_init(void)
2005 {
2006         struct lprocfs_static_vars lvars;
2007         int rc;
2008
2009         obj_cache = kmem_cache_create("lmv_objects",
2010                                       sizeof(struct lmv_obj),
2011                                       0, 0, NULL, NULL);
2012         if (!obj_cache) {
2013                 CERROR("error allocating lmv objects cache\n");
2014                 return -ENOMEM;
2015         }
2016
2017         lprocfs_init_vars(lmv, &lvars);
2018         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2019                                  lvars.module_vars,
2020                                  OBD_LMV_DEVICENAME);
2021         if (rc)
2022                 kmem_cache_destroy(obj_cache);
2023         
2024         return rc;
2025 }
2026
2027 #ifdef __KERNEL__
2028 static void lmv_exit(void)
2029 {
2030         class_unregister_type(OBD_LMV_DEVICENAME);
2031
2032         LASSERTF(kmem_cache_destroy(obj_cache) == 0,
2033                  "can't free lmv objects cache, %d object(s)"
2034                  "still in use\n", atomic_read(&obj_cache_count));
2035 }
2036
2037 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2038 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2039 MODULE_LICENSE("GPL");
2040
2041 module_init(lmv_init);
2042 module_exit(lmv_exit);
2043 #endif