Whamcloud - gitweb
0d6a088e652c50d2a4f3c261cf1a5ff4db47bda0
[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, 2004, 2005, 2006 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 <linux/mm.h>
33 #include <asm/div64.h>
34 #include <linux/seq_file.h>
35 #include <linux/namei.h>
36 #else
37 #include <liblustre.h>
38 #endif
39
40 #include <linux/ext2_fs.h>
41
42 #include <lustre/lustre_idl.h>
43 #include <lustre_log.h>
44 #include <obd_support.h>
45 #include <lustre_lib.h>
46 #include <lustre_net.h>
47 #include <obd_class.h>
48 #include <lprocfs_status.h>
49 #include <lustre_lite.h>
50 #include <lustre_fid.h>
51 #include "lmv_internal.h"
52
53 /* not defined for liblustre building */
54 #if !defined(ATOMIC_INIT)
55 #define ATOMIC_INIT(val) { (val) }
56 #endif
57
58 /* object cache. */
59 cfs_mem_cache_t *obj_cache;
60 atomic_t obj_cache_count = ATOMIC_INIT(0);
61
62 static void lmv_activate_target(struct lmv_obd *lmv,
63                                 struct lmv_tgt_desc *tgt,
64                                 int activate)
65 {
66         if (tgt->ltd_active == activate)
67                 return;
68
69         tgt->ltd_active = activate;
70         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
71 }
72
73 /* Error codes:
74  *
75  *  -EINVAL  : UUID can't be found in the LMV's target list
76  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
77  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
78  */
79 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
80                               int activate)
81 {
82         struct lmv_tgt_desc *tgt;
83         struct obd_device *obd;
84         int i, rc = 0;
85         ENTRY;
86
87         CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
88                lmv, uuid->uuid, activate);
89
90         spin_lock(&lmv->lmv_lock);
91         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
92                 if (tgt->ltd_exp == NULL)
93                         continue;
94
95                 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
96                        i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
97
98                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
99                         break;
100         }
101
102         if (i == lmv->desc.ld_tgt_count)
103                 GOTO(out_lmv_lock, rc = -EINVAL);
104
105         obd = class_exp2obd(tgt->ltd_exp);
106         if (obd == NULL)
107                 GOTO(out_lmv_lock, rc = -ENOTCONN);
108
109         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
110                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
111                obd->obd_type->typ_name, i);
112         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
113
114         if (tgt->ltd_active == activate) {
115                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
116                        activate ? "" : "in");
117                 GOTO(out_lmv_lock, rc);
118         }
119
120         CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
121                obd, activate ? "" : "in");
122
123         lmv_activate_target(lmv, tgt, activate);
124
125         EXIT;
126
127  out_lmv_lock:
128         spin_unlock(&lmv->lmv_lock);
129         return rc;
130 }
131
132 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
133                             struct obd_connect_data *data)
134 {
135         struct lmv_tgt_desc *tgt;
136         int i;
137         ENTRY;
138
139         LASSERT(data != NULL);
140
141         spin_lock(&lmv->lmv_lock);
142         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
143                 if (tgt->ltd_exp == NULL)
144                         continue;
145
146                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
147                         lmv->datas[tgt->ltd_idx] = *data;
148                         break;
149                 }
150         }
151         spin_unlock(&lmv->lmv_lock);
152         RETURN(0);
153 }
154
155 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
156                       enum obd_notify_event ev, void *data)
157 {
158         struct lmv_obd *lmv = &obd->u.lmv;
159         struct obd_uuid *uuid;
160         int rc = 0;
161         ENTRY;
162
163         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
164                 CERROR("unexpected notification of %s %s!\n",
165                        watched->obd_type->typ_name,
166                        watched->obd_name);
167                 RETURN(-EINVAL);
168         }
169
170         uuid = &watched->u.cli.cl_target_uuid;
171         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
172                 /*
173                  * Set MDC as active before notifying the observer, so the
174                  * observer can use the MDC normally.
175                  */
176                 rc = lmv_set_mdc_active(lmv, uuid,
177                                         ev == OBD_NOTIFY_ACTIVE);
178                 if (rc) {
179                         CERROR("%sactivation of %s failed: %d\n",
180                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
181                                uuid->uuid, rc);
182                         RETURN(rc);
183                 }
184         } else if (ev == OBD_NOTIFY_OCD) {
185                 struct obd_connect_data *conn_data =
186                         &watched->u.cli.cl_import->imp_connect_data;
187
188                 /* Set connect data to desired target, update
189                  * exp_connect_flags. */
190                 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
191                 if (rc) {
192                         CERROR("can't set connect data to target %s, rc %d\n",
193                                uuid->uuid, rc);
194                         RETURN(rc);
195                 }
196
197                 /*
198                  * XXX: Make sure that ocd_connect_flags from all targets are
199                  * the same. Otherwise one of MDTs runs wrong version or
200                  * something like this.  --umka
201                  */
202                 obd->obd_self_export->exp_connect_flags =
203                         conn_data->ocd_connect_flags;
204         }
205 #if 0
206         else if (ev == OBD_NOTIFY_DISCON) {
207                 /* For disconnect event, flush fld cache for failout MDS case. */
208                 fld_client_flush(&lmv->lmv_fld);
209         }
210 #endif
211         /* Pass the notification up the chain. */
212         if (obd->obd_observer)
213                 rc = obd_notify(obd->obd_observer, watched, ev, data);
214
215         RETURN(rc);
216 }
217
218 /* this is fake connect function. Its purpose is to initialize lmv and say
219  * caller that everything is okay. Real connection will be performed later. */
220 static int lmv_connect(const struct lu_env *env,
221                        struct lustre_handle *conn, struct obd_device *obd,
222                        struct obd_uuid *cluuid, struct obd_connect_data *data)
223 {
224 #ifdef __KERNEL__
225         struct proc_dir_entry *lmv_proc_dir;
226 #endif
227         struct lmv_obd *lmv = &obd->u.lmv;
228         struct obd_export *exp;
229         int rc = 0;
230         ENTRY;
231
232         rc = class_connect(conn, obd, cluuid);
233         if (rc) {
234                 CERROR("class_connection() returned %d\n", rc);
235                 RETURN(rc);
236         }
237
238         exp = class_conn2export(conn);
239
240         /* we don't want to actually do the underlying connections more than
241          * once, so keep track. */
242         lmv->refcount++;
243         if (lmv->refcount > 1) {
244                 class_export_put(exp);
245                 RETURN(0);
246         }
247
248         lmv->exp = exp;
249         lmv->connected = 0;
250         lmv->cluuid = *cluuid;
251
252         if (data)
253                 lmv->conn_data = *data;
254
255 #ifdef __KERNEL__
256         lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
257                                         NULL, NULL);
258         if (IS_ERR(lmv_proc_dir)) {
259                 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
260                        obd->obd_type->typ_name, obd->obd_name);
261                 lmv_proc_dir = NULL;
262         }
263 #endif
264
265         /* all real clients should perform actual connection right away, because
266          * it is possible, that LMV will not have opportunity to connect targets
267          * and MDC stuff will be called directly, for instance while reading
268          * ../mdc/../kbytesfree procfs file, etc. */
269         if (data->ocd_connect_flags & OBD_CONNECT_REAL)
270                 rc = lmv_check_connect(obd);
271
272 #ifdef __KERNEL__
273         if (rc) {
274                 if (lmv_proc_dir)
275                         lprocfs_remove(&lmv_proc_dir);
276         }
277 #endif
278
279         RETURN(rc);
280 }
281
282 static void lmv_set_timeouts(struct obd_device *obd)
283 {
284         struct lmv_tgt_desc *tgts;
285         struct lmv_obd *lmv;
286         int i;
287
288         lmv = &obd->u.lmv;
289         if (lmv->server_timeout == 0)
290                 return;
291
292         if (lmv->connected == 0)
293                 return;
294
295         for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
296                 if (tgts->ltd_exp == NULL)
297                         continue;
298
299                 obd_set_info_async(tgts->ltd_exp, strlen("inter_mds"),
300                                    "inter_mds", 0, NULL, NULL);
301         }
302 }
303
304 static int lmv_init_ea_size(struct obd_export *exp, int easize,
305                             int def_easize, int cookiesize)
306 {
307         struct obd_device *obd = exp->exp_obd;
308         struct lmv_obd *lmv = &obd->u.lmv;
309         int i, rc = 0, change = 0;
310         ENTRY;
311
312         if (lmv->max_easize < easize) {
313                 lmv->max_easize = easize;
314                 change = 1;
315         }
316         if (lmv->max_def_easize < def_easize) {
317                 lmv->max_def_easize = def_easize;
318                 change = 1;
319         }
320         if (lmv->max_cookiesize < cookiesize) {
321                 lmv->max_cookiesize = cookiesize;
322                 change = 1;
323         }
324         if (change == 0)
325                 RETURN(0);
326
327         if (lmv->connected == 0)
328                 RETURN(0);
329
330         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
331                 if (lmv->tgts[i].ltd_exp == NULL) {
332                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
333                         continue;
334                 }
335
336                 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
337                                      cookiesize);
338                 if (rc) {
339                         CERROR("obd_init_ea_size() failed on MDT target %d, "
340                                "error %d.\n", i, rc);
341                         break;
342                 }
343         }
344         RETURN(rc);
345 }
346
347 #define MAX_STRING_SIZE 128
348
349 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
350 {
351         struct lmv_obd *lmv = &obd->u.lmv;
352         struct obd_uuid *cluuid = &lmv->cluuid;
353         struct obd_connect_data *mdc_data = NULL;
354         struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
355         struct lustre_handle conn = {0, };
356         struct obd_device *mdc_obd;
357         struct obd_export *mdc_exp;
358         struct lu_fld_target target;
359         int rc;
360 #ifdef __KERNEL__
361         struct proc_dir_entry *lmv_proc_dir;
362 #endif
363         ENTRY;
364
365         /* for MDS: don't connect to yourself */
366         if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
367                 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
368                 /* XXX - the old code didn't increment active tgt count.
369                  *       should we ? */
370                 RETURN(0);
371         }
372
373         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
374                                         &obd->obd_uuid);
375         if (!mdc_obd) {
376                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
377                 RETURN(-EINVAL);
378         }
379
380         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
381                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
382                 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
383                 cluuid->uuid);
384
385         if (!mdc_obd->obd_set_up) {
386                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
387                 RETURN(-EINVAL);
388         }
389
390         rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
391                          &lmv->conn_data);
392         if (rc) {
393                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
394                 RETURN(rc);
395         }
396
397         mdc_exp = class_conn2export(&conn);
398
399         /* Init fid sequence client for this mdc. */
400         rc = obd_fid_init(mdc_exp);
401         if (rc)
402                 RETURN(rc);
403
404         /* Add new FLD target. */
405         target.ft_srv = NULL;
406         target.ft_exp = mdc_exp;
407         target.ft_idx = tgt->ltd_idx;
408
409         fld_client_add_target(&lmv->lmv_fld, &target);
410
411         mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
412
413         rc = obd_register_observer(mdc_obd, obd);
414         if (rc) {
415                 obd_disconnect(mdc_exp);
416                 CERROR("target %s register_observer error %d\n",
417                        tgt->ltd_uuid.uuid, rc);
418                 RETURN(rc);
419         }
420
421         if (obd->obd_observer) {
422                 /* tell the mds_lmv about the new target */
423                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
424                                 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
425                 if (rc) {
426                         obd_disconnect(mdc_exp);
427                         RETURN(rc);
428                 }
429         }
430
431         tgt->ltd_active = 1;
432         tgt->ltd_exp = mdc_exp;
433         lmv->desc.ld_active_tgt_count++;
434
435         /* copy connect data, it may be used later */
436         lmv->datas[tgt->ltd_idx] = *mdc_data;
437
438         md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
439                         lmv->max_def_easize, lmv->max_cookiesize);
440
441         CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
442                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
443                 atomic_read(&obd->obd_refcount));
444
445 #ifdef __KERNEL__
446         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
447         if (lmv_proc_dir) {
448                 struct proc_dir_entry *mdc_symlink;
449                 char name[MAX_STRING_SIZE + 1];
450
451                 LASSERT(mdc_obd->obd_type != NULL);
452                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
453                 name[MAX_STRING_SIZE] = '\0';
454                 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
455                          mdc_obd->obd_type->typ_name,
456                          mdc_obd->obd_name);
457                 mdc_symlink = proc_symlink(mdc_obd->obd_name,
458                                            lmv_proc_dir, name);
459                 if (mdc_symlink == NULL) {
460                         CERROR("could not register LMV target "
461                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
462                                obd->obd_type->typ_name, obd->obd_name,
463                                mdc_obd->obd_name);
464                         lprocfs_remove(&lmv_proc_dir);
465                         lmv_proc_dir = NULL;
466                 }
467         }
468 #endif
469         RETURN(0);
470 }
471
472 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
473 {
474         struct lmv_obd *lmv = &obd->u.lmv;
475         struct lmv_tgt_desc *tgt;
476         int rc = 0;
477         ENTRY;
478
479         CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
480
481         lmv_init_lock(lmv);
482
483         if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
484                 lmv_init_unlock(lmv);
485                 CERROR("can't add %s, LMV module compiled for %d MDCs. "
486                        "That many MDCs already configured.\n",
487                        tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
488                 RETURN(-EINVAL);
489         }
490         if (lmv->desc.ld_tgt_count == 0) {
491                 struct obd_device *mdc_obd;
492
493                 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
494                                                 &obd->obd_uuid);
495                 if (!mdc_obd) {
496                         lmv_init_unlock(lmv);
497                         CERROR("Target %s not attached\n", tgt_uuid->uuid);
498                         RETURN(-EINVAL);
499                 }
500
501                 rc = obd_llog_init(obd, NULL, mdc_obd, 0, NULL, tgt_uuid);
502                 if (rc) {
503                         lmv_init_unlock(lmv);
504                         CERROR("lmv failed to setup llogging subsystems\n");
505                 }
506         }
507         spin_lock(&lmv->lmv_lock);
508         tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
509         tgt->ltd_uuid = *tgt_uuid;
510         spin_unlock(&lmv->lmv_lock);
511
512         if (lmv->connected) {
513                 rc = lmv_connect_mdc(obd, tgt);
514                 if (rc) {
515                         spin_lock(&lmv->lmv_lock);
516                         lmv->desc.ld_tgt_count--;
517                         memset(tgt, 0, sizeof(*tgt));
518                         spin_unlock(&lmv->lmv_lock);
519                 } else {
520                         int easize = sizeof(struct lmv_stripe_md) +
521                                      lmv->desc.ld_tgt_count *
522                                      sizeof(struct lu_fid);
523                         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
524                 }
525         }
526
527         lmv_init_unlock(lmv);
528         RETURN(rc);
529 }
530
531 /* performs a check if passed obd is connected. If no - connect it. */
532 int lmv_check_connect(struct obd_device *obd)
533 {
534         struct lmv_obd *lmv = &obd->u.lmv;
535         struct lmv_tgt_desc *tgt;
536         int i, rc, easize;
537         ENTRY;
538
539         if (lmv->connected)
540                 RETURN(0);
541
542         lmv_init_lock(lmv);
543         if (lmv->connected) {
544                 lmv_init_unlock(lmv);
545                 RETURN(0);
546         }
547
548         if (lmv->desc.ld_tgt_count == 0) {
549                 CERROR("%s: no targets configured.\n", obd->obd_name);
550                 RETURN(-EINVAL);
551         }
552
553         CDEBUG(D_CONFIG, "time to connect %s to %s\n",
554                lmv->cluuid.uuid, obd->obd_name);
555
556         LASSERT(lmv->tgts != NULL);
557
558         for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
559                 rc = lmv_connect_mdc(obd, tgt);
560                 if (rc)
561                         GOTO(out_disc, rc);
562         }
563
564         lmv_set_timeouts(obd);
565         class_export_put(lmv->exp);
566         lmv->connected = 1;
567         easize = lmv_get_easize(lmv);
568         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
569         lmv_init_unlock(lmv);
570         RETURN(0);
571
572  out_disc:
573         while (i-- > 0) {
574                 int rc2;
575                 --tgt;
576                 tgt->ltd_active = 0;
577                 if (tgt->ltd_exp) {
578                         --lmv->desc.ld_active_tgt_count;
579                         rc2 = obd_disconnect(tgt->ltd_exp);
580                         if (rc2) {
581                                 CERROR("error: LMV target %s disconnect on "
582                                        "MDC idx %d: error %d\n",
583                                        tgt->ltd_uuid.uuid, i, rc2);
584                         }
585                 }
586         }
587         class_disconnect(lmv->exp);
588         lmv_init_unlock(lmv);
589         RETURN(rc);
590 }
591
592 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
593 {
594 #ifdef __KERNEL__
595         struct proc_dir_entry *lmv_proc_dir;
596 #endif
597         struct lmv_obd *lmv = &obd->u.lmv;
598         struct obd_device *mdc_obd;
599         int rc;
600         ENTRY;
601
602         LASSERT(tgt != NULL);
603         LASSERT(obd != NULL);
604
605         mdc_obd = class_exp2obd(tgt->ltd_exp);
606
607         if (mdc_obd)
608                 mdc_obd->obd_no_recov = obd->obd_no_recov;
609
610 #ifdef __KERNEL__
611         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
612         if (lmv_proc_dir) {
613                 struct proc_dir_entry *mdc_symlink;
614
615                 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
616                 if (mdc_symlink) {
617                         lprocfs_remove(&mdc_symlink);
618                 } else {
619                         CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
620                                obd->obd_type->typ_name, obd->obd_name,
621                                mdc_obd->obd_name);
622                 }
623         }
624 #endif
625         rc = obd_fid_fini(tgt->ltd_exp);
626         if (rc)
627                 CERROR("Can't finanize fids factory\n");
628
629         CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
630                tgt->ltd_exp->exp_obd->obd_name,
631                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
632
633         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
634         rc = obd_disconnect(tgt->ltd_exp);
635         if (rc) {
636                 if (tgt->ltd_active) {
637                         CERROR("Target %s disconnect error %d\n",
638                                tgt->ltd_uuid.uuid, rc);
639                 }
640         }
641
642         lmv_activate_target(lmv, tgt, 0);
643         tgt->ltd_exp = NULL;
644         RETURN(0);
645 }
646
647 static int lmv_disconnect(struct obd_export *exp)
648 {
649         struct obd_device *obd = class_exp2obd(exp);
650 #ifdef __KERNEL__
651         struct proc_dir_entry *lmv_proc_dir;
652 #endif
653         struct lmv_obd *lmv = &obd->u.lmv;
654         int rc, i;
655         ENTRY;
656
657         if (!lmv->tgts)
658                 goto out_local;
659
660         /* Only disconnect the underlying layers on the final disconnect. */
661         lmv->refcount--;
662         if (lmv->refcount != 0)
663                 goto out_local;
664
665         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
666                 if (lmv->tgts[i].ltd_exp == NULL)
667                         continue;
668                 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
669         }
670
671 #ifdef __KERNEL__
672         lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
673         if (lmv_proc_dir) {
674                 lprocfs_remove(&lmv_proc_dir);
675         } else {
676                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
677                        obd->obd_type->typ_name, obd->obd_name);
678         }
679 #endif
680
681 out_local:
682         /*
683          * This is the case when no real connection is established by
684          * lmv_check_connect().
685          */
686         if (!lmv->connected)
687                 class_export_put(exp);
688         rc = class_disconnect(exp);
689         if (lmv->refcount == 0)
690                 lmv->connected = 0;
691         RETURN(rc);
692 }
693
694 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
695                          int len, void *karg, void *uarg)
696 {
697         struct obd_device *obddev = class_exp2obd(exp);
698         struct lmv_obd *lmv = &obddev->u.lmv;
699         int i, rc = 0, set = 0;
700         ENTRY;
701
702         if (lmv->desc.ld_tgt_count == 0)
703                 RETURN(-ENOTTY);
704
705         switch (cmd) {
706         case IOC_OBD_STATFS: {
707                 struct obd_ioctl_data *data = karg;
708                 struct obd_device *mdc_obd;
709                 struct obd_statfs stat_buf = {0};
710                 __u32 index;
711
712                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
713                 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
714
715                 if ((index >= lmv->desc.ld_tgt_count))
716                         RETURN(-ENODEV);
717                 
718                 if (!lmv->tgts[index].ltd_active)
719                         RETURN(-ENODATA);
720
721                 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
722                 if (!mdc_obd)
723                         RETURN(-EINVAL);
724
725                 /* got statfs data */
726                 rc = obd_statfs(mdc_obd, &stat_buf, cfs_time_current_64() - 1);
727                 if (rc)
728                         RETURN(rc);
729                 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
730                         RETURN(rc);
731                 /* copy UUID */
732                 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
733                                   data->ioc_plen2);
734                 break;
735         }
736         default : {
737                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
738                         int err;
739
740                         if (lmv->tgts[i].ltd_exp == NULL)
741                                 continue;
742
743                         err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
744                                             karg, uarg);
745                         if (err) {
746                                 if (lmv->tgts[i].ltd_active) {
747                                         CERROR("error: iocontrol MDC %s on MDT"
748                                                "idx %d cmd %x: err = %d\n",
749                                                 lmv->tgts[i].ltd_uuid.uuid,
750                                                 i, cmd, err);
751                                         if (!rc)
752                                                 rc = err;
753                                 }
754                         } else
755                                 set = 1;
756                 }
757                 if (!set && !rc)
758                         rc = -EIO;
759         }
760         }
761         RETURN(rc);
762 }
763
764 enum MDS_POLICY {
765      CHAR_TYPE,
766      NID_TYPE
767 };
768
769 static int lmv_all_chars_policy(int count, const char *name,
770                                 int len)
771 {
772         unsigned int c = 0;
773
774         while (len > 0)
775                 c += name[--len];
776         c = c % count;
777         return c;
778 }
779
780 static int lmv_nid_policy(struct lmv_obd *lmv)
781 {
782         struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
783         __u32 id;
784         /*
785          * XXX Hack: to get nid we assume that underlying obd device is mdc.
786          */
787         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
788         return id % lmv->desc.ld_tgt_count;
789 }
790
791 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
792                           int type)
793 {
794         switch (type) {
795         case CHAR_TYPE:
796                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
797                                             op_data->op_name,
798                                             op_data->op_namelen);
799         case NID_TYPE:
800                 return lmv_nid_policy(lmv);
801
802         default:
803                 break;
804         }
805
806         CERROR("unsupport type %d \n", type);
807         return -EINVAL;
808 }
809
810 /* This is _inode_ placement policy function (not name). */
811 static int lmv_placement_policy(struct obd_device *obd,
812                                 struct md_op_data *op_data,
813                                 mdsno_t *mds)
814 {
815         struct lmv_obd *lmv = &obd->u.lmv;
816         struct lmv_obj *obj;
817         int rc;
818         ENTRY;
819
820         LASSERT(mds != NULL);
821
822         /*
823          * Allocate new fid on target according to operation type and parent
824          * home mds.
825          */
826         obj = lmv_obj_grab(obd, &op_data->op_fid1);
827         if (obj != NULL || op_data->op_name == NULL ||
828             op_data->op_opc != LUSTRE_OPC_MKDIR) {
829                 /*
830                  * Allocate fid for non-dir or for null name or for case parent
831                  * dir is split.
832                  */
833                 if (obj) {
834                         lmv_obj_put(obj);
835
836                         /*
837                          * If we have this flag turned on, and we see that
838                          * parent dir is split, this means, that caller did not
839                          * notice split yet. This is race and we would like to
840                          * let caller know that.
841                          */
842                         if (op_data->op_bias & MDS_CHECK_SPLIT)
843                                 RETURN(-ERESTART);
844                 }
845
846                 /*
847                  * Allocate new fid on same mds where parent fid is located and
848                  * where operation will be sent. In case of split dir, ->op_fid1
849                  * and ->op_mds here will contain fid and mds of slave directory
850                  * object (assigned by caller).
851                  */
852                 *mds = op_data->op_mds;
853                 rc = 0;
854
855 #if 0
856                 /* XXX: This should be removed later wehn we sure it is not
857                  * needed. */
858                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
859                 if (rc)
860                         GOTO(out, rc);
861 #endif
862         } else {
863                 /*
864                  * Parent directory is not split and we want to create a
865                  * directory in it. Let's calculate where to place it according
866                  * to name.
867                  */
868                 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
869                 rc = 0;
870         }
871         EXIT;
872 #if 0
873 out:
874 #endif
875         if (rc) {
876                 CERROR("Can't choose MDS, err = %d\n", rc);
877         } else {
878                 LASSERT(*mds < lmv->desc.ld_tgt_count);
879         }
880
881         return rc;
882 }
883
884 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
885                     mdsno_t mds)
886 {
887         struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
888         int rc;
889         ENTRY;
890
891         /* New seq alloc and FLD setup should be atomic. */
892         down(&tgt->ltd_fid_sem);
893
894         /* Asking underlaying tgt layer to allocate new fid. */
895         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
896         if (rc > 0) {
897                 LASSERT(fid_is_sane(fid));
898
899                 /* Client switches to new sequence, setup FLD. */
900                 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
901                                        mds, NULL);
902                 if (rc) {
903                         CERROR("Can't create fld entry, rc %d\n", rc);
904                         /* Delete just allocated fid sequence */
905                         obd_fid_delete(tgt->ltd_exp, NULL);
906                 }
907         }
908         up(&tgt->ltd_fid_sem);
909         RETURN(rc);
910 }
911
912 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
913                   struct md_op_data *op_data)
914 {
915         struct obd_device *obd = class_exp2obd(exp);
916         struct lmv_obd *lmv = &obd->u.lmv;
917         mdsno_t mds;
918         int rc;
919         ENTRY;
920
921         LASSERT(op_data != NULL);
922         LASSERT(fid != NULL);
923
924         rc = lmv_placement_policy(obd, op_data, &mds);
925         if (rc) {
926                 CERROR("Can't get target for allocating fid, "
927                        "rc %d\n", rc);
928                 RETURN(rc);
929         }
930
931         rc = __lmv_fid_alloc(lmv, fid, mds);
932         if (rc) {
933                 CERROR("Can't alloc new fid, rc %d\n", rc);
934                 RETURN(rc);
935         }
936
937         RETURN(rc);
938 }
939
940 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
941 {
942         ENTRY;
943
944         LASSERT(exp && fid);
945         if (lmv_obj_delete(exp, fid)) {
946                 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
947                        PFID(fid));
948         }
949         RETURN(0);
950 }
951
952 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
953 {
954         struct lmv_obd *lmv = &obd->u.lmv;
955         struct lprocfs_static_vars lvars;
956         struct lmv_desc *desc;
957         int rc, i = 0;
958         ENTRY;
959
960         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
961                 CERROR("LMV setup requires a descriptor\n");
962                 RETURN(-EINVAL);
963         }
964
965         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
966         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
967                 CERROR("descriptor size wrong: %d > %d\n",
968                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
969                 RETURN(-EINVAL);
970         }
971
972         lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
973
974         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
975         if (lmv->tgts == NULL)
976                 RETURN(-ENOMEM);
977
978         for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
979                 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
980                 lmv->tgts[i].ltd_idx = i;
981         }
982
983         lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
984
985         OBD_ALLOC(lmv->datas, lmv->datas_size);
986         if (lmv->datas == NULL)
987                 GOTO(out_free_tgts, rc = -ENOMEM);
988
989         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
990         lmv->desc.ld_tgt_count = 0;
991         lmv->desc.ld_active_tgt_count = 0;
992         lmv->max_cookiesize = 0;
993         lmv->max_def_easize = 0;
994         lmv->max_easize = 0;
995
996         spin_lock_init(&lmv->lmv_lock);
997         sema_init(&lmv->init_sem, 1);
998
999         rc = lmv_obj_setup(obd);
1000         if (rc) {
1001                 CERROR("Can't setup LMV object manager, "
1002                        "error %d.\n", rc);
1003                 GOTO(out_free_datas, rc);
1004         }
1005
1006         lprocfs_lmv_init_vars(&lvars);
1007         lprocfs_obd_setup(obd, lvars.obd_vars);
1008 #ifdef LPROCFS
1009         {
1010                 struct proc_dir_entry *entry;
1011
1012                 entry = create_proc_entry("target_obd_status", 0444,
1013                                           obd->obd_proc_entry);
1014                 if (entry != NULL) {
1015                         entry->proc_fops = &lmv_proc_target_fops;
1016                         entry->data = obd;
1017                 }
1018        }
1019 #endif
1020         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1021                              LUSTRE_CLI_FLD_HASH_DHT);
1022         if (rc) {
1023                 CERROR("can't init FLD, err %d\n",
1024                        rc);
1025                 GOTO(out_free_datas, rc);
1026         }
1027
1028         RETURN(0);
1029
1030 out_free_datas:
1031         OBD_FREE(lmv->datas, lmv->datas_size);
1032         lmv->datas = NULL;
1033 out_free_tgts:
1034         OBD_FREE(lmv->tgts, lmv->tgts_size);
1035         lmv->tgts = NULL;
1036         return rc;
1037 }
1038
1039 static int lmv_cleanup(struct obd_device *obd)
1040 {
1041         struct lmv_obd *lmv = &obd->u.lmv;
1042         ENTRY;
1043
1044         fld_client_fini(&lmv->lmv_fld);
1045         lprocfs_obd_cleanup(obd);
1046         lmv_obj_cleanup(obd);
1047         OBD_FREE(lmv->datas, lmv->datas_size);
1048         OBD_FREE(lmv->tgts, lmv->tgts_size);
1049
1050         RETURN(0);
1051 }
1052
1053 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1054 {
1055         struct lustre_cfg *lcfg = buf;
1056         struct obd_uuid tgt_uuid;
1057         int rc;
1058         ENTRY;
1059
1060         switch(lcfg->lcfg_command) {
1061         case LCFG_ADD_MDC:
1062                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1063                         GOTO(out, rc = -EINVAL);
1064
1065                 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1066                 rc = lmv_add_target(obd, &tgt_uuid);
1067                 GOTO(out, rc);
1068         default: {
1069                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1070                 GOTO(out, rc = -EINVAL);
1071         }
1072         }
1073 out:
1074         RETURN(rc);
1075 }
1076
1077 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1078                       __u64 max_age)
1079 {
1080         struct lmv_obd *lmv = &obd->u.lmv;
1081         struct obd_statfs *temp;
1082         int rc = 0, i;
1083         ENTRY;
1084
1085         rc = lmv_check_connect(obd);
1086         if (rc)
1087                 RETURN(rc);
1088
1089         OBD_ALLOC(temp, sizeof(*temp));
1090         if (temp == NULL)
1091                 RETURN(-ENOMEM);
1092
1093         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1094                 if (lmv->tgts[i].ltd_exp == NULL)
1095                         continue;
1096
1097                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp, max_age);
1098                 if (rc) {
1099                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1100                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1101                                rc);
1102                         GOTO(out_free_temp, rc);
1103                 }
1104                 if (i == 0) {
1105                         *osfs = *temp;
1106                 } else {
1107                         osfs->os_bavail += temp->os_bavail;
1108                         osfs->os_blocks += temp->os_blocks;
1109                         osfs->os_ffree += temp->os_ffree;
1110                         osfs->os_files += temp->os_files;
1111                 }
1112         }
1113
1114         EXIT;
1115 out_free_temp:
1116         OBD_FREE(temp, sizeof(*temp));
1117         return rc;
1118 }
1119
1120 static int lmv_getstatus(struct obd_export *exp,
1121                          struct lu_fid *fid,
1122                          struct obd_capa **pc)
1123 {
1124         struct obd_device *obd = exp->exp_obd;
1125         struct lmv_obd *lmv = &obd->u.lmv;
1126         int rc;
1127         ENTRY;
1128
1129         rc = lmv_check_connect(obd);
1130         if (rc)
1131                 RETURN(rc);
1132
1133         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1134
1135         RETURN(rc);
1136 }
1137
1138 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1139                         struct obd_capa *oc, obd_valid valid, const char *name,
1140                         const char *input, int input_size, int output_size,
1141                         int flags, struct ptlrpc_request **request)
1142 {
1143         struct obd_device *obd = exp->exp_obd;
1144         struct lmv_obd *lmv = &obd->u.lmv;
1145         struct obd_export *tgt_exp;
1146         int rc;
1147         ENTRY;
1148
1149         rc = lmv_check_connect(obd);
1150         if (rc)
1151                 RETURN(rc);
1152
1153         tgt_exp = lmv_find_export(lmv, fid);
1154         if (IS_ERR(tgt_exp))
1155                 RETURN(PTR_ERR(tgt_exp));
1156
1157         rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1158                          output_size, flags, request);
1159
1160         RETURN(rc);
1161 }
1162
1163 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1164                         struct obd_capa *oc, obd_valid valid, const char *name,
1165                         const char *input, int input_size, int output_size,
1166                         int flags, __u32 suppgid,
1167                         struct ptlrpc_request **request)
1168 {
1169         struct obd_device *obd = exp->exp_obd;
1170         struct lmv_obd *lmv = &obd->u.lmv;
1171         struct obd_export *tgt_exp;
1172         int rc;
1173         ENTRY;
1174
1175         rc = lmv_check_connect(obd);
1176         if (rc)
1177                 RETURN(rc);
1178
1179         tgt_exp = lmv_find_export(lmv, fid);
1180         if (IS_ERR(tgt_exp))
1181                 RETURN(PTR_ERR(tgt_exp));
1182
1183         rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1184                          input, input_size, output_size, flags, suppgid,
1185                          request);
1186
1187         RETURN(rc);
1188 }
1189
1190 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1191                        struct obd_capa *oc, obd_valid valid, int ea_size,
1192                        struct ptlrpc_request **request)
1193 {
1194         struct obd_device *obd = exp->exp_obd;
1195         struct lmv_obd *lmv = &obd->u.lmv;
1196         struct obd_export *tgt_exp;
1197         struct lmv_obj *obj;
1198         int rc, i;
1199         ENTRY;
1200
1201         rc = lmv_check_connect(obd);
1202         if (rc)
1203                 RETURN(rc);
1204
1205         tgt_exp = lmv_find_export(lmv, fid);
1206         if (IS_ERR(tgt_exp))
1207                 RETURN(PTR_ERR(tgt_exp));
1208
1209         rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1210         if (rc)
1211                 RETURN(rc);
1212
1213         obj = lmv_obj_grab(obd, fid);
1214
1215         CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1216                obj ? "(split)" : "");
1217
1218         /*
1219          * If object is split, then we loop over all the slaves and gather size
1220          * attribute. In ideal world we would have to gather also mds field from
1221          * all slaves, as object is spread over the cluster and this is
1222          * definitely interesting information and it is not good to loss it,
1223          * but...
1224          */
1225         if (obj) {
1226                 struct mdt_body *body;
1227
1228                 if (*request == NULL) {
1229                         lmv_obj_put(obj);
1230                         RETURN(rc);
1231                 }
1232
1233                 body = req_capsule_server_get(&(*request)->rq_pill,
1234                                               &RMF_MDT_BODY);
1235                 LASSERT(body != NULL);
1236
1237                 lmv_obj_lock(obj);
1238
1239                 for (i = 0; i < obj->lo_objcount; i++) {
1240                         if (lmv->tgts[i].ltd_exp == NULL) {
1241                                 CWARN("%s: NULL export for %d\n",
1242                                       obd->obd_name, i);
1243                                 continue;
1244                         }
1245
1246                         /* skip master obj. */
1247                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1248                                 continue;
1249
1250                         lmv_update_body(body, &obj->lo_inodes[i]);
1251                 }
1252
1253                 lmv_obj_unlock(obj);
1254                 lmv_obj_put(obj);
1255         }
1256
1257         RETURN(rc);
1258 }
1259
1260 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1261                              ldlm_iterator_t it, void *data)
1262 {
1263         struct obd_device *obd = exp->exp_obd;
1264         struct lmv_obd *lmv = &obd->u.lmv;
1265         int i, rc;
1266         ENTRY;
1267
1268         rc = lmv_check_connect(obd);
1269         if (rc)
1270                 RETURN(rc);
1271
1272         CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1273
1274         /*
1275          * With CMD every object can have two locks in different namespaces:
1276          * lookup lock in space of mds storing direntry and update/open lock in
1277          * space of mds storing inode.
1278          */
1279         for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1280                 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1281
1282         RETURN(0);
1283 }
1284
1285 static int lmv_close(struct obd_export *exp,
1286                      struct md_op_data *op_data,
1287                      struct md_open_data *mod,
1288                      struct ptlrpc_request **request)
1289 {
1290         struct obd_device *obd = exp->exp_obd;
1291         struct lmv_obd *lmv = &obd->u.lmv;
1292         struct obd_export *tgt_exp;
1293         int rc;
1294         ENTRY;
1295
1296         rc = lmv_check_connect(obd);
1297         if (rc)
1298                 RETURN(rc);
1299
1300         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1301         if (IS_ERR(tgt_exp))
1302                 RETURN(PTR_ERR(tgt_exp));
1303
1304         CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1305         rc = md_close(tgt_exp, op_data, mod, request);
1306         RETURN(rc);
1307 }
1308
1309 /*
1310  * Called in the case MDS returns -ERESTART on create on open, what means that
1311  * directory is split and its LMV presentation object has to be updated.
1312  */
1313 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1314 {
1315         struct obd_device *obd = exp->exp_obd;
1316         struct lmv_obd *lmv = &obd->u.lmv;
1317         struct ptlrpc_request *req = NULL;
1318         struct obd_export *tgt_exp;
1319         struct lmv_obj *obj;
1320         struct lustre_md md;
1321         int mealen, rc;
1322         __u64 valid;
1323         ENTRY;
1324
1325         md.mea = NULL;
1326         mealen = lmv_get_easize(lmv);
1327
1328         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1329
1330         tgt_exp = lmv_find_export(lmv, fid);
1331         if (IS_ERR(tgt_exp))
1332                 RETURN(PTR_ERR(tgt_exp));
1333
1334         /* time to update mea of parent fid */
1335         rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1336         if (rc) {
1337                 CERROR("md_getattr() failed, error %d\n", rc);
1338                 GOTO(cleanup, rc);
1339         }
1340
1341         rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1342         if (rc) {
1343                 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1344                 GOTO(cleanup, rc);
1345         }
1346
1347         if (md.mea == NULL)
1348                 GOTO(cleanup, rc = -ENODATA);
1349
1350         obj = lmv_obj_create(exp, fid, md.mea);
1351         if (IS_ERR(obj))
1352                 rc = PTR_ERR(obj);
1353         else
1354                 lmv_obj_put(obj);
1355
1356         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
1357
1358         EXIT;
1359 cleanup:
1360         if (req)
1361                 ptlrpc_req_finished(req);
1362         return rc;
1363 }
1364
1365 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1366                const void *data, int datalen, int mode, __u32 uid,
1367                __u32 gid, __u32 cap_effective,  __u64 rdev,
1368                struct ptlrpc_request **request)
1369 {
1370         struct obd_device *obd = exp->exp_obd;
1371         struct lmv_obd *lmv = &obd->u.lmv;
1372         struct obd_export *tgt_exp;
1373         struct lmv_obj *obj;
1374         int rc, loop = 0;
1375         ENTRY;
1376
1377         rc = lmv_check_connect(obd);
1378         if (rc)
1379                 RETURN(rc);
1380
1381         if (!lmv->desc.ld_active_tgt_count)
1382                 RETURN(-EIO);
1383 repeat:
1384         ++loop;
1385         LASSERT(loop <= 2);
1386         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1387         if (obj) {
1388                 int mea_idx;
1389
1390                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1391                                        op_data->op_name, op_data->op_namelen);
1392                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1393                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1394                 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1395                 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1396                 lmv_obj_put(obj);
1397         } else {
1398                 struct lmv_tgt_desc *tgt;
1399
1400                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1401                 op_data->op_bias |= MDS_CHECK_SPLIT;
1402                 op_data->op_mds = tgt->ltd_idx;
1403                 tgt_exp = tgt->ltd_exp;
1404         }
1405
1406         if (IS_ERR(tgt_exp))
1407                 RETURN(PTR_ERR(tgt_exp));
1408
1409         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1410         if (rc == -ERESTART)
1411                 goto repeat;
1412         else if (rc)
1413                 RETURN(rc);
1414
1415         CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1416                op_data->op_name, PFID(&op_data->op_fid1));
1417
1418         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1419         rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1420                        cap_effective, rdev, request);
1421         if (rc == 0) {
1422                 if (*request == NULL)
1423                         RETURN(rc);
1424                 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1425         } else if (rc == -ERESTART) {
1426                 LASSERT(*request != NULL);
1427                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1428                           "Got -ERESTART during create!\n");
1429                 ptlrpc_req_finished(*request);
1430                 *request = NULL;
1431
1432                 /*
1433                  * Directory got split. Time to update local object and repeat
1434                  * the request with proper MDS.
1435                  */
1436                 rc = lmv_handle_split(exp, &op_data->op_fid1);
1437                 if (rc == 0) {
1438                         rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1439                                                   op_data, &op_data->op_fid2);
1440                         if (rc)
1441                                 RETURN(rc);
1442                         goto repeat;
1443                 }
1444         }
1445         RETURN(rc);
1446 }
1447
1448 static int lmv_done_writing(struct obd_export *exp,
1449                             struct md_op_data *op_data,
1450                             struct md_open_data *mod)
1451 {
1452         struct obd_device *obd = exp->exp_obd;
1453         struct lmv_obd *lmv = &obd->u.lmv;
1454         struct obd_export *tgt_exp;
1455         int rc;
1456         ENTRY;
1457
1458         rc = lmv_check_connect(obd);
1459         if (rc)
1460                 RETURN(rc);
1461
1462         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1463         if (IS_ERR(tgt_exp))
1464                 RETURN(PTR_ERR(tgt_exp));
1465
1466         rc = md_done_writing(tgt_exp, op_data, mod);
1467         RETURN(rc);
1468 }
1469
1470 static int
1471 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1472                    struct lookup_intent *it, struct md_op_data *op_data,
1473                    struct lustre_handle *lockh, void *lmm, int lmmsize)
1474 {
1475         struct obd_device *obd = exp->exp_obd;
1476         struct lmv_obd *lmv = &obd->u.lmv;
1477         struct lmv_stripe_md *mea = op_data->op_mea1;
1478         struct md_op_data *op_data2;
1479         struct obd_export *tgt_exp;
1480         int i, rc = 0;
1481         ENTRY;
1482
1483         OBD_ALLOC_PTR(op_data2);
1484         if (op_data2 == NULL)
1485                 RETURN(-ENOMEM);
1486
1487         LASSERT(mea != NULL);
1488         for (i = 0; i < mea->mea_count; i++) {
1489                 memset(op_data2, 0, sizeof(*op_data2));
1490                 op_data2->op_fid1 = mea->mea_ids[i];
1491                 op_data2->op_bias = 0;
1492
1493                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1494                 if (IS_ERR(tgt_exp))
1495                         GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1496
1497                 if (tgt_exp == NULL)
1498                         continue;
1499
1500                 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1501                                 lockh + i, lmm, lmmsize, 0);
1502
1503                 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1504                        PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1505
1506                 if (rc)
1507                         GOTO(cleanup, rc);
1508
1509                 if (it->d.lustre.it_data) {
1510                         struct ptlrpc_request *req;
1511                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1512                         ptlrpc_req_finished(req);
1513                 }
1514
1515                 if (it->d.lustre.it_status)
1516                         GOTO(cleanup, rc = it->d.lustre.it_status);
1517         }
1518
1519         EXIT;
1520 cleanup:
1521         OBD_FREE_PTR(op_data2);
1522
1523         if (rc != 0) {
1524                 /* drop all taken locks */
1525                 while (--i >= 0) {
1526                         if (lockh[i].cookie)
1527                                 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1528                         lockh[i].cookie = 0;
1529                 }
1530         }
1531         return rc;
1532 }
1533
1534 static int
1535 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1536                    struct lookup_intent *it, struct md_op_data *op_data,
1537                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1538                    int extra_lock_flags)
1539 {
1540         struct ptlrpc_request *req = it->d.lustre.it_data;
1541         struct obd_device *obd = exp->exp_obd;
1542         struct lmv_obd *lmv = &obd->u.lmv;
1543         struct lustre_handle plock;
1544         struct obd_export *tgt_exp;
1545         struct md_op_data *rdata;
1546         struct lu_fid fid_copy;
1547         struct mdt_body *body;
1548         int rc = 0, pmode;
1549         ENTRY;
1550
1551         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1552         LASSERT(body != NULL);
1553
1554         if (!(body->valid & OBD_MD_MDS))
1555                 RETURN(0);
1556
1557         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1558                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1559
1560         /* We got LOOKUP lock, but we really need attrs */
1561         pmode = it->d.lustre.it_lock_mode;
1562         LASSERT(pmode != 0);
1563         memcpy(&plock, lockh, sizeof(plock));
1564         it->d.lustre.it_lock_mode = 0;
1565         it->d.lustre.it_data = NULL;
1566         fid_copy = body->fid1;
1567
1568         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1569         ptlrpc_req_finished(req);
1570
1571         tgt_exp = lmv_find_export(lmv, &fid_copy);
1572         if (IS_ERR(tgt_exp))
1573                 GOTO(out, rc = PTR_ERR(tgt_exp));
1574
1575         OBD_ALLOC_PTR(rdata);
1576         if (rdata == NULL)
1577                 GOTO(out, rc = -ENOMEM);
1578
1579         rdata->op_fid1 = fid_copy;
1580         rdata->op_bias = MDS_CROSS_REF;
1581
1582         rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1583                         lmm, lmmsize, extra_lock_flags);
1584         OBD_FREE_PTR(rdata);
1585         EXIT;
1586 out:
1587         ldlm_lock_decref(&plock, pmode);
1588         return rc;
1589 }
1590
1591 static int
1592 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1593             struct lookup_intent *it, struct md_op_data *op_data,
1594             struct lustre_handle *lockh, void *lmm, int lmmsize,
1595             int extra_lock_flags)
1596 {
1597         struct obd_device *obd = exp->exp_obd;
1598         struct lmv_obd *lmv = &obd->u.lmv;
1599         struct obd_export *tgt_exp = NULL;
1600         struct lmv_obj *obj;
1601         int rc;
1602         ENTRY;
1603
1604         rc = lmv_check_connect(obd);
1605         if (rc)
1606                 RETURN(rc);
1607
1608         if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1609                 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1610                                         lockh, lmm, lmmsize);
1611                 RETURN(rc);
1612         }
1613
1614         if (op_data->op_namelen) {
1615                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1616                 if (obj) {
1617                         int mea_idx;
1618
1619                         /* directory is split. look for right mds for this
1620                          * name */
1621                         mea_idx = raw_name2idx(obj->lo_hashtype,
1622                                                obj->lo_objcount,
1623                                                (char *)op_data->op_name,
1624                                                op_data->op_namelen);
1625                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1626                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1627                         lmv_obj_put(obj);
1628                 }
1629         }
1630
1631         if (tgt_exp == NULL)
1632                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1633         if (IS_ERR(tgt_exp))
1634                 RETURN(PTR_ERR(tgt_exp));
1635
1636         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1637                PFID(&op_data->op_fid1));
1638
1639         rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1640                         lmm, lmmsize, extra_lock_flags);
1641
1642         if (rc == 0 && it->it_op == IT_OPEN)
1643                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1644                                         lmm, lmmsize, extra_lock_flags);
1645         RETURN(rc);
1646 }
1647
1648 static int
1649 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1650                  struct obd_capa *oc, const char *filename, int namelen,
1651                  obd_valid valid, int ea_size, __u32 suppgid,
1652                  struct ptlrpc_request **request)
1653 {
1654         struct obd_device *obd = exp->exp_obd;
1655         struct lmv_obd *lmv = &obd->u.lmv;
1656         struct lu_fid rid = *fid;
1657         struct obd_export *tgt_exp;
1658         struct mdt_body *body;
1659         struct lmv_obj *obj;
1660         int rc, loop = 0;
1661         ENTRY;
1662
1663         rc = lmv_check_connect(obd);
1664         if (rc)
1665                 RETURN(rc);
1666
1667 repeat:
1668         ++loop;
1669         LASSERT(loop <= 2);
1670         obj = lmv_obj_grab(obd, &rid);
1671         if (obj) {
1672                 int mea_idx;
1673
1674                 /* Directory is split. Look for right mds for this name */
1675                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1676                                        filename, namelen - 1);
1677                 rid = obj->lo_inodes[mea_idx].li_fid;
1678                 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1679                 lmv_obj_put(obj);
1680                 valid &= ~OBD_MD_FLCKSPLIT;
1681         } else {
1682                 tgt_exp = lmv_find_export(lmv, &rid);
1683                 valid |= OBD_MD_FLCKSPLIT;
1684         }
1685         if (IS_ERR(tgt_exp))
1686                 RETURN(PTR_ERR(tgt_exp));
1687
1688         CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1689                namelen, filename, PFID(fid), PFID(&rid));
1690
1691         rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1692                              ea_size, suppgid, request);
1693         if (rc == 0) {
1694                 body = req_capsule_server_get(&(*request)->rq_pill,
1695                                               &RMF_MDT_BODY);
1696                 LASSERT(body != NULL);
1697
1698                 if (body->valid & OBD_MD_MDS) {
1699                         struct ptlrpc_request *req = NULL;
1700
1701                         rid = body->fid1;
1702                         CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1703                                PFID(&rid));
1704
1705                         tgt_exp = lmv_find_export(lmv, &rid);
1706                         if (IS_ERR(tgt_exp)) {
1707                                 ptlrpc_req_finished(*request);
1708                                 RETURN(PTR_ERR(tgt_exp));
1709                         }
1710
1711                         rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1712                                              valid | OBD_MD_FLCROSSREF,
1713                                              ea_size, suppgid, &req);
1714                         ptlrpc_req_finished(*request);
1715                         *request = req;
1716                 }
1717         } else if (rc == -ERESTART) {
1718                 LASSERT(*request != NULL);
1719                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1720                           "Got -ERESTART during getattr!\n");
1721                 ptlrpc_req_finished(*request);
1722                 *request = NULL;
1723
1724                 /*
1725                  * Directory got split. Time to update local object and repeat
1726                  * the request with proper MDS.
1727                  */
1728                 rc = lmv_handle_split(exp, &rid);
1729                 if (rc == 0)
1730                         goto repeat;
1731         }
1732         RETURN(rc);
1733 }
1734
1735 #define md_op_data_fid(op_data, fl)                     \
1736         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1737          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1738          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1739          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1740          NULL)
1741
1742 /* @tgt_exp is the export the metadata request is sent.
1743  * @fid_exp is the export the cancel should be sent for the current fid.
1744  * if @fid_exp is NULL, the export is found for the current fid.
1745  * @op_data keeps the current fid, which is pointed through @flag.
1746  * @mode, @bits -- lock match parameters. */
1747 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1748                             struct obd_export *fid_exp,
1749                             struct md_op_data *op_data,
1750                             ldlm_mode_t mode, int bits, int flag)
1751 {
1752         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1753         ldlm_policy_data_t policy = {{0}};
1754         int rc = 0;
1755         ENTRY;
1756
1757         if (!fid_is_sane(fid))
1758                 RETURN(0);
1759         
1760         if (fid_exp == NULL)
1761                 fid_exp = lmv_find_export(lmv, fid);
1762
1763         if (tgt_exp == fid_exp) {
1764                 /* The export is the same as on the target server, cancel 
1765                  * will be sent along with the main metadata operation. */
1766                 op_data->op_flags |= flag;
1767                 RETURN(0);
1768         }
1769
1770         policy.l_inodebits.bits = bits;
1771         rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1772         RETURN(rc);
1773 }
1774
1775 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1776 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1777  * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1778  * md_cancel_unused for child export(s). */
1779 static int lmv_early_cancel_stripes(struct obd_export *exp,
1780                                     struct obd_export *tgt_exp,
1781                                     struct md_op_data *op_data,
1782                                     ldlm_mode_t mode, int bits, int flag)
1783 {
1784         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1785         struct obd_device *obd = exp->exp_obd;
1786         struct lmv_obd *lmv = &obd->u.lmv;
1787         struct obd_export *st_exp;
1788         struct lmv_obj *obj;
1789         int rc = 0;
1790         ENTRY;
1791
1792         if (!fid_is_sane(fid))
1793                 RETURN(0);
1794
1795         obj = lmv_obj_grab(obd, fid);
1796         if (obj) {
1797                 ldlm_policy_data_t policy = {{0}};
1798                 struct lu_fid *st_fid;
1799                 int i;
1800                 
1801                 policy.l_inodebits.bits = bits;
1802                 for (i = 0; i < obj->lo_objcount; i++) {
1803                         st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1804                         st_fid = &obj->lo_inodes[i].li_fid;
1805                         if (tgt_exp != st_exp) {
1806                                 rc = md_cancel_unused(st_exp, st_fid, &policy,
1807                                                       mode, LDLM_FL_ASYNC,
1808                                                       NULL);
1809                                 if (rc)
1810                                         break;
1811                         } else {
1812                                 /* Some export matches to @tgt_exp, do cancel
1813                                  * for its fid in mdc */
1814                                 *fid = *st_fid;
1815                                 op_data->op_flags |= flag;
1816                         }
1817                 }
1818                 lmv_obj_put(obj);
1819         } else {
1820                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1821                                       mode, bits, flag);
1822         }
1823         RETURN(rc);
1824 }
1825 #endif
1826
1827 /*
1828  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1829  * op_data->op_fid2
1830  */
1831 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1832                     struct ptlrpc_request **request)
1833 {
1834         struct obd_device *obd = exp->exp_obd;
1835         struct lmv_obd *lmv = &obd->u.lmv;
1836         struct obd_export *tgt_exp;
1837         struct lmv_obj *obj;
1838         int rc, loop = 0;
1839         mdsno_t mds;
1840         ENTRY;
1841
1842         rc = lmv_check_connect(obd);
1843         if (rc)
1844                 RETURN(rc);
1845
1846 repeat:
1847         ++loop;
1848         LASSERT(loop <= 2);
1849         if (op_data->op_namelen != 0) {
1850                 int mea_idx;
1851
1852                 /* Usual link request */
1853                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1854                 if (obj) {
1855                         mea_idx = raw_name2idx(obj->lo_hashtype,
1856                                                obj->lo_objcount,
1857                                                op_data->op_name,
1858                                                op_data->op_namelen);
1859                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1860                         mds = obj->lo_inodes[mea_idx].li_mds;
1861                         lmv_obj_put(obj);
1862                 } else {
1863                         rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1864                         if (rc)
1865                                 RETURN(rc);
1866                 }
1867
1868                 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1869                        PFID(&op_data->op_fid2), op_data->op_namelen,
1870                        op_data->op_name, PFID(&op_data->op_fid1));
1871         } else {
1872                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1873                 if (rc)
1874                         RETURN(rc);
1875
1876                 /* request from MDS to acquire i_links for inode by fid1 */
1877                 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1878                        PFID(&op_data->op_fid1));
1879         }
1880
1881         CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1882                mds, PFID(&op_data->op_fid1));
1883
1884         op_data->op_fsuid = current->fsuid;
1885         op_data->op_fsgid = current->fsgid;
1886         op_data->op_cap   = current->cap_effective;
1887
1888         tgt_exp = lmv->tgts[mds].ltd_exp;
1889         if (op_data->op_namelen) {
1890                 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1891                 /* Cancel UPDATE lock on child (fid1). */
1892                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1893                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1894         }
1895         if (rc == 0)
1896                 rc = md_link(tgt_exp, op_data, request);
1897         if (rc == -ERESTART) {
1898                 LASSERT(*request != NULL);
1899                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1900                           "Got -ERESTART during link!\n");
1901                 ptlrpc_req_finished(*request);
1902                 *request = NULL;
1903
1904                 /*
1905                  * Directory got split. Time to update local object and repeat
1906                  * the request with proper MDS.
1907                  */
1908                 rc = lmv_handle_split(exp, &op_data->op_fid2);
1909                 if (rc == 0)
1910                         goto repeat;
1911         }
1912
1913         RETURN(rc);
1914 }
1915
1916 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1917                       const char *old, int oldlen, const char *new, int newlen,
1918                       struct ptlrpc_request **request)
1919 {
1920         struct obd_export *tgt_exp = NULL, *src_exp;
1921         struct obd_device *obd = exp->exp_obd;
1922         struct lmv_obd *lmv = &obd->u.lmv;
1923         int rc, mea_idx, loop = 0;
1924         struct lmv_obj *obj;
1925         mdsno_t mds1, mds2;
1926         ENTRY;
1927
1928         CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1929                oldlen, old, PFID(&op_data->op_fid1),
1930                newlen, new, PFID(&op_data->op_fid2));
1931
1932         rc = lmv_check_connect(obd);
1933         if (rc)
1934                 RETURN(rc);
1935
1936         if (oldlen == 0) {
1937                 /*
1938                  * MDS with old dir entry is asking another MDS to create name
1939                  * there.
1940                  */
1941                 CDEBUG(D_OTHER,
1942                        "create %*s(%d/%d) in "DFID" pointing "
1943                        "to "DFID"\n", newlen, new, oldlen, newlen,
1944                        PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1945
1946                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1947                 if (rc)
1948                         RETURN(rc);
1949
1950                 /*
1951                  * Target directory can be split, sowe should forward request to
1952                  * the right MDS.
1953                  */
1954                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1955                 if (obj) {
1956                         mea_idx = raw_name2idx(obj->lo_hashtype,
1957                                                obj->lo_objcount,
1958                                                (char *)new, newlen);
1959                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1960                         CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1961                                PFID(&op_data->op_fid2));
1962                         lmv_obj_put(obj);
1963                 }
1964                 goto request;
1965         }
1966
1967 repeat:
1968         ++loop;
1969         LASSERT(loop <= 2);
1970         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1971         if (obj) {
1972                 /*
1973                  * directory is already split, so we have to forward request to
1974                  * the right MDS.
1975                  */
1976                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1977                                        (char *)old, oldlen);
1978                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1979                 mds1 = obj->lo_inodes[mea_idx].li_mds;
1980                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1981                 lmv_obj_put(obj);
1982         } else {
1983                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1984                 if (rc)
1985                         RETURN(rc);
1986         }
1987
1988         obj = lmv_obj_grab(obd, &op_data->op_fid2);
1989         if (obj) {
1990                 /*
1991                  * Directory is already split, so we have to forward request to
1992                  * the right MDS.
1993                  */
1994                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1995                                        (char *)new, newlen);
1996
1997                 mds2 = obj->lo_inodes[mea_idx].li_mds;
1998                 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1999                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2000                 lmv_obj_put(obj);
2001         } else {
2002                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2003                 if (rc)
2004                         RETURN(rc);
2005         }
2006
2007 request:
2008         op_data->op_fsuid = current->fsuid;
2009         op_data->op_fsgid = current->fsgid;
2010         op_data->op_cap   = current->cap_effective;
2011
2012         src_exp = lmv_get_export(lmv, mds1);
2013         tgt_exp = lmv_get_export(lmv, mds2);
2014         if (oldlen) {
2015                 /* LOOKUP lock on src child (fid3) should also be cancelled for
2016                  * src_exp in mdc_rename. */
2017                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2018
2019                 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2020                  * own export. */
2021                 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2022                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2023
2024                 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2025                 if (rc == 0)
2026                         rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2027                                               LCK_EX, MDS_INODELOCK_LOOKUP,
2028                                               MF_MDC_CANCEL_FID4);
2029
2030                 /* XXX: the case when child is a striped dir is not supported.
2031                  * Only the master stripe has all locks cancelled early. */
2032                 /* Cancel all the locks on tgt child (fid4). */
2033                 if (rc == 0)
2034                         rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2035                                               LCK_EX, MDS_INODELOCK_FULL,
2036                                               MF_MDC_CANCEL_FID4);
2037         }
2038
2039         if (rc == 0)
2040                 rc = md_rename(src_exp, op_data, old, oldlen,
2041                                new, newlen, request);
2042         if (rc == -ERESTART) {
2043                 LASSERT(*request != NULL);
2044                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2045                           "Got -ERESTART during rename!\n");
2046                 ptlrpc_req_finished(*request);
2047                 *request = NULL;
2048
2049                 /*
2050                  * Directory got split. Time to update local object and repeat
2051                  * the request with proper MDS.
2052                  */
2053                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2054                 if (rc == 0)
2055                         goto repeat;
2056         }
2057         RETURN(rc);
2058 }
2059
2060 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2061                        void *ea, int ealen, void *ea2, int ea2len,
2062                        struct ptlrpc_request **request,
2063                        struct md_open_data **mod)
2064 {
2065         struct obd_device *obd = exp->exp_obd;
2066         struct lmv_obd *lmv = &obd->u.lmv;
2067         struct ptlrpc_request *req;
2068         struct obd_export *tgt_exp;
2069         struct lmv_obj *obj;
2070         int rc = 0, i;
2071         ENTRY;
2072
2073         rc = lmv_check_connect(obd);
2074         if (rc)
2075                 RETURN(rc);
2076
2077         obj = lmv_obj_grab(obd, &op_data->op_fid1);
2078
2079         CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2080                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2081                obj ? ", split" : "");
2082
2083         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2084         if (obj) {
2085                 for (i = 0; i < obj->lo_objcount; i++) {
2086                         op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2087
2088                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2089                         if (IS_ERR(tgt_exp)) {
2090                                 rc = PTR_ERR(tgt_exp);
2091                                 break;
2092                         }
2093
2094                         rc = md_setattr(tgt_exp, op_data, ea, ealen,
2095                                         ea2, ea2len, &req, mod);
2096
2097                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2098                                 /*
2099                                  * this is master object and this request should
2100                                  * be returned back to llite.
2101                                  */
2102                                 *request = req;
2103                         } else {
2104                                 ptlrpc_req_finished(req);
2105                         }
2106
2107                         if (rc)
2108                                 break;
2109                 }
2110                 lmv_obj_put(obj);
2111         } else {
2112                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2113                 if (IS_ERR(tgt_exp))
2114                         RETURN(PTR_ERR(tgt_exp));
2115
2116                 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2117                                 ea2len, request, mod);
2118         }
2119         RETURN(rc);
2120 }
2121
2122 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2123                     struct obd_capa *oc, struct ptlrpc_request **request)
2124 {
2125         struct obd_device *obd = exp->exp_obd;
2126         struct lmv_obd *lmv = &obd->u.lmv;
2127         struct obd_export *tgt_exp;
2128         int rc;
2129         ENTRY;
2130
2131         rc = lmv_check_connect(obd);
2132         if (rc)
2133                 RETURN(rc);
2134
2135         tgt_exp = lmv_find_export(lmv, fid);
2136         if (IS_ERR(tgt_exp))
2137                 RETURN(PTR_ERR(tgt_exp));
2138
2139         rc = md_sync(tgt_exp, fid, oc, request);
2140         RETURN(rc);
2141 }
2142
2143 /* main purpose of LMV blocking ast is to remove split directory LMV
2144  * presentation object (struct lmv_obj) attached to the lock being revoked. */
2145 int lmv_blocking_ast(struct ldlm_lock *lock,
2146                      struct ldlm_lock_desc *desc,
2147                      void *data, int flag)
2148 {
2149         struct lustre_handle lockh;
2150         struct lmv_obj *obj;
2151         int rc;
2152         ENTRY;
2153
2154         switch (flag) {
2155         case LDLM_CB_BLOCKING:
2156                 ldlm_lock2handle(lock, &lockh);
2157                 rc = ldlm_cli_cancel(&lockh);
2158                 if (rc < 0) {
2159                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2160                         RETURN(rc);
2161                 }
2162                 break;
2163         case LDLM_CB_CANCELING:
2164                 /* time to drop cached attrs for dirobj */
2165                 obj = lock->l_ast_data;
2166                 if (obj) {
2167                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2168                                ", master "DFID"\n",
2169                                lock->l_resource->lr_name.name[3] == 1 ?
2170                                "LOOKUP" : "UPDATE",
2171                                lock->l_resource->lr_name.name[0],
2172                                lock->l_resource->lr_name.name[1],
2173                                PFID(&obj->lo_fid));
2174                         lmv_obj_put(obj);
2175                 }
2176                 break;
2177         default:
2178                 LBUG();
2179         }
2180         RETURN(0);
2181 }
2182
2183 static void lmv_hash_adjust(__u32 *hash, __u32 hash_adj)
2184 {
2185         __u32 val;
2186
2187         val = le32_to_cpu(*hash);
2188         if (val < hash_adj)
2189                 val += MAX_HASH_SIZE;
2190         if (val != DIR_END_OFF)
2191                 *hash = cpu_to_le32(val - hash_adj);
2192 }
2193
2194 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2195 {
2196         __u64 id;
2197         struct obd_import *imp;
2198
2199         /*
2200          * XXX Hack: to get nid we assume that underlying obd device is mdc.
2201          */
2202         imp  = class_exp2cliimp(exp);
2203         id   = imp->imp_connection->c_self + fid_flatten(fid);
2204
2205         CDEBUG(D_INFO, "node rank: %llx "DFID" %llx %llx\n",
2206                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2207
2208         return id ^ (id >> 32);
2209 }
2210
2211 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2212                         struct obd_capa *oc, __u64 offset64, struct page *page,
2213                         struct ptlrpc_request **request)
2214 {
2215         struct obd_device *obd = exp->exp_obd;
2216         struct lmv_obd *lmv = &obd->u.lmv;
2217         struct obd_export *tgt_exp;
2218         struct lu_fid rid = *fid;
2219         struct lmv_obj *obj;
2220         __u32 offset0;
2221         __u32 offset;
2222         __u32 hash_adj = 0;
2223         __u32 rank = 0;
2224         __u32 seg_size = 0;
2225         int tgt = 0;
2226         int tgt0 = 0;
2227         int rc;
2228         int nr = 0;
2229         ENTRY;
2230
2231         offset0 = offset = offset64;
2232         /*
2233          * Check that offset is representable by 32bit number.
2234          */
2235         LASSERT((__u64)offset == offset64);
2236
2237         rc = lmv_check_connect(obd);
2238         if (rc)
2239                 RETURN(rc);
2240
2241         CDEBUG(D_INFO, "READPAGE at %x from "DFID"\n", offset, PFID(&rid));
2242
2243         obj = lmv_obj_grab(obd, fid);
2244         if (obj) {
2245                 struct lmv_inode *loi;
2246
2247                 lmv_obj_lock(obj);
2248
2249                 nr       = obj->lo_objcount;
2250                 LASSERT(nr > 0);
2251                 seg_size = MAX_HASH_SIZE / nr;
2252                 loi      = obj->lo_inodes;
2253                 rank     = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2254                                          fid) % nr;
2255                 tgt0     = (offset / seg_size) % nr;
2256                 tgt      = (tgt0 + rank) % nr;
2257
2258                 if (tgt < tgt0)
2259                         /*
2260                          * Wrap around.
2261                          *
2262                          * Last segment has unusual length due to division
2263                          * rounding.
2264                          */
2265                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2266                 else
2267                         hash_adj = 0;
2268
2269                 hash_adj += rank * seg_size;
2270
2271                 CDEBUG(D_INFO, "hash_adj: %x %x %x/%x -> %x/%x\n",
2272                        rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2273
2274                 offset = (offset + hash_adj) % MAX_HASH_SIZE;
2275                 rid = obj->lo_inodes[tgt].li_fid;
2276                 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2277
2278                 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2279                        PFID(&rid), (unsigned long)offset, tgt);
2280         } else
2281                 tgt_exp = lmv_find_export(lmv, &rid);
2282
2283         if (IS_ERR(tgt_exp))
2284                 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2285
2286         rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2287         if (rc)
2288                 GOTO(cleanup, rc);
2289         if (obj) {
2290                 struct lu_dirpage *dp;
2291                 struct lu_dirent  *ent;
2292
2293                 dp = cfs_kmap(page);
2294
2295                 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2296                 lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2297                 LASSERT(cpu_to_le32(dp->ldp_hash_start) <= offset0);
2298
2299                 for (ent = lu_dirent_start(dp); ent != NULL;
2300                      ent = lu_dirent_next(ent))
2301                         lmv_hash_adjust(&ent->lde_hash, hash_adj);
2302
2303                 if (tgt0 != nr - 1) {
2304                         __u32 end;
2305
2306                         end = le32_to_cpu(dp->ldp_hash_end);
2307                         if (end == DIR_END_OFF) {
2308                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2309                                                                (tgt0 + 1));
2310                                 CDEBUG(D_INFO, ""DFID" reset end %x tgt %d\n",
2311                                        PFID(&rid),
2312                                        le32_to_cpu(dp->ldp_hash_end), tgt);
2313                         }
2314                 }
2315                 cfs_kunmap(page);
2316         }
2317         /*
2318          * Here we could remove "." and ".." from all pages which at not from
2319          * master. But MDS has only "." and ".." for master dir.
2320          */
2321         EXIT;
2322 cleanup:
2323         if (obj) {
2324                 lmv_obj_unlock(obj);
2325                 lmv_obj_put(obj);
2326         }
2327         return rc;
2328 }
2329
2330 static int lmv_unlink_slaves(struct obd_export *exp,
2331                              struct md_op_data *op_data,
2332                              struct ptlrpc_request **req)
2333 {
2334         struct obd_device *obd = exp->exp_obd;
2335         struct lmv_obd *lmv = &obd->u.lmv;
2336         struct lmv_stripe_md *mea = op_data->op_mea1;
2337         struct md_op_data *op_data2;
2338         struct obd_export *tgt_exp;
2339         int i, rc = 0;
2340         ENTRY;
2341
2342         OBD_ALLOC_PTR(op_data2);
2343         if (op_data2 == NULL)
2344                 RETURN(-ENOMEM);
2345
2346         op_data2->op_mode = S_IFDIR;
2347         op_data2->op_fsuid = current->fsuid;
2348         op_data2->op_fsgid = current->fsgid;
2349         op_data2->op_bias = 0;
2350
2351         LASSERT(mea != NULL);
2352         for (i = 0; i < mea->mea_count; i++) {
2353                 memset(op_data2, 0, sizeof(*op_data2));
2354                 op_data2->op_fid1 = mea->mea_ids[i];
2355                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2356                 if (IS_ERR(tgt_exp))
2357                         GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2358
2359                 if (tgt_exp == NULL)
2360                         continue;
2361
2362                 rc = md_unlink(tgt_exp, op_data2, req);
2363
2364                 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2365                        PFID(&mea->mea_ids[i]), rc);
2366
2367                 if (*req) {
2368                         ptlrpc_req_finished(*req);
2369                         *req = NULL;
2370                 }
2371                 if (rc)
2372                         GOTO(out_free_op_data2, rc);
2373         }
2374
2375         EXIT;
2376 out_free_op_data2:
2377         OBD_FREE_PTR(op_data2);
2378         return rc;
2379 }
2380
2381 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2382                       struct ptlrpc_request **request)
2383 {
2384         struct obd_device *obd = exp->exp_obd;
2385         struct lmv_obd *lmv = &obd->u.lmv;
2386         struct obd_export *tgt_exp = NULL;
2387         struct lmv_obj *obj;
2388         int rc, loop = 0;
2389         ENTRY;
2390
2391         rc = lmv_check_connect(obd);
2392         if (rc)
2393                 RETURN(rc);
2394
2395         if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2396                 /* mds asks to remove slave objects */
2397                 rc = lmv_unlink_slaves(exp, op_data, request);
2398                 RETURN(rc);
2399         }
2400
2401 repeat:
2402         ++loop;
2403         LASSERT(loop <= 2);
2404         if (op_data->op_namelen != 0) {
2405                 int mea_idx;
2406
2407                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2408                 if (obj) {
2409                         mea_idx = raw_name2idx(obj->lo_hashtype,
2410                                                obj->lo_objcount,
2411                                                op_data->op_name,
2412                                                op_data->op_namelen);
2413                         op_data->op_bias &= ~MDS_CHECK_SPLIT;
2414                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2415                         tgt_exp = lmv_get_export(lmv,
2416                                                  obj->lo_inodes[mea_idx].li_mds);
2417                         lmv_obj_put(obj);
2418                         CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2419                                op_data->op_namelen, op_data->op_name,
2420                                PFID(&op_data->op_fid1), mea_idx);
2421                 }
2422         } else {
2423                 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2424                        PFID(&op_data->op_fid1));
2425         }
2426         if (tgt_exp == NULL) {
2427                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2428                 if (IS_ERR(tgt_exp))
2429                         RETURN(PTR_ERR(tgt_exp));
2430                 op_data->op_bias |= MDS_CHECK_SPLIT;
2431         }
2432
2433         op_data->op_fsuid = current->fsuid;
2434         op_data->op_fsgid = current->fsgid;
2435         op_data->op_cap   = current->cap_effective;
2436
2437         /* If child's fid is given, cancel unused locks for it if it is from
2438          * another export than parent. */
2439         if (op_data->op_namelen) {
2440                 /* LOOKUP lock for child (fid3) should also be cancelled on 
2441                  * parent tgt_exp in mdc_unlink(). */
2442                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2443
2444                 /* XXX: the case when child is a striped dir is not supported.
2445                  * Only the master stripe has all locks cancelled early. */
2446                 /* Cancel FULL locks on child (fid3). */
2447                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2448                                       MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2449         }
2450         if (rc == 0)
2451                 rc = md_unlink(tgt_exp, op_data, request);
2452         if (rc == -ERESTART) {
2453                 LASSERT(*request != NULL);
2454                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2455                           "Got -ERESTART during unlink!\n");
2456                 ptlrpc_req_finished(*request);
2457                 *request = NULL;
2458
2459                 /*
2460                  * Directory got split. Time to update local object and repeat
2461                  * the request with proper MDS.
2462                  */
2463                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2464                 if (rc == 0)
2465                         goto repeat;
2466         }
2467         RETURN(rc);
2468 }
2469
2470 static int lmv_llog_init(struct obd_device *obd, struct obd_llogs* llogs,
2471                          struct obd_device *tgt, int count,
2472                          struct llog_catid *logid, struct obd_uuid *uuid)
2473 {
2474         struct llog_ctxt *ctxt;
2475         int rc;
2476         ENTRY;
2477
2478         rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2479                         &llog_client_ops);
2480         if (rc == 0) {
2481                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2482                 ctxt->loc_imp = tgt->u.cli.cl_import;
2483         }
2484
2485         RETURN(rc);
2486 }
2487
2488 static int lmv_llog_finish(struct obd_device *obd, int count)
2489 {
2490         int rc;
2491         ENTRY;
2492
2493         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
2494         RETURN(rc);
2495 }
2496
2497 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2498 {
2499         int rc = 0;
2500
2501         switch (stage) {
2502         case OBD_CLEANUP_EARLY:
2503                 /* XXX: here should be calling obd_precleanup() down to
2504                  * stack. */
2505                 break;
2506         case OBD_CLEANUP_SELF_EXP:
2507                 rc = obd_llog_finish(obd, 0);
2508                 if (rc != 0)
2509                         CERROR("failed to cleanup llogging subsystems\n");
2510                 break;
2511         default:
2512                 break;
2513         }
2514         RETURN(rc);
2515 }
2516
2517 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2518                         void *key, __u32 *vallen, void *val)
2519 {
2520         struct obd_device *obd;
2521         struct lmv_obd *lmv;
2522         int rc = 0;
2523         ENTRY;
2524
2525         obd = class_exp2obd(exp);
2526         if (obd == NULL) {
2527                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2528                        exp->exp_handle.h_cookie);
2529                 RETURN(-EINVAL);
2530         }
2531
2532         lmv = &obd->u.lmv;
2533         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2534                 struct lmv_tgt_desc *tgts;
2535                 int i;
2536
2537                 rc = lmv_check_connect(obd);
2538                 if (rc)
2539                         RETURN(rc);
2540
2541                 LASSERT(*vallen == sizeof(__u32));
2542                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2543                      i++, tgts++) {
2544
2545                         /* all tgts should be connected when this get called. */
2546                         if (!tgts || !tgts->ltd_exp) {
2547                                 CERROR("target not setup?\n");
2548                                 continue;
2549                         }
2550
2551                         if (!obd_get_info(tgts->ltd_exp, keylen, key,
2552                                           vallen, val))
2553                                 RETURN(0);
2554                 }
2555                 RETURN(-EINVAL);
2556         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2557                 rc = lmv_check_connect(obd);
2558                 if (rc)
2559                         RETURN(rc);
2560
2561                 /* forwarding this request to first MDS, it should know LOV
2562                  * desc. */
2563                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2564                                   vallen, val);
2565                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2566                         exp->exp_connect_flags =
2567                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2568                 }
2569                 RETURN(rc);
2570         }
2571
2572         CDEBUG(D_IOCTL, "invalid key\n");
2573         RETURN(-EINVAL);
2574 }
2575
2576 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2577                        void *key, obd_count vallen, void *val,
2578                        struct ptlrpc_request_set *set)
2579 {
2580         struct lmv_tgt_desc    *tgt;
2581         struct obd_device      *obd;
2582         struct lmv_obd         *lmv;
2583         int rc = 0;
2584         ENTRY;
2585
2586         obd = class_exp2obd(exp);
2587         if (obd == NULL) {
2588                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2589                        exp->exp_handle.h_cookie);
2590                 RETURN(-EINVAL);
2591         }
2592         lmv = &obd->u.lmv;
2593
2594         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2595             KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2596                 int i, err = 0;
2597
2598                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2599                         tgt = &lmv->tgts[i];
2600
2601                         if (!tgt->ltd_exp)
2602                                 continue;
2603
2604                         err = obd_set_info_async(tgt->ltd_exp,
2605                                                  keylen, key, vallen, val, set);
2606                         if (err && rc == 0)
2607                                 rc = err;
2608                 }
2609
2610                 RETURN(rc);
2611         }
2612
2613         RETURN(-EINVAL);
2614 }
2615
2616 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2617                struct lov_stripe_md *lsm)
2618 {
2619         struct obd_device *obd = class_exp2obd(exp);
2620         struct lmv_obd *lmv = &obd->u.lmv;
2621         struct lmv_stripe_md *meap, *lsmp;
2622         int mea_size, i;
2623         ENTRY;
2624
2625         mea_size = lmv_get_easize(lmv);
2626         if (!lmmp)
2627                 RETURN(mea_size);
2628
2629         if (*lmmp && !lsm) {
2630                 OBD_FREE(*lmmp, mea_size);
2631                 *lmmp = NULL;
2632                 RETURN(0);
2633         }
2634
2635         if (*lmmp == NULL) {
2636                 OBD_ALLOC(*lmmp, mea_size);
2637                 if (*lmmp == NULL)
2638                         RETURN(-ENOMEM);
2639         }
2640
2641         if (!lsm)
2642                 RETURN(mea_size);
2643
2644         lsmp = (struct lmv_stripe_md *)lsm;
2645         meap = (struct lmv_stripe_md *)*lmmp;
2646
2647         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2648             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2649                 RETURN(-EINVAL);
2650
2651         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2652         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2653         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2654
2655         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2656                 meap->mea_ids[i] = meap->mea_ids[i];
2657                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2658         }
2659
2660         RETURN(mea_size);
2661 }
2662
2663 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2664                  struct lov_mds_md *lmm, int lmm_size)
2665 {
2666         struct obd_device *obd = class_exp2obd(exp);
2667         struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2668         struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2669         struct lmv_obd *lmv = &obd->u.lmv;
2670         int mea_size, i;
2671         __u32 magic;
2672         ENTRY;
2673
2674         mea_size = lmv_get_easize(lmv);
2675         if (lsmp == NULL)
2676                 return mea_size;
2677
2678         if (*lsmp != NULL && lmm == NULL) {
2679                 OBD_FREE(*tmea, mea_size);
2680                 *lsmp = NULL;
2681                 RETURN(0);
2682         }
2683
2684         LASSERT(mea_size == lmm_size);
2685
2686         OBD_ALLOC(*tmea, mea_size);
2687         if (*tmea == NULL)
2688                 RETURN(-ENOMEM);
2689
2690         if (!lmm)
2691                 RETURN(mea_size);
2692
2693         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2694             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2695             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2696         {
2697                 magic = le32_to_cpu(mea->mea_magic);
2698         } else {
2699                 /* old mea is not handled here */
2700                 LBUG();
2701         }
2702
2703         (*tmea)->mea_magic = magic;
2704         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2705         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2706
2707         for (i = 0; i < (*tmea)->mea_count; i++) {
2708                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2709                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2710         }
2711         RETURN(mea_size);
2712 }
2713
2714 static int lmv_cancel_unused(struct obd_export *exp,
2715                              const struct lu_fid *fid,
2716                              ldlm_policy_data_t *policy,
2717                              ldlm_mode_t mode, int flags, void *opaque)
2718 {
2719         struct obd_device *obd = exp->exp_obd;
2720         struct lmv_obd *lmv = &obd->u.lmv;
2721         int rc = 0, err, i;
2722         ENTRY;
2723
2724         LASSERT(fid != NULL);
2725
2726         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2727                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2728                         continue;
2729
2730                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2731                                        policy, mode, flags, opaque);
2732                 if (!rc)
2733                         rc = err;
2734         }
2735         RETURN(rc);
2736 }
2737
2738 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2739 {
2740         struct obd_device *obd = exp->exp_obd;
2741         struct lmv_obd *lmv = &obd->u.lmv;
2742
2743         ENTRY;
2744         RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2745 }
2746
2747 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2748                            const struct lu_fid *fid, ldlm_type_t type,
2749                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2750                            struct lustre_handle *lockh)
2751 {
2752         struct obd_device *obd = exp->exp_obd;
2753         struct lmv_obd *lmv = &obd->u.lmv;
2754         ldlm_mode_t rc;
2755         int i;
2756         ENTRY;
2757
2758         CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2759
2760         /* with CMD every object can have two locks in different namespaces:
2761          * lookup lock in space of mds storing direntry and update/open lock in
2762          * space of mds storing inode. Thus we check all targets, not only that
2763          * one fid was created in. */
2764         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2765                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2766                                    type, policy, mode, lockh);
2767                 if (rc)
2768                         RETURN(rc);
2769         }
2770
2771         RETURN(0);
2772 }
2773
2774 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2775                       struct obd_export *dt_exp, struct obd_export *md_exp,
2776                       struct lustre_md *md)
2777 {
2778         struct obd_device *obd = exp->exp_obd;
2779         struct lmv_obd *lmv = &obd->u.lmv;
2780         int rc;
2781
2782         ENTRY;
2783         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2784         RETURN(rc);
2785 }
2786
2787 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2788 {
2789         struct obd_device *obd = exp->exp_obd;
2790         struct lmv_obd *lmv = &obd->u.lmv;
2791
2792         ENTRY;
2793         if (md->mea)
2794                 obd_free_memmd(exp, (struct lov_stripe_md**)&md->mea);
2795         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2796 }
2797
2798 int lmv_set_open_replay_data(struct obd_export *exp,
2799                              struct obd_client_handle *och,
2800                              struct ptlrpc_request *open_req)
2801 {
2802         struct obd_device *obd = exp->exp_obd;
2803         struct lmv_obd *lmv = &obd->u.lmv;
2804         struct obd_export *tgt_exp;
2805
2806         ENTRY;
2807
2808         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2809         if (IS_ERR(tgt_exp))
2810                 RETURN(PTR_ERR(tgt_exp));
2811
2812         RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2813 }
2814
2815 int lmv_clear_open_replay_data(struct obd_export *exp,
2816                                struct obd_client_handle *och)
2817 {
2818         struct obd_device *obd = exp->exp_obd;
2819         struct lmv_obd *lmv = &obd->u.lmv;
2820         struct obd_export *tgt_exp;
2821         ENTRY;
2822
2823         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2824         if (IS_ERR(tgt_exp))
2825                 RETURN(PTR_ERR(tgt_exp));
2826
2827         RETURN(md_clear_open_replay_data(tgt_exp, och));
2828 }
2829
2830 static int lmv_get_remote_perm(struct obd_export *exp,
2831                                const struct lu_fid *fid,
2832                                struct obd_capa *oc, __u32 suppgid,
2833                                struct ptlrpc_request **request)
2834 {
2835         struct obd_device *obd = exp->exp_obd;
2836         struct lmv_obd *lmv = &obd->u.lmv;
2837         struct obd_export *tgt_exp;
2838         int rc;
2839
2840         ENTRY;
2841
2842         rc = lmv_check_connect(obd);
2843         if (rc)
2844                 RETURN(rc);
2845
2846         tgt_exp = lmv_find_export(lmv, fid);
2847         if (IS_ERR(tgt_exp))
2848                 RETURN(PTR_ERR(tgt_exp));
2849
2850         rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2851
2852         RETURN(rc);
2853 }
2854
2855 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2856                           renew_capa_cb_t cb)
2857 {
2858         struct obd_device *obd = exp->exp_obd;
2859         struct lmv_obd *lmv = &obd->u.lmv;
2860         struct obd_export *tgt_exp;
2861         int rc;
2862         ENTRY;
2863
2864         rc = lmv_check_connect(obd);
2865         if (rc)
2866                 RETURN(rc);
2867
2868         tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2869         if (IS_ERR(tgt_exp))
2870                 RETURN(PTR_ERR(tgt_exp));
2871
2872         rc = md_renew_capa(tgt_exp, oc, cb);
2873         RETURN(rc);
2874 }
2875
2876 struct obd_ops lmv_obd_ops = {
2877         .o_owner                = THIS_MODULE,
2878         .o_setup                = lmv_setup,
2879         .o_cleanup              = lmv_cleanup,
2880         .o_precleanup           = lmv_precleanup,
2881         .o_process_config       = lmv_process_config,
2882         .o_connect              = lmv_connect,
2883         .o_disconnect           = lmv_disconnect,
2884         .o_statfs               = lmv_statfs,
2885         .o_llog_init            = lmv_llog_init,
2886         .o_llog_finish          = lmv_llog_finish,
2887         .o_get_info             = lmv_get_info,
2888         .o_set_info_async       = lmv_set_info_async,
2889         .o_packmd               = lmv_packmd,
2890         .o_unpackmd             = lmv_unpackmd,
2891         .o_notify               = lmv_notify,
2892         .o_iocontrol            = lmv_iocontrol,
2893         .o_fid_delete           = lmv_fid_delete
2894 };
2895
2896 struct md_ops lmv_md_ops = {
2897         .m_getstatus            = lmv_getstatus,
2898         .m_change_cbdata        = lmv_change_cbdata,
2899         .m_close                = lmv_close,
2900         .m_create               = lmv_create,
2901         .m_done_writing         = lmv_done_writing,
2902         .m_enqueue              = lmv_enqueue,
2903         .m_getattr              = lmv_getattr,
2904         .m_getxattr             = lmv_getxattr,
2905         .m_getattr_name         = lmv_getattr_name,
2906         .m_intent_lock          = lmv_intent_lock,
2907         .m_link                 = lmv_link,
2908         .m_rename               = lmv_rename,
2909         .m_setattr              = lmv_setattr,
2910         .m_setxattr             = lmv_setxattr,
2911         .m_sync                 = lmv_sync,
2912         .m_readpage             = lmv_readpage,
2913         .m_unlink               = lmv_unlink,
2914         .m_init_ea_size         = lmv_init_ea_size,
2915         .m_cancel_unused        = lmv_cancel_unused,
2916         .m_set_lock_data        = lmv_set_lock_data,
2917         .m_lock_match           = lmv_lock_match,
2918         .m_get_lustre_md        = lmv_get_lustre_md,
2919         .m_free_lustre_md       = lmv_free_lustre_md,
2920         .m_set_open_replay_data = lmv_set_open_replay_data,
2921         .m_clear_open_replay_data = lmv_clear_open_replay_data,
2922         .m_get_remote_perm      = lmv_get_remote_perm,
2923         .m_renew_capa           = lmv_renew_capa
2924 };
2925
2926 int __init lmv_init(void)
2927 {
2928         struct lprocfs_static_vars lvars;
2929         int rc;
2930
2931         obj_cache = cfs_mem_cache_create("lmv_objects",
2932                                       sizeof(struct lmv_obj),
2933                                       0, 0);
2934         if (!obj_cache) {
2935                 CERROR("error allocating lmv objects cache\n");
2936                 return -ENOMEM;
2937         }
2938
2939         lprocfs_lmv_init_vars(&lvars);
2940         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2941                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2942         if (rc)
2943                 cfs_mem_cache_destroy(obj_cache);
2944
2945         return rc;
2946 }
2947
2948 #ifdef __KERNEL__
2949 static void lmv_exit(void)
2950 {
2951         int rc;
2952
2953         class_unregister_type(LUSTRE_LMV_NAME);
2954
2955         rc = cfs_mem_cache_destroy(obj_cache);
2956         LASSERTF(rc == 0,
2957                  "can't free lmv objects cache, %d object(s)"
2958                  "still in use\n", atomic_read(&obj_cache_count));
2959 }
2960
2961 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2962 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2963 MODULE_LICENSE("GPL");
2964
2965 module_init(lmv_init);
2966 module_exit(lmv_exit);
2967 #endif