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