Whamcloud - gitweb
- testing on mountain showed lmv_sync() works properly
[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 /* Error codes:
53  *
54  *  -EINVAL  : UUID can't be found in the LMV's target list
55  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
56  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
57  */
58 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
59                               int activate)
60 {
61         struct obd_device *obd;
62         struct lmv_tgt_desc *tgt;
63         int i, rc = 0;
64         ENTRY;
65
66         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
67                lmv, uuid->uuid, activate);
68
69         spin_lock(&lmv->lmv_lock);
70         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
71                 if (tgt->ltd_exp == NULL)
72                         continue;
73
74                 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
75                        i, tgt->uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
76                 if (strncmp(uuid->uuid, tgt->uuid.uuid, sizeof uuid->uuid) == 0)
77                         break;
78         }
79
80         if (i == lmv->desc.ld_tgt_count)
81                 GOTO(out, rc = -EINVAL);
82
83         obd = class_exp2obd(tgt->ltd_exp);
84         if (obd == NULL)
85                 GOTO(out, rc = -ENOTCONN);
86
87         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
88                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
89                obd->obd_type->typ_name, i);
90         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
91
92         if (tgt->active == activate) {
93                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
94                        activate ? "" : "in");
95                 GOTO(out, rc);
96         }
97
98         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd, activate ? "" : "in");
99
100         tgt->active = activate;
101         if (activate)
102                 lmv->desc.ld_active_tgt_count++;
103         else
104                 lmv->desc.ld_active_tgt_count--;
105
106         EXIT;
107  out:
108         spin_unlock(&lmv->lmv_lock);
109         return rc;
110 }
111
112 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
113                       int active)
114 {
115         int rc;
116         struct obd_uuid *uuid;
117
118         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
119                 CERROR("unexpected notification of %s %s!\n",
120                        watched->obd_type->typ_name,
121                        watched->obd_name);
122                 return -EINVAL;
123         }
124         uuid = &watched->u.cli.cl_import->imp_target_uuid;
125
126         /* Set MDC as active before notifying the observer, so the
127          * observer can use the MDC normally.  
128          */
129         rc = lmv_set_mdc_active(&obd->u.lmv, uuid, active);
130         if (rc) {
131                 CERROR("%sactivation of %s failed: %d\n",
132                        active ? "" : "de", uuid->uuid, rc);
133                 RETURN(rc);
134         }
135
136         if (obd->obd_observer)
137                 /* Pass the notification up the chain. */
138                 rc = obd_notify(obd->obd_observer, watched, active);
139
140         RETURN(rc);
141 }
142
143 int lmv_attach(struct obd_device *dev, obd_count len, void *data)
144 {
145         struct lprocfs_static_vars lvars;
146         int rc;
147         ENTRY;
148
149         lprocfs_init_vars(lmv, &lvars);
150         rc = lprocfs_obd_attach(dev, lvars.obd_vars);
151         if (rc == 0) {
152 #ifdef __KERNEL__
153                 struct proc_dir_entry *entry;
154                 
155                 entry = create_proc_entry("target_obd", 0444, dev->obd_proc_entry);
156                 if (entry == NULL)
157                         RETURN(-ENOMEM);
158                 /* entry->proc_fops = &lmv_proc_target_fops; */
159                 entry->data = dev;
160 #endif
161        }
162         RETURN (rc);
163 }
164
165 int lmv_detach(struct obd_device *dev)
166 {
167         return lprocfs_obd_detach(dev);
168 }
169
170 /* This is fake connect function. Its purpose is to initialize lmv and 
171  * say caller that everything is okay. Real connection will be performed
172  * later. */
173 static int lmv_connect(struct lustre_handle *conn, struct obd_device *obd,
174                        struct obd_uuid *cluuid)
175 {
176         struct lmv_obd *lmv = &obd->u.lmv;
177         struct obd_export *exp;
178         int rc;
179         ENTRY;
180
181         rc = class_connect(conn, obd, cluuid);
182         if (rc) {
183                 CERROR("class_connection() returned %d\n", rc);
184                 RETURN(rc);
185         }
186
187         exp = class_conn2export(conn);
188         /* We don't want to actually do the underlying connections more than
189          * once, so keep track. */
190         lmv->refcount++;
191         if (lmv->refcount > 1) {
192                 class_export_put(exp);
193                 RETURN(0);
194         }
195
196         lmv->cluuid = *cluuid;
197         lmv->connected = 0;
198         lmv->exp = exp;
199
200         RETURN(0);
201 }
202
203 void lmv_set_timeouts(struct obd_device *obd)
204 {
205         struct lmv_tgt_desc *tgts;
206         struct lmv_obd *lmv;
207         int i;
208
209         lmv = &obd->u.lmv;
210         if (lmv->server_timeout == 0)
211                 return;
212
213         if (lmv->connected == 0)
214                 return;
215
216         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
217                 if (tgts->ltd_exp == NULL)
218                         continue;
219                 obd_set_info(tgts->ltd_exp, strlen("inter_mds"),
220                              "inter_mds", 0, NULL);
221         }
222 }
223
224 /* Performs a check if passed obd is connected. If no - connect it. */
225 int lmv_check_connect(struct obd_device *obd) {
226         struct lmv_obd *lmv = &obd->u.lmv;
227         struct obd_uuid *cluuid;
228         struct lmv_tgt_desc *tgts;
229         struct obd_export *exp;
230         int rc, rc2, i;
231
232         if (lmv->connected)
233                 return 0;
234       
235         lmv->connected = 1;
236         cluuid = &lmv->cluuid;
237         exp = lmv->exp;
238         
239         CDEBUG(D_OTHER, "time to connect %s to %s\n",
240                cluuid->uuid, obd->obd_name);
241
242         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
243                 struct obd_device *tgt_obd;
244                 struct obd_uuid lmv_osc_uuid = { "LMV_OSC_UUID" };
245                 struct lustre_handle conn = {0, };
246
247                 LASSERT(tgts != NULL);
248
249                 tgt_obd = class_find_client_obd(&tgts->uuid, LUSTRE_MDC_NAME, 
250                                                 &obd->obd_uuid);
251                 if (!tgt_obd) {
252                         CERROR("Target %s not attached\n", tgts->uuid.uuid);
253                         GOTO(out_disc, rc = -EINVAL);
254                 }
255
256                 /* for MDS: don't connect to yourself */
257                 if (obd_uuid_equals(&tgts->uuid, cluuid)) {
258                         CDEBUG(D_OTHER, "don't connect back to %s\n",
259                                cluuid->uuid);
260                         tgts->ltd_exp = NULL;
261                         continue;
262                 }
263
264                 CDEBUG(D_OTHER, "connect to %s(%s) - %s, %s FOR %s\n",
265                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
266                         tgts->uuid.uuid, obd->obd_uuid.uuid,
267                         cluuid->uuid);
268
269                 if (!tgt_obd->obd_set_up) {
270                         CERROR("Target %s not set up\n", tgts->uuid.uuid);
271                         GOTO(out_disc, rc = -EINVAL);
272                 }
273                 
274                 rc = obd_connect(&conn, tgt_obd, &lmv_osc_uuid);
275                 if (rc) {
276                         CERROR("Target %s connect error %d\n",
277                                 tgts->uuid.uuid, rc);
278                         GOTO(out_disc, rc);
279                 }
280                 tgts->ltd_exp = class_conn2export(&conn);
281
282                 obd_init_ea_size(tgts->ltd_exp, lmv->max_easize,
283                                  lmv->max_cookiesize);
284                 
285                 rc = obd_register_observer(tgt_obd, obd);
286                 if (rc) {
287                         CERROR("Target %s register_observer error %d\n",
288                                tgts->uuid.uuid, rc);
289                         obd_disconnect(tgts->ltd_exp, 0);
290                         GOTO(out_disc, rc);
291                 }
292
293                 lmv->desc.ld_active_tgt_count++;
294                 tgts->active = 1;
295                 
296                 CDEBUG(D_OTHER, "connected to %s(%s) successfully (%d)\n",
297                         tgt_obd->obd_name, tgt_obd->obd_uuid.uuid,
298                         atomic_read(&obd->obd_refcount));
299         }
300
301         lmv_set_timeouts(obd);
302
303         class_export_put(exp);
304         return 0;
305
306  out_disc:
307         while (i-- > 0) {
308                 struct obd_uuid uuid;
309                 --tgts;
310                 --lmv->desc.ld_active_tgt_count;
311                 tgts->active = 0;
312                 /* save for CERROR below; (we know it's terminated) */
313                 uuid = tgts->uuid;
314                 rc2 = obd_disconnect(tgts->ltd_exp, 0);
315                 if (rc2)
316                         CERROR("error: LMV target %s disconnect on MDT idx %d: "
317                                "error %d\n", uuid.uuid, i, rc2);
318         }
319         class_disconnect(exp, 0);
320         RETURN (rc);
321 }
322
323 static int lmv_disconnect(struct obd_export *exp, int flags)
324 {
325         struct obd_device *obd = class_exp2obd(exp);
326         struct lmv_obd *lmv = &obd->u.lmv;
327         int rc, i;
328         ENTRY;
329
330         if (!lmv->tgts)
331                 goto out_local;
332
333         /* Only disconnect the underlying layers on the final disconnect. */
334         lmv->refcount--;
335         if (lmv->refcount != 0)
336                 goto out_local;
337
338         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
339                 if (lmv->tgts[i].ltd_exp == NULL)
340                         continue;
341
342                 if (obd->obd_no_recov) {
343                         /* Pass it on to our clients.
344                          * XXX This should be an argument to disconnect,
345                          * XXX not a back-door flag on the OBD.  Ah well.
346                          */
347                         struct obd_device *mdc_obd;
348                         mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
349                         if (mdc_obd)
350                                 mdc_obd->obd_no_recov = 1;
351                 }
352
353                 CDEBUG(D_OTHER, "disconnected from %s(%s) successfully\n",
354                         lmv->tgts[i].ltd_exp->exp_obd->obd_name,
355                         lmv->tgts[i].ltd_exp->exp_obd->obd_uuid.uuid);
356
357                 obd_register_observer(lmv->tgts[i].ltd_exp->exp_obd, NULL);
358
359                 rc = obd_disconnect(lmv->tgts[i].ltd_exp, flags);
360                 if (rc) {
361                         if (lmv->tgts[i].active) {
362                                 CERROR("Target %s disconnect error %d\n",
363                                        lmv->tgts[i].uuid.uuid, rc);
364                         }
365                         rc = 0;
366                 }
367                 if (lmv->tgts[i].active) {
368                         lmv->desc.ld_active_tgt_count--;
369                         lmv->tgts[i].active = 0;
370                 }
371                 lmv->tgts[i].ltd_exp = NULL;
372         }
373
374 out_local:
375         /* this is the case when no real connection is established by
376          * lmv_check_connect(). */
377         if (!lmv->connected)
378                 class_export_put(exp);
379         rc = class_disconnect(exp, 0);
380         RETURN(rc);
381 }
382
383 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
384                          int len, void *karg, void *uarg)
385 {
386         struct obd_device *obddev = class_exp2obd(exp);
387         struct lmv_obd *lmv = &obddev->u.lmv;
388         int i, rc = 0, set = 0;
389
390         ENTRY;
391
392         if (lmv->desc.ld_tgt_count == 0)
393                 RETURN(-ENOTTY);
394         
395         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
396                 int err;
397
398                 if (lmv->tgts[i].ltd_exp == NULL) {
399                         CWARN("%s: NULL export for %d\n", obddev->obd_name, i);
400                         continue;
401                 }
402
403                 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len, karg, uarg);
404                 if (err) {
405                         if (lmv->tgts[i].active) {
406                                 CERROR("error: iocontrol MDC %s on MDT"
407                                        "idx %d: err = %d\n",
408                                        lmv->tgts[i].uuid.uuid, i, err);
409                                 if (!rc)
410                                         rc = err;
411                         }
412                 } else
413                         set = 1;
414         }
415         if (!set && !rc)
416                 rc = -EIO;
417
418         RETURN(rc);
419 }
420
421 static int lmv_setup(struct obd_device *obd, obd_count len, void *buf)
422 {
423         int i, rc = 0;
424         struct lmv_desc *desc;
425         struct obd_uuid *uuids;
426         struct lmv_tgt_desc *tgts;
427         struct lustre_cfg *lcfg = buf;
428         struct lmv_obd *lmv = &obd->u.lmv;
429         ENTRY;
430
431         if (lcfg->lcfg_inllen1 < 1) {
432                 CERROR("LMV setup requires a descriptor\n");
433                 RETURN(-EINVAL);
434         }
435
436         if (lcfg->lcfg_inllen2 < 1) {
437                 CERROR("LMV setup requires an OST UUID list\n");
438                 RETURN(-EINVAL);
439         }
440
441         desc = (struct lmv_desc *)lcfg->lcfg_inlbuf1;
442         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
443                 CERROR("descriptor size wrong: %d > %d\n",
444                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
445                 RETURN(-EINVAL);
446         }
447
448         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
449         if (sizeof(*uuids) * desc->ld_tgt_count != lcfg->lcfg_inllen2) {
450                 CERROR("UUID array size wrong: %u * %u != %u\n",
451                        sizeof(*uuids), desc->ld_tgt_count, lcfg->lcfg_inllen2);
452                 RETURN(-EINVAL);
453         }
454
455         lmv->bufsize = sizeof(struct lmv_tgt_desc) * desc->ld_tgt_count;
456         OBD_ALLOC(lmv->tgts, lmv->bufsize);
457         if (lmv->tgts == NULL) {
458                 CERROR("Out of memory\n");
459                 RETURN(-ENOMEM);
460         }
461
462         lmv->desc = *desc;
463         spin_lock_init(&lmv->lmv_lock);
464         
465         for (i = 0, tgts = lmv->tgts; i < desc->ld_tgt_count; i++, tgts++)
466                 tgts->uuid = uuids[i];
467         
468         lmv->max_cookiesize = 0;
469
470         lmv->max_easize = sizeof(struct ll_fid) *
471                 desc->ld_tgt_count + sizeof(struct mea);
472         
473         rc = lmv_setup_mgr(obd);
474         if (rc) {
475                 CERROR("Can't setup LMV object manager, "
476                        "error %d.\n", rc);
477                 OBD_FREE(lmv->tgts, lmv->bufsize);
478         }
479
480         RETURN(rc);
481 }
482
483 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
484                       unsigned long max_age)
485 {
486         struct lmv_obd *lmv = &obd->u.lmv;
487         struct obd_statfs temp;
488         int rc = 0, i;
489         ENTRY;
490         
491         rc = lmv_check_connect(obd);
492         if (rc)
493                 RETURN(rc);
494                 
495         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
496                 if (lmv->tgts[i].ltd_exp == NULL) {
497                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
498                         continue;
499                 }
500
501                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, &temp, max_age);
502                 if (rc) {
503                         CERROR("can't stat MDS #%d (%s)\n", i,
504                                lmv->tgts[i].ltd_exp->exp_obd->obd_name);
505                         RETURN(rc);
506                 }
507                 if (i == 0) {
508                         memcpy(osfs, &temp, sizeof(temp));
509                 } else {
510                         osfs->os_bavail += temp.os_bavail;
511                         osfs->os_blocks += temp.os_blocks;
512                         osfs->os_ffree += temp.os_ffree;
513                         osfs->os_files += temp.os_files;
514                 }
515         }
516         RETURN(rc);
517 }
518
519 static int lmv_cleanup(struct obd_device *obd, int flags) 
520 {
521         struct lmv_obd *lmv = &obd->u.lmv;
522         ENTRY;
523         lmv_cleanup_mgr(obd);
524         OBD_FREE(lmv->tgts, lmv->bufsize);
525         RETURN(0);
526 }
527
528 static int lmv_getstatus(struct obd_export *exp, struct ll_fid *fid)
529 {
530         struct obd_device *obd = exp->exp_obd;
531         struct lmv_obd *lmv = &obd->u.lmv;
532         int rc;
533         ENTRY;
534         rc = lmv_check_connect(obd);
535         if (rc)
536                 RETURN(rc);
537         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid);
538         fid->mds = 0;
539         RETURN(rc);
540 }
541
542 static int lmv_getattr(struct obd_export *exp, struct ll_fid *fid,
543                        unsigned long valid, unsigned int ea_size,
544                        struct ptlrpc_request **request)
545 {
546         struct obd_device *obd = exp->exp_obd;
547         struct lmv_obd *lmv = &obd->u.lmv;
548         int rc, i = fid->mds;
549         struct lmv_obj *obj;
550         ENTRY;
551
552         rc = lmv_check_connect(obd);
553         if (rc)
554                 RETURN(rc);
555
556         LASSERT(i < lmv->desc.ld_tgt_count);
557
558         rc = md_getattr(lmv->tgts[i].ltd_exp, fid, valid,
559                         ea_size, request);
560         if (rc)
561                 RETURN(rc);
562         
563         obj = lmv_grab_obj(obd, fid);
564         
565         CDEBUG(D_OTHER, "GETATTR for %lu/%lu/%lu %s\n",
566                (unsigned long)fid->mds, (unsigned long)fid->id,
567                (unsigned long)fid->generation, obj ? "(splitted)" : "");
568
569         /* if object is splitted, then we loop over all the slaves and gather
570          * size attribute. In ideal world we would have to gather also mds field
571          * from all slaves, as object is spread over the cluster and this is
572          * definitely interesting information and it is not good to loss it,
573          * but...*/
574         if (obj) {
575                 struct mds_body *body;
576
577                 if (*request == NULL) {
578                         lmv_put_obj(obj);
579                         RETURN(rc);
580                 }
581                         
582                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
583                                       sizeof(*body));
584                 LASSERT(body != NULL);
585
586                 lmv_lock_obj(obj);
587         
588                 for (i = 0; i < obj->objcount; i++) {
589
590                         if (lmv->tgts[i].ltd_exp == NULL) {
591                                 CWARN("%s: NULL export for %d\n",
592                                       obd->obd_name, i);
593                                 continue;
594                         }
595
596                         /* skip master obj. */
597                         if (fid_equal(&obj->fid, &obj->objs[i].fid))
598                                 continue;
599                         
600                         body->size += obj->objs[i].size;
601                 }
602
603                 lmv_unlock_obj(obj);
604                 lmv_put_obj(obj);
605         }
606         
607         RETURN(rc);
608 }
609
610 static int lmv_change_cbdata(struct obd_export *exp, struct ll_fid *fid, 
611                              ldlm_iterator_t it, void *data)
612 {
613         struct obd_device *obd = exp->exp_obd;
614         struct lmv_obd *lmv = &obd->u.lmv;
615         int rc = 0;
616         ENTRY;
617         
618         rc = lmv_check_connect(obd);
619         if (rc)
620                 RETURN(rc);
621         
622         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu\n", (unsigned long)fid->mds,
623                (unsigned long)fid->id, (unsigned long)fid->generation);
624         
625         LASSERT(fid->mds < lmv->desc.ld_tgt_count);
626
627         rc = md_change_cbdata(lmv->tgts[fid->mds].ltd_exp,
628                               fid, it, data);
629         
630         RETURN(rc);
631 }
632
633 static int lmv_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
634                                   char *name, int len, struct ll_fid *cfid,
635                                   ldlm_iterator_t it, void *data)
636 {
637         struct obd_device *obd = exp->exp_obd;
638         struct lmv_obd *lmv = &obd->u.lmv;
639         struct lmv_obj *obj;
640         int rc = 0, mds;
641         ENTRY;
642
643         rc = lmv_check_connect(obd);
644         if (rc)
645                 RETURN(rc);
646
647         LASSERT(pfid->mds < lmv->desc.ld_tgt_count);
648         LASSERT(cfid->mds < lmv->desc.ld_tgt_count);
649         
650         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu:%*s -> %lu/%lu/%lu\n",
651                (unsigned long)pfid->mds, (unsigned long)pfid->id,
652                (unsigned long)pfid->generation, len, name,
653                (unsigned long)cfid->mds, (unsigned long)cfid->id,
654                (unsigned long)cfid->generation);
655
656         /* this is default mds for directory name belongs to. */
657         mds = pfid->mds;
658         obj = lmv_grab_obj(obd, pfid);
659         if (obj) {
660                 /* directory is splitted. look for right mds for this name. */
661                 mds = raw_name2idx(obj->objcount, name, len);
662                 mds = obj->objs[mds].fid.mds;
663                 lmv_put_obj(obj);
664         }
665         rc = md_change_cbdata(lmv->tgts[mds].ltd_exp, cfid, it, data);
666         RETURN(rc);
667 }
668
669 static int lmv_valid_attrs(struct obd_export *exp, struct ll_fid *fid) 
670 {
671         struct obd_device *obd = exp->exp_obd;
672         struct lmv_obd *lmv = &obd->u.lmv;
673         int rc = 0;
674         ENTRY;
675         rc = lmv_check_connect(obd);
676         if (rc)
677                 RETURN(rc);
678         CDEBUG(D_OTHER, "validate %lu/%lu/%lu\n", (unsigned long) fid->mds,
679                (unsigned long) fid->id, (unsigned long) fid->generation);
680         LASSERT(fid->mds < lmv->desc.ld_tgt_count);
681         rc = md_valid_attrs(lmv->tgts[fid->mds].ltd_exp, fid);
682         RETURN(rc);
683 }
684
685 int lmv_close(struct obd_export *exp, struct obdo *obdo,
686                   struct obd_client_handle *och,
687                   struct ptlrpc_request **request)
688 {
689         struct obd_device *obd = exp->exp_obd;
690         struct lmv_obd *lmv = &obd->u.lmv;
691         int rc, i = obdo->o_mds;
692         ENTRY;
693         rc = lmv_check_connect(obd);
694         if (rc)
695                 RETURN(rc);
696         LASSERT(i < lmv->desc.ld_tgt_count);
697         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long) obdo->o_mds,
698                (unsigned long) obdo->o_id, (unsigned long) obdo->o_generation);
699         rc = md_close(lmv->tgts[i].ltd_exp, obdo, och, request);
700         RETURN(rc);
701 }
702
703 int lmv_get_mea_and_update_object(struct obd_export *exp, struct ll_fid *fid)
704 {
705         struct obd_device *obd = exp->exp_obd;
706         struct lmv_obd *lmv = &obd->u.lmv;
707         struct ptlrpc_request *req = NULL;
708         struct lmv_obj *obj;
709         struct lustre_md md;
710         unsigned long valid;
711         int mealen, rc;
712
713         md.mea = NULL;
714         mealen = MEA_SIZE_LMV(lmv);
715         
716         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
717
718         /* time to update mea of parent fid */
719         rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
720                         valid, mealen, &req);
721         if (rc) {
722                 CERROR("md_getattr() failed, error %d\n", rc);
723                 GOTO(cleanup, rc);
724         }
725
726         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
727         if (rc) {
728                 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
729                 GOTO(cleanup, rc);
730         }
731
732         if (md.mea == NULL)
733                 GOTO(cleanup, rc = -ENODATA);
734
735         obj = lmv_create_obj(exp, fid, md.mea);
736         if (IS_ERR(obj))
737                 rc = PTR_ERR(obj);
738         
739         lmv_put_obj(obj);
740         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
741
742 cleanup:
743         if (req)
744                 ptlrpc_req_finished(req);
745         RETURN(rc);
746 }
747
748 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
749                const void *data, int datalen, int mode, __u32 uid,
750                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
751 {
752         struct obd_device *obd = exp->exp_obd;
753         struct lmv_obd *lmv = &obd->u.lmv;
754         struct mds_body *body;
755         struct lmv_obj *obj;
756         int rc, mds, loop = 0;
757         ENTRY;
758
759         rc = lmv_check_connect(obd);
760         if (rc)
761                 RETURN(rc);
762
763         if (!lmv->desc.ld_active_tgt_count)
764                 RETURN(-EIO);
765 repeat:
766         LASSERT(++loop <= 2);
767         obj = lmv_grab_obj(obd, &op_data->fid1);
768         if (obj) {
769                 mds = raw_name2idx(obj->objcount, op_data->name,
770                                    op_data->namelen);
771                 op_data->fid1 = obj->objs[mds].fid;
772                 lmv_put_obj(obj);
773         }
774
775         CDEBUG(D_OTHER, "CREATE '%*s' on %lu/%lu/%lu\n", op_data->namelen,
776                op_data->name, (unsigned long)op_data->fid1.mds,
777                (unsigned long)op_data->fid1.id,
778                (unsigned long)op_data->fid1.generation);
779         
780         rc = md_create(lmv->tgts[op_data->fid1.mds].ltd_exp, op_data, data,
781                        datalen, mode, uid, gid, rdev, request);
782         if (rc == 0) {
783                 if (*request == NULL)
784                         RETURN(rc);
785
786                 body = lustre_msg_buf((*request)->rq_repmsg, 0,
787                                       sizeof(*body));
788                 LASSERT(body != NULL);
789                 
790                 CDEBUG(D_OTHER, "created. id = %lu, generation = %lu, "
791                        "mds = %d\n", (unsigned long)body->fid1.id,
792                        (unsigned long)body->fid1.generation, op_data->fid1.mds);
793                 
794                 LASSERT(body->valid & OBD_MD_MDS ||
795                         body->mds == op_data->fid1.mds);
796         } else if (rc == -ERESTART) {
797                 /* directory got splitted. time to update local object and
798                  * repeat the request with proper MDS */
799                 rc = lmv_get_mea_and_update_object(exp, &op_data->fid1);
800                 if (rc == 0) {
801                         ptlrpc_req_finished(*request);
802                         goto repeat;
803                 }
804         }
805         RETURN(rc);
806 }
807
808 int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
809 {
810         struct obd_device *obd = exp->exp_obd;
811         struct lmv_obd *lmv = &obd->u.lmv;
812         int rc;
813         ENTRY;
814         rc = lmv_check_connect(obd);
815         if (rc)
816                 RETURN(rc);
817
818         /* FIXME: choose right MDC here */
819         CWARN("this method isn't implemented yet\n");
820         rc = md_done_writing(lmv->tgts[0].ltd_exp, obdo);
821         RETURN(rc);
822 }
823
824 int lmv_enqueue_slaves(struct obd_export *exp, int locktype,
825                        struct lookup_intent *it, int lockmode,
826                        struct mdc_op_data *data, struct lustre_handle *lockh,
827                        void *lmm, int lmmsize, ldlm_completion_callback cb_completion,
828                        ldlm_blocking_callback cb_blocking, void *cb_data)
829 {
830         struct obd_device *obd = exp->exp_obd;
831         struct lmv_obd *lmv = &obd->u.lmv;
832         struct mea *mea = data->mea1;
833         struct mdc_op_data data2;
834         int i, rc, mds;
835         ENTRY;
836
837         LASSERT(mea != NULL);
838         for (i = 0; i < mea->mea_count; i++) {
839                 memset(&data2, 0, sizeof(data2));
840                 data2.fid1 = mea->mea_fids[i];
841                 mds = data2.fid1.mds;
842                 
843                 if (lmv->tgts[mds].ltd_exp == NULL)
844                         continue;
845
846                 rc = md_enqueue(lmv->tgts[mds].ltd_exp, locktype, it, lockmode,
847                                 &data2, lockh + i, lmm, lmmsize, cb_completion,
848                                 cb_blocking, cb_data);
849                 
850                 CDEBUG(D_OTHER, "take lock on slave %lu/%lu/%lu -> %d/%d\n",
851                        (unsigned long)mea->mea_fids[i].mds,
852                        (unsigned long)mea->mea_fids[i].id,
853                        (unsigned long)mea->mea_fids[i].generation,
854                        rc, it->d.lustre.it_status);
855                 if (rc)
856                         GOTO(cleanup, rc);
857                 if (it->d.lustre.it_data) {
858                         struct ptlrpc_request *req;
859                         req = (struct ptlrpc_request *) it->d.lustre.it_data;
860                         ptlrpc_req_finished(req);
861                 }
862                 
863                 if (it->d.lustre.it_status)
864                         GOTO(cleanup, rc = it->d.lustre.it_status);
865         }
866         RETURN(0);
867         
868 cleanup:
869         /* drop all taken locks */
870         while (--i >= 0) {
871                 if (lockh[i].cookie)
872                         ldlm_lock_decref(lockh + i, lockmode);
873                 lockh[i].cookie = 0;
874         }
875         RETURN(rc);
876 }
877
878 int lmv_enqueue(struct obd_export *exp, int lock_type,
879                 struct lookup_intent *it, int lock_mode,
880                 struct mdc_op_data *data, struct lustre_handle *lockh,
881                 void *lmm, int lmmsize, ldlm_completion_callback cb_completion,
882                 ldlm_blocking_callback cb_blocking, void *cb_data)
883 {
884         struct obd_device *obd = exp->exp_obd;
885         struct lmv_obd *lmv = &obd->u.lmv;
886         struct lmv_obj *obj;
887         int rc, mds;
888         ENTRY;
889
890         rc = lmv_check_connect(obd);
891         if (rc)
892                 RETURN(rc);
893
894         if (it->it_op == IT_UNLINK) {
895                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
896                                         data, lockh, lmm, lmmsize,
897                                         cb_completion, cb_blocking, cb_data);
898                 RETURN(rc);
899         }
900
901         if (data->namelen) {
902                 obj = lmv_grab_obj(obd, &data->fid1);
903                 if (obj) {
904                         /* directory is splitted. look for right mds for this
905                          * name */
906                         mds = raw_name2idx(obj->objcount, (char *)data->name,
907                                            data->namelen);
908                         data->fid1 = obj->objs[mds].fid;
909                         lmv_put_obj(obj);
910                 }
911         }
912         CDEBUG(D_OTHER, "ENQUEUE '%s' on %lu/%lu\n", LL_IT2STR(it),
913                (unsigned long)data->fid1.id, (unsigned long)data->fid1.generation);
914         
915         rc = md_enqueue(lmv->tgts[data->fid1.mds].ltd_exp, lock_type, it,
916                         lock_mode, data, lockh, lmm, lmmsize, cb_completion,
917                         cb_blocking, cb_data);
918
919         RETURN(rc);
920 }
921
922 int lmv_getattr_name(struct obd_export *exp, struct ll_fid *fid,
923                      char *filename, int namelen, unsigned long valid,
924                      unsigned int ea_size, struct ptlrpc_request **request)
925 {
926         struct obd_device *obd = exp->exp_obd;
927         struct lmv_obd *lmv = &obd->u.lmv;
928         struct ll_fid rfid = *fid;
929         int rc, mds = fid->mds, loop = 0;
930         struct mds_body *body;
931         struct lmv_obj *obj;
932         ENTRY;
933         rc = lmv_check_connect(obd);
934         if (rc)
935                 RETURN(rc);
936 repeat:
937         LASSERT(++loop <= 2);
938         obj = lmv_grab_obj(obd, fid);
939         if (obj) {
940                 /* directory is splitted. look for right mds for this name */
941                 mds = raw_name2idx(obj->objcount, filename, namelen - 1);
942                 rfid = obj->objs[mds].fid;
943                 lmv_put_obj(obj);
944         }
945         
946         CDEBUG(D_OTHER, "getattr_name for %*s on %lu/%lu/%lu -> %lu/%lu/%lu\n",
947                namelen, filename, (unsigned long)fid->mds,
948                (unsigned long)fid->id, (unsigned long)fid->generation,
949                (unsigned long)rfid.mds, (unsigned long)rfid.id,
950                (unsigned long)rfid.generation);
951
952         rc = md_getattr_name(lmv->tgts[rfid.mds].ltd_exp, &rfid, filename,
953                              namelen, valid, ea_size, request);
954         if (rc == 0) {
955                 /* this could be cross-node reference. in this case all we have
956                  * right now is mds/ino/generation triple. we'd like to find
957                  * other attributes */
958                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
959                 LASSERT(body != NULL);
960                 if (body->valid & OBD_MD_MDS) {
961                         struct ptlrpc_request *req = NULL;
962                         rfid = body->fid1;
963                         CDEBUG(D_OTHER, "request attrs for %lu/%lu/%lu\n",
964                                (unsigned long) rfid.mds,
965                                (unsigned long) rfid.id,
966                                (unsigned long) rfid.generation);
967                         rc = md_getattr_name(lmv->tgts[rfid.mds].ltd_exp, &rfid,
968                                              NULL, 1, valid, ea_size, &req);
969                         ptlrpc_req_finished(*request);
970                         *request = req;
971                 }
972         } else if (rc == -ERESTART) {
973                 /* directory got splitted. time to update local object and
974                  * repeat the request with proper MDS */
975                 rc = lmv_get_mea_and_update_object(exp, &rfid);
976                 if (rc == 0) {
977                         ptlrpc_req_finished(*request);
978                         goto repeat;
979                 }
980         }
981         RETURN(rc);
982 }
983
984
985 /*
986  * llite passes fid of an target inode in data->fid1 and fid of directory in
987  * data->fid2
988  */
989 int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
990              struct ptlrpc_request **request)
991 {
992         struct obd_device *obd = exp->exp_obd;
993         struct lmv_obd *lmv = &obd->u.lmv;
994         struct lmv_obj *obj;
995         int rc;
996         ENTRY;
997         
998         rc = lmv_check_connect(obd);
999         if (rc)
1000                 RETURN(rc);
1001
1002         if (data->namelen != 0) {
1003                 /* usual link request */
1004                 obj = lmv_grab_obj(obd, &data->fid1);
1005                 if (obj) {
1006                         rc = raw_name2idx(obj->objcount, data->name,
1007                                           data->namelen);
1008                         data->fid1 = obj->objs[rc].fid;
1009                         lmv_put_obj(obj);
1010                 }
1011                 
1012                 CDEBUG(D_OTHER,"link %lu/%lu/%lu:%*s to %lu/%lu/%lu mds %lu\n",
1013                        (unsigned long)data->fid2.mds,
1014                        (unsigned long)data->fid2.id,
1015                        (unsigned long)data->fid2.generation,
1016                        data->namelen, data->name,
1017                        (unsigned long)data->fid1.mds,
1018                        (unsigned long)data->fid1.id,
1019                        (unsigned long)data->fid1.generation,
1020                        (unsigned long)data->fid1.mds);
1021         } else {
1022                 /* request from MDS to acquire i_links for inode by fid1 */
1023                 CDEBUG(D_OTHER, "inc i_nlinks for %lu/%lu/%lu\n",
1024                        (unsigned long)data->fid1.mds,
1025                        (unsigned long)data->fid1.id,
1026                        (unsigned long)data->fid1.generation);
1027         }
1028                         
1029         rc = md_link(lmv->tgts[data->fid1.mds].ltd_exp, data, request);
1030         RETURN(rc);
1031 }
1032
1033 int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
1034                const char *old, int oldlen, const char *new, int newlen,
1035                struct ptlrpc_request **request)
1036 {
1037         struct obd_device *obd = exp->exp_obd;
1038         struct lmv_obd *lmv = &obd->u.lmv;
1039         struct lmv_obj *obj;
1040         int rc, mds;
1041         ENTRY;
1042
1043         CDEBUG(D_OTHER, "rename %*s in %lu/%lu/%lu to %*s in %lu/%lu/%lu\n",
1044                oldlen, old, (unsigned long)data->fid1.mds,
1045                (unsigned long)data->fid1.id,
1046                (unsigned long)data->fid1.generation,
1047                newlen, new, (unsigned long) data->fid2.mds,
1048                (unsigned long) data->fid2.id,
1049                (unsigned long) data->fid2.generation);
1050         
1051         if (!fid_equal(&data->fid1, &data->fid2))
1052                 CWARN("cross-node rename %lu/%lu/%lu:%*s to %lu/%lu/%lu:%*s\n",
1053                       (unsigned long)data->fid1.mds,
1054                       (unsigned long)data->fid1.id,
1055                       (unsigned long)data->fid1.generation, oldlen, old,
1056                       (unsigned long)data->fid2.mds,
1057                       (unsigned long)data->fid2.id,
1058                       (unsigned long)data->fid2.generation, newlen, new);
1059
1060         rc = lmv_check_connect(obd);
1061         if (rc)
1062                 RETURN(rc);
1063
1064         if (oldlen == 0) {
1065                 /* MDS with old dir entry is asking another MDS to create name
1066                  * there */
1067                 CDEBUG(D_OTHER,
1068                        "create %*s(%d/%d) in %lu/%lu/%lu pointing to %lu/%lu/%lu\n",
1069                        newlen, new, oldlen, newlen,
1070                        (unsigned long)data->fid2.mds,
1071                        (unsigned long)data->fid2.id,
1072                        (unsigned long)data->fid2.generation,
1073                        (unsigned long)data->fid1.mds,
1074                        (unsigned long)data->fid1.id,
1075                        (unsigned long)data->fid1.generation);
1076                 mds = data->fid2.mds;
1077                 goto request;
1078         }
1079
1080         obj = lmv_grab_obj(obd, &data->fid1);
1081         if (obj) {
1082                 /* directory is already splitted, so we have to forward request
1083                  * to the right MDS */
1084                 mds = raw_name2idx(obj->objcount, (char *)old, oldlen);
1085                 data->fid1 = obj->objs[mds].fid;
1086                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
1087                        (unsigned long)obj->objs[mds].fid.mds,
1088                        (unsigned long)obj->objs[mds].fid.id,
1089                        (unsigned long)obj->objs[mds].fid.generation);
1090                 lmv_put_obj(obj);
1091         }
1092
1093         obj = lmv_grab_obj(obd, &data->fid2);
1094         if (obj) {
1095                 /* directory is already splitted, so we have to forward request
1096                  * to the right MDS */
1097                 mds = raw_name2idx(obj->objcount, (char *)new, newlen);
1098                 data->fid2 = obj->objs[mds].fid;
1099                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
1100                        (unsigned long)obj->objs[mds].fid.mds,
1101                        (unsigned long)obj->objs[mds].fid.id,
1102                        (unsigned long)obj->objs[mds].fid.generation);
1103                 lmv_put_obj(obj);
1104         }
1105         
1106         mds = data->fid1.mds;
1107
1108 request:
1109         rc = md_rename(lmv->tgts[mds].ltd_exp, data, old, oldlen,
1110                        new, newlen, request); 
1111         RETURN(rc);
1112 }
1113
1114 int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
1115                 struct iattr *iattr, void *ea, int ealen, void *ea2,
1116                 int ea2len, struct ptlrpc_request **request)
1117 {
1118         struct obd_device *obd = exp->exp_obd;
1119         struct lmv_obd *lmv = &obd->u.lmv;
1120         struct ptlrpc_request *req;
1121         struct mds_body *body;
1122         struct lmv_obj *obj;
1123         int rc = 0, i;
1124         ENTRY;
1125
1126         rc = lmv_check_connect(obd);
1127         if (rc)
1128                 RETURN(rc);
1129
1130         obj = lmv_grab_obj(obd, &data->fid1);
1131         
1132         CDEBUG(D_OTHER, "SETATTR for %lu/%lu/%lu, valid 0x%x%s\n",
1133                (unsigned long)data->fid1.mds, (unsigned long)data->fid1.id,
1134                (unsigned long)data->fid1.generation, iattr->ia_valid,
1135                obj ? ", splitted" : "");
1136         
1137         if (obj) {
1138                 for (i = 0; i < obj->objcount; i++) {
1139                         data->fid1 = obj->objs[i].fid;
1140                         
1141                         rc = md_setattr(lmv->tgts[data->fid1.mds].ltd_exp, data,
1142                                         iattr, ea, ealen, ea2, ea2len, &req);
1143
1144                         if (fid_equal(&obj->fid, &obj->objs[i].fid)) {
1145                                 /* this is master object and this request should
1146                                  * be returned back to llite */
1147                                 *request = req;
1148                         } else {
1149                                 ptlrpc_req_finished(req);
1150                         }
1151
1152                         if (rc)
1153                                 break;
1154                 }
1155                 lmv_put_obj(obj);
1156         } else {
1157                 LASSERT(data->fid1.mds < lmv->desc.ld_tgt_count);
1158                 rc = md_setattr(lmv->tgts[data->fid1.mds].ltd_exp, data,
1159                                 iattr, ea, ealen, ea2, ea2len, request); 
1160                 if (rc == 0) {
1161                         body = lustre_msg_buf((*request)->rq_repmsg, 0,
1162                                               sizeof(*body));
1163                         LASSERT(body != NULL);
1164                         LASSERT(body->mds == data->fid1.mds);
1165                 }
1166         }
1167         RETURN(rc);
1168 }
1169
1170 int lmv_sync(struct obd_export *exp, struct ll_fid *fid,
1171              struct ptlrpc_request **request)
1172 {
1173         struct obd_device *obd = exp->exp_obd;
1174         struct lmv_obd *lmv = &obd->u.lmv;
1175         int rc;
1176         ENTRY;
1177
1178         rc = lmv_check_connect(obd);
1179         if (rc)
1180                 RETURN(rc);
1181
1182         rc = md_sync(lmv->tgts[fid->mds].ltd_exp, fid, request); 
1183         RETURN(rc);
1184 }
1185
1186 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1187                             void *data, int flag)
1188 {
1189         struct lustre_handle lockh;
1190         struct lmv_obj *obj;
1191         int rc;
1192         ENTRY;
1193
1194         switch (flag) {
1195         case LDLM_CB_BLOCKING:
1196                 ldlm_lock2handle(lock, &lockh);
1197                 rc = ldlm_cli_cancel(&lockh);
1198                 if (rc < 0) {
1199                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1200                         RETURN(rc);
1201                 }
1202                 break;
1203         case LDLM_CB_CANCELING:
1204                 /* time to drop cached attrs for dirobj */
1205                 obj = lock->l_ast_data;
1206                 if (obj) {
1207                         CDEBUG(D_OTHER, "cancel %s on %lu/%lu, master %lu/%lu/%lu\n",
1208                                lock->l_resource->lr_name.name[3] == 1 ? "LOOKUP" : "UPDATE",
1209                                (unsigned long)lock->l_resource->lr_name.name[0],
1210                                (unsigned long)lock->l_resource->lr_name.name[1],
1211                                (unsigned long)obj->fid.mds, (unsigned long)obj->fid.id,
1212                                (unsigned long)obj->fid.generation);
1213                         lmv_put_obj(obj);
1214                 }
1215                 break;
1216         default:
1217                 LBUG();
1218         }
1219         RETURN(0);
1220 }
1221
1222 void lmv_remove_dots(struct page *page)
1223 {
1224         char *kaddr = page_address(page);
1225         unsigned limit = PAGE_CACHE_SIZE;
1226         unsigned offs, rec_len;
1227         struct ext2_dir_entry_2 *p;
1228
1229         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
1230                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
1231                 rec_len = le16_to_cpu(p->rec_len);
1232
1233                 if ((p->name_len == 1 && p->name[0] == '.') ||
1234                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
1235                         p->inode = 0;
1236         }
1237 }
1238
1239 int lmv_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
1240                  __u64 offset, struct page *page,
1241                  struct ptlrpc_request **request)
1242 {
1243         struct obd_device *obd = exp->exp_obd;
1244         struct lmv_obd *lmv = &obd->u.lmv;
1245         struct ll_fid rfid = *mdc_fid;
1246         struct lmv_obj *obj;
1247         int rc, i;
1248         ENTRY;
1249
1250         rc = lmv_check_connect(obd);
1251         if (rc)
1252                 RETURN(rc);
1253
1254         LASSERT(mdc_fid->mds < lmv->desc.ld_tgt_count);
1255         CDEBUG(D_OTHER, "READPAGE at %llu from %lu/%lu/%lu\n",
1256                offset, (unsigned long) rfid.mds,
1257                (unsigned long) rfid.id,
1258                (unsigned long) rfid.generation);
1259
1260         obj = lmv_grab_obj(obd, mdc_fid);
1261         if (obj) {
1262                 lmv_lock_obj(obj);
1263
1264                 /* find dirobj containing page with requested offset. */
1265                 for (i = 0; i < obj->objcount; i++) {
1266                         if (offset < obj->objs[i].size)
1267                                 break;
1268                         offset -= obj->objs[i].size;
1269                 }
1270                 rfid = obj->objs[i].fid;
1271                 
1272                 lmv_unlock_obj(obj);
1273                 lmv_put_obj(obj);
1274                 
1275                 CDEBUG(D_OTHER, "forward to %lu/%lu/%lu with offset %lu\n",
1276                        (unsigned long)rfid.mds, (unsigned long)rfid.id,
1277                        (unsigned long)rfid.generation, (unsigned long)offset);
1278         }
1279         rc = md_readpage(lmv->tgts[rfid.mds].ltd_exp, &rfid, offset,
1280                          page, request);
1281         
1282         if (rc == 0 && !fid_equal(&rfid, mdc_fid))
1283                 /* this page isn't from master object. To avoid "." and ".." 
1284                  * duplication in directory, we have to remove them from all
1285                  * slave objects */
1286                 lmv_remove_dots(page);
1287         
1288         RETURN(rc);
1289 }
1290
1291 int lmv_unlink_slaves(struct obd_export *exp, struct mdc_op_data *data,
1292                       struct ptlrpc_request **req)
1293 {
1294         struct obd_device *obd = exp->exp_obd;
1295         struct lmv_obd *lmv = &obd->u.lmv;
1296         struct mea *mea = data->mea1;
1297         struct mdc_op_data data2;
1298         int i, rc = 0, mds;
1299         ENTRY;
1300
1301         LASSERT(mea != NULL);
1302         for (i = 0; i < mea->mea_count; i++) {
1303                 memset(&data2, 0, sizeof(data2));
1304                 data2.fid1 = mea->mea_fids[i];
1305                 data2.create_mode = MDS_MODE_DONT_LOCK | S_IFDIR;
1306                 mds = data2.fid1.mds;
1307
1308                 if (lmv->tgts[mds].ltd_exp == NULL)
1309                         continue;
1310
1311                 rc = md_unlink(lmv->tgts[mds].ltd_exp, &data2, req);
1312                 CDEBUG(D_OTHER, "unlink slave %lu/%lu/%lu -> %d\n",
1313                        (unsigned long) mea->mea_fids[i].mds,
1314                        (unsigned long) mea->mea_fids[i].id,
1315                        (unsigned long) mea->mea_fids[i].generation, rc);
1316                 if (*req) {
1317                         ptlrpc_req_finished(*req);
1318                         *req = NULL;
1319                 }
1320                 if (rc)
1321                         break;
1322         }
1323         RETURN(rc);
1324 }
1325
1326 int lmv_delete_object(struct obd_export *exp, struct ll_fid *fid)
1327 {
1328         ENTRY;
1329
1330         if (!lmv_delete_obj(exp, fid)) {
1331                 CDEBUG(D_OTHER, "Object %lu/%lu/%lu is not found.\n",
1332                        (unsigned long)fid->mds, (unsigned long)fid->id,
1333                        (unsigned long)fid->generation);
1334         }
1335         
1336         RETURN(0);
1337 }
1338
1339 int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
1340                struct ptlrpc_request **request)
1341 {
1342         struct obd_device *obd = exp->exp_obd;
1343         struct lmv_obd *lmv = &obd->u.lmv;
1344         int rc, i = 0;
1345         ENTRY;
1346         
1347         rc = lmv_check_connect(obd);
1348         if (rc)
1349                 RETURN(rc);
1350
1351         if (data->namelen == 0 && data->mea1 != NULL) {
1352                 /* mds asks to remove slave objects */
1353                 rc = lmv_unlink_slaves(exp, data, request);
1354                 RETURN(rc);
1355         } else if (data->namelen != 0) {
1356                 struct lmv_obj *obj;
1357                 
1358                 obj = lmv_grab_obj(obd, &data->fid1);
1359                 if (obj) {
1360                         i = raw_name2idx(obj->objcount, data->name,
1361                                          data->namelen);
1362                         data->fid1 = obj->objs[i].fid;
1363                         lmv_put_obj(obj);
1364                 }
1365                 CDEBUG(D_OTHER, "unlink '%*s' in %lu/%lu/%lu -> %u\n",
1366                        data->namelen, data->name,
1367                        (unsigned long) data->fid1.mds,
1368                        (unsigned long) data->fid1.id,
1369                        (unsigned long) data->fid1.generation, i);
1370         } else {
1371                 CDEBUG(D_OTHER, "drop i_nlink on %lu/%lu/%lu\n",
1372                        (unsigned long) data->fid1.mds,
1373                        (unsigned long) data->fid1.id,
1374                        (unsigned long) data->fid1.generation);
1375         }
1376         rc = md_unlink(lmv->tgts[data->fid1.mds].ltd_exp, data, request); 
1377         RETURN(rc);
1378 }
1379
1380 struct obd_device *lmv_get_real_obd(struct obd_export *exp,
1381                                     char *name, int len)
1382 {
1383         struct obd_device *obd = exp->exp_obd;
1384         struct lmv_obd *lmv = &obd->u.lmv;
1385         int rc;
1386         ENTRY;
1387
1388         rc = lmv_check_connect(obd);
1389         if (rc)
1390                 RETURN(ERR_PTR(rc));
1391 #warning "we need well-desgined readdir() implementation to remove this mess"
1392         obd = lmv->tgts[0].ltd_exp->exp_obd;
1393         EXIT;
1394         return obd;
1395 }
1396
1397 int lmv_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1398 {
1399         struct obd_device *obd = exp->exp_obd;
1400         struct lmv_obd *lmv = &obd->u.lmv;
1401         int i, rc = 0, change = 0;
1402         ENTRY;
1403
1404         if (lmv->max_easize < easize) {
1405                 lmv->max_easize = easize;
1406                 change = 1;
1407         }
1408         if (lmv->max_cookiesize < cookiesize) {
1409                 lmv->max_cookiesize = cookiesize;
1410                 change = 1;
1411         }
1412         if (change == 0)
1413                 RETURN(0);
1414         
1415         if (lmv->connected == 0)
1416                 RETURN(0);
1417
1418         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1419                 if (lmv->tgts[i].ltd_exp == NULL) {
1420                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
1421                         continue;
1422                 }
1423
1424                 rc = obd_init_ea_size(lmv->tgts[i].ltd_exp, easize, cookiesize);
1425                 if (rc) {
1426                         CERROR("obd_init_ea_size() failed on MDT target %d, "
1427                                "error %d.\n", i, rc);
1428                         break;
1429                 }
1430         }
1431         RETURN(rc);
1432 }
1433
1434 int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1435                           struct lov_stripe_md **ea, struct obd_trans_info *oti)
1436 {
1437         struct obd_device *obd = exp->exp_obd;
1438         struct lmv_obd *lmv = &obd->u.lmv;
1439         struct lov_stripe_md obj_md;
1440         struct lov_stripe_md *obj_mdp = &obj_md;
1441         int rc = 0;
1442         ENTRY;
1443
1444         rc = lmv_check_connect(obd);
1445         if (rc)
1446                 RETURN(rc);
1447
1448         LASSERT(ea == NULL);
1449         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1450
1451         rc = obd_create(lmv->tgts[oa->o_mds].ltd_exp, oa, &obj_mdp, oti);
1452
1453         RETURN(rc);
1454 }
1455
1456 /*
1457  * to be called from MDS only
1458  */
1459 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1460                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1461 {
1462         struct obd_device *obd = exp->exp_obd;
1463         struct lmv_obd *lmv = &obd->u.lmv;
1464         int i, c, rc = 0;
1465         struct mea *mea;
1466         struct ll_fid mfid;
1467         int lcount;
1468         ENTRY;
1469
1470         rc = lmv_check_connect(obd);
1471         if (rc)
1472                 RETURN(rc);
1473
1474         LASSERT(oa != NULL);
1475         
1476         if (ea == NULL) {
1477                 rc = lmv_obd_create_single(exp, oa, NULL, oti);
1478                 RETURN(rc);
1479         }
1480
1481         if (*ea == NULL) {
1482                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1483                 if (rc < 0) {
1484                         CERROR("obd_alloc_diskmd() failed, error %d\n",
1485                                rc);
1486                         RETURN(rc);
1487                 }
1488                 
1489                 if (*ea == NULL)
1490                         RETURN(-EINVAL);
1491         }
1492
1493         rc = 0;
1494         mfid.id = oa->o_id;
1495         mfid.generation = oa->o_generation;
1496         
1497         mea = (struct mea *)*ea;
1498         if (!mea->mea_count || mea->mea_count > lmv->desc.ld_tgt_count)
1499                 mea->mea_count = lmv->desc.ld_tgt_count;
1500
1501         mea->mea_master = -1;
1502         lcount = lmv->desc.ld_tgt_count;
1503         for (i = 0, c = 0; c < mea->mea_count && i < lcount; i++) {
1504                 struct lov_stripe_md obj_md;
1505                 struct lov_stripe_md *obj_mdp = &obj_md;
1506                
1507                 if (lmv->tgts[i].ltd_exp == NULL) {
1508                         /* this is master MDS */
1509                         mea->mea_fids[c].id = mfid.id;
1510                         mea->mea_fids[c].generation = mfid.generation;
1511                         mea->mea_fids[c].mds = i;
1512                         mea->mea_master = i;
1513                         c++;
1514                         continue;
1515                 }
1516
1517                 /* "master" MDS should always be part of stripped dir, so scan
1518                    for it. */
1519                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1520                         continue;
1521
1522                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE
1523                         | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1524
1525                 rc = obd_create(lmv->tgts[c].ltd_exp, oa, &obj_mdp, oti);
1526                 if (rc) {
1527                         CERROR("obd_create() failed on MDT target %d, "
1528                                "error %d\n", c, rc);
1529                         RETURN(rc);
1530                 }
1531
1532                 mea->mea_fids[c].id = oa->o_id;
1533                 mea->mea_fids[c].generation = oa->o_generation;
1534                 mea->mea_fids[c].mds = i;
1535                 c++;
1536                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1537                        i, oa->o_id, oa->o_generation);
1538         }
1539         LASSERT(c == mea->mea_count);
1540         CDEBUG(D_OTHER, "%d dirobjects created\n", (int) mea->mea_count);
1541
1542         RETURN(rc);
1543 }
1544
1545 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1546                         void *key, __u32 *vallen, void *val)
1547 {
1548         struct obd_device *obd;
1549         struct lmv_obd *lmv;
1550         ENTRY;
1551
1552         obd = class_exp2obd(exp);
1553         if (obd == NULL) {
1554                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1555                        exp->exp_handle.h_cookie);
1556                 RETURN(-EINVAL);
1557         }
1558
1559         lmv = &obd->u.lmv;
1560         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1561                 __u32 *mdsize = val;
1562                 *vallen = sizeof(__u32);
1563                 *mdsize = sizeof(struct ll_fid) * lmv->desc.ld_tgt_count
1564                                 + sizeof(struct mea);
1565                 RETURN(0);
1566         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1567                 struct obd_uuid *cluuid = &lmv->cluuid;
1568                 struct lmv_tgt_desc *tgts;
1569                 __u32 *mdsnum = val;
1570                 int i;
1571
1572                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
1573                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1574                                 *vallen = sizeof(__u32);
1575                                 *mdsnum = i;
1576                                 RETURN(0);
1577                         }
1578                 }
1579                 LASSERT(0);
1580         }
1581
1582         CDEBUG(D_IOCTL, "invalid key\n");
1583         RETURN(-EINVAL);
1584 }
1585
1586 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1587                  void *key, obd_count vallen, void *val)
1588 {
1589         struct obd_device *obd;
1590         struct lmv_obd *lmv;
1591         ENTRY;
1592
1593         obd = class_exp2obd(exp);
1594         if (obd == NULL) {
1595                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1596                        exp->exp_handle.h_cookie);
1597                 RETURN(-EINVAL);
1598         }
1599         lmv = &obd->u.lmv;
1600
1601         if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
1602                 struct lmv_tgt_desc *tgts;
1603                 int i, rc;
1604
1605                 rc = lmv_check_connect(obd);
1606                 if (rc)
1607                         RETURN(rc);
1608
1609                 for (i = 0, tgts = lmv->tgts; 
1610                         i < lmv->desc.ld_tgt_count; i++, tgts++) {
1611                         rc = obd_set_info(tgts->ltd_exp, keylen, key, vallen, val);
1612                         if (rc)
1613                                 RETURN(rc);
1614                 }
1615                 RETURN(0);
1616         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1617                 lmv->server_timeout = 1;
1618                 lmv_set_timeouts(obd);
1619                 RETURN(0);
1620         }
1621         
1622         RETURN(-EINVAL);
1623 }
1624
1625 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
1626                struct lov_stripe_md *lsm)
1627 {
1628         struct obd_device *obd = class_exp2obd(exp);
1629         struct lmv_obd *lmv = &obd->u.lmv;
1630         int mea_size;
1631         ENTRY;
1632
1633         mea_size = sizeof(struct ll_fid) * 
1634                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1635         if (!lmmp)
1636                 RETURN(mea_size);
1637
1638         if (*lmmp && !lsm) {
1639                 OBD_FREE(*lmmp, mea_size);
1640                 *lmmp = NULL;
1641                 RETURN(0);
1642         }
1643
1644         if (*lmmp == NULL) {
1645                 OBD_ALLOC(*lmmp, mea_size);
1646                 if (*lmmp == NULL)
1647                         RETURN(-ENOMEM);
1648         }
1649
1650         if (!lsm)
1651                 RETURN(mea_size);
1652
1653 #warning "MEA packing/convertation must be here! -bzzz"
1654         memcpy(*lmmp, lsm, mea_size);
1655         RETURN(mea_size);
1656 }
1657
1658 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **mem_tgt,
1659                  struct lov_mds_md *disk_src, int mdsize)
1660 {
1661         struct obd_device *obd = class_exp2obd(exp);
1662         struct lmv_obd *lmv = &obd->u.lmv;
1663         struct mea **tmea = (struct mea **) mem_tgt;
1664         struct mea *mea = (void *) disk_src;
1665         int mea_size;
1666         ENTRY;
1667
1668         mea_size = sizeof(struct ll_fid) * 
1669                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1670         if (mem_tgt == NULL)
1671                 return mea_size;
1672
1673         if (*mem_tgt != NULL && disk_src == NULL) {
1674                 OBD_FREE(*tmea, mea_size);
1675                 RETURN(0);
1676         }
1677
1678         LASSERT(mea_size == mdsize);
1679
1680         OBD_ALLOC(*tmea, mea_size);
1681         if (*tmea == NULL)
1682                 RETURN(-ENOMEM);
1683
1684         if (!disk_src)
1685                 RETURN(mea_size);
1686
1687 #warning "MEA unpacking/convertation must be here! -bzzz"
1688         memcpy(*tmea, mea, mdsize);
1689         RETURN(mea_size);
1690 }
1691
1692 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
1693             struct lov_stripe_md *ea, obd_count oa_bufs,
1694             struct brw_page *pgarr, struct obd_trans_info *oti)
1695 {
1696         struct obd_device *obd = exp->exp_obd;
1697         struct lmv_obd *lmv = &obd->u.lmv;
1698         struct mea *mea = (struct mea *) ea;
1699         int err;
1700       
1701         LASSERT(oa != NULL);
1702         LASSERT(ea != NULL);
1703         LASSERT(pgarr != NULL);
1704         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1705
1706         oa->o_gr = mea->mea_fids[oa->o_mds].generation;
1707         oa->o_id = mea->mea_fids[oa->o_mds].id;
1708         oa->o_valid =  OBD_MD_FLID | OBD_MD_FLGROUP;
1709         err = obd_brw(rw, lmv->tgts[oa->o_mds].ltd_exp, oa,
1710                       NULL, oa_bufs, pgarr, oti);
1711         RETURN(err);
1712 }
1713
1714 struct obd_ops lmv_obd_ops = {
1715         .o_owner                = THIS_MODULE,
1716         .o_attach               = lmv_attach,
1717         .o_detach               = lmv_detach,
1718         .o_setup                = lmv_setup,
1719         .o_cleanup              = lmv_cleanup,
1720         .o_connect              = lmv_connect,
1721         .o_disconnect           = lmv_disconnect,
1722         .o_statfs               = lmv_statfs,
1723         .o_get_info             = lmv_get_info,
1724         .o_set_info             = lmv_set_info,
1725         .o_create               = lmv_obd_create,
1726         .o_packmd               = lmv_packmd,
1727         .o_unpackmd             = lmv_unpackmd,
1728         .o_brw                  = lmv_brw,
1729         .o_init_ea_size         = lmv_init_ea_size,
1730         .o_notify               = lmv_notify,
1731         .o_iocontrol            = lmv_iocontrol,
1732 };
1733
1734 struct md_ops lmv_md_ops = {
1735         .m_getstatus            = lmv_getstatus,
1736         .m_getattr              = lmv_getattr,
1737         .m_change_cbdata        = lmv_change_cbdata,
1738         .m_change_cbdata_name   = lmv_change_cbdata_name,
1739         .m_close                = lmv_close,
1740         .m_create               = lmv_create,
1741         .m_done_writing         = lmv_done_writing,
1742         .m_enqueue              = lmv_enqueue,
1743         .m_getattr_name         = lmv_getattr_name,
1744         .m_intent_lock          = lmv_intent_lock,
1745         .m_link                 = lmv_link,
1746         .m_rename               = lmv_rename,
1747         .m_setattr              = lmv_setattr,
1748         .m_sync                 = lmv_sync,
1749         .m_readpage             = lmv_readpage,
1750         .m_unlink               = lmv_unlink,
1751         .m_get_real_obd         = lmv_get_real_obd,
1752         .m_valid_attrs          = lmv_valid_attrs,
1753         .m_delete_object        = lmv_delete_object,
1754 };
1755
1756 int __init lmv_init(void)
1757 {
1758         struct lprocfs_static_vars lvars;
1759         int rc;
1760
1761         lprocfs_init_vars(lmv, &lvars);
1762         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
1763                                  lvars.module_vars, OBD_LMV_DEVICENAME);
1764         RETURN(rc);
1765 }
1766
1767 #ifdef __KERNEL__
1768 static void lmv_exit(void)
1769 {
1770         class_unregister_type(OBD_LMV_DEVICENAME);
1771 }
1772
1773 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1774 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
1775 MODULE_LICENSE("GPL");
1776
1777 module_init(lmv_init);
1778 module_exit(lmv_exit);
1779 #endif