Whamcloud - gitweb
land b_hd_remote_acl: support get/set ACL from remote client.
[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, OBD_MDC_DEVICENAME) == 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, OBD_MDC_DEVICENAME)) {
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 static 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 static 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 static 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, OBD_MDC_DEVICENAME, 
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, OBD_MDC_DEVICENAME,
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 *xattr_name,
687                        const void *xattr_data, unsigned int xattr_datalen,
688                        unsigned int ea_size, struct ptlrpc_request **request)
689 {
690         struct obd_device *obd = exp->exp_obd;
691         struct lmv_obd *lmv = &obd->u.lmv;
692         int rc, i = id_group(id);
693         struct lmv_obj *obj;
694         ENTRY;
695
696         rc = lmv_check_connect(obd);
697         if (rc)
698                 RETURN(rc);
699
700         LASSERT(i < lmv->desc.ld_tgt_count);
701
702
703         rc = md_getattr(lmv->tgts[i].ltd_exp, id, valid,
704                         xattr_name, xattr_data, xattr_datalen,
705                         ea_size, request);
706         if (rc)
707                 RETURN(rc);
708         
709         obj = lmv_grab_obj(obd, id);
710         
711         CDEBUG(D_OTHER, "GETATTR for "DLID4" %s\n",
712                OLID4(id), obj ? "(splitted)" : "");
713
714         /*
715          * if object is splitted, then we loop over all the slaves and gather
716          * size attribute. In ideal world we would have to gather also mds field
717          * from all slaves, as object is spread over the cluster and this is
718          * definitely interesting information and it is not good to loss it,
719          * but...
720          */
721         if (obj) {
722                 struct mds_body *body;
723
724                 if (*request == NULL) {
725                         lmv_put_obj(obj);
726                         RETURN(rc);
727                 }
728                         
729                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
730                                       sizeof(*body));
731                 LASSERT(body != NULL);
732
733                 lmv_lock_obj(obj);
734         
735                 for (i = 0; i < obj->objcount; i++) {
736
737                         if (lmv->tgts[i].ltd_exp == NULL) {
738                                 CWARN("%s: NULL export for %d\n",
739                                       obd->obd_name, i);
740                                 continue;
741                         }
742
743                         /* skip master obj. */
744                         if (id_equal_fid(&obj->id, &obj->objs[i].id))
745                                 continue;
746                         
747                         body->size += obj->objs[i].size;
748                 }
749
750                 lmv_unlock_obj(obj);
751                 lmv_put_obj(obj);
752         }
753         
754         RETURN(rc);
755 }
756
757 static int lmv_access_check(struct obd_export *exp,
758                             struct lustre_id *id,
759                             struct ptlrpc_request **request)
760 {
761         struct obd_device *obd = exp->exp_obd;
762         struct lmv_obd *lmv = &obd->u.lmv;
763         int rc, i = id_group(id);
764         ENTRY;
765
766         rc = lmv_check_connect(obd);
767         if (rc)
768                 RETURN(rc);
769
770         LASSERT(i < lmv->desc.ld_tgt_count);
771         rc = md_access_check(lmv->tgts[i].ltd_exp, id, request);
772         RETURN(rc);
773 }
774
775 static int lmv_change_cbdata(struct obd_export *exp,
776                              struct lustre_id *id, 
777                              ldlm_iterator_t it,
778                              void *data)
779 {
780         struct obd_device *obd = exp->exp_obd;
781         struct lmv_obd *lmv = &obd->u.lmv;
782         int rc = 0;
783         ENTRY;
784         
785         rc = lmv_check_connect(obd);
786         if (rc)
787                 RETURN(rc);
788         
789         CDEBUG(D_OTHER, "CBDATA for "DLID4"\n", OLID4(id));
790         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
791
792         rc = md_change_cbdata(lmv->tgts[id_group(id)].ltd_exp,
793                               id, it, data);
794         
795         RETURN(rc);
796 }
797
798 static int lmv_change_cbdata_name(struct obd_export *exp,
799                                   struct lustre_id *pid,
800                                   char *name, int len,
801                                   struct lustre_id *cid,
802                                   ldlm_iterator_t it,
803                                   void *data)
804 {
805         struct obd_device *obd = exp->exp_obd;
806         struct lmv_obd *lmv = &obd->u.lmv;
807         struct lustre_id rcid = *cid;
808         struct lmv_obj *obj;
809         int rc = 0, mds;
810         ENTRY;
811
812         rc = lmv_check_connect(obd);
813         if (rc)
814                 RETURN(rc);
815
816         LASSERT(id_group(pid) < lmv->desc.ld_tgt_count);
817         LASSERT(id_group(cid) < lmv->desc.ld_tgt_count);
818         
819         CDEBUG(D_OTHER, "CBDATA for "DLID4":%*s -> "DLID4"\n",
820                OLID4(pid), len, name, OLID4(cid));
821
822         /* this is default mds for directory name belongs to. */
823         mds = id_group(pid);
824         obj = lmv_grab_obj(obd, pid);
825         if (obj) {
826                 /* directory is splitted. look for right mds for this name. */
827                 mds = raw_name2idx(obj->hashtype, obj->objcount, name, len);
828                 rcid = obj->objs[mds].id;
829                 mds = id_group(&rcid);
830                 lmv_put_obj(obj);
831         }
832         rc = md_change_cbdata(lmv->tgts[mds].ltd_exp, &rcid, it, data);
833         RETURN(rc);
834 }
835
836 static int lmv_valid_attrs(struct obd_export *exp, struct lustre_id *id) 
837 {
838         struct obd_device *obd = exp->exp_obd;
839         struct lmv_obd *lmv = &obd->u.lmv;
840         int rc = 0;
841         ENTRY;
842
843         rc = lmv_check_connect(obd);
844         if (rc)
845                 RETURN(rc);
846
847         CDEBUG(D_OTHER, "validate "DLID4"\n", OLID4(id));
848         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
849         rc = md_valid_attrs(lmv->tgts[id_group(id)].ltd_exp, id);
850         RETURN(rc);
851 }
852
853 static int lmv_close(struct obd_export *exp, struct obdo *obdo,
854                      struct obd_client_handle *och,
855                      struct ptlrpc_request **request)
856 {
857         struct obd_device *obd = exp->exp_obd;
858         struct lmv_obd *lmv = &obd->u.lmv;
859         int rc, i = obdo->o_mds;
860         ENTRY;
861         
862         rc = lmv_check_connect(obd);
863         if (rc)
864                 RETURN(rc);
865
866         LASSERT(i < lmv->desc.ld_tgt_count);
867         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long)obdo->o_mds,
868                (unsigned long)obdo->o_id, (unsigned long)obdo->o_generation);
869         rc = md_close(lmv->tgts[i].ltd_exp, obdo, och, request);
870         RETURN(rc);
871 }
872
873 int lmv_get_mea_and_update_object(struct obd_export *exp, 
874                                   struct lustre_id *id)
875 {
876         struct obd_device *obd = exp->exp_obd;
877         struct lmv_obd *lmv = &obd->u.lmv;
878         struct ptlrpc_request *req = NULL;
879         struct lmv_obj *obj;
880         struct lustre_md md;
881         int mealen, rc;
882         __u64 valid;
883         ENTRY;
884
885         md.mea = NULL;
886         mealen = MEA_SIZE_LMV(lmv);
887         
888         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
889
890         /* time to update mea of parent id */
891         rc = md_getattr(lmv->tgts[id_group(id)].ltd_exp,
892                         id, valid, NULL, NULL, 0, mealen, &req);
893         if (rc) {
894                 CERROR("md_getattr() failed, error %d\n", rc);
895                 GOTO(cleanup, rc);
896         }
897
898         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
899         if (rc) {
900                 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
901                 GOTO(cleanup, rc);
902         }
903
904         if (md.mea == NULL)
905                 GOTO(cleanup, rc = -ENODATA);
906
907         obj = lmv_create_obj(exp, id, md.mea);
908         if (IS_ERR(obj))
909                 rc = PTR_ERR(obj);
910         else
911                 lmv_put_obj(obj);
912
913         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
914
915         EXIT;
916 cleanup:
917         if (req)
918                 ptlrpc_req_finished(req);
919         return rc;
920 }
921
922 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
923                const void *data, int datalen, int mode, __u32 uid,
924                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
925 {
926         struct obd_device *obd = exp->exp_obd;
927         struct lmv_obd *lmv = &obd->u.lmv;
928         struct mds_body *body;
929         struct lmv_obj *obj;
930         int rc, mds, loop = 0;
931         ENTRY;
932
933         rc = lmv_check_connect(obd);
934         if (rc)
935                 RETURN(rc);
936
937         if (!lmv->desc.ld_active_tgt_count)
938                 RETURN(-EIO);
939 repeat:
940         LASSERT(++loop <= 2);
941         obj = lmv_grab_obj(obd, &op_data->id1);
942         if (obj) {
943                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
944                                    op_data->name, op_data->namelen);
945                 op_data->id1 = obj->objs[mds].id;
946                 lmv_put_obj(obj);
947         }
948
949         CDEBUG(D_OTHER, "CREATE '%*s' on "DLID4"\n", op_data->namelen,
950                op_data->name, OLID4(&op_data->id1));
951         
952         rc = md_create(lmv->tgts[id_group(&op_data->id1)].ltd_exp, 
953                        op_data, data, datalen, mode, uid, gid, rdev,
954                        request);
955         if (rc == 0) {
956                 if (*request == NULL)
957                         RETURN(rc);
958
959                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
960                                       sizeof(*body));
961                 if (body == NULL)
962                         RETURN(-ENOMEM);
963                 
964                 CDEBUG(D_OTHER, "created. "DLID4"\n", OLID4(&op_data->id1));
965         } else if (rc == -ERESTART) {
966                 /*
967                  * directory got splitted. time to update local object and
968                  * repeat the request with proper MDS.
969                  */
970                 rc = lmv_get_mea_and_update_object(exp, &op_data->id1);
971                 if (rc == 0) {
972                         ptlrpc_req_finished(*request);
973                         goto repeat;
974                 }
975         }
976         RETURN(rc);
977 }
978
979 static int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
980 {
981         struct obd_device *obd = exp->exp_obd;
982         struct lmv_obd *lmv = &obd->u.lmv;
983         int rc;
984         ENTRY;
985         
986         rc = lmv_check_connect(obd);
987         if (rc)
988                 RETURN(rc);
989
990         /* FIXME: choose right MDC here */
991         CWARN("this method isn't implemented yet\n");
992         rc = md_done_writing(lmv->tgts[0].ltd_exp, obdo);
993         RETURN(rc);
994 }
995
996 static int
997 lmv_enqueue_slaves(struct obd_export *exp, int locktype,
998                    struct lookup_intent *it, int lockmode,
999                    struct mdc_op_data *data, struct lustre_handle *lockh,
1000                    void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1001                    ldlm_blocking_callback cb_blocking, void *cb_data)
1002 {
1003         struct obd_device *obd = exp->exp_obd;
1004         struct lmv_obd *lmv = &obd->u.lmv;
1005         struct mea *mea = data->mea1;
1006         struct mdc_op_data *data2;
1007         int i, rc, mds;
1008         ENTRY;
1009
1010         OBD_ALLOC(data2, sizeof(*data2));
1011         if (data2 == NULL)
1012                 RETURN(-ENOMEM);
1013         
1014         LASSERT(mea != NULL);
1015         for (i = 0; i < mea->mea_count; i++) {
1016                 memset(data2, 0, sizeof(*data2));
1017                 data2->id1 = mea->mea_ids[i];
1018                 mds = id_group(&data2->id1);
1019                 
1020                 if (lmv->tgts[mds].ltd_exp == NULL)
1021                         continue;
1022
1023                 rc = md_enqueue(lmv->tgts[mds].ltd_exp, locktype, it, 
1024                                 lockmode, data2, lockh + i, lmm, lmmsize, 
1025                                 cb_compl, cb_blocking, cb_data);
1026                 
1027                 CDEBUG(D_OTHER, "take lock on slave "DLID4" -> %d/%d\n",
1028                        OLID4(&mea->mea_ids[i]), rc, LUSTRE_IT(it)->it_status);
1029                 if (rc)
1030                         GOTO(cleanup, rc);
1031                 if (LUSTRE_IT(it)->it_data) {
1032                         struct ptlrpc_request *req;
1033                         req = (struct ptlrpc_request *) LUSTRE_IT(it)->it_data;
1034                         ptlrpc_req_finished(req);
1035                 }
1036                 
1037                 if (LUSTRE_IT(it)->it_status)
1038                         GOTO(cleanup, rc = LUSTRE_IT(it)->it_status);
1039         }
1040         
1041         OBD_FREE(data2, sizeof(*data2));
1042         RETURN(0);
1043 cleanup:
1044         OBD_FREE(data2, sizeof(*data2));
1045         
1046         /* drop all taken locks */
1047         while (--i >= 0) {
1048                 if (lockh[i].cookie)
1049                         ldlm_lock_decref(lockh + i, lockmode);
1050                 lockh[i].cookie = 0;
1051         }
1052         return rc;
1053 }
1054
1055 static int
1056 lmv_enqueue_remote(struct obd_export *exp, int lock_type,
1057                    struct lookup_intent *it, int lock_mode,
1058                    struct mdc_op_data *data, struct lustre_handle *lockh,
1059                    void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1060                    ldlm_blocking_callback cb_blocking, void *cb_data)
1061 {
1062         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
1063         struct obd_device *obd = exp->exp_obd;
1064         struct lmv_obd *lmv = &obd->u.lmv;
1065         struct lustre_handle plock;
1066         struct mdc_op_data rdata;
1067         struct mds_body *body = NULL;
1068         int rc = 0, pmode;
1069         ENTRY;
1070
1071         body = lustre_msg_buf(req->rq_repmsg, 1, sizeof(*body));
1072         LASSERT(body != NULL);
1073
1074         if (!(body->valid & OBD_MD_MDS))
1075                 RETURN(0);
1076
1077         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DLID4" -> "DLID4"\n",
1078                LL_IT2STR(it), OLID4(&data->id1), OLID4(&body->id1));
1079
1080         /* we got LOOKUP lock, but we really need attrs */
1081         pmode = LUSTRE_IT(it)->it_lock_mode;
1082         LASSERT(pmode != 0);
1083         memcpy(&plock, lockh, sizeof(plock));
1084         LUSTRE_IT(it)->it_lock_mode = 0;
1085         LUSTRE_IT(it)->it_data = NULL;
1086         LASSERT((body->valid & OBD_MD_FID) != 0);
1087
1088         memcpy(&rdata, data, sizeof(rdata));
1089         rdata.id1 = body->id1;
1090         rdata.name = NULL;
1091         rdata.namelen = 0;
1092
1093         LUSTRE_IT(it)->it_disposition &= ~DISP_ENQ_COMPLETE;
1094         ptlrpc_req_finished(req);
1095
1096         rc = md_enqueue(lmv->tgts[id_group(&rdata.id1)].ltd_exp, 
1097                         lock_type, it, lock_mode, &rdata, lockh, lmm, 
1098                         lmmsize, cb_compl, cb_blocking, cb_data);
1099         ldlm_lock_decref(&plock, pmode);
1100         RETURN(rc);
1101 }
1102
1103 static int
1104 lmv_enqueue(struct obd_export *exp, int lock_type,
1105             struct lookup_intent *it, int lock_mode,
1106             struct mdc_op_data *data, struct lustre_handle *lockh,
1107             void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1108             ldlm_blocking_callback cb_blocking, void *cb_data)
1109 {
1110         struct obd_device *obd = exp->exp_obd;
1111         struct lmv_obd *lmv = &obd->u.lmv;
1112         struct lmv_obj *obj;
1113         int rc, mds;
1114         ENTRY;
1115
1116         rc = lmv_check_connect(obd);
1117         if (rc)
1118                 RETURN(rc);
1119
1120         if (data->mea1 && it->it_op == IT_UNLINK) {
1121                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
1122                                         data, lockh, lmm, lmmsize,
1123                                         cb_compl, cb_blocking, cb_data);
1124                 RETURN(rc);
1125         }
1126
1127         if (data->namelen) {
1128                 obj = lmv_grab_obj(obd, &data->id1);
1129                 if (obj) {
1130                         /* directory is splitted. look for right mds for this
1131                          * name */
1132                         mds = raw_name2idx(obj->hashtype, obj->objcount,
1133                                            (char *)data->name, data->namelen);
1134                         data->id1 = obj->objs[mds].id;
1135                         lmv_put_obj(obj);
1136                 }
1137         }
1138         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DLID4"\n", LL_IT2STR(it),
1139                OLID4(&data->id1));
1140         
1141         rc = md_enqueue(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1142                         lock_type, it, lock_mode, data, lockh, lmm, 
1143                         lmmsize, cb_compl, cb_blocking, cb_data);
1144         if (rc == 0 && it->it_op == IT_OPEN)
1145                 rc = lmv_enqueue_remote(exp, lock_type, it, lock_mode,
1146                                         data, lockh, lmm, lmmsize,
1147                                         cb_compl, cb_blocking, cb_data);
1148         RETURN(rc);
1149 }
1150
1151 static int
1152 lmv_getattr_lock(struct obd_export *exp, struct lustre_id *id,
1153                  char *filename, int namelen, __u64 valid,
1154                  unsigned int ea_size, struct ptlrpc_request **request)
1155 {
1156         int rc, mds = id_group(id), loop = 0;
1157         struct obd_device *obd = exp->exp_obd;
1158         struct lmv_obd *lmv = &obd->u.lmv;
1159         struct lustre_id rid = *id;
1160         struct mds_body *body;
1161         struct lmv_obj *obj;
1162         ENTRY;
1163         
1164         rc = lmv_check_connect(obd);
1165         if (rc)
1166                 RETURN(rc);
1167 repeat:
1168         LASSERT(++loop <= 2);
1169         obj = lmv_grab_obj(obd, id);
1170         if (obj) {
1171                 /* directory is splitted. look for right mds for this name */
1172                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1173                                    filename, namelen - 1);
1174                 rid = obj->objs[mds].id;
1175                 lmv_put_obj(obj);
1176         }
1177         
1178         CDEBUG(D_OTHER, "getattr_lock for %*s on "DLID4" -> "DLID4"\n",
1179                namelen, filename, OLID4(id), OLID4(&rid));
1180
1181         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp,
1182                              &rid, filename, namelen,
1183                              valid == OBD_MD_FLID ? valid : valid | OBD_MD_FID,
1184                              ea_size, request);
1185         if (rc == 0) {
1186                 /*
1187                  * this could be cross-node reference. in this case all we have
1188                  * right now is lustre_id triple. we'd like to find other
1189                  * attributes.
1190                  */
1191                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
1192                 LASSERT(body != NULL);
1193                 LASSERT((body->valid & OBD_MD_FID) != 0
1194                                 || body->valid == OBD_MD_FLID);
1195
1196                 if (body->valid & OBD_MD_MDS) {
1197                         struct ptlrpc_request *req = NULL;
1198                         
1199                         rid = body->id1;
1200                         CDEBUG(D_OTHER, "request attrs for "DLID4"\n", OLID4(&rid));
1201
1202                         rc = md_getattr_lock(lmv->tgts[id_group(&rid)].ltd_exp, 
1203                                              &rid, NULL, 1, valid, ea_size, &req);
1204                         ptlrpc_req_finished(*request);
1205                         *request = req;
1206                 }
1207         } else if (rc == -ERESTART) {
1208                 /* directory got splitted. time to update local object and
1209                  * repeat the request with proper MDS */
1210                 rc = lmv_get_mea_and_update_object(exp, &rid);
1211                 if (rc == 0) {
1212                         ptlrpc_req_finished(*request);
1213                         goto repeat;
1214                 }
1215         }
1216         RETURN(rc);
1217 }
1218
1219 /*
1220  * llite passes id of an target inode in data->id1 and id of directory in
1221  * data->id2
1222  */
1223 static int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
1224                     struct ptlrpc_request **request)
1225 {
1226         struct obd_device *obd = exp->exp_obd;
1227         struct lmv_obd *lmv = &obd->u.lmv;
1228         struct lmv_obj *obj;
1229         int rc, mds;
1230         ENTRY;
1231         
1232         rc = lmv_check_connect(obd);
1233         if (rc)
1234                 RETURN(rc);
1235
1236         if (data->namelen != 0) {
1237                 /* usual link request */
1238                 obj = lmv_grab_obj(obd, &data->id2);
1239                 if (obj) {
1240                         rc = raw_name2idx(obj->hashtype, obj->objcount, 
1241                                           data->name, data->namelen);
1242                         data->id2 = obj->objs[rc].id;
1243                         lmv_put_obj(obj);
1244                 }
1245
1246                 mds = id_group(&data->id2);
1247                 
1248                 CDEBUG(D_OTHER,"link "DLID4":%*s to "DLID4"\n",
1249                        OLID4(&data->id2), data->namelen, data->name,
1250                        OLID4(&data->id1));
1251         } else {
1252                 mds = id_group(&data->id1);
1253                 
1254                 /* request from MDS to acquire i_links for inode by id1 */
1255                 CDEBUG(D_OTHER, "inc i_nlinks for "DLID4"\n",
1256                        OLID4(&data->id1));
1257         }
1258
1259         CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n",
1260                mds, OLID4(&data->id1));
1261         rc = md_link(lmv->tgts[mds].ltd_exp, data, request);
1262         
1263         RETURN(rc);
1264 }
1265
1266 static int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
1267                       const char *old, int oldlen, const char *new, int newlen,
1268                       struct ptlrpc_request **request)
1269 {
1270         struct obd_device *obd = exp->exp_obd;
1271         struct lmv_obd *lmv = &obd->u.lmv;
1272         struct lmv_obj *obj;
1273         int rc, mds;
1274         ENTRY;
1275
1276         CDEBUG(D_OTHER, "rename %*s in "DLID4" to %*s in "DLID4"\n",
1277                oldlen, old, OLID4(&data->id1), newlen, new,
1278                OLID4(&data->id2));
1279
1280         rc = lmv_check_connect(obd);
1281         if (rc)
1282                 RETURN(rc);
1283
1284         if (oldlen == 0) {
1285                 /*
1286                  * MDS with old dir entry is asking another MDS to create name
1287                  * there.
1288                  */
1289                 CDEBUG(D_OTHER,
1290                        "create %*s(%d/%d) in "DLID4" pointing "
1291                        "to "DLID4"\n", newlen, new, oldlen, newlen,
1292                        OLID4(&data->id2), OLID4(&data->id1));
1293
1294                 mds = id_group(&data->id2);
1295
1296                 /* 
1297                  * target directory can be splitted, sowe should forward request
1298                  * to the right MDS.
1299                  */
1300                 obj = lmv_grab_obj(obd, &data->id2);
1301                 if (obj) {
1302                         mds = raw_name2idx(obj->hashtype, obj->objcount, 
1303                                            (char *)new, newlen);
1304                         data->id2 = obj->objs[mds].id;
1305                         CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1306                                OLID4(&data->id2));
1307                         lmv_put_obj(obj);
1308                 }
1309                 goto request;
1310         }
1311
1312         obj = lmv_grab_obj(obd, &data->id1);
1313         if (obj) {
1314                 /*
1315                  * directory is already splitted, so we have to forward request
1316                  * to the right MDS.
1317                  */
1318                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1319                                    (char *)old, oldlen);
1320                 data->id1 = obj->objs[mds].id;
1321                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1322                        OLID4(&data->id1));
1323                 lmv_put_obj(obj);
1324         }
1325
1326         obj = lmv_grab_obj(obd, &data->id2);
1327         if (obj) {
1328                 /*
1329                  * directory is already splitted, so we have to forward request
1330                  * to the right MDS.
1331                  */
1332                 mds = raw_name2idx(obj->hashtype, obj->objcount, 
1333                                    (char *)new, newlen);
1334                 
1335                 data->id2 = obj->objs[mds].id;
1336                 CDEBUG(D_OTHER, "forward to MDS #%u ("DLID4")\n", mds,
1337                        OLID4(&data->id2));
1338                 lmv_put_obj(obj);
1339         }
1340         
1341         mds = id_group(&data->id1);
1342
1343 request:
1344         if (id_group(&data->id1) != id_group(&data->id2)) {
1345                 CDEBUG(D_OTHER,"cross-node rename "DLID4"/%*s to "DLID4"/%*s\n",
1346                        OLID4(&data->id1), oldlen, old, OLID4(&data->id2),
1347                        newlen, new);
1348         }
1349
1350         rc = md_rename(lmv->tgts[mds].ltd_exp, data, old, oldlen,
1351                        new, newlen, request); 
1352         RETURN(rc);
1353 }
1354
1355 static int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
1356                        struct iattr *iattr, void *ea, int ealen, void *ea2,
1357                        int ea2len, struct ptlrpc_request **request)
1358 {
1359         struct obd_device *obd = exp->exp_obd;
1360         struct lmv_obd *lmv = &obd->u.lmv;
1361         struct ptlrpc_request *req;
1362         struct mds_body *body;
1363         struct lmv_obj *obj;
1364         int rc = 0, i;
1365         ENTRY;
1366
1367         rc = lmv_check_connect(obd);
1368         if (rc)
1369                 RETURN(rc);
1370
1371         obj = lmv_grab_obj(obd, &data->id1);
1372         
1373         CDEBUG(D_OTHER, "SETATTR for "DLID4", valid 0x%x%s\n",
1374                OLID4(&data->id1), iattr->ia_valid, obj ? ", splitted" : "");
1375         
1376         if (obj) {
1377                 for (i = 0; i < obj->objcount; i++) {
1378                         data->id1 = obj->objs[i].id;
1379                         
1380                         rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1381                                         data, iattr, ea, ealen, ea2, ea2len, &req);
1382
1383                         if (id_equal_fid(&obj->id, &obj->objs[i].id)) {
1384                                 /*
1385                                  * this is master object and this request should
1386                                  * be returned back to llite.
1387                                  */
1388                                 *request = req;
1389                         } else {
1390                                 ptlrpc_req_finished(req);
1391                         }
1392
1393                         if (rc)
1394                                 break;
1395                 }
1396                 lmv_put_obj(obj);
1397         } else {
1398                 LASSERT(id_group(&data->id1) < lmv->desc.ld_tgt_count);
1399                 rc = md_setattr(lmv->tgts[id_group(&data->id1)].ltd_exp,
1400                                 data, iattr, ea, ealen, ea2, ea2len, request); 
1401                 if (rc == 0) {
1402                         body = lustre_msg_buf((*request)->rq_repmsg, 0,
1403                                               sizeof(*body));
1404                         LASSERT(body != NULL);
1405                         LASSERT((body->valid & OBD_MD_FID) != 0);
1406                         LASSERT(id_group(&body->id1) == id_group(&data->id1));
1407                 }
1408         }
1409         RETURN(rc);
1410 }
1411
1412 static int lmv_sync(struct obd_export *exp, struct lustre_id *id,
1413                     struct ptlrpc_request **request)
1414 {
1415         struct obd_device *obd = exp->exp_obd;
1416         struct lmv_obd *lmv = &obd->u.lmv;
1417         int rc;
1418         ENTRY;
1419
1420         rc = lmv_check_connect(obd);
1421         if (rc)
1422                 RETURN(rc);
1423
1424         rc = md_sync(lmv->tgts[id_group(id)].ltd_exp, 
1425                      id, request);
1426         RETURN(rc);
1427 }
1428
1429 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock, 
1430                             struct ldlm_lock_desc *desc,
1431                             void *data, int flag)
1432 {
1433         struct lustre_handle lockh;
1434         struct lmv_obj *obj;
1435         int rc;
1436         ENTRY;
1437
1438         switch (flag) {
1439         case LDLM_CB_BLOCKING:
1440                 ldlm_lock2handle(lock, &lockh);
1441                 rc = ldlm_cli_cancel(&lockh);
1442                 if (rc < 0) {
1443                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1444                         RETURN(rc);
1445                 }
1446                 break;
1447         case LDLM_CB_CANCELING:
1448                 /* time to drop cached attrs for dirobj */
1449                 obj = lock->l_ast_data;
1450                 if (obj) {
1451                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
1452                                ", master "DLID4"\n",
1453                                lock->l_resource->lr_name.name[3] == 1 ?
1454                                "LOOKUP" : "UPDATE",
1455                                lock->l_resource->lr_name.name[0],
1456                                lock->l_resource->lr_name.name[1], 
1457                                OLID4(&obj->id));
1458                         lmv_put_obj(obj);
1459                 }
1460                 break;
1461         default:
1462                 LBUG();
1463         }
1464         RETURN(0);
1465 }
1466
1467 static void lmv_remove_dots(struct page *page)
1468 {
1469         unsigned limit = PAGE_CACHE_SIZE;
1470         char *kaddr = page_address(page);
1471         struct ext2_dir_entry_2 *p;
1472         unsigned offs, rec_len;
1473
1474         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
1475                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
1476                 rec_len = le16_to_cpu(p->rec_len);
1477
1478                 if ((p->name_len == 1 && p->name[0] == '.') ||
1479                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
1480                         p->inode = 0;
1481         }
1482 }
1483
1484 static int lmv_readpage(struct obd_export *exp, struct lustre_id *id,
1485                         __u64 offset, struct page *page,
1486                         struct ptlrpc_request **request)
1487 {
1488         struct obd_device *obd = exp->exp_obd;
1489         struct lmv_obd *lmv = &obd->u.lmv;
1490         struct lustre_id rid = *id;
1491         struct lmv_obj *obj;
1492         int rc, i;
1493         ENTRY;
1494
1495 #warning "we need well-desgined readdir() implementation"
1496         rc = lmv_check_connect(obd);
1497         if (rc)
1498                 RETURN(rc);
1499
1500         LASSERT(id_group(id) < lmv->desc.ld_tgt_count);
1501         CDEBUG(D_OTHER, "READPAGE at %llu from "DLID4"\n",
1502                offset, OLID4(&rid));
1503
1504         obj = lmv_grab_obj(obd, id);
1505         if (obj) {
1506                 lmv_lock_obj(obj);
1507
1508                 /* find dirobj containing page with requested offset. */
1509                 for (i = 0; i < obj->objcount; i++) {
1510                         if (offset < obj->objs[i].size)
1511                                 break;
1512                         offset -= obj->objs[i].size;
1513                 }
1514                 rid = obj->objs[i].id;
1515                 
1516                 lmv_unlock_obj(obj);
1517                 lmv_put_obj(obj);
1518                 
1519                 CDEBUG(D_OTHER, "forward to "DLID4" with offset %lu\n",
1520                        OLID4(&rid), (unsigned long)offset);
1521         }
1522         rc = md_readpage(lmv->tgts[id_group(&rid)].ltd_exp, &rid, 
1523                          offset, page, request);
1524         
1525         if (rc == 0 && !id_equal_fid(&rid, id))
1526                 /* this page isn't from master object. To avoid "." and ".." 
1527                  * duplication in directory, we have to remove them from all
1528                  * slave objects */
1529                 lmv_remove_dots(page);
1530         
1531         RETURN(rc);
1532 }
1533
1534 static int lmv_unlink_slaves(struct obd_export *exp, struct mdc_op_data *data,
1535                              struct ptlrpc_request **req)
1536 {
1537         struct obd_device *obd = exp->exp_obd;
1538         struct lmv_obd *lmv = &obd->u.lmv;
1539         struct mea *mea = data->mea1;
1540         struct mdc_op_data *data2;
1541         int i, rc = 0;
1542         ENTRY;
1543
1544         OBD_ALLOC(data2, sizeof(*data2));
1545         if (data2 == NULL)
1546                 RETURN(-ENOMEM);
1547         
1548         LASSERT(mea != NULL);
1549         for (i = 0; i < mea->mea_count; i++) {
1550                 memset(data2, 0, sizeof(*data2));
1551                 data2->id1 = mea->mea_ids[i];
1552                 data2->create_mode = MDS_MODE_DONT_LOCK | S_IFDIR;
1553                 
1554                 if (lmv->tgts[id_group(&data2->id1)].ltd_exp == NULL)
1555                         continue;
1556
1557                 rc = md_unlink(lmv->tgts[id_group(&data2->id1)].ltd_exp,
1558                                data2, req);
1559                 
1560                 CDEBUG(D_OTHER, "unlink slave "DLID4" -> %d\n",
1561                        OLID4(&mea->mea_ids[i]), rc);
1562                 
1563                 if (*req) {
1564                         ptlrpc_req_finished(*req);
1565                         *req = NULL;
1566                 }
1567                 if (rc)
1568                         RETURN(rc);
1569         }
1570         OBD_FREE(data2, sizeof(*data2));
1571         RETURN(rc);
1572 }
1573
1574 static int lmv_delete_inode(struct obd_export *exp, struct lustre_id *id)
1575 {
1576         ENTRY;
1577
1578         LASSERT(exp && id);
1579         if (lmv_delete_obj(exp, id)) {
1580                 CDEBUG(D_OTHER, "lmv object "DLID4" is destroyed.\n",
1581                        OLID4(id));
1582         }
1583         RETURN(0);
1584 }
1585
1586 static int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
1587                       struct ptlrpc_request **request)
1588 {
1589         struct obd_device *obd = exp->exp_obd;
1590         struct lmv_obd *lmv = &obd->u.lmv;
1591         int rc, i = 0;
1592         ENTRY;
1593         
1594         rc = lmv_check_connect(obd);
1595         if (rc)
1596                 RETURN(rc);
1597
1598         if (data->namelen == 0 && data->mea1 != NULL) {
1599                 /* mds asks to remove slave objects */
1600                 rc = lmv_unlink_slaves(exp, data, request);
1601                 RETURN(rc);
1602         }
1603
1604         if (data->namelen != 0) {
1605                 struct lmv_obj *obj;
1606                 
1607                 obj = lmv_grab_obj(obd, &data->id1);
1608                 if (obj) {
1609                         i = raw_name2idx(obj->hashtype, obj->objcount,
1610                                          data->name, data->namelen);
1611                         data->id1 = obj->objs[i].id;
1612                         lmv_put_obj(obj);
1613                 }
1614                 CDEBUG(D_OTHER, "unlink '%*s' in "DLID4" -> %u\n",
1615                        data->namelen, data->name, OLID4(&data->id1),
1616                        i);
1617         } else {
1618                 CDEBUG(D_OTHER, "drop i_nlink on "DLID4"\n",
1619                        OLID4(&data->id1));
1620         }
1621         rc = md_unlink(lmv->tgts[id_group(&data->id1)].ltd_exp, 
1622                        data, request);
1623         RETURN(rc);
1624 }
1625
1626 static struct obd_device *lmv_get_real_obd(struct obd_export *exp,
1627                                            struct lustre_id *id)
1628 {
1629         struct obd_device *obd = exp->exp_obd;
1630         struct lmv_obd *lmv = &obd->u.lmv;
1631         int rc;
1632         ENTRY;
1633
1634         rc = lmv_check_connect(obd);
1635         if (rc)
1636                 RETURN(ERR_PTR(rc));
1637         obd = lmv->tgts[id_group(id)].ltd_exp->exp_obd;
1638         EXIT;
1639         
1640         return obd;
1641 }
1642
1643 static int lmv_init_ea_size(struct obd_export *exp, int easize,
1644                             int cookiesize)
1645 {
1646         struct obd_device *obd = exp->exp_obd;
1647         struct lmv_obd *lmv = &obd->u.lmv;
1648         int i, rc = 0, change = 0;
1649         ENTRY;
1650
1651         if (lmv->max_easize < easize) {
1652                 lmv->max_easize = easize;
1653                 change = 1;
1654         }
1655         if (lmv->max_cookiesize < cookiesize) {
1656                 lmv->max_cookiesize = cookiesize;
1657                 change = 1;
1658         }
1659         if (change == 0)
1660                 RETURN(0);
1661         
1662         if (lmv->connected == 0)
1663                 RETURN(0);
1664
1665         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1666                 if (lmv->tgts[i].ltd_exp == NULL) {
1667                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
1668                         continue;
1669                 }
1670
1671                 rc = obd_init_ea_size(lmv->tgts[i].ltd_exp, easize, cookiesize);
1672                 if (rc) {
1673                         CERROR("obd_init_ea_size() failed on MDT target %d, "
1674                                "error %d.\n", i, rc);
1675                         break;
1676                 }
1677         }
1678         RETURN(rc);
1679 }
1680
1681 static int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1682                                  void *acl, int acl_size,
1683                                  struct lov_stripe_md **ea,
1684                                  struct obd_trans_info *oti)
1685 {
1686         struct obd_device *obd = exp->exp_obd;
1687         struct lmv_obd *lmv = &obd->u.lmv;
1688         struct lov_stripe_md obj_md;
1689         struct lov_stripe_md *obj_mdp = &obj_md;
1690         int rc = 0;
1691         ENTRY;
1692
1693         LASSERT(ea == NULL);
1694         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1695
1696         rc = obd_create(lmv->tgts[oa->o_mds].ltd_exp, oa,
1697                         acl, acl_size, &obj_mdp, oti);
1698
1699         RETURN(rc);
1700 }
1701
1702 /*
1703  * to be called from MDS only. @oa should have correct store cookie and o_fid
1704  * values for "master" object, as it will be used.
1705  */
1706 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1707                    void *acl, int acl_size,
1708                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1709 {
1710         struct obd_device *obd = exp->exp_obd;
1711         struct lmv_obd *lmv = &obd->u.lmv;
1712         struct lustre_id mid;
1713         int i, c, rc = 0;
1714         struct mea *mea;
1715         ENTRY;
1716
1717         rc = lmv_check_connect(obd);
1718         if (rc)
1719                 RETURN(rc);
1720
1721         LASSERT(oa != NULL);
1722         
1723         if (ea == NULL) {
1724                 rc = lmv_obd_create_single(exp, oa, acl, acl_size, NULL, oti);
1725                 if (rc)
1726                         CERROR("Can't create object, rc = %d\n", rc);
1727                 RETURN(rc);
1728         }
1729
1730         /* acl is only suppied when mds create single remote obj */
1731         LASSERT(acl == NULL && acl_size == 0);
1732
1733         if (*ea == NULL) {
1734                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1735                 if (rc < 0) {
1736                         CERROR("obd_alloc_diskmd() failed, error %d\n",
1737                                rc);
1738                         RETURN(rc);
1739                 } else
1740                         rc = 0;
1741                 
1742                 if (*ea == NULL)
1743                         RETURN(-ENOMEM);
1744         }
1745
1746         /* 
1747          * here we should take care about splitted dir, so store cookie and fid
1748          * for "master" object should already be allocated and passed in @oa.
1749          */
1750         LASSERT(oa->o_id != 0);
1751         LASSERT(oa->o_fid != 0);
1752
1753         /* save "master" object id */
1754         obdo2id(&mid, oa);
1755
1756         mea = (struct mea *)*ea;
1757         mea->mea_master = -1;
1758         mea->mea_magic = MEA_MAGIC_ALL_CHARS;
1759
1760         if (!mea->mea_count || mea->mea_count > lmv->desc.ld_tgt_count)
1761                 mea->mea_count = lmv->desc.ld_tgt_count;
1762
1763         for (i = 0, c = 0; c < mea->mea_count && i < lmv->desc.ld_tgt_count; i++) {
1764                 struct lov_stripe_md obj_md;
1765                 struct lov_stripe_md *obj_mdp = &obj_md;
1766                
1767                 if (lmv->tgts[i].ltd_exp == NULL) {
1768                         /* this is "master" MDS */
1769                         mea->mea_master = i;
1770                         mea->mea_ids[c] = mid;
1771                         c++;
1772                         continue;
1773                 }
1774
1775                 /*
1776                  * "master" MDS should always be part of stripped dir,
1777                  * so scan for it.
1778                  */
1779                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1780                         continue;
1781
1782                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE |
1783                         OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1784
1785                 rc = obd_create(lmv->tgts[c].ltd_exp, oa, NULL, 0,
1786                                 &obj_mdp, oti);
1787                 if (rc) {
1788                         CERROR("obd_create() failed on MDT target %d, "
1789                                "error %d\n", c, rc);
1790                         RETURN(rc);
1791                 }
1792
1793                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1794                        i, oa->o_id, oa->o_generation);
1795
1796
1797                 /*
1798                  * here, when object is created (or it is master and was passed
1799                  * from caller) on desired MDS we save its fid to local mea_ids.
1800                  */
1801                 LASSERT(oa->o_fid);
1802
1803                 /* 
1804                  * store cookie should be defined here for both cases (master
1805                  * object and not master), because master is already created.
1806                  */
1807                 LASSERT(oa->o_id);
1808
1809                 /* fill mea by store cookie and fid */
1810                 obdo2id(&mea->mea_ids[c], oa);
1811                 c++;
1812         }
1813         LASSERT(c == mea->mea_count);
1814
1815         CDEBUG(D_OTHER, "%d dirobjects created\n",
1816                (int)mea->mea_count);
1817         
1818         RETURN(rc);
1819 }
1820
1821 static int lmv_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
1822                          struct obd_device *tgt, int count,
1823                          struct llog_catid *logid)
1824 {
1825         struct llog_ctxt *ctxt;
1826         int rc;
1827         ENTRY;
1828
1829         rc = obd_llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1830                             &llog_client_ops);
1831         if (rc == 0) {
1832                 ctxt = llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT);
1833                 ctxt->loc_imp = tgt->u.cli.cl_import;
1834         }
1835
1836         RETURN(rc);
1837 }
1838
1839 static int lmv_llog_finish(struct obd_device *obd,
1840                            struct obd_llogs *llogs, int count)
1841 {
1842         int rc;
1843         ENTRY;
1844
1845         rc = obd_llog_cleanup(llog_get_context(llogs, LLOG_CONFIG_REPL_CTXT));
1846         RETURN(rc);
1847 }
1848
1849 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1850                         void *key, __u32 *vallen, void *val)
1851 {
1852         struct obd_device *obd;
1853         struct lmv_obd *lmv;
1854         int rc = 0;
1855         ENTRY;
1856
1857         obd = class_exp2obd(exp);
1858         if (obd == NULL) {
1859                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1860                        exp->exp_handle.h_cookie);
1861                 RETURN(-EINVAL);
1862         }
1863
1864         lmv = &obd->u.lmv;
1865         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1866                 __u32 *mdsize = val;
1867                 *vallen = sizeof(__u32);
1868                 *mdsize = sizeof(struct lustre_id) * lmv->desc.ld_tgt_count
1869                         + sizeof(struct mea);
1870                 RETURN(0);
1871         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1872                 struct obd_uuid *cluuid = &lmv->cluuid;
1873                 struct lmv_tgt_desc *tgts;
1874                 __u32 *mdsnum = val;
1875                 int i;
1876
1877                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
1878                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1879                                 *vallen = sizeof(__u32);
1880                                 *mdsnum = i;
1881                                 RETURN(0);
1882                         }
1883                 }
1884                 LASSERT(0);
1885         } else if (keylen == 6 && memcmp(key, "rootid", 6) == 0) {
1886                 rc = lmv_check_connect(obd);
1887                 if (rc)
1888                         RETURN(rc);
1889                 
1890                 /* getting rootid from first MDS. */
1891                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
1892                                   vallen, val);
1893                 RETURN(rc);
1894         } else if (keylen >= strlen("lmvdesc") && strcmp(key, "lmvdesc") == 0) {
1895                 struct lmv_desc *desc_ret = val;
1896                 *desc_ret = lmv->desc;
1897                 RETURN(0);
1898         } else if (keylen == strlen("remote_flag") &&
1899                    !strcmp(key, "remote_flag")) {
1900                 struct lmv_tgt_desc *tgts;
1901                 int i;
1902
1903                 rc = lmv_check_connect(obd);
1904                 if (rc)
1905                         RETURN(rc);
1906                 
1907                 LASSERT(*vallen == sizeof(__u32));
1908                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
1909                      i++, tgts++) {
1910
1911                         /* all tgts should be connected when this get called. */
1912                         if (!tgts || !tgts->ltd_exp) {
1913                                 CERROR("target not setup?\n");
1914                                 continue;
1915                         }
1916
1917                         if (!obd_get_info(tgts->ltd_exp, keylen, key,
1918                                           vallen, val))
1919                                 RETURN(0);
1920                 }
1921                 RETURN(-EINVAL);
1922         } else if (keylen >= strlen("lovdesc") && strcmp(key, "lovdesc") == 0) {
1923                 rc = lmv_check_connect(obd);
1924                 if (rc)
1925                         RETURN(rc);
1926
1927                 /* forwarding this request to first MDS, it should know LOV
1928                  * desc. */
1929                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
1930                                   vallen, val);
1931                 RETURN(rc);
1932         }
1933
1934         CDEBUG(D_IOCTL, "invalid key\n");
1935         RETURN(-EINVAL);
1936 }
1937
1938 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1939                  void *key, obd_count vallen, void *val)
1940 {
1941         struct lmv_tgt_desc    *tgt;
1942         struct obd_device      *obd;
1943         struct lmv_obd         *lmv;
1944         ENTRY;
1945
1946         obd = class_exp2obd(exp);
1947         if (obd == NULL) {
1948                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1949                        exp->exp_handle.h_cookie);
1950                 RETURN(-EINVAL);
1951         }
1952         lmv = &obd->u.lmv;
1953
1954         if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1955                 lmv->server_timeout = 1;
1956                 lmv_set_timeouts(obd);
1957                 RETURN(0);
1958         }
1959
1960         /* maybe this could be default */
1961         if ((keylen == strlen("sec") && strcmp(key, "sec") == 0) ||
1962             (keylen == strlen("sec_flags") && strcmp(key, "sec_flags") == 0) ||
1963             (keylen == strlen("nllu") && strcmp(key, "nllu") == 0)) {
1964                 struct obd_export *exp;
1965                 int rc = 0, err, i;
1966
1967                 spin_lock(&lmv->lmv_lock);
1968                 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count;
1969                      i++, tgt++) {
1970                         exp = tgt->ltd_exp;
1971                         /* during setup time the connections to mdc might
1972                          * haven't been established.
1973                          */
1974                         if (exp == NULL) {
1975                                 struct obd_device *tgt_obd;
1976
1977                                 tgt_obd = class_find_client_obd(&tgt->uuid,
1978                                                                 OBD_MDC_DEVICENAME,
1979                                                                 &obd->obd_uuid);
1980                                 if (!tgt_obd) {
1981                                         CERROR("can't set info %s, "
1982                                                "device %s not attached?\n",
1983                                                 (char *) key, tgt->uuid.uuid);
1984                                         rc = -EINVAL;
1985                                         continue;
1986                                 }
1987                                 exp = tgt_obd->obd_self_export;
1988                         }
1989
1990                         err = obd_set_info(exp, keylen, key, vallen, val);
1991                         if (!rc)
1992                                 rc = err;
1993                 }
1994                 spin_unlock(&lmv->lmv_lock);
1995
1996                 RETURN(rc);
1997         }
1998
1999         if ((keylen == strlen("flush_cred") &&
2000              strcmp(key, "flush_cred") == 0)) {
2001                 int rc = 0, i;
2002
2003                 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count;
2004                      i++, tgt++) {
2005                         if (!tgt->ltd_exp)
2006                                 continue;
2007                         rc = obd_set_info(tgt->ltd_exp,
2008                                           keylen, key, vallen, val);
2009                         if (rc)
2010                                 RETURN(rc);
2011                 }
2012
2013                 RETURN(0);
2014         }
2015
2016         RETURN(-EINVAL);
2017 }
2018
2019 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2020                struct lov_stripe_md *lsm)
2021 {
2022         struct obd_device *obd = class_exp2obd(exp);
2023         struct lmv_obd *lmv = &obd->u.lmv;
2024         struct mea *meap, *lsmp;
2025         int mea_size, i;
2026         ENTRY;
2027
2028         mea_size = (sizeof(struct lustre_id) * 
2029                     lmv->desc.ld_tgt_count) + sizeof(struct mea);
2030         if (!lmmp)
2031                 RETURN(mea_size);
2032
2033         if (*lmmp && !lsm) {
2034                 OBD_FREE(*lmmp, mea_size);
2035                 *lmmp = NULL;
2036                 RETURN(0);
2037         }
2038
2039         if (*lmmp == NULL) {
2040                 OBD_ALLOC(*lmmp, mea_size);
2041                 if (*lmmp == NULL)
2042                         RETURN(-ENOMEM);
2043         }
2044
2045         if (!lsm)
2046                 RETURN(mea_size);
2047
2048         lsmp = (struct mea *)lsm;
2049         meap = (struct mea *)*lmmp;
2050
2051         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2052             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2053                 RETURN(-EINVAL);
2054
2055         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2056         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2057         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2058
2059         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2060                 meap->mea_ids[i] = meap->mea_ids[i];
2061                 id_cpu_to_le(&meap->mea_ids[i]);
2062         }
2063
2064         RETURN(mea_size);
2065 }
2066
2067 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2068                  struct lov_mds_md *lmm, int lmm_size)
2069 {
2070         struct obd_device *obd = class_exp2obd(exp);
2071         struct mea **tmea = (struct mea **)lsmp;
2072         struct mea *mea = (struct mea *)lmm;
2073         struct lmv_obd *lmv = &obd->u.lmv;
2074         int mea_size, i, rc = 0;
2075         __u32 magic;
2076         ENTRY;
2077
2078         mea_size = sizeof(struct lustre_id) * 
2079                 lmv->desc.ld_tgt_count + sizeof(struct mea);
2080
2081         if (lsmp == NULL)
2082                 return mea_size;
2083
2084         if (*lsmp != NULL && lmm == NULL) {
2085                 OBD_FREE(*tmea, mea_size);
2086                 RETURN(0);
2087         }
2088
2089         LASSERT(mea_size == lmm_size);
2090
2091         OBD_ALLOC(*tmea, mea_size);
2092         if (*tmea == NULL)
2093                 RETURN(-ENOMEM);
2094
2095         if (!lmm)
2096                 RETURN(mea_size);
2097
2098         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2099             mea->mea_magic == MEA_MAGIC_ALL_CHARS)
2100         {
2101                 magic = le32_to_cpu(mea->mea_magic);
2102         } else {
2103                 struct mea_old *old = (struct mea_old *)lmm;
2104         
2105                 mea_size = sizeof(struct lustre_id) * old->mea_count + 
2106                         sizeof(struct mea_old);
2107         
2108                 if (old->mea_count > 256 || old->mea_master > 256 ||
2109                     lmm_size < mea_size || old->mea_master > old->mea_count) {
2110                         CWARN("bad MEA: count %u, master %u, size %u\n",
2111                               old->mea_count, old->mea_master, mea_size);
2112                         GOTO(out_free_mea, rc = -EINVAL);
2113                 }
2114                 magic = MEA_MAGIC_LAST_CHAR;
2115         }
2116
2117         (*tmea)->mea_magic = magic;
2118         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2119         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2120
2121         for (i = 0; i < (*tmea)->mea_count; i++) {
2122                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2123                 id_le_to_cpu(&(*tmea)->mea_ids[i]);
2124         }
2125         RETURN(mea_size);
2126
2127 out_free_mea:
2128         OBD_FREE(*tmea, mea_size);
2129         return rc;
2130 }
2131
2132 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
2133             struct lov_stripe_md *ea, obd_count oa_bufs,
2134             struct brw_page *pgarr, struct obd_trans_info *oti)
2135 {
2136         struct obd_device *obd = exp->exp_obd;
2137         struct lmv_obd *lmv = &obd->u.lmv;
2138         struct mea *mea = (struct mea *) ea;
2139         int err;
2140       
2141         LASSERT(oa != NULL);
2142         LASSERT(ea != NULL);
2143         LASSERT(pgarr != NULL);
2144         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
2145
2146         oa->o_gr = id_gen(&mea->mea_ids[oa->o_mds]);
2147         oa->o_id = id_ino(&mea->mea_ids[oa->o_mds]);
2148         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2149         
2150         err = obd_brw(rw, lmv->tgts[oa->o_mds].ltd_exp,
2151                       oa, NULL, oa_bufs, pgarr, oti);
2152         RETURN(err);
2153 }
2154
2155 static int lmv_cancel_unused(struct obd_export *exp,
2156                              struct lov_stripe_md *lsm, 
2157                              int flags, void *opaque)
2158 {
2159         struct obd_device *obd = exp->exp_obd;
2160         struct lmv_obd *lmv = &obd->u.lmv;
2161         int rc = 0, err, i;
2162         ENTRY;
2163
2164         LASSERT(lsm == NULL);
2165         
2166         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2167                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].active)
2168                         continue;
2169                 
2170                 err = obd_cancel_unused(lmv->tgts[i].ltd_exp,
2171                                         NULL, flags, opaque);
2172                 if (!rc)
2173                         rc = err;
2174         }
2175         RETURN(rc);
2176 }
2177
2178 struct obd_ops lmv_obd_ops = {
2179         .o_owner                = THIS_MODULE,
2180         .o_attach               = lmv_attach,
2181         .o_detach               = lmv_detach,
2182         .o_setup                = lmv_setup,
2183         .o_cleanup              = lmv_cleanup,
2184         .o_connect              = lmv_connect,
2185         .o_disconnect           = lmv_disconnect,
2186         .o_statfs               = lmv_statfs,
2187         .o_llog_init            = lmv_llog_init,
2188         .o_llog_finish          = lmv_llog_finish,
2189         .o_get_info             = lmv_get_info,
2190         .o_set_info             = lmv_set_info,
2191         .o_create               = lmv_obd_create,
2192         .o_packmd               = lmv_packmd,
2193         .o_unpackmd             = lmv_unpackmd,
2194         .o_brw                  = lmv_brw,
2195         .o_init_ea_size         = lmv_init_ea_size,
2196         .o_notify               = lmv_notify,
2197         .o_iocontrol            = lmv_iocontrol,
2198         .o_cancel_unused        = lmv_cancel_unused,
2199 };
2200
2201 struct md_ops lmv_md_ops = {
2202         .m_getstatus           = lmv_getstatus,
2203         .m_getattr             = lmv_getattr,
2204         .m_change_cbdata       = lmv_change_cbdata,
2205         .m_change_cbdata_name  = lmv_change_cbdata_name,
2206         .m_close               = lmv_close,
2207         .m_create              = lmv_create,
2208         .m_done_writing        = lmv_done_writing,
2209         .m_enqueue             = lmv_enqueue,
2210         .m_getattr_lock        = lmv_getattr_lock,
2211         .m_intent_lock         = lmv_intent_lock,
2212         .m_link                = lmv_link,
2213         .m_rename              = lmv_rename,
2214         .m_setattr             = lmv_setattr,
2215         .m_sync                = lmv_sync,
2216         .m_readpage            = lmv_readpage,
2217         .m_unlink              = lmv_unlink,
2218         .m_get_real_obd        = lmv_get_real_obd,
2219         .m_valid_attrs         = lmv_valid_attrs,
2220         .m_delete_inode        = lmv_delete_inode,
2221         .m_access_check        = lmv_access_check,
2222 };
2223
2224 int __init lmv_init(void)
2225 {
2226         struct lprocfs_static_vars lvars;
2227         int rc;
2228
2229         obj_cache = kmem_cache_create("lmv_objects",
2230                                       sizeof(struct lmv_obj),
2231                                       0, 0, NULL, NULL);
2232         if (!obj_cache) {
2233                 CERROR("error allocating lmv objects cache\n");
2234                 return -ENOMEM;
2235         }
2236
2237         lprocfs_init_vars(lmv, &lvars);
2238         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2239                                  lvars.module_vars,
2240                                  OBD_LMV_DEVICENAME);
2241         if (rc)
2242                 kmem_cache_destroy(obj_cache);
2243         
2244         return rc;
2245 }
2246
2247 #ifdef __KERNEL__
2248 static void lmv_exit(void)
2249 {
2250         class_unregister_type(OBD_LMV_DEVICENAME);
2251
2252         LASSERTF(kmem_cache_destroy(obj_cache) == 0,
2253                  "can't free lmv objects cache, %d object(s)"
2254                  "still in use\n", atomic_read(&obj_cache_count));
2255 }
2256
2257 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2258 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2259 MODULE_LICENSE("GPL");
2260
2261 module_init(lmv_init);
2262 module_exit(lmv_exit);
2263 #endif