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