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