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