Whamcloud - gitweb
- added error handling in various places of lmv code.
[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                 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp,
399                                     len, karg, uarg);
400                 if (err) {
401                         if (lmv->tgts[i].active) {
402                                 CERROR("error: iocontrol MDC %s on MDT"
403                                        "idx %d: err = %d\n",
404                                        lmv->tgts[i].uuid.uuid, i, err);
405                                 if (!rc)
406                                         rc = err;
407                         }
408                 } else
409                         set = 1;
410         }
411         if (!set && !rc)
412                 rc = -EIO;
413
414         RETURN(rc);
415 }
416
417 static int lmv_setup(struct obd_device *obd, obd_count len, void *buf)
418 {
419         struct lustre_cfg *lcfg = buf;
420         struct lmv_desc *desc;
421         struct lmv_obd *lmv = &obd->u.lmv;
422         struct obd_uuid *uuids;
423         struct lmv_tgt_desc *tgts;
424         int i;
425         int rc = 0;
426         ENTRY;
427
428         if (lcfg->lcfg_inllen1 < 1) {
429                 CERROR("LMV setup requires a descriptor\n");
430                 RETURN(-EINVAL);
431         }
432
433         if (lcfg->lcfg_inllen2 < 1) {
434                 CERROR("LMV setup requires an OST UUID list\n");
435                 RETURN(-EINVAL);
436         }
437
438         desc = (struct lmv_desc *)lcfg->lcfg_inlbuf1;
439         if (sizeof(*desc) > lcfg->lcfg_inllen1) {
440                 CERROR("descriptor size wrong: %d > %d\n",
441                        (int)sizeof(*desc), lcfg->lcfg_inllen1);
442                 RETURN(-EINVAL);
443         }
444
445         uuids = (struct obd_uuid *)lcfg->lcfg_inlbuf2;
446         if (sizeof(*uuids) * desc->ld_tgt_count != lcfg->lcfg_inllen2) {
447                 CERROR("UUID array size wrong: %u * %u != %u\n",
448                        sizeof(*uuids), desc->ld_tgt_count, lcfg->lcfg_inllen2);
449                 RETURN(-EINVAL);
450         }
451
452         lmv->bufsize = sizeof(struct lmv_tgt_desc) * desc->ld_tgt_count;
453         OBD_ALLOC(lmv->tgts, lmv->bufsize);
454         if (lmv->tgts == NULL) {
455                 CERROR("Out of memory\n");
456                 RETURN(-EINVAL);
457         }
458
459         lmv->desc = *desc;
460         spin_lock_init(&lmv->lmv_lock);
461         
462         for (i = 0, tgts = lmv->tgts; i < desc->ld_tgt_count; i++, tgts++)
463                 tgts->uuid = uuids[i];
464         
465         lmv->max_easize = sizeof(struct ll_fid) *
466                 desc->ld_tgt_count + sizeof(struct mea);
467         
468         lmv->max_cookiesize = 0;
469
470         RETURN(rc);
471 }
472
473 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
474                       unsigned long max_age)
475 {
476         struct lmv_obd *lmv = &obd->u.lmv;
477         struct obd_statfs temp;
478         int rc = 0, i;
479         ENTRY;
480         
481         rc = lmv_check_connect(obd);
482         if (rc)
483                 RETURN(rc);
484                 
485         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
486                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, &temp, max_age);
487                 if (rc) {
488                         CERROR("can't stat MDS #%d (%s)\n", i,
489                                lmv->tgts[i].ltd_exp->exp_obd->obd_name);
490                         RETURN(rc);
491                 }
492                 if (i == 0) {
493                         memcpy(osfs, &temp, sizeof(temp));
494                 } else {
495                         osfs->os_bavail += temp.os_bavail;
496                         osfs->os_blocks += temp.os_blocks;
497                         osfs->os_ffree += temp.os_ffree;
498                         osfs->os_files += temp.os_files;
499                 }
500         }
501         RETURN(rc);
502 }
503
504 static int lmv_cleanup(struct obd_device *obd, int flags) 
505 {
506         struct lmv_obd *lmv = &obd->u.lmv;
507         ENTRY;
508         lmv_cleanup_objs(obd);
509         OBD_FREE(lmv->tgts, lmv->bufsize);
510         RETURN(0);
511 }
512
513 static int lmv_getstatus(struct obd_export *exp, struct ll_fid *fid)
514 {
515         struct obd_device *obd = exp->exp_obd;
516         struct lmv_obd *lmv = &obd->u.lmv;
517         int rc;
518         ENTRY;
519         rc = lmv_check_connect(obd);
520         if (rc)
521                 RETURN(rc);
522         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid);
523         fid->mds = 0;
524         RETURN(rc);
525 }
526
527 static int lmv_getattr(struct obd_export *exp, struct ll_fid *fid,
528                 unsigned long valid, unsigned int ea_size,
529                 struct ptlrpc_request **request)
530 {
531         struct obd_device *obd = exp->exp_obd;
532         struct lmv_obd *lmv = &obd->u.lmv;
533         int rc, i = fid->mds;
534         struct lmv_obj *obj;
535         ENTRY;
536         rc = lmv_check_connect(obd);
537         if (rc)
538                 RETURN(rc);
539         obj = lmv_grab_obj(obd, fid, 0);
540         CDEBUG(D_OTHER, "GETATTR for %lu/%lu/%lu %s\n",
541                (unsigned long) fid->mds,
542                (unsigned long) fid->id,
543                (unsigned long) fid->generation,
544                obj ? "(splitted)" : "");
545
546         LASSERT(fid->mds < lmv->desc.ld_tgt_count);
547         rc = md_getattr(lmv->tgts[i].ltd_exp, fid,
548                              valid, ea_size, request);
549         if (rc == 0 && obj) {
550                 /* we have to loop over dirobjs here and gather attrs
551                  * for all the slaves */
552 #warning "attrs gathering here"
553         }
554         lmv_put_obj(obj);
555         RETURN(rc);
556 }
557
558 static int lmv_change_cbdata(struct obd_export *exp,
559                                  struct ll_fid *fid, 
560                                  ldlm_iterator_t it, void *data)
561 {
562         struct obd_device *obd = exp->exp_obd;
563         struct lmv_obd *lmv = &obd->u.lmv;
564         int rc = 0;
565         ENTRY;
566         
567         rc = lmv_check_connect(obd);
568         if (rc)
569                 RETURN(rc);
570         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu\n",
571                (unsigned long) fid->mds,
572                (unsigned long) fid->id,
573                (unsigned long) fid->generation);
574         LASSERT(fid->mds < lmv->desc.ld_tgt_count);
575         rc = md_change_cbdata(lmv->tgts[fid->mds].ltd_exp, fid, it, data);
576         RETURN(rc);
577 }
578
579 static int lmv_change_cbdata_name(struct obd_export *exp, struct ll_fid *pfid,
580                                   char *name, int len, struct ll_fid *cfid,
581                                   ldlm_iterator_t it, void *data)
582 {
583         struct obd_device *obd = exp->exp_obd;
584         struct lmv_obd *lmv = &obd->u.lmv;
585         struct lmv_obj *obj;
586         int rc = 0, mds;
587         ENTRY;
588         rc = lmv_check_connect(obd);
589         if (rc)
590                 RETURN(rc);
591         LASSERT(pfid->mds < lmv->desc.ld_tgt_count);
592         LASSERT(cfid->mds < lmv->desc.ld_tgt_count);
593         CDEBUG(D_OTHER, "CBDATA for %lu/%lu/%lu:%*s -> %lu/%lu/%lu\n",
594                (unsigned long) pfid->mds, (unsigned long) pfid->id,
595                (unsigned long) pfid->generation, len, name,
596                (unsigned long) cfid->mds, (unsigned long) cfid->id,
597                (unsigned long) cfid->generation);
598
599         /* this is default mds for directory name belongs to */
600         mds = pfid->mds;
601         obj = lmv_grab_obj(obd, pfid, 0);
602         if (obj) {
603                 /* directory is splitted. look for right mds for this name */
604                 mds = raw_name2idx(obj->objcount, name, len);
605                 lmv_put_obj(obj);
606         }
607         rc = md_change_cbdata(lmv->tgts[mds].ltd_exp, cfid, it, data);
608         RETURN(rc);
609 }
610
611 static int lmv_valid_attrs(struct obd_export *exp, struct ll_fid *fid) 
612 {
613         struct obd_device *obd = exp->exp_obd;
614         struct lmv_obd *lmv = &obd->u.lmv;
615         int rc = 0;
616         ENTRY;
617         rc = lmv_check_connect(obd);
618         if (rc)
619                 RETURN(rc);
620         CDEBUG(D_OTHER, "validate %lu/%lu/%lu\n", (unsigned long) fid->mds,
621                (unsigned long) fid->id, (unsigned long) fid->generation);
622         LASSERT(fid->mds < lmv->desc.ld_tgt_count);
623         rc = md_valid_attrs(lmv->tgts[fid->mds].ltd_exp, fid);
624         RETURN(rc);
625 }
626
627 int lmv_close(struct obd_export *exp, struct obdo *obdo,
628                   struct obd_client_handle *och,
629                   struct ptlrpc_request **request)
630 {
631         struct obd_device *obd = exp->exp_obd;
632         struct lmv_obd *lmv = &obd->u.lmv;
633         int rc, i = obdo->o_mds;
634         ENTRY;
635         rc = lmv_check_connect(obd);
636         if (rc)
637                 RETURN(rc);
638         LASSERT(i < lmv->desc.ld_tgt_count);
639         CDEBUG(D_OTHER, "CLOSE %lu/%lu/%lu\n", (unsigned long) obdo->o_mds,
640                (unsigned long) obdo->o_id, (unsigned long) obdo->o_generation);
641         rc = md_close(lmv->tgts[i].ltd_exp, obdo, och, request);
642         RETURN(rc);
643 }
644
645 int lmv_get_mea_and_update_object(struct obd_export *exp, struct ll_fid *fid)
646 {
647         struct obd_device *obd = exp->exp_obd;
648         struct lmv_obd *lmv = &obd->u.lmv;
649         struct ptlrpc_request *req = NULL;
650         struct lustre_md md;
651         unsigned long valid;
652         int mealen, rc;
653
654         md.mea = NULL;
655         mealen = MEA_SIZE_LMV(lmv);
656         
657         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA;
658
659         /* time to update mea of parent fid */
660         rc = md_getattr(lmv->tgts[fid->mds].ltd_exp, fid,
661                         valid, mealen, &req);
662         if (rc) {
663                 CERROR("md_getattr() failed, error %d\n", rc);
664                 GOTO(cleanup, rc);
665         }
666
667         rc = mdc_req2lustre_md(exp, req, 0, NULL, &md);
668         if (rc) {
669                 CERROR("mdc_req2lustre_md() failed, error %d\n", rc);
670                 GOTO(cleanup, rc);
671         }
672
673         if (md.mea == NULL)
674                 GOTO(cleanup, rc = -ENODATA);
675
676         rc = lmv_create_obj_from_attrs(exp, fid, md.mea);
677         obd_free_memmd(exp, (struct lov_stripe_md **) &md.mea);
678
679 cleanup:
680         if (req)
681                 ptlrpc_req_finished(req);
682         RETURN(rc);
683 }
684
685 int lmv_create(struct obd_export *exp, struct mdc_op_data *op_data,
686                const void *data, int datalen, int mode, __u32 uid,
687                __u32 gid, __u64 rdev, struct ptlrpc_request **request)
688 {
689         struct obd_device *obd = exp->exp_obd;
690         struct lmv_obd *lmv = &obd->u.lmv;
691         struct mds_body *mds_body;
692         struct lmv_obj *obj;
693         int rc, mds;
694         ENTRY;
695
696         rc = lmv_check_connect(obd);
697         if (rc)
698                 RETURN(rc);
699
700         if (!lmv->desc.ld_active_tgt_count)
701                 RETURN(-EIO);
702 repeat:
703         obj = lmv_grab_obj(obd, &op_data->fid1, 0);
704         if (obj) {
705                 mds = raw_name2idx(obj->objcount, op_data->name,
706                                    op_data->namelen);
707                 op_data->fid1 = obj->objs[mds].fid;
708                 lmv_put_obj(obj);
709         }
710
711         CDEBUG(D_OTHER, "CREATE '%*s' on %lu/%lu/%lu\n",
712                         op_data->namelen, op_data->name,
713                         (unsigned long) op_data->fid1.mds,
714                         (unsigned long) op_data->fid1.id,
715                         (unsigned long) op_data->fid1.generation);
716         rc = md_create(lmv->tgts[op_data->fid1.mds].ltd_exp, op_data, data,
717                        datalen, mode, uid, gid, rdev, request);
718         if (rc == 0) {
719                 if (*request == NULL)
720                      RETURN(rc);
721                 mds_body = lustre_msg_buf((*request)->rq_repmsg, 0,
722                                           sizeof(*mds_body));
723                 LASSERT(mds_body != NULL);
724                 CDEBUG(D_OTHER, "created. id = %lu, generation = %lu, mds = %d\n",
725                        (unsigned long) mds_body->fid1.id,
726                        (unsigned long) mds_body->fid1.generation,
727                        op_data->fid1.mds);
728                 LASSERT(mds_body->valid & OBD_MD_MDS ||
729                         mds_body->mds == op_data->fid1.mds);
730         } else if (rc == -ERESTART) {
731                 /* directory got splitted. time to update local object
732                  * and repeat the request with proper MDS */
733                 rc = lmv_get_mea_and_update_object(exp, &op_data->fid1);
734                 if (rc == 0) {
735                         ptlrpc_req_finished(*request);
736                         goto repeat;
737                 }
738         }
739         RETURN(rc);
740 }
741
742 int lmv_done_writing(struct obd_export *exp, struct obdo *obdo)
743 {
744         struct obd_device *obd = exp->exp_obd;
745         struct lmv_obd *lmv = &obd->u.lmv;
746         int rc;
747         ENTRY;
748         rc = lmv_check_connect(obd);
749         if (rc)
750                 RETURN(rc);
751
752         /* FIXME: choose right MDC here */
753         CWARN("this method isn't implemented yet\n");
754         rc = md_done_writing(lmv->tgts[0].ltd_exp, obdo);
755         RETURN(rc);
756 }
757
758 int lmv_enqueue_slaves(struct obd_export *exp, int locktype,
759                          struct lookup_intent *it, int lockmode,
760                          struct mdc_op_data *data, struct lustre_handle *lockh,
761                          void *lmm, int lmmsize,
762                          ldlm_completion_callback cb_completion,
763                          ldlm_blocking_callback cb_blocking, void *cb_data)
764 {
765         struct obd_device *obd = exp->exp_obd;
766         struct lmv_obd *lmv = &obd->u.lmv;
767         struct mea *mea = data->mea1;
768         struct mdc_op_data data2;
769         int i, rc, mds;
770         ENTRY;
771
772         LASSERT(mea != NULL);
773         for (i = 0; i < mea->mea_count; i++) {
774                 if (lmv->tgts[i].ltd_exp == NULL)
775                         continue;
776
777                 memset(&data2, 0, sizeof(data2));
778                 data2.fid1 = mea->mea_fids[i];
779                 mds = data2.fid1.mds;
780                 rc = md_enqueue(lmv->tgts[mds].ltd_exp, locktype, it, lockmode,
781                                 &data2, lockh + i, lmm, lmmsize, cb_completion,
782                                 cb_blocking, cb_data);
783                 CDEBUG(D_OTHER, "take lock on slave %lu/%lu/%lu -> %d/%d\n",
784                        (unsigned long) mea->mea_fids[i].mds,
785                        (unsigned long) mea->mea_fids[i].id,
786                        (unsigned long) mea->mea_fids[i].generation,
787                        rc, it->d.lustre.it_status);
788                 if (rc)
789                         GOTO(cleanup, rc);
790                 if (it->d.lustre.it_data) {
791                         struct ptlrpc_request *req;
792                         req = (struct ptlrpc_request *) it->d.lustre.it_data;
793                         ptlrpc_req_finished(req);
794                 }
795                 
796                 if (it->d.lustre.it_status)
797                         GOTO(cleanup, rc = it->d.lustre.it_status);
798         }
799         RETURN(0);
800         
801 cleanup:
802         /* drop all taken locks */
803         while (--i >= 0) {
804                 if (lockh[i].cookie)
805                         ldlm_lock_decref(lockh + i, lockmode);
806                 lockh[i].cookie = 0;
807         }
808         RETURN(rc);
809 }
810
811 int lmv_enqueue(struct obd_export *exp, int lock_type,
812                 struct lookup_intent *it, int lock_mode,
813                 struct mdc_op_data *data, struct lustre_handle *lockh,
814                 void *lmm, int lmmsize,
815                 ldlm_completion_callback cb_completion,
816                 ldlm_blocking_callback cb_blocking, void *cb_data)
817 {
818         struct obd_device *obd = exp->exp_obd;
819         struct lmv_obd *lmv = &obd->u.lmv;
820         struct lmv_obj *obj;
821         int rc, mds;
822         ENTRY;
823
824         rc = lmv_check_connect(obd);
825         if (rc)
826                 RETURN(rc);
827
828         if (it->it_op == IT_UNLINK) {
829                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
830                                         data, lockh, lmm, lmmsize,
831                                         cb_completion, cb_blocking, cb_data);
832                 RETURN(rc);
833         }
834
835         if (data->namelen) {
836                 obj = lmv_grab_obj(obd, &data->fid1, 0);
837                 if (obj) {
838                         /* directory is splitted. look for
839                          * right mds for this name */
840                         mds = raw_name2idx(obj->objcount, (char *)data->name,
841                                            data->namelen);
842                         data->fid1 = obj->objs[mds].fid;
843                         lmv_put_obj(obj);
844                 }
845         }
846         CDEBUG(D_OTHER, "ENQUEUE '%s' on %lu/%lu\n",
847                LL_IT2STR(it), (unsigned long) data->fid1.id,
848                (unsigned long) data->fid1.generation);
849         rc = md_enqueue(lmv->tgts[data->fid1.mds].ltd_exp, lock_type, it,
850                         lock_mode, data, lockh, lmm, lmmsize, cb_completion,
851                         cb_blocking, cb_data);
852
853         RETURN(rc);
854 }
855
856 int lmv_getattr_name(struct obd_export *exp, struct ll_fid *fid,
857                          char *filename, int namelen, unsigned long valid,
858                          unsigned int ea_size, struct ptlrpc_request **request)
859 {
860         struct obd_device *obd = exp->exp_obd;
861         struct lmv_obd *lmv = &obd->u.lmv;
862         struct ll_fid rfid = *fid;
863         int rc, mds = fid->mds;
864         struct mds_body *body;
865         struct lmv_obj *obj;
866         ENTRY;
867         rc = lmv_check_connect(obd);
868         if (rc)
869                 RETURN(rc);
870 repeat:
871         obj = lmv_grab_obj(obd, fid, 0);
872         if (obj) {
873                 /* directory is splitted. look for right mds for this name */
874                 mds = raw_name2idx(obj->objcount, filename, namelen - 1);
875                 rfid = obj->objs[mds].fid;
876                 lmv_put_obj(obj);
877         }
878         CDEBUG(D_OTHER, "getattr_name for %*s on %lu/%lu/%lu -> %lu/%lu/%lu\n",
879                namelen, filename, (unsigned long) fid->mds,
880                (unsigned long) fid->id, (unsigned long) fid->generation,
881                (unsigned long) rfid.mds, (unsigned long) rfid.id,
882                (unsigned long) rfid.generation);
883         rc = md_getattr_name(lmv->tgts[mds].ltd_exp, &rfid, filename, namelen,
884                                   valid, ea_size, request);
885         if (rc == 0) {
886                 /* this could be cross-node reference. in this case all
887                  * we have right now is mds/ino/generation triple. we'd
888                  * like to find other attributes */
889                 body = lustre_msg_buf((*request)->rq_repmsg, 0, sizeof(*body));
890                 LASSERT(body != NULL);
891                 if (body->valid & OBD_MD_MDS) {
892                         struct ptlrpc_request *req = NULL;
893                         rfid = body->fid1;
894                         CDEBUG(D_OTHER, "request attrs for %lu/%lu/%lu\n",
895                                (unsigned long) rfid.mds,
896                                (unsigned long) rfid.id,
897                                (unsigned long) rfid.generation);
898                         rc = md_getattr_name(lmv->tgts[rfid.mds].ltd_exp, &rfid,
899                                              NULL, 1, valid, ea_size, &req);
900                         ptlrpc_req_finished(*request);
901                         *request = req;
902                 }
903         } else if (rc == -ERESTART) {
904                 /* directory got splitted. time to update local object
905                  * and repeat the request with proper MDS */
906                 rc = lmv_get_mea_and_update_object(exp, &rfid);
907                 if (rc == 0) {
908                         ptlrpc_req_finished(*request);
909                         goto repeat;
910                 }
911         }
912         RETURN(rc);
913 }
914
915
916 /*
917  * llite passes fid of an target inode in data->fid1 and
918  * fid of directory in data->fid2
919  */
920 int lmv_link(struct obd_export *exp, struct mdc_op_data *data,
921              struct ptlrpc_request **request)
922 {
923         struct obd_device *obd = exp->exp_obd;
924         struct lmv_obd *lmv = &obd->u.lmv;
925         struct lmv_obj *obj;
926         int rc;
927         ENTRY;
928         rc = lmv_check_connect(obd);
929         if (rc)
930                 RETURN(rc);
931         if (data->namelen != 0) {
932                 /* usual link request */
933                 obj = lmv_grab_obj(obd, &data->fid1, 0);
934                 if (obj) {
935                         rc = raw_name2idx(obj->objcount, data->name,
936                                           data->namelen);
937                         data->fid1 = obj->objs[rc].fid;
938                         lmv_put_obj(obj);
939                 }
940                 CDEBUG(D_OTHER,"link %u/%u/%u:%*s to %u/%u/%u mds %d\n",
941                        (unsigned) data->fid2.mds, (unsigned) data->fid2.id,
942                        (unsigned) data->fid2.generation, data->namelen,
943                        data->name, (unsigned) data->fid1.mds,
944                        (unsigned) data->fid1.id,
945                        (unsigned) data->fid1.generation, data->fid1.mds);
946         } else {
947                 /* request from MDS to acquire i_links for inode by fid1 */
948                 CDEBUG(D_OTHER, "inc i_nlinks for %u/%u/%u\n",
949                        (unsigned) data->fid1.mds, (unsigned) data->fid1.id,
950                        (unsigned) data->fid1.generation);
951         }
952                         
953         rc = md_link(lmv->tgts[data->fid1.mds].ltd_exp, data, request);
954         RETURN(rc);
955 }
956
957 int lmv_rename(struct obd_export *exp, struct mdc_op_data *data,
958                const char *old, int oldlen, const char *new, int newlen,
959                struct ptlrpc_request **request)
960 {
961         struct obd_device *obd = exp->exp_obd;
962         struct lmv_obd *lmv = &obd->u.lmv;
963         struct lmv_obj *obj;
964         int rc, mds;
965         ENTRY;
966
967         CDEBUG(D_OTHER, "rename %*s in %lu/%lu/%lu to %*s in %lu/%lu/%lu\n",
968                oldlen, old, (unsigned long) data->fid1.mds,
969                (unsigned long) data->fid1.id,
970                (unsigned long) data->fid1.generation,
971                newlen, new, (unsigned long) data->fid2.mds,
972                (unsigned long) data->fid2.id,
973                (unsigned long) data->fid2.generation);
974         if (!fid_equal(&data->fid1, &data->fid2))
975                 CWARN("cross-node rename %lu/%lu/%lu:%*s to %lu/%lu/%lu:%*s\n",
976                       (unsigned long) data->fid1.mds,
977                       (unsigned long) data->fid1.id,
978                       (unsigned long) data->fid1.generation, oldlen, old,
979                       (unsigned long) data->fid2.mds,
980                       (unsigned long) data->fid2.id,
981                       (unsigned long) data->fid2.generation, newlen, new);
982
983         rc = lmv_check_connect(obd);
984         if (rc)
985                 RETURN(rc);
986
987         if (oldlen == 0) {
988                 /* MDS with old dir entry is asking another MDS
989                  * to create name there */
990                 CDEBUG(D_OTHER,
991                        "create %*s(%d/%d) in %lu/%lu/%lu pointing to %lu/%lu/%lu\n",
992                        newlen, new, oldlen, newlen,
993                        (unsigned long) data->fid2.mds,
994                        (unsigned long) data->fid2.id,
995                        (unsigned long) data->fid2.generation,
996                        (unsigned long) data->fid1.mds,
997                        (unsigned long) data->fid1.id,
998                        (unsigned long) data->fid1.generation);
999                 mds = data->fid2.mds;
1000                 goto request;
1001         }
1002
1003         obj = lmv_grab_obj(obd, &data->fid1, 0);
1004         if (obj) {
1005                 /* directory is already splitted, so we have to forward
1006                  * request to the right MDS */
1007                 mds = raw_name2idx(obj->objcount, (char *)old, oldlen);
1008                 data->fid1 = obj->objs[mds].fid;
1009                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
1010                        (unsigned long) obj->objs[mds].fid.mds,
1011                        (unsigned long) obj->objs[mds].fid.id,
1012                        (unsigned long) obj->objs[mds].fid.generation);
1013         }
1014         lmv_put_obj(obj);
1015
1016         obj = lmv_grab_obj(obd, &data->fid2, 0);
1017         if (obj) {
1018                 /* directory is already splitted, so we have to forward
1019                  * request to the right MDS */
1020                 mds = raw_name2idx(obj->objcount, (char *)new, newlen);
1021                 data->fid2 = obj->objs[mds].fid;
1022                 CDEBUG(D_OTHER, "forward to MDS #%u (%lu/%lu/%lu)\n", mds,
1023                        (unsigned long) obj->objs[mds].fid.mds,
1024                        (unsigned long) obj->objs[mds].fid.id,
1025                        (unsigned long) obj->objs[mds].fid.generation);
1026         }
1027         lmv_put_obj(obj);
1028         
1029         mds = data->fid1.mds;
1030
1031 request:
1032         rc = md_rename(lmv->tgts[mds].ltd_exp, data, old, oldlen,
1033                        new, newlen, request); 
1034         RETURN(rc);
1035 }
1036
1037 int lmv_setattr(struct obd_export *exp, struct mdc_op_data *data,
1038                 struct iattr *iattr, void *ea, int ealen, void *ea2, int ea2len,
1039                 struct ptlrpc_request **request)
1040 {
1041         struct obd_device *obd = exp->exp_obd;
1042         struct lmv_obd *lmv = &obd->u.lmv;
1043         int rc = 0, i = data->fid1.mds;
1044         struct ptlrpc_request *req;
1045         struct mds_body *mds_body;
1046         struct lmv_obj *obj;
1047         ENTRY;
1048
1049         rc = lmv_check_connect(obd);
1050         if (rc)
1051                 RETURN(rc);
1052
1053         obj = lmv_grab_obj(obd, &data->fid1, 0);
1054         CDEBUG(D_OTHER, "SETATTR for %lu/%lu/%lu, valid 0x%x%s\n",
1055                (unsigned long) data->fid1.mds,
1056                (unsigned long) data->fid1.id,
1057                (unsigned long) data->fid1.generation, iattr->ia_valid,
1058                obj ? ", splitted" : "");
1059         if (obj) {
1060                 for (i = 0; i < obj->objcount; i++) {
1061                         data->fid1 = obj->objs[i].fid;
1062                         rc = md_setattr(lmv->tgts[i].ltd_exp, data, iattr, ea,
1063                                         ealen, ea2, ea2len, &req);
1064                         LASSERT(rc == 0);
1065                         if (fid_equal(&obj->fid, &obj->objs[i].fid)) {
1066                                 /* this is master object and this request
1067                                  * should be returned back to llite */
1068                                 *request = req;
1069                         } else {
1070                                 ptlrpc_req_finished(req);
1071                         }
1072                 }
1073                 lmv_put_obj(obj);
1074         } else {
1075                 LASSERT(data->fid1.mds < lmv->desc.ld_tgt_count);
1076                 rc = md_setattr(lmv->tgts[i].ltd_exp, data, iattr, ea, ealen,
1077                                 ea2, ea2len, request); 
1078                 if (rc == 0) {
1079                         mds_body = lustre_msg_buf((*request)->rq_repmsg, 0,
1080                                         sizeof(*mds_body));
1081                         LASSERT(mds_body != NULL);
1082                         LASSERT(mds_body->mds == i);
1083                 }
1084         }
1085         RETURN(rc);
1086 }
1087
1088 int lmv_sync(struct obd_export *exp, struct ll_fid *fid,
1089              struct ptlrpc_request **request)
1090 {
1091         struct obd_device *obd = exp->exp_obd;
1092         struct lmv_obd *lmv = &obd->u.lmv;
1093         int rc;
1094         ENTRY;
1095
1096         rc = lmv_check_connect(obd);
1097         if (rc)
1098                 RETURN(rc);
1099
1100         rc = md_sync(lmv->tgts[0].ltd_exp, fid, request); 
1101         RETURN(rc);
1102 }
1103
1104 int lmv_dirobj_blocking_ast(struct ldlm_lock *lock,
1105                             struct ldlm_lock_desc *desc, void *data, int flag)
1106 {
1107         struct lustre_handle lockh;
1108         struct lmv_obj *obj;
1109         int rc;
1110         ENTRY;
1111
1112         switch (flag) {
1113         case LDLM_CB_BLOCKING:
1114                 ldlm_lock2handle(lock, &lockh);
1115                 rc = ldlm_cli_cancel(&lockh);
1116                 if (rc < 0) {
1117                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
1118                         RETURN(rc);
1119                 }
1120                 break;
1121         case LDLM_CB_CANCELING:
1122                 /* time to drop cached attrs for dirobj */
1123                 obj = lock->l_ast_data;
1124                 if (!obj)
1125                         break;
1126
1127                 CDEBUG(D_OTHER, "cancel %s on %lu/%lu, master %lu/%lu/%lu\n",
1128                        lock->l_resource->lr_name.name[3] == 1 ?
1129                                 "LOOKUP" : "UPDATE",
1130                        (unsigned long) lock->l_resource->lr_name.name[0],
1131                        (unsigned long) lock->l_resource->lr_name.name[1],
1132                        (unsigned long) obj->fid.mds,
1133                        (unsigned long) obj->fid.id,
1134                        (unsigned long) obj->fid.generation);
1135                 break;
1136         default:
1137                 LBUG();
1138         }
1139         RETURN(0);
1140 }
1141
1142 void lmv_remove_dots(struct page *page)
1143 {
1144         char *kaddr = page_address(page);
1145         unsigned limit = PAGE_CACHE_SIZE;
1146         unsigned offs, rec_len;
1147         struct ext2_dir_entry_2 *p;
1148
1149         for (offs = 0; offs <= limit - EXT2_DIR_REC_LEN(1); offs += rec_len) {
1150                 p = (struct ext2_dir_entry_2 *)(kaddr + offs);
1151                 rec_len = le16_to_cpu(p->rec_len);
1152
1153                 if ((p->name_len == 1 && p->name[0] == '.') ||
1154                     (p->name_len == 2 && p->name[0] == '.' && p->name[1] == '.'))
1155                         p->inode = 0;
1156         }
1157 }
1158
1159 int lmv_readpage(struct obd_export *exp, struct ll_fid *mdc_fid,
1160                  __u64 offset, struct page *page,
1161                  struct ptlrpc_request **request)
1162 {
1163         struct obd_device *obd = exp->exp_obd;
1164         struct lmv_obd *lmv = &obd->u.lmv;
1165         struct ll_fid rfid = *mdc_fid;
1166         struct lmv_obj *obj;
1167         int rc, i;
1168         ENTRY;
1169
1170         rc = lmv_check_connect(obd);
1171         if (rc)
1172                 RETURN(rc);
1173
1174         LASSERT(mdc_fid->mds < lmv->desc.ld_tgt_count);
1175         CDEBUG(D_OTHER, "READPAGE at %llu from %lu/%lu/%lu\n",
1176                offset, (unsigned long) rfid.mds,
1177                (unsigned long) rfid.id,
1178                (unsigned long) rfid.generation);
1179
1180         obj = lmv_grab_obj(obd, mdc_fid, 0);
1181         if (obj) {
1182                 /* find dirobj containing page with requested offset */
1183                 /* FIXME: what about protecting cached attrs here? */
1184                 for (i = 0; i < obj->objcount; i++) {
1185                         if (offset < obj->objs[i].size)
1186                                 break;
1187                         offset -= obj->objs[i].size;
1188                 }
1189                 rfid = obj->objs[i].fid;
1190                 CDEBUG(D_OTHER, "forward to %lu/%lu/%lu with offset %lu\n",
1191                        (unsigned long) rfid.mds,
1192                        (unsigned long) rfid.id,
1193                        (unsigned long) rfid.generation,
1194                        (unsigned long) offset);
1195         }
1196         rc = md_readpage(lmv->tgts[rfid.mds].ltd_exp, &rfid, offset, page, request);
1197         if (rc == 0 && !fid_equal(&rfid, mdc_fid)) {
1198                 /* this page isn't from master object. to avoid
1199                  * ./.. duplication in directory, we have to remove them
1200                  * from all slave objects */
1201                 lmv_remove_dots(page);
1202         }
1203       
1204         lmv_put_obj(obj);
1205
1206         RETURN(rc);
1207 }
1208
1209 int lmv_unlink_slaves(struct obd_export *exp, struct mdc_op_data *data,
1210                       struct ptlrpc_request **req)
1211 {
1212         struct obd_device *obd = exp->exp_obd;
1213         struct lmv_obd *lmv = &obd->u.lmv;
1214         struct mea *mea = data->mea1;
1215         struct mdc_op_data data2;
1216         int i, rc = 0, mds;
1217         ENTRY;
1218
1219         LASSERT(mea != NULL);
1220         for (i = 0; i < mea->mea_count; i++) {
1221                 if (lmv->tgts[i].ltd_exp == NULL)
1222                         continue;
1223
1224                 memset(&data2, 0, sizeof(data2));
1225                 data2.fid1 = mea->mea_fids[i];
1226                 data2.create_mode = MDS_MODE_DONT_LOCK | S_IFDIR;
1227                 mds = data2.fid1.mds;
1228                 rc = md_unlink(lmv->tgts[mds].ltd_exp, &data2, req);
1229                 CDEBUG(D_OTHER, "unlink slave %lu/%lu/%lu -> %d\n",
1230                        (unsigned long) mea->mea_fids[i].mds,
1231                        (unsigned long) mea->mea_fids[i].id,
1232                        (unsigned long) mea->mea_fids[i].generation, rc);
1233                 if (*req) {
1234                         ptlrpc_req_finished(*req);
1235                         *req = NULL;
1236                 }
1237                 if (rc)
1238                         break;
1239         }
1240         RETURN(rc);
1241 }
1242
1243 int lmv_unlink(struct obd_export *exp, struct mdc_op_data *data,
1244                struct ptlrpc_request **request)
1245 {
1246         struct obd_device *obd = exp->exp_obd;
1247         struct lmv_obd *lmv = &obd->u.lmv;
1248         int rc, i = 0;
1249         ENTRY;
1250         
1251         rc = lmv_check_connect(obd);
1252         if (rc)
1253                 RETURN(rc);
1254
1255         if (data->namelen == 0 && data->mea1 != NULL) {
1256                 /* mds asks to remove slave objects */
1257                 rc = lmv_unlink_slaves(exp, data, request);
1258                 RETURN(rc);
1259         } else if (data->namelen != 0) {
1260                 struct lmv_obj *obj;
1261                 obj = lmv_grab_obj(obd, &data->fid1, 0);
1262                 if (obj) {
1263                         i = raw_name2idx(obj->objcount, data->name,
1264                                          data->namelen);
1265                         data->fid1 = obj->objs[i].fid;
1266                         lmv_put_obj(obj);
1267                 }
1268                 CDEBUG(D_OTHER, "unlink '%*s' in %lu/%lu/%lu -> %u\n",
1269                        data->namelen, data->name,
1270                        (unsigned long) data->fid1.mds,
1271                        (unsigned long) data->fid1.id,
1272                        (unsigned long) data->fid1.generation, i);
1273         } else {
1274                 CDEBUG(D_OTHER, "drop i_nlink on %lu/%lu/%lu\n",
1275                        (unsigned long) data->fid1.mds,
1276                        (unsigned long) data->fid1.id,
1277                        (unsigned long) data->fid1.generation);
1278         }
1279         rc = md_unlink(lmv->tgts[data->fid1.mds].ltd_exp, data, request); 
1280         RETURN(rc);
1281 }
1282
1283 struct obd_device *lmv_get_real_obd(struct obd_export *exp,
1284                                     char *name, int len)
1285 {
1286         struct obd_device *obd = exp->exp_obd;
1287         struct lmv_obd *lmv = &obd->u.lmv;
1288         int rc;
1289         ENTRY;
1290
1291         rc = lmv_check_connect(obd);
1292         if (rc)
1293                 RETURN(ERR_PTR(rc));
1294         obd = lmv->tgts[0].ltd_exp->exp_obd;
1295         EXIT;
1296         return obd;
1297 }
1298
1299 int lmv_init_ea_size(struct obd_export *exp, int easize, int cookiesize)
1300 {
1301         struct obd_device *obd = exp->exp_obd;
1302         struct lmv_obd *lmv = &obd->u.lmv;
1303         int i, rc = 0, change = 0;
1304         ENTRY;
1305
1306         if (lmv->max_easize < easize) {
1307                 lmv->max_easize = easize;
1308                 change = 1;
1309         }
1310         if (lmv->max_cookiesize < cookiesize) {
1311                 lmv->max_cookiesize = cookiesize;
1312                 change = 1;
1313         }
1314         if (change == 0)
1315                 RETURN(0);
1316         
1317         if (lmv->connected == 0)
1318                 RETURN(0);
1319
1320         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1321                 rc = obd_init_ea_size(lmv->tgts[i].ltd_exp, easize, cookiesize);
1322                 if (rc) {
1323                         CERROR("obd_init_ea_size() failed on MDT target %d, "
1324                                "error %d.\n", i, rc);
1325                         break;
1326                 }
1327         }
1328         RETURN(rc);
1329 }
1330
1331 int lmv_obd_create_single(struct obd_export *exp, struct obdo *oa,
1332                           struct lov_stripe_md **ea, struct obd_trans_info *oti)
1333 {
1334         struct obd_device *obd = exp->exp_obd;
1335         struct lmv_obd *lmv = &obd->u.lmv;
1336         struct lov_stripe_md obj_md;
1337         struct lov_stripe_md *obj_mdp = &obj_md;
1338         int rc = 0;
1339         ENTRY;
1340
1341         rc = lmv_check_connect(obd);
1342         if (rc)
1343                 RETURN(rc);
1344
1345         LASSERT(ea == NULL);
1346         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1347
1348         rc = obd_create(lmv->tgts[oa->o_mds].ltd_exp, oa, &obj_mdp, oti);
1349
1350         RETURN(rc);
1351 }
1352
1353 /*
1354  * to be called from MDS only
1355  */
1356 int lmv_obd_create(struct obd_export *exp, struct obdo *oa,
1357                    struct lov_stripe_md **ea, struct obd_trans_info *oti)
1358 {
1359         struct obd_device *obd = exp->exp_obd;
1360         struct lmv_obd *lmv = &obd->u.lmv;
1361         struct mea *mea;
1362         int i, c, rc = 0;
1363         struct ll_fid mfid;
1364         ENTRY;
1365
1366         rc = lmv_check_connect(obd);
1367         if (rc)
1368                 RETURN(rc);
1369
1370         LASSERT(oa != NULL);
1371         
1372         if (ea == NULL) {
1373                 rc = lmv_obd_create_single(exp, oa, NULL, oti);
1374                 RETURN(rc);
1375         }
1376
1377         if (*ea == NULL) {
1378                 rc = obd_alloc_diskmd(exp, (struct lov_mds_md **)ea);
1379                 if (rc < 0) {
1380                         CERROR("obd_alloc_diskmd() failed, error %d\n",
1381                                rc);
1382                         RETURN(rc);
1383                 }
1384                 
1385                 if (*ea == NULL)
1386                         RETURN(-EINVAL);
1387         }
1388
1389         rc = 0;
1390         mfid.id = oa->o_id;
1391         mfid.generation = oa->o_generation;
1392         
1393         mea = (struct mea *)*ea;
1394         if (!mea->mea_count || mea->mea_count > lmv->desc.ld_tgt_count)
1395                 mea->mea_count = lmv->desc.ld_tgt_count;
1396
1397         mea->mea_master = -1;
1398         
1399         for (i = 0, c = 0; c < mea->mea_count && 
1400                 i < lmv->desc.ld_tgt_count; i++) {
1401                 struct lov_stripe_md obj_md;
1402                 struct lov_stripe_md *obj_mdp = &obj_md;
1403                
1404                 if (lmv->tgts[i].ltd_exp == NULL) {
1405                         /* this is master MDS */
1406                         mea->mea_fids[c].id = mfid.id;
1407                         mea->mea_fids[c].generation = mfid.generation;
1408                         mea->mea_fids[c].mds = i;
1409                         mea->mea_master = i;
1410                         c++;
1411                         continue;
1412                 }
1413
1414                 /* "master" MDS should always be part of stripped dir, so scan
1415                    for it. */
1416                 if (mea->mea_master == -1 && c == mea->mea_count - 1)
1417                         continue;
1418
1419                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLTYPE | OBD_MD_FLMODE
1420                         | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLID;
1421
1422                 rc = obd_create(lmv->tgts[c].ltd_exp, oa, &obj_mdp, oti);
1423                 if (rc) {
1424                         CERROR("obd_create() failed on MDT target %d, "
1425                                "error %d\n", c, rc);
1426                         RETURN(rc);
1427                 }
1428
1429                 mea->mea_fids[c].id = oa->o_id;
1430                 mea->mea_fids[c].generation = oa->o_generation;
1431                 mea->mea_fids[c].mds = i;
1432                 c++;
1433                 CDEBUG(D_OTHER, "dirobj at mds %d: "LPU64"/%u\n",
1434                        i, oa->o_id, oa->o_generation);
1435         }
1436         LASSERT(c == mea->mea_count);
1437         CDEBUG(D_OTHER, "%d dirobjects created\n", (int) mea->mea_count);
1438
1439         RETURN(rc);
1440 }
1441
1442 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
1443                         void *key, __u32 *vallen, void *val)
1444 {
1445         struct obd_device *obd;
1446         struct lmv_obd *lmv;
1447         ENTRY;
1448
1449         obd = class_exp2obd(exp);
1450         if (obd == NULL) {
1451                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1452                        exp->exp_handle.h_cookie);
1453                 RETURN(-EINVAL);
1454         }
1455
1456         lmv = &obd->u.lmv;
1457         if (keylen == 6 && memcmp(key, "mdsize", 6) == 0) {
1458                 __u32 *mdsize = val;
1459                 *vallen = sizeof(__u32);
1460                 *mdsize = sizeof(struct ll_fid) * lmv->desc.ld_tgt_count
1461                                 + sizeof(struct mea);
1462                 RETURN(0);
1463         } else if (keylen == 6 && memcmp(key, "mdsnum", 6) == 0) {
1464                 struct obd_uuid *cluuid = &lmv->cluuid;
1465                 struct lmv_tgt_desc *tgts;
1466                 __u32 *mdsnum = val;
1467                 int i;
1468
1469                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
1470                         if (obd_uuid_equals(&tgts->uuid, cluuid)) {
1471                                 *vallen = sizeof(__u32);
1472                                 *mdsnum = i;
1473                                 RETURN(0);
1474                         }
1475                 }
1476                 LASSERT(0);
1477         }
1478
1479         CDEBUG(D_IOCTL, "invalid key\n");
1480         RETURN(-EINVAL);
1481 }
1482
1483 int lmv_set_info(struct obd_export *exp, obd_count keylen,
1484                  void *key, obd_count vallen, void *val)
1485 {
1486         struct obd_device *obd;
1487         struct lmv_obd *lmv;
1488         ENTRY;
1489
1490         obd = class_exp2obd(exp);
1491         if (obd == NULL) {
1492                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
1493                        exp->exp_handle.h_cookie);
1494                 RETURN(-EINVAL);
1495         }
1496         lmv = &obd->u.lmv;
1497
1498         if (keylen >= strlen("client") && strcmp(key, "client") == 0) {
1499                 struct lmv_tgt_desc *tgts;
1500                 int i, rc;
1501
1502                 rc = lmv_check_connect(obd);
1503                 if (rc)
1504                         RETURN(rc);
1505
1506                 for (i = 0, tgts = lmv->tgts; 
1507                         i < lmv->desc.ld_tgt_count; i++, tgts++) {
1508                         rc = obd_set_info(tgts->ltd_exp, keylen, key, vallen, val);
1509                         if (rc)
1510                                 RETURN(rc);
1511                 }
1512                 RETURN(0);
1513         } else if (keylen >= strlen("inter_mds") && strcmp(key, "inter_mds") == 0) {
1514                 lmv->server_timeout = 1;
1515                 lmv_set_timeouts(obd);
1516                 RETURN(0);
1517         }
1518         
1519         RETURN(-EINVAL);
1520 }
1521
1522 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
1523                struct lov_stripe_md *lsm)
1524 {
1525         struct obd_device *obd = class_exp2obd(exp);
1526         struct lmv_obd *lmv = &obd->u.lmv;
1527         int mea_size;
1528         ENTRY;
1529
1530         mea_size = sizeof(struct ll_fid) * 
1531                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1532         if (!lmmp)
1533                 RETURN(mea_size);
1534
1535         if (*lmmp && !lsm) {
1536                 OBD_FREE(*lmmp, mea_size);
1537                 *lmmp = NULL;
1538                 RETURN(0);
1539         }
1540
1541         if (*lmmp == NULL) {
1542                 OBD_ALLOC(*lmmp, mea_size);
1543                 if (*lmmp == NULL)
1544                         RETURN(-ENOMEM);
1545         }
1546
1547         if (!lsm)
1548                 RETURN(mea_size);
1549
1550 #warning "MEA packing/convertation must be here! -bzzz"
1551         memcpy(*lmmp, lsm, mea_size);
1552         RETURN(mea_size);
1553 }
1554
1555 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **mem_tgt,
1556                         struct lov_mds_md *disk_src, int mdsize)
1557 {
1558         struct obd_device *obd = class_exp2obd(exp);
1559         struct lmv_obd *lmv = &obd->u.lmv;
1560         struct mea **tmea = (struct mea **) mem_tgt;
1561         struct mea *mea = (void *) disk_src;
1562         int mea_size;
1563         ENTRY;
1564
1565         mea_size = sizeof(struct ll_fid) * 
1566                 lmv->desc.ld_tgt_count + sizeof(struct mea);
1567         if (mem_tgt == NULL)
1568                 return mea_size;
1569
1570         if (*mem_tgt != NULL && disk_src == NULL) {
1571                 OBD_FREE(*tmea, mea_size);
1572                 RETURN(0);
1573         }
1574
1575         LASSERT(mea_size == mdsize);
1576
1577         OBD_ALLOC(*tmea, mea_size);
1578         if (*tmea == NULL)
1579                 RETURN(-ENOMEM);
1580
1581         if (!disk_src)
1582                 RETURN(mea_size);
1583
1584 #warning "MEA unpacking/convertation must be here! -bzzz"
1585         memcpy(*tmea, mea, mdsize);
1586         RETURN(mea_size);
1587 }
1588
1589 int lmv_brw(int rw, struct obd_export *exp, struct obdo *oa,
1590                 struct lov_stripe_md *ea, obd_count oa_bufs,
1591                 struct brw_page *pgarr, struct obd_trans_info *oti)
1592 {
1593         struct obd_device *obd = exp->exp_obd;
1594         struct lmv_obd *lmv = &obd->u.lmv;
1595         struct mea *mea = (struct mea *) ea;
1596         int err;
1597       
1598         LASSERT(oa != NULL);
1599         LASSERT(ea != NULL);
1600         LASSERT(pgarr != NULL);
1601         LASSERT(oa->o_mds < lmv->desc.ld_tgt_count);
1602
1603         oa->o_gr = mea->mea_fids[oa->o_mds].generation;
1604         oa->o_id = mea->mea_fids[oa->o_mds].id;
1605         oa->o_valid =  OBD_MD_FLID | OBD_MD_FLGROUP;
1606         err = obd_brw(rw, lmv->tgts[oa->o_mds].ltd_exp, oa,
1607                       NULL, oa_bufs, pgarr, oti);
1608         RETURN(err);
1609 }
1610
1611 struct obd_ops lmv_obd_ops = {
1612         .o_owner                = THIS_MODULE,
1613         .o_attach               = lmv_attach,
1614         .o_detach               = lmv_detach,
1615         .o_setup                = lmv_setup,
1616         .o_cleanup              = lmv_cleanup,
1617         .o_connect              = lmv_connect,
1618         .o_disconnect           = lmv_disconnect,
1619         .o_statfs               = lmv_statfs,
1620         .o_get_info             = lmv_get_info,
1621         .o_set_info             = lmv_set_info,
1622         .o_create               = lmv_obd_create,
1623         .o_packmd               = lmv_packmd,
1624         .o_unpackmd             = lmv_unpackmd,
1625         .o_brw                  = lmv_brw,
1626         .o_init_ea_size         = lmv_init_ea_size,
1627         .o_notify               = lmv_notify,
1628         .o_iocontrol            = lmv_iocontrol,
1629 };
1630
1631 struct md_ops lmv_md_ops = {
1632         .m_getstatus            = lmv_getstatus,
1633         .m_getattr              = lmv_getattr,
1634         .m_change_cbdata        = lmv_change_cbdata,
1635         .m_change_cbdata_name   = lmv_change_cbdata_name,
1636         .m_close                = lmv_close,
1637         .m_create               = lmv_create,
1638         .m_done_writing         = lmv_done_writing,
1639         .m_enqueue              = lmv_enqueue,
1640         .m_getattr_name         = lmv_getattr_name,
1641         .m_intent_lock          = lmv_intent_lock,
1642         .m_link                 = lmv_link,
1643         .m_rename               = lmv_rename,
1644         .m_setattr              = lmv_setattr,
1645         .m_sync                 = lmv_sync,
1646         .m_readpage             = lmv_readpage,
1647         .m_unlink               = lmv_unlink,
1648         .m_get_real_obd         = lmv_get_real_obd,
1649         .m_valid_attrs          = lmv_valid_attrs,
1650 };
1651
1652 int __init lmv_init(void)
1653 {
1654         struct lprocfs_static_vars lvars;
1655         int rc;
1656
1657         lprocfs_init_vars(lmv, &lvars);
1658         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
1659                                  lvars.module_vars, OBD_LMV_DEVICENAME);
1660         RETURN(rc);
1661 }
1662
1663 #ifdef __KERNEL__
1664 static void lmv_exit(void)
1665 {
1666         class_unregister_type(OBD_LMV_DEVICENAME);
1667 }
1668
1669 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1670 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
1671 MODULE_LICENSE("GPL");
1672
1673 module_init(lmv_init);
1674 module_exit(lmv_exit);
1675 #endif