Whamcloud - gitweb
Branch HEAD
[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         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
706                 int err;
707
708                 if (lmv->tgts[i].ltd_exp == NULL)
709                         continue;
710
711                 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len, karg, uarg);
712                 if (err) {
713                         if (lmv->tgts[i].ltd_active) {
714                                 CERROR("error: iocontrol MDC %s on MDT"
715                                        "idx %d: err = %d\n",
716                                        lmv->tgts[i].ltd_uuid.uuid, i, err);
717                                 if (!rc)
718                                         rc = err;
719                         }
720                 } else
721                         set = 1;
722         }
723         if (!set && !rc)
724                 rc = -EIO;
725
726         RETURN(rc);
727 }
728
729 enum MDS_POLICY {
730      CHAR_TYPE,
731      NID_TYPE
732 };
733
734 static int lmv_all_chars_policy(int count, const char *name,
735                                 int len)
736 {
737         unsigned int c = 0;
738
739         while (len > 0)
740                 c += name[--len];
741         c = c % count;
742         return c;
743 }
744
745 static int lmv_nid_policy(struct lmv_obd *lmv)
746 {
747         struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
748         __u32 id;
749         /*
750          * XXX Hack: to get nid we assume that underlying obd device is mdc.
751          */
752         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
753         return id % lmv->desc.ld_tgt_count;
754 }
755
756 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
757                           int type)
758 {
759         switch (type) {
760         case CHAR_TYPE:
761                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
762                                             op_data->op_name,
763                                             op_data->op_namelen);
764         case NID_TYPE:
765                 return lmv_nid_policy(lmv);
766
767         default:
768                 break;
769         }
770
771         CERROR("unsupport type %d \n", type);
772         return -EINVAL;
773 }
774
775 /* This is _inode_ placement policy function (not name). */
776 static int lmv_placement_policy(struct obd_device *obd,
777                                 struct md_op_data *op_data,
778                                 mdsno_t *mds)
779 {
780         struct lmv_obd *lmv = &obd->u.lmv;
781         struct lmv_obj *obj;
782         int rc;
783         ENTRY;
784
785         LASSERT(mds != NULL);
786
787         /*
788          * Allocate new fid on target according to operation type and parent
789          * home mds.
790          */
791         obj = lmv_obj_grab(obd, &op_data->op_fid1);
792         if (obj != NULL || op_data->op_name == NULL ||
793             op_data->op_opc != LUSTRE_OPC_MKDIR) {
794                 /*
795                  * Allocate fid for non-dir or for null name or for case parent
796                  * dir is split.
797                  */
798                 if (obj) {
799                         lmv_obj_put(obj);
800
801                         /*
802                          * If we have this flag turned on, and we see that
803                          * parent dir is split, this means, that caller did not
804                          * notice split yet. This is race and we would like to
805                          * let caller know that.
806                          */
807                         if (op_data->op_bias & MDS_CHECK_SPLIT)
808                                 RETURN(-ERESTART);
809                 }
810
811                 /*
812                  * Allocate new fid on same mds where parent fid is located and
813                  * where operation will be sent. In case of split dir, ->op_fid1
814                  * and ->op_mds here will contain fid and mds of slave directory
815                  * object (assigned by caller).
816                  */
817                 *mds = op_data->op_mds;
818                 rc = 0;
819
820 #if 0
821                 /* XXX: This should be removed later wehn we sure it is not
822                  * needed. */
823                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
824                 if (rc)
825                         GOTO(out, rc);
826 #endif
827         } else {
828                 /*
829                  * Parent directory is not split and we want to create a
830                  * directory in it. Let's calculate where to place it according
831                  * to name.
832                  */
833                 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
834                 rc = 0;
835         }
836         EXIT;
837 #if 0
838 out:
839 #endif
840         if (rc) {
841                 CERROR("Can't choose MDS, err = %d\n", rc);
842         } else {
843                 LASSERT(*mds < lmv->desc.ld_tgt_count);
844         }
845
846         return rc;
847 }
848
849 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
850                     mdsno_t mds)
851 {
852         struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
853         int rc;
854         ENTRY;
855
856         /* New seq alloc and FLD setup should be atomic. */
857         down(&tgt->ltd_fid_sem);
858
859         /* Asking underlaying tgt layer to allocate new fid. */
860         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
861         if (rc > 0) {
862                 LASSERT(fid_is_sane(fid));
863
864                 /* Client switches to new sequence, setup FLD. */
865                 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
866                                        mds, NULL);
867                 if (rc) {
868                         CERROR("Can't create fld entry, rc %d\n", rc);
869                         /* Delete just allocated fid sequence */
870                         obd_fid_delete(tgt->ltd_exp, NULL);
871                 }
872         }
873         up(&tgt->ltd_fid_sem);
874         RETURN(rc);
875 }
876
877 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
878                   struct md_op_data *op_data)
879 {
880         struct obd_device *obd = class_exp2obd(exp);
881         struct lmv_obd *lmv = &obd->u.lmv;
882         mdsno_t mds;
883         int rc;
884         ENTRY;
885
886         LASSERT(op_data != NULL);
887         LASSERT(fid != NULL);
888
889         rc = lmv_placement_policy(obd, op_data, &mds);
890         if (rc) {
891                 CERROR("Can't get target for allocating fid, "
892                        "rc %d\n", rc);
893                 RETURN(rc);
894         }
895
896         rc = __lmv_fid_alloc(lmv, fid, mds);
897         if (rc) {
898                 CERROR("Can't alloc new fid, rc %d\n", rc);
899                 RETURN(rc);
900         }
901
902         RETURN(rc);
903 }
904
905 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
906 {
907         ENTRY;
908
909         LASSERT(exp && fid);
910         if (lmv_obj_delete(exp, fid)) {
911                 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
912                        PFID(fid));
913         }
914         RETURN(0);
915 }
916
917 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
918 {
919         struct lmv_obd *lmv = &obd->u.lmv;
920         struct lprocfs_static_vars lvars;
921         struct lmv_desc *desc;
922         int rc, i = 0;
923         ENTRY;
924
925         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
926                 CERROR("LMV setup requires a descriptor\n");
927                 RETURN(-EINVAL);
928         }
929
930         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
931         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
932                 CERROR("descriptor size wrong: %d > %d\n",
933                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
934                 RETURN(-EINVAL);
935         }
936
937         lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
938
939         OBD_ALLOC(lmv->tgts, lmv->tgts_size);
940         if (lmv->tgts == NULL)
941                 RETURN(-ENOMEM);
942
943         for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
944                 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
945                 lmv->tgts[i].ltd_idx = i;
946         }
947
948         lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
949
950         OBD_ALLOC(lmv->datas, lmv->datas_size);
951         if (lmv->datas == NULL)
952                 GOTO(out_free_tgts, rc = -ENOMEM);
953
954         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
955         lmv->desc.ld_tgt_count = 0;
956         lmv->desc.ld_active_tgt_count = 0;
957         lmv->max_cookiesize = 0;
958         lmv->max_def_easize = 0;
959         lmv->max_easize = 0;
960
961         spin_lock_init(&lmv->lmv_lock);
962         sema_init(&lmv->init_sem, 1);
963
964         rc = lmv_obj_setup(obd);
965         if (rc) {
966                 CERROR("Can't setup LMV object manager, "
967                        "error %d.\n", rc);
968                 GOTO(out_free_datas, rc);
969         }
970
971         lprocfs_init_vars(lmv, &lvars);
972         lprocfs_obd_setup(obd, lvars.obd_vars);
973 #ifdef LPROCFS
974         {
975                 struct proc_dir_entry *entry;
976
977                 entry = create_proc_entry("target_obd_status", 0444,
978                                           obd->obd_proc_entry);
979                 if (entry != NULL) {
980                         entry->proc_fops = &lmv_proc_target_fops;
981                         entry->data = obd;
982                 }
983        }
984 #endif
985         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
986                              LUSTRE_CLI_FLD_HASH_DHT);
987         if (rc) {
988                 CERROR("can't init FLD, err %d\n",
989                        rc);
990                 GOTO(out_free_datas, rc);
991         }
992
993         RETURN(0);
994
995 out_free_datas:
996         OBD_FREE(lmv->datas, lmv->datas_size);
997         lmv->datas = NULL;
998 out_free_tgts:
999         OBD_FREE(lmv->tgts, lmv->tgts_size);
1000         lmv->tgts = NULL;
1001         return rc;
1002 }
1003
1004 static int lmv_cleanup(struct obd_device *obd)
1005 {
1006         struct lmv_obd *lmv = &obd->u.lmv;
1007         ENTRY;
1008
1009         fld_client_fini(&lmv->lmv_fld);
1010         lprocfs_obd_cleanup(obd);
1011         lmv_obj_cleanup(obd);
1012         OBD_FREE(lmv->datas, lmv->datas_size);
1013         OBD_FREE(lmv->tgts, lmv->tgts_size);
1014
1015         RETURN(0);
1016 }
1017
1018 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1019 {
1020         struct lustre_cfg *lcfg = buf;
1021         struct obd_uuid tgt_uuid;
1022         int rc;
1023         ENTRY;
1024
1025         switch(lcfg->lcfg_command) {
1026         case LCFG_ADD_MDC:
1027                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1028                         GOTO(out, rc = -EINVAL);
1029
1030                 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1031                 rc = lmv_add_target(obd, &tgt_uuid);
1032                 GOTO(out, rc);
1033         default: {
1034                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1035                 GOTO(out, rc = -EINVAL);
1036         }
1037         }
1038 out:
1039         RETURN(rc);
1040 }
1041
1042 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1043                       __u64 max_age)
1044 {
1045         struct lmv_obd *lmv = &obd->u.lmv;
1046         struct obd_statfs *temp;
1047         int rc = 0, i;
1048         ENTRY;
1049
1050         rc = lmv_check_connect(obd);
1051         if (rc)
1052                 RETURN(rc);
1053
1054         OBD_ALLOC(temp, sizeof(*temp));
1055         if (temp == NULL)
1056                 RETURN(-ENOMEM);
1057
1058         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1059                 if (lmv->tgts[i].ltd_exp == NULL)
1060                         continue;
1061
1062                 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp, max_age);
1063                 if (rc) {
1064                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1065                                lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1066                                rc);
1067                         GOTO(out_free_temp, rc);
1068                 }
1069                 if (i == 0) {
1070                         *osfs = *temp;
1071                 } else {
1072                         osfs->os_bavail += temp->os_bavail;
1073                         osfs->os_blocks += temp->os_blocks;
1074                         osfs->os_ffree += temp->os_ffree;
1075                         osfs->os_files += temp->os_files;
1076                 }
1077         }
1078
1079         EXIT;
1080 out_free_temp:
1081         OBD_FREE(temp, sizeof(*temp));
1082         return rc;
1083 }
1084
1085 static int lmv_getstatus(struct obd_export *exp,
1086                          struct lu_fid *fid,
1087                          struct obd_capa **pc)
1088 {
1089         struct obd_device *obd = exp->exp_obd;
1090         struct lmv_obd *lmv = &obd->u.lmv;
1091         int rc;
1092         ENTRY;
1093
1094         rc = lmv_check_connect(obd);
1095         if (rc)
1096                 RETURN(rc);
1097
1098         rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1099
1100         RETURN(rc);
1101 }
1102
1103 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1104                         struct obd_capa *oc, obd_valid valid, const char *name,
1105                         const char *input, int input_size, int output_size,
1106                         int flags, struct ptlrpc_request **request)
1107 {
1108         struct obd_device *obd = exp->exp_obd;
1109         struct lmv_obd *lmv = &obd->u.lmv;
1110         struct obd_export *tgt_exp;
1111         int rc;
1112         ENTRY;
1113
1114         rc = lmv_check_connect(obd);
1115         if (rc)
1116                 RETURN(rc);
1117
1118         tgt_exp = lmv_find_export(lmv, fid);
1119         if (IS_ERR(tgt_exp))
1120                 RETURN(PTR_ERR(tgt_exp));
1121
1122         rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1123                          output_size, flags, request);
1124
1125         RETURN(rc);
1126 }
1127
1128 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1129                         struct obd_capa *oc, obd_valid valid, const char *name,
1130                         const char *input, int input_size, int output_size,
1131                         int flags, struct ptlrpc_request **request)
1132 {
1133         struct obd_device *obd = exp->exp_obd;
1134         struct lmv_obd *lmv = &obd->u.lmv;
1135         struct obd_export *tgt_exp;
1136         int rc;
1137         ENTRY;
1138
1139         rc = lmv_check_connect(obd);
1140         if (rc)
1141                 RETURN(rc);
1142
1143         tgt_exp = lmv_find_export(lmv, fid);
1144         if (IS_ERR(tgt_exp))
1145                 RETURN(PTR_ERR(tgt_exp));
1146
1147         rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1148                          input, input_size, output_size, flags, request);
1149
1150         RETURN(rc);
1151 }
1152
1153 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1154                        struct obd_capa *oc, obd_valid valid, int ea_size,
1155                        struct ptlrpc_request **request)
1156 {
1157         struct obd_device *obd = exp->exp_obd;
1158         struct lmv_obd *lmv = &obd->u.lmv;
1159         struct obd_export *tgt_exp;
1160         struct lmv_obj *obj;
1161         int rc, i;
1162         ENTRY;
1163
1164         rc = lmv_check_connect(obd);
1165         if (rc)
1166                 RETURN(rc);
1167
1168         tgt_exp = lmv_find_export(lmv, fid);
1169         if (IS_ERR(tgt_exp))
1170                 RETURN(PTR_ERR(tgt_exp));
1171
1172         rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1173         if (rc)
1174                 RETURN(rc);
1175
1176         obj = lmv_obj_grab(obd, fid);
1177
1178         CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1179                obj ? "(split)" : "");
1180
1181         /*
1182          * If object is split, then we loop over all the slaves and gather size
1183          * attribute. In ideal world we would have to gather also mds field from
1184          * all slaves, as object is spread over the cluster and this is
1185          * definitely interesting information and it is not good to loss it,
1186          * but...
1187          */
1188         if (obj) {
1189                 struct mdt_body *body;
1190
1191                 if (*request == NULL) {
1192                         lmv_obj_put(obj);
1193                         RETURN(rc);
1194                 }
1195
1196                 body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
1197                                       sizeof(*body));
1198                 LASSERT(body != NULL);
1199                 LASSERT_REPSWABBED((*request), REPLY_REC_OFF);
1200
1201                 lmv_obj_lock(obj);
1202
1203                 for (i = 0; i < obj->lo_objcount; i++) {
1204                         if (lmv->tgts[i].ltd_exp == NULL) {
1205                                 CWARN("%s: NULL export for %d\n",
1206                                       obd->obd_name, i);
1207                                 continue;
1208                         }
1209
1210                         /* skip master obj. */
1211                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1212                                 continue;
1213
1214                         lmv_update_body(body, &obj->lo_inodes[i]);
1215                 }
1216
1217                 lmv_obj_unlock(obj);
1218                 lmv_obj_put(obj);
1219         }
1220
1221         RETURN(rc);
1222 }
1223
1224 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1225                              ldlm_iterator_t it, void *data)
1226 {
1227         struct obd_device *obd = exp->exp_obd;
1228         struct lmv_obd *lmv = &obd->u.lmv;
1229         int i, rc;
1230         ENTRY;
1231
1232         rc = lmv_check_connect(obd);
1233         if (rc)
1234                 RETURN(rc);
1235
1236         CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1237
1238         /*
1239          * With CMD every object can have two locks in different namespaces:
1240          * lookup lock in space of mds storing direntry and update/open lock in
1241          * space of mds storing inode.
1242          */
1243         for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1244                 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1245
1246         RETURN(0);
1247 }
1248
1249 static int lmv_close(struct obd_export *exp,
1250                      struct md_op_data *op_data,
1251                      struct obd_client_handle *och,
1252                      struct ptlrpc_request **request)
1253 {
1254         struct obd_device *obd = exp->exp_obd;
1255         struct lmv_obd *lmv = &obd->u.lmv;
1256         struct obd_export *tgt_exp;
1257         int rc;
1258         ENTRY;
1259
1260         rc = lmv_check_connect(obd);
1261         if (rc)
1262                 RETURN(rc);
1263
1264         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1265         if (IS_ERR(tgt_exp))
1266                 RETURN(PTR_ERR(tgt_exp));
1267
1268         CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1269         rc = md_close(tgt_exp, op_data, och, request);
1270         RETURN(rc);
1271 }
1272
1273 /*
1274  * Called in the case MDS returns -ERESTART on create on open, what means that
1275  * directory is split and its LMV presentation object has to be updated.
1276  */
1277 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1278 {
1279         struct obd_device *obd = exp->exp_obd;
1280         struct lmv_obd *lmv = &obd->u.lmv;
1281         struct ptlrpc_request *req = NULL;
1282         struct obd_export *tgt_exp;
1283         struct lmv_obj *obj;
1284         struct lustre_md md;
1285         int mealen, rc;
1286         __u64 valid;
1287         ENTRY;
1288
1289         md.mea = NULL;
1290         mealen = lmv_get_easize(lmv);
1291
1292         valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1293
1294         tgt_exp = lmv_find_export(lmv, fid);
1295         if (IS_ERR(tgt_exp))
1296                 RETURN(PTR_ERR(tgt_exp));
1297
1298         /* time to update mea of parent fid */
1299         rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1300         if (rc) {
1301                 CERROR("md_getattr() failed, error %d\n", rc);
1302                 GOTO(cleanup, rc);
1303         }
1304
1305         rc = md_get_lustre_md(tgt_exp, req, 1, NULL, exp, &md);
1306         if (rc) {
1307                 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1308                 GOTO(cleanup, rc);
1309         }
1310
1311         if (md.mea == NULL)
1312                 GOTO(cleanup, rc = -ENODATA);
1313
1314         obj = lmv_obj_create(exp, fid, md.mea);
1315         if (IS_ERR(obj))
1316                 rc = PTR_ERR(obj);
1317         else
1318                 lmv_obj_put(obj);
1319
1320         obd_free_memmd(exp, (struct lov_stripe_md **)&md.mea);
1321
1322         EXIT;
1323 cleanup:
1324         if (req)
1325                 ptlrpc_req_finished(req);
1326         return rc;
1327 }
1328
1329 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1330                const void *data, int datalen, int mode, __u32 uid,
1331                __u32 gid, __u32 cap_effective,  __u64 rdev,
1332                struct ptlrpc_request **request)
1333 {
1334         struct obd_device *obd = exp->exp_obd;
1335         struct lmv_obd *lmv = &obd->u.lmv;
1336         struct obd_export *tgt_exp;
1337         struct lmv_obj *obj;
1338         int rc, loop = 0;
1339         ENTRY;
1340
1341         rc = lmv_check_connect(obd);
1342         if (rc)
1343                 RETURN(rc);
1344
1345         if (!lmv->desc.ld_active_tgt_count)
1346                 RETURN(-EIO);
1347 repeat:
1348         ++loop;
1349         LASSERT(loop <= 2);
1350         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1351         if (obj) {
1352                 int mea_idx;
1353
1354                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1355                                        op_data->op_name, op_data->op_namelen);
1356                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1357                 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1358                 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1359                 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1360                 lmv_obj_put(obj);
1361         } else {
1362                 struct lmv_tgt_desc *tgt;
1363
1364                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1365                 op_data->op_bias |= MDS_CHECK_SPLIT;
1366                 op_data->op_mds = tgt->ltd_idx;
1367                 tgt_exp = tgt->ltd_exp;
1368         }
1369
1370         if (IS_ERR(tgt_exp))
1371                 RETURN(PTR_ERR(tgt_exp));
1372
1373         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1374         if (rc == -ERESTART)
1375                 goto repeat;
1376         else if (rc)
1377                 RETURN(rc);
1378
1379         CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1380                op_data->op_name, PFID(&op_data->op_fid1));
1381
1382         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1383         rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1384                        cap_effective, rdev, request);
1385         if (rc == 0) {
1386                 if (*request == NULL)
1387                         RETURN(rc);
1388                 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1389         } else if (rc == -ERESTART) {
1390                 LASSERT(*request != NULL);
1391                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1392                           "Got -ERESTART during create!\n");
1393                 ptlrpc_req_finished(*request);
1394                 *request = NULL;
1395
1396                 /*
1397                  * Directory got split. Time to update local object and repeat
1398                  * the request with proper MDS.
1399                  */
1400                 rc = lmv_handle_split(exp, &op_data->op_fid1);
1401                 if (rc == 0) {
1402                         rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1403                                                   op_data, &op_data->op_fid2);
1404                         if (rc)
1405                                 RETURN(rc);
1406                         goto repeat;
1407                 }
1408         }
1409         RETURN(rc);
1410 }
1411
1412 static int lmv_done_writing(struct obd_export *exp,
1413                             struct md_op_data *op_data,
1414                             struct obd_client_handle *och)
1415 {
1416         struct obd_device *obd = exp->exp_obd;
1417         struct lmv_obd *lmv = &obd->u.lmv;
1418         struct obd_export *tgt_exp;
1419         int rc;
1420         ENTRY;
1421
1422         rc = lmv_check_connect(obd);
1423         if (rc)
1424                 RETURN(rc);
1425
1426         tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1427         if (IS_ERR(tgt_exp))
1428                 RETURN(PTR_ERR(tgt_exp));
1429
1430         rc = md_done_writing(tgt_exp, op_data, och);
1431         RETURN(rc);
1432 }
1433
1434 static int
1435 lmv_enqueue_slaves(struct obd_export *exp, int locktype,
1436                    struct lookup_intent *it, int lockmode,
1437                    struct md_op_data *op_data, struct lustre_handle *lockh,
1438                    void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1439                    ldlm_blocking_callback cb_blocking, void *cb_data)
1440 {
1441         struct obd_device *obd = exp->exp_obd;
1442         struct lmv_obd *lmv = &obd->u.lmv;
1443         struct lmv_stripe_md *mea = op_data->op_mea1;
1444         struct md_op_data *op_data2;
1445         struct obd_export *tgt_exp;
1446         int i, rc = 0;
1447         ENTRY;
1448
1449         OBD_ALLOC_PTR(op_data2);
1450         if (op_data2 == NULL)
1451                 RETURN(-ENOMEM);
1452
1453         LASSERT(mea != NULL);
1454         for (i = 0; i < mea->mea_count; i++) {
1455                 memset(op_data2, 0, sizeof(*op_data2));
1456                 op_data2->op_fid1 = mea->mea_ids[i];
1457                 op_data2->op_bias = 0;
1458
1459                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1460                 if (IS_ERR(tgt_exp))
1461                         GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1462
1463                 if (tgt_exp == NULL)
1464                         continue;
1465
1466                 rc = md_enqueue(tgt_exp, locktype, it, lockmode, op_data2,
1467                                 lockh + i, lmm, lmmsize, cb_compl, cb_blocking,
1468                                 cb_data, 0);
1469
1470                 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1471                        PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1472
1473                 if (rc)
1474                         GOTO(cleanup, rc);
1475
1476                 if (it->d.lustre.it_data) {
1477                         struct ptlrpc_request *req;
1478                         req = (struct ptlrpc_request *)it->d.lustre.it_data;
1479                         ptlrpc_req_finished(req);
1480                 }
1481
1482                 if (it->d.lustre.it_status)
1483                         GOTO(cleanup, rc = it->d.lustre.it_status);
1484         }
1485
1486         EXIT;
1487 cleanup:
1488         OBD_FREE_PTR(op_data2);
1489
1490         if (rc != 0) {
1491                 /* drop all taken locks */
1492                 while (--i >= 0) {
1493                         if (lockh[i].cookie)
1494                                 ldlm_lock_decref(lockh + i, lockmode);
1495                         lockh[i].cookie = 0;
1496                 }
1497         }
1498         return rc;
1499 }
1500
1501 static int
1502 lmv_enqueue_remote(struct obd_export *exp, int lock_type,
1503                    struct lookup_intent *it, int lock_mode,
1504                    struct md_op_data *op_data, struct lustre_handle *lockh,
1505                    void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1506                    ldlm_blocking_callback cb_blocking, void *cb_data,
1507                    int extra_lock_flags)
1508 {
1509         struct ptlrpc_request *req = it->d.lustre.it_data;
1510         struct obd_device *obd = exp->exp_obd;
1511         struct lmv_obd *lmv = &obd->u.lmv;
1512         struct lustre_handle plock;
1513         struct obd_export *tgt_exp;
1514         struct md_op_data *rdata;
1515         struct lu_fid fid_copy;
1516         struct mdt_body *body;
1517         int rc = 0, pmode;
1518         ENTRY;
1519
1520         body = lustre_msg_buf(req->rq_repmsg,
1521                               DLM_REPLY_REC_OFF, sizeof(*body));
1522         LASSERT(body != NULL);
1523         LASSERT_REPSWABBED(req, DLM_REPLY_REC_OFF);
1524
1525         if (!(body->valid & OBD_MD_MDS))
1526                 RETURN(0);
1527
1528         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1529                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1530
1531         /* We got LOOKUP lock, but we really need attrs */
1532         pmode = it->d.lustre.it_lock_mode;
1533         LASSERT(pmode != 0);
1534         memcpy(&plock, lockh, sizeof(plock));
1535         it->d.lustre.it_lock_mode = 0;
1536         it->d.lustre.it_data = NULL;
1537         fid_copy = body->fid1;
1538
1539         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1540         ptlrpc_req_finished(req);
1541
1542         tgt_exp = lmv_find_export(lmv, &fid_copy);
1543         if (IS_ERR(tgt_exp))
1544                 GOTO(out, rc = PTR_ERR(tgt_exp));
1545
1546         OBD_ALLOC_PTR(rdata);
1547         if (rdata == NULL)
1548                 GOTO(out, rc = -ENOMEM);
1549
1550         rdata->op_fid1 = fid_copy;
1551         rdata->op_bias = MDS_CROSS_REF;
1552
1553         rc = md_enqueue(tgt_exp, lock_type, it, lock_mode, rdata,
1554                         lockh, lmm, lmmsize, cb_compl, cb_blocking,
1555                         cb_data, extra_lock_flags);
1556         OBD_FREE_PTR(rdata);
1557         EXIT;
1558 out:
1559         ldlm_lock_decref(&plock, pmode);
1560         return rc;
1561 }
1562
1563 static int
1564 lmv_enqueue(struct obd_export *exp, int lock_type,
1565             struct lookup_intent *it, int lock_mode,
1566             struct md_op_data *op_data, struct lustre_handle *lockh,
1567             void *lmm, int lmmsize, ldlm_completion_callback cb_compl,
1568             ldlm_blocking_callback cb_blocking, void *cb_data,
1569             int extra_lock_flags)
1570 {
1571         struct obd_device *obd = exp->exp_obd;
1572         struct lmv_obd *lmv = &obd->u.lmv;
1573         struct obd_export *tgt_exp = NULL;
1574         struct lmv_obj *obj;
1575         int rc;
1576         ENTRY;
1577
1578         rc = lmv_check_connect(obd);
1579         if (rc)
1580                 RETURN(rc);
1581
1582         if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1583                 rc = lmv_enqueue_slaves(exp, lock_type, it, lock_mode,
1584                                         op_data, lockh, lmm, lmmsize,
1585                                         cb_compl, cb_blocking, cb_data);
1586                 RETURN(rc);
1587         }
1588
1589         if (op_data->op_namelen) {
1590                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1591                 if (obj) {
1592                         int mea_idx;
1593
1594                         /* directory is split. look for right mds for this
1595                          * name */
1596                         mea_idx = raw_name2idx(obj->lo_hashtype,
1597                                                obj->lo_objcount,
1598                                                (char *)op_data->op_name,
1599                                                op_data->op_namelen);
1600                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1601                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1602                         lmv_obj_put(obj);
1603                 }
1604         }
1605
1606         if (tgt_exp == NULL)
1607                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1608         if (IS_ERR(tgt_exp))
1609                 RETURN(PTR_ERR(tgt_exp));
1610
1611         CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1612                PFID(&op_data->op_fid1));
1613
1614         rc = md_enqueue(tgt_exp, lock_type, it, lock_mode, op_data, lockh,
1615                         lmm, lmmsize, cb_compl, cb_blocking, cb_data,
1616                         extra_lock_flags);
1617
1618         if (rc == 0 && it->it_op == IT_OPEN)
1619                 rc = lmv_enqueue_remote(exp, lock_type, it, lock_mode,
1620                                         op_data, lockh, lmm, lmmsize,
1621                                         cb_compl, cb_blocking, cb_data,
1622                                         extra_lock_flags);
1623         RETURN(rc);
1624 }
1625
1626 static int
1627 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1628                  struct obd_capa *oc, const char *filename, int namelen,
1629                  obd_valid valid, int ea_size, struct ptlrpc_request **request)
1630 {
1631         struct obd_device *obd = exp->exp_obd;
1632         struct lmv_obd *lmv = &obd->u.lmv;
1633         struct lu_fid rid = *fid;
1634         struct obd_export *tgt_exp;
1635         struct mdt_body *body;
1636         struct lmv_obj *obj;
1637         int rc, loop = 0;
1638         ENTRY;
1639
1640         rc = lmv_check_connect(obd);
1641         if (rc)
1642                 RETURN(rc);
1643
1644 repeat:
1645         ++loop;
1646         LASSERT(loop <= 2);
1647         obj = lmv_obj_grab(obd, &rid);
1648         if (obj) {
1649                 int mea_idx;
1650
1651                 /* Directory is split. Look for right mds for this name */
1652                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1653                                        filename, namelen - 1);
1654                 rid = obj->lo_inodes[mea_idx].li_fid;
1655                 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1656                 lmv_obj_put(obj);
1657                 valid &= ~OBD_MD_FLCKSPLIT;
1658         } else {
1659                 tgt_exp = lmv_find_export(lmv, &rid);
1660                 valid |= OBD_MD_FLCKSPLIT;
1661         }
1662         if (IS_ERR(tgt_exp))
1663                 RETURN(PTR_ERR(tgt_exp));
1664
1665         CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1666                namelen, filename, PFID(fid), PFID(&rid));
1667
1668         rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1669                              ea_size, request);
1670         if (rc == 0) {
1671                 body = lustre_msg_buf((*request)->rq_repmsg,
1672                                       REQ_REC_OFF, sizeof(*body));
1673                 LASSERT(body != NULL);
1674                 LASSERT_REPSWABBED((*request), REQ_REC_OFF);
1675
1676                 if (body->valid & OBD_MD_MDS) {
1677                         struct ptlrpc_request *req = NULL;
1678
1679                         rid = body->fid1;
1680                         CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1681                                PFID(&rid));
1682
1683                         tgt_exp = lmv_find_export(lmv, &rid);
1684                         if (IS_ERR(tgt_exp)) {
1685                                 ptlrpc_req_finished(*request);
1686                                 RETURN(PTR_ERR(tgt_exp));
1687                         }
1688
1689                         rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1690                                              valid | OBD_MD_FLCROSSREF,
1691                                              ea_size, &req);
1692                         ptlrpc_req_finished(*request);
1693                         *request = req;
1694                 }
1695         } else if (rc == -ERESTART) {
1696                 LASSERT(*request != NULL);
1697                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1698                           "Got -ERESTART during getattr!\n");
1699                 ptlrpc_req_finished(*request);
1700                 *request = NULL;
1701
1702                 /*
1703                  * Directory got split. Time to update local object and repeat
1704                  * the request with proper MDS.
1705                  */
1706                 rc = lmv_handle_split(exp, &rid);
1707                 if (rc == 0)
1708                         goto repeat;
1709         }
1710         RETURN(rc);
1711 }
1712
1713 #define md_op_data_fid(op_data, fl)                     \
1714         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1715          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1716          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1717          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1718          NULL)
1719
1720 /* @tgt_exp is the export the metadata request is sent.
1721  * @fid_exp is the export the cancel should be sent for the current fid.
1722  * if @fid_exp is NULL, the export is found for the current fid.
1723  * @op_data keeps the current fid, which is pointed through @flag.
1724  * @mode, @bits -- lock match parameters. */
1725 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1726                             struct obd_export *fid_exp,
1727                             struct md_op_data *op_data,
1728                             ldlm_mode_t mode, int bits, int flag)
1729 {
1730         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1731         ldlm_policy_data_t policy = {{0}};
1732         int rc = 0;
1733         ENTRY;
1734
1735         if (!fid_is_sane(fid))
1736                 RETURN(0);
1737         
1738         if (fid_exp == NULL)
1739                 fid_exp = lmv_find_export(lmv, fid);
1740
1741         if (tgt_exp == fid_exp) {
1742                 /* The export is the same as on the target server, cancel 
1743                  * will be sent along with the main metadata operation. */
1744                 op_data->op_flags |= flag;
1745                 RETURN(0);
1746         }
1747
1748         policy.l_inodebits.bits = bits;
1749         rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1750         RETURN(rc);
1751 }
1752
1753 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1754 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1755  * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1756  * md_cancel_unused for child export(s). */
1757 static int lmv_early_cancel_stripes(struct obd_export *exp,
1758                                     struct obd_export *tgt_exp,
1759                                     struct md_op_data *op_data,
1760                                     ldlm_mode_t mode, int bits, int flag)
1761 {
1762         struct lu_fid *fid = md_op_data_fid(op_data, flag);
1763         struct obd_device *obd = exp->exp_obd;
1764         struct lmv_obd *lmv = &obd->u.lmv;
1765         struct obd_export *st_exp;
1766         struct lmv_obj *obj;
1767         int rc = 0;
1768         ENTRY;
1769
1770         if (!fid_is_sane(fid))
1771                 RETURN(0);
1772
1773         obj = lmv_obj_grab(obd, fid);
1774         if (obj) {
1775                 ldlm_policy_data_t policy = {{0}};
1776                 struct lu_fid *st_fid;
1777                 int i;
1778                 
1779                 policy.l_inodebits.bits = bits;
1780                 for (i = 0; i < obj->lo_objcount; i++) {
1781                         st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1782                         st_fid = &obj->lo_inodes[i].li_fid;
1783                         if (tgt_exp != st_exp) {
1784                                 rc = md_cancel_unused(st_exp, st_fid, &policy,
1785                                                       mode, 0, NULL);
1786                                 if (rc)
1787                                         break;
1788                         } else {
1789                                 /* Some export matches to @tgt_exp, do cancel
1790                                  * for its fid in mdc */
1791                                 *fid = *st_fid;
1792                                 op_data->op_flags |= flag;
1793                         }
1794                 }
1795                 lmv_obj_put(obj);
1796         } else {
1797                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1798                                       mode, bits, flag);
1799         }
1800         RETURN(rc);
1801 }
1802 #endif
1803
1804 /*
1805  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1806  * op_data->op_fid2
1807  */
1808 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1809                     struct ptlrpc_request **request)
1810 {
1811         struct obd_device *obd = exp->exp_obd;
1812         struct lmv_obd *lmv = &obd->u.lmv;
1813         struct obd_export *tgt_exp;
1814         struct lmv_obj *obj;
1815         int rc, loop = 0;
1816         mdsno_t mds;
1817         ENTRY;
1818
1819         rc = lmv_check_connect(obd);
1820         if (rc)
1821                 RETURN(rc);
1822
1823 repeat:
1824         ++loop;
1825         LASSERT(loop <= 2);
1826         if (op_data->op_namelen != 0) {
1827                 int mea_idx;
1828
1829                 /* Usual link request */
1830                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1831                 if (obj) {
1832                         mea_idx = raw_name2idx(obj->lo_hashtype,
1833                                                obj->lo_objcount,
1834                                                op_data->op_name,
1835                                                op_data->op_namelen);
1836                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1837                         mds = obj->lo_inodes[mea_idx].li_mds;
1838                         lmv_obj_put(obj);
1839                 } else {
1840                         rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1841                         if (rc)
1842                                 RETURN(rc);
1843                 }
1844
1845                 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1846                        PFID(&op_data->op_fid2), op_data->op_namelen,
1847                        op_data->op_name, PFID(&op_data->op_fid1));
1848         } else {
1849                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1850                 if (rc)
1851                         RETURN(rc);
1852
1853                 /* request from MDS to acquire i_links for inode by fid1 */
1854                 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1855                        PFID(&op_data->op_fid1));
1856         }
1857
1858         CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1859                mds, PFID(&op_data->op_fid1));
1860
1861         op_data->op_fsuid = current->fsuid;
1862         op_data->op_fsgid = current->fsgid;
1863         op_data->op_cap   = current->cap_effective;
1864
1865         tgt_exp = lmv->tgts[mds].ltd_exp;
1866         if (op_data->op_namelen) {
1867                 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1868                 /* Cancel UPDATE lock on child (fid1). */
1869                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1870                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1871         }
1872         if (rc == 0)
1873                 rc = md_link(tgt_exp, op_data, request);
1874         if (rc == -ERESTART) {
1875                 LASSERT(*request != NULL);
1876                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1877                           "Got -ERESTART during link!\n");
1878                 ptlrpc_req_finished(*request);
1879                 *request = NULL;
1880
1881                 /*
1882                  * Directory got split. Time to update local object and repeat
1883                  * the request with proper MDS.
1884                  */
1885                 rc = lmv_handle_split(exp, &op_data->op_fid2);
1886                 if (rc == 0)
1887                         goto repeat;
1888         }
1889
1890         RETURN(rc);
1891 }
1892
1893 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1894                       const char *old, int oldlen, const char *new, int newlen,
1895                       struct ptlrpc_request **request)
1896 {
1897         struct obd_export *tgt_exp = NULL, *src_exp;
1898         struct obd_device *obd = exp->exp_obd;
1899         struct lmv_obd *lmv = &obd->u.lmv;
1900         int rc, mea_idx, loop = 0;
1901         struct lmv_obj *obj;
1902         mdsno_t mds1, mds2;
1903         ENTRY;
1904
1905         CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1906                oldlen, old, PFID(&op_data->op_fid1),
1907                newlen, new, PFID(&op_data->op_fid2));
1908
1909         rc = lmv_check_connect(obd);
1910         if (rc)
1911                 RETURN(rc);
1912
1913         if (oldlen == 0) {
1914                 /*
1915                  * MDS with old dir entry is asking another MDS to create name
1916                  * there.
1917                  */
1918                 CDEBUG(D_OTHER,
1919                        "create %*s(%d/%d) in "DFID" pointing "
1920                        "to "DFID"\n", newlen, new, oldlen, newlen,
1921                        PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1922
1923                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1924                 if (rc)
1925                         RETURN(rc);
1926
1927                 /*
1928                  * Target directory can be split, sowe should forward request to
1929                  * the right MDS.
1930                  */
1931                 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1932                 if (obj) {
1933                         mea_idx = raw_name2idx(obj->lo_hashtype,
1934                                                obj->lo_objcount,
1935                                                (char *)new, newlen);
1936                         op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1937                         CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1938                                PFID(&op_data->op_fid2));
1939                         lmv_obj_put(obj);
1940                 }
1941                 goto request;
1942         }
1943
1944 repeat:
1945         ++loop;
1946         LASSERT(loop <= 2);
1947         obj = lmv_obj_grab(obd, &op_data->op_fid1);
1948         if (obj) {
1949                 /*
1950                  * directory is already split, so we have to forward request to
1951                  * the right MDS.
1952                  */
1953                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1954                                        (char *)old, oldlen);
1955                 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1956                 mds1 = obj->lo_inodes[mea_idx].li_mds;
1957                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1958                 lmv_obj_put(obj);
1959         } else {
1960                 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
1961                 if (rc)
1962                         RETURN(rc);
1963         }
1964
1965         obj = lmv_obj_grab(obd, &op_data->op_fid2);
1966         if (obj) {
1967                 /*
1968                  * Directory is already split, so we have to forward request to
1969                  * the right MDS.
1970                  */
1971                 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1972                                        (char *)new, newlen);
1973
1974                 mds2 = obj->lo_inodes[mea_idx].li_mds;
1975                 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1976                 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
1977                 lmv_obj_put(obj);
1978         } else {
1979                 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
1980                 if (rc)
1981                         RETURN(rc);
1982         }
1983
1984 request:
1985         op_data->op_fsuid = current->fsuid;
1986         op_data->op_fsgid = current->fsgid;
1987         op_data->op_cap   = current->cap_effective;
1988
1989         src_exp = lmv_get_export(lmv, mds1);
1990         tgt_exp = lmv_get_export(lmv, mds2);
1991         if (oldlen) {
1992                 /* LOOKUP lock on src child (fid3) should also be cancelled for
1993                  * src_exp in mdc_rename. */
1994                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1995
1996                 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
1997                  * own export. */
1998                 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
1999                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2000
2001                 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2002                 if (rc == 0)
2003                         rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2004                                               LCK_EX, MDS_INODELOCK_LOOKUP,
2005                                               MF_MDC_CANCEL_FID4);
2006
2007                 /* XXX: the case when child is a striped dir is not supported.
2008                  * Only the master stripe has all locks cancelled early. */
2009                 /* Cancel all the locks on tgt child (fid4). */
2010                 if (rc == 0)
2011                         rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2012                                               LCK_EX, MDS_INODELOCK_FULL,
2013                                               MF_MDC_CANCEL_FID4);
2014         }
2015
2016         if (rc == 0)
2017                 rc = md_rename(src_exp, op_data, old, oldlen,
2018                                new, newlen, request);
2019         if (rc == -ERESTART) {
2020                 LASSERT(*request != NULL);
2021                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2022                           "Got -ERESTART during rename!\n");
2023                 ptlrpc_req_finished(*request);
2024                 *request = NULL;
2025
2026                 /*
2027                  * Directory got split. Time to update local object and repeat
2028                  * the request with proper MDS.
2029                  */
2030                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2031                 if (rc == 0)
2032                         goto repeat;
2033         }
2034         RETURN(rc);
2035 }
2036
2037 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2038                        void *ea, int ealen, void *ea2, int ea2len,
2039                        struct ptlrpc_request **request)
2040 {
2041         struct obd_device *obd = exp->exp_obd;
2042         struct lmv_obd *lmv = &obd->u.lmv;
2043         struct ptlrpc_request *req;
2044         struct obd_export *tgt_exp;
2045         struct lmv_obj *obj;
2046         int rc = 0, i;
2047         ENTRY;
2048
2049         rc = lmv_check_connect(obd);
2050         if (rc)
2051                 RETURN(rc);
2052
2053         obj = lmv_obj_grab(obd, &op_data->op_fid1);
2054
2055         CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2056                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2057                obj ? ", split" : "");
2058
2059         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2060         if (obj) {
2061                 for (i = 0; i < obj->lo_objcount; i++) {
2062                         op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2063
2064                         tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2065                         if (IS_ERR(tgt_exp)) {
2066                                 rc = PTR_ERR(tgt_exp);
2067                                 break;
2068                         }
2069
2070                         rc = md_setattr(tgt_exp, op_data, ea, ealen,
2071                                         ea2, ea2len, &req);
2072
2073                         if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2074                                 /*
2075                                  * this is master object and this request should
2076                                  * be returned back to llite.
2077                                  */
2078                                 *request = req;
2079                         } else {
2080                                 ptlrpc_req_finished(req);
2081                         }
2082
2083                         if (rc)
2084                                 break;
2085                 }
2086                 lmv_obj_put(obj);
2087         } else {
2088                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2089                 if (IS_ERR(tgt_exp))
2090                         RETURN(PTR_ERR(tgt_exp));
2091
2092                 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2093                                 ea2len, request);
2094         }
2095         RETURN(rc);
2096 }
2097
2098 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2099                     struct obd_capa *oc, struct ptlrpc_request **request)
2100 {
2101         struct obd_device *obd = exp->exp_obd;
2102         struct lmv_obd *lmv = &obd->u.lmv;
2103         struct obd_export *tgt_exp;
2104         int rc;
2105         ENTRY;
2106
2107         rc = lmv_check_connect(obd);
2108         if (rc)
2109                 RETURN(rc);
2110
2111         tgt_exp = lmv_find_export(lmv, fid);
2112         if (IS_ERR(tgt_exp))
2113                 RETURN(PTR_ERR(tgt_exp));
2114
2115         rc = md_sync(tgt_exp, fid, oc, request);
2116         RETURN(rc);
2117 }
2118
2119 /* main purpose of LMV blocking ast is to remove split directory LMV
2120  * presentation object (struct lmv_obj) attached to the lock being revoked. */
2121 int lmv_blocking_ast(struct ldlm_lock *lock,
2122                      struct ldlm_lock_desc *desc,
2123                      void *data, int flag)
2124 {
2125         struct lustre_handle lockh;
2126         struct lmv_obj *obj;
2127         int rc;
2128         ENTRY;
2129
2130         switch (flag) {
2131         case LDLM_CB_BLOCKING:
2132                 ldlm_lock2handle(lock, &lockh);
2133                 rc = ldlm_cli_cancel(&lockh);
2134                 if (rc < 0) {
2135                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2136                         RETURN(rc);
2137                 }
2138                 break;
2139         case LDLM_CB_CANCELING:
2140                 /* time to drop cached attrs for dirobj */
2141                 obj = lock->l_ast_data;
2142                 if (obj) {
2143                         CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2144                                ", master "DFID"\n",
2145                                lock->l_resource->lr_name.name[3] == 1 ?
2146                                "LOOKUP" : "UPDATE",
2147                                lock->l_resource->lr_name.name[0],
2148                                lock->l_resource->lr_name.name[1],
2149                                PFID(&obj->lo_fid));
2150                         lmv_obj_put(obj);
2151                 }
2152                 break;
2153         default:
2154                 LBUG();
2155         }
2156         RETURN(0);
2157 }
2158
2159 static void lmv_hash_adjust(__u32 *hash, __u32 hash_adj)
2160 {
2161         __u32 val;
2162
2163         val = le32_to_cpu(*hash);
2164         if (val < hash_adj)
2165                 val += MAX_HASH_SIZE;
2166         if (val != DIR_END_OFF)
2167                 *hash = cpu_to_le32(val - hash_adj);
2168 }
2169
2170 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2171 {
2172         __u64 id;
2173         struct obd_import *imp;
2174
2175         /*
2176          * XXX Hack: to get nid we assume that underlying obd device is mdc.
2177          */
2178         imp  = class_exp2cliimp(exp);
2179         id   = imp->imp_connection->c_self + fid_flatten(fid);
2180
2181         CDEBUG(D_INFO, "node rank: %llx "DFID" %llx %llx\n",
2182                imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2183
2184         return id ^ (id >> 32);
2185 }
2186
2187 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2188                         struct obd_capa *oc, __u64 offset64, struct page *page,
2189                         struct ptlrpc_request **request)
2190 {
2191         struct obd_device *obd = exp->exp_obd;
2192         struct lmv_obd *lmv = &obd->u.lmv;
2193         struct obd_export *tgt_exp;
2194         struct lu_fid rid = *fid;
2195         struct lmv_obj *obj;
2196         __u32 offset0;
2197         __u32 offset;
2198         __u32 hash_adj = 0;
2199         __u32 rank = 0;
2200         __u32 seg_size = 0;
2201         int tgt = 0;
2202         int tgt0 = 0;
2203         int rc;
2204         int nr = 0;
2205         ENTRY;
2206
2207         offset0 = offset = offset64;
2208         /*
2209          * Check that offset is representable by 32bit number.
2210          */
2211         LASSERT((__u64)offset == offset64);
2212
2213         rc = lmv_check_connect(obd);
2214         if (rc)
2215                 RETURN(rc);
2216
2217         CDEBUG(D_INFO, "READPAGE at %x from "DFID"\n", offset, PFID(&rid));
2218
2219         obj = lmv_obj_grab(obd, fid);
2220         if (obj) {
2221                 struct lmv_inode *loi;
2222
2223                 lmv_obj_lock(obj);
2224
2225                 nr       = obj->lo_objcount;
2226                 LASSERT(nr > 0);
2227                 seg_size = MAX_HASH_SIZE / nr;
2228                 loi      = obj->lo_inodes;
2229                 rank     = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2230                                          fid) % nr;
2231                 tgt0     = (offset / seg_size) % nr;
2232                 tgt      = (tgt0 + rank) % nr;
2233
2234                 if (tgt < tgt0)
2235                         /*
2236                          * Wrap around.
2237                          *
2238                          * Last segment has unusual length due to division
2239                          * rounding.
2240                          */
2241                         hash_adj = MAX_HASH_SIZE - seg_size * nr;
2242                 else
2243                         hash_adj = 0;
2244
2245                 hash_adj += rank * seg_size;
2246
2247                 CDEBUG(D_INFO, "hash_adj: %x %x %x/%x -> %x/%x\n",
2248                        rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2249
2250                 offset = (offset + hash_adj) % MAX_HASH_SIZE;
2251                 rid = obj->lo_inodes[tgt].li_fid;
2252                 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2253
2254                 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2255                        PFID(&rid), (unsigned long)offset, tgt);
2256         } else
2257                 tgt_exp = lmv_find_export(lmv, &rid);
2258
2259         if (IS_ERR(tgt_exp))
2260                 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2261
2262         rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2263         if (rc)
2264                 GOTO(cleanup, rc);
2265         if (obj) {
2266                 struct lu_dirpage *dp;
2267                 struct lu_dirent  *ent;
2268
2269                 dp = cfs_kmap(page);
2270
2271                 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2272                 lmv_hash_adjust(&dp->ldp_hash_end,   hash_adj);
2273                 LASSERT(cpu_to_le32(dp->ldp_hash_start) <= offset0);
2274
2275                 for (ent = lu_dirent_start(dp); ent != NULL;
2276                      ent = lu_dirent_next(ent))
2277                         lmv_hash_adjust(&ent->lde_hash, hash_adj);
2278
2279                 if (tgt0 != nr - 1) {
2280                         __u32 end;
2281
2282                         end = le32_to_cpu(dp->ldp_hash_end);
2283                         if (end == DIR_END_OFF) {
2284                                 dp->ldp_hash_end = cpu_to_le32(seg_size *
2285                                                                (tgt0 + 1));
2286                                 CDEBUG(D_INFO, ""DFID" reset end %x tgt %d\n",
2287                                        PFID(&rid),
2288                                        le32_to_cpu(dp->ldp_hash_end), tgt);
2289                         }
2290                 }
2291                 cfs_kunmap(page);
2292         }
2293         /*
2294          * Here we could remove "." and ".." from all pages which at not from
2295          * master. But MDS has only "." and ".." for master dir.
2296          */
2297         EXIT;
2298 cleanup:
2299         if (obj) {
2300                 lmv_obj_unlock(obj);
2301                 lmv_obj_put(obj);
2302         }
2303         return rc;
2304 }
2305
2306 static int lmv_unlink_slaves(struct obd_export *exp,
2307                              struct md_op_data *op_data,
2308                              struct ptlrpc_request **req)
2309 {
2310         struct obd_device *obd = exp->exp_obd;
2311         struct lmv_obd *lmv = &obd->u.lmv;
2312         struct lmv_stripe_md *mea = op_data->op_mea1;
2313         struct md_op_data *op_data2;
2314         struct obd_export *tgt_exp;
2315         int i, rc = 0;
2316         ENTRY;
2317
2318         OBD_ALLOC_PTR(op_data2);
2319         if (op_data2 == NULL)
2320                 RETURN(-ENOMEM);
2321
2322         op_data2->op_mode = S_IFDIR;
2323         op_data2->op_fsuid = current->fsuid;
2324         op_data2->op_fsgid = current->fsgid;
2325         op_data2->op_bias = 0;
2326
2327         LASSERT(mea != NULL);
2328         for (i = 0; i < mea->mea_count; i++) {
2329                 memset(op_data2, 0, sizeof(*op_data2));
2330                 op_data2->op_fid1 = mea->mea_ids[i];
2331                 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2332                 if (IS_ERR(tgt_exp))
2333                         GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2334
2335                 if (tgt_exp == NULL)
2336                         continue;
2337
2338                 rc = md_unlink(tgt_exp, op_data2, req);
2339
2340                 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2341                        PFID(&mea->mea_ids[i]), rc);
2342
2343                 if (*req) {
2344                         ptlrpc_req_finished(*req);
2345                         *req = NULL;
2346                 }
2347                 if (rc)
2348                         GOTO(out_free_op_data2, rc);
2349         }
2350
2351         EXIT;
2352 out_free_op_data2:
2353         OBD_FREE_PTR(op_data2);
2354         return rc;
2355 }
2356
2357 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2358                       struct ptlrpc_request **request)
2359 {
2360         struct obd_device *obd = exp->exp_obd;
2361         struct lmv_obd *lmv = &obd->u.lmv;
2362         struct obd_export *tgt_exp = NULL;
2363         struct lmv_obj *obj;
2364         int rc, loop = 0;
2365         ENTRY;
2366
2367         rc = lmv_check_connect(obd);
2368         if (rc)
2369                 RETURN(rc);
2370
2371         if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2372                 /* mds asks to remove slave objects */
2373                 rc = lmv_unlink_slaves(exp, op_data, request);
2374                 RETURN(rc);
2375         }
2376
2377 repeat:
2378         ++loop;
2379         LASSERT(loop <= 2);
2380         if (op_data->op_namelen != 0) {
2381                 int mea_idx;
2382
2383                 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2384                 if (obj) {
2385                         mea_idx = raw_name2idx(obj->lo_hashtype,
2386                                                obj->lo_objcount,
2387                                                op_data->op_name,
2388                                                op_data->op_namelen);
2389                         op_data->op_bias &= ~MDS_CHECK_SPLIT;
2390                         op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2391                         tgt_exp = lmv_get_export(lmv,
2392                                                  obj->lo_inodes[mea_idx].li_mds);
2393                         lmv_obj_put(obj);
2394                         CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2395                                op_data->op_namelen, op_data->op_name,
2396                                PFID(&op_data->op_fid1), mea_idx);
2397                 }
2398         } else {
2399                 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2400                        PFID(&op_data->op_fid1));
2401         }
2402         if (tgt_exp == NULL) {
2403                 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2404                 if (IS_ERR(tgt_exp))
2405                         RETURN(PTR_ERR(tgt_exp));
2406                 op_data->op_bias |= MDS_CHECK_SPLIT;
2407         }
2408
2409         op_data->op_fsuid = current->fsuid;
2410         op_data->op_fsgid = current->fsgid;
2411         op_data->op_cap   = current->cap_effective;
2412
2413         /* If child's fid is given, cancel unused locks for it if it is from
2414          * another export than parent. */
2415         if (op_data->op_namelen) {
2416                 /* LOOKUP lock for child (fid3) should also be cancelled on 
2417                  * parent tgt_exp in mdc_unlink(). */
2418                 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2419
2420                 /* XXX: the case when child is a striped dir is not supported.
2421                  * Only the master stripe has all locks cancelled early. */
2422                 /* Cancel FULL locks on child (fid3). */
2423                 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2424                                       MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2425         }
2426         if (rc == 0)
2427                 rc = md_unlink(tgt_exp, op_data, request);
2428         if (rc == -ERESTART) {
2429                 LASSERT(*request != NULL);
2430                 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2431                           "Got -ERESTART during unlink!\n");
2432                 ptlrpc_req_finished(*request);
2433                 *request = NULL;
2434
2435                 /*
2436                  * Directory got split. Time to update local object and repeat
2437                  * the request with proper MDS.
2438                  */
2439                 rc = lmv_handle_split(exp, &op_data->op_fid1);
2440                 if (rc == 0)
2441                         goto repeat;
2442         }
2443         RETURN(rc);
2444 }
2445
2446 static int lmv_llog_init(struct obd_device *obd, struct obd_llogs* llogs,
2447                          struct obd_device *tgt, int count,
2448                          struct llog_catid *logid, struct obd_uuid *uuid)
2449 {
2450         struct llog_ctxt *ctxt;
2451         int rc;
2452         ENTRY;
2453
2454         rc = llog_setup(obd, llogs, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2455                         &llog_client_ops);
2456         if (rc == 0) {
2457                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2458                 ctxt->loc_imp = tgt->u.cli.cl_import;
2459         }
2460
2461         RETURN(rc);
2462 }
2463
2464 static int lmv_llog_finish(struct obd_device *obd, int count)
2465 {
2466         int rc;
2467         ENTRY;
2468
2469         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
2470         RETURN(rc);
2471 }
2472
2473 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2474 {
2475         int rc = 0;
2476
2477         switch (stage) {
2478         case OBD_CLEANUP_EARLY:
2479                 /* XXX: here should be calling obd_precleanup() down to
2480                  * stack. */
2481                 break;
2482         case OBD_CLEANUP_SELF_EXP:
2483                 rc = obd_llog_finish(obd, 0);
2484                 if (rc != 0)
2485                         CERROR("failed to cleanup llogging subsystems\n");
2486                 break;
2487         default:
2488                 break;
2489         }
2490         RETURN(rc);
2491 }
2492
2493 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2494                         void *key, __u32 *vallen, void *val)
2495 {
2496         struct obd_device *obd;
2497         struct lmv_obd *lmv;
2498         int rc = 0;
2499         ENTRY;
2500
2501         obd = class_exp2obd(exp);
2502         if (obd == NULL) {
2503                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2504                        exp->exp_handle.h_cookie);
2505                 RETURN(-EINVAL);
2506         }
2507
2508         lmv = &obd->u.lmv;
2509         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2510                 struct lmv_tgt_desc *tgts;
2511                 int i;
2512
2513                 rc = lmv_check_connect(obd);
2514                 if (rc)
2515                         RETURN(rc);
2516
2517                 LASSERT(*vallen == sizeof(__u32));
2518                 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2519                      i++, tgts++) {
2520
2521                         /* all tgts should be connected when this get called. */
2522                         if (!tgts || !tgts->ltd_exp) {
2523                                 CERROR("target not setup?\n");
2524                                 continue;
2525                         }
2526
2527                         if (!obd_get_info(tgts->ltd_exp, keylen, key,
2528                                           vallen, val))
2529                                 RETURN(0);
2530                 }
2531                 RETURN(-EINVAL);
2532         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2533                 rc = lmv_check_connect(obd);
2534                 if (rc)
2535                         RETURN(rc);
2536
2537                 /* forwarding this request to first MDS, it should know LOV
2538                  * desc. */
2539                 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2540                                   vallen, val);
2541                 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2542                         exp->exp_connect_flags =
2543                         ((struct obd_connect_data *)val)->ocd_connect_flags;
2544                 }
2545                 RETURN(rc);
2546         }
2547
2548         CDEBUG(D_IOCTL, "invalid key\n");
2549         RETURN(-EINVAL);
2550 }
2551
2552 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2553                        void *key, obd_count vallen, void *val,
2554                        struct ptlrpc_request_set *set)
2555 {
2556         struct lmv_tgt_desc    *tgt;
2557         struct obd_device      *obd;
2558         struct lmv_obd         *lmv;
2559         int rc = 0;
2560         ENTRY;
2561
2562         obd = class_exp2obd(exp);
2563         if (obd == NULL) {
2564                 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2565                        exp->exp_handle.h_cookie);
2566                 RETURN(-EINVAL);
2567         }
2568         lmv = &obd->u.lmv;
2569
2570         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2571             KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2572                 int i, err = 0;
2573
2574                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2575                         tgt = &lmv->tgts[i];
2576
2577                         if (!tgt->ltd_exp)
2578                                 continue;
2579
2580                         err = obd_set_info_async(tgt->ltd_exp,
2581                                                  keylen, key, vallen, val, set);
2582                         if (err && rc == 0)
2583                                 rc = err;
2584                 }
2585
2586                 RETURN(rc);
2587         }
2588
2589         RETURN(-EINVAL);
2590 }
2591
2592 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2593                struct lov_stripe_md *lsm)
2594 {
2595         struct obd_device *obd = class_exp2obd(exp);
2596         struct lmv_obd *lmv = &obd->u.lmv;
2597         struct lmv_stripe_md *meap, *lsmp;
2598         int mea_size, i;
2599         ENTRY;
2600
2601         mea_size = lmv_get_easize(lmv);
2602         if (!lmmp)
2603                 RETURN(mea_size);
2604
2605         if (*lmmp && !lsm) {
2606                 OBD_FREE(*lmmp, mea_size);
2607                 *lmmp = NULL;
2608                 RETURN(0);
2609         }
2610
2611         if (*lmmp == NULL) {
2612                 OBD_ALLOC(*lmmp, mea_size);
2613                 if (*lmmp == NULL)
2614                         RETURN(-ENOMEM);
2615         }
2616
2617         if (!lsm)
2618                 RETURN(mea_size);
2619
2620         lsmp = (struct lmv_stripe_md *)lsm;
2621         meap = (struct lmv_stripe_md *)*lmmp;
2622
2623         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2624             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2625                 RETURN(-EINVAL);
2626
2627         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2628         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2629         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2630
2631         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2632                 meap->mea_ids[i] = meap->mea_ids[i];
2633                 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2634         }
2635
2636         RETURN(mea_size);
2637 }
2638
2639 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2640                  struct lov_mds_md *lmm, int lmm_size)
2641 {
2642         struct obd_device *obd = class_exp2obd(exp);
2643         struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2644         struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2645         struct lmv_obd *lmv = &obd->u.lmv;
2646         int mea_size, i;
2647         __u32 magic;
2648         ENTRY;
2649
2650         mea_size = lmv_get_easize(lmv);
2651         if (lsmp == NULL)
2652                 return mea_size;
2653
2654         if (*lsmp != NULL && lmm == NULL) {
2655                 OBD_FREE(*tmea, mea_size);
2656                 *lsmp = NULL;
2657                 RETURN(0);
2658         }
2659
2660         LASSERT(mea_size == lmm_size);
2661
2662         OBD_ALLOC(*tmea, mea_size);
2663         if (*tmea == NULL)
2664                 RETURN(-ENOMEM);
2665
2666         if (!lmm)
2667                 RETURN(mea_size);
2668
2669         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2670             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2671             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2672         {
2673                 magic = le32_to_cpu(mea->mea_magic);
2674         } else {
2675                 /* old mea is not handled here */
2676                 LBUG();
2677         }
2678
2679         (*tmea)->mea_magic = magic;
2680         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2681         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2682
2683         for (i = 0; i < (*tmea)->mea_count; i++) {
2684                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2685                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2686         }
2687         RETURN(mea_size);
2688 }
2689
2690 static int lmv_cancel_unused(struct obd_export *exp,
2691                              const struct lu_fid *fid,
2692                              ldlm_policy_data_t *policy,
2693                              ldlm_mode_t mode, int flags, void *opaque)
2694 {
2695         struct obd_device *obd = exp->exp_obd;
2696         struct lmv_obd *lmv = &obd->u.lmv;
2697         int rc = 0, err, i;
2698         ENTRY;
2699
2700         LASSERT(fid != NULL);
2701
2702         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2703                 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2704                         continue;
2705
2706                 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2707                                        policy, mode, flags, opaque);
2708                 if (!rc)
2709                         rc = err;
2710         }
2711         RETURN(rc);
2712 }
2713
2714 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2715 {
2716         struct obd_device *obd = exp->exp_obd;
2717         struct lmv_obd *lmv = &obd->u.lmv;
2718
2719         ENTRY;
2720         RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2721 }
2722
2723 int lmv_lock_match(struct obd_export *exp, int flags,
2724                    const struct lu_fid *fid, ldlm_type_t type,
2725                    ldlm_policy_data_t *policy, ldlm_mode_t mode,
2726                    struct lustre_handle *lockh)
2727 {
2728         struct obd_device *obd = exp->exp_obd;
2729         struct lmv_obd *lmv = &obd->u.lmv;
2730         int i, rc = 0;
2731         ENTRY;
2732
2733         CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2734
2735         /* with CMD every object can have two locks in different namespaces:
2736          * lookup lock in space of mds storing direntry and update/open lock in
2737          * space of mds storing inode. Thus we check all targets, not only that
2738          * one fid was created in. */
2739         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2740                 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2741                                    type, policy, mode, lockh);
2742                 if (rc)
2743                         RETURN(1);
2744         }
2745
2746         RETURN(rc);
2747 }
2748
2749 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2750                       int offset, struct obd_export *dt_exp,
2751                       struct obd_export *md_exp, struct lustre_md *md)
2752 {
2753         struct obd_device *obd = exp->exp_obd;
2754         struct lmv_obd *lmv = &obd->u.lmv;
2755         int rc;
2756
2757         ENTRY;
2758         rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, offset, dt_exp, md_exp,
2759                               md);
2760         RETURN(rc);
2761 }
2762
2763 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2764 {
2765         struct obd_device *obd = exp->exp_obd;
2766         struct lmv_obd *lmv = &obd->u.lmv;
2767
2768         ENTRY;
2769         if (md->mea)
2770                 obd_free_memmd(exp, (struct lov_stripe_md**)&md->mea);
2771         RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2772 }
2773
2774 int lmv_set_open_replay_data(struct obd_export *exp,
2775                              struct obd_client_handle *och,
2776                              struct ptlrpc_request *open_req)
2777 {
2778         struct obd_device *obd = exp->exp_obd;
2779         struct lmv_obd *lmv = &obd->u.lmv;
2780         struct obd_export *tgt_exp;
2781
2782         ENTRY;
2783
2784         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2785         if (IS_ERR(tgt_exp))
2786                 RETURN(PTR_ERR(tgt_exp));
2787
2788         RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2789 }
2790
2791 int lmv_clear_open_replay_data(struct obd_export *exp,
2792                                struct obd_client_handle *och)
2793 {
2794         struct obd_device *obd = exp->exp_obd;
2795         struct lmv_obd *lmv = &obd->u.lmv;
2796         struct obd_export *tgt_exp;
2797         ENTRY;
2798
2799         tgt_exp = lmv_find_export(lmv, &och->och_fid);
2800         if (IS_ERR(tgt_exp))
2801                 RETURN(PTR_ERR(tgt_exp));
2802
2803         RETURN(md_clear_open_replay_data(tgt_exp, och));
2804 }
2805
2806 static int lmv_get_remote_perm(struct obd_export *exp,
2807                                const struct lu_fid *fid,
2808                                struct obd_capa *oc,
2809                                struct ptlrpc_request **request)
2810 {
2811         struct obd_device *obd = exp->exp_obd;
2812         struct lmv_obd *lmv = &obd->u.lmv;
2813         struct obd_export *tgt_exp;
2814         int rc;
2815
2816         ENTRY;
2817
2818         rc = lmv_check_connect(obd);
2819         if (rc)
2820                 RETURN(rc);
2821
2822         tgt_exp = lmv_find_export(lmv, fid);
2823         if (IS_ERR(tgt_exp))
2824                 RETURN(PTR_ERR(tgt_exp));
2825
2826         rc = md_get_remote_perm(tgt_exp, fid, oc, request);
2827
2828         RETURN(rc);
2829 }
2830
2831 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2832                           renew_capa_cb_t cb)
2833 {
2834         struct obd_device *obd = exp->exp_obd;
2835         struct lmv_obd *lmv = &obd->u.lmv;
2836         struct obd_export *tgt_exp;
2837         int rc;
2838         ENTRY;
2839
2840         rc = lmv_check_connect(obd);
2841         if (rc)
2842                 RETURN(rc);
2843
2844         tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2845         if (IS_ERR(tgt_exp))
2846                 RETURN(PTR_ERR(tgt_exp));
2847
2848         rc = md_renew_capa(tgt_exp, oc, cb);
2849         RETURN(rc);
2850 }
2851
2852 struct obd_ops lmv_obd_ops = {
2853         .o_owner                = THIS_MODULE,
2854         .o_setup                = lmv_setup,
2855         .o_cleanup              = lmv_cleanup,
2856         .o_precleanup           = lmv_precleanup,
2857         .o_process_config       = lmv_process_config,
2858         .o_connect              = lmv_connect,
2859         .o_disconnect           = lmv_disconnect,
2860         .o_statfs               = lmv_statfs,
2861         .o_llog_init            = lmv_llog_init,
2862         .o_llog_finish          = lmv_llog_finish,
2863         .o_get_info             = lmv_get_info,
2864         .o_set_info_async       = lmv_set_info_async,
2865         .o_packmd               = lmv_packmd,
2866         .o_unpackmd             = lmv_unpackmd,
2867         .o_notify               = lmv_notify,
2868         .o_iocontrol            = lmv_iocontrol,
2869         .o_fid_delete           = lmv_fid_delete
2870 };
2871
2872 struct md_ops lmv_md_ops = {
2873         .m_getstatus            = lmv_getstatus,
2874         .m_change_cbdata        = lmv_change_cbdata,
2875         .m_close                = lmv_close,
2876         .m_create               = lmv_create,
2877         .m_done_writing         = lmv_done_writing,
2878         .m_enqueue              = lmv_enqueue,
2879         .m_getattr              = lmv_getattr,
2880         .m_getxattr             = lmv_getxattr,
2881         .m_getattr_name         = lmv_getattr_name,
2882         .m_intent_lock          = lmv_intent_lock,
2883         .m_link                 = lmv_link,
2884         .m_rename               = lmv_rename,
2885         .m_setattr              = lmv_setattr,
2886         .m_setxattr             = lmv_setxattr,
2887         .m_sync                 = lmv_sync,
2888         .m_readpage             = lmv_readpage,
2889         .m_unlink               = lmv_unlink,
2890         .m_init_ea_size         = lmv_init_ea_size,
2891         .m_cancel_unused        = lmv_cancel_unused,
2892         .m_set_lock_data        = lmv_set_lock_data,
2893         .m_lock_match           = lmv_lock_match,
2894         .m_get_lustre_md        = lmv_get_lustre_md,
2895         .m_free_lustre_md       = lmv_free_lustre_md,
2896         .m_set_open_replay_data = lmv_set_open_replay_data,
2897         .m_clear_open_replay_data = lmv_clear_open_replay_data,
2898         .m_get_remote_perm      = lmv_get_remote_perm,
2899         .m_renew_capa           = lmv_renew_capa
2900 };
2901
2902 int __init lmv_init(void)
2903 {
2904         struct lprocfs_static_vars lvars;
2905         int rc;
2906
2907         obj_cache = cfs_mem_cache_create("lmv_objects",
2908                                       sizeof(struct lmv_obj),
2909                                       0, 0);
2910         if (!obj_cache) {
2911                 CERROR("error allocating lmv objects cache\n");
2912                 return -ENOMEM;
2913         }
2914
2915         lprocfs_init_vars(lmv, &lvars);
2916         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2917                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2918         if (rc)
2919                 cfs_mem_cache_destroy(obj_cache);
2920
2921         return rc;
2922 }
2923
2924 #ifdef __KERNEL__
2925 static void lmv_exit(void)
2926 {
2927         int rc;
2928
2929         class_unregister_type(LUSTRE_LMV_NAME);
2930
2931         rc = cfs_mem_cache_destroy(obj_cache);
2932         LASSERTF(rc == 0,
2933                  "can't free lmv objects cache, %d object(s)"
2934                  "still in use\n", atomic_read(&obj_cache_count));
2935 }
2936
2937 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
2938 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2939 MODULE_LICENSE("GPL");
2940
2941 module_init(lmv_init);
2942 module_exit(lmv_exit);
2943 #endif