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