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