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