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