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