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