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