Whamcloud - gitweb
LU-3882 hsm: Prevent duplicate CT registrations
[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 #else
49 #include <liblustre.h>
50 #endif
51
52 #include <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lprocfs_status.h>
58 #include <lustre_lite.h>
59 #include <lustre_fid.h>
60 #include "lmv_internal.h"
61
62 static void lmv_activate_target(struct lmv_obd *lmv,
63                                 struct lmv_tgt_desc *tgt,
64                                 int activate)
65 {
66         if (tgt->ltd_active == activate)
67                 return;
68
69         tgt->ltd_active = activate;
70         lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
71 }
72
73 /**
74  * Error codes:
75  *
76  *  -EINVAL  : UUID can't be found in the LMV's target list
77  *  -ENOTCONN: The UUID is found, but the target connection is bad (!)
78  *  -EBADF   : The UUID is found, but the OBD of the wrong type (!)
79  */
80 static int lmv_set_mdc_active(struct lmv_obd *lmv,
81                               const struct obd_uuid *uuid,
82                               int activate)
83 {
84         struct lmv_tgt_desc     *tgt = NULL;
85         struct obd_device       *obd;
86         __u32                    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 != NULL && (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         __u32                    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         __u32                    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         __u32                    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         __u32                    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         __u32                    i;
821         int                      nr = 0;
822         struct lmv_tgt_desc     *curr_tgt;
823
824         /* count how many requests must be sent to the given target */
825         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
826                 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
827                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
828                         nr++;
829         }
830         return nr;
831 }
832
833 static void lmv_hsm_req_build(struct lmv_obd *lmv,
834                               struct hsm_user_request *hur_in,
835                               const struct lmv_tgt_desc *tgt_mds,
836                               struct hsm_user_request *hur_out)
837 {
838         __u32                    i, nr_out;
839         struct lmv_tgt_desc     *curr_tgt;
840
841         /* build the hsm_user_request for the given target */
842         hur_out->hur_request = hur_in->hur_request;
843         nr_out = 0;
844         for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
845                 curr_tgt = lmv_find_target(lmv,
846                                            &hur_in->hur_user_item[i].hui_fid);
847                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
848                         hur_out->hur_user_item[nr_out] =
849                                                 hur_in->hur_user_item[i];
850                         nr_out++;
851                 }
852         }
853         hur_out->hur_request.hr_itemcount = nr_out;
854         memcpy(hur_data(hur_out), hur_data(hur_in),
855                hur_in->hur_request.hr_data_len);
856 }
857
858 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
859                                  struct lustre_kernelcomm *lk, void *uarg)
860 {
861         __u32                    i;
862         int                      rc;
863         struct kkuc_ct_data     *kcd = NULL;
864         ENTRY;
865
866         /* unregister request (call from llapi_hsm_copytool_fini) */
867         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
868                 /* best effort: try to clean as much as possible
869                  * (continue on error) */
870                 obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len, lk, uarg);
871         }
872
873         /* Whatever the result, remove copytool from kuc groups.
874          * Unreached coordinators will get EPIPE on next requests
875          * and will unregister automatically.
876          */
877         rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group, (void **)&kcd);
878         if (kcd != NULL)
879                 OBD_FREE_PTR(kcd);
880
881         RETURN(rc);
882 }
883
884 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
885                                struct lustre_kernelcomm *lk, void *uarg)
886 {
887         struct file             *filp;
888         __u32                    i, j;
889         int                      err, rc;
890         bool                     any_set = false;
891         struct kkuc_ct_data     *kcd;
892         ENTRY;
893
894         /* All or nothing: try to register to all MDS.
895          * In case of failure, unregister from previous MDS,
896          * except if it because of inactive target. */
897         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
898                 err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
899                                    len, lk, uarg);
900                 if (err) {
901                         if (lmv->tgts[i]->ltd_active) {
902                                 /* permanent error */
903                                 CERROR("%s: iocontrol MDC %s on MDT"
904                                        " idx %d cmd %x: err = %d\n",
905                                        class_exp2obd(lmv->exp)->obd_name,
906                                        lmv->tgts[i]->ltd_uuid.uuid,
907                                        i, cmd, err);
908                                 rc = err;
909                                 lk->lk_flags |= LK_FLG_STOP;
910                                 /* unregister from previous MDS */
911                                 for (j = 0; j < i; j++)
912                                         obd_iocontrol(cmd,
913                                                       lmv->tgts[j]->ltd_exp,
914                                                       len, lk, uarg);
915                                 RETURN(rc);
916                         }
917                         /* else: transient error.
918                          * kuc will register to the missing MDT
919                          * when it is back */
920                 } else {
921                         any_set = true;
922                 }
923         }
924
925         if (!any_set)
926                 /* no registration done: return error */
927                 RETURN(-ENOTCONN);
928
929         /* at least one registration done, with no failure */
930         filp = fget(lk->lk_wfd);
931         if (filp == NULL)
932                 RETURN(-EBADF);
933
934         OBD_ALLOC_PTR(kcd);
935         if (kcd == NULL) {
936                 fput(filp);
937                 RETURN(-ENOMEM);
938         }
939         kcd->kcd_magic = KKUC_CT_DATA_MAGIC;
940         kcd->kcd_uuid = lmv->cluuid;
941         kcd->kcd_archive = lk->lk_data;
942
943         rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, kcd);
944         if (rc != 0) {
945                 if (filp != NULL)
946                         fput(filp);
947                 OBD_FREE_PTR(kcd);
948         }
949
950         RETURN(rc);
951 }
952
953
954
955
956 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
957                          int len, void *karg, void *uarg)
958 {
959         struct obd_device       *obddev = class_exp2obd(exp);
960         struct lmv_obd          *lmv = &obddev->u.lmv;
961         __u32                    i = 0;
962         int                      rc = 0;
963         int                      set = 0;
964         __u32                    count = lmv->desc.ld_tgt_count;
965         ENTRY;
966
967         if (count == 0)
968                 RETURN(-ENOTTY);
969
970         switch (cmd) {
971         case IOC_OBD_STATFS: {
972                 struct obd_ioctl_data *data = karg;
973                 struct obd_device *mdc_obd;
974                 struct obd_statfs stat_buf = {0};
975                 __u32 index;
976
977                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
978                 if ((index >= count))
979                         RETURN(-ENODEV);
980
981                 if (lmv->tgts[index] == NULL ||
982                     lmv->tgts[index]->ltd_active == 0)
983                         RETURN(-ENODATA);
984
985                 mdc_obd = class_exp2obd(lmv->tgts[index]->ltd_exp);
986                 if (!mdc_obd)
987                         RETURN(-EINVAL);
988
989                 /* copy UUID */
990                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
991                                  min((int) data->ioc_plen2,
992                                      (int) sizeof(struct obd_uuid))))
993                         RETURN(-EFAULT);
994
995                 rc = obd_statfs(NULL, lmv->tgts[index]->ltd_exp, &stat_buf,
996                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
997                                 0);
998                 if (rc)
999                         RETURN(rc);
1000                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1001                                  min((int) data->ioc_plen1,
1002                                      (int) sizeof(stat_buf))))
1003                         RETURN(-EFAULT);
1004                 break;
1005         }
1006         case OBD_IOC_QUOTACTL: {
1007                 struct if_quotactl *qctl = karg;
1008                 struct lmv_tgt_desc *tgt = NULL;
1009                 struct obd_quotactl *oqctl;
1010
1011                 if (qctl->qc_valid == QC_MDTIDX) {
1012                         if (count <= qctl->qc_idx)
1013                                 RETURN(-EINVAL);
1014
1015                         tgt = lmv->tgts[qctl->qc_idx];
1016                         if (tgt == NULL || tgt->ltd_exp == NULL)
1017                                 RETURN(-EINVAL);
1018                 } else if (qctl->qc_valid == QC_UUID) {
1019                         for (i = 0; i < count; i++) {
1020                                 tgt = lmv->tgts[i];
1021                                 if (tgt == NULL)
1022                                         continue;
1023                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
1024                                                      &qctl->obd_uuid))
1025                                         continue;
1026
1027                                 if (tgt->ltd_exp == NULL)
1028                                         RETURN(-EINVAL);
1029
1030                                 break;
1031                         }
1032                 } else {
1033                         RETURN(-EINVAL);
1034                 }
1035
1036                 if (i >= count)
1037                         RETURN(-EAGAIN);
1038
1039                 LASSERT(tgt && tgt->ltd_exp);
1040                 OBD_ALLOC_PTR(oqctl);
1041                 if (!oqctl)
1042                         RETURN(-ENOMEM);
1043
1044                 QCTL_COPY(oqctl, qctl);
1045                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1046                 if (rc == 0) {
1047                         QCTL_COPY(qctl, oqctl);
1048                         qctl->qc_valid = QC_MDTIDX;
1049                         qctl->obd_uuid = tgt->ltd_uuid;
1050                 }
1051                 OBD_FREE_PTR(oqctl);
1052                 break;
1053         }
1054         case OBD_IOC_CHANGELOG_SEND:
1055         case OBD_IOC_CHANGELOG_CLEAR: {
1056                 struct ioc_changelog *icc = karg;
1057
1058                 if (icc->icc_mdtindex >= count)
1059                         RETURN(-ENODEV);
1060
1061                 if (lmv->tgts[icc->icc_mdtindex] == NULL ||
1062                     lmv->tgts[icc->icc_mdtindex]->ltd_exp == NULL ||
1063                     lmv->tgts[icc->icc_mdtindex]->ltd_active == 0)
1064                         RETURN(-ENODEV);
1065                 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex]->ltd_exp,
1066                                    sizeof(*icc), icc, NULL);
1067                 break;
1068         }
1069         case LL_IOC_GET_CONNECT_FLAGS: {
1070                 if (lmv->tgts[0] == NULL)
1071                         RETURN(-ENODATA);
1072                 rc = obd_iocontrol(cmd, lmv->tgts[0]->ltd_exp, len, karg, uarg);
1073                 break;
1074         }
1075         case OBD_IOC_FID2PATH: {
1076                 rc = lmv_fid2path(exp, len, karg, uarg);
1077                 break;
1078         }
1079         case LL_IOC_HSM_STATE_GET:
1080         case LL_IOC_HSM_STATE_SET:
1081         case LL_IOC_HSM_ACTION: {
1082                 struct md_op_data       *op_data = karg;
1083                 struct lmv_tgt_desc     *tgt;
1084
1085                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1086                 if (IS_ERR(tgt))
1087                         RETURN(PTR_ERR(tgt));
1088
1089                 if (tgt->ltd_exp == NULL)
1090                         RETURN(-EINVAL);
1091
1092                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1093                 break;
1094         }
1095         case LL_IOC_HSM_PROGRESS: {
1096                 const struct hsm_progress_kernel *hpk = karg;
1097                 struct lmv_tgt_desc     *tgt;
1098
1099                 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1100                 if (IS_ERR(tgt))
1101                         RETURN(PTR_ERR(tgt));
1102                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1103                 break;
1104         }
1105         case LL_IOC_HSM_REQUEST: {
1106                 struct hsm_user_request *hur = karg;
1107                 struct lmv_tgt_desc     *tgt;
1108                 unsigned int reqcount = hur->hur_request.hr_itemcount;
1109
1110                 if (reqcount == 0)
1111                         RETURN(0);
1112
1113                 /* if the request is about a single fid
1114                  * or if there is a single MDS, no need to split
1115                  * the request. */
1116                 if (reqcount == 1 || count == 1) {
1117                         tgt = lmv_find_target(lmv,
1118                                               &hur->hur_user_item[0].hui_fid);
1119                         if (IS_ERR(tgt))
1120                                 RETURN(PTR_ERR(tgt));
1121                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1122                 } else {
1123                         /* split fid list to their respective MDS */
1124                         for (i = 0; i < count; i++) {
1125                                 unsigned int            nr, reqlen;
1126                                 int                     rc1;
1127                                 struct hsm_user_request *req;
1128
1129                                 nr = lmv_hsm_req_count(lmv, hur, lmv->tgts[i]);
1130                                 if (nr == 0) /* nothing for this MDS */
1131                                         continue;
1132
1133                                 /* build a request with fids for this MDS */
1134                                 reqlen = offsetof(typeof(*hur),
1135                                                   hur_user_item[nr])
1136                                                 + hur->hur_request.hr_data_len;
1137                                 OBD_ALLOC_LARGE(req, reqlen);
1138                                 if (req == NULL)
1139                                         RETURN(-ENOMEM);
1140
1141                                 lmv_hsm_req_build(lmv, hur, lmv->tgts[i], req);
1142
1143                                 rc1 = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp,
1144                                                     reqlen, req, uarg);
1145                                 if (rc1 != 0 && rc == 0)
1146                                         rc = rc1;
1147                                 OBD_FREE_LARGE(req, reqlen);
1148                         }
1149                 }
1150                 break;
1151         }
1152         case LL_IOC_LOV_SWAP_LAYOUTS: {
1153                 struct md_op_data       *op_data = karg;
1154                 struct lmv_tgt_desc     *tgt1, *tgt2;
1155
1156                 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1157                 if (IS_ERR(tgt1))
1158                         RETURN(PTR_ERR(tgt1));
1159
1160                 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1161                 if (IS_ERR(tgt2))
1162                         RETURN(PTR_ERR(tgt2));
1163
1164                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1165                         RETURN(-EINVAL);
1166
1167                 /* only files on same MDT can have their layouts swapped */
1168                 if (tgt1->ltd_idx != tgt2->ltd_idx)
1169                         RETURN(-EPERM);
1170
1171                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1172                 break;
1173         }
1174         case LL_IOC_HSM_CT_START: {
1175                 struct lustre_kernelcomm *lk = karg;
1176                 if (lk->lk_flags & LK_FLG_STOP)
1177                         rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1178                 else
1179                         rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1180                 break;
1181         }
1182         default:
1183                 for (i = 0; i < count; i++) {
1184                         struct obd_device *mdc_obd;
1185                         int err;
1186
1187                         if (lmv->tgts[i] == NULL ||
1188                             lmv->tgts[i]->ltd_exp == NULL)
1189                                 continue;
1190                         /* ll_umount_begin() sets force flag but for lmv, not
1191                          * mdc. Let's pass it through */
1192                         mdc_obd = class_exp2obd(lmv->tgts[i]->ltd_exp);
1193                         mdc_obd->obd_force = obddev->obd_force;
1194                         err = obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len,
1195                                             karg, uarg);
1196                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1197                                 RETURN(err);
1198                         } else if (err) {
1199                                 if (lmv->tgts[i]->ltd_active) {
1200                                         CERROR("error: iocontrol MDC %s on MDT"
1201                                                " idx %d cmd %x: err = %d\n",
1202                                                lmv->tgts[i]->ltd_uuid.uuid,
1203                                                i, cmd, err);
1204                                         if (!rc)
1205                                                 rc = err;
1206                                 }
1207                         } else
1208                                 set = 1;
1209                 }
1210                 if (!set && !rc)
1211                         rc = -EIO;
1212         }
1213         RETURN(rc);
1214 }
1215
1216 #if 0
1217 static int lmv_all_chars_policy(int count, const char *name,
1218                                 int len)
1219 {
1220         unsigned int c = 0;
1221
1222         while (len > 0)
1223                 c += name[--len];
1224         c = c % count;
1225         return c;
1226 }
1227
1228 static int lmv_nid_policy(struct lmv_obd *lmv)
1229 {
1230         struct obd_import *imp;
1231         __u32              id;
1232
1233         /*
1234          * XXX: To get nid we assume that underlying obd device is mdc.
1235          */
1236         imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1237         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1238         return id % lmv->desc.ld_tgt_count;
1239 }
1240
1241 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1242                           placement_policy_t placement)
1243 {
1244         switch (placement) {
1245         case PLACEMENT_CHAR_POLICY:
1246                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1247                                             op_data->op_name,
1248                                             op_data->op_namelen);
1249         case PLACEMENT_NID_POLICY:
1250                 return lmv_nid_policy(lmv);
1251
1252         default:
1253                 break;
1254         }
1255
1256         CERROR("Unsupported placement policy %x\n", placement);
1257         return -EINVAL;
1258 }
1259 #endif
1260
1261 /**
1262  * This is _inode_ placement policy function (not name).
1263  */
1264 static int lmv_placement_policy(struct obd_device *obd,
1265                                 struct md_op_data *op_data,
1266                                 mdsno_t *mds)
1267 {
1268         struct lmv_obd          *lmv = &obd->u.lmv;
1269         ENTRY;
1270
1271         LASSERT(mds != NULL);
1272
1273         if (lmv->desc.ld_tgt_count == 1) {
1274                 *mds = 0;
1275                 RETURN(0);
1276         }
1277
1278         /**
1279          * If stripe_offset is provided during setdirstripe
1280          * (setdirstripe -i xx), xx MDS will be choosen.
1281          */
1282         if (op_data->op_cli_flags & CLI_SET_MEA) {
1283                 struct lmv_user_md *lum;
1284
1285                 lum = (struct lmv_user_md *)op_data->op_data;
1286                 if (lum->lum_type == LMV_STRIPE_TYPE &&
1287                     lum->lum_stripe_offset != -1) {
1288                         if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1289                                 CERROR("%s: Stripe_offset %d > MDT count %d:"
1290                                        " rc = %d\n", obd->obd_name,
1291                                        lum->lum_stripe_offset,
1292                                        lmv->desc.ld_tgt_count, -ERANGE);
1293                                 RETURN(-ERANGE);
1294                         }
1295                         *mds = lum->lum_stripe_offset;
1296                         RETURN(0);
1297                 }
1298         }
1299
1300         /* Allocate new fid on target according to operation type and parent
1301          * home mds. */
1302         *mds = op_data->op_mds;
1303         RETURN(0);
1304 }
1305
1306 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1307                     mdsno_t mds)
1308 {
1309         struct lmv_tgt_desc     *tgt;
1310         int                      rc;
1311         ENTRY;
1312
1313         tgt = lmv_get_target(lmv, mds);
1314         if (IS_ERR(tgt))
1315                 RETURN(PTR_ERR(tgt));
1316
1317         /*
1318          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1319          * on server that seq in new allocated fid is not yet known.
1320          */
1321         mutex_lock(&tgt->ltd_fid_mutex);
1322
1323         if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1324                 GOTO(out, rc = -ENODEV);
1325
1326         /*
1327          * Asking underlaying tgt layer to allocate new fid.
1328          */
1329         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1330         if (rc > 0) {
1331                 LASSERT(fid_is_sane(fid));
1332                 rc = 0;
1333         }
1334
1335         EXIT;
1336 out:
1337         mutex_unlock(&tgt->ltd_fid_mutex);
1338         return rc;
1339 }
1340
1341 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1342                   struct md_op_data *op_data)
1343 {
1344         struct obd_device     *obd = class_exp2obd(exp);
1345         struct lmv_obd        *lmv = &obd->u.lmv;
1346         mdsno_t                mds = 0;
1347         int                    rc;
1348         ENTRY;
1349
1350         LASSERT(op_data != NULL);
1351         LASSERT(fid != NULL);
1352
1353         rc = lmv_placement_policy(obd, op_data, &mds);
1354         if (rc) {
1355                 CERROR("Can't get target for allocating fid, "
1356                        "rc %d\n", rc);
1357                 RETURN(rc);
1358         }
1359
1360         rc = __lmv_fid_alloc(lmv, fid, mds);
1361         if (rc) {
1362                 CERROR("Can't alloc new fid, rc %d\n", rc);
1363                 RETURN(rc);
1364         }
1365
1366         RETURN(rc);
1367 }
1368
1369 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1370 {
1371         struct lmv_obd             *lmv = &obd->u.lmv;
1372         struct lprocfs_static_vars  lvars;
1373         struct lmv_desc            *desc;
1374         int                         rc;
1375         ENTRY;
1376
1377         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1378                 CERROR("LMV setup requires a descriptor\n");
1379                 RETURN(-EINVAL);
1380         }
1381
1382         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1383         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1384                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1385                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1386                 RETURN(-EINVAL);
1387         }
1388
1389         OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1390         if (lmv->tgts == NULL)
1391                 RETURN(-ENOMEM);
1392         lmv->tgts_size = 32;
1393
1394         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1395         lmv->desc.ld_tgt_count = 0;
1396         lmv->desc.ld_active_tgt_count = 0;
1397         lmv->max_cookiesize = 0;
1398         lmv->max_def_easize = 0;
1399         lmv->max_easize = 0;
1400         lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1401
1402         spin_lock_init(&lmv->lmv_lock);
1403         mutex_init(&lmv->init_mutex);
1404
1405         lprocfs_lmv_init_vars(&lvars);
1406
1407         lprocfs_obd_setup(obd, lvars.obd_vars);
1408         lprocfs_alloc_md_stats(obd, 0);
1409 #ifdef LPROCFS
1410         {
1411                 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1412                                         0444, &lmv_proc_target_fops, obd);
1413                 if (rc)
1414                         CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1415                               obd->obd_name, rc);
1416         }
1417 #endif
1418         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1419                              LUSTRE_CLI_FLD_HASH_DHT);
1420         if (rc) {
1421                 CERROR("Can't init FLD, err %d\n", rc);
1422                 GOTO(out, rc);
1423         }
1424
1425         RETURN(0);
1426
1427 out:
1428         return rc;
1429 }
1430
1431 static int lmv_cleanup(struct obd_device *obd)
1432 {
1433         struct lmv_obd   *lmv = &obd->u.lmv;
1434         ENTRY;
1435
1436         fld_client_fini(&lmv->lmv_fld);
1437         if (lmv->tgts != NULL) {
1438                 int i;
1439                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1440                         if (lmv->tgts[i] == NULL)
1441                                 continue;
1442                         lmv_del_target(lmv, i);
1443                 }
1444                 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1445                 lmv->tgts_size = 0;
1446         }
1447         RETURN(0);
1448 }
1449
1450 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1451 {
1452         struct lustre_cfg       *lcfg = buf;
1453         struct obd_uuid         obd_uuid;
1454         int                     gen;
1455         __u32                   index;
1456         int                     rc;
1457         ENTRY;
1458
1459         switch (lcfg->lcfg_command) {
1460         case LCFG_ADD_MDC:
1461                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1462                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
1463                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1464                         GOTO(out, rc = -EINVAL);
1465
1466                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1467
1468                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1469                         GOTO(out, rc = -EINVAL);
1470                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1471                         GOTO(out, rc = -EINVAL);
1472                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1473                 GOTO(out, rc);
1474         default:
1475                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1476                 GOTO(out, rc = -EINVAL);
1477         }
1478 out:
1479         RETURN(rc);
1480 }
1481
1482 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1483                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1484 {
1485         struct obd_device       *obd = class_exp2obd(exp);
1486         struct lmv_obd          *lmv = &obd->u.lmv;
1487         struct obd_statfs       *temp;
1488         int                      rc = 0;
1489         __u32                    i;
1490         ENTRY;
1491
1492         rc = lmv_check_connect(obd);
1493         if (rc)
1494                 RETURN(rc);
1495
1496         OBD_ALLOC(temp, sizeof(*temp));
1497         if (temp == NULL)
1498                 RETURN(-ENOMEM);
1499
1500         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1501                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1502                         continue;
1503
1504                 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1505                                 max_age, flags);
1506                 if (rc) {
1507                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1508                                lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1509                                rc);
1510                         GOTO(out_free_temp, rc);
1511                 }
1512
1513                 if (i == 0) {
1514                         *osfs = *temp;
1515                         /* If the statfs is from mount, it will needs
1516                          * retrieve necessary information from MDT0.
1517                          * i.e. mount does not need the merged osfs
1518                          * from all of MDT.
1519                          * And also clients can be mounted as long as
1520                          * MDT0 is in service*/
1521                         if (flags & OBD_STATFS_FOR_MDT0)
1522                                 GOTO(out_free_temp, rc);
1523                 } else {
1524                         osfs->os_bavail += temp->os_bavail;
1525                         osfs->os_blocks += temp->os_blocks;
1526                         osfs->os_ffree += temp->os_ffree;
1527                         osfs->os_files += temp->os_files;
1528                 }
1529         }
1530
1531         EXIT;
1532 out_free_temp:
1533         OBD_FREE(temp, sizeof(*temp));
1534         return rc;
1535 }
1536
1537 static int lmv_getstatus(struct obd_export *exp,
1538                          struct lu_fid *fid,
1539                          struct obd_capa **pc)
1540 {
1541         struct obd_device    *obd = exp->exp_obd;
1542         struct lmv_obd       *lmv = &obd->u.lmv;
1543         int                   rc;
1544         ENTRY;
1545
1546         rc = lmv_check_connect(obd);
1547         if (rc)
1548                 RETURN(rc);
1549
1550         rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1551         RETURN(rc);
1552 }
1553
1554 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1555                         struct obd_capa *oc, obd_valid valid, const char *name,
1556                         const char *input, int input_size, int output_size,
1557                         int flags, struct ptlrpc_request **request)
1558 {
1559         struct obd_device      *obd = exp->exp_obd;
1560         struct lmv_obd         *lmv = &obd->u.lmv;
1561         struct lmv_tgt_desc    *tgt;
1562         int                     rc;
1563         ENTRY;
1564
1565         rc = lmv_check_connect(obd);
1566         if (rc)
1567                 RETURN(rc);
1568
1569         tgt = lmv_find_target(lmv, fid);
1570         if (IS_ERR(tgt))
1571                 RETURN(PTR_ERR(tgt));
1572
1573         rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1574                          input_size, output_size, flags, request);
1575
1576         RETURN(rc);
1577 }
1578
1579 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1580                         struct obd_capa *oc, obd_valid valid, const char *name,
1581                         const char *input, int input_size, int output_size,
1582                         int flags, __u32 suppgid,
1583                         struct ptlrpc_request **request)
1584 {
1585         struct obd_device      *obd = exp->exp_obd;
1586         struct lmv_obd         *lmv = &obd->u.lmv;
1587         struct lmv_tgt_desc    *tgt;
1588         int                     rc;
1589         ENTRY;
1590
1591         rc = lmv_check_connect(obd);
1592         if (rc)
1593                 RETURN(rc);
1594
1595         tgt = lmv_find_target(lmv, fid);
1596         if (IS_ERR(tgt))
1597                 RETURN(PTR_ERR(tgt));
1598
1599         rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1600                          input_size, output_size, flags, suppgid,
1601                          request);
1602
1603         RETURN(rc);
1604 }
1605
1606 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1607                        struct ptlrpc_request **request)
1608 {
1609         struct obd_device       *obd = exp->exp_obd;
1610         struct lmv_obd          *lmv = &obd->u.lmv;
1611         struct lmv_tgt_desc     *tgt;
1612         int                      rc;
1613         ENTRY;
1614
1615         rc = lmv_check_connect(obd);
1616         if (rc)
1617                 RETURN(rc);
1618
1619         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1620         if (IS_ERR(tgt))
1621                 RETURN(PTR_ERR(tgt));
1622
1623         if (op_data->op_flags & MF_GET_MDT_IDX) {
1624                 op_data->op_mds = tgt->ltd_idx;
1625                 RETURN(0);
1626         }
1627
1628         rc = md_getattr(tgt->ltd_exp, op_data, request);
1629
1630         RETURN(rc);
1631 }
1632
1633 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1634 {
1635         struct obd_device   *obd = exp->exp_obd;
1636         struct lmv_obd      *lmv = &obd->u.lmv;
1637         __u32                i;
1638         int                  rc;
1639         ENTRY;
1640
1641         rc = lmv_check_connect(obd);
1642         if (rc)
1643                 RETURN(rc);
1644
1645         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1646
1647         /*
1648          * With DNE every object can have two locks in different namespaces:
1649          * lookup lock in space of MDT storing direntry and update/open lock in
1650          * space of MDT storing inode.
1651          */
1652         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1653                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1654                         continue;
1655                 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1656         }
1657
1658         RETURN(0);
1659 }
1660
1661 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1662                            ldlm_iterator_t it, void *data)
1663 {
1664         struct obd_device   *obd = exp->exp_obd;
1665         struct lmv_obd      *lmv = &obd->u.lmv;
1666         __u32                i;
1667         int                  rc;
1668         ENTRY;
1669
1670         rc = lmv_check_connect(obd);
1671         if (rc)
1672                 RETURN(rc);
1673
1674         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1675
1676         /*
1677          * With DNE every object can have two locks in different namespaces:
1678          * lookup lock in space of MDT storing direntry and update/open lock in
1679          * space of MDT storing inode.
1680          */
1681         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1682                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1683                         continue;
1684                 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1685                 if (rc)
1686                         RETURN(rc);
1687         }
1688
1689         RETURN(rc);
1690 }
1691
1692
1693 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1694                      struct md_open_data *mod, struct ptlrpc_request **request)
1695 {
1696         struct obd_device     *obd = exp->exp_obd;
1697         struct lmv_obd        *lmv = &obd->u.lmv;
1698         struct lmv_tgt_desc   *tgt;
1699         int                    rc;
1700         ENTRY;
1701
1702         rc = lmv_check_connect(obd);
1703         if (rc)
1704                 RETURN(rc);
1705
1706         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1707         if (IS_ERR(tgt))
1708                 RETURN(PTR_ERR(tgt));
1709
1710         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1711         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1712         RETURN(rc);
1713 }
1714
1715 struct lmv_tgt_desc
1716 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1717                 struct lu_fid *fid)
1718 {
1719         struct lmv_tgt_desc *tgt;
1720
1721         tgt = lmv_find_target(lmv, fid);
1722         if (IS_ERR(tgt))
1723                 return tgt;
1724
1725         op_data->op_mds = tgt->ltd_idx;
1726
1727         return tgt;
1728 }
1729
1730 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1731                const void *data, int datalen, int mode, __u32 uid,
1732                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1733                struct ptlrpc_request **request)
1734 {
1735         struct obd_device       *obd = exp->exp_obd;
1736         struct lmv_obd          *lmv = &obd->u.lmv;
1737         struct lmv_tgt_desc     *tgt;
1738         int                      rc;
1739         ENTRY;
1740
1741         rc = lmv_check_connect(obd);
1742         if (rc)
1743                 RETURN(rc);
1744
1745         if (!lmv->desc.ld_active_tgt_count)
1746                 RETURN(-EIO);
1747
1748         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1749         if (IS_ERR(tgt))
1750                 RETURN(PTR_ERR(tgt));
1751
1752         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1753         if (rc)
1754                 RETURN(rc);
1755
1756         CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1757                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1758                op_data->op_mds);
1759
1760         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1761         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1762                        cap_effective, rdev, request);
1763
1764         if (rc == 0) {
1765                 if (*request == NULL)
1766                         RETURN(rc);
1767                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1768         }
1769         RETURN(rc);
1770 }
1771
1772 static int lmv_done_writing(struct obd_export *exp,
1773                             struct md_op_data *op_data,
1774                             struct md_open_data *mod)
1775 {
1776         struct obd_device     *obd = exp->exp_obd;
1777         struct lmv_obd        *lmv = &obd->u.lmv;
1778         struct lmv_tgt_desc   *tgt;
1779         int                    rc;
1780         ENTRY;
1781
1782         rc = lmv_check_connect(obd);
1783         if (rc)
1784                 RETURN(rc);
1785
1786         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1787         if (IS_ERR(tgt))
1788                 RETURN(PTR_ERR(tgt));
1789
1790         rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1791         RETURN(rc);
1792 }
1793
1794 static int
1795 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1796                    struct lookup_intent *it, struct md_op_data *op_data,
1797                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1798                    int extra_lock_flags)
1799 {
1800         struct ptlrpc_request      *req = it->d.lustre.it_data;
1801         struct obd_device          *obd = exp->exp_obd;
1802         struct lmv_obd             *lmv = &obd->u.lmv;
1803         struct lustre_handle        plock;
1804         struct lmv_tgt_desc        *tgt;
1805         struct md_op_data          *rdata;
1806         struct lu_fid               fid1;
1807         struct mdt_body            *body;
1808         int                         rc = 0;
1809         int                         pmode;
1810         ENTRY;
1811
1812         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1813         LASSERT(body != NULL);
1814
1815         if (!(body->valid & OBD_MD_MDS))
1816                 RETURN(0);
1817
1818         CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1819                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1820
1821         /*
1822          * We got LOOKUP lock, but we really need attrs.
1823          */
1824         pmode = it->d.lustre.it_lock_mode;
1825         LASSERT(pmode != 0);
1826         memcpy(&plock, lockh, sizeof(plock));
1827         it->d.lustre.it_lock_mode = 0;
1828         it->d.lustre.it_data = NULL;
1829         fid1 = body->fid1;
1830
1831         it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1832         ptlrpc_req_finished(req);
1833
1834         tgt = lmv_find_target(lmv, &fid1);
1835         if (IS_ERR(tgt))
1836                 GOTO(out, rc = PTR_ERR(tgt));
1837
1838         OBD_ALLOC_PTR(rdata);
1839         if (rdata == NULL)
1840                 GOTO(out, rc = -ENOMEM);
1841
1842         rdata->op_fid1 = fid1;
1843         rdata->op_bias = MDS_CROSS_REF;
1844
1845         rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1846                         lmm, lmmsize, NULL, extra_lock_flags);
1847         OBD_FREE_PTR(rdata);
1848         EXIT;
1849 out:
1850         ldlm_lock_decref(&plock, pmode);
1851         return rc;
1852 }
1853
1854 static int
1855 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1856             struct lookup_intent *it, struct md_op_data *op_data,
1857             struct lustre_handle *lockh, void *lmm, int lmmsize,
1858             struct ptlrpc_request **req, __u64 extra_lock_flags)
1859 {
1860         struct obd_device        *obd = exp->exp_obd;
1861         struct lmv_obd           *lmv = &obd->u.lmv;
1862         struct lmv_tgt_desc      *tgt;
1863         int                       rc;
1864         ENTRY;
1865
1866         rc = lmv_check_connect(obd);
1867         if (rc)
1868                 RETURN(rc);
1869
1870         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1871                LL_IT2STR(it), PFID(&op_data->op_fid1));
1872
1873         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1874         if (IS_ERR(tgt))
1875                 RETURN(PTR_ERR(tgt));
1876
1877         CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1878                LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1879
1880         rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1881                         lmm, lmmsize, req, extra_lock_flags);
1882
1883         if (rc == 0 && it && it->it_op == IT_OPEN) {
1884                 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1885                                         lmm, lmmsize, extra_lock_flags);
1886         }
1887         RETURN(rc);
1888 }
1889
1890 static int
1891 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1892                  struct ptlrpc_request **request)
1893 {
1894         struct ptlrpc_request   *req = NULL;
1895         struct obd_device       *obd = exp->exp_obd;
1896         struct lmv_obd          *lmv = &obd->u.lmv;
1897         struct lmv_tgt_desc     *tgt;
1898         struct mdt_body         *body;
1899         int                      rc;
1900         ENTRY;
1901
1902         rc = lmv_check_connect(obd);
1903         if (rc)
1904                 RETURN(rc);
1905
1906         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1907         if (IS_ERR(tgt))
1908                 RETURN(PTR_ERR(tgt));
1909
1910         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1911                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1912                tgt->ltd_idx);
1913
1914         rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1915         if (rc != 0)
1916                 RETURN(rc);
1917
1918         body = req_capsule_server_get(&(*request)->rq_pill,
1919                                       &RMF_MDT_BODY);
1920         LASSERT(body != NULL);
1921
1922         if (body->valid & OBD_MD_MDS) {
1923                 struct lu_fid rid = body->fid1;
1924                 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1925                        PFID(&rid));
1926
1927                 tgt = lmv_find_target(lmv, &rid);
1928                 if (IS_ERR(tgt)) {
1929                         ptlrpc_req_finished(*request);
1930                         RETURN(PTR_ERR(tgt));
1931                 }
1932
1933                 op_data->op_fid1 = rid;
1934                 op_data->op_valid |= OBD_MD_FLCROSSREF;
1935                 op_data->op_namelen = 0;
1936                 op_data->op_name = NULL;
1937                 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1938                 ptlrpc_req_finished(*request);
1939                 *request = req;
1940         }
1941
1942         RETURN(rc);
1943 }
1944
1945 #define md_op_data_fid(op_data, fl)                     \
1946         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1947          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1948          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1949          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1950          NULL)
1951
1952 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1953                             int op_tgt, ldlm_mode_t mode, int bits, int flag)
1954 {
1955         struct lu_fid          *fid = md_op_data_fid(op_data, flag);
1956         struct obd_device      *obd = exp->exp_obd;
1957         struct lmv_obd         *lmv = &obd->u.lmv;
1958         struct lmv_tgt_desc    *tgt;
1959         ldlm_policy_data_t      policy = {{0}};
1960         int                     rc = 0;
1961         ENTRY;
1962
1963         if (!fid_is_sane(fid))
1964                 RETURN(0);
1965
1966         tgt = lmv_find_target(lmv, fid);
1967         if (IS_ERR(tgt))
1968                 RETURN(PTR_ERR(tgt));
1969
1970         if (tgt->ltd_idx != op_tgt) {
1971                 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1972                 policy.l_inodebits.bits = bits;
1973                 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1974                                       mode, LCF_ASYNC, NULL);
1975         } else {
1976                 CDEBUG(D_INODE,
1977                        "EARLY_CANCEL skip operation target %d on "DFID"\n",
1978                        op_tgt, PFID(fid));
1979                 op_data->op_flags |= flag;
1980                 rc = 0;
1981         }
1982
1983         RETURN(rc);
1984 }
1985
1986 /*
1987  * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1988  * op_data->op_fid2
1989  */
1990 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1991                     struct ptlrpc_request **request)
1992 {
1993         struct obd_device       *obd = exp->exp_obd;
1994         struct lmv_obd          *lmv = &obd->u.lmv;
1995         struct lmv_tgt_desc     *tgt;
1996         int                      rc;
1997         ENTRY;
1998
1999         rc = lmv_check_connect(obd);
2000         if (rc)
2001                 RETURN(rc);
2002
2003         LASSERT(op_data->op_namelen != 0);
2004
2005         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2006                PFID(&op_data->op_fid2), op_data->op_namelen,
2007                op_data->op_name, PFID(&op_data->op_fid1));
2008
2009         op_data->op_fsuid = current_fsuid();
2010         op_data->op_fsgid = current_fsgid();
2011         op_data->op_cap = cfs_curproc_cap_pack();
2012         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2013         if (IS_ERR(tgt))
2014                 RETURN(PTR_ERR(tgt));
2015
2016         /*
2017          * Cancel UPDATE lock on child (fid1).
2018          */
2019         op_data->op_flags |= MF_MDC_CANCEL_FID2;
2020         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2021                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2022         if (rc != 0)
2023                 RETURN(rc);
2024
2025         rc = md_link(tgt->ltd_exp, op_data, request);
2026
2027         RETURN(rc);
2028 }
2029
2030 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2031                       const char *old, int oldlen, const char *new, int newlen,
2032                       struct ptlrpc_request **request)
2033 {
2034         struct obd_device       *obd = exp->exp_obd;
2035         struct lmv_obd          *lmv = &obd->u.lmv;
2036         struct lmv_tgt_desc     *src_tgt;
2037         struct lmv_tgt_desc     *tgt_tgt;
2038         int                     rc;
2039         ENTRY;
2040
2041         LASSERT(oldlen != 0);
2042
2043         CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2044                oldlen, old, PFID(&op_data->op_fid1),
2045                newlen, new, PFID(&op_data->op_fid2));
2046
2047         rc = lmv_check_connect(obd);
2048         if (rc)
2049                 RETURN(rc);
2050
2051         op_data->op_fsuid = current_fsuid();
2052         op_data->op_fsgid = current_fsgid();
2053         op_data->op_cap = cfs_curproc_cap_pack();
2054         src_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2055         if (IS_ERR(src_tgt))
2056                 RETURN(PTR_ERR(src_tgt));
2057
2058         tgt_tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2059         if (IS_ERR(tgt_tgt))
2060                 RETURN(PTR_ERR(tgt_tgt));
2061         /*
2062          * LOOKUP lock on src child (fid3) should also be cancelled for
2063          * src_tgt in mdc_rename.
2064          */
2065         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2066
2067         /*
2068          * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2069          * own target.
2070          */
2071         rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2072                               LCK_EX, MDS_INODELOCK_UPDATE,
2073                               MF_MDC_CANCEL_FID2);
2074
2075         /*
2076          * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2077          */
2078         if (rc == 0) {
2079                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2080                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2081                                       MF_MDC_CANCEL_FID4);
2082         }
2083
2084         /*
2085          * Cancel all the locks on tgt child (fid4).
2086          */
2087         if (rc == 0)
2088                 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2089                                       LCK_EX, MDS_INODELOCK_FULL,
2090                                       MF_MDC_CANCEL_FID4);
2091
2092         if (rc == 0)
2093                 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2094                                new, newlen, request);
2095         RETURN(rc);
2096 }
2097
2098 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2099                        void *ea, int ealen, void *ea2, int ea2len,
2100                        struct ptlrpc_request **request,
2101                        struct md_open_data **mod)
2102 {
2103         struct obd_device       *obd = exp->exp_obd;
2104         struct lmv_obd          *lmv = &obd->u.lmv;
2105         struct lmv_tgt_desc     *tgt;
2106         int                      rc = 0;
2107         ENTRY;
2108
2109         rc = lmv_check_connect(obd);
2110         if (rc)
2111                 RETURN(rc);
2112
2113         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2114                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2115
2116         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2117         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2118         if (IS_ERR(tgt))
2119                 RETURN(PTR_ERR(tgt));
2120
2121         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2122                         ea2len, request, mod);
2123
2124         RETURN(rc);
2125 }
2126
2127 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2128                      struct obd_capa *oc, struct ptlrpc_request **request)
2129 {
2130         struct obd_device       *obd = exp->exp_obd;
2131         struct lmv_obd          *lmv = &obd->u.lmv;
2132         struct lmv_tgt_desc     *tgt;
2133         int                      rc;
2134         ENTRY;
2135
2136         rc = lmv_check_connect(obd);
2137         if (rc != 0)
2138                 RETURN(rc);
2139
2140         tgt = lmv_find_target(lmv, fid);
2141         if (IS_ERR(tgt))
2142                 RETURN(PTR_ERR(tgt));
2143
2144         rc = md_fsync(tgt->ltd_exp, fid, oc, request);
2145         RETURN(rc);
2146 }
2147
2148 /*
2149  * Adjust a set of pages, each page containing an array of lu_dirpages,
2150  * so that each page can be used as a single logical lu_dirpage.
2151  *
2152  * A lu_dirpage is laid out as follows, where s = ldp_hash_start,
2153  * e = ldp_hash_end, f = ldp_flags, p = padding, and each "ent" is a
2154  * struct lu_dirent.  It has size up to LU_PAGE_SIZE. The ldp_hash_end
2155  * value is used as a cookie to request the next lu_dirpage in a
2156  * directory listing that spans multiple pages (two in this example):
2157  *   ________
2158  *  |        |
2159  * .|--------v-------   -----.
2160  * |s|e|f|p|ent|ent| ... |ent|
2161  * '--|--------------   -----'   Each CFS_PAGE contains a single
2162  *    '------.                   lu_dirpage.
2163  * .---------v-------   -----.
2164  * |s|e|f|p|ent| 0 | ... | 0 |
2165  * '-----------------   -----'
2166  *
2167  * However, on hosts where the native VM page size (PAGE_CACHE_SIZE) is
2168  * larger than LU_PAGE_SIZE, a single host page may contain multiple
2169  * lu_dirpages. After reading the lu_dirpages from the MDS, the
2170  * ldp_hash_end of the first lu_dirpage refers to the one immediately
2171  * after it in the same CFS_PAGE (arrows simplified for brevity, but
2172  * in general e0==s1, e1==s2, etc.):
2173  *
2174  * .--------------------   -----.
2175  * |s0|e0|f0|p|ent|ent| ... |ent|
2176  * |---v----------------   -----|
2177  * |s1|e1|f1|p|ent|ent| ... |ent|
2178  * |---v----------------   -----|  Here, each CFS_PAGE contains
2179  *             ...                 multiple lu_dirpages.
2180  * |---v----------------   -----|
2181  * |s'|e'|f'|p|ent|ent| ... |ent|
2182  * '---|----------------   -----'
2183  *     v
2184  * .----------------------------.
2185  * |        next CFS_PAGE       |
2186  *
2187  * This structure is transformed into a single logical lu_dirpage as follows:
2188  *
2189  * - Replace e0 with e' so the request for the next lu_dirpage gets the page
2190  *   labeled 'next CFS_PAGE'.
2191  *
2192  * - Copy the LDF_COLLIDE flag from f' to f0 to correctly reflect whether
2193  *   a hash collision with the next page exists.
2194  *
2195  * - Adjust the lde_reclen of the ending entry of each lu_dirpage to span
2196  *   to the first entry of the next lu_dirpage.
2197  */
2198 #if PAGE_CACHE_SIZE > LU_PAGE_SIZE
2199 static void lmv_adjust_dirpages(struct page **pages, int ncfspgs, int nlupgs)
2200 {
2201         int i;
2202
2203         for (i = 0; i < ncfspgs; i++) {
2204                 struct lu_dirpage       *dp = kmap(pages[i]);
2205                 struct lu_dirpage       *first = dp;
2206                 struct lu_dirent        *end_dirent = NULL;
2207                 struct lu_dirent        *ent;
2208                 __u64                   hash_end = dp->ldp_hash_end;
2209                 __u32                   flags = dp->ldp_flags;
2210
2211                 while (--nlupgs > 0) {
2212                         ent = lu_dirent_start(dp);
2213                         for (end_dirent = ent; ent != NULL;
2214                              end_dirent = ent, ent = lu_dirent_next(ent));
2215
2216                         /* Advance dp to next lu_dirpage. */
2217                         dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2218
2219                         /* Check if we've reached the end of the CFS_PAGE. */
2220                         if (!((unsigned long)dp & ~CFS_PAGE_MASK))
2221                                 break;
2222
2223                         /* Save the hash and flags of this lu_dirpage. */
2224                         hash_end = dp->ldp_hash_end;
2225                         flags = dp->ldp_flags;
2226
2227                         /* Check if lu_dirpage contains no entries. */
2228                         if (!end_dirent)
2229                                 break;
2230
2231                         /* Enlarge the end entry lde_reclen from 0 to
2232                          * first entry of next lu_dirpage. */
2233                         LASSERT(le16_to_cpu(end_dirent->lde_reclen) == 0);
2234                         end_dirent->lde_reclen =
2235                                 cpu_to_le16((char *)(dp->ldp_entries) -
2236                                             (char *)end_dirent);
2237                 }
2238
2239                 first->ldp_hash_end = hash_end;
2240                 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2241                 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2242
2243                 kunmap(pages[i]);
2244         }
2245         LASSERTF(nlupgs == 0, "left = %d", nlupgs);
2246 }
2247 #else
2248 #define lmv_adjust_dirpages(pages, ncfspgs, nlupgs) do {} while (0)
2249 #endif  /* PAGE_CACHE_SIZE > LU_PAGE_SIZE */
2250
2251 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2252                         struct page **pages, struct ptlrpc_request **request)
2253 {
2254         struct obd_device       *obd = exp->exp_obd;
2255         struct lmv_obd          *lmv = &obd->u.lmv;
2256         __u64                   offset = op_data->op_offset;
2257         int                     rc;
2258         int                     ncfspgs; /* pages read in PAGE_CACHE_SIZE */
2259         int                     nlupgs; /* pages read in LU_PAGE_SIZE */
2260         struct lmv_tgt_desc     *tgt;
2261         ENTRY;
2262
2263         rc = lmv_check_connect(obd);
2264         if (rc)
2265                 RETURN(rc);
2266
2267         CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n",
2268                offset, PFID(&op_data->op_fid1));
2269
2270         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2271         if (IS_ERR(tgt))
2272                 RETURN(PTR_ERR(tgt));
2273
2274         rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2275         if (rc != 0)
2276                 RETURN(rc);
2277
2278         ncfspgs = ((*request)->rq_bulk->bd_nob_transferred +
2279                    PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2280         nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2281         LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2282         LASSERT(ncfspgs > 0 && ncfspgs <= op_data->op_npages);
2283
2284         CDEBUG(D_INODE, "read %d(%d)/%d pages\n", ncfspgs, nlupgs,
2285                op_data->op_npages);
2286
2287         lmv_adjust_dirpages(pages, ncfspgs, nlupgs);
2288
2289         RETURN(rc);
2290 }
2291
2292 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2293                       struct ptlrpc_request **request)
2294 {
2295         struct obd_device       *obd = exp->exp_obd;
2296         struct lmv_obd          *lmv = &obd->u.lmv;
2297         struct lmv_tgt_desc     *tgt = NULL;
2298         struct mdt_body         *body;
2299         int                     rc;
2300         ENTRY;
2301
2302         rc = lmv_check_connect(obd);
2303         if (rc)
2304                 RETURN(rc);
2305 retry:
2306         /* Send unlink requests to the MDT where the child is located */
2307         if (likely(!fid_is_zero(&op_data->op_fid2)))
2308                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2309         else
2310                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2311         if (IS_ERR(tgt))
2312                 RETURN(PTR_ERR(tgt));
2313
2314         op_data->op_fsuid = current_fsuid();
2315         op_data->op_fsgid = current_fsgid();
2316         op_data->op_cap = cfs_curproc_cap_pack();
2317
2318         /*
2319          * If child's fid is given, cancel unused locks for it if it is from
2320          * another export than parent.
2321          *
2322          * LOOKUP lock for child (fid3) should also be cancelled on parent
2323          * tgt_tgt in mdc_unlink().
2324          */
2325         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2326
2327         /*
2328          * Cancel FULL locks on child (fid3).
2329          */
2330         rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2331                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2332
2333         if (rc != 0)
2334                 RETURN(rc);
2335
2336         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2337                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2338
2339         rc = md_unlink(tgt->ltd_exp, op_data, request);
2340         if (rc != 0 && rc != -EREMOTE)
2341                 RETURN(rc);
2342
2343         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2344         if (body == NULL)
2345                 RETURN(-EPROTO);
2346
2347         /* Not cross-ref case, just get out of here. */
2348         if (likely(!(body->valid & OBD_MD_MDS)))
2349                 RETURN(0);
2350
2351         CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2352                exp->exp_obd->obd_name, PFID(&body->fid1));
2353
2354         /* This is a remote object, try remote MDT, Note: it may
2355          * try more than 1 time here, Considering following case
2356          * /mnt/lustre is root on MDT0, remote1 is on MDT1
2357          * 1. Initially A does not know where remote1 is, it send
2358          *    unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2359          *    resend unlink RPC to MDT1 (retry 1st time).
2360          *
2361          * 2. During the unlink RPC in flight,
2362          *    client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2363          *    and create new remote1, but on MDT0
2364          *
2365          * 3. MDT1 get unlink RPC(from A), then do remote lock on
2366          *    /mnt/lustre, then lookup get fid of remote1, and find
2367          *    it is remote dir again, and replay -EREMOTE again.
2368          *
2369          * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2370          *
2371          * In theory, it might try unlimited time here, but it should
2372          * be very rare case.  */
2373         op_data->op_fid2 = body->fid1;
2374         ptlrpc_req_finished(*request);
2375         *request = NULL;
2376
2377         goto retry;
2378 }
2379
2380 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2381 {
2382         struct lmv_obd *lmv = &obd->u.lmv;
2383         int rc = 0;
2384
2385         switch (stage) {
2386         case OBD_CLEANUP_EARLY:
2387                 /* XXX: here should be calling obd_precleanup() down to
2388                  * stack. */
2389                 break;
2390         case OBD_CLEANUP_EXPORTS:
2391                 fld_client_proc_fini(&lmv->lmv_fld);
2392                 lprocfs_obd_cleanup(obd);
2393                 lprocfs_free_md_stats(obd);
2394                 break;
2395         default:
2396                 break;
2397         }
2398         RETURN(rc);
2399 }
2400
2401 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2402                         __u32 keylen, void *key, __u32 *vallen, void *val,
2403                         struct lov_stripe_md *lsm)
2404 {
2405         struct obd_device       *obd;
2406         struct lmv_obd          *lmv;
2407         int                      rc = 0;
2408         ENTRY;
2409
2410         obd = class_exp2obd(exp);
2411         if (obd == NULL) {
2412                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2413                        exp->exp_handle.h_cookie);
2414                 RETURN(-EINVAL);
2415         }
2416
2417         lmv = &obd->u.lmv;
2418         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2419                 struct lmv_tgt_desc *tgt;
2420                 int i;
2421
2422                 rc = lmv_check_connect(obd);
2423                 if (rc)
2424                         RETURN(rc);
2425
2426                 LASSERT(*vallen == sizeof(__u32));
2427                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2428                         tgt = lmv->tgts[i];
2429                         /*
2430                          * All tgts should be connected when this gets called.
2431                          */
2432                         if (tgt == NULL || tgt->ltd_exp == NULL)
2433                                 continue;
2434
2435                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2436                                           vallen, val, NULL))
2437                                 RETURN(0);
2438                 }
2439                 RETURN(-EINVAL);
2440         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2441                 rc = lmv_check_connect(obd);
2442                 if (rc)
2443                         RETURN(rc);
2444
2445                 /*
2446                  * Forwarding this request to first MDS, it should know LOV
2447                  * desc.
2448                  */
2449                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2450                                   vallen, val, NULL);
2451                 if (!rc && KEY_IS(KEY_CONN_DATA))
2452                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2453                 RETURN(rc);
2454         } else if (KEY_IS(KEY_TGT_COUNT)) {
2455                 *((int *)val) = lmv->desc.ld_tgt_count;
2456                 RETURN(0);
2457         }
2458
2459         CDEBUG(D_IOCTL, "Invalid key\n");
2460         RETURN(-EINVAL);
2461 }
2462
2463 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2464                        obd_count keylen, void *key, obd_count vallen,
2465                        void *val, struct ptlrpc_request_set *set)
2466 {
2467         struct lmv_tgt_desc    *tgt;
2468         struct obd_device      *obd;
2469         struct lmv_obd         *lmv;
2470         int rc = 0;
2471         ENTRY;
2472
2473         obd = class_exp2obd(exp);
2474         if (obd == NULL) {
2475                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2476                        exp->exp_handle.h_cookie);
2477                 RETURN(-EINVAL);
2478         }
2479         lmv = &obd->u.lmv;
2480
2481         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2482                 int i, err = 0;
2483
2484                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2485                         tgt = lmv->tgts[i];
2486
2487                         if (tgt == NULL || tgt->ltd_exp == NULL)
2488                                 continue;
2489
2490                         err = obd_set_info_async(env, tgt->ltd_exp,
2491                                                  keylen, key, vallen, val, set);
2492                         if (err && rc == 0)
2493                                 rc = err;
2494                 }
2495
2496                 RETURN(rc);
2497         }
2498
2499         RETURN(-EINVAL);
2500 }
2501
2502 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2503                struct lov_stripe_md *lsm)
2504 {
2505         struct obd_device         *obd = class_exp2obd(exp);
2506         struct lmv_obd            *lmv = &obd->u.lmv;
2507         struct lmv_stripe_md      *meap;
2508         struct lmv_stripe_md      *lsmp;
2509         int                        mea_size;
2510         __u32                      i;
2511         ENTRY;
2512
2513         mea_size = lmv_get_easize(lmv);
2514         if (!lmmp)
2515                 RETURN(mea_size);
2516
2517         if (*lmmp && !lsm) {
2518                 OBD_FREE_LARGE(*lmmp, mea_size);
2519                 *lmmp = NULL;
2520                 RETURN(0);
2521         }
2522
2523         if (*lmmp == NULL) {
2524                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2525                 if (*lmmp == NULL)
2526                         RETURN(-ENOMEM);
2527         }
2528
2529         if (!lsm)
2530                 RETURN(mea_size);
2531
2532         lsmp = (struct lmv_stripe_md *)lsm;
2533         meap = (struct lmv_stripe_md *)*lmmp;
2534
2535         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2536             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2537                 RETURN(-EINVAL);
2538
2539         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2540         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2541         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2542
2543         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2544                 meap->mea_ids[i] = lsmp->mea_ids[i];
2545                 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2546         }
2547
2548         RETURN(mea_size);
2549 }
2550
2551 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2552                  struct lov_mds_md *lmm, int lmm_size)
2553 {
2554         struct obd_device          *obd = class_exp2obd(exp);
2555         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2556         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2557         struct lmv_obd             *lmv = &obd->u.lmv;
2558         int                         mea_size;
2559         __u32                       i;
2560         __u32                       magic;
2561         ENTRY;
2562
2563         mea_size = lmv_get_easize(lmv);
2564         if (lsmp == NULL)
2565                 return mea_size;
2566
2567         if (*lsmp != NULL && lmm == NULL) {
2568                 OBD_FREE_LARGE(*tmea, mea_size);
2569                 *lsmp = NULL;
2570                 RETURN(0);
2571         }
2572
2573         LASSERT(mea_size == lmm_size);
2574
2575         OBD_ALLOC_LARGE(*tmea, mea_size);
2576         if (*tmea == NULL)
2577                 RETURN(-ENOMEM);
2578
2579         if (!lmm)
2580                 RETURN(mea_size);
2581
2582         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2583             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2584             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2585         {
2586                 magic = le32_to_cpu(mea->mea_magic);
2587         } else {
2588                 /*
2589                  * Old mea is not handled here.
2590                  */
2591                 CERROR("Old not supportable EA is found\n");
2592                 LBUG();
2593         }
2594
2595         (*tmea)->mea_magic = magic;
2596         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2597         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2598
2599         for (i = 0; i < (*tmea)->mea_count; i++) {
2600                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2601                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2602         }
2603         RETURN(mea_size);
2604 }
2605
2606 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2607                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2608                              ldlm_cancel_flags_t flags, void *opaque)
2609 {
2610         struct obd_device       *obd = exp->exp_obd;
2611         struct lmv_obd          *lmv = &obd->u.lmv;
2612         int                      rc = 0;
2613         int                      err;
2614         __u32                    i;
2615         ENTRY;
2616
2617         LASSERT(fid != NULL);
2618
2619         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2620                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL ||
2621                     lmv->tgts[i]->ltd_active == 0)
2622                         continue;
2623
2624                 err = md_cancel_unused(lmv->tgts[i]->ltd_exp, fid,
2625                                        policy, mode, flags, opaque);
2626                 if (!rc)
2627                         rc = err;
2628         }
2629         RETURN(rc);
2630 }
2631
2632 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2633                       __u64 *bits)
2634 {
2635         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2636         int                      rc;
2637         ENTRY;
2638
2639         rc =  md_set_lock_data(lmv->tgts[0]->ltd_exp, lockh, data, bits);
2640         RETURN(rc);
2641 }
2642
2643 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2644                            const struct lu_fid *fid, ldlm_type_t type,
2645                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2646                            struct lustre_handle *lockh)
2647 {
2648         struct obd_device       *obd = exp->exp_obd;
2649         struct lmv_obd          *lmv = &obd->u.lmv;
2650         ldlm_mode_t              rc;
2651         __u32                    i;
2652         ENTRY;
2653
2654         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2655
2656         /*
2657          * With CMD every object can have two locks in different namespaces:
2658          * lookup lock in space of mds storing direntry and update/open lock in
2659          * space of mds storing inode. Thus we check all targets, not only that
2660          * one fid was created in.
2661          */
2662         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2663                 if (lmv->tgts[i] == NULL ||
2664                     lmv->tgts[i]->ltd_exp == NULL ||
2665                     lmv->tgts[i]->ltd_active == 0)
2666                         continue;
2667
2668                 rc = md_lock_match(lmv->tgts[i]->ltd_exp, flags, fid,
2669                                    type, policy, mode, lockh);
2670                 if (rc)
2671                         RETURN(rc);
2672         }
2673
2674         RETURN(0);
2675 }
2676
2677 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2678                       struct obd_export *dt_exp, struct obd_export *md_exp,
2679                       struct lustre_md *md)
2680 {
2681         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2682
2683         return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
2684 }
2685
2686 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2687 {
2688         struct obd_device       *obd = exp->exp_obd;
2689         struct lmv_obd          *lmv = &obd->u.lmv;
2690         ENTRY;
2691
2692         if (md->mea)
2693                 obd_free_memmd(exp, (void *)&md->mea);
2694         RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
2695 }
2696
2697 int lmv_set_open_replay_data(struct obd_export *exp,
2698                              struct obd_client_handle *och,
2699                              struct ptlrpc_request *open_req)
2700 {
2701         struct obd_device       *obd = exp->exp_obd;
2702         struct lmv_obd          *lmv = &obd->u.lmv;
2703         struct lmv_tgt_desc     *tgt;
2704         ENTRY;
2705
2706         tgt = lmv_find_target(lmv, &och->och_fid);
2707         if (IS_ERR(tgt))
2708                 RETURN(PTR_ERR(tgt));
2709
2710         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2711 }
2712
2713 int lmv_clear_open_replay_data(struct obd_export *exp,
2714                                struct obd_client_handle *och)
2715 {
2716         struct obd_device       *obd = exp->exp_obd;
2717         struct lmv_obd          *lmv = &obd->u.lmv;
2718         struct lmv_tgt_desc     *tgt;
2719         ENTRY;
2720
2721         tgt = lmv_find_target(lmv, &och->och_fid);
2722         if (IS_ERR(tgt))
2723                 RETURN(PTR_ERR(tgt));
2724
2725         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2726 }
2727
2728 static int lmv_get_remote_perm(struct obd_export *exp,
2729                                const struct lu_fid *fid,
2730                                struct obd_capa *oc, __u32 suppgid,
2731                                struct ptlrpc_request **request)
2732 {
2733         struct obd_device       *obd = exp->exp_obd;
2734         struct lmv_obd          *lmv = &obd->u.lmv;
2735         struct lmv_tgt_desc     *tgt;
2736         int                      rc;
2737         ENTRY;
2738
2739         rc = lmv_check_connect(obd);
2740         if (rc)
2741                 RETURN(rc);
2742
2743         tgt = lmv_find_target(lmv, fid);
2744         if (IS_ERR(tgt))
2745                 RETURN(PTR_ERR(tgt));
2746
2747         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2748         RETURN(rc);
2749 }
2750
2751 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2752                           renew_capa_cb_t cb)
2753 {
2754         struct obd_device       *obd = exp->exp_obd;
2755         struct lmv_obd          *lmv = &obd->u.lmv;
2756         struct lmv_tgt_desc     *tgt;
2757         int                      rc;
2758         ENTRY;
2759
2760         rc = lmv_check_connect(obd);
2761         if (rc)
2762                 RETURN(rc);
2763
2764         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2765         if (IS_ERR(tgt))
2766                 RETURN(PTR_ERR(tgt));
2767
2768         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2769         RETURN(rc);
2770 }
2771
2772 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2773                     const struct req_msg_field *field, struct obd_capa **oc)
2774 {
2775         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2776
2777         return md_unpack_capa(lmv->tgts[0]->ltd_exp, req, field, oc);
2778 }
2779
2780 int lmv_intent_getattr_async(struct obd_export *exp,
2781                              struct md_enqueue_info *minfo,
2782                              struct ldlm_enqueue_info *einfo)
2783 {
2784         struct md_op_data       *op_data = &minfo->mi_data;
2785         struct obd_device       *obd = exp->exp_obd;
2786         struct lmv_obd          *lmv = &obd->u.lmv;
2787         struct lmv_tgt_desc     *tgt = NULL;
2788         int                      rc;
2789         ENTRY;
2790
2791         rc = lmv_check_connect(obd);
2792         if (rc)
2793                 RETURN(rc);
2794
2795         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2796         if (IS_ERR(tgt))
2797                 RETURN(PTR_ERR(tgt));
2798
2799         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2800         RETURN(rc);
2801 }
2802
2803 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2804                         struct lu_fid *fid, __u64 *bits)
2805 {
2806         struct obd_device       *obd = exp->exp_obd;
2807         struct lmv_obd          *lmv = &obd->u.lmv;
2808         struct lmv_tgt_desc     *tgt;
2809         int                      rc;
2810         ENTRY;
2811
2812         rc = lmv_check_connect(obd);
2813         if (rc)
2814                 RETURN(rc);
2815
2816         tgt = lmv_find_target(lmv, fid);
2817         if (IS_ERR(tgt))
2818                 RETURN(PTR_ERR(tgt));
2819
2820         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2821         RETURN(rc);
2822 }
2823
2824 /**
2825  * For lmv, only need to send request to master MDT, and the master MDT will
2826  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2827  * we directly fetch data from the slave MDTs.
2828  */
2829 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2830                  struct obd_quotactl *oqctl)
2831 {
2832         struct obd_device   *obd = class_exp2obd(exp);
2833         struct lmv_obd      *lmv = &obd->u.lmv;
2834         struct lmv_tgt_desc *tgt = lmv->tgts[0];
2835         int                  rc = 0;
2836         __u32                i;
2837         __u64                curspace, curinodes;
2838         ENTRY;
2839
2840         if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
2841                 CERROR("master lmv inactive\n");
2842                 RETURN(-EIO);
2843         }
2844
2845         if (oqctl->qc_cmd != Q_GETOQUOTA) {
2846                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2847                 RETURN(rc);
2848         }
2849
2850         curspace = curinodes = 0;
2851         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2852                 int err;
2853                 tgt = lmv->tgts[i];
2854
2855                 if (tgt == NULL || tgt->ltd_exp == NULL || tgt->ltd_active == 0)
2856                         continue;
2857                 if (!tgt->ltd_active) {
2858                         CDEBUG(D_HA, "mdt %d is inactive.\n", i);
2859                         continue;
2860                 }
2861
2862                 err = obd_quotactl(tgt->ltd_exp, oqctl);
2863                 if (err) {
2864                         CERROR("getquota on mdt %d failed. %d\n", i, err);
2865                         if (!rc)
2866                                 rc = err;
2867                 } else {
2868                         curspace += oqctl->qc_dqblk.dqb_curspace;
2869                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
2870                 }
2871         }
2872         oqctl->qc_dqblk.dqb_curspace = curspace;
2873         oqctl->qc_dqblk.dqb_curinodes = curinodes;
2874
2875         RETURN(rc);
2876 }
2877
2878 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2879                    struct obd_quotactl *oqctl)
2880 {
2881         struct obd_device       *obd = class_exp2obd(exp);
2882         struct lmv_obd          *lmv = &obd->u.lmv;
2883         struct lmv_tgt_desc     *tgt;
2884         __u32                    i;
2885         int                      rc = 0;
2886         ENTRY;
2887
2888         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2889                 int err;
2890                 tgt = lmv->tgts[i];
2891                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2892                         CERROR("lmv idx %d inactive\n", i);
2893                         RETURN(-EIO);
2894                 }
2895
2896                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2897                 if (err && !rc)
2898                         rc = err;
2899         }
2900
2901         RETURN(rc);
2902 }
2903
2904 struct obd_ops lmv_obd_ops = {
2905         .o_owner                = THIS_MODULE,
2906         .o_setup                = lmv_setup,
2907         .o_cleanup              = lmv_cleanup,
2908         .o_precleanup           = lmv_precleanup,
2909         .o_process_config       = lmv_process_config,
2910         .o_connect              = lmv_connect,
2911         .o_disconnect           = lmv_disconnect,
2912         .o_statfs               = lmv_statfs,
2913         .o_get_info             = lmv_get_info,
2914         .o_set_info_async       = lmv_set_info_async,
2915         .o_packmd               = lmv_packmd,
2916         .o_unpackmd             = lmv_unpackmd,
2917         .o_notify               = lmv_notify,
2918         .o_get_uuid             = lmv_get_uuid,
2919         .o_iocontrol            = lmv_iocontrol,
2920         .o_quotacheck           = lmv_quotacheck,
2921         .o_quotactl             = lmv_quotactl
2922 };
2923
2924 struct md_ops lmv_md_ops = {
2925         .m_getstatus            = lmv_getstatus,
2926         .m_null_inode           = lmv_null_inode,
2927         .m_find_cbdata          = lmv_find_cbdata,
2928         .m_close                = lmv_close,
2929         .m_create               = lmv_create,
2930         .m_done_writing         = lmv_done_writing,
2931         .m_enqueue              = lmv_enqueue,
2932         .m_getattr              = lmv_getattr,
2933         .m_getxattr             = lmv_getxattr,
2934         .m_getattr_name         = lmv_getattr_name,
2935         .m_intent_lock          = lmv_intent_lock,
2936         .m_link                 = lmv_link,
2937         .m_rename               = lmv_rename,
2938         .m_setattr              = lmv_setattr,
2939         .m_setxattr             = lmv_setxattr,
2940         .m_fsync                = lmv_fsync,
2941         .m_readpage             = lmv_readpage,
2942         .m_unlink               = lmv_unlink,
2943         .m_init_ea_size         = lmv_init_ea_size,
2944         .m_cancel_unused        = lmv_cancel_unused,
2945         .m_set_lock_data        = lmv_set_lock_data,
2946         .m_lock_match           = lmv_lock_match,
2947         .m_get_lustre_md        = lmv_get_lustre_md,
2948         .m_free_lustre_md       = lmv_free_lustre_md,
2949         .m_set_open_replay_data = lmv_set_open_replay_data,
2950         .m_clear_open_replay_data = lmv_clear_open_replay_data,
2951         .m_renew_capa           = lmv_renew_capa,
2952         .m_unpack_capa          = lmv_unpack_capa,
2953         .m_get_remote_perm      = lmv_get_remote_perm,
2954         .m_intent_getattr_async = lmv_intent_getattr_async,
2955         .m_revalidate_lock      = lmv_revalidate_lock
2956 };
2957
2958 int __init lmv_init(void)
2959 {
2960         struct lprocfs_static_vars lvars;
2961         int                        rc;
2962
2963         lprocfs_lmv_init_vars(&lvars);
2964
2965         rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
2966                                  lvars.module_vars, LUSTRE_LMV_NAME, NULL);
2967         return rc;
2968 }
2969
2970 #ifdef __KERNEL__
2971 static void lmv_exit(void)
2972 {
2973         class_unregister_type(LUSTRE_LMV_NAME);
2974 }
2975
2976 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2977 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2978 MODULE_LICENSE("GPL");
2979
2980 module_init(lmv_init);
2981 module_exit(lmv_exit);
2982 #endif