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