Whamcloud - gitweb
LU-3319 procfs: move lmv proc handling over to seq_file
[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         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
138
139         return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
140 }
141
142 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
143                       enum obd_notify_event ev, void *data)
144 {
145         struct obd_connect_data *conn_data;
146         struct lmv_obd          *lmv = &obd->u.lmv;
147         struct obd_uuid         *uuid;
148         int                      rc = 0;
149         ENTRY;
150
151         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
152                 CERROR("unexpected notification of %s %s!\n",
153                        watched->obd_type->typ_name,
154                        watched->obd_name);
155                 RETURN(-EINVAL);
156         }
157
158         uuid = &watched->u.cli.cl_target_uuid;
159         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
160                 /*
161                  * Set MDC as active before notifying the observer, so the
162                  * observer can use the MDC normally.
163                  */
164                 rc = lmv_set_mdc_active(lmv, uuid,
165                                         ev == OBD_NOTIFY_ACTIVE);
166                 if (rc) {
167                         CERROR("%sactivation of %s failed: %d\n",
168                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
169                                uuid->uuid, rc);
170                         RETURN(rc);
171                 }
172         } else if (ev == OBD_NOTIFY_OCD) {
173                 conn_data = &watched->u.cli.cl_import->imp_connect_data;
174                 /*
175                  * XXX: Make sure that ocd_connect_flags from all targets are
176                  * the same. Otherwise one of MDTs runs wrong version or
177                  * something like this.  --umka
178                  */
179                 obd->obd_self_export->exp_connect_data = *conn_data;
180         }
181 #if 0
182         else if (ev == OBD_NOTIFY_DISCON) {
183                 /*
184                  * For disconnect event, flush fld cache for failout MDS case.
185                  */
186                 fld_client_flush(&lmv->lmv_fld);
187         }
188 #endif
189         /*
190          * Pass the notification up the chain.
191          */
192         if (obd->obd_observer)
193                 rc = obd_notify(obd->obd_observer, watched, ev, data);
194
195         RETURN(rc);
196 }
197
198 /**
199  * This is fake connect function. Its purpose is to initialize lmv and say
200  * caller that everything is okay. Real connection will be performed later.
201  */
202 static int lmv_connect(const struct lu_env *env,
203                        struct obd_export **exp, struct obd_device *obd,
204                        struct obd_uuid *cluuid, struct obd_connect_data *data,
205                        void *localdata)
206 {
207 #ifdef __KERNEL__
208         struct proc_dir_entry *lmv_proc_dir;
209 #endif
210         struct lmv_obd        *lmv = &obd->u.lmv;
211         struct lustre_handle  conn = { 0 };
212         int                    rc = 0;
213         ENTRY;
214
215         /*
216          * We don't want to actually do the underlying connections more than
217          * once, so keep track.
218          */
219         lmv->refcount++;
220         if (lmv->refcount > 1) {
221                 *exp = NULL;
222                 RETURN(0);
223         }
224
225         rc = class_connect(&conn, obd, cluuid);
226         if (rc) {
227                 CERROR("class_connection() returned %d\n", rc);
228                 RETURN(rc);
229         }
230
231         *exp = class_conn2export(&conn);
232         class_export_get(*exp);
233
234         lmv->exp = *exp;
235         lmv->connected = 0;
236         lmv->cluuid = *cluuid;
237
238         if (data)
239                 lmv->conn_data = *data;
240
241 #ifdef __KERNEL__
242         if (obd->obd_proc_private != NULL) {
243                 lmv_proc_dir = obd->obd_proc_private;
244         } else {
245                 lmv_proc_dir = lprocfs_seq_register("target_obds",
246                                                     obd->obd_proc_entry,
247                                                     NULL, NULL);
248                 if (IS_ERR(lmv_proc_dir)) {
249                         CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
250                                obd->obd_type->typ_name, obd->obd_name);
251                         lmv_proc_dir = NULL;
252                 }
253                 obd->obd_proc_private = lmv_proc_dir;
254         }
255 #endif
256
257         /*
258          * All real clients should perform actual connection right away, because
259          * it is possible, that LMV will not have opportunity to connect targets
260          * and MDC stuff will be called directly, for instance while reading
261          * ../mdc/../kbytesfree procfs file, etc.
262          */
263         if (data != NULL && (data->ocd_connect_flags & OBD_CONNECT_REAL))
264                 rc = lmv_check_connect(obd);
265
266 #ifdef __KERNEL__
267         if (rc && lmv_proc_dir) {
268                 lprocfs_remove(&lmv_proc_dir);
269                 obd->obd_proc_private = NULL;
270         }
271 #endif
272         RETURN(rc);
273 }
274
275 static void lmv_set_timeouts(struct obd_device *obd)
276 {
277         struct lmv_obd          *lmv;
278         __u32                    i;
279
280         lmv = &obd->u.lmv;
281         if (lmv->server_timeout == 0)
282                 return;
283
284         if (lmv->connected == 0)
285                 return;
286
287         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
288                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
289
290                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
291                         continue;
292
293                 obd_set_info_async(NULL, tgt->ltd_exp, sizeof(KEY_INTERMDS),
294                                    KEY_INTERMDS, 0, NULL, NULL);
295         }
296 }
297
298 static int lmv_init_ea_size(struct obd_export *exp, int easize,
299                             int def_easize, int cookiesize)
300 {
301         struct obd_device       *obd = exp->exp_obd;
302         struct lmv_obd          *lmv = &obd->u.lmv;
303         __u32                    i;
304         int                      rc = 0;
305         int                      change = 0;
306         ENTRY;
307
308         if (lmv->max_easize < easize) {
309                 lmv->max_easize = easize;
310                 change = 1;
311         }
312         if (lmv->max_def_easize < def_easize) {
313                 lmv->max_def_easize = def_easize;
314                 change = 1;
315         }
316         if (lmv->max_cookiesize < cookiesize) {
317                 lmv->max_cookiesize = cookiesize;
318                 change = 1;
319         }
320         if (change == 0)
321                 RETURN(0);
322
323         if (lmv->connected == 0)
324                 RETURN(0);
325
326         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
327                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
328
329                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
330                         CWARN("%s: NULL export for %d\n", obd->obd_name, i);
331                         continue;
332                 }
333
334                 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize,
335                                      cookiesize);
336                 if (rc) {
337                         CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
338                                " rc = %d.\n", obd->obd_name, i, rc);
339                         break;
340                 }
341         }
342         RETURN(rc);
343 }
344
345 #define MAX_STRING_SIZE 128
346
347 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
348 {
349 #ifdef __KERNEL__
350         struct proc_dir_entry   *lmv_proc_dir;
351 #endif
352         struct lmv_obd          *lmv = &obd->u.lmv;
353         struct obd_uuid         *cluuid = &lmv->cluuid;
354         struct obd_uuid          lmv_mdc_uuid = { "LMV_MDC_UUID" };
355         struct obd_device       *mdc_obd;
356         struct obd_export       *mdc_exp;
357         struct lu_fld_target     target;
358         int                      rc;
359         ENTRY;
360
361         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
362                                         &obd->obd_uuid);
363         if (!mdc_obd) {
364                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
365                 RETURN(-EINVAL);
366         }
367
368         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
369                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
370                 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
371                 cluuid->uuid);
372
373         if (!mdc_obd->obd_set_up) {
374                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
375                 RETURN(-EINVAL);
376         }
377
378         rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
379                          &lmv->conn_data, NULL);
380         if (rc) {
381                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
382                 RETURN(rc);
383         }
384
385         /*
386          * Init fid sequence client for this mdc and add new fld target.
387          */
388         rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
389         if (rc)
390                 RETURN(rc);
391
392         target.ft_srv = NULL;
393         target.ft_exp = mdc_exp;
394         target.ft_idx = tgt->ltd_idx;
395
396         fld_client_add_target(&lmv->lmv_fld, &target);
397
398         rc = obd_register_observer(mdc_obd, obd);
399         if (rc) {
400                 obd_disconnect(mdc_exp);
401                 CERROR("target %s register_observer error %d\n",
402                        tgt->ltd_uuid.uuid, rc);
403                 RETURN(rc);
404         }
405
406         if (obd->obd_observer) {
407                 /*
408                  * Tell the observer about the new target.
409                  */
410                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
411                                 OBD_NOTIFY_ACTIVE,
412                                 (void *)(tgt - lmv->tgts[0]));
413                 if (rc) {
414                         obd_disconnect(mdc_exp);
415                         RETURN(rc);
416                 }
417         }
418
419         tgt->ltd_active = 1;
420         tgt->ltd_exp = mdc_exp;
421         lmv->desc.ld_active_tgt_count++;
422
423         md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
424                         lmv->max_def_easize, lmv->max_cookiesize);
425
426         CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
427                 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
428                 cfs_atomic_read(&obd->obd_refcount));
429
430 #ifdef __KERNEL__
431         lmv_proc_dir = obd->obd_proc_private;
432         if (lmv_proc_dir) {
433                 struct proc_dir_entry *mdc_symlink;
434
435                 LASSERT(mdc_obd->obd_type != NULL);
436                 LASSERT(mdc_obd->obd_type->typ_name != NULL);
437                 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
438                                                   lmv_proc_dir,
439                                                   "../../../%s/%s",
440                                                   mdc_obd->obd_type->typ_name,
441                                                   mdc_obd->obd_name);
442                 if (mdc_symlink == NULL) {
443                         CERROR("Could not register LMV target "
444                                "/proc/fs/lustre/%s/%s/target_obds/%s.",
445                                obd->obd_type->typ_name, obd->obd_name,
446                                mdc_obd->obd_name);
447                         lprocfs_remove(&lmv_proc_dir);
448                         obd->obd_proc_private = NULL;
449                 }
450         }
451 #endif
452         RETURN(0);
453 }
454
455 static void lmv_del_target(struct lmv_obd *lmv, int index)
456 {
457         if (lmv->tgts[index] == NULL)
458                 return;
459
460         OBD_FREE_PTR(lmv->tgts[index]);
461         lmv->tgts[index] = NULL;
462         return;
463 }
464
465 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
466                            __u32 index, int gen)
467 {
468         struct lmv_obd      *lmv = &obd->u.lmv;
469         struct lmv_tgt_desc *tgt;
470         int                  rc = 0;
471         ENTRY;
472
473         CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
474
475         lmv_init_lock(lmv);
476
477         if (lmv->desc.ld_tgt_count == 0) {
478                 struct obd_device *mdc_obd;
479
480                 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
481                                                 &obd->obd_uuid);
482                 if (!mdc_obd) {
483                         lmv_init_unlock(lmv);
484                         CERROR("%s: Target %s not attached: rc = %d\n",
485                                obd->obd_name, uuidp->uuid, -EINVAL);
486                         RETURN(-EINVAL);
487                 }
488         }
489
490         if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
491                 tgt = lmv->tgts[index];
492                 CERROR("%s: UUID %s already assigned at LOV target index %d:"
493                        " rc = %d\n", obd->obd_name,
494                        obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
495                 lmv_init_unlock(lmv);
496                 RETURN(-EEXIST);
497         }
498
499         if (index >= lmv->tgts_size) {
500                 /* We need to reallocate the lmv target array. */
501                 struct lmv_tgt_desc **newtgts, **old = NULL;
502                 __u32 newsize = 1;
503                 __u32 oldsize = 0;
504
505                 while (newsize < index + 1)
506                         newsize = newsize << 1;
507                 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
508                 if (newtgts == NULL) {
509                         lmv_init_unlock(lmv);
510                         RETURN(-ENOMEM);
511                 }
512
513                 if (lmv->tgts_size) {
514                         memcpy(newtgts, lmv->tgts,
515                                sizeof(*newtgts) * lmv->tgts_size);
516                         old = lmv->tgts;
517                         oldsize = lmv->tgts_size;
518                 }
519
520                 lmv->tgts = newtgts;
521                 lmv->tgts_size = newsize;
522                 smp_rmb();
523                 if (old)
524                         OBD_FREE(old, sizeof(*old) * oldsize);
525
526                 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
527                        lmv->tgts_size);
528         }
529
530         OBD_ALLOC_PTR(tgt);
531         if (!tgt) {
532                 lmv_init_unlock(lmv);
533                 RETURN(-ENOMEM);
534         }
535
536         mutex_init(&tgt->ltd_fid_mutex);
537         tgt->ltd_idx = index;
538         tgt->ltd_uuid = *uuidp;
539         tgt->ltd_active = 0;
540         lmv->tgts[index] = tgt;
541         if (index >= lmv->desc.ld_tgt_count)
542                 lmv->desc.ld_tgt_count = index + 1;
543
544         if (lmv->connected) {
545                 rc = lmv_connect_mdc(obd, tgt);
546                 if (rc) {
547                         spin_lock(&lmv->lmv_lock);
548                         lmv->desc.ld_tgt_count--;
549                         memset(tgt, 0, sizeof(*tgt));
550                         spin_unlock(&lmv->lmv_lock);
551                 } else {
552                         int easize = sizeof(struct lmv_stripe_md) +
553                                      lmv->desc.ld_tgt_count *
554                                      sizeof(struct lu_fid);
555                         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
556                 }
557         }
558
559         lmv_init_unlock(lmv);
560         RETURN(rc);
561 }
562
563 int lmv_check_connect(struct obd_device *obd)
564 {
565         struct lmv_obd          *lmv = &obd->u.lmv;
566         struct lmv_tgt_desc     *tgt;
567         __u32                    i;
568         int                      rc;
569         int                      easize;
570         ENTRY;
571
572         if (lmv->connected)
573                 RETURN(0);
574
575         lmv_init_lock(lmv);
576         if (lmv->connected) {
577                 lmv_init_unlock(lmv);
578                 RETURN(0);
579         }
580
581         if (lmv->desc.ld_tgt_count == 0) {
582                 lmv_init_unlock(lmv);
583                 CERROR("%s: no targets configured.\n", obd->obd_name);
584                 RETURN(-EINVAL);
585         }
586
587         LASSERT(lmv->tgts != NULL);
588
589         if (lmv->tgts[0] == NULL) {
590                 lmv_init_unlock(lmv);
591                 CERROR("%s: no target configured for index 0.\n",
592                        obd->obd_name);
593                 RETURN(-EINVAL);
594         }
595
596         CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
597                lmv->cluuid.uuid, obd->obd_name);
598
599         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
600                 tgt = lmv->tgts[i];
601                 if (tgt == NULL)
602                         continue;
603                 rc = lmv_connect_mdc(obd, tgt);
604                 if (rc)
605                         GOTO(out_disc, rc);
606         }
607
608         lmv_set_timeouts(obd);
609         class_export_put(lmv->exp);
610         lmv->connected = 1;
611         easize = lmv_get_easize(lmv);
612         lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
613         lmv_init_unlock(lmv);
614         RETURN(0);
615
616  out_disc:
617         while (i-- > 0) {
618                 int rc2;
619                 tgt = lmv->tgts[i];
620                 if (tgt == NULL)
621                         continue;
622                 tgt->ltd_active = 0;
623                 if (tgt->ltd_exp) {
624                         --lmv->desc.ld_active_tgt_count;
625                         rc2 = obd_disconnect(tgt->ltd_exp);
626                         if (rc2) {
627                                 CERROR("LMV target %s disconnect on "
628                                        "MDC idx %d: error %d\n",
629                                        tgt->ltd_uuid.uuid, i, rc2);
630                         }
631                 }
632         }
633         class_disconnect(lmv->exp);
634         lmv_init_unlock(lmv);
635         RETURN(rc);
636 }
637
638 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
639 {
640 #ifdef __KERNEL__
641         struct proc_dir_entry  *lmv_proc_dir;
642 #endif
643         struct lmv_obd         *lmv = &obd->u.lmv;
644         struct obd_device      *mdc_obd;
645         int                     rc;
646         ENTRY;
647
648         LASSERT(tgt != NULL);
649         LASSERT(obd != NULL);
650
651         mdc_obd = class_exp2obd(tgt->ltd_exp);
652
653         if (mdc_obd) {
654                 mdc_obd->obd_force = obd->obd_force;
655                 mdc_obd->obd_fail = obd->obd_fail;
656                 mdc_obd->obd_no_recov = obd->obd_no_recov;
657         }
658
659 #ifdef __KERNEL__
660         lmv_proc_dir = obd->obd_proc_private;
661         if (lmv_proc_dir)
662                 lprocfs_remove_proc_entry(mdc_obd->obd_name, lmv_proc_dir);
663 #endif
664         rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
665         if (rc)
666                 CERROR("Can't finanize fids factory\n");
667
668         CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
669                tgt->ltd_exp->exp_obd->obd_name,
670                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
671
672         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
673         rc = obd_disconnect(tgt->ltd_exp);
674         if (rc) {
675                 if (tgt->ltd_active) {
676                         CERROR("Target %s disconnect error %d\n",
677                                tgt->ltd_uuid.uuid, rc);
678                 }
679         }
680
681         lmv_activate_target(lmv, tgt, 0);
682         tgt->ltd_exp = NULL;
683         RETURN(0);
684 }
685
686 static int lmv_disconnect(struct obd_export *exp)
687 {
688         struct obd_device       *obd = class_exp2obd(exp);
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         if (obd->obd_proc_private)
713                 lprocfs_remove((struct proc_dir_entry **)&obd->obd_proc_private);
714         else
715                 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
716                        obd->obd_type->typ_name, obd->obd_name);
717 #endif
718
719 out_local:
720         /*
721          * This is the case when no real connection is established by
722          * lmv_check_connect().
723          */
724         if (!lmv->connected)
725                 class_export_put(exp);
726         rc = class_disconnect(exp);
727         if (lmv->refcount == 0)
728                 lmv->connected = 0;
729         RETURN(rc);
730 }
731
732 static int lmv_fid2path(struct obd_export *exp, int len, void *karg, void *uarg)
733 {
734         struct obd_device       *obddev = class_exp2obd(exp);
735         struct lmv_obd          *lmv = &obddev->u.lmv;
736         struct getinfo_fid2path *gf;
737         struct lmv_tgt_desc     *tgt;
738         struct getinfo_fid2path *remote_gf = NULL;
739         int                     remote_gf_size = 0;
740         int                     rc;
741
742         gf = (struct getinfo_fid2path *)karg;
743         tgt = lmv_find_target(lmv, &gf->gf_fid);
744         if (IS_ERR(tgt))
745                 RETURN(PTR_ERR(tgt));
746
747 repeat_fid2path:
748         rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
749         if (rc != 0 && rc != -EREMOTE)
750                 GOTO(out_fid2path, rc);
751
752         /* If remote_gf != NULL, it means just building the
753          * path on the remote MDT, copy this path segement to gf */
754         if (remote_gf != NULL) {
755                 struct getinfo_fid2path *ori_gf;
756                 char *ptr;
757
758                 ori_gf = (struct getinfo_fid2path *)karg;
759                 if (strlen(ori_gf->gf_path) +
760                     strlen(gf->gf_path) > ori_gf->gf_pathlen)
761                         GOTO(out_fid2path, rc = -EOVERFLOW);
762
763                 ptr = ori_gf->gf_path;
764
765                 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
766                         strlen(ori_gf->gf_path));
767
768                 strncpy(ptr, gf->gf_path, strlen(gf->gf_path));
769                 ptr += strlen(gf->gf_path);
770                 *ptr = '/';
771         }
772
773         CDEBUG(D_INFO, "%s: get path %s "DFID" rec: "LPU64" ln: %u\n",
774                tgt->ltd_exp->exp_obd->obd_name,
775                gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
776                gf->gf_linkno);
777
778         if (rc == 0)
779                 GOTO(out_fid2path, rc);
780
781         /* sigh, has to go to another MDT to do path building further */
782         if (remote_gf == NULL) {
783                 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
784                 OBD_ALLOC(remote_gf, remote_gf_size);
785                 if (remote_gf == NULL)
786                         GOTO(out_fid2path, rc = -ENOMEM);
787                 remote_gf->gf_pathlen = PATH_MAX;
788         }
789
790         if (!fid_is_sane(&gf->gf_fid)) {
791                 CERROR("%s: invalid FID "DFID": rc = %d\n",
792                        tgt->ltd_exp->exp_obd->obd_name,
793                        PFID(&gf->gf_fid), -EINVAL);
794                 GOTO(out_fid2path, rc = -EINVAL);
795         }
796
797         tgt = lmv_find_target(lmv, &gf->gf_fid);
798         if (IS_ERR(tgt))
799                 GOTO(out_fid2path, rc = -EINVAL);
800
801         remote_gf->gf_fid = gf->gf_fid;
802         remote_gf->gf_recno = -1;
803         remote_gf->gf_linkno = -1;
804         memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
805         gf = remote_gf;
806         goto repeat_fid2path;
807
808 out_fid2path:
809         if (remote_gf != NULL)
810                 OBD_FREE(remote_gf, remote_gf_size);
811         RETURN(rc);
812 }
813
814 static int lmv_hsm_req_count(struct lmv_obd *lmv,
815                              const struct hsm_user_request *hur,
816                              const struct lmv_tgt_desc *tgt_mds)
817 {
818         __u32                    i;
819         int                      nr = 0;
820         struct lmv_tgt_desc     *curr_tgt;
821
822         /* count how many requests must be sent to the given target */
823         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
824                 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
825                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
826                         nr++;
827         }
828         return nr;
829 }
830
831 static void lmv_hsm_req_build(struct lmv_obd *lmv,
832                               struct hsm_user_request *hur_in,
833                               const struct lmv_tgt_desc *tgt_mds,
834                               struct hsm_user_request *hur_out)
835 {
836         __u32                    i, nr_out;
837         struct lmv_tgt_desc     *curr_tgt;
838
839         /* build the hsm_user_request for the given target */
840         hur_out->hur_request = hur_in->hur_request;
841         nr_out = 0;
842         for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
843                 curr_tgt = lmv_find_target(lmv,
844                                            &hur_in->hur_user_item[i].hui_fid);
845                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
846                         hur_out->hur_user_item[nr_out] =
847                                                 hur_in->hur_user_item[i];
848                         nr_out++;
849                 }
850         }
851         hur_out->hur_request.hr_itemcount = nr_out;
852         memcpy(hur_data(hur_out), hur_data(hur_in),
853                hur_in->hur_request.hr_data_len);
854 }
855
856 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
857                                  struct lustre_kernelcomm *lk, void *uarg)
858 {
859         __u32                    i;
860         int                      rc;
861         struct kkuc_ct_data     *kcd = NULL;
862         ENTRY;
863
864         /* unregister request (call from llapi_hsm_copytool_fini) */
865         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
866                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
867
868                 if (tgt == NULL || tgt->ltd_exp == NULL)
869                         continue;
870                 /* best effort: try to clean as much as possible
871                  * (continue on error) */
872                 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
873         }
874
875         /* Whatever the result, remove copytool from kuc groups.
876          * Unreached coordinators will get EPIPE on next requests
877          * and will unregister automatically.
878          */
879         rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group, (void **)&kcd);
880         if (kcd != NULL)
881                 OBD_FREE_PTR(kcd);
882
883         RETURN(rc);
884 }
885
886 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
887                                struct lustre_kernelcomm *lk, void *uarg)
888 {
889         struct file             *filp;
890         __u32                    i, j;
891         int                      err, rc;
892         bool                     any_set = false;
893         struct kkuc_ct_data     *kcd;
894         ENTRY;
895
896         /* All or nothing: try to register to all MDS.
897          * In case of failure, unregister from previous MDS,
898          * except if it because of inactive target. */
899         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
900                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
901
902                 if (tgt == NULL || tgt->ltd_exp == NULL)
903                         continue;
904                 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
905                 if (err) {
906                         if (tgt->ltd_active) {
907                                 /* permanent error */
908                                 CERROR("%s: iocontrol MDC %s on MDT"
909                                        " idx %d cmd %x: err = %d\n",
910                                        class_exp2obd(lmv->exp)->obd_name,
911                                        tgt->ltd_uuid.uuid, i, cmd, err);
912                                 rc = err;
913                                 lk->lk_flags |= LK_FLG_STOP;
914                                 /* unregister from previous MDS */
915                                 for (j = 0; j < i; j++) {
916                                         tgt = lmv->tgts[j];
917                                         if (tgt == NULL || tgt->ltd_exp == NULL)
918                                                 continue;
919                                         obd_iocontrol(cmd, tgt->ltd_exp, len,
920                                                       lk, uarg);
921                                 }
922                                 RETURN(rc);
923                         }
924                         /* else: transient error.
925                          * kuc will register to the missing MDT
926                          * when it is back */
927                 } else {
928                         any_set = true;
929                 }
930         }
931
932         if (!any_set)
933                 /* no registration done: return error */
934                 RETURN(-ENOTCONN);
935
936         /* at least one registration done, with no failure */
937         filp = fget(lk->lk_wfd);
938         if (filp == NULL)
939                 RETURN(-EBADF);
940
941         OBD_ALLOC_PTR(kcd);
942         if (kcd == NULL) {
943                 fput(filp);
944                 RETURN(-ENOMEM);
945         }
946         kcd->kcd_magic = KKUC_CT_DATA_MAGIC;
947         kcd->kcd_uuid = lmv->cluuid;
948         kcd->kcd_archive = lk->lk_data;
949
950         rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group, kcd);
951         if (rc != 0) {
952                 if (filp != NULL)
953                         fput(filp);
954                 OBD_FREE_PTR(kcd);
955         }
956
957         RETURN(rc);
958 }
959
960
961
962
963 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
964                          int len, void *karg, void *uarg)
965 {
966         struct obd_device       *obddev = class_exp2obd(exp);
967         struct lmv_obd          *lmv = &obddev->u.lmv;
968         struct lmv_tgt_desc     *tgt = NULL;
969         __u32                    i = 0;
970         int                      rc = 0;
971         int                      set = 0;
972         __u32                    count = lmv->desc.ld_tgt_count;
973         ENTRY;
974
975         if (count == 0)
976                 RETURN(-ENOTTY);
977
978         switch (cmd) {
979         case IOC_OBD_STATFS: {
980                 struct obd_ioctl_data *data = karg;
981                 struct obd_device *mdc_obd;
982                 struct obd_statfs stat_buf = {0};
983                 __u32 index;
984
985                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
986                 if ((index >= count))
987                         RETURN(-ENODEV);
988
989                 tgt = lmv->tgts[index];
990                 if (tgt == NULL || !tgt->ltd_active)
991                         RETURN(-ENODATA);
992
993                 mdc_obd = class_exp2obd(tgt->ltd_exp);
994                 if (!mdc_obd)
995                         RETURN(-EINVAL);
996
997                 /* copy UUID */
998                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
999                                  min((int) data->ioc_plen2,
1000                                      (int) sizeof(struct obd_uuid))))
1001                         RETURN(-EFAULT);
1002
1003                 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
1004                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1005                                 0);
1006                 if (rc)
1007                         RETURN(rc);
1008                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1009                                  min((int) data->ioc_plen1,
1010                                      (int) sizeof(stat_buf))))
1011                         RETURN(-EFAULT);
1012                 break;
1013         }
1014         case OBD_IOC_QUOTACTL: {
1015                 struct if_quotactl *qctl = karg;
1016                 struct obd_quotactl *oqctl;
1017
1018                 if (qctl->qc_valid == QC_MDTIDX) {
1019                         if (count <= qctl->qc_idx)
1020                                 RETURN(-EINVAL);
1021
1022                         tgt = lmv->tgts[qctl->qc_idx];
1023                         if (tgt == NULL || tgt->ltd_exp == NULL)
1024                                 RETURN(-EINVAL);
1025                 } else if (qctl->qc_valid == QC_UUID) {
1026                         for (i = 0; i < count; i++) {
1027                                 tgt = lmv->tgts[i];
1028                                 if (tgt == NULL)
1029                                         continue;
1030                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
1031                                                      &qctl->obd_uuid))
1032                                         continue;
1033
1034                                 if (tgt->ltd_exp == NULL)
1035                                         RETURN(-EINVAL);
1036
1037                                 break;
1038                         }
1039                 } else {
1040                         RETURN(-EINVAL);
1041                 }
1042
1043                 if (i >= count)
1044                         RETURN(-EAGAIN);
1045
1046                 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
1047                 OBD_ALLOC_PTR(oqctl);
1048                 if (!oqctl)
1049                         RETURN(-ENOMEM);
1050
1051                 QCTL_COPY(oqctl, qctl);
1052                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1053                 if (rc == 0) {
1054                         QCTL_COPY(qctl, oqctl);
1055                         qctl->qc_valid = QC_MDTIDX;
1056                         qctl->obd_uuid = tgt->ltd_uuid;
1057                 }
1058                 OBD_FREE_PTR(oqctl);
1059                 break;
1060         }
1061         case OBD_IOC_CHANGELOG_SEND:
1062         case OBD_IOC_CHANGELOG_CLEAR: {
1063                 struct ioc_changelog *icc = karg;
1064
1065                 if (icc->icc_mdtindex >= count)
1066                         RETURN(-ENODEV);
1067
1068                 tgt = lmv->tgts[icc->icc_mdtindex];
1069                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
1070                         RETURN(-ENODEV);
1071                 rc = obd_iocontrol(cmd, tgt->ltd_exp, sizeof(*icc), icc, NULL);
1072                 break;
1073         }
1074         case LL_IOC_GET_CONNECT_FLAGS: {
1075                 tgt = lmv->tgts[0];
1076                 if (tgt == NULL || tgt->ltd_exp == NULL)
1077                         RETURN(-ENODATA);
1078                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1079                 break;
1080         }
1081         case OBD_IOC_FID2PATH: {
1082                 rc = lmv_fid2path(exp, len, karg, uarg);
1083                 break;
1084         }
1085         case LL_IOC_HSM_STATE_GET:
1086         case LL_IOC_HSM_STATE_SET:
1087         case LL_IOC_HSM_ACTION: {
1088                 struct md_op_data       *op_data = karg;
1089
1090                 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1091                 if (IS_ERR(tgt))
1092                         RETURN(PTR_ERR(tgt));
1093
1094                 if (tgt->ltd_exp == NULL)
1095                         RETURN(-EINVAL);
1096
1097                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1098                 break;
1099         }
1100         case LL_IOC_HSM_PROGRESS: {
1101                 const struct hsm_progress_kernel *hpk = karg;
1102
1103                 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1104                 if (IS_ERR(tgt))
1105                         RETURN(PTR_ERR(tgt));
1106                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1107                 break;
1108         }
1109         case LL_IOC_HSM_REQUEST: {
1110                 struct hsm_user_request *hur = karg;
1111                 unsigned int reqcount = hur->hur_request.hr_itemcount;
1112
1113                 if (reqcount == 0)
1114                         RETURN(0);
1115
1116                 /* if the request is about a single fid
1117                  * or if there is a single MDS, no need to split
1118                  * the request. */
1119                 if (reqcount == 1 || count == 1) {
1120                         tgt = lmv_find_target(lmv,
1121                                               &hur->hur_user_item[0].hui_fid);
1122                         if (IS_ERR(tgt))
1123                                 RETURN(PTR_ERR(tgt));
1124                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1125                 } else {
1126                         /* split fid list to their respective MDS */
1127                         for (i = 0; i < count; i++) {
1128                                 unsigned int            nr, reqlen;
1129                                 int                     rc1;
1130                                 struct hsm_user_request *req;
1131
1132                                 tgt = lmv->tgts[i];
1133                                 if (tgt == NULL || tgt->ltd_exp == NULL)
1134                                         continue;
1135
1136                                 nr = lmv_hsm_req_count(lmv, hur, tgt);
1137                                 if (nr == 0) /* nothing for this MDS */
1138                                         continue;
1139
1140                                 /* build a request with fids for this MDS */
1141                                 reqlen = offsetof(typeof(*hur),
1142                                                   hur_user_item[nr])
1143                                                 + hur->hur_request.hr_data_len;
1144                                 OBD_ALLOC_LARGE(req, reqlen);
1145                                 if (req == NULL)
1146                                         RETURN(-ENOMEM);
1147
1148                                 lmv_hsm_req_build(lmv, hur, tgt, req);
1149
1150                                 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1151                                                     req, uarg);
1152                                 if (rc1 != 0 && rc == 0)
1153                                         rc = rc1;
1154                                 OBD_FREE_LARGE(req, reqlen);
1155                         }
1156                 }
1157                 break;
1158         }
1159         case LL_IOC_LOV_SWAP_LAYOUTS: {
1160                 struct md_op_data       *op_data = karg;
1161                 struct lmv_tgt_desc     *tgt1, *tgt2;
1162
1163                 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1164                 if (IS_ERR(tgt1))
1165                         RETURN(PTR_ERR(tgt1));
1166
1167                 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1168                 if (IS_ERR(tgt2))
1169                         RETURN(PTR_ERR(tgt2));
1170
1171                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1172                         RETURN(-EINVAL);
1173
1174                 /* only files on same MDT can have their layouts swapped */
1175                 if (tgt1->ltd_idx != tgt2->ltd_idx)
1176                         RETURN(-EPERM);
1177
1178                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1179                 break;
1180         }
1181         case LL_IOC_HSM_CT_START: {
1182                 struct lustre_kernelcomm *lk = karg;
1183                 if (lk->lk_flags & LK_FLG_STOP)
1184                         rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1185                 else
1186                         rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1187                 break;
1188         }
1189         default:
1190                 for (i = 0; i < count; i++) {
1191                         struct obd_device *mdc_obd;
1192                         int err;
1193
1194                         tgt = lmv->tgts[i];
1195                         if (tgt == NULL || tgt->ltd_exp == NULL)
1196                                 continue;
1197                         /* ll_umount_begin() sets force flag but for lmv, not
1198                          * mdc. Let's pass it through */
1199                         mdc_obd = class_exp2obd(tgt->ltd_exp);
1200                         mdc_obd->obd_force = obddev->obd_force;
1201                         err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1202                         if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
1203                                 RETURN(err);
1204                         } else if (err) {
1205                                 if (tgt->ltd_active) {
1206                                         CERROR("error: iocontrol MDC %s on MDT"
1207                                                " idx %d cmd %x: err = %d\n",
1208                                                tgt->ltd_uuid.uuid, i, cmd, err);
1209                                         if (!rc)
1210                                                 rc = err;
1211                                 }
1212                         } else
1213                                 set = 1;
1214                 }
1215                 if (!set && !rc)
1216                         rc = -EIO;
1217         }
1218         RETURN(rc);
1219 }
1220
1221 #if 0
1222 static int lmv_all_chars_policy(int count, const char *name,
1223                                 int len)
1224 {
1225         unsigned int c = 0;
1226
1227         while (len > 0)
1228                 c += name[--len];
1229         c = c % count;
1230         return c;
1231 }
1232
1233 static int lmv_nid_policy(struct lmv_obd *lmv)
1234 {
1235         struct obd_import *imp;
1236         __u32              id;
1237
1238         /*
1239          * XXX: To get nid we assume that underlying obd device is mdc.
1240          */
1241         imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
1242         id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
1243         return id % lmv->desc.ld_tgt_count;
1244 }
1245
1246 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1247                           placement_policy_t placement)
1248 {
1249         switch (placement) {
1250         case PLACEMENT_CHAR_POLICY:
1251                 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
1252                                             op_data->op_name,
1253                                             op_data->op_namelen);
1254         case PLACEMENT_NID_POLICY:
1255                 return lmv_nid_policy(lmv);
1256
1257         default:
1258                 break;
1259         }
1260
1261         CERROR("Unsupported placement policy %x\n", placement);
1262         return -EINVAL;
1263 }
1264 #endif
1265
1266 /**
1267  * This is _inode_ placement policy function (not name).
1268  */
1269 static int lmv_placement_policy(struct obd_device *obd,
1270                                 struct md_op_data *op_data,
1271                                 mdsno_t *mds)
1272 {
1273         struct lmv_obd          *lmv = &obd->u.lmv;
1274         ENTRY;
1275
1276         LASSERT(mds != NULL);
1277
1278         if (lmv->desc.ld_tgt_count == 1) {
1279                 *mds = 0;
1280                 RETURN(0);
1281         }
1282
1283         /**
1284          * If stripe_offset is provided during setdirstripe
1285          * (setdirstripe -i xx), xx MDS will be choosen.
1286          */
1287         if (op_data->op_cli_flags & CLI_SET_MEA) {
1288                 struct lmv_user_md *lum;
1289
1290                 lum = (struct lmv_user_md *)op_data->op_data;
1291                 if (lum->lum_type == LMV_STRIPE_TYPE &&
1292                     lum->lum_stripe_offset != -1) {
1293                         if (lum->lum_stripe_offset >= lmv->desc.ld_tgt_count) {
1294                                 CERROR("%s: Stripe_offset %d > MDT count %d:"
1295                                        " rc = %d\n", obd->obd_name,
1296                                        lum->lum_stripe_offset,
1297                                        lmv->desc.ld_tgt_count, -ERANGE);
1298                                 RETURN(-ERANGE);
1299                         }
1300                         *mds = lum->lum_stripe_offset;
1301                         RETURN(0);
1302                 }
1303         }
1304
1305         /* Allocate new fid on target according to operation type and parent
1306          * home mds. */
1307         *mds = op_data->op_mds;
1308         RETURN(0);
1309 }
1310
1311 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
1312                     mdsno_t mds)
1313 {
1314         struct lmv_tgt_desc     *tgt;
1315         int                      rc;
1316         ENTRY;
1317
1318         tgt = lmv_get_target(lmv, mds);
1319         if (IS_ERR(tgt))
1320                 RETURN(PTR_ERR(tgt));
1321
1322         /*
1323          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1324          * on server that seq in new allocated fid is not yet known.
1325          */
1326         mutex_lock(&tgt->ltd_fid_mutex);
1327
1328         if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1329                 GOTO(out, rc = -ENODEV);
1330
1331         /*
1332          * Asking underlaying tgt layer to allocate new fid.
1333          */
1334         rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1335         if (rc > 0) {
1336                 LASSERT(fid_is_sane(fid));
1337                 rc = 0;
1338         }
1339
1340         EXIT;
1341 out:
1342         mutex_unlock(&tgt->ltd_fid_mutex);
1343         return rc;
1344 }
1345
1346 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1347                   struct md_op_data *op_data)
1348 {
1349         struct obd_device     *obd = class_exp2obd(exp);
1350         struct lmv_obd        *lmv = &obd->u.lmv;
1351         mdsno_t                mds = 0;
1352         int                    rc;
1353         ENTRY;
1354
1355         LASSERT(op_data != NULL);
1356         LASSERT(fid != NULL);
1357
1358         rc = lmv_placement_policy(obd, op_data, &mds);
1359         if (rc) {
1360                 CERROR("Can't get target for allocating fid, "
1361                        "rc %d\n", rc);
1362                 RETURN(rc);
1363         }
1364
1365         rc = __lmv_fid_alloc(lmv, fid, mds);
1366         if (rc) {
1367                 CERROR("Can't alloc new fid, rc %d\n", rc);
1368                 RETURN(rc);
1369         }
1370
1371         RETURN(rc);
1372 }
1373
1374 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1375 {
1376         struct lmv_obd  *lmv = &obd->u.lmv;
1377         struct lmv_desc *desc;
1378         int             rc;
1379         ENTRY;
1380
1381         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1382                 CERROR("LMV setup requires a descriptor\n");
1383                 RETURN(-EINVAL);
1384         }
1385
1386         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1387         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1388                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1389                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1390                 RETURN(-EINVAL);
1391         }
1392
1393         OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * 32);
1394         if (lmv->tgts == NULL)
1395                 RETURN(-ENOMEM);
1396         lmv->tgts_size = 32;
1397
1398         obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1399         lmv->desc.ld_tgt_count = 0;
1400         lmv->desc.ld_active_tgt_count = 0;
1401         lmv->max_cookiesize = 0;
1402         lmv->max_def_easize = 0;
1403         lmv->max_easize = 0;
1404         lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1405
1406         spin_lock_init(&lmv->lmv_lock);
1407         mutex_init(&lmv->init_mutex);
1408
1409 #ifdef LPROCFS
1410         obd->obd_vars = lprocfs_lmv_obd_vars;
1411         lprocfs_seq_obd_setup(obd);
1412         lprocfs_alloc_md_stats(obd, 0);
1413         rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1414                                 0444, &lmv_proc_target_fops, obd);
1415         if (rc)
1416                 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1417                       obd->obd_name, rc);
1418 #endif
1419         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1420                              LUSTRE_CLI_FLD_HASH_DHT);
1421         if (rc) {
1422                 CERROR("Can't init FLD, err %d\n", rc);
1423                 GOTO(out, rc);
1424         }
1425
1426         RETURN(0);
1427
1428 out:
1429         return rc;
1430 }
1431
1432 static int lmv_cleanup(struct obd_device *obd)
1433 {
1434         struct lmv_obd   *lmv = &obd->u.lmv;
1435         ENTRY;
1436
1437         fld_client_fini(&lmv->lmv_fld);
1438         if (lmv->tgts != NULL) {
1439                 int i;
1440                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1441                         if (lmv->tgts[i] == NULL)
1442                                 continue;
1443                         lmv_del_target(lmv, i);
1444                 }
1445                 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1446                 lmv->tgts_size = 0;
1447         }
1448         RETURN(0);
1449 }
1450
1451 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1452 {
1453         struct lustre_cfg       *lcfg = buf;
1454         struct obd_uuid         obd_uuid;
1455         int                     gen;
1456         __u32                   index;
1457         int                     rc;
1458         ENTRY;
1459
1460         switch (lcfg->lcfg_command) {
1461         case LCFG_ADD_MDC:
1462                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1463                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
1464                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1465                         GOTO(out, rc = -EINVAL);
1466
1467                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1468
1469                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1470                         GOTO(out, rc = -EINVAL);
1471                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1472                         GOTO(out, rc = -EINVAL);
1473                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1474                 GOTO(out, rc);
1475         default:
1476                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1477                 GOTO(out, rc = -EINVAL);
1478         }
1479 out:
1480         RETURN(rc);
1481 }
1482
1483 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1484                       struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1485 {
1486         struct obd_device       *obd = class_exp2obd(exp);
1487         struct lmv_obd          *lmv = &obd->u.lmv;
1488         struct obd_statfs       *temp;
1489         int                      rc = 0;
1490         __u32                    i;
1491         ENTRY;
1492
1493         rc = lmv_check_connect(obd);
1494         if (rc)
1495                 RETURN(rc);
1496
1497         OBD_ALLOC(temp, sizeof(*temp));
1498         if (temp == NULL)
1499                 RETURN(-ENOMEM);
1500
1501         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1502                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1503                         continue;
1504
1505                 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1506                                 max_age, flags);
1507                 if (rc) {
1508                         CERROR("can't stat MDS #%d (%s), error %d\n", i,
1509                                lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1510                                rc);
1511                         GOTO(out_free_temp, rc);
1512                 }
1513
1514                 if (i == 0) {
1515                         *osfs = *temp;
1516                         /* If the statfs is from mount, it will needs
1517                          * retrieve necessary information from MDT0.
1518                          * i.e. mount does not need the merged osfs
1519                          * from all of MDT.
1520                          * And also clients can be mounted as long as
1521                          * MDT0 is in service*/
1522                         if (flags & OBD_STATFS_FOR_MDT0)
1523                                 GOTO(out_free_temp, rc);
1524                 } else {
1525                         osfs->os_bavail += temp->os_bavail;
1526                         osfs->os_blocks += temp->os_blocks;
1527                         osfs->os_ffree += temp->os_ffree;
1528                         osfs->os_files += temp->os_files;
1529                 }
1530         }
1531
1532         EXIT;
1533 out_free_temp:
1534         OBD_FREE(temp, sizeof(*temp));
1535         return rc;
1536 }
1537
1538 static int lmv_getstatus(struct obd_export *exp,
1539                          struct lu_fid *fid,
1540                          struct obd_capa **pc)
1541 {
1542         struct obd_device    *obd = exp->exp_obd;
1543         struct lmv_obd       *lmv = &obd->u.lmv;
1544         int                   rc;
1545         ENTRY;
1546
1547         rc = lmv_check_connect(obd);
1548         if (rc)
1549                 RETURN(rc);
1550
1551         rc = md_getstatus(lmv->tgts[0]->ltd_exp, fid, pc);
1552         RETURN(rc);
1553 }
1554
1555 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1556                         struct obd_capa *oc, obd_valid valid, const char *name,
1557                         const char *input, int input_size, int output_size,
1558                         int flags, struct ptlrpc_request **request)
1559 {
1560         struct obd_device      *obd = exp->exp_obd;
1561         struct lmv_obd         *lmv = &obd->u.lmv;
1562         struct lmv_tgt_desc    *tgt;
1563         int                     rc;
1564         ENTRY;
1565
1566         rc = lmv_check_connect(obd);
1567         if (rc)
1568                 RETURN(rc);
1569
1570         tgt = lmv_find_target(lmv, fid);
1571         if (IS_ERR(tgt))
1572                 RETURN(PTR_ERR(tgt));
1573
1574         rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1575                          input_size, output_size, flags, request);
1576
1577         RETURN(rc);
1578 }
1579
1580 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1581                         struct obd_capa *oc, obd_valid valid, const char *name,
1582                         const char *input, int input_size, int output_size,
1583                         int flags, __u32 suppgid,
1584                         struct ptlrpc_request **request)
1585 {
1586         struct obd_device      *obd = exp->exp_obd;
1587         struct lmv_obd         *lmv = &obd->u.lmv;
1588         struct lmv_tgt_desc    *tgt;
1589         int                     rc;
1590         ENTRY;
1591
1592         rc = lmv_check_connect(obd);
1593         if (rc)
1594                 RETURN(rc);
1595
1596         tgt = lmv_find_target(lmv, fid);
1597         if (IS_ERR(tgt))
1598                 RETURN(PTR_ERR(tgt));
1599
1600         rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1601                          input_size, output_size, flags, suppgid,
1602                          request);
1603
1604         RETURN(rc);
1605 }
1606
1607 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1608                        struct ptlrpc_request **request)
1609 {
1610         struct obd_device       *obd = exp->exp_obd;
1611         struct lmv_obd          *lmv = &obd->u.lmv;
1612         struct lmv_tgt_desc     *tgt;
1613         int                      rc;
1614         ENTRY;
1615
1616         rc = lmv_check_connect(obd);
1617         if (rc)
1618                 RETURN(rc);
1619
1620         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1621         if (IS_ERR(tgt))
1622                 RETURN(PTR_ERR(tgt));
1623
1624         if (op_data->op_flags & MF_GET_MDT_IDX) {
1625                 op_data->op_mds = tgt->ltd_idx;
1626                 RETURN(0);
1627         }
1628
1629         rc = md_getattr(tgt->ltd_exp, op_data, request);
1630
1631         RETURN(rc);
1632 }
1633
1634 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1635 {
1636         struct obd_device   *obd = exp->exp_obd;
1637         struct lmv_obd      *lmv = &obd->u.lmv;
1638         __u32                i;
1639         int                  rc;
1640         ENTRY;
1641
1642         rc = lmv_check_connect(obd);
1643         if (rc)
1644                 RETURN(rc);
1645
1646         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1647
1648         /*
1649          * With DNE every object can have two locks in different namespaces:
1650          * lookup lock in space of MDT storing direntry and update/open lock in
1651          * space of MDT storing inode.
1652          */
1653         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1654                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1655                         continue;
1656                 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1657         }
1658
1659         RETURN(0);
1660 }
1661
1662 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1663                            ldlm_iterator_t it, void *data)
1664 {
1665         struct obd_device   *obd = exp->exp_obd;
1666         struct lmv_obd      *lmv = &obd->u.lmv;
1667         __u32                i;
1668         int                  rc;
1669         ENTRY;
1670
1671         rc = lmv_check_connect(obd);
1672         if (rc)
1673                 RETURN(rc);
1674
1675         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1676
1677         /*
1678          * With DNE every object can have two locks in different namespaces:
1679          * lookup lock in space of MDT storing direntry and update/open lock in
1680          * space of MDT storing inode.
1681          */
1682         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1683                 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1684                         continue;
1685                 rc = md_find_cbdata(lmv->tgts[i]->ltd_exp, fid, it, data);
1686                 if (rc)
1687                         RETURN(rc);
1688         }
1689
1690         RETURN(rc);
1691 }
1692
1693
1694 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1695                      struct md_open_data *mod, struct ptlrpc_request **request)
1696 {
1697         struct obd_device     *obd = exp->exp_obd;
1698         struct lmv_obd        *lmv = &obd->u.lmv;
1699         struct lmv_tgt_desc   *tgt;
1700         int                    rc;
1701         ENTRY;
1702
1703         rc = lmv_check_connect(obd);
1704         if (rc)
1705                 RETURN(rc);
1706
1707         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1708         if (IS_ERR(tgt))
1709                 RETURN(PTR_ERR(tgt));
1710
1711         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1712         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1713         RETURN(rc);
1714 }
1715
1716 struct lmv_tgt_desc
1717 *lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1718                 struct lu_fid *fid)
1719 {
1720         struct lmv_tgt_desc *tgt;
1721
1722         tgt = lmv_find_target(lmv, fid);
1723         if (IS_ERR(tgt))
1724                 return tgt;
1725
1726         op_data->op_mds = tgt->ltd_idx;
1727
1728         return tgt;
1729 }
1730
1731 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1732                const void *data, int datalen, int mode, __u32 uid,
1733                __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1734                struct ptlrpc_request **request)
1735 {
1736         struct obd_device       *obd = exp->exp_obd;
1737         struct lmv_obd          *lmv = &obd->u.lmv;
1738         struct lmv_tgt_desc     *tgt;
1739         int                      rc;
1740         ENTRY;
1741
1742         rc = lmv_check_connect(obd);
1743         if (rc)
1744                 RETURN(rc);
1745
1746         if (!lmv->desc.ld_active_tgt_count)
1747                 RETURN(-EIO);
1748
1749         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1750         if (IS_ERR(tgt))
1751                 RETURN(PTR_ERR(tgt));
1752
1753         rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1754         if (rc)
1755                 RETURN(rc);
1756
1757         CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1758                op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1759                op_data->op_mds);
1760
1761         op_data->op_flags |= MF_MDC_CANCEL_FID1;
1762         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1763                        cap_effective, rdev, request);
1764
1765         if (rc == 0) {
1766                 if (*request == NULL)
1767                         RETURN(rc);
1768                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1769         }
1770         RETURN(rc);
1771 }
1772
1773 static int lmv_done_writing(struct obd_export *exp,
1774                             struct md_op_data *op_data,
1775                             struct md_open_data *mod)
1776 {
1777         struct obd_device     *obd = exp->exp_obd;
1778         struct lmv_obd        *lmv = &obd->u.lmv;
1779         struct lmv_tgt_desc   *tgt;
1780         int                    rc;
1781         ENTRY;
1782
1783         rc = lmv_check_connect(obd);
1784         if (rc)
1785                 RETURN(rc);
1786
1787         tgt = lmv_find_target(lmv, &op_data->op_fid1);
1788         if (IS_ERR(tgt))
1789                 RETURN(PTR_ERR(tgt));
1790
1791         rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1792         RETURN(rc);
1793 }
1794
1795 static int
1796 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1797                    struct lookup_intent *it, struct md_op_data *op_data,
1798                    struct lustre_handle *lockh, void *lmm, int lmmsize,
1799                    __u64 extra_lock_flags)
1800 {
1801         struct ptlrpc_request      *req = it->d.lustre.it_data;
1802         struct obd_device          *obd = exp->exp_obd;
1803         struct lmv_obd             *lmv = &obd->u.lmv;
1804         struct lustre_handle        plock;
1805         struct lmv_tgt_desc        *tgt;
1806         struct md_op_data          *rdata;
1807         struct lu_fid               fid1;
1808         struct mdt_body            *body;
1809         int                         rc = 0;
1810         int                         pmode;
1811         ENTRY;
1812
1813         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1814         LASSERT(body != NULL);
1815
1816         if (!(body->valid & OBD_MD_MDS))
1817                 RETURN(0);
1818
1819         CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1820                LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1821
1822         /*
1823          * We got LOOKUP lock, but we really need attrs.
1824          */
1825         pmode = it->d.lustre.it_lock_mode;
1826         LASSERT(pmode != 0);
1827         memcpy(&plock, lockh, sizeof(plock));
1828         it->d.lustre.it_lock_mode = 0;
1829         it->d.lustre.it_data = NULL;
1830         fid1 = body->fid1;
1831
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                 int i;
2420
2421                 rc = lmv_check_connect(obd);
2422                 if (rc)
2423                         RETURN(rc);
2424
2425                 LASSERT(*vallen == sizeof(__u32));
2426                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2427                         struct lmv_tgt_desc *tgt = lmv->tgts[i];
2428                         /*
2429                          * All tgts should be connected when this gets called.
2430                          */
2431                         if (tgt == NULL || tgt->ltd_exp == NULL)
2432                                 continue;
2433
2434                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2435                                           vallen, val, NULL))
2436                                 RETURN(0);
2437                 }
2438                 RETURN(-EINVAL);
2439         } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2440                 rc = lmv_check_connect(obd);
2441                 if (rc)
2442                         RETURN(rc);
2443
2444                 /*
2445                  * Forwarding this request to first MDS, it should know LOV
2446                  * desc.
2447                  */
2448                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2449                                   vallen, val, NULL);
2450                 if (!rc && KEY_IS(KEY_CONN_DATA))
2451                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2452                 RETURN(rc);
2453         } else if (KEY_IS(KEY_TGT_COUNT)) {
2454                 *((int *)val) = lmv->desc.ld_tgt_count;
2455                 RETURN(0);
2456         }
2457
2458         CDEBUG(D_IOCTL, "Invalid key\n");
2459         RETURN(-EINVAL);
2460 }
2461
2462 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2463                        obd_count keylen, void *key, obd_count vallen,
2464                        void *val, struct ptlrpc_request_set *set)
2465 {
2466         struct lmv_tgt_desc    *tgt = NULL;
2467         struct obd_device      *obd;
2468         struct lmv_obd         *lmv;
2469         int rc = 0;
2470         ENTRY;
2471
2472         obd = class_exp2obd(exp);
2473         if (obd == NULL) {
2474                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2475                        exp->exp_handle.h_cookie);
2476                 RETURN(-EINVAL);
2477         }
2478         lmv = &obd->u.lmv;
2479
2480         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2481                 int i, err = 0;
2482
2483                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2484                         tgt = lmv->tgts[i];
2485
2486                         if (tgt == NULL || tgt->ltd_exp == NULL)
2487                                 continue;
2488
2489                         err = obd_set_info_async(env, tgt->ltd_exp,
2490                                                  keylen, key, vallen, val, set);
2491                         if (err && rc == 0)
2492                                 rc = err;
2493                 }
2494
2495                 RETURN(rc);
2496         }
2497
2498         RETURN(-EINVAL);
2499 }
2500
2501 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2502                struct lov_stripe_md *lsm)
2503 {
2504         struct obd_device         *obd = class_exp2obd(exp);
2505         struct lmv_obd            *lmv = &obd->u.lmv;
2506         struct lmv_stripe_md      *meap;
2507         struct lmv_stripe_md      *lsmp;
2508         int                        mea_size;
2509         __u32                      i;
2510         ENTRY;
2511
2512         mea_size = lmv_get_easize(lmv);
2513         if (!lmmp)
2514                 RETURN(mea_size);
2515
2516         if (*lmmp && !lsm) {
2517                 OBD_FREE_LARGE(*lmmp, mea_size);
2518                 *lmmp = NULL;
2519                 RETURN(0);
2520         }
2521
2522         if (*lmmp == NULL) {
2523                 OBD_ALLOC_LARGE(*lmmp, mea_size);
2524                 if (*lmmp == NULL)
2525                         RETURN(-ENOMEM);
2526         }
2527
2528         if (!lsm)
2529                 RETURN(mea_size);
2530
2531         lsmp = (struct lmv_stripe_md *)lsm;
2532         meap = (struct lmv_stripe_md *)*lmmp;
2533
2534         if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2535             lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2536                 RETURN(-EINVAL);
2537
2538         meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2539         meap->mea_count = cpu_to_le32(lsmp->mea_count);
2540         meap->mea_master = cpu_to_le32(lsmp->mea_master);
2541
2542         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2543                 meap->mea_ids[i] = lsmp->mea_ids[i];
2544                 fid_cpu_to_le(&meap->mea_ids[i], &lsmp->mea_ids[i]);
2545         }
2546
2547         RETURN(mea_size);
2548 }
2549
2550 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2551                  struct lov_mds_md *lmm, int lmm_size)
2552 {
2553         struct obd_device          *obd = class_exp2obd(exp);
2554         struct lmv_stripe_md      **tmea = (struct lmv_stripe_md **)lsmp;
2555         struct lmv_stripe_md       *mea = (struct lmv_stripe_md *)lmm;
2556         struct lmv_obd             *lmv = &obd->u.lmv;
2557         int                         mea_size;
2558         __u32                       i;
2559         __u32                       magic;
2560         ENTRY;
2561
2562         mea_size = lmv_get_easize(lmv);
2563         if (lsmp == NULL)
2564                 return mea_size;
2565
2566         if (*lsmp != NULL && lmm == NULL) {
2567                 OBD_FREE_LARGE(*tmea, mea_size);
2568                 *lsmp = NULL;
2569                 RETURN(0);
2570         }
2571
2572         LASSERT(mea_size == lmm_size);
2573
2574         OBD_ALLOC_LARGE(*tmea, mea_size);
2575         if (*tmea == NULL)
2576                 RETURN(-ENOMEM);
2577
2578         if (!lmm)
2579                 RETURN(mea_size);
2580
2581         if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2582             mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2583             mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2584         {
2585                 magic = le32_to_cpu(mea->mea_magic);
2586         } else {
2587                 /*
2588                  * Old mea is not handled here.
2589                  */
2590                 CERROR("Old not supportable EA is found\n");
2591                 LBUG();
2592         }
2593
2594         (*tmea)->mea_magic = magic;
2595         (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2596         (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2597
2598         for (i = 0; i < (*tmea)->mea_count; i++) {
2599                 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2600                 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2601         }
2602         RETURN(mea_size);
2603 }
2604
2605 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2606                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2607                              ldlm_cancel_flags_t flags, void *opaque)
2608 {
2609         struct obd_device       *obd = exp->exp_obd;
2610         struct lmv_obd          *lmv = &obd->u.lmv;
2611         int                      rc = 0;
2612         int                      err;
2613         __u32                    i;
2614         ENTRY;
2615
2616         LASSERT(fid != NULL);
2617
2618         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2619                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2620
2621                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2622                         continue;
2623
2624                 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
2625                                        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         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2637         int                      rc;
2638         ENTRY;
2639
2640         if (tgt == NULL || tgt->ltd_exp == NULL)
2641                 RETURN(-EINVAL);
2642         rc =  md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
2643         RETURN(rc);
2644 }
2645
2646 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2647                            const struct lu_fid *fid, ldlm_type_t type,
2648                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2649                            struct lustre_handle *lockh)
2650 {
2651         struct obd_device       *obd = exp->exp_obd;
2652         struct lmv_obd          *lmv = &obd->u.lmv;
2653         ldlm_mode_t              rc;
2654         __u32                    i;
2655         ENTRY;
2656
2657         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2658
2659         /*
2660          * With CMD every object can have two locks in different namespaces:
2661          * lookup lock in space of mds storing direntry and update/open lock in
2662          * space of mds storing inode. Thus we check all targets, not only that
2663          * one fid was created in.
2664          */
2665         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2666                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2667
2668                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2669                         continue;
2670
2671                 rc = md_lock_match(tgt->ltd_exp, flags, fid, type, policy, mode,
2672                                    lockh);
2673                 if (rc)
2674                         RETURN(rc);
2675         }
2676
2677         RETURN(0);
2678 }
2679
2680 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2681                       struct obd_export *dt_exp, struct obd_export *md_exp,
2682                       struct lustre_md *md)
2683 {
2684         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2685         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2686
2687         if (tgt == NULL || tgt->ltd_exp == NULL)
2688                 RETURN(-EINVAL);
2689         return md_get_lustre_md(tgt->ltd_exp, req, dt_exp, md_exp, md);
2690 }
2691
2692 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2693 {
2694         struct obd_device       *obd = exp->exp_obd;
2695         struct lmv_obd          *lmv = &obd->u.lmv;
2696         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2697         ENTRY;
2698
2699         if (md->mea)
2700                 obd_free_memmd(exp, (void *)&md->mea);
2701         if (tgt == NULL || tgt->ltd_exp == NULL)
2702                 RETURN(-EINVAL);
2703         RETURN(md_free_lustre_md(tgt->ltd_exp, md));
2704 }
2705
2706 int lmv_set_open_replay_data(struct obd_export *exp,
2707                              struct obd_client_handle *och,
2708                              struct lookup_intent *it)
2709 {
2710         struct obd_device       *obd = exp->exp_obd;
2711         struct lmv_obd          *lmv = &obd->u.lmv;
2712         struct lmv_tgt_desc     *tgt;
2713         ENTRY;
2714
2715         tgt = lmv_find_target(lmv, &och->och_fid);
2716         if (IS_ERR(tgt))
2717                 RETURN(PTR_ERR(tgt));
2718
2719         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
2720 }
2721
2722 int lmv_clear_open_replay_data(struct obd_export *exp,
2723                                struct obd_client_handle *och)
2724 {
2725         struct obd_device       *obd = exp->exp_obd;
2726         struct lmv_obd          *lmv = &obd->u.lmv;
2727         struct lmv_tgt_desc     *tgt;
2728         ENTRY;
2729
2730         tgt = lmv_find_target(lmv, &och->och_fid);
2731         if (IS_ERR(tgt))
2732                 RETURN(PTR_ERR(tgt));
2733
2734         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2735 }
2736
2737 static int lmv_get_remote_perm(struct obd_export *exp,
2738                                const struct lu_fid *fid,
2739                                struct obd_capa *oc, __u32 suppgid,
2740                                struct ptlrpc_request **request)
2741 {
2742         struct obd_device       *obd = exp->exp_obd;
2743         struct lmv_obd          *lmv = &obd->u.lmv;
2744         struct lmv_tgt_desc     *tgt;
2745         int                      rc;
2746         ENTRY;
2747
2748         rc = lmv_check_connect(obd);
2749         if (rc)
2750                 RETURN(rc);
2751
2752         tgt = lmv_find_target(lmv, fid);
2753         if (IS_ERR(tgt))
2754                 RETURN(PTR_ERR(tgt));
2755
2756         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2757         RETURN(rc);
2758 }
2759
2760 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2761                           renew_capa_cb_t cb)
2762 {
2763         struct obd_device       *obd = exp->exp_obd;
2764         struct lmv_obd          *lmv = &obd->u.lmv;
2765         struct lmv_tgt_desc     *tgt;
2766         int                      rc;
2767         ENTRY;
2768
2769         rc = lmv_check_connect(obd);
2770         if (rc)
2771                 RETURN(rc);
2772
2773         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2774         if (IS_ERR(tgt))
2775                 RETURN(PTR_ERR(tgt));
2776
2777         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2778         RETURN(rc);
2779 }
2780
2781 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2782                     const struct req_msg_field *field, struct obd_capa **oc)
2783 {
2784         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2785         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2786
2787         if (tgt == NULL || tgt->ltd_exp == NULL)
2788                 RETURN(-EINVAL);
2789         return md_unpack_capa(tgt->ltd_exp, req, field, oc);
2790 }
2791
2792 int lmv_intent_getattr_async(struct obd_export *exp,
2793                              struct md_enqueue_info *minfo,
2794                              struct ldlm_enqueue_info *einfo)
2795 {
2796         struct md_op_data       *op_data = &minfo->mi_data;
2797         struct obd_device       *obd = exp->exp_obd;
2798         struct lmv_obd          *lmv = &obd->u.lmv;
2799         struct lmv_tgt_desc     *tgt = NULL;
2800         int                      rc;
2801         ENTRY;
2802
2803         rc = lmv_check_connect(obd);
2804         if (rc)
2805                 RETURN(rc);
2806
2807         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2808         if (IS_ERR(tgt))
2809                 RETURN(PTR_ERR(tgt));
2810
2811         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2812         RETURN(rc);
2813 }
2814
2815 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2816                         struct lu_fid *fid, __u64 *bits)
2817 {
2818         struct obd_device       *obd = exp->exp_obd;
2819         struct lmv_obd          *lmv = &obd->u.lmv;
2820         struct lmv_tgt_desc     *tgt;
2821         int                      rc;
2822         ENTRY;
2823
2824         rc = lmv_check_connect(obd);
2825         if (rc)
2826                 RETURN(rc);
2827
2828         tgt = lmv_find_target(lmv, fid);
2829         if (IS_ERR(tgt))
2830                 RETURN(PTR_ERR(tgt));
2831
2832         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2833         RETURN(rc);
2834 }
2835
2836 /**
2837  * For lmv, only need to send request to master MDT, and the master MDT will
2838  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2839  * we directly fetch data from the slave MDTs.
2840  */
2841 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2842                  struct obd_quotactl *oqctl)
2843 {
2844         struct obd_device   *obd = class_exp2obd(exp);
2845         struct lmv_obd      *lmv = &obd->u.lmv;
2846         struct lmv_tgt_desc *tgt = lmv->tgts[0];
2847         int                  rc = 0;
2848         __u32                i;
2849         __u64                curspace, curinodes;
2850         ENTRY;
2851
2852         if (tgt == NULL ||
2853             tgt->ltd_exp == NULL ||
2854             !tgt->ltd_active ||
2855             lmv->desc.ld_tgt_count == 0) {
2856                 CERROR("master lmv inactive\n");
2857                 RETURN(-EIO);
2858         }
2859
2860         if (oqctl->qc_cmd != Q_GETOQUOTA) {
2861                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
2862                 RETURN(rc);
2863         }
2864
2865         curspace = curinodes = 0;
2866         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2867                 int err;
2868                 tgt = lmv->tgts[i];
2869
2870                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2871                         continue;
2872
2873                 err = obd_quotactl(tgt->ltd_exp, oqctl);
2874                 if (err) {
2875                         CERROR("getquota on mdt %d failed. %d\n", i, err);
2876                         if (!rc)
2877                                 rc = err;
2878                 } else {
2879                         curspace += oqctl->qc_dqblk.dqb_curspace;
2880                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
2881                 }
2882         }
2883         oqctl->qc_dqblk.dqb_curspace = curspace;
2884         oqctl->qc_dqblk.dqb_curinodes = curinodes;
2885
2886         RETURN(rc);
2887 }
2888
2889 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
2890                    struct obd_quotactl *oqctl)
2891 {
2892         struct obd_device       *obd = class_exp2obd(exp);
2893         struct lmv_obd          *lmv = &obd->u.lmv;
2894         struct lmv_tgt_desc     *tgt;
2895         __u32                    i;
2896         int                      rc = 0;
2897         ENTRY;
2898
2899         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2900                 int err;
2901                 tgt = lmv->tgts[i];
2902                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
2903                         CERROR("lmv idx %d inactive\n", i);
2904                         RETURN(-EIO);
2905                 }
2906
2907                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
2908                 if (err && !rc)
2909                         rc = err;
2910         }
2911
2912         RETURN(rc);
2913 }
2914
2915 struct obd_ops lmv_obd_ops = {
2916         .o_owner                = THIS_MODULE,
2917         .o_setup                = lmv_setup,
2918         .o_cleanup              = lmv_cleanup,
2919         .o_precleanup           = lmv_precleanup,
2920         .o_process_config       = lmv_process_config,
2921         .o_connect              = lmv_connect,
2922         .o_disconnect           = lmv_disconnect,
2923         .o_statfs               = lmv_statfs,
2924         .o_get_info             = lmv_get_info,
2925         .o_set_info_async       = lmv_set_info_async,
2926         .o_packmd               = lmv_packmd,
2927         .o_unpackmd             = lmv_unpackmd,
2928         .o_notify               = lmv_notify,
2929         .o_get_uuid             = lmv_get_uuid,
2930         .o_iocontrol            = lmv_iocontrol,
2931         .o_quotacheck           = lmv_quotacheck,
2932         .o_quotactl             = lmv_quotactl
2933 };
2934
2935 struct md_ops lmv_md_ops = {
2936         .m_getstatus            = lmv_getstatus,
2937         .m_null_inode           = lmv_null_inode,
2938         .m_find_cbdata          = lmv_find_cbdata,
2939         .m_close                = lmv_close,
2940         .m_create               = lmv_create,
2941         .m_done_writing         = lmv_done_writing,
2942         .m_enqueue              = lmv_enqueue,
2943         .m_getattr              = lmv_getattr,
2944         .m_getxattr             = lmv_getxattr,
2945         .m_getattr_name         = lmv_getattr_name,
2946         .m_intent_lock          = lmv_intent_lock,
2947         .m_link                 = lmv_link,
2948         .m_rename               = lmv_rename,
2949         .m_setattr              = lmv_setattr,
2950         .m_setxattr             = lmv_setxattr,
2951         .m_fsync                = lmv_fsync,
2952         .m_readpage             = lmv_readpage,
2953         .m_unlink               = lmv_unlink,
2954         .m_init_ea_size         = lmv_init_ea_size,
2955         .m_cancel_unused        = lmv_cancel_unused,
2956         .m_set_lock_data        = lmv_set_lock_data,
2957         .m_lock_match           = lmv_lock_match,
2958         .m_get_lustre_md        = lmv_get_lustre_md,
2959         .m_free_lustre_md       = lmv_free_lustre_md,
2960         .m_set_open_replay_data = lmv_set_open_replay_data,
2961         .m_clear_open_replay_data = lmv_clear_open_replay_data,
2962         .m_renew_capa           = lmv_renew_capa,
2963         .m_unpack_capa          = lmv_unpack_capa,
2964         .m_get_remote_perm      = lmv_get_remote_perm,
2965         .m_intent_getattr_async = lmv_intent_getattr_async,
2966         .m_revalidate_lock      = lmv_revalidate_lock
2967 };
2968
2969 int __init lmv_init(void)
2970 {
2971         return class_register_type(&lmv_obd_ops, &lmv_md_ops, NULL,
2972 #ifndef HAVE_ONLY_PROCFS_SEQ
2973                                         NULL,
2974 #endif
2975                                         LUSTRE_LMV_NAME, NULL);
2976 }
2977
2978 #ifdef __KERNEL__
2979 static void lmv_exit(void)
2980 {
2981         class_unregister_type(LUSTRE_LMV_NAME);
2982 }
2983
2984 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2985 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
2986 MODULE_LICENSE("GPL");
2987
2988 module_init(lmv_init);
2989 module_exit(lmv_exit);
2990 #endif