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