Whamcloud - gitweb
LU-2675 obd: decruft md_enqueue() and md_intent_lock()
[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 #define NORMAL_MAX_STRIPES 4
2335 int lmv_read_entry(struct obd_export *exp, struct md_op_data *op_data,
2336                    struct md_callback *cb_op, struct lu_dirent **ldp,
2337                    struct page **ppage)
2338 {
2339         struct obd_device       *obd = exp->exp_obd;
2340         struct lmv_obd          *lmv = &obd->u.lmv;
2341         struct lmv_stripe_md    *lsm = op_data->op_mea1;
2342         struct lu_dirent        *tmp_ents[NORMAL_MAX_STRIPES];
2343         struct lu_dirent        **ents = NULL;
2344         int                     stripe_count;
2345         __u64                   min_hash;
2346         int                     min_idx = 0;
2347         struct page             *min_page = NULL;
2348         int                     i;
2349         int                     rc;
2350         ENTRY;
2351
2352         rc = lmv_check_connect(obd);
2353         if (rc)
2354                 RETURN(rc);
2355
2356         if (lsm == NULL)
2357                 stripe_count = 1;
2358         else
2359                 stripe_count = lsm->lsm_md_stripe_count;
2360
2361         if (stripe_count > NORMAL_MAX_STRIPES) {
2362                 OBD_ALLOC(ents, sizeof(ents[0]) * stripe_count);
2363                 if (ents == NULL)
2364                         GOTO(out, rc = -ENOMEM);
2365         } else {
2366                 ents = tmp_ents;
2367                 memset(ents, 0, sizeof(ents[0]) * stripe_count);
2368         }
2369
2370         min_hash = MDS_DIR_END_OFF;
2371         for (i = 0; i < stripe_count; i++) {
2372                 struct lmv_tgt_desc *tgt;
2373                 struct page *page = NULL;
2374
2375                 if (likely(lsm == NULL)) {
2376                         tgt = lmv_find_target(lmv, &op_data->op_fid1);
2377                         if (IS_ERR(tgt))
2378                                 GOTO(out, rc = PTR_ERR(tgt));
2379                         LASSERT(op_data->op_data != NULL);
2380                 } else {
2381                         tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds);
2382                         if (IS_ERR(tgt))
2383                                 GOTO(out, rc = PTR_ERR(tgt));
2384                         op_data->op_fid1 = lsm->lsm_md_oinfo[i].lmo_fid;
2385                         op_data->op_fid2 = lsm->lsm_md_oinfo[i].lmo_fid;
2386                         op_data->op_stripe_offset = i;
2387                 }
2388
2389                 rc = md_read_entry(tgt->ltd_exp, op_data, cb_op, &ents[i],
2390                                    &page);
2391                 if (rc != 0)
2392                         GOTO(out, rc);
2393
2394                 if (ents[i] != NULL &&
2395                     le64_to_cpu(ents[i]->lde_hash) <= min_hash) {
2396                         if (min_page != NULL)
2397                                 page_cache_release(min_page);
2398                         min_page = page;
2399                         min_hash = le64_to_cpu(ents[i]->lde_hash);
2400                         min_idx = i;
2401                 }
2402         }
2403
2404         if (min_hash != MDS_DIR_END_OFF)
2405                 *ldp = ents[min_idx];
2406         else
2407                 *ldp = NULL;
2408 out:
2409         if (stripe_count > NORMAL_MAX_STRIPES && ents != NULL)
2410                 OBD_FREE(ents, sizeof(ents[0]) * stripe_count);
2411
2412         if (rc != 0 && min_page != NULL) {
2413                 kunmap(min_page);
2414                 page_cache_release(min_page);
2415         } else {
2416                 *ppage = min_page;
2417         }
2418
2419         RETURN(rc);
2420 }
2421
2422 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2423                       struct ptlrpc_request **request)
2424 {
2425         struct obd_device       *obd = exp->exp_obd;
2426         struct lmv_obd          *lmv = &obd->u.lmv;
2427         struct lmv_tgt_desc     *tgt = NULL;
2428         struct lmv_tgt_desc     *parent_tgt = NULL;
2429         struct mdt_body         *body;
2430         int                     rc;
2431         ENTRY;
2432
2433         rc = lmv_check_connect(obd);
2434         if (rc)
2435                 RETURN(rc);
2436 retry:
2437         /* Send unlink requests to the MDT where the child is located */
2438         if (likely(!fid_is_zero(&op_data->op_fid2))) {
2439                 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2440                 if (IS_ERR(tgt))
2441                         RETURN(PTR_ERR(tgt));
2442
2443                 /* For striped dir, we need to locate the parent as well */
2444                 if (op_data->op_mea1 != NULL) {
2445                         struct lmv_tgt_desc *tmp;
2446
2447                         LASSERT(op_data->op_name != NULL &&
2448                                 op_data->op_namelen != 0);
2449                         tmp = lmv_locate_target_for_name(lmv,
2450                                                    op_data->op_mea1,
2451                                                    op_data->op_name,
2452                                                    op_data->op_namelen,
2453                                                    &op_data->op_fid1,
2454                                                    &op_data->op_mds);
2455                         if (IS_ERR(tmp))
2456                                 RETURN(PTR_ERR(tmp));
2457                 }
2458         } else {
2459                 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2460                 if (IS_ERR(tgt))
2461                         RETURN(PTR_ERR(tgt));
2462         }
2463
2464         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2465         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2466         op_data->op_cap = cfs_curproc_cap_pack();
2467
2468         /*
2469          * If child's fid is given, cancel unused locks for it if it is from
2470          * another export than parent.
2471          *
2472          * LOOKUP lock for child (fid3) should also be cancelled on parent
2473          * tgt_tgt in mdc_unlink().
2474          */
2475         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2476
2477         /*
2478          * Cancel FULL locks on child (fid3).
2479          */
2480         parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2481         if (IS_ERR(parent_tgt))
2482                 RETURN(PTR_ERR(parent_tgt));
2483
2484         if (parent_tgt != tgt) {
2485                 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2486                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2487                                       MF_MDC_CANCEL_FID3);
2488         }
2489
2490         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2491                               MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2492         if (rc != 0)
2493                 RETURN(rc);
2494
2495         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%d\n",
2496                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2497
2498         rc = md_unlink(tgt->ltd_exp, op_data, request);
2499         if (rc != 0 && rc != -EREMOTE)
2500                 RETURN(rc);
2501
2502         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2503         if (body == NULL)
2504                 RETURN(-EPROTO);
2505
2506         /* Not cross-ref case, just get out of here. */
2507         if (likely(!(body->valid & OBD_MD_MDS)))
2508                 RETURN(0);
2509
2510         CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2511                exp->exp_obd->obd_name, PFID(&body->fid1));
2512
2513         /* This is a remote object, try remote MDT, Note: it may
2514          * try more than 1 time here, Considering following case
2515          * /mnt/lustre is root on MDT0, remote1 is on MDT1
2516          * 1. Initially A does not know where remote1 is, it send
2517          *    unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2518          *    resend unlink RPC to MDT1 (retry 1st time).
2519          *
2520          * 2. During the unlink RPC in flight,
2521          *    client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2522          *    and create new remote1, but on MDT0
2523          *
2524          * 3. MDT1 get unlink RPC(from A), then do remote lock on
2525          *    /mnt/lustre, then lookup get fid of remote1, and find
2526          *    it is remote dir again, and replay -EREMOTE again.
2527          *
2528          * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2529          *
2530          * In theory, it might try unlimited time here, but it should
2531          * be very rare case.  */
2532         op_data->op_fid2 = body->fid1;
2533         ptlrpc_req_finished(*request);
2534         *request = NULL;
2535
2536         goto retry;
2537 }
2538
2539 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2540 {
2541         struct lmv_obd *lmv = &obd->u.lmv;
2542         int rc = 0;
2543
2544         switch (stage) {
2545         case OBD_CLEANUP_EARLY:
2546                 /* XXX: here should be calling obd_precleanup() down to
2547                  * stack. */
2548                 break;
2549         case OBD_CLEANUP_EXPORTS:
2550                 fld_client_proc_fini(&lmv->lmv_fld);
2551                 lprocfs_obd_cleanup(obd);
2552                 lprocfs_free_md_stats(obd);
2553                 break;
2554         default:
2555                 break;
2556         }
2557         RETURN(rc);
2558 }
2559
2560 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2561                         __u32 keylen, void *key, __u32 *vallen, void *val,
2562                         struct lov_stripe_md *lsm)
2563 {
2564         struct obd_device       *obd;
2565         struct lmv_obd          *lmv;
2566         int                      rc = 0;
2567         ENTRY;
2568
2569         obd = class_exp2obd(exp);
2570         if (obd == NULL) {
2571                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2572                        exp->exp_handle.h_cookie);
2573                 RETURN(-EINVAL);
2574         }
2575
2576         lmv = &obd->u.lmv;
2577         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2578                 int i;
2579
2580                 rc = lmv_check_connect(obd);
2581                 if (rc)
2582                         RETURN(rc);
2583
2584                 LASSERT(*vallen == sizeof(__u32));
2585                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2586                         struct lmv_tgt_desc *tgt = lmv->tgts[i];
2587                         /*
2588                          * All tgts should be connected when this gets called.
2589                          */
2590                         if (tgt == NULL || tgt->ltd_exp == NULL)
2591                                 continue;
2592
2593                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2594                                           vallen, val, NULL))
2595                                 RETURN(0);
2596                 }
2597                 RETURN(-EINVAL);
2598         } else if (KEY_IS(KEY_MAX_EASIZE) ||
2599                    KEY_IS(KEY_DEFAULT_EASIZE) ||
2600                    KEY_IS(KEY_MAX_COOKIESIZE) ||
2601                    KEY_IS(KEY_DEFAULT_COOKIESIZE) ||
2602                    KEY_IS(KEY_CONN_DATA)) {
2603                 rc = lmv_check_connect(obd);
2604                 if (rc)
2605                         RETURN(rc);
2606
2607                 /*
2608                  * Forwarding this request to first MDS, it should know LOV
2609                  * desc.
2610                  */
2611                 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2612                                   vallen, val, NULL);
2613                 if (!rc && KEY_IS(KEY_CONN_DATA))
2614                         exp->exp_connect_data = *(struct obd_connect_data *)val;
2615                 RETURN(rc);
2616         } else if (KEY_IS(KEY_TGT_COUNT)) {
2617                 *((int *)val) = lmv->desc.ld_tgt_count;
2618                 RETURN(0);
2619         }
2620
2621         CDEBUG(D_IOCTL, "Invalid key\n");
2622         RETURN(-EINVAL);
2623 }
2624
2625 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2626                        obd_count keylen, void *key, obd_count vallen,
2627                        void *val, struct ptlrpc_request_set *set)
2628 {
2629         struct lmv_tgt_desc    *tgt = NULL;
2630         struct obd_device      *obd;
2631         struct lmv_obd         *lmv;
2632         int rc = 0;
2633         ENTRY;
2634
2635         obd = class_exp2obd(exp);
2636         if (obd == NULL) {
2637                 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2638                        exp->exp_handle.h_cookie);
2639                 RETURN(-EINVAL);
2640         }
2641         lmv = &obd->u.lmv;
2642
2643         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2644                 int i, err = 0;
2645
2646                 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2647                         tgt = lmv->tgts[i];
2648
2649                         if (tgt == NULL || tgt->ltd_exp == NULL)
2650                                 continue;
2651
2652                         err = obd_set_info_async(env, tgt->ltd_exp,
2653                                                  keylen, key, vallen, val, set);
2654                         if (err && rc == 0)
2655                                 rc = err;
2656                 }
2657
2658                 RETURN(rc);
2659         }
2660
2661         RETURN(-EINVAL);
2662 }
2663
2664 static int lmv_pack_md_v1(const struct lmv_stripe_md *lsm,
2665                           struct lmv_mds_md_v1 *lmm1)
2666 {
2667         int     cplen;
2668         int     i;
2669
2670         lmm1->lmv_magic = cpu_to_le32(lsm->lsm_md_magic);
2671         lmm1->lmv_stripe_count = cpu_to_le32(lsm->lsm_md_stripe_count);
2672         lmm1->lmv_master_mdt_index = cpu_to_le32(lsm->lsm_md_master_mdt_index);
2673         lmm1->lmv_hash_type = cpu_to_le32(lsm->lsm_md_hash_type);
2674         cplen = strlcpy(lmm1->lmv_pool_name, lsm->lsm_md_pool_name,
2675                         sizeof(lmm1->lmv_pool_name));
2676         if (cplen >= sizeof(lmm1->lmv_pool_name))
2677                 return -E2BIG;
2678
2679         for (i = 0; i < lsm->lsm_md_stripe_count; i++)
2680                 fid_cpu_to_le(&lmm1->lmv_stripe_fids[i],
2681                               &lsm->lsm_md_oinfo[i].lmo_fid);
2682         return 0;
2683 }
2684
2685 int lmv_pack_md(union lmv_mds_md **lmmp, const struct lmv_stripe_md *lsm,
2686                 int stripe_count)
2687 {
2688         int     lmm_size = 0;
2689         bool    allocated = false;
2690         int     rc = 0;
2691         ENTRY;
2692
2693         LASSERT(lmmp != NULL);
2694         /* Free lmm */
2695         if (*lmmp != NULL && lsm == NULL) {
2696                 int stripe_count;
2697
2698                 stripe_count = lmv_mds_md_stripe_count_get(*lmmp);
2699                 lmm_size = lmv_mds_md_size(stripe_count,
2700                                            le32_to_cpu((*lmmp)->lmv_magic));
2701                 if (lmm_size == 0)
2702                         RETURN(-EINVAL);
2703                 OBD_FREE(*lmmp, lmm_size);
2704                 *lmmp = NULL;
2705                 RETURN(0);
2706         }
2707
2708         /* Alloc lmm */
2709         if (*lmmp == NULL && lsm == NULL) {
2710                 lmm_size = lmv_mds_md_size(stripe_count, LMV_MAGIC);
2711                 LASSERT(lmm_size > 0);
2712                 OBD_ALLOC(*lmmp, lmm_size);
2713                 if (*lmmp == NULL)
2714                         RETURN(-ENOMEM);
2715                 lmv_mds_md_stripe_count_set(*lmmp, stripe_count);
2716                 (*lmmp)->lmv_magic = cpu_to_le32(LMV_MAGIC);
2717                 RETURN(lmm_size);
2718         }
2719
2720         /* pack lmm */
2721         LASSERT(lsm != NULL);
2722         lmm_size = lmv_mds_md_size(lsm->lsm_md_stripe_count, lsm->lsm_md_magic);
2723         if (*lmmp == NULL) {
2724                 OBD_ALLOC(*lmmp, lmm_size);
2725                 if (*lmmp == NULL)
2726                         RETURN(-ENOMEM);
2727                 allocated = true;
2728         }
2729
2730         switch (lsm->lsm_md_magic) {
2731         case LMV_MAGIC_V1:
2732                 rc = lmv_pack_md_v1(lsm, &(*lmmp)->lmv_md_v1);
2733                 break;
2734         default:
2735                 rc = -EINVAL;
2736                 break;
2737         }
2738
2739         if (rc != 0 && allocated) {
2740                 OBD_FREE(*lmmp, lmm_size);
2741                 *lmmp = NULL;
2742         }
2743
2744         RETURN(lmm_size);
2745 }
2746 EXPORT_SYMBOL(lmv_pack_md);
2747
2748 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2749                             const struct lmv_mds_md_v1 *lmm1)
2750 {
2751         struct lmv_obd  *lmv = &exp->exp_obd->u.lmv;
2752         int             stripe_count;
2753         int             cplen;
2754         int             i;
2755         int             rc = 0;
2756         ENTRY;
2757
2758         lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2759         lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2760         lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2761         lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2762         lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2763         fid_le_to_cpu(&lsm->lsm_md_master_fid, &lmm1->lmv_master_fid);
2764         cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2765                         sizeof(lsm->lsm_md_pool_name));
2766
2767         if (!fid_is_sane(&lsm->lsm_md_master_fid))
2768                 RETURN(-EPROTO);
2769
2770         if (cplen >= sizeof(lsm->lsm_md_pool_name))
2771                 RETURN(-E2BIG);
2772
2773         CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d"
2774                "layout_version %d\n", lsm->lsm_md_stripe_count,
2775                lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
2776                lsm->lsm_md_layout_version);
2777
2778         stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2779         for (i = 0; i < le32_to_cpu(stripe_count); i++) {
2780                 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2781                               &lmm1->lmv_stripe_fids[i]);
2782                 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2783                                     &lsm->lsm_md_oinfo[i].lmo_mds);
2784                 if (rc != 0)
2785                         RETURN(rc);
2786                 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
2787                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2788         }
2789
2790         RETURN(rc);
2791 }
2792
2793 int lmv_unpack_md(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2794                   const union lmv_mds_md *lmm, int stripe_count)
2795 {
2796         struct lmv_stripe_md     *lsm;
2797         int                      lsm_size;
2798         int                      rc;
2799         bool                     allocated = false;
2800         ENTRY;
2801
2802         LASSERT(lsmp != NULL);
2803
2804         lsm = *lsmp;
2805         /* Free memmd */
2806         if (lsm != NULL && lmm == NULL) {
2807 #ifdef __KERNEL__
2808                 int i;
2809                 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
2810                         /* For migrating inode, the master stripe and master
2811                          * object will be the same, so do not need iput, see
2812                          * ll_update_lsm_md */
2813                         if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
2814                               i == 0) && lsm->lsm_md_oinfo[i].lmo_root != NULL)
2815                                 iput(lsm->lsm_md_oinfo[i].lmo_root);
2816                 }
2817 #endif
2818                 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
2819                 OBD_FREE(lsm, lsm_size);
2820                 *lsmp = NULL;
2821                 RETURN(0);
2822         }
2823
2824         /* Alloc memmd */
2825         if (lsm == NULL && lmm == NULL) {
2826                 lsm_size = lmv_stripe_md_size(stripe_count);
2827                 OBD_ALLOC(lsm, lsm_size);
2828                 if (lsm == NULL)
2829                         RETURN(-ENOMEM);
2830                 lsm->lsm_md_stripe_count = stripe_count;
2831                 *lsmp = lsm;
2832                 RETURN(0);
2833         }
2834
2835         /* Unpack memmd */
2836         if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2837             le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2838                 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2839                        exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2840                        -EIO);
2841                 RETURN(-EIO);
2842         }
2843
2844         if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2845                 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2846         else
2847                 /**
2848                  * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2849                  * stripecount should be 0 then.
2850                  */
2851                 lsm_size = lmv_stripe_md_size(0);
2852
2853         lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2854         if (lsm == NULL) {
2855                 OBD_ALLOC(lsm, lsm_size);
2856                 if (lsm == NULL)
2857                         RETURN(-ENOMEM);
2858                 allocated = true;
2859                 *lsmp = lsm;
2860         }
2861
2862         switch (le32_to_cpu(lmm->lmv_magic)) {
2863         case LMV_MAGIC_V1:
2864                 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
2865                 break;
2866         default:
2867                 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
2868                        le32_to_cpu(lmm->lmv_magic));
2869                 rc = -EINVAL;
2870                 break;
2871         }
2872
2873         if (rc != 0 && allocated) {
2874                 OBD_FREE(lsm, lsm_size);
2875                 *lsmp = NULL;
2876                 lsm_size = rc;
2877         }
2878         RETURN(lsm_size);
2879 }
2880
2881 int lmv_alloc_memmd(struct lmv_stripe_md **lsmp, int stripes)
2882 {
2883         return lmv_unpack_md(NULL, lsmp, NULL, stripes);
2884 }
2885 EXPORT_SYMBOL(lmv_alloc_memmd);
2886
2887 void lmv_free_memmd(struct lmv_stripe_md *lsm)
2888 {
2889         lmv_unpack_md(NULL, &lsm, NULL, 0);
2890 }
2891 EXPORT_SYMBOL(lmv_free_memmd);
2892
2893 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2894                  struct lov_mds_md *lmm, int disk_len)
2895 {
2896         return lmv_unpack_md(exp, (struct lmv_stripe_md **)lsmp,
2897                              (union lmv_mds_md *)lmm, disk_len);
2898 }
2899
2900 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2901                struct lov_stripe_md *lsm)
2902 {
2903         struct obd_device               *obd = exp->exp_obd;
2904         struct lmv_obd                  *lmv_obd = &obd->u.lmv;
2905         const struct lmv_stripe_md      *lmv = (struct lmv_stripe_md *)lsm;
2906         int                             stripe_count;
2907
2908         if (lmmp == NULL) {
2909                 if (lsm != NULL)
2910                         stripe_count = lmv->lsm_md_stripe_count;
2911                 else
2912                         stripe_count = lmv_obd->desc.ld_tgt_count;
2913
2914                 return lmv_mds_md_size(stripe_count, LMV_MAGIC_V1);
2915         }
2916
2917         return lmv_pack_md((union lmv_mds_md **)lmmp, lmv, 0);
2918 }
2919
2920 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2921                              ldlm_policy_data_t *policy, ldlm_mode_t mode,
2922                              ldlm_cancel_flags_t flags, void *opaque)
2923 {
2924         struct obd_device       *obd = exp->exp_obd;
2925         struct lmv_obd          *lmv = &obd->u.lmv;
2926         int                      rc = 0;
2927         int                      err;
2928         __u32                    i;
2929         ENTRY;
2930
2931         LASSERT(fid != NULL);
2932
2933         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2934                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2935
2936                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2937                         continue;
2938
2939                 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
2940                                        opaque);
2941                 if (!rc)
2942                         rc = err;
2943         }
2944         RETURN(rc);
2945 }
2946
2947 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2948                       __u64 *bits)
2949 {
2950         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
2951         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
2952         int                      rc;
2953         ENTRY;
2954
2955         if (tgt == NULL || tgt->ltd_exp == NULL)
2956                 RETURN(-EINVAL);
2957         rc =  md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
2958         RETURN(rc);
2959 }
2960
2961 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2962                            const struct lu_fid *fid, ldlm_type_t type,
2963                            ldlm_policy_data_t *policy, ldlm_mode_t mode,
2964                            struct lustre_handle *lockh)
2965 {
2966         struct obd_device       *obd = exp->exp_obd;
2967         struct lmv_obd          *lmv = &obd->u.lmv;
2968         ldlm_mode_t              rc;
2969         __u32                    i;
2970         ENTRY;
2971
2972         CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2973
2974         /*
2975          * With CMD every object can have two locks in different namespaces:
2976          * lookup lock in space of mds storing direntry and update/open lock in
2977          * space of mds storing inode. Thus we check all targets, not only that
2978          * one fid was created in.
2979          */
2980         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2981                 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2982
2983                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
2984                         continue;
2985
2986                 rc = md_lock_match(tgt->ltd_exp, flags, fid, type, policy, mode,
2987                                    lockh);
2988                 if (rc)
2989                         RETURN(rc);
2990         }
2991
2992         RETURN(0);
2993 }
2994
2995 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2996                       struct obd_export *dt_exp, struct obd_export *md_exp,
2997                       struct lustre_md *md)
2998 {
2999         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
3000         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
3001
3002         if (tgt == NULL || tgt->ltd_exp == NULL)
3003                 RETURN(-EINVAL);
3004
3005         return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
3006 }
3007
3008 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3009 {
3010         struct obd_device       *obd = exp->exp_obd;
3011         struct lmv_obd          *lmv = &obd->u.lmv;
3012         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
3013         ENTRY;
3014
3015         if (md->lmv != NULL) {
3016                 lmv_free_memmd(md->lmv);
3017                 md->lmv = NULL;
3018         }
3019         if (tgt == NULL || tgt->ltd_exp == NULL)
3020                 RETURN(-EINVAL);
3021         RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
3022 }
3023
3024 int lmv_set_open_replay_data(struct obd_export *exp,
3025                              struct obd_client_handle *och,
3026                              struct lookup_intent *it)
3027 {
3028         struct obd_device       *obd = exp->exp_obd;
3029         struct lmv_obd          *lmv = &obd->u.lmv;
3030         struct lmv_tgt_desc     *tgt;
3031         ENTRY;
3032
3033         tgt = lmv_find_target(lmv, &och->och_fid);
3034         if (IS_ERR(tgt))
3035                 RETURN(PTR_ERR(tgt));
3036
3037         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3038 }
3039
3040 int lmv_clear_open_replay_data(struct obd_export *exp,
3041                                struct obd_client_handle *och)
3042 {
3043         struct obd_device       *obd = exp->exp_obd;
3044         struct lmv_obd          *lmv = &obd->u.lmv;
3045         struct lmv_tgt_desc     *tgt;
3046         ENTRY;
3047
3048         tgt = lmv_find_target(lmv, &och->och_fid);
3049         if (IS_ERR(tgt))
3050                 RETURN(PTR_ERR(tgt));
3051
3052         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3053 }
3054
3055 static int lmv_get_remote_perm(struct obd_export *exp,
3056                                const struct lu_fid *fid,
3057                                struct obd_capa *oc, __u32 suppgid,
3058                                struct ptlrpc_request **request)
3059 {
3060         struct obd_device       *obd = exp->exp_obd;
3061         struct lmv_obd          *lmv = &obd->u.lmv;
3062         struct lmv_tgt_desc     *tgt;
3063         int                      rc;
3064         ENTRY;
3065
3066         rc = lmv_check_connect(obd);
3067         if (rc)
3068                 RETURN(rc);
3069
3070         tgt = lmv_find_target(lmv, fid);
3071         if (IS_ERR(tgt))
3072                 RETURN(PTR_ERR(tgt));
3073
3074         rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
3075         RETURN(rc);
3076 }
3077
3078 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3079                           renew_capa_cb_t cb)
3080 {
3081         struct obd_device       *obd = exp->exp_obd;
3082         struct lmv_obd          *lmv = &obd->u.lmv;
3083         struct lmv_tgt_desc     *tgt;
3084         int                      rc;
3085         ENTRY;
3086
3087         rc = lmv_check_connect(obd);
3088         if (rc)
3089                 RETURN(rc);
3090
3091         tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3092         if (IS_ERR(tgt))
3093                 RETURN(PTR_ERR(tgt));
3094
3095         rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3096         RETURN(rc);
3097 }
3098
3099 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3100                     const struct req_msg_field *field, struct obd_capa **oc)
3101 {
3102         struct lmv_obd          *lmv = &exp->exp_obd->u.lmv;
3103         struct lmv_tgt_desc     *tgt = lmv->tgts[0];
3104
3105         if (tgt == NULL || tgt->ltd_exp == NULL)
3106                 RETURN(-EINVAL);
3107         return md_unpack_capa(tgt->ltd_exp, req, field, oc);
3108 }
3109
3110 int lmv_intent_getattr_async(struct obd_export *exp,
3111                              struct md_enqueue_info *minfo,
3112                              struct ldlm_enqueue_info *einfo)
3113 {
3114         struct md_op_data       *op_data = &minfo->mi_data;
3115         struct obd_device       *obd = exp->exp_obd;
3116         struct lmv_obd          *lmv = &obd->u.lmv;
3117         struct lmv_tgt_desc     *tgt = NULL;
3118         int                      rc;
3119         ENTRY;
3120
3121         rc = lmv_check_connect(obd);
3122         if (rc)
3123                 RETURN(rc);
3124
3125         tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
3126         if (IS_ERR(tgt))
3127                 RETURN(PTR_ERR(tgt));
3128
3129         rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3130         RETURN(rc);
3131 }
3132
3133 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3134                         struct lu_fid *fid, __u64 *bits)
3135 {
3136         struct obd_device       *obd = exp->exp_obd;
3137         struct lmv_obd          *lmv = &obd->u.lmv;
3138         struct lmv_tgt_desc     *tgt;
3139         int                      rc;
3140         ENTRY;
3141
3142         rc = lmv_check_connect(obd);
3143         if (rc)
3144                 RETURN(rc);
3145
3146         tgt = lmv_find_target(lmv, fid);
3147         if (IS_ERR(tgt))
3148                 RETURN(PTR_ERR(tgt));
3149
3150         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3151         RETURN(rc);
3152 }
3153
3154 /**
3155  * For lmv, only need to send request to master MDT, and the master MDT will
3156  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3157  * we directly fetch data from the slave MDTs.
3158  */
3159 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3160                  struct obd_quotactl *oqctl)
3161 {
3162         struct obd_device   *obd = class_exp2obd(exp);
3163         struct lmv_obd      *lmv = &obd->u.lmv;
3164         struct lmv_tgt_desc *tgt = lmv->tgts[0];
3165         int                  rc = 0;
3166         __u32                i;
3167         __u64                curspace, curinodes;
3168         ENTRY;
3169
3170         if (tgt == NULL ||
3171             tgt->ltd_exp == NULL ||
3172             !tgt->ltd_active ||
3173             lmv->desc.ld_tgt_count == 0) {
3174                 CERROR("master lmv inactive\n");
3175                 RETURN(-EIO);
3176         }
3177
3178         if (oqctl->qc_cmd != Q_GETOQUOTA) {
3179                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3180                 RETURN(rc);
3181         }
3182
3183         curspace = curinodes = 0;
3184         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3185                 int err;
3186                 tgt = lmv->tgts[i];
3187
3188                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3189                         continue;
3190
3191                 err = obd_quotactl(tgt->ltd_exp, oqctl);
3192                 if (err) {
3193                         CERROR("getquota on mdt %d failed. %d\n", i, err);
3194                         if (!rc)
3195                                 rc = err;
3196                 } else {
3197                         curspace += oqctl->qc_dqblk.dqb_curspace;
3198                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
3199                 }
3200         }
3201         oqctl->qc_dqblk.dqb_curspace = curspace;
3202         oqctl->qc_dqblk.dqb_curinodes = curinodes;
3203
3204         RETURN(rc);
3205 }
3206
3207 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3208                    struct obd_quotactl *oqctl)
3209 {
3210         struct obd_device       *obd = class_exp2obd(exp);
3211         struct lmv_obd          *lmv = &obd->u.lmv;
3212         struct lmv_tgt_desc     *tgt;
3213         __u32                    i;
3214         int                      rc = 0;
3215         ENTRY;
3216
3217         for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3218                 int err;
3219                 tgt = lmv->tgts[i];
3220                 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
3221                         CERROR("lmv idx %d inactive\n", i);
3222                         RETURN(-EIO);
3223                 }
3224
3225                 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3226                 if (err && !rc)
3227                         rc = err;
3228         }
3229
3230         RETURN(rc);
3231 }
3232
3233 int lmv_update_lsm_md(struct obd_export *exp, struct lmv_stripe_md *lsm,
3234                       struct mdt_body *body, ldlm_blocking_callback cb_blocking)
3235 {
3236         return lmv_revalidate_slaves(exp, body, lsm, cb_blocking, 0);
3237 }
3238
3239 int lmv_merge_attr(struct obd_export *exp, const struct lmv_stripe_md *lsm,
3240                    struct cl_attr *attr)
3241 {
3242 #ifdef __KERNEL__
3243         int i;
3244
3245         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3246                 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3247
3248                 CDEBUG(D_INFO, ""DFID" size %llu, nlink %u, atime %lu ctime"
3249                        "%lu, mtime %lu.\n", PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3250                        i_size_read(inode), inode->i_nlink,
3251                        LTIME_S(inode->i_atime), LTIME_S(inode->i_ctime),
3252                        LTIME_S(inode->i_mtime));
3253
3254                 /* for slave stripe, it needs to subtract nlink for . and .. */
3255                 if (i != 0)
3256                         attr->cat_nlink += inode->i_nlink - 2;
3257                 else
3258                         attr->cat_nlink = inode->i_nlink;
3259
3260                 attr->cat_size += i_size_read(inode);
3261
3262                 if (attr->cat_atime < LTIME_S(inode->i_atime))
3263                         attr->cat_atime = LTIME_S(inode->i_atime);
3264
3265                 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3266                         attr->cat_ctime = LTIME_S(inode->i_ctime);
3267
3268                 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3269                         attr->cat_mtime = LTIME_S(inode->i_mtime);
3270         }
3271 #endif
3272         return 0;
3273 }
3274
3275 struct obd_ops lmv_obd_ops = {
3276         .o_owner                = THIS_MODULE,
3277         .o_setup                = lmv_setup,
3278         .o_cleanup              = lmv_cleanup,
3279         .o_precleanup           = lmv_precleanup,
3280         .o_process_config       = lmv_process_config,
3281         .o_connect              = lmv_connect,
3282         .o_disconnect           = lmv_disconnect,
3283         .o_statfs               = lmv_statfs,
3284         .o_get_info             = lmv_get_info,
3285         .o_set_info_async       = lmv_set_info_async,
3286         .o_packmd               = lmv_packmd,
3287         .o_unpackmd             = lmv_unpackmd,
3288         .o_notify               = lmv_notify,
3289         .o_get_uuid             = lmv_get_uuid,
3290         .o_iocontrol            = lmv_iocontrol,
3291         .o_quotacheck           = lmv_quotacheck,
3292         .o_quotactl             = lmv_quotactl
3293 };
3294
3295 struct md_ops lmv_md_ops = {
3296         .m_getstatus            = lmv_getstatus,
3297         .m_null_inode           = lmv_null_inode,
3298         .m_find_cbdata          = lmv_find_cbdata,
3299         .m_close                = lmv_close,
3300         .m_create               = lmv_create,
3301         .m_done_writing         = lmv_done_writing,
3302         .m_enqueue              = lmv_enqueue,
3303         .m_getattr              = lmv_getattr,
3304         .m_getxattr             = lmv_getxattr,
3305         .m_getattr_name         = lmv_getattr_name,
3306         .m_intent_lock          = lmv_intent_lock,
3307         .m_link                 = lmv_link,
3308         .m_rename               = lmv_rename,
3309         .m_setattr              = lmv_setattr,
3310         .m_setxattr             = lmv_setxattr,
3311         .m_fsync                = lmv_fsync,
3312         .m_read_entry           = lmv_read_entry,
3313         .m_unlink               = lmv_unlink,
3314         .m_init_ea_size         = lmv_init_ea_size,
3315         .m_cancel_unused        = lmv_cancel_unused,
3316         .m_set_lock_data        = lmv_set_lock_data,
3317         .m_lock_match           = lmv_lock_match,
3318         .m_get_lustre_md        = lmv_get_lustre_md,
3319         .m_free_lustre_md       = lmv_free_lustre_md,
3320         .m_update_lsm_md        = lmv_update_lsm_md,
3321         .m_merge_attr           = lmv_merge_attr,
3322         .m_set_open_replay_data = lmv_set_open_replay_data,
3323         .m_clear_open_replay_data = lmv_clear_open_replay_data,
3324         .m_renew_capa           = lmv_renew_capa,
3325         .m_unpack_capa          = lmv_unpack_capa,
3326         .m_get_remote_perm      = lmv_get_remote_perm,
3327         .m_intent_getattr_async = lmv_intent_getattr_async,
3328         .m_revalidate_lock      = lmv_revalidate_lock
3329 };
3330
3331 int __init lmv_init(void)
3332 {
3333         return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3334 #ifndef HAVE_ONLY_PROCFS_SEQ
3335                                    NULL,
3336 #endif
3337                                    LUSTRE_LMV_NAME, NULL);
3338 }
3339
3340 #ifdef __KERNEL__
3341 static void lmv_exit(void)
3342 {
3343         class_unregister_type(LUSTRE_LMV_NAME);
3344 }
3345
3346 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3347 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3348 MODULE_LICENSE("GPL");
3349
3350 module_init(lmv_init);
3351 module_exit(lmv_exit);
3352 #endif