Whamcloud - gitweb
52ec1de414a4644ca6ab3a8699d2aba0c8676c75
[fs/lustre-release.git] / lustre / lmv / lmv_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_LMV
26 #ifdef __KERNEL__
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/pagemap.h>
32 #include <asm/div64.h>
33 #include <linux/seq_file.h>
34 #else
35 #include <liblustre.h>
36 #endif
37 #include <linux/ext2_fs.h>
38
39 #include <linux/obd_support.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_net.h>
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_dlm.h>
44 #include <linux/lustre_mds.h>
45 #include <linux/obd_class.h>
46 #include <linux/obd_ost.h>
47 #include <linux/lprocfs_status.h>
48 #include <linux/lustre_fsfilt.h>
49 #include <linux/obd_lmv.h>
50 #include "lmv_internal.h"
51
52 int lmv_attach(struct obd_device *dev, obd_count len, void *data)
53 {
54         struct lprocfs_static_vars lvars;
55         int rc;
56         ENTRY;
57
58         lprocfs_init_vars(lmv, &lvars);
59         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
60         if (rc == 0) {
61 #ifdef __KERNEL__
62                 struct proc_dir_entry *entry;
63                 
64                 entry = create_proc_entry("target_obd", 0444, dev->obd_proc_entry);
65                 if (entry == NULL)
66                         RETURN(-ENOMEM);
67                 /* entry->proc_fops = &lmv_proc_target_fops; */
68                 entry->data = dev;
69 #endif
70        }
71         RETURN (rc);
72 }
73
74 int lmv_detach(struct obd_device *dev)
75 {
76         return lprocfs_obd_detach(dev);
77 }
78
79 static int lmv_connect_fake(struct lustre_handle *conn,
80                             struct obd_device *obd,
81                             struct obd_uuid *cluuid)
82 {
83         struct lmv_obd *lmv = &obd->u.lmv;
84         struct obd_export *exp;
85         int rc;
86         ENTRY;
87
88         rc = class_connect(conn, obd, cluuid);
89         if (rc) {
90                 CERROR("class_connection() returned %d\n", rc);
91                 RETURN(rc);
92         }
93
94         exp = class_conn2export(conn);
95         /* We don't want to actually do the underlying connections more than
96          * once, so keep track. */
97         lmv->refcount++;
98         if (lmv->refcount > 1) {
99                 class_export_put(exp);
100                 RETURN(0);
101         }
102
103         lmv->cluuid = *cluuid;
104         lmv->connected = 0;
105         lmv->exp = exp;
106
107         RETURN(0);
108 }
109
110 void lmv_set_timeouts(struct obd_device *obd)
111 {
112         struct lmv_tgt_desc *tgts;
113         struct lmv_obd *lmv;
114         int i;
115
116         lmv = &obd->u.lmv;
117         if (lmv->server_timeout == 0)
118                 return;
119
120         if (lmv->connected == 0)
121                 return;
122
123         for (i = 0, tgts = lmv->tgts; i < lmv->count; i++, tgts++) {
124                 if (tgts->exp == NULL)
125                         continue;
126                 obd_set_info(tgts->exp, strlen("inter_mds"),
127                              "inter_mds", 0, NULL);
128         }
129 }
130
131 int lmv_connect(struct obd_device *obd)
132 {
133         struct lmv_obd *lmv = &obd->u.lmv;
134         struct obd_uuid *cluuid;
135         struct lmv_tgt_desc *tgts;
136         struct obd_export *exp;
137         int rc, i;
138         ENTRY;
139
140         if (lmv->connected)
141                 RETURN(0);
142       
143         lmv->connected = 1;
144         cluuid = &lmv->cluuid;
145         exp = lmv->exp;
146         CDEBUG(D_OTHER, "time to connect %s to %s\n",
147                         cluuid->uuid, obd->obd_name);
148
149         for (i = 0, tgts = lmv->tgts; i < lmv->count; i++, tgts++) {
150                 struct obd_device *tgt_obd;
151                 struct obd_uuid lmv_osc_uuid = { "LMV_OSC_UUID" };
152                 struct lustre_handle conn = {0, };
153
154                 LASSERT(tgts != NULL);
155
156                 tgt_obd = class_find_client_obd(&tgts->uuid, LUSTRE_MDC_NAME, 
157                                                 &obd->obd_uuid);
158                 if (!tgt_obd) {
159                         CERROR("Target %s not attached\n", tgts->uuid.uuid);
160                         GOTO(out_disc, rc = -EINVAL);
161                 }
162
163                 /* for MDS: don't connect to yourself */
164                 if (obd_uuid_equals(&tgts->uuid, cluuid)) {
165                         CDEBUG(D_OTHER, "don't connect back to %s\n",
166                                cluuid->uuid);
167                         tgts->exp = NULL;
168                         continue;
169                 }
170
171                 CDEBUG(D_OTHER, "connect to %s(%s) - %s, %s FOR %s\n",
172                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
173                         tgts->uuid.uuid, obd->obd_uuid.uuid,
174                         cluuid->uuid);
175
176                 if (!tgt_obd->obd_set_up) {
177                         CERROR("Target %s not set up\n", tgts->uuid.uuid);
178                         GOTO(out_disc, rc = -EINVAL);
179                 }
180                 
181                 rc = obd_connect(&conn, tgt_obd, &lmv_osc_uuid);
182                 if (rc) {
183                         CERROR("Target %s connect error %d\n",
184                                 tgts->uuid.uuid, rc);
185                         GOTO(out_disc, rc);
186                 }
187                 tgts->exp = class_conn2export(&conn);
188
189                 obd_init_ea_size(tgts->exp, lmv->max_easize,
190                                         lmv->max_cookiesize);
191                 
192                 rc = obd_register_observer(tgt_obd, obd);
193                 if (rc) {
194                         CERROR("Target %s register_observer error %d\n",
195                                tgts->uuid.uuid, rc);
196                         obd_disconnect(tgts->exp, 0);
197                         GOTO(out_disc, rc);
198                 }
199
200                 CDEBUG(D_OTHER, "connected to %s(%s) successfully (%d)\n",
201                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
202                         atomic_read(&obd->obd_refcount));
203         }
204
205         lmv_set_timeouts(obd);
206
207         class_export_put(exp);
208         RETURN (0);
209
210  out_disc:
211         /* FIXME: cleanup here */
212         class_disconnect(exp, 0);
213         RETURN (rc);
214 }
215
216 static int lmv_disconnect(struct obd_export *exp, int flags)
217 {
218         struct obd_device *obd = class_exp2obd(exp);
219         struct lmv_obd *lmv = &obd->u.lmv;
220         int rc, i;
221         ENTRY;
222
223         if (!lmv->tgts)
224                 goto out_local;
225
226         /* Only disconnect the underlying layers on the final disconnect. */
227         lmv->refcount--;
228         if (lmv->refcount != 0)
229                 goto out_local;
230
231         for (i = 0; i < lmv->count; i++) {
232                 if (lmv->tgts[i].exp == NULL)
233                         continue;
234
235                 if (obd->obd_no_recov) {
236                         /* Pass it on to our clients.
237                          * XXX This should be an argument to disconnect,
238                          * XXX not a back-door flag on the OBD.  Ah well.
239                          */
240                         struct obd_device *mdc_obd;
241                         mdc_obd = class_exp2obd(lmv->tgts[i].exp);
242                         if (mdc_obd)
243                                 mdc_obd->obd_no_recov = 1;
244                 }
245
246                 CDEBUG(D_OTHER, "disconnected from %s(%s) successfully\n",
247                         lmv->tgts[i].exp->exp_obd->obd_name,
248                         lmv->tgts[i].exp->exp_obd->obd_uuid.uuid);
249
250                 obd_register_observer(lmv->tgts[i].exp->exp_obd, NULL);
251
252                 rc = obd_disconnect(lmv->tgts[i].exp, flags);
253                 lmv->tgts[i].exp = NULL;
254         }
255
256  out_local:
257         /* FIXME: cleanup here */
258         if (!lmv->connected)
259                 class_export_put(exp);
260         rc = class_disconnect(exp, 0);
261         RETURN(rc);
262 }
263
264 static int lmv_setup(struct obd_device *obd, obd_count len, void *buf)
265 {
266         struct lustre_cfg *lcfg = buf;
267         struct lmv_desc *desc;
268         struct lmv_obd *lmv = &obd->u.lmv;
269         struct obd_uuid *uuids;
270         struct lmv_tgt_desc *tgts;
271         int i;
272         int count;
273         int rc = 0;
274         ENTRY;
275
276         if (lcfg->lcfg_inllen1 < 1) {
277                 CERROR("LMV setup requires a descriptor\n");
278                 RETURN(-EINVAL);
279         }
280
281         if (lcfg->lcfg_inllen2 < 1) {
282                 CERROR("LMV setup requires an OST UUID list\n");
283                 RETURN(-EINVAL);
284         }
285
286         desc = (struct lmv_desc *)lcfg->lcfg_inlbuf1;
287         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
288                 CERROR("descriptor size wrong: %d > %d\n",
289                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
290                 RETURN(-EINVAL);
291         }
292
293         count = desc->ld_count;
294         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
295         if (sizeof(*uuids) * count != lcfg->lcfg_inllen2) {
296                 CERROR("UUID array size wrong: %u * %u != %u\n",
297                        sizeof(*uuids), count, lcfg->lcfg_inllen2);
298                 RETURN(-EINVAL);
299         }
300
301         lmv->bufsize = sizeof(struct lmv_tgt_desc) * count;
302         OBD_ALLOC(lmv->tgts, lmv->bufsize);
303         if (lmv->tgts == NULL) {
304                 CERROR("Out of memory\n");
305                 RETURN(-EINVAL);
306         }
307
308         for (i = 0, tgts = lmv->tgts; i < count; i++, tgts++) {
309                 tgts->uuid = uuids[i];
310                 lmv->count++;
311         }
312
313         lmv->max_easize = sizeof(struct ll_fid) * lmv->count
314                                         + sizeof(struct mea);
315         lmv->max_cookiesize = 0;
316
317         RETURN(rc);
318 }
319
320 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
321                       unsigned long max_age)
322 {
323         struct lmv_obd *lmv = &obd->u.lmv;
324         struct obd_statfs temp;
325         int rc = 0, i;
326         ENTRY;
327         lmv_connect(obd);
328         for (i = 0; i < lmv->count; i++) {
329                 rc = obd_statfs(lmv->tgts[i].exp->exp_obd, &temp, max_age);
330                 if (rc) {
331                         CERROR("can't stat MDS #%d (%s)\n", i,
332                                lmv->tgts[i].exp->exp_obd->obd_name);
333                         RETURN(rc);
334                 }
335                 if (i == 0) {
336                         memcpy(osfs, &temp, sizeof(temp));
337                 } else {
338                         osfs->os_bavail += temp.os_bavail;
339                         osfs->os_blocks += temp.os_blocks;
340                         osfs->os_ffree += temp.os_ffree;
341                         osfs->os_files += temp.os_files;
342                 }
343         }
344         RETURN(rc);
345 }
346
347 static int lmv_cleanup(struct obd_device *obd, int flags) 
348 {
349         struct lmv_obd *lmv = &obd->u.lmv;
350         ENTRY;
351         lmv_cleanup_objs(obd);
352         OBD_FREE(lmv->tgts, lmv->bufsize);
353         RETURN(0);
354 }
355
356 static int lmv_getstatus(struct obd_export *exp, struct ll_fid *fid)
357 {
358         struct obd_device *obd = exp->exp_obd;
359         struct lmv_obd *lmv = &obd->u.lmv;
360         int rc;
361         ENTRY;
362         lmv_connect(obd);
363         rc = md_getstatus(lmv->tgts[0].exp, fid);
364         fid->mds = 0;
365         RETURN(rc);
366 }
367
368 static int lmv_getattr(struct obd_export *exp, struct ll_fid *fid,
369                 unsigned long valid, unsigned int ea_size,
370                 struct ptlrpc_request **request)
371 {
372         struct obd_device *obd = exp->exp_obd;
373         struct lmv_obd *lmv = &obd->u.lmv;
374         int rc, i = fid->mds;
375         struct lmv_obj *obj;
376         ENTRY;
377         lmv_connect(obd);
378         obj = lmv_grab_obj(obd, fid, 0);
379         CDEBUG(D_OTHER, "GETATTR for %lu/%lu/%lu %s\n",
380                (unsigned long) fid->mds,
381                (unsigned long) fid->id,
382                (unsigned long) fid->generation,
383                obj ? "(splitted)" : "");
384
385         LASSERT(fid->mds < lmv->count);
386         rc = md_getattr(lmv->tgts[i].exp, fid,
387                              valid, ea_size, request);
388         if (rc == 0 && obj) {
389                 /* we have to loop over dirobjs here and gather attrs
390                  * for all the slaves */
391 #warning "attrs gathering here"
392         }
393         lmv_put_obj(obj);
394         RETURN(rc);
395 }
396
397 static int lmv_change_cbdata(struct obd_export *exp,
398                                  struct ll_fid *fid, 
399                                  ldlm_iterator_t it, void *data)
400 {
401         struct obd_device *obd = exp->exp_obd;
402         struct lmv_obd *lmv = &obd->u.lmv;
403         int rc = 0;
404         ENTRY;
405         lmv_connect(obd);
406         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu\n",
407                (unsigned long) fid->mds,
408                (unsigned long) fid->id,
409                (unsigned long) fid->generation);
410         LASSERT(fid->mds < lmv->count);
411         rc = md_change_cbdata(lmv->tgts[fid->mds].exp, fid, it, data);
412         RETURN(rc);
413 }
414
415 static int lmv_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
416                                   char *name, int len, struct ll_fid *cfid,
417                                   ldlm_iterator_t it, void *data)
418 {
419         struct obd_device *obd = exp->exp_obd;
420         struct lmv_obd *lmv = &obd->u.lmv;
421         struct lmv_obj *obj;
422         int rc = 0, mds;
423         ENTRY;
424         lmv_connect(obd);
425         LASSERT(pfid->mds < lmv->count);
426         LASSERT(cfid->mds < lmv->count);
427         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu:%*s -> %lu/%lu/%lu\n",
428                (unsigned long) pfid->mds, (unsigned long) pfid->id,
429                (unsigned long) pfid->generation, len, name,
430                (unsigned long) cfid->mds, (unsigned long) cfid->id,
431                (unsigned long) cfid->generation);
432
433         /* this is default mds for directory name belongs to */
434         mds = pfid->mds;
435         obj = lmv_grab_obj(obd, pfid, 0);
436         if (obj) {
437                 /* directory is splitted. look for right mds for this name */
438                 mds = raw_name2idx(obj->objcount, name, len);
439                 lmv_put_obj(obj);
440         }
441         rc = md_change_cbdata(lmv->tgts[mds].exp, cfid, it, data);
442         RETURN(rc);
443 }
444
445 static int lmv_valid_attrs(struct obd_export *exp, struct ll_fid *fid) 
446 {
447         struct obd_device *obd = exp->exp_obd;
448         struct lmv_obd *lmv = &obd->u.lmv;
449         int rc = 0;
450         ENTRY;
451         lmv_connect(obd);
452         CDEBUG(D_OTHER, "validate %lu/%lu/%lu\n",
453                (unsigned long) fid->mds,
454                (unsigned long) fid->id,
455                (unsigned long) fid->generation);
456         LASSERT(fid->mds < lmv->count);
457         rc = md_valid_attrs(lmv->tgts[fid->mds].exp, fid);
458         RETURN(rc);
459 }
460
461 int lmv_close(struct obd_export *exp, struct obdo *obdo,
462                   struct obd_client_handle *och,
463                   struct ptlrpc_request **request)
464 {
465         struct obd_device *obd = exp->exp_obd;
466         struct lmv_obd *lmv = &obd->u.lmv;
467         int rc, i = obdo->o_mds;
468         ENTRY;
469         lmv_connect(obd);
470         LASSERT(i < lmv->count);
471         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long) obdo->o_mds,
472                (unsigned long) obdo->o_id, (unsigned long) obdo->o_generation);
473         rc = md_close(lmv->tgts[i].exp, obdo, och, request);
474         RETURN(rc);
475 }
476
477 int lmv_get_mea_and_update_object(struct obd_export *exp, struct ll_fid *fid)
478 {
479         struct obd_device *obd = exp->exp_obd;
480         struct lmv_obd *lmv = &obd->u.lmv;
481         struct ptlrpc_request *req = NULL;
482         struct lustre_md md;
483         unsigned long valid;
484         int mealen, rc;
485
486         md.mea = NULL;
487         mealen = MEA_SIZE_LMV(lmv);
488         
489         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
490
491         /* time to update mea of parent fid */
492         rc = md_getattr(lmv->tgts[fid->mds].exp, fid,
493                         valid, mealen, &req);
494         if (rc) {
495                 CERROR("md_getattr() failed, rc = %d\n", rc);
496                 GOTO(cleanup, rc);
497         }
498
499         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
500         if (rc) {
501                 CERROR("mdc_req2lustre_md() failed, rc = %d\n", rc);
502                 GOTO(cleanup, rc);
503         }
504
505         if (md.mea == NULL)
506                 GOTO(cleanup, rc = -ENODATA);
507
508         rc = lmv_create_obj_from_attrs(exp, fid, md.mea);
509         obd_free_memmd(exp, (struct lov_stripe_md **) &md.mea);
510
511 cleanup:
512         if (req)
513                 ptlrpc_req_finished(req);
514         RETURN(rc);
515 }
516
517 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
518                const void *data, int datalen, int mode, __u32 uid,
519                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
520 {
521         struct obd_device *obd = exp->exp_obd;
522         struct lmv_obd *lmv = &obd->u.lmv;
523         struct mds_body *mds_body;
524         struct lmv_obj *obj;
525         int rc, mds;
526         ENTRY;
527
528         lmv_connect(obd);
529 repeat:
530         obj = lmv_grab_obj(obd, &op_data->fid1, 0);
531         if (obj) {
532                 mds = raw_name2idx(obj->objcount, op_data->name,
533                                         op_data->namelen);
534                 op_data->fid1 = obj->objs[mds].fid;
535                 lmv_put_obj(obj);
536         }
537
538         CDEBUG(D_OTHER, "CREATE '%*s' on %lu/%lu/%lu\n",
539                         op_data->namelen, op_data->name,
540                         (unsigned long) op_data->fid1.mds,
541                         (unsigned long) op_data->fid1.id,
542                         (unsigned long) op_data->fid1.generation);
543         rc = md_create(lmv->tgts[op_data->fid1.mds].exp, op_data, data,
544                        datalen, mode, uid, gid, rdev, request);
545         if (rc == 0) {
546                 if (*request == NULL)
547                      RETURN(rc);
548                 mds_body = lustre_msg_buf((*request)->rq_repmsg, 0,
549                                           sizeof(*mds_body));
550                 LASSERT(mds_body != NULL);
551                 CDEBUG(D_OTHER, "created. id = %lu, generation = %lu, mds = %d\n",
552                        (unsigned long) mds_body->fid1.id,
553                        (unsigned long) mds_body->fid1.generation,
554                        op_data->fid1.mds);
555                 LASSERT(mds_body->valid & OBD_MD_MDS ||
556                                 mds_body->mds == op_data->fid1.mds);
557         } else if (rc == -ERESTART) {
558                 /* directory got splitted. time to update local object
559                  * and repeat the request with proper MDS */
560                 rc = lmv_get_mea_and_update_object(exp, &op_data->fid1);
561                 if (rc == 0) {
562                         ptlrpc_req_finished(*request);
563                         goto repeat;
564                 }
565         }
566         RETURN(rc);
567 }
568
569 int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
570 {
571         struct obd_device *obd = exp->exp_obd;
572         struct lmv_obd *lmv = &obd->u.lmv;
573         int rc;
574         ENTRY;
575         lmv_connect(obd);
576         /* FIXME: choose right MDC here */
577         rc = md_done_writing(lmv->tgts[0].exp, obdo);
578         RETURN(rc);
579 }
580
581 int lmv_enqueue(struct obd_export *exp, int lock_type,
582                 struct lookup_intent *it, int lock_mode,
583                 struct mdc_op_data *data, struct lustre_handle *lockh,
584                 void *lmm, int lmmsize,
585                 ldlm_completion_callback cb_completion,
586                 ldlm_blocking_callback cb_blocking, void *cb_data)
587 {
588         struct obd_device *obd = exp->exp_obd;
589         struct lmv_obd *lmv = &obd->u.lmv;
590         struct lmv_obj *obj;
591         int rc, mds;
592         ENTRY;
593         lmv_connect(obd);
594         if (data->namelen) {
595                 obj = lmv_grab_obj(obd, &data->fid1, 0);
596                 if (obj) {
597                         /* directory is splitted. look for
598                          * right mds for this name */
599                         mds = raw_name2idx(obj->objcount, (char *)data->name,
600                                            data->namelen);
601                         data->fid1 = obj->objs[mds].fid;
602                         lmv_put_obj(obj);
603                 }
604         }
605         CDEBUG(D_OTHER, "ENQUEUE '%s' on %lu/%lu\n",
606                LL_IT2STR(it), (unsigned long) data->fid1.id,
607                (unsigned long) data->fid1.generation);
608         rc = md_enqueue(lmv->tgts[data->fid1.mds].exp, lock_type, it,
609                         lock_mode, data, lockh, lmm, lmmsize, cb_completion,
610                         cb_blocking, cb_data);
611
612         RETURN(rc);
613 }
614
615 int lmv_getattr_name(struct obd_export *exp, struct ll_fid *fid,
616                          char *filename, int namelen, unsigned long valid,
617                          unsigned int ea_size, struct ptlrpc_request **request)
618 {
619         struct obd_device *obd = exp->exp_obd;
620         struct lmv_obd *lmv = &obd->u.lmv;
621         struct ll_fid rfid = *fid;
622         int rc, mds = fid->mds;
623         struct mds_body *body;
624         struct lmv_obj *obj;
625         ENTRY;
626         lmv_connect(obd);
627 repeat:
628         obj = lmv_grab_obj(obd, fid, 0);
629         if (obj) {
630                 /* directory is splitted. look for right mds for this name */
631                 mds = raw_name2idx(obj->objcount, filename, namelen - 1);
632                 rfid = obj->objs[mds].fid;
633                 lmv_put_obj(obj);
634         }
635         CDEBUG(D_OTHER, "getattr_name for %*s on %lu/%lu/%lu -> %lu/%lu/%lu\n",
636                namelen, filename, (unsigned long) fid->mds,
637                (unsigned long) fid->id, (unsigned long) fid->generation,
638                (unsigned long) rfid.mds, (unsigned long) rfid.id,
639                (unsigned long) rfid.generation);
640         rc = md_getattr_name(lmv->tgts[mds].exp, &rfid, filename, namelen,
641                                   valid, ea_size, request);
642         if (rc == 0) {
643                 /* this could be cross-node reference. in this case all
644                  * we have right now is mds/ino/generation triple. we'd
645                  * like to find other attributes */
646                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
647                 LASSERT(body != NULL);
648                 if (body->valid & OBD_MD_MDS) {
649                         struct ptlrpc_request *req = NULL;
650                         rfid = body->fid1;
651                         CDEBUG(D_OTHER, "request attrs for %lu/%lu/%lu\n",
652                                (unsigned long) rfid.mds,
653                                (unsigned long) rfid.id,
654                                (unsigned long) rfid.generation);
655                         rc = md_getattr_name(lmv->tgts[rfid.mds].exp, &rfid,
656                                              NULL, 1, valid, ea_size, &req);
657                         ptlrpc_req_finished(*request);
658                         *request = req;
659                 }
660         } else if (rc == -ERESTART) {
661                 /* directory got splitted. time to update local object
662                  * and repeat the request with proper MDS */
663                 rc = lmv_get_mea_and_update_object(exp, &rfid);
664                 if (rc == 0) {
665                         ptlrpc_req_finished(*request);
666                         goto repeat;
667                 }
668         }
669         RETURN(rc);
670 }
671
672
673 /*
674  * llite passes fid of an target inode in data->fid1 and
675  * fid of directory in data->fid2
676  */
677 int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
678              struct ptlrpc_request **request)
679 {
680         struct obd_device *obd = exp->exp_obd;
681         struct lmv_obd *lmv = &obd->u.lmv;
682         struct lmv_obj *obj;
683         int rc;
684         ENTRY;
685         lmv_connect(obd);
686         if (data->namelen != 0) {
687                 /* usual link request */
688                 obj = lmv_grab_obj(obd, &data->fid1, 0);
689                 if (obj) {
690                         rc = raw_name2idx(obj->objcount, data->name,
691                                          data->namelen);
692                         data->fid1 = obj->objs[rc].fid;
693                         lmv_put_obj(obj);
694                 }
695                 CDEBUG(D_OTHER,"link %u/%u/%u:%*s to %u/%u/%u mds %d\n",
696                        (unsigned) data->fid2.mds, (unsigned) data->fid2.id,
697                        (unsigned) data->fid2.generation, data->namelen,
698                        data->name, (unsigned) data->fid1.mds,
699                        (unsigned) data->fid1.id,
700                        (unsigned) data->fid1.generation, data->fid1.mds);
701         } else {
702                 /* request from MDS to acquire i_links for inode by fid1 */
703                 CDEBUG(D_OTHER, "inc i_nlinks for %u/%u/%u\n",
704                        (unsigned) data->fid1.mds, (unsigned) data->fid1.id,
705                        (unsigned) data->fid1.generation);
706         }
707                         
708         rc = md_link(lmv->tgts[data->fid1.mds].exp, data, request);
709         RETURN(rc);
710 }
711
712 int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
713                const char *old, int oldlen, const char *new, int newlen,
714                struct ptlrpc_request **request)
715 {
716         struct obd_device *obd = exp->exp_obd;
717         struct lmv_obd *lmv = &obd->u.lmv;
718         struct lmv_obj *obj;
719         int rc, mds;
720         ENTRY;
721
722         CDEBUG(D_OTHER, "rename %*s in %lu/%lu/%lu to %*s in %lu/%lu/%lu\n",
723                oldlen, old, (unsigned long) data->fid1.mds,
724                (unsigned long) data->fid1.id,
725                (unsigned long) data->fid1.generation,
726                newlen, new, (unsigned long) data->fid2.mds,
727                (unsigned long) data->fid2.id,
728                (unsigned long) data->fid2.generation);
729         if (!fid_equal(&data->fid1, &data->fid2))
730                 CWARN("cross-node rename %lu/%lu/%lu:%*s to %lu/%lu/%lu:%*s\n",
731                       (unsigned long) data->fid1.mds,
732                       (unsigned long) data->fid1.id,
733                       (unsigned long) data->fid1.generation, oldlen, old,
734                       (unsigned long) data->fid2.mds,
735                       (unsigned long) data->fid2.id,
736                       (unsigned long) data->fid2.generation, newlen, new);
737
738         lmv_connect(obd);
739
740         if (oldlen == 0) {
741                 /* MDS with old dir entry is asking another MDS
742                  * to create name there */
743                 CDEBUG(D_OTHER,
744                        "create %*s(%d/%d) in %lu/%lu/%lu pointing to %lu/%lu/%lu\n",
745                        newlen, new, oldlen, newlen,
746                        (unsigned long) data->fid2.mds,
747                        (unsigned long) data->fid2.id,
748                        (unsigned long) data->fid2.generation,
749                        (unsigned long) data->fid1.mds,
750                        (unsigned long) data->fid1.id,
751                        (unsigned long) data->fid1.generation);
752                 mds = data->fid2.mds;
753                 goto request;
754         }
755
756         obj = lmv_grab_obj(obd, &data->fid1, 0);
757         if (obj) {
758                 /* directory is already splitted, so we have to forward
759                  * request to the right MDS */
760                 mds = raw_name2idx(obj->objcount, (char *)old, oldlen);
761                 data->fid1 = obj->objs[mds].fid;
762                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
763                        (unsigned long) obj->objs[mds].fid.mds,
764                        (unsigned long) obj->objs[mds].fid.id,
765                        (unsigned long) obj->objs[mds].fid.generation);
766         }
767         lmv_put_obj(obj);
768
769         obj = lmv_grab_obj(obd, &data->fid2, 0);
770         if (obj) {
771                 /* directory is already splitted, so we have to forward
772                  * request to the right MDS */
773                 mds = raw_name2idx(obj->objcount, (char *)new, newlen);
774                 data->fid2 = obj->objs[mds].fid;
775                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
776                        (unsigned long) obj->objs[mds].fid.mds,
777                        (unsigned long) obj->objs[mds].fid.id,
778                        (unsigned long) obj->objs[mds].fid.generation);
779         }
780         lmv_put_obj(obj);
781         
782         mds = data->fid1.mds;
783
784 request:
785         rc = md_rename(lmv->tgts[mds].exp, data, old, oldlen,
786                             new, newlen, request); 
787         RETURN(rc);
788 }
789
790 int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
791                 struct iattr *iattr, void *ea, int ealen, void *ea2, int ea2len,
792                 struct ptlrpc_request **request)
793 {
794         struct obd_device *obd = exp->exp_obd;
795         struct lmv_obd *lmv = &obd->u.lmv;
796         int rc = 0, i = data->fid1.mds;
797         struct ptlrpc_request *req;
798         struct mds_body *mds_body;
799         struct lmv_obj *obj;
800         ENTRY;
801         lmv_connect(obd);
802         obj = lmv_grab_obj(obd, &data->fid1, 0);
803         CDEBUG(D_OTHER, "SETATTR for %lu/%lu/%lu, valid 0x%x%s\n",
804                (unsigned long) data->fid1.mds,
805                (unsigned long) data->fid1.id,
806                (unsigned long) data->fid1.generation, iattr->ia_valid,
807                obj ? ", splitted" : "");
808         if (obj) {
809                 for (i = 0; i < obj->objcount; i++) {
810                         data->fid1 = obj->objs[i].fid;
811                         rc = md_setattr(lmv->tgts[i].exp, data, iattr, ea,
812                                         ealen, ea2, ea2len, &req);
813                         LASSERT(rc == 0);
814                         if (fid_equal(&obj->fid, &obj->objs[i].fid)) {
815                                 /* this is master object and this request
816                                  * should be returned back to llite */
817                                 *request = req;
818                         } else {
819                                 ptlrpc_req_finished(req);
820                         }
821                 }
822                 lmv_put_obj(obj);
823         } else {
824                 LASSERT(data->fid1.mds < lmv->count);
825                 rc = md_setattr(lmv->tgts[i].exp, data, iattr, ea, ealen,
826                                 ea2, ea2len, request); 
827                 if (rc == 0) {
828                         mds_body = lustre_msg_buf((*request)->rq_repmsg, 0,
829                                         sizeof(*mds_body));
830                         LASSERT(mds_body != NULL);
831                         LASSERT(mds_body->mds == i);
832                 }
833         }
834         RETURN(rc);
835 }
836
837 int lmv_sync(struct obd_export *exp, struct ll_fid *fid,
838              struct ptlrpc_request **request)
839 {
840         struct obd_device *obd = exp->exp_obd;
841         struct lmv_obd *lmv = &obd->u.lmv;
842         int rc;
843         ENTRY;
844         lmv_connect(obd);
845         rc = md_sync(lmv->tgts[0].exp, fid, request); 
846         RETURN(rc);
847 }
848
849 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock,
850                             struct ldlm_lock_desc *desc, void *data, int flag)
851 {
852         struct lustre_handle lockh;
853         struct lmv_obj *obj;
854         int rc;
855         ENTRY;
856
857         switch (flag) {
858         case LDLM_CB_BLOCKING:
859                 ldlm_lock2handle(lock, &lockh);
860                 rc = ldlm_cli_cancel(&lockh);
861                 if (rc < 0) {
862                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
863                         RETURN(rc);
864                 }
865                 break;
866         case LDLM_CB_CANCELING:
867                 /* time to drop cached attrs for dirobj */
868                 obj = lock->l_ast_data;
869                 if (!obj)
870                         break;
871
872                 CDEBUG(D_OTHER, "cancel %s on %lu/%lu, master %lu/%lu/%lu\n",
873                        lock->l_resource->lr_name.name[3] == 1 ?
874                                 "LOOKUP" : "UPDATE",
875                        (unsigned long) lock->l_resource->lr_name.name[0],
876                        (unsigned long) lock->l_resource->lr_name.name[1],
877                        (unsigned long) obj->fid.mds,
878                        (unsigned long) obj->fid.id,
879                        (unsigned long) obj->fid.generation);
880                 break;
881         default:
882                 LBUG();
883         }
884         RETURN(0);
885 }
886
887 void lmv_remove_dots(struct page *page)
888 {
889         char *kaddr = page_address(page);
890         unsigned limit = PAGE_CACHE_SIZE;
891         unsigned offs, rec_len;
892         struct ext2_dir_entry_2 *p;
893
894         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
895                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
896                 rec_len = le16_to_cpu(p->rec_len);
897
898                 if ((p->name_len == 1 && p->name[0] == '.') ||
899                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
900                         p->inode = 0;
901         }
902 }
903
904 int lmv_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
905                  __u64 offset, struct page *page,
906                  struct ptlrpc_request **request)
907 {
908         struct obd_device *obd = exp->exp_obd;
909         struct lmv_obd *lmv = &obd->u.lmv;
910         struct ll_fid rfid = *mdc_fid;
911         struct lmv_obj *obj;
912         int rc, i;
913         ENTRY;
914         lmv_connect(obd);
915        
916         LASSERT(mdc_fid->mds < lmv->count);
917         CDEBUG(D_OTHER, "READPAGE at %llu from %lu/%lu/%lu\n",
918                offset, (unsigned long) rfid.mds,
919                (unsigned long) rfid.id,
920                (unsigned long) rfid.generation);
921
922         obj = lmv_grab_obj(obd, mdc_fid, 0);
923         if (obj) {
924                 /* find dirobj containing page with requested offset */
925                 /* FIXME: what about protecting cached attrs here? */
926                 for (i = 0; i < obj->objcount; i++) {
927                         if (offset < obj->objs[i].size)
928                                 break;
929                         offset -= obj->objs[i].size;
930                 }
931                 rfid = obj->objs[i].fid;
932                 CDEBUG(D_OTHER, "forward to %lu/%lu/%lu with offset %lu\n",
933                        (unsigned long) rfid.mds,
934                        (unsigned long) rfid.id,
935                        (unsigned long) rfid.generation,
936                        (unsigned long) offset);
937         }
938         rc = md_readpage(lmv->tgts[rfid.mds].exp, &rfid, offset, page, request);
939         if (rc == 0 && !fid_equal(&rfid, mdc_fid)) {
940                 /* this page isn't from master object. to avoid
941                  * ./.. duplication in directory, we have to remove them
942                  * from all slave objects */
943                 lmv_remove_dots(page);
944         }
945       
946         lmv_put_obj(obj);
947
948         RETURN(rc);
949 }
950
951 int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
952                struct ptlrpc_request **request)
953 {
954         struct obd_device *obd = exp->exp_obd;
955         struct lmv_obd *lmv = &obd->u.lmv;
956         int rc, i = 0;
957         ENTRY;
958         lmv_connect(obd);
959         if (data->namelen != 0) {
960                 struct lmv_obj *obj;
961                 obj = lmv_grab_obj(obd, &data->fid1, 0);
962                 if (obj) {
963                         i = raw_name2idx(obj->objcount, data->name,
964                                          data->namelen);
965                         data->fid1 = obj->objs[i].fid;
966                         lmv_put_obj(obj);
967                 }
968                 CDEBUG(D_OTHER, "unlink '%*s' in %lu/%lu/%lu -> %u\n",
969                        data->namelen, data->name,
970                        (unsigned long) data->fid1.mds,
971                        (unsigned long) data->fid1.id,
972                        (unsigned long) data->fid1.generation, i);
973         } else {
974                 CDEBUG(D_OTHER, "drop i_nlink on %lu/%lu/%lu\n",
975                        (unsigned long) data->fid1.mds,
976                        (unsigned long) data->fid1.id,
977                        (unsigned long) data->fid1.generation);
978         }
979         rc = md_unlink(lmv->tgts[data->fid1.mds].exp, data, request); 
980         RETURN(rc);
981 }
982
983 struct obd_device *lmv_get_real_obd(struct obd_export *exp,
984                                     char *name, int len)
985 {
986         struct obd_device *obd = exp->exp_obd;
987         struct lmv_obd *lmv = &obd->u.lmv;
988         ENTRY;
989         lmv_connect(obd);
990         obd = lmv->tgts[0].exp->exp_obd;
991         EXIT;
992         return obd;
993 }
994
995 int lmv_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
996 {
997         struct obd_device *obd = exp->exp_obd;
998         struct lmv_obd *lmv = &obd->u.lmv;
999         int i, rc = 0, change = 0;
1000         ENTRY;
1001
1002         if (lmv->max_easize < easize) {
1003                 lmv->max_easize = easize;
1004                 change = 1;
1005         }
1006         if (lmv->max_cookiesize < cookiesize) {
1007                 lmv->max_cookiesize = cookiesize;
1008                 change = 1;
1009         }
1010         if (change == 0)
1011                 RETURN(0);
1012         
1013         if (lmv->connected == 0)
1014                 RETURN(0);
1015
1016         /* FIXME: error handling? */
1017         for (i = 0; i < lmv->count; i++)
1018                 rc = obd_init_ea_size(lmv->tgts[i].exp, easize, cookiesize);
1019         RETURN(rc);
1020 }
1021
1022 int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1023                           struct lov_stripe_md **ea, struct obd_trans_info *oti)
1024 {
1025         struct obd_device *obd = exp->exp_obd;
1026         struct lmv_obd *lmv = &obd->u.lmv;
1027         struct lov_stripe_md obj_md;
1028         struct lov_stripe_md *obj_mdp = &obj_md;
1029         int rc = 0;
1030         ENTRY;
1031         lmv_connect(obd);
1032
1033         LASSERT(ea == NULL);
1034         LASSERT(oa->o_mds < lmv->count);
1035
1036         rc = obd_create(lmv->tgts[oa->o_mds].exp, oa, &obj_mdp, oti);
1037         LASSERT(rc == 0);
1038
1039         RETURN(rc);
1040 }
1041
1042 /*
1043  * to be called from MDS only
1044  */
1045 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1046                struct lov_stripe_md **ea, struct obd_trans_info *oti)
1047 {
1048         struct obd_device *obd = exp->exp_obd;
1049         struct lmv_obd *lmv = &obd->u.lmv;
1050         struct mea *mea;
1051         int i, c, rc = 0;
1052         struct ll_fid mfid;
1053         ENTRY;
1054         lmv_connect(obd);
1055
1056         LASSERT(oa != NULL);
1057         
1058         if (ea == NULL) {
1059                 rc = lmv_obd_create_single(exp, oa, NULL, oti);
1060                 RETURN(rc);
1061         }
1062
1063         if (*ea == NULL) {
1064                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1065                 LASSERT(*ea != NULL);
1066         }
1067
1068         mea = (struct mea *)*ea;
1069         mfid.id = oa->o_id;
1070         mfid.generation = oa->o_generation;
1071         rc = 0;
1072         if (!mea->mea_count || mea->mea_count > lmv->count)
1073                 mea->mea_count = lmv->count;
1074
1075         mea->mea_master = -1;
1076         
1077         /* FIXME: error handling? */
1078         for (i = 0, c = 0; c < mea->mea_count && i < lmv->count; i++) {
1079                 struct lov_stripe_md obj_md;
1080                 struct lov_stripe_md *obj_mdp = &obj_md;
1081                
1082                 if (lmv->tgts[i].exp == NULL) {
1083                         /* this is master MDS */
1084                         mea->mea_fids[c].id = mfid.id;
1085                         mea->mea_fids[c].generation = mfid.generation;
1086                         mea->mea_fids[c].mds = i;
1087                         mea->mea_master = i;
1088                         c++;
1089                         continue;
1090                 }
1091
1092                 /* "Master" MDS should always be part of stripped dir, so
1093                    scan for it */
1094                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1095                         continue;
1096
1097                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE
1098                                 | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1099
1100                 rc = obd_create(lmv->tgts[c].exp, oa, &obj_mdp, oti);
1101                 /* FIXME: error handling here */
1102                 LASSERT(rc == 0);
1103
1104                 mea->mea_fids[c].id = oa->o_id;
1105                 mea->mea_fids[c].generation = oa->o_generation;
1106                 mea->mea_fids[c].mds = i;
1107                 c++;
1108                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1109                        i, oa->o_id, oa->o_generation);
1110         }
1111         LASSERT(c == mea->mea_count);
1112         CDEBUG(D_OTHER, "%d dirobjects created\n", (int) mea->mea_count);
1113
1114         RETURN(rc);
1115 }
1116
1117 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1118                            void *key, __u32 *vallen, void *val)
1119 {
1120         struct obd_device *obd;
1121         struct lmv_obd *lmv;
1122         ENTRY;
1123
1124         obd = class_exp2obd(exp);
1125         if (obd == NULL) {
1126                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1127                        exp->exp_handle.h_cookie);
1128                 RETURN(-EINVAL);
1129         }
1130
1131         lmv = &obd->u.lmv;
1132         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1133                 __u32 *mdsize = val;
1134                 *vallen = sizeof(__u32);
1135                 *mdsize = sizeof(struct ll_fid) * lmv->count
1136                                 + sizeof(struct mea);
1137                 RETURN(0);
1138         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1139                 struct obd_uuid *cluuid = &lmv->cluuid;
1140                 struct lmv_tgt_desc *tgts;
1141                 __u32 *mdsnum = val;
1142                 int i;
1143
1144                 for (i = 0, tgts = lmv->tgts; i < lmv->count; i++, tgts++) {
1145                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1146                                 *vallen = sizeof(__u32);
1147                                 *mdsnum = i;
1148                                 RETURN(0);
1149                         }
1150                 }
1151                 LASSERT(0);
1152         }
1153
1154         CDEBUG(D_IOCTL, "invalid key\n");
1155         RETURN(-EINVAL);
1156 }
1157
1158 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1159                  void *key, obd_count vallen, void *val)
1160 {
1161         struct obd_device *obd;
1162         struct lmv_obd *lmv;
1163         ENTRY;
1164
1165         obd = class_exp2obd(exp);
1166         if (obd == NULL) {
1167                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1168                        exp->exp_handle.h_cookie);
1169                 RETURN(-EINVAL);
1170         }
1171         lmv = &obd->u.lmv;
1172
1173         if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
1174                 struct lmv_tgt_desc *tgts;
1175                 int i, rc;
1176
1177                 lmv_connect(obd);
1178                 for (i = 0, tgts = lmv->tgts; i < lmv->count; i++, tgts++) {
1179                         rc = obd_set_info(tgts->exp, keylen, key, vallen, val);
1180                         if (rc)
1181                                 RETURN(rc);
1182                 }
1183                 RETURN(0);
1184         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1185                 lmv->server_timeout = 1;
1186                 lmv_set_timeouts(obd);
1187                 RETURN(0);
1188         }
1189         
1190         RETURN(-EINVAL);
1191 }
1192
1193 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
1194                struct lov_stripe_md *lsm)
1195 {
1196         struct obd_device *obd = class_exp2obd(exp);
1197         struct lmv_obd *lmv = &obd->u.lmv;
1198         int mea_size;
1199         ENTRY;
1200
1201         mea_size = sizeof(struct ll_fid) * lmv->count + sizeof(struct mea);
1202         if (!lmmp)
1203                 RETURN(mea_size);
1204
1205         if (*lmmp && !lsm) {
1206                 OBD_FREE(*lmmp, mea_size);
1207                 *lmmp = NULL;
1208                 RETURN(0);
1209         }
1210
1211         if (!*lmmp) {
1212                 OBD_ALLOC(*lmmp, mea_size);
1213                 if (!*lmmp)
1214                         RETURN(-ENOMEM);
1215         }
1216
1217         if (!lsm)
1218                 RETURN(mea_size);
1219
1220 #warning "MEA packing/convertation must be here! -bzzz"
1221         memcpy(*lmmp, lsm, mea_size);
1222         RETURN(mea_size);
1223 }
1224
1225 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **mem_tgt,
1226                         struct lov_mds_md *disk_src, int mdsize)
1227 {
1228         struct obd_device *obd = class_exp2obd(exp);
1229         struct lmv_obd *lmv = &obd->u.lmv;
1230         struct mea **tmea = (struct mea **) mem_tgt;
1231         struct mea *mea = (void *) disk_src;
1232         int mea_size;
1233         ENTRY;
1234
1235         mea_size = sizeof(struct ll_fid) * lmv->count + sizeof(struct mea);
1236         if (mem_tgt == NULL)
1237                 return mea_size;
1238
1239         if (*mem_tgt != NULL && disk_src == NULL) {
1240                 OBD_FREE(*tmea, mea_size);
1241                 RETURN(0);
1242         }
1243
1244         LASSERT(mea_size == mdsize);
1245
1246         OBD_ALLOC(*tmea, mea_size);
1247         /* FIXME: error handling here */
1248         LASSERT(*tmea != NULL);
1249
1250         if (!disk_src)
1251                 RETURN(mea_size);
1252
1253 #warning "MEA unpacking/convertation must be here! -bzzz"
1254         memcpy(*tmea, mea, mdsize);
1255         RETURN(mea_size);
1256 }
1257
1258 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
1259                 struct lov_stripe_md *ea, obd_count oa_bufs,
1260                 struct brw_page *pgarr, struct obd_trans_info *oti)
1261 {
1262         struct obd_device *obd = exp->exp_obd;
1263         struct lmv_obd *lmv = &obd->u.lmv;
1264         struct mea *mea = (struct mea *) ea;
1265         int err;
1266       
1267         LASSERT(oa != NULL);
1268         LASSERT(ea != NULL);
1269         LASSERT(pgarr != NULL);
1270         LASSERT(oa->o_mds < lmv->count);
1271
1272         oa->o_gr = mea->mea_fids[oa->o_mds].generation;
1273         oa->o_id = mea->mea_fids[oa->o_mds].id;
1274         oa->o_valid =  OBD_MD_FLID | OBD_MD_FLGROUP;
1275         err = obd_brw(rw, lmv->tgts[oa->o_mds].exp, oa,
1276                         NULL, oa_bufs, pgarr, oti);
1277         RETURN(err);
1278 }
1279
1280 struct obd_ops lmv_obd_ops = {
1281         o_owner:                THIS_MODULE,
1282         o_attach:               lmv_attach,
1283         o_detach:               lmv_detach,
1284         o_setup:                lmv_setup,
1285         o_cleanup:              lmv_cleanup,
1286         o_connect:              lmv_connect_fake,
1287         o_disconnect:           lmv_disconnect,
1288         o_statfs:               lmv_statfs,
1289         o_get_info:             lmv_get_info,
1290         o_set_info:             lmv_set_info,
1291         o_create:               lmv_obd_create,
1292         o_packmd:               lmv_packmd,
1293         o_unpackmd:             lmv_unpackmd,
1294         o_brw:                  lmv_brw,
1295         o_init_ea_size:         lmv_init_ea_size,
1296 };
1297
1298 struct md_ops lmv_md_ops = {
1299         m_getstatus:            lmv_getstatus,
1300         m_getattr:              lmv_getattr,
1301         m_change_cbdata:        lmv_change_cbdata,
1302         m_change_cbdata_name:   lmv_change_cbdata_name,
1303         m_close:                lmv_close,
1304         m_create:               lmv_create,
1305         m_done_writing:         lmv_done_writing,
1306         m_enqueue:              lmv_enqueue,
1307         m_getattr_name:         lmv_getattr_name,
1308         m_intent_lock:          lmv_intent_lock,
1309         m_link:                 lmv_link,
1310         m_rename:               lmv_rename,
1311         m_setattr:              lmv_setattr,
1312         m_sync:                 lmv_sync,
1313         m_readpage:             lmv_readpage,
1314         m_unlink:               lmv_unlink,
1315         m_get_real_obd:         lmv_get_real_obd,
1316         m_valid_attrs:          lmv_valid_attrs,
1317 };
1318
1319 int __init lmv_init(void)
1320 {
1321         struct lprocfs_static_vars lvars;
1322         int rc;
1323
1324         lprocfs_init_vars(lmv, &lvars);
1325         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
1326                                  lvars.module_vars, OBD_LMV_DEVICENAME);
1327         RETURN(rc);
1328 }
1329
1330 #ifdef __KERNEL__
1331 static void lmv_exit(void)
1332 {
1333         class_unregister_type(OBD_LMV_DEVICENAME);
1334 }
1335
1336 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1337 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
1338 MODULE_LICENSE("GPL");
1339
1340 module_init(lmv_init);
1341 module_exit(lmv_exit);
1342 #endif