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