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