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