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