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