Whamcloud - gitweb
- many fixes and cleanups in cobd. By now it load and unloads fine.
[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                 if (mdc_obd)
445                         mdc_obd->obd_no_recov = obd->obd_no_recov;
446
447 #ifdef __KERNEL__
448                 if (lmv_proc_dir) {
449                         struct proc_dir_entry *mdc_symlink;
450
451                         mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
452                         if (mdc_symlink) {
453                                 lprocfs_remove(mdc_symlink);
454                         } else {
455                                 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
456                                        obd->obd_type->typ_name, obd->obd_name,
457                                        mdc_obd->obd_name);
458                         }
459                 }
460 #endif
461                 CDEBUG(D_OTHER, "disconnected from %s(%s) successfully\n",
462                         lmv->tgts[i].ltd_exp->exp_obd->obd_name,
463                         lmv->tgts[i].ltd_exp->exp_obd->obd_uuid.uuid);
464
465                 obd_register_observer(lmv->tgts[i].ltd_exp->exp_obd, NULL);
466                 rc = obd_disconnect(lmv->tgts[i].ltd_exp, flags);
467                 if (rc) {
468                         if (lmv->tgts[i].active) {
469                                 CERROR("Target %s disconnect error %d\n",
470                                        lmv->tgts[i].uuid.uuid, rc);
471                         }
472                         rc = 0;
473                 }
474                 
475                 lmv_activate_target(lmv, &lmv->tgts[i], 0);
476                 lmv->tgts[i].ltd_exp = NULL;
477         }
478
479 #ifdef __KERNEL__
480         if (lmv_proc_dir) {
481                 lprocfs_remove(lmv_proc_dir);
482         } else {
483                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
484                        obd->obd_type->typ_name, obd->obd_name);
485         }
486 #endif
487
488 out_local:
489         /* this is the case when no real connection is established by
490          * lmv_check_connect(). */
491         if (!lmv->connected)
492                 class_export_put(exp);
493         rc = class_disconnect(exp, 0);
494         if (lmv->refcount == 0)
495                 lmv->connected = 0;
496         RETURN(rc);
497 }
498
499 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
500                          int len, void *karg, void *uarg)
501 {
502         struct obd_device *obddev = class_exp2obd(exp);
503         struct lmv_obd *lmv = &obddev->u.lmv;
504         int i, rc = 0, set = 0;
505         ENTRY;
506
507         if (lmv->desc.ld_tgt_count == 0)
508                 RETURN(-ENOTTY);
509         
510         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
511                 int err;
512
513                 if (lmv->tgts[i].ltd_exp == NULL)
514                         continue;
515
516                 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len, karg, uarg);
517                 if (err) {
518                         if (lmv->tgts[i].active) {
519                                 CERROR("error: iocontrol MDC %s on MDT"
520                                        "idx %d: err = %d\n",
521                                        lmv->tgts[i].uuid.uuid, i, err);
522                                 if (!rc)
523                                         rc = err;
524                         }
525                 } else
526                         set = 1;
527         }
528         if (!set && !rc)
529                 rc = -EIO;
530
531         RETURN(rc);
532 }
533
534 static int lmv_setup(struct obd_device *obd, obd_count len, void *buf)
535 {
536         int i, rc = 0;
537         struct lmv_desc *desc;
538         struct obd_uuid *uuids;
539         struct lmv_tgt_desc *tgts;
540         struct obd_device *tgt_obd;
541         struct lustre_cfg *lcfg = buf;
542         struct lmv_obd *lmv = &obd->u.lmv;
543         ENTRY;
544
545         if (lcfg->lcfg_inllen1 < 1) {
546                 CERROR("LMV setup requires a descriptor\n");
547                 RETURN(-EINVAL);
548         }
549
550         if (lcfg->lcfg_inllen2 < 1) {
551                 CERROR("LMV setup requires an MDT UUID list\n");
552                 RETURN(-EINVAL);
553         }
554
555         desc = (struct lmv_desc *)lcfg->lcfg_inlbuf1;
556         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
557                 CERROR("descriptor size wrong: %d > %d\n",
558                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
559                 RETURN(-EINVAL);
560         }
561
562         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
563         if (sizeof(*uuids) * desc->ld_tgt_count != lcfg->lcfg_inllen2) {
564                 CERROR("UUID array size wrong: %u * %u != %u\n",
565                        sizeof(*uuids), desc->ld_tgt_count, lcfg->lcfg_inllen2);
566                 RETURN(-EINVAL);
567         }
568
569         lmv->tgts_size = sizeof(struct lmv_tgt_desc) * desc->ld_tgt_count;
570         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
571         if (lmv->tgts == NULL) {
572                 CERROR("Out of memory\n");
573                 RETURN(-ENOMEM);
574         }
575
576         lmv->desc = *desc;
577         spin_lock_init(&lmv->lmv_lock);
578         
579         for (i = 0, tgts = lmv->tgts; i < desc->ld_tgt_count; i++, tgts++)
580                 tgts->uuid = uuids[i];
581         
582         lmv->max_cookiesize = 0;
583
584         lmv->max_easize = sizeof(struct lustre_id) *
585                 desc->ld_tgt_count + sizeof(struct mea);
586         
587         rc = lmv_setup_mgr(obd);
588         if (rc) {
589                 CERROR("Can't setup LMV object manager, "
590                        "error %d.\n", rc);
591                 OBD_FREE(lmv->tgts, lmv->tgts_size);
592         }
593
594         tgt_obd = class_find_client_obd(&lmv->tgts->uuid, LUSTRE_MDC_NAME,
595                                         &obd->obd_uuid);
596         if (!tgt_obd) {
597                 CERROR("Target %s not attached\n", lmv->tgts->uuid.uuid);
598                 RETURN(-EINVAL);
599         }
600
601         RETURN(rc);
602 }
603
604 static int lmv_cleanup(struct obd_device *obd, int flags) 
605 {
606         struct lmv_obd *lmv = &obd->u.lmv;
607         ENTRY;
608
609         lmv_cleanup_mgr(obd);
610         OBD_FREE(lmv->tgts, lmv->tgts_size);
611         
612         RETURN(0);
613 }
614
615 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
616                       unsigned long max_age)
617 {
618         struct lmv_obd *lmv = &obd->u.lmv;
619         struct obd_statfs *temp;
620         int rc = 0, i;
621         ENTRY;
622         
623         rc = lmv_check_connect(obd);
624         if (rc)
625                 RETURN(rc);
626
627         OBD_ALLOC(temp, sizeof(*temp));
628         if (temp == NULL)
629                 RETURN(-ENOMEM);
630                 
631         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
632                 if (lmv->tgts[i].ltd_exp == NULL) {
633                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
634                         continue;
635                 }
636
637                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp, max_age);
638                 if (rc) {
639                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
640                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
641                                rc);
642                         GOTO(out_free_temp, rc);
643                 }
644                 if (i == 0) {
645                         memcpy(osfs, temp, sizeof(*temp));
646                 } else {
647                         osfs->os_bavail += temp->os_bavail;
648                         osfs->os_blocks += temp->os_blocks;
649                         osfs->os_ffree += temp->os_ffree;
650                         osfs->os_files += temp->os_files;
651                 }
652         }
653
654         EXIT;
655 out_free_temp:
656         OBD_FREE(temp, sizeof(*temp));
657         return rc;
658 }
659
660 static int lmv_getstatus(struct obd_export *exp, struct lustre_id *id)
661 {
662         struct obd_device *obd = exp->exp_obd;
663         struct lmv_obd *lmv = &obd->u.lmv;
664         int rc;
665         ENTRY;
666
667         rc = lmv_check_connect(obd);
668         if (rc)
669                 RETURN(rc);
670
671         rc = md_getstatus(lmv->tgts[0].ltd_exp, id);
672         id_group(id) = 0;
673         
674         RETURN(rc);
675 }
676
677 static int lmv_getattr(struct obd_export *exp, struct lustre_id *id,
678                        __u64 valid, unsigned int ea_size,
679                        struct ptlrpc_request **request)
680 {
681         struct obd_device *obd = exp->exp_obd;
682         struct lmv_obd *lmv = &obd->u.lmv;
683         int rc, i = id_group(id);
684         struct lmv_obj *obj;
685         ENTRY;
686
687         rc = lmv_check_connect(obd);
688         if (rc)
689                 RETURN(rc);
690
691         LASSERT(i < lmv->desc.ld_tgt_count);
692
693         rc = md_getattr(lmv->tgts[i].ltd_exp, id, valid,
694                         ea_size, request);
695         if (rc)
696                 RETURN(rc);
697         
698         obj = lmv_grab_obj(obd, id);
699         
700         CDEBUG(D_OTHER, "GETATTR for "DLID4" %s\n",
701                OLID4(id), obj ? "(splitted)" : "");
702
703         /*
704          * if object is splitted, then we loop over all the slaves and gather
705          * size attribute. In ideal world we would have to gather also mds field
706          * from all slaves, as object is spread over the cluster and this is
707          * definitely interesting information and it is not good to loss it,
708          * but...
709          */
710         if (obj) {
711                 struct mds_body *body;
712
713                 if (*request == NULL) {
714                         lmv_put_obj(obj);
715                         RETURN(rc);
716                 }
717                         
718                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
719                                       sizeof(*body));
720                 LASSERT(body != NULL);
721
722                 lmv_lock_obj(obj);
723         
724                 for (i = 0; i < obj->objcount; i++) {
725
726                         if (lmv->tgts[i].ltd_exp == NULL) {
727                                 CWARN("%s: NULL export for %d\n",
728                                       obd->obd_name, i);
729                                 continue;
730                         }
731
732                         /* skip master obj. */
733                         if (id_equal_fid(&obj->id, &obj->objs[i].id))
734                                 continue;
735                         
736                         body->size += obj->objs[i].size;
737                 }
738
739                 lmv_unlock_obj(obj);
740                 lmv_put_obj(obj);
741         }
742         
743         RETURN(rc);
744 }
745
746 static int lmv_change_cbdata(struct obd_export *exp,
747                              struct lustre_id *id, 
748                              ldlm_iterator_t it,
749                              void *data)
750 {
751         struct obd_device *obd = exp->exp_obd;
752         struct lmv_obd *lmv = &obd->u.lmv;
753         int rc = 0;
754         ENTRY;
755         
756         rc = lmv_check_connect(obd);
757         if (rc)
758                 RETURN(rc);
759         
760         CDEBUG(D_OTHER, "CBDATA for "DLID4"\n", OLID4(id));
761         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
762
763         rc = md_change_cbdata(lmv->tgts[id_group(id)].ltd_exp,
764                               id, it, data);
765         
766         RETURN(rc);
767 }
768
769 static int lmv_change_cbdata_name(struct obd_export *exp,
770                                   struct lustre_id *pid,
771                                   char *name, int len,
772                                   struct lustre_id *cid,
773                                   ldlm_iterator_t it,
774                                   void *data)
775 {
776         struct obd_device *obd = exp->exp_obd;
777         struct lmv_obd *lmv = &obd->u.lmv;
778         struct lustre_id rcid = *cid;
779         struct lmv_obj *obj;
780         int rc = 0, mds;
781         ENTRY;
782
783         rc = lmv_check_connect(obd);
784         if (rc)
785                 RETURN(rc);
786
787         LASSERT(id_group(pid) < lmv->desc.ld_tgt_count);
788         LASSERT(id_group(cid) < lmv->desc.ld_tgt_count);
789         
790         CDEBUG(D_OTHER, "CBDATA for "DLID4":%*s -> "DLID4"\n",
791                OLID4(pid), len, name, OLID4(cid));
792
793         /* this is default mds for directory name belongs to. */
794         mds = id_group(pid);
795         obj = lmv_grab_obj(obd, pid);
796         if (obj) {
797                 /* directory is splitted. look for right mds for this name. */
798                 mds = raw_name2idx(obj->hashtype, obj->objcount, name, len);
799                 rcid = obj->objs[mds].id;
800                 mds = id_group(&rcid);
801                 lmv_put_obj(obj);
802         }
803         rc = md_change_cbdata(lmv->tgts[mds].ltd_exp, &rcid, it, data);
804         RETURN(rc);
805 }
806
807 static int lmv_valid_attrs(struct obd_export *exp, struct lustre_id *id) 
808 {
809         struct obd_device *obd = exp->exp_obd;
810         struct lmv_obd *lmv = &obd->u.lmv;
811         int rc = 0;
812         ENTRY;
813
814         rc = lmv_check_connect(obd);
815         if (rc)
816                 RETURN(rc);
817
818         CDEBUG(D_OTHER, "validate "DLID4"\n", OLID4(id));
819         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
820         rc = md_valid_attrs(lmv->tgts[id_group(id)].ltd_exp, id);
821         RETURN(rc);
822 }
823
824 int lmv_close(struct obd_export *exp, struct obdo *obdo,
825               struct obd_client_handle *och,
826               struct ptlrpc_request **request)
827 {
828         struct obd_device *obd = exp->exp_obd;
829         struct lmv_obd *lmv = &obd->u.lmv;
830         int rc, i = obdo->o_mds;
831         ENTRY;
832         
833         rc = lmv_check_connect(obd);
834         if (rc)
835                 RETURN(rc);
836
837         LASSERT(i < lmv->desc.ld_tgt_count);
838         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long)obdo->o_mds,
839                (unsigned long)obdo->o_id, (unsigned long)obdo->o_generation);
840         rc = md_close(lmv->tgts[i].ltd_exp, obdo, och, request);
841         RETURN(rc);
842 }
843
844 int lmv_get_mea_and_update_object(struct obd_export *exp, 
845                                   struct lustre_id *id)
846 {
847         struct obd_device *obd = exp->exp_obd;
848         struct lmv_obd *lmv = &obd->u.lmv;
849         struct ptlrpc_request *req = NULL;
850         struct lmv_obj *obj;
851         struct lustre_md md;
852         int mealen, rc;
853         __u64 valid;
854         ENTRY;
855
856         md.mea = NULL;
857         mealen = MEA_SIZE_LMV(lmv);
858         
859         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
860
861         /* time to update mea of parent id */
862         rc = md_getattr(lmv->tgts[id_group(id)].ltd_exp,
863                         id, valid, mealen, &req);
864         if (rc) {
865                 CERROR("md_getattr() failed, error %d\n", rc);
866                 GOTO(cleanup, rc);
867         }
868
869         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
870         if (rc) {
871                 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
872                 GOTO(cleanup, rc);
873         }
874
875         if (md.mea == NULL)
876                 GOTO(cleanup, rc = -ENODATA);
877
878         obj = lmv_create_obj(exp, id, md.mea);
879         if (IS_ERR(obj))
880                 rc = PTR_ERR(obj);
881         
882         lmv_put_obj(obj);
883         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
884
885         EXIT;
886 cleanup:
887         if (req)
888                 ptlrpc_req_finished(req);
889         return rc;
890 }
891
892 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
893                const void *data, int datalen, int mode, __u32 uid,
894                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
895 {
896         struct obd_device *obd = exp->exp_obd;
897         struct lmv_obd *lmv = &obd->u.lmv;
898         struct mds_body *body;
899         struct lmv_obj *obj;
900         int rc, mds, loop = 0;
901         ENTRY;
902
903         rc = lmv_check_connect(obd);
904         if (rc)
905                 RETURN(rc);
906
907         if (!lmv->desc.ld_active_tgt_count)
908                 RETURN(-EIO);
909 repeat:
910         LASSERT(++loop <= 2);
911         obj = lmv_grab_obj(obd, &op_data->id1);
912         if (obj) {
913                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
914                                    op_data->name, op_data->namelen);
915                 op_data->id1 = obj->objs[mds].id;
916                 lmv_put_obj(obj);
917         }
918
919         CDEBUG(D_OTHER, "CREATE '%*s' on "DLID4"\n", op_data->namelen,
920                op_data->name, OLID4(&op_data->id1));
921         
922         rc = md_create(lmv->tgts[id_group(&op_data->id1)].ltd_exp, 
923                        op_data, data, datalen, mode, uid, gid, rdev,
924                        request);
925         if (rc == 0) {
926                 if (*request == NULL)
927                         RETURN(rc);
928
929                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
930                                       sizeof(*body));
931                 if (body == NULL)
932                         RETURN(-ENOMEM);
933                 
934                 CDEBUG(D_OTHER, "created. "DLID4"\n", OLID4(&op_data->id1));
935         } else if (rc == -ERESTART) {
936                 /*
937                  * directory got splitted. time to update local object and
938                  * repeat the request with proper MDS.
939                  */
940                 rc = lmv_get_mea_and_update_object(exp, &op_data->id1);
941                 if (rc == 0) {
942                         ptlrpc_req_finished(*request);
943                         goto repeat;
944                 }
945         }
946         RETURN(rc);
947 }
948
949 int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
950 {
951         struct obd_device *obd = exp->exp_obd;
952         struct lmv_obd *lmv = &obd->u.lmv;
953         int rc;
954         ENTRY;
955         
956         rc = lmv_check_connect(obd);
957         if (rc)
958                 RETURN(rc);
959
960         /* FIXME: choose right MDC here */
961         CWARN("this method isn't implemented yet\n");
962         rc = md_done_writing(lmv->tgts[0].ltd_exp, obdo);
963         RETURN(rc);
964 }
965
966 int lmv_enqueue_slaves(struct obd_export *exp, int locktype,
967                        struct lookup_intent *it, int lockmode,
968                        struct mdc_op_data *data, struct lustre_handle *lockh,
969                        void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
970                        ldlm_blocking_callback cb_blocking, void *cb_data)
971 {
972         struct obd_device *obd = exp->exp_obd;
973         struct lmv_obd *lmv = &obd->u.lmv;
974         struct mea *mea = data->mea1;
975         struct mdc_op_data *data2;
976         int i, rc, mds;
977         ENTRY;
978
979         OBD_ALLOC(data2, sizeof(*data2));
980         if (data2 == NULL)
981                 RETURN(-ENOMEM);
982         
983         LASSERT(mea != NULL);
984         for (i = 0; i < mea->mea_count; i++) {
985                 memset(data2, 0, sizeof(*data2));
986                 data2->id1 = mea->mea_ids[i];
987                 mds = id_group(&data2->id1);
988                 
989                 if (lmv->tgts[mds].ltd_exp == NULL)
990                         continue;
991
992                 rc = md_enqueue(lmv->tgts[mds].ltd_exp, locktype, it, 
993                                 lockmode, data2, lockh + i, lmm, lmmsize, 
994                                 cb_compl, cb_blocking, cb_data);
995                 
996                 CDEBUG(D_OTHER, "take lock on slave "DLID4" -> %d/%d\n",
997                        OLID4(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
998                 if (rc)
999                         GOTO(cleanup, rc);
1000                 if (it->d.lustre.it_data) {
1001                         struct ptlrpc_request *req;
1002                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1003                         ptlrpc_req_finished(req);
1004                 }
1005                 
1006                 if (it->d.lustre.it_status)
1007                         GOTO(cleanup, rc = it->d.lustre.it_status);
1008         }
1009         
1010         OBD_FREE(data2, sizeof(*data2));
1011         RETURN(0);
1012 cleanup:
1013         OBD_FREE(data2, sizeof(*data2));
1014         
1015         /* drop all taken locks */
1016         while (--i >= 0) {
1017                 if (lockh[i].cookie)
1018                         ldlm_lock_decref(lockh + i, lockmode);
1019                 lockh[i].cookie = 0;
1020         }
1021         return rc;
1022 }
1023
1024 int lmv_enqueue(struct obd_export *exp, int lock_type,
1025                 struct lookup_intent *it, int lock_mode,
1026                 struct mdc_op_data *data, struct lustre_handle *lockh,
1027                 void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1028                 ldlm_blocking_callback cb_blocking, void *cb_data)
1029 {
1030         struct obd_device *obd = exp->exp_obd;
1031         struct lmv_obd *lmv = &obd->u.lmv;
1032         struct lmv_obj *obj;
1033         int rc, mds;
1034         ENTRY;
1035
1036         rc = lmv_check_connect(obd);
1037         if (rc)
1038                 RETURN(rc);
1039
1040         if (data->mea1 && it->it_op == IT_UNLINK) {
1041                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
1042                                         data, lockh, lmm, lmmsize,
1043                                         cb_compl, cb_blocking, cb_data);
1044                 RETURN(rc);
1045         }
1046
1047         if (data->namelen) {
1048                 obj = lmv_grab_obj(obd, &data->id1);
1049                 if (obj) {
1050                         /* directory is splitted. look for right mds for this
1051                          * name */
1052                         mds = raw_name2idx(obj->hashtype, obj->objcount,
1053                                            (char *)data->name, data->namelen);
1054                         data->id1 = obj->objs[mds].id;
1055                         lmv_put_obj(obj);
1056                 }
1057         }
1058         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DLID4"\n", LL_IT2STR(it),
1059                OLID4(&data->id1));
1060         
1061         rc = md_enqueue(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1062                         lock_type, it, lock_mode, data, lockh, lmm, 
1063                         lmmsize, cb_compl, cb_blocking, cb_data);
1064         RETURN(rc);
1065 }
1066
1067 int lmv_getattr_lock(struct obd_export *exp, struct lustre_id *id,
1068                      char *filename, int namelen, __u64 valid,
1069                      unsigned int ea_size, struct ptlrpc_request **request)
1070 {
1071         int rc, mds = id_group(id), loop = 0;
1072         struct obd_device *obd = exp->exp_obd;
1073         struct lmv_obd *lmv = &obd->u.lmv;
1074         struct lustre_id rid = *id;
1075         struct mds_body *body;
1076         struct lmv_obj *obj;
1077         __u64 old_valid;
1078         ENTRY;
1079         
1080         rc = lmv_check_connect(obd);
1081         if (rc)
1082                 RETURN(rc);
1083 repeat:
1084         LASSERT(++loop <= 2);
1085         obj = lmv_grab_obj(obd, id);
1086         if (obj) {
1087                 /* directory is splitted. look for right mds for this name */
1088                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1089                                    filename, namelen - 1);
1090                 rid = obj->objs[mds].id;
1091                 lmv_put_obj(obj);
1092         }
1093         
1094         CDEBUG(D_OTHER, "getattr_lock for %*s on "DLID4" -> "DLID4"\n",
1095                namelen, filename, OLID4(id), OLID4(&rid));
1096
1097         old_valid = valid;
1098
1099         /*
1100          * here should be applied OBD_MD_FID to ->valid, because otherwise,
1101          * mds_getattr_lock() will not fetch fid component of lustre_id and
1102          * thus, next call to md_getattr_lock() will be performed to wrong mds.
1103          */
1104         if (!(old_valid & OBD_MD_FID))
1105                 valid |= OBD_MD_FID;
1106         
1107         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp, 
1108                              &rid, filename, namelen, valid,
1109                              ea_size, request);
1110         if (rc == 0) {
1111                 /*
1112                  * this could be cross-node reference. in this case all we have
1113                  * right now is lustre_id triple. we'd like to find other
1114                  * attributes.
1115                  */
1116                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
1117                 LASSERT(body != NULL);
1118                 if (body->valid & OBD_MD_MDS) {
1119                         struct ptlrpc_request *req = NULL;
1120                         
1121                         rid = body->id1;
1122                         CDEBUG(D_OTHER, "request attrs for "DLID4"\n", OLID4(&rid));
1123
1124                         /* 
1125                          * turning OBD_MD_FID fetching off, as we already have
1126                          * full lustre_id and do need to fetch fid component
1127                          * again. This will help to make thing slightly faster.
1128                          */
1129                         if (!(old_valid & OBD_MD_FID))
1130                                 valid &= ~OBD_MD_FID;
1131                         
1132                         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp, 
1133                                              &rid, NULL, 1, valid, ea_size, &req);
1134                         ptlrpc_req_finished(*request);
1135                         *request = req;
1136                 }
1137         } else if (rc == -ERESTART) {
1138                 /* directory got splitted. time to update local object and
1139                  * repeat the request with proper MDS */
1140                 rc = lmv_get_mea_and_update_object(exp, &rid);
1141                 if (rc == 0) {
1142                         ptlrpc_req_finished(*request);
1143                         goto repeat;
1144                 }
1145         }
1146         RETURN(rc);
1147 }
1148
1149 /*
1150  * llite passes id of an target inode in data->id1 and id of directory in
1151  * data->id2
1152  */
1153 int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
1154              struct ptlrpc_request **request)
1155 {
1156         struct obd_device *obd = exp->exp_obd;
1157         struct lmv_obd *lmv = &obd->u.lmv;
1158         struct lmv_obj *obj;
1159         int rc;
1160         ENTRY;
1161         
1162         rc = lmv_check_connect(obd);
1163         if (rc)
1164                 RETURN(rc);
1165
1166         if (data->namelen != 0) {
1167                 /* usual link request */
1168                 obj = lmv_grab_obj(obd, &data->id1);
1169                 if (obj) {
1170                         rc = raw_name2idx(obj->hashtype, obj->objcount, 
1171                                           data->name, data->namelen);
1172                         data->id1 = obj->objs[rc].id;
1173                         lmv_put_obj(obj);
1174                 }
1175                 
1176                 CDEBUG(D_OTHER,"link "DLID4":%*s to "DLID4"\n",
1177                        OLID4(&data->id2), data->namelen, data->name,
1178                        OLID4(&data->id1));
1179         } else {
1180                 /* request from MDS to acquire i_links for inode by id1 */
1181                 CDEBUG(D_OTHER, "inc i_nlinks for "DLID4"\n",
1182                        OLID4(&data->id1));
1183         }
1184                         
1185         rc = md_link(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1186                      data, request);
1187         RETURN(rc);
1188 }
1189
1190 int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
1191                const char *old, int oldlen, const char *new, int newlen,
1192                struct ptlrpc_request **request)
1193 {
1194         struct obd_device *obd = exp->exp_obd;
1195         struct lmv_obd *lmv = &obd->u.lmv;
1196         struct lmv_obj *obj;
1197         int rc, mds;
1198         ENTRY;
1199
1200         CDEBUG(D_OTHER, "rename %*s in "DLID4" to %*s in "DLID4"\n",
1201                oldlen, old, OLID4(&data->id1), newlen, new,
1202                OLID4(&data->id2));
1203
1204         rc = lmv_check_connect(obd);
1205         if (rc)
1206                 RETURN(rc);
1207
1208         if (oldlen == 0) {
1209                 /*
1210                  * MDS with old dir entry is asking another MDS to create name
1211                  * there.
1212                  */
1213                 CDEBUG(D_OTHER,
1214                        "create %*s(%d/%d) in "DLID4" pointing "
1215                        "to "DLID4"\n", newlen, new, oldlen, newlen,
1216                        OLID4(&data->id2), OLID4(&data->id1));
1217
1218                 mds = id_group(&data->id2);
1219
1220                 /* 
1221                  * target directory can be splitted, sowe should forward request
1222                  * to the right MDS.
1223                  */
1224                 obj = lmv_grab_obj(obd, &data->id2);
1225                 if (obj) {
1226                         mds = raw_name2idx(obj->hashtype, obj->objcount, 
1227                                            (char *)new, newlen);
1228                         data->id2 = obj->objs[mds].id;
1229                         CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1230                                OLID4(&data->id2));
1231                         lmv_put_obj(obj);
1232                 }
1233                 goto request;
1234         }
1235
1236         obj = lmv_grab_obj(obd, &data->id1);
1237         if (obj) {
1238                 /*
1239                  * directory is already splitted, so we have to forward request
1240                  * to the right MDS.
1241                  */
1242                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1243                                    (char *)old, oldlen);
1244                 data->id1 = obj->objs[mds].id;
1245                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1246                        OLID4(&data->id1));
1247                 lmv_put_obj(obj);
1248         }
1249
1250         obj = lmv_grab_obj(obd, &data->id2);
1251         if (obj) {
1252                 /*
1253                  * directory is already splitted, so we have to forward request
1254                  * to the right MDS.
1255                  */
1256                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1257                                    (char *)new, newlen);
1258                 
1259                 data->id2 = obj->objs[mds].id;
1260                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1261                        OLID4(&data->id2));
1262                 lmv_put_obj(obj);
1263         }
1264         
1265         mds = id_group(&data->id1);
1266
1267 request:
1268         if (id_group(&data->id1) != id_group(&data->id2)) {
1269                 CDEBUG(D_OTHER,"cross-node rename "DLID4"/%*s to "DLID4"/%*s\n",
1270                        OLID4(&data->id1), oldlen, old, OLID4(&data->id2),
1271                        newlen, new);
1272         }
1273
1274         rc = md_rename(lmv->tgts[mds].ltd_exp, data, old, oldlen,
1275                        new, newlen, request); 
1276         RETURN(rc);
1277 }
1278
1279 int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
1280                 struct iattr *iattr, void *ea, int ealen, void *ea2,
1281                 int ea2len, struct ptlrpc_request **request)
1282 {
1283         struct obd_device *obd = exp->exp_obd;
1284         struct lmv_obd *lmv = &obd->u.lmv;
1285         struct ptlrpc_request *req;
1286         struct mds_body *body;
1287         struct lmv_obj *obj;
1288         int rc = 0, i;
1289         ENTRY;
1290
1291         rc = lmv_check_connect(obd);
1292         if (rc)
1293                 RETURN(rc);
1294
1295         obj = lmv_grab_obj(obd, &data->id1);
1296         
1297         CDEBUG(D_OTHER, "SETATTR for "DLID4", valid 0x%x%s\n",
1298                OLID4(&data->id1), iattr->ia_valid, obj ? ", splitted" : "");
1299         
1300         if (obj) {
1301                 for (i = 0; i < obj->objcount; i++) {
1302                         data->id1 = obj->objs[i].id;
1303                         
1304                         rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1305                                         data, iattr, ea, ealen, ea2, ea2len, &req);
1306
1307                         if (id_equal_fid(&obj->id, &obj->objs[i].id)) {
1308                                 /*
1309                                  * this is master object and this request should
1310                                  * be returned back to llite.
1311                                  */
1312                                 *request = req;
1313                         } else {
1314                                 ptlrpc_req_finished(req);
1315                         }
1316
1317                         if (rc)
1318                                 break;
1319                 }
1320                 lmv_put_obj(obj);
1321         } else {
1322                 LASSERT(id_group(&data->id1) < lmv->desc.ld_tgt_count);
1323                 rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp,
1324                                 data, iattr, ea, ealen, ea2, ea2len, request); 
1325                 if (rc == 0) {
1326                         body = lustre_msg_buf((*request)->rq_repmsg, 0,
1327                                               sizeof(*body));
1328                         LASSERT(body != NULL);
1329                         LASSERT(id_group(&body->id1) == id_group(&data->id1));
1330                 }
1331         }
1332         RETURN(rc);
1333 }
1334
1335 int lmv_sync(struct obd_export *exp, struct lustre_id *id,
1336              struct ptlrpc_request **request)
1337 {
1338         struct obd_device *obd = exp->exp_obd;
1339         struct lmv_obd *lmv = &obd->u.lmv;
1340         int rc;
1341         ENTRY;
1342
1343         rc = lmv_check_connect(obd);
1344         if (rc)
1345                 RETURN(rc);
1346
1347         rc = md_sync(lmv->tgts[id_group(id)].ltd_exp, 
1348                      id, request);
1349         RETURN(rc);
1350 }
1351
1352 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock, 
1353                             struct ldlm_lock_desc *desc,
1354                             void *data, int flag)
1355 {
1356         struct lustre_handle lockh;
1357         struct lmv_obj *obj;
1358         int rc;
1359         ENTRY;
1360
1361         switch (flag) {
1362         case LDLM_CB_BLOCKING:
1363                 ldlm_lock2handle(lock, &lockh);
1364                 rc = ldlm_cli_cancel(&lockh);
1365                 if (rc < 0) {
1366                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1367                         RETURN(rc);
1368                 }
1369                 break;
1370         case LDLM_CB_CANCELING:
1371                 /* time to drop cached attrs for dirobj */
1372                 obj = lock->l_ast_data;
1373                 if (obj) {
1374                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
1375                                ", master "DLID4"\n",
1376                                lock->l_resource->lr_name.name[3] == 1 ?
1377                                "LOOKUP" : "UPDATE",
1378                                lock->l_resource->lr_name.name[0],
1379                                lock->l_resource->lr_name.name[1], 
1380                                OLID4(&obj->id));
1381                         lmv_put_obj(obj);
1382                 }
1383                 break;
1384         default:
1385                 LBUG();
1386         }
1387         RETURN(0);
1388 }
1389
1390 void lmv_remove_dots(struct page *page)
1391 {
1392         unsigned limit = PAGE_CACHE_SIZE;
1393         char *kaddr = page_address(page);
1394         struct ext2_dir_entry_2 *p;
1395         unsigned offs, rec_len;
1396
1397         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
1398                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
1399                 rec_len = le16_to_cpu(p->rec_len);
1400
1401                 if ((p->name_len == 1 && p->name[0] == '.') ||
1402                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
1403                         p->inode = 0;
1404         }
1405 }
1406
1407 int lmv_readpage(struct obd_export *exp, struct lustre_id *id,
1408                  __u64 offset, struct page *page,
1409                  struct ptlrpc_request **request)
1410 {
1411         struct obd_device *obd = exp->exp_obd;
1412         struct lmv_obd *lmv = &obd->u.lmv;
1413         struct lustre_id rid = *id;
1414         struct lmv_obj *obj;
1415         int rc, i;
1416         ENTRY;
1417
1418 #warning "we need well-desgined readdir() implementation"
1419         rc = lmv_check_connect(obd);
1420         if (rc)
1421                 RETURN(rc);
1422
1423         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
1424         CDEBUG(D_OTHER, "READPAGE at %llu from "DLID4"\n",
1425                offset, OLID4(&rid));
1426
1427         obj = lmv_grab_obj(obd, id);
1428         if (obj) {
1429                 lmv_lock_obj(obj);
1430
1431                 /* find dirobj containing page with requested offset. */
1432                 for (i = 0; i < obj->objcount; i++) {
1433                         if (offset < obj->objs[i].size)
1434                                 break;
1435                         offset -= obj->objs[i].size;
1436                 }
1437                 rid = obj->objs[i].id;
1438                 
1439                 lmv_unlock_obj(obj);
1440                 lmv_put_obj(obj);
1441                 
1442                 CDEBUG(D_OTHER, "forward to "DLID4" with offset %lu\n",
1443                        OLID4(&rid), (unsigned long)offset);
1444         }
1445         rc = md_readpage(lmv->tgts[id_group(&rid)].ltd_exp, &rid, 
1446                          offset, page, request);
1447         
1448         if (rc == 0 && !id_equal_fid(&rid, id))
1449                 /* this page isn't from master object. To avoid "." and ".." 
1450                  * duplication in directory, we have to remove them from all
1451                  * slave objects */
1452                 lmv_remove_dots(page);
1453         
1454         RETURN(rc);
1455 }
1456
1457 int lmv_unlink_slaves(struct obd_export *exp, struct mdc_op_data *data,
1458                       struct ptlrpc_request **req)
1459 {
1460         struct obd_device *obd = exp->exp_obd;
1461         struct lmv_obd *lmv = &obd->u.lmv;
1462         struct mea *mea = data->mea1;
1463         struct mdc_op_data *data2;
1464         int i, rc = 0;
1465         ENTRY;
1466
1467         OBD_ALLOC(data2, sizeof(*data2));
1468         if (data2 == NULL)
1469                 RETURN(-ENOMEM);
1470         
1471         LASSERT(mea != NULL);
1472         for (i = 0; i < mea->mea_count; i++) {
1473                 memset(data2, 0, sizeof(*data2));
1474                 data2->id1 = mea->mea_ids[i];
1475                 data2->create_mode = MDS_MODE_DONT_LOCK | S_IFDIR;
1476                 
1477                 if (lmv->tgts[id_group(&data2->id1)].ltd_exp == NULL)
1478                         continue;
1479
1480                 rc = md_unlink(lmv->tgts[id_group(&data2->id1)].ltd_exp,
1481                                data2, req);
1482                 
1483                 CDEBUG(D_OTHER, "unlink slave "DLID4" -> %d\n",
1484                        OLID4(&mea->mea_ids[i]), rc);
1485                 
1486                 if (*req) {
1487                         ptlrpc_req_finished(*req);
1488                         *req = NULL;
1489                 }
1490                 if (rc)
1491                         RETURN(rc);
1492         }
1493         OBD_FREE(data2, sizeof(*data2));
1494         RETURN(rc);
1495 }
1496
1497 int lmv_delete_inode(struct obd_export *exp, struct lustre_id *id)
1498 {
1499         ENTRY;
1500
1501         LASSERT(exp && id);
1502         if (lmv_delete_obj(exp, id)) {
1503                 CDEBUG(D_OTHER, "lmv object "DLID4" is destroyed.\n",
1504                        OLID4(id));
1505         }
1506         RETURN(0);
1507 }
1508
1509 int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
1510                struct ptlrpc_request **request)
1511 {
1512         struct obd_device *obd = exp->exp_obd;
1513         struct lmv_obd *lmv = &obd->u.lmv;
1514         int rc, i = 0;
1515         ENTRY;
1516         
1517         rc = lmv_check_connect(obd);
1518         if (rc)
1519                 RETURN(rc);
1520
1521         if (data->namelen == 0 && data->mea1 != NULL) {
1522                 /* mds asks to remove slave objects */
1523                 rc = lmv_unlink_slaves(exp, data, request);
1524                 RETURN(rc);
1525         }
1526
1527         if (data->namelen != 0) {
1528                 struct lmv_obj *obj;
1529                 
1530                 obj = lmv_grab_obj(obd, &data->id1);
1531                 if (obj) {
1532                         i = raw_name2idx(obj->hashtype, obj->objcount,
1533                                          data->name, data->namelen);
1534                         data->id1 = obj->objs[i].id;
1535                         lmv_put_obj(obj);
1536                 }
1537                 CDEBUG(D_OTHER, "unlink '%*s' in "DLID4" -> %u\n",
1538                        data->namelen, data->name, OLID4(&data->id1),
1539                        i);
1540         } else {
1541                 CDEBUG(D_OTHER, "drop i_nlink on "DLID4"\n",
1542                        OLID4(&data->id1));
1543         }
1544         rc = md_unlink(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1545                        data, request);
1546         RETURN(rc);
1547 }
1548
1549 struct obd_device *lmv_get_real_obd(struct obd_export *exp,
1550                                     char *name, int len)
1551 {
1552         struct obd_device *obd = exp->exp_obd;
1553         struct lmv_obd *lmv = &obd->u.lmv;
1554         int rc;
1555         ENTRY;
1556
1557         rc = lmv_check_connect(obd);
1558         if (rc)
1559                 RETURN(ERR_PTR(rc));
1560         obd = lmv->tgts[0].ltd_exp->exp_obd;
1561         EXIT;
1562         
1563         return obd;
1564 }
1565
1566 int lmv_init_ea_size(struct obd_export *exp, int easize,
1567                      int cookiesize)
1568 {
1569         struct obd_device *obd = exp->exp_obd;
1570         struct lmv_obd *lmv = &obd->u.lmv;
1571         int i, rc = 0, change = 0;
1572         ENTRY;
1573
1574         if (lmv->max_easize < easize) {
1575                 lmv->max_easize = easize;
1576                 change = 1;
1577         }
1578         if (lmv->max_cookiesize < cookiesize) {
1579                 lmv->max_cookiesize = cookiesize;
1580                 change = 1;
1581         }
1582         if (change == 0)
1583                 RETURN(0);
1584         
1585         if (lmv->connected == 0)
1586                 RETURN(0);
1587
1588         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1589                 if (lmv->tgts[i].ltd_exp == NULL) {
1590                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
1591                         continue;
1592                 }
1593
1594                 rc = obd_init_ea_size(lmv->tgts[i].ltd_exp, easize, cookiesize);
1595                 if (rc) {
1596                         CERROR("obd_init_ea_size() failed on MDT target %d, "
1597                                "error %d.\n", i, rc);
1598                         break;
1599                 }
1600         }
1601         RETURN(rc);
1602 }
1603
1604 int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1605                           struct lov_stripe_md **ea, struct obd_trans_info *oti)
1606 {
1607         struct obd_device *obd = exp->exp_obd;
1608         struct lmv_obd *lmv = &obd->u.lmv;
1609         struct lov_stripe_md obj_md;
1610         struct lov_stripe_md *obj_mdp = &obj_md;
1611         int rc = 0;
1612         ENTRY;
1613
1614         LASSERT(ea == NULL);
1615         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1616
1617         rc = obd_create(lmv->tgts[oa->o_mds].ltd_exp,
1618                         oa, &obj_mdp, oti);
1619
1620         RETURN(rc);
1621 }
1622
1623 int lmv_getready(struct obd_export *exp)
1624 {
1625         struct obd_device *obd = exp->exp_obd;
1626         int rc = 0;
1627         
1628         ENTRY;
1629         rc = lmv_check_connect(obd);
1630         RETURN(rc);
1631 }
1632
1633 /*
1634  * to be called from MDS only. @oa should have correct store cookie and o_fid
1635  * values for "master" object, as it will be used.
1636  */
1637 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1638                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1639 {
1640         struct obd_device *obd = exp->exp_obd;
1641         struct lmv_obd *lmv = &obd->u.lmv;
1642         struct lustre_id mid;
1643         int i, c, rc = 0;
1644         struct mea *mea;
1645         ENTRY;
1646
1647         rc = lmv_check_connect(obd);
1648         if (rc)
1649                 RETURN(rc);
1650
1651         LASSERT(oa != NULL);
1652         
1653         if (ea == NULL) {
1654                 rc = lmv_obd_create_single(exp, oa, NULL, oti);
1655                 if (rc)
1656                         CERROR("Can't create object, rc = %d\n", rc);
1657                 RETURN(rc);
1658         }
1659
1660         if (*ea == NULL) {
1661                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1662                 if (rc < 0) {
1663                         CERROR("obd_alloc_diskmd() failed, error %d\n",
1664                                rc);
1665                         RETURN(rc);
1666                 } else
1667                         rc = 0;
1668                 
1669                 if (*ea == NULL)
1670                         RETURN(-ENOMEM);
1671         }
1672
1673         /* 
1674          * here we should take care about splitted dir, so store cookie and fid
1675          * for "master" object should already be allocated and passed in @oa.
1676          */
1677         LASSERT(oa->o_id != 0);
1678         LASSERT(oa->o_fid != 0);
1679
1680         /* save "master" object id */
1681         obdo2id(&mid, oa);
1682
1683         mea = (struct mea *)*ea;
1684         mea->mea_master = -1;
1685         mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1686
1687         if (!mea->mea_count || mea->mea_count > lmv->desc.ld_tgt_count)
1688                 mea->mea_count = lmv->desc.ld_tgt_count;
1689
1690         for (i = 0, c = 0; c < mea->mea_count && i < lmv->desc.ld_tgt_count; i++) {
1691                 struct lov_stripe_md obj_md;
1692                 struct lov_stripe_md *obj_mdp = &obj_md;
1693                
1694                 if (lmv->tgts[i].ltd_exp == NULL) {
1695                         /* this is "master" MDS */
1696                         mea->mea_master = i;
1697                         mea->mea_ids[c] = mid;
1698                         c++;
1699                         continue;
1700                 }
1701
1702                 /*
1703                  * "master" MDS should always be part of stripped dir,
1704                  * so scan for it.
1705                  */
1706                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1707                         continue;
1708
1709                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1710                         OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1711
1712                 rc = obd_create(lmv->tgts[c].ltd_exp, oa, &obj_mdp, oti);
1713                 if (rc) {
1714                         CERROR("obd_create() failed on MDT target %d, "
1715                                "error %d\n", c, rc);
1716                         RETURN(rc);
1717                 }
1718
1719                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1720                        i, oa->o_id, oa->o_generation);
1721
1722
1723                 /*
1724                  * here, when object is created (or it is master and was passed
1725                  * from caller) on desired MDS we save its fid to local mea_ids.
1726                  */
1727                 LASSERT(oa->o_fid);
1728
1729                 /* 
1730                  * store cookie should be defined here for both cases (master
1731                  * object and not master), because master is already created.
1732                  */
1733                 LASSERT(oa->o_id);
1734
1735                 /* fill mea by store cookie and fid */
1736                 obdo2id(&mea->mea_ids[c], oa);
1737                 c++;
1738         }
1739         LASSERT(c == mea->mea_count);
1740
1741         CDEBUG(D_OTHER, "%d dirobjects created\n",
1742                (int)mea->mea_count);
1743         
1744         RETURN(rc);
1745 }
1746
1747 static int lmv_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1748                          struct obd_device *tgt, int count,
1749                          struct llog_catid *logid)
1750 {
1751         struct llog_ctxt *ctxt;
1752         int rc;
1753         ENTRY;
1754
1755         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1756                             &llog_client_ops);
1757         if (rc == 0) {
1758                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1759                 ctxt->loc_imp = tgt->u.cli.cl_import;
1760         }
1761
1762         RETURN(rc);
1763 }
1764
1765 static int lmv_llog_finish(struct obd_device *obd,
1766                            struct obd_llogs *llogs, int count)
1767 {
1768         int rc;
1769         ENTRY;
1770
1771         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1772         RETURN(rc);
1773 }
1774
1775 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1776                         void *key, __u32 *vallen, void *val)
1777 {
1778         struct obd_device *obd;
1779         struct lmv_obd *lmv;
1780         int rc = 0;
1781         ENTRY;
1782
1783         obd = class_exp2obd(exp);
1784         if (obd == NULL) {
1785                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1786                        exp->exp_handle.h_cookie);
1787                 RETURN(-EINVAL);
1788         }
1789
1790         lmv = &obd->u.lmv;
1791         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1792                 __u32 *mdsize = val;
1793                 *vallen = sizeof(__u32);
1794                 *mdsize = sizeof(struct lustre_id) * lmv->desc.ld_tgt_count
1795                         + sizeof(struct mea);
1796                 RETURN(0);
1797         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1798                 struct obd_uuid *cluuid = &lmv->cluuid;
1799                 struct lmv_tgt_desc *tgts;
1800                 __u32 *mdsnum = val;
1801                 int i;
1802
1803                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
1804                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1805                                 *vallen = sizeof(__u32);
1806                                 *mdsnum = i;
1807                                 RETURN(0);
1808                         }
1809                 }
1810                 LASSERT(0);
1811         } else if (keylen == 6 && memcmp(key, "rootid", 6) == 0) {
1812                 /* getting rootid from first MDS. */
1813                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
1814                                   vallen, val);
1815                 RETURN(rc);
1816         } else if (keylen >= strlen("lmvdesc") && strcmp(key, "lmvdesc") == 0) {
1817                 struct lmv_desc *desc_ret = val;
1818                 *desc_ret = lmv->desc;
1819                 RETURN(0);
1820         }
1821
1822         CDEBUG(D_IOCTL, "invalid key\n");
1823         RETURN(-EINVAL);
1824 }
1825
1826 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1827                  void *key, obd_count vallen, void *val)
1828 {
1829         struct obd_device *obd;
1830         struct lmv_obd *lmv;
1831         ENTRY;
1832
1833         obd = class_exp2obd(exp);
1834         if (obd == NULL) {
1835                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1836                        exp->exp_handle.h_cookie);
1837                 RETURN(-EINVAL);
1838         }
1839         lmv = &obd->u.lmv;
1840
1841         if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1842                 lmv->server_timeout = 1;
1843                 lmv_set_timeouts(obd);
1844                 RETURN(0);
1845         }
1846         
1847         RETURN(-EINVAL);
1848 }
1849
1850 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
1851                struct lov_stripe_md *lsm)
1852 {
1853         struct obd_device *obd = class_exp2obd(exp);
1854         struct lmv_obd *lmv = &obd->u.lmv;
1855         struct mea *meap, *lsmp;
1856         int mea_size, i;
1857         ENTRY;
1858
1859         mea_size = (sizeof(struct lustre_id) * 
1860                     lmv->desc.ld_tgt_count) + sizeof(struct mea);
1861         if (!lmmp)
1862                 RETURN(mea_size);
1863
1864         if (*lmmp && !lsm) {
1865                 OBD_FREE(*lmmp, mea_size);
1866                 *lmmp = NULL;
1867                 RETURN(0);
1868         }
1869
1870         if (*lmmp == NULL) {
1871                 OBD_ALLOC(*lmmp, mea_size);
1872                 if (*lmmp == NULL)
1873                         RETURN(-ENOMEM);
1874         }
1875
1876         if (!lsm)
1877                 RETURN(mea_size);
1878
1879         lsmp = (struct mea *)lsm;
1880         meap = (struct mea *)*lmmp;
1881
1882         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
1883         meap->mea_count = cpu_to_le32(lsmp->mea_count);
1884         meap->mea_master = cpu_to_le32(lsmp->mea_master);
1885
1886         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1887                 meap->mea_ids[i] = meap->mea_ids[i];
1888                 id_cpu_to_le(&meap->mea_ids[i]);
1889         }
1890
1891         RETURN(mea_size);
1892 }
1893
1894 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **mem_tgt,
1895                  struct lov_mds_md *disk_src, int mdsize)
1896 {
1897         struct obd_device *obd = class_exp2obd(exp);
1898         struct mea **tmea = (struct mea **)mem_tgt;
1899         struct mea *mea = (struct mea *)disk_src;
1900         struct lmv_obd *lmv = &obd->u.lmv;
1901         int mea_size, i;
1902         ENTRY;
1903
1904         mea_size = sizeof(struct lustre_id) * 
1905                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1906         if (mem_tgt == NULL)
1907                 return mea_size;
1908
1909         if (*mem_tgt != NULL && disk_src == NULL) {
1910                 OBD_FREE(*tmea, mea_size);
1911                 RETURN(0);
1912         }
1913
1914         LASSERT(mea_size == mdsize);
1915
1916         OBD_ALLOC(*tmea, mea_size);
1917         if (*tmea == NULL)
1918                 RETURN(-ENOMEM);
1919
1920         if (!disk_src)
1921                 RETURN(mea_size);
1922
1923         (*tmea)->mea_magic = le32_to_cpu(mea->mea_magic);
1924         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
1925         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
1926
1927         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1928                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
1929                 id_le_to_cpu(&(*tmea)->mea_ids[i]);
1930         }
1931
1932         RETURN(mea_size);
1933 }
1934
1935 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
1936             struct lov_stripe_md *ea, obd_count oa_bufs,
1937             struct brw_page *pgarr, struct obd_trans_info *oti)
1938 {
1939         struct obd_device *obd = exp->exp_obd;
1940         struct lmv_obd *lmv = &obd->u.lmv;
1941         struct mea *mea = (struct mea *) ea;
1942         int err;
1943       
1944         LASSERT(oa != NULL);
1945         LASSERT(ea != NULL);
1946         LASSERT(pgarr != NULL);
1947         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1948
1949         oa->o_gr = id_gen(&mea->mea_ids[oa->o_mds]);
1950         oa->o_id = id_ino(&mea->mea_ids[oa->o_mds]);
1951         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1952         
1953         err = obd_brw(rw, lmv->tgts[oa->o_mds].ltd_exp,
1954                       oa, NULL, oa_bufs, pgarr, oti);
1955         RETURN(err);
1956 }
1957
1958 struct obd_ops lmv_obd_ops = {
1959         .o_owner                = THIS_MODULE,
1960         .o_attach               = lmv_attach,
1961         .o_detach               = lmv_detach,
1962         .o_setup                = lmv_setup,
1963         .o_cleanup              = lmv_cleanup,
1964         .o_connect              = lmv_connect,
1965         .o_disconnect           = lmv_disconnect,
1966         .o_statfs               = lmv_statfs,
1967         .o_llog_init            = lmv_llog_init,
1968         .o_llog_finish          = lmv_llog_finish,
1969         .o_get_info             = lmv_get_info,
1970         .o_set_info             = lmv_set_info,
1971         .o_create               = lmv_obd_create,
1972         .o_packmd               = lmv_packmd,
1973         .o_unpackmd             = lmv_unpackmd,
1974         .o_brw                  = lmv_brw,
1975         .o_init_ea_size         = lmv_init_ea_size,
1976         .o_notify               = lmv_notify,
1977         .o_iocontrol            = lmv_iocontrol,
1978         .o_getready             = lmv_getready,
1979 };
1980
1981 struct md_ops lmv_md_ops = {
1982         .m_getstatus           = lmv_getstatus,
1983         .m_getattr             = lmv_getattr,
1984         .m_change_cbdata       = lmv_change_cbdata,
1985         .m_change_cbdata_name  = lmv_change_cbdata_name,
1986         .m_close               = lmv_close,
1987         .m_create              = lmv_create,
1988         .m_done_writing        = lmv_done_writing,
1989         .m_enqueue             = lmv_enqueue,
1990         .m_getattr_lock        = lmv_getattr_lock,
1991         .m_intent_lock         = lmv_intent_lock,
1992         .m_link                = lmv_link,
1993         .m_rename              = lmv_rename,
1994         .m_setattr             = lmv_setattr,
1995         .m_sync                = lmv_sync,
1996         .m_readpage            = lmv_readpage,
1997         .m_unlink              = lmv_unlink,
1998         .m_get_real_obd        = lmv_get_real_obd,
1999         .m_valid_attrs         = lmv_valid_attrs,
2000         .m_delete_inode        = lmv_delete_inode,
2001 };
2002
2003 int __init lmv_init(void)
2004 {
2005         struct lprocfs_static_vars lvars;
2006         int rc;
2007
2008         obj_cache = kmem_cache_create("lmv_objects",
2009                                       sizeof(struct lmv_obj),
2010                                       0, 0, NULL, NULL);
2011         if (!obj_cache) {
2012                 CERROR("error allocating lmv objects cache\n");
2013                 return -ENOMEM;
2014         }
2015
2016         lprocfs_init_vars(lmv, &lvars);
2017         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2018                                  lvars.module_vars,
2019                                  OBD_LMV_DEVICENAME);
2020         if (rc)
2021                 kmem_cache_destroy(obj_cache);
2022         
2023         return rc;
2024 }
2025
2026 #ifdef __KERNEL__
2027 static void lmv_exit(void)
2028 {
2029         class_unregister_type(OBD_LMV_DEVICENAME);
2030
2031         LASSERTF(kmem_cache_destroy(obj_cache) == 0,
2032                  "can't free lmv objects cache, %d object(s)"
2033                  "still in use\n", atomic_read(&obj_cache_count));
2034 }
2035
2036 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2037 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2038 MODULE_LICENSE("GPL");
2039
2040 module_init(lmv_init);
2041 module_exit(lmv_exit);
2042 #endif