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