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