Whamcloud - gitweb
LU-17662 osd-zfs: Support for ZFS 2.2.3
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_LMV
33
34 #include <linux/file.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/user_namespace.h>
38 #include <linux/uidgid.h>
39 #include <linux/slab.h>
40 #include <linux/pagemap.h>
41 #include <linux/mm.h>
42 #include <linux/math64.h>
43 #include <linux/seq_file.h>
44 #include <linux/namei.h>
45
46 #include <obd_support.h>
47 #include <lustre_lib.h>
48 #include <lustre_net.h>
49 #include <obd_class.h>
50 #include <lustre_lmv.h>
51 #include <lprocfs_status.h>
52 #include <cl_object.h>
53 #include <lustre_fid.h>
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include <lustre_ioctl_old.h>
56 #include <lustre_kernelcomm.h>
57 #include "lmv_internal.h"
58
59 static int lmv_check_connect(struct obd_device *obd);
60 static inline bool lmv_op_default_rr_mkdir(const struct md_op_data *op_data);
61
62 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
63                          int activate)
64 {
65         if (tgt->ltd_active == activate)
66                 return;
67
68         tgt->ltd_active = activate;
69         lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count +=
70                 (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 lu_tgt_desc *tgt = NULL;
87         struct obd_device *obd;
88         int rc = 0;
89
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         lmv_foreach_connected_tgt(lmv, tgt) {
97                 CDEBUG(D_INFO, "Target idx %d is %s conn %#llx\n",
98                        tgt->ltd_index, tgt->ltd_uuid.uuid,
99                        tgt->ltd_exp->exp_handle.h_cookie);
100
101                 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
102                         break;
103         }
104
105         if (!tgt)
106                 GOTO(out_lmv_lock, rc = -EINVAL);
107
108         obd = class_exp2obd(tgt->ltd_exp);
109         if (obd == NULL)
110                 GOTO(out_lmv_lock, rc = -ENOTCONN);
111
112         CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
113                obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
114                obd->obd_type->typ_name, tgt->ltd_index);
115         LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
116
117         if (tgt->ltd_active == activate) {
118                 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
119                        activate ? "" : "in");
120                 GOTO(out_lmv_lock, rc);
121         }
122
123         CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
124                activate ? "" : "in");
125         lmv_activate_target(lmv, tgt, activate);
126         EXIT;
127
128  out_lmv_lock:
129         spin_unlock(&lmv->lmv_lock);
130         return rc;
131 }
132
133 struct lu_tgt_desc *lmv_tgt_retry(struct lmv_obd *lmv, __u32 index)
134 {
135         struct obd_device *obd = lmv2obd_dev(lmv);
136         struct lu_tgt_desc *tgt;
137         static time64_t next_print;
138         time64_t retry_limit = 0;
139         time64_t now;
140         unsigned int level;
141         int rc;
142
143         might_sleep();
144 retry:
145         tgt = lmv_tgt(lmv, index);
146         if (likely(tgt && tgt->ltd_exp))
147                 return tgt;
148
149         now = ktime_get_seconds();
150         if (retry_limit == 0) {
151                 level = now > next_print ? D_WARNING : D_INFO;
152                 retry_limit = now + RECONNECT_DELAY_MAX;
153         } else if (now > retry_limit) {
154                 level = D_ERROR;
155         } else {
156                 level = D_INFO;
157         }
158         CDEBUG_LIMIT(level, index < lmv->lmv_mdt_count ?
159                      "%s: MDT index %u/%u not configured\n" :
160                      "%s: MDT index %u more than MDT count %u\n",
161                      obd->obd_name, index, lmv->lmv_mdt_count);
162
163         if (index >= LOV_V1_INSANE_STRIPE_COUNT)
164                 return NULL;
165
166         if (now > next_print) {
167                 LCONSOLE_INFO("%s: wait %ds while client connects to new MDT\n",
168                               obd->obd_name, (int)(retry_limit - now));
169                 next_print = retry_limit + 600;
170         }
171         if (now < retry_limit) {
172                 rc = schedule_timeout_interruptible(cfs_time_seconds(1));
173                 if (rc == 0)
174                         goto retry;
175         }
176
177         return NULL;
178 }
179
180 static struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
181 {
182         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
183         struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
184
185         return tgt ? obd_get_uuid(tgt->ltd_exp) : NULL;
186 }
187
188 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
189                       enum obd_notify_event ev)
190 {
191         struct obd_connect_data *conn_data;
192         struct lmv_obd          *lmv = &obd->u.lmv;
193         struct obd_uuid         *uuid;
194         int                      rc = 0;
195
196         ENTRY;
197
198         if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
199                 CERROR("unexpected notification of %s %s!\n",
200                        watched->obd_type->typ_name,
201                        watched->obd_name);
202                 RETURN(-EINVAL);
203         }
204
205         uuid = &watched->u.cli.cl_target_uuid;
206         if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
207                 /*
208                  * Set MDC as active before notifying the observer, so the
209                  * observer can use the MDC normally.
210                  */
211                 rc = lmv_set_mdc_active(lmv, uuid,
212                                         ev == OBD_NOTIFY_ACTIVE);
213                 if (rc) {
214                         CERROR("%sactivation of %s failed: %d\n",
215                                ev == OBD_NOTIFY_ACTIVE ? "" : "de",
216                                uuid->uuid, rc);
217                         RETURN(rc);
218                 }
219         } else if (ev == OBD_NOTIFY_OCD) {
220                 conn_data = &watched->u.cli.cl_import->imp_connect_data;
221                 /*
222                  * XXX: Make sure that ocd_connect_flags from all targets are
223                  * the same. Otherwise one of MDTs runs wrong version or
224                  * something like this.  --umka
225                  */
226                 obd->obd_self_export->exp_connect_data = *conn_data;
227         }
228
229         /* Pass the notification up the chain.  */
230         if (obd->obd_observer)
231                 rc = obd_notify(obd->obd_observer, watched, ev);
232
233         RETURN(rc);
234 }
235
236 static int lmv_connect(const struct lu_env *env,
237                        struct obd_export **pexp, struct obd_device *obd,
238                        struct obd_uuid *cluuid, struct obd_connect_data *data,
239                        void *localdata)
240 {
241         struct lmv_obd *lmv = &obd->u.lmv;
242         struct lustre_handle conn = { 0 };
243         struct obd_export *exp;
244         int rc;
245
246         ENTRY;
247
248         rc = class_connect(&conn, obd, cluuid);
249         if (rc) {
250                 CERROR("class_connection() returned %d\n", rc);
251                 RETURN(rc);
252         }
253
254         exp = class_conn2export(&conn);
255
256         lmv->connected = 0;
257         lmv->conn_data = *data;
258         lmv->lmv_cache = localdata;
259
260         lmv->lmv_tgts_kobj = kobject_create_and_add("target_obds",
261                                                     &obd->obd_kset.kobj);
262         if (!lmv->lmv_tgts_kobj) {
263                 CERROR("%s: cannot create /sys/fs/lustre/%s/%s/target_obds\n",
264                        obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
265         }
266
267         rc = lmv_check_connect(obd);
268         if (rc != 0)
269                 GOTO(out_sysfs, rc);
270
271         *pexp = exp;
272
273         RETURN(rc);
274
275 out_sysfs:
276         if (lmv->lmv_tgts_kobj)
277                 kobject_put(lmv->lmv_tgts_kobj);
278
279         class_disconnect(exp);
280
281         return rc;
282 }
283
284 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
285                             __u32 def_easize)
286 {
287         struct obd_device *obd = exp->exp_obd;
288         struct lmv_obd *lmv = &obd->u.lmv;
289         struct lmv_tgt_desc *tgt;
290         int change = 0;
291         int rc = 0;
292
293         ENTRY;
294
295         if (lmv->max_easize < easize) {
296                 lmv->max_easize = easize;
297                 change = 1;
298         }
299         if (lmv->max_def_easize < def_easize) {
300                 lmv->max_def_easize = def_easize;
301                 change = 1;
302         }
303
304         if (change == 0)
305                 RETURN(0);
306
307         if (lmv->connected == 0)
308                 RETURN(0);
309
310         lmv_foreach_connected_tgt(lmv, tgt) {
311                 if (!tgt->ltd_active)
312                         continue;
313
314                 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
315                 if (rc) {
316                         CERROR("%s: obd_init_ea_size() failed on MDT target %d: rc = %d\n",
317                                obd->obd_name, tgt->ltd_index, rc);
318                         break;
319                 }
320         }
321         RETURN(rc);
322 }
323
324 #define MAX_STRING_SIZE 128
325
326 static int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
327 {
328         struct lmv_obd *lmv = &obd->u.lmv;
329         struct obd_device *mdc_obd;
330         struct obd_export *mdc_exp;
331         struct lu_fld_target target;
332         int  rc;
333
334         ENTRY;
335
336         mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
337                                         &obd->obd_uuid);
338         if (!mdc_obd) {
339                 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
340                 RETURN(-EINVAL);
341         }
342
343         CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s\n",
344                mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
345                tgt->ltd_uuid.uuid, obd->obd_uuid.uuid);
346
347         if (!mdc_obd->obd_set_up) {
348                 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
349                 RETURN(-EINVAL);
350         }
351
352         rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
353                          &lmv->conn_data, lmv->lmv_cache);
354         if (rc) {
355                 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
356                 RETURN(rc);
357         }
358
359         /* Init fid sequence client for this mdc and add new fld target.  */
360         rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
361         if (rc)
362                 RETURN(rc);
363
364         target.ft_srv = NULL;
365         target.ft_exp = mdc_exp;
366         target.ft_idx = tgt->ltd_index;
367
368         fld_client_add_target(&lmv->lmv_fld, &target);
369
370         rc = obd_register_observer(mdc_obd, obd);
371         if (rc) {
372                 obd_disconnect(mdc_exp);
373                 CERROR("target %s register_observer error %d\n",
374                        tgt->ltd_uuid.uuid, rc);
375                 RETURN(rc);
376         }
377
378         if (obd->obd_observer) {
379                 /* Tell the observer about the new target.  */
380                 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
381                                 OBD_NOTIFY_ACTIVE);
382                 if (rc) {
383                         obd_disconnect(mdc_exp);
384                         RETURN(rc);
385                 }
386         }
387
388         tgt->ltd_active = 1;
389         tgt->ltd_exp = mdc_exp;
390         lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count++;
391
392         md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
393
394         rc = lu_qos_add_tgt(&lmv->lmv_qos, tgt);
395         if (rc) {
396                 obd_disconnect(mdc_exp);
397                 RETURN(rc);
398         }
399
400         CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
401                mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
402                kref_read(&obd->obd_refcount));
403
404         lmv_statfs_check_update(obd, tgt);
405
406         if (lmv->lmv_tgts_kobj)
407                 /* Even if we failed to create the link, that's fine */
408                 rc = sysfs_create_link(lmv->lmv_tgts_kobj,
409                                        &mdc_obd->obd_kset.kobj,
410                                        mdc_obd->obd_name);
411         RETURN(0);
412 }
413
414 static void lmv_del_target(struct lmv_obd *lmv, struct lu_tgt_desc *tgt)
415 {
416         LASSERT(tgt);
417         ltd_del_tgt(&lmv->lmv_mdt_descs, tgt);
418         OBD_FREE_PTR(tgt);
419 }
420
421 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
422                            __u32 index, int gen)
423 {
424         struct obd_device *mdc_obd;
425         struct lmv_obd *lmv = &obd->u.lmv;
426         struct lmv_tgt_desc *tgt;
427         struct lu_tgt_descs *ltd = &lmv->lmv_mdt_descs;
428         int rc = 0;
429
430         ENTRY;
431
432         CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
433         mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
434                                         &obd->obd_uuid);
435         if (!mdc_obd) {
436                 CERROR("%s: Target %s not attached: rc = %d\n",
437                        obd->obd_name, uuidp->uuid, -EINVAL);
438                 RETURN(-EINVAL);
439         }
440
441         OBD_ALLOC_PTR(tgt);
442         if (!tgt)
443                 RETURN(-ENOMEM);
444
445         mutex_init(&tgt->ltd_fid_mutex);
446         tgt->ltd_index = index;
447         tgt->ltd_uuid = *uuidp;
448         tgt->ltd_active = 0;
449
450         mutex_lock(&ltd->ltd_mutex);
451         rc = ltd_add_tgt(ltd, tgt);
452         mutex_unlock(&ltd->ltd_mutex);
453
454         if (rc)
455                 GOTO(out_tgt, rc);
456
457         if (!lmv->connected)
458                 /* lmv_check_connect() will connect this target. */
459                 RETURN(0);
460
461         rc = lmv_connect_mdc(obd, tgt);
462         if (!rc) {
463                 int easize = sizeof(struct lmv_stripe_md) +
464                         lmv->lmv_mdt_count * sizeof(struct lu_fid);
465
466                 lmv_init_ea_size(obd->obd_self_export, easize, 0);
467         }
468
469         RETURN(rc);
470
471 out_tgt:
472         OBD_FREE_PTR(tgt);
473         return rc;
474 }
475
476 static int lmv_check_connect(struct obd_device *obd)
477 {
478         struct lmv_obd *lmv = &obd->u.lmv;
479         struct lmv_tgt_desc *tgt;
480         int easize;
481         int rc;
482
483         ENTRY;
484
485         if (lmv->connected)
486                 RETURN(0);
487
488         mutex_lock(&lmv->lmv_mdt_descs.ltd_mutex);
489         if (lmv->connected)
490                 GOTO(unlock, rc = 0);
491
492         if (!lmv->lmv_mdt_count) {
493                 CERROR("%s: no targets configured: rc = -EINVAL\n",
494                        obd->obd_name);
495                 GOTO(unlock, rc = -EINVAL);
496         }
497
498         if (!lmv_mdt0_inited(lmv)) {
499                 CERROR("%s: no target configured for index 0: rc = -EINVAL.\n",
500                        obd->obd_name);
501                 GOTO(unlock, rc = -EINVAL);
502         }
503
504         CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
505                obd->obd_uuid.uuid, obd->obd_name);
506
507         lmv_foreach_tgt(lmv, tgt) {
508                 rc = lmv_connect_mdc(obd, tgt);
509                 if (rc)
510                         GOTO(out_disc, rc);
511         }
512
513         lmv->connected = 1;
514         easize = lmv_mds_md_size(lmv->lmv_mdt_count, LMV_MAGIC);
515         lmv_init_ea_size(obd->obd_self_export, easize, 0);
516         EXIT;
517 unlock:
518         mutex_unlock(&lmv->lmv_mdt_descs.ltd_mutex);
519
520         return rc;
521
522 out_disc:
523         lmv_foreach_tgt(lmv, tgt) {
524                 tgt->ltd_active = 0;
525                 if (!tgt->ltd_exp)
526                         continue;
527
528                 --lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count;
529                 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
530                 obd_disconnect(tgt->ltd_exp);
531         }
532
533         goto unlock;
534 }
535
536 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
537 {
538         struct lmv_obd *lmv = &obd->u.lmv;
539         struct obd_device *mdc_obd;
540         int rc;
541
542         ENTRY;
543
544         LASSERT(tgt != NULL);
545         LASSERT(obd != NULL);
546
547         mdc_obd = class_exp2obd(tgt->ltd_exp);
548
549         if (mdc_obd) {
550                 mdc_obd->obd_force = obd->obd_force;
551                 mdc_obd->obd_fail = obd->obd_fail;
552                 mdc_obd->obd_no_recov = obd->obd_no_recov;
553
554                 if (lmv->lmv_tgts_kobj)
555                         sysfs_remove_link(lmv->lmv_tgts_kobj,
556                                           mdc_obd->obd_name);
557         }
558
559         rc = lu_qos_del_tgt(&lmv->lmv_qos, tgt);
560         if (rc)
561                 CERROR("%s: Can't del target from QoS table: rc = %d\n",
562                        tgt->ltd_exp->exp_obd->obd_name, rc);
563
564         rc = fld_client_del_target(&lmv->lmv_fld, tgt->ltd_index);
565         if (rc)
566                 CERROR("%s: Can't del fld targets: rc = %d\n",
567                        tgt->ltd_exp->exp_obd->obd_name, rc);
568
569         rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
570         if (rc)
571                 CERROR("%s: Can't finalize fids factory: rc = %d\n",
572                        tgt->ltd_exp->exp_obd->obd_name, rc);
573
574         CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
575                tgt->ltd_exp->exp_obd->obd_name,
576                tgt->ltd_exp->exp_obd->obd_uuid.uuid);
577
578         lmv_activate_target(lmv, tgt, 0);
579         obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
580         rc = obd_disconnect(tgt->ltd_exp);
581         if (rc) {
582                 CERROR("%s: Target %s disconnect error: rc = %d\n",
583                        tgt->ltd_exp->exp_obd->obd_name,
584                        tgt->ltd_uuid.uuid, rc);
585         }
586         tgt->ltd_exp = NULL;
587         RETURN(0);
588 }
589
590 static int lmv_disconnect(struct obd_export *exp)
591 {
592         struct obd_device *obd = class_exp2obd(exp);
593         struct lmv_obd *lmv = &obd->u.lmv;
594         struct lmv_tgt_desc *tgt;
595         int rc;
596
597         ENTRY;
598
599         lmv_foreach_connected_tgt(lmv, tgt)
600                 lmv_disconnect_mdc(obd, tgt);
601
602         if (lmv->lmv_tgts_kobj)
603                 kobject_put(lmv->lmv_tgts_kobj);
604
605         lmv->connected = 0;
606         rc = class_disconnect(exp);
607
608         RETURN(rc);
609 }
610
611 static void lmv_statfs_update(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
612                               struct obd_statfs *osfs)
613 {
614         spin_lock(&lmv->lmv_lock);
615         tgt->ltd_statfs = *osfs;
616         tgt->ltd_statfs_age = ktime_get_seconds();
617         spin_unlock(&lmv->lmv_lock);
618         set_bit(LQ_DIRTY, &lmv->lmv_qos.lq_flags);
619 }
620
621 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
622                         void __user *uarg)
623 {
624         struct obd_device *obd = class_exp2obd(exp);
625         struct lmv_obd *lmv = &obd->u.lmv;
626         struct getinfo_fid2path *gf;
627         struct lmv_tgt_desc *tgt;
628         struct getinfo_fid2path *remote_gf = NULL;
629         struct lu_fid root_fid;
630         int remote_gf_size = 0;
631         int currentisenc = 0;
632         int globalisenc = 0;
633         int rc;
634
635         gf = karg;
636         tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
637         if (IS_ERR(tgt))
638                 RETURN(PTR_ERR(tgt));
639
640         root_fid = *gf->gf_u.gf_root_fid;
641         LASSERT(fid_is_sane(&root_fid));
642
643 repeat_fid2path:
644         rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
645         if (rc != 0 && rc != -EREMOTE)
646                 GOTO(out_fid2path, rc);
647
648         if (gf->gf_u.gf_path[0] == '/') {
649                 /* by convention, server side (mdt_path_current()) puts
650                  * a leading '/' to tell client that we are dealing with
651                  * an encrypted file
652                  */
653                 currentisenc = 1;
654                 globalisenc = 1;
655         } else {
656                 currentisenc = 0;
657         }
658
659         /* If remote_gf != NULL, it means just building the
660          * path on the remote MDT, copy this path segment to gf.
661          */
662         if (remote_gf != NULL) {
663                 struct getinfo_fid2path *ori_gf;
664                 int oldisenc = 0;
665                 char *ptr;
666                 int len;
667
668                 ori_gf = (struct getinfo_fid2path *)karg;
669                 if (strlen(ori_gf->gf_u.gf_path) + 1 +
670                     strlen(gf->gf_u.gf_path) + 1 > ori_gf->gf_pathlen)
671                         GOTO(out_fid2path, rc = -EOVERFLOW);
672
673                 ptr = ori_gf->gf_u.gf_path;
674                 oldisenc = ptr[0] == '/';
675
676                 len = strlen(gf->gf_u.gf_path);
677                 if (len) {
678                         /* move the current path to the right to release space
679                          * for closer-to-root part
680                          */
681                         memmove(ptr + len - currentisenc + 1 + globalisenc,
682                                 ptr + oldisenc,
683                                 strlen(ori_gf->gf_u.gf_path) - oldisenc + 1);
684                         if (globalisenc)
685                                 *(ptr++) = '/';
686                         memcpy(ptr, gf->gf_u.gf_path + currentisenc,
687                                len - currentisenc);
688                         ptr[len - currentisenc] = '/';
689                 }
690         }
691
692         CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
693                tgt->ltd_exp->exp_obd->obd_name,
694                gf->gf_u.gf_path, PFID(&gf->gf_fid), gf->gf_recno,
695                gf->gf_linkno);
696
697         if (rc == 0)
698                 GOTO(out_fid2path, rc);
699
700         /* sigh, has to go to another MDT to do path building further */
701         if (remote_gf == NULL) {
702                 remote_gf_size = sizeof(*remote_gf) + len - sizeof(*gf);
703                 OBD_ALLOC(remote_gf, remote_gf_size);
704                 if (remote_gf == NULL)
705                         GOTO(out_fid2path, rc = -ENOMEM);
706                 remote_gf->gf_pathlen = len - sizeof(*gf);
707         }
708
709         if (!fid_is_sane(&gf->gf_fid)) {
710                 CERROR("%s: invalid FID "DFID": rc = %d\n",
711                        tgt->ltd_exp->exp_obd->obd_name,
712                        PFID(&gf->gf_fid), -EINVAL);
713                 GOTO(out_fid2path, rc = -EINVAL);
714         }
715
716         tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
717         if (IS_ERR(tgt))
718                 GOTO(out_fid2path, rc = -EINVAL);
719
720         remote_gf->gf_fid = gf->gf_fid;
721         remote_gf->gf_recno = -1;
722         remote_gf->gf_linkno = -1;
723         memset(remote_gf->gf_u.gf_path, 0, remote_gf->gf_pathlen);
724         *remote_gf->gf_u.gf_root_fid = root_fid;
725         gf = remote_gf;
726         goto repeat_fid2path;
727
728 out_fid2path:
729         if (remote_gf != NULL)
730                 OBD_FREE(remote_gf, remote_gf_size);
731         RETURN(rc);
732 }
733
734 static int lmv_hsm_req_count(struct lmv_obd *lmv,
735                              const struct hsm_user_request *hur,
736                              const struct lmv_tgt_desc *tgt_mds)
737 {
738         struct lmv_tgt_desc *curr_tgt;
739         __u32 i;
740         int nr = 0;
741
742         /* count how many requests must be sent to the given target */
743         for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
744                 curr_tgt = lmv_fid2tgt(lmv, &hur->hur_user_item[i].hui_fid);
745                 if (IS_ERR(curr_tgt))
746                         RETURN(PTR_ERR(curr_tgt));
747                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
748                         nr++;
749         }
750         return nr;
751 }
752
753 static int lmv_hsm_req_build(struct lmv_obd *lmv,
754                               struct hsm_user_request *hur_in,
755                               const struct lmv_tgt_desc *tgt_mds,
756                               struct hsm_user_request *hur_out)
757 {
758         __u32 i, nr_out;
759         struct lmv_tgt_desc *curr_tgt;
760
761         /* build the hsm_user_request for the given target */
762         hur_out->hur_request = hur_in->hur_request;
763         nr_out = 0;
764         for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
765                 curr_tgt = lmv_fid2tgt(lmv, &hur_in->hur_user_item[i].hui_fid);
766                 if (IS_ERR(curr_tgt))
767                         RETURN(PTR_ERR(curr_tgt));
768                 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
769                         hur_out->hur_user_item[nr_out] =
770                                                 hur_in->hur_user_item[i];
771                         nr_out++;
772                 }
773         }
774         hur_out->hur_request.hr_itemcount = nr_out;
775         memcpy(hur_data(hur_out), hur_data(hur_in),
776                hur_in->hur_request.hr_data_len);
777
778         RETURN(0);
779 }
780
781 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
782                                  int len, struct lustre_kernelcomm *lk,
783                                  void __user *uarg)
784 {
785         struct lmv_obd *lmv = &obd->u.lmv;
786         struct lu_tgt_desc *tgt;
787         int rc;
788
789         ENTRY;
790
791         /* unregister request (call from llapi_hsm_copytool_fini) */
792         lmv_foreach_connected_tgt(lmv, tgt)
793                 /* try to clean as much as possible (continue on error) */
794                 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
795
796         /* Whatever the result, remove copytool from kuc groups.
797          * Unreached coordinators will get EPIPE on next requests
798          * and will unregister automatically.
799          */
800         rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
801
802         RETURN(rc);
803 }
804
805 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
806                                int len, struct lustre_kernelcomm *lk,
807                                void __user *uarg)
808 {
809         struct lmv_obd *lmv = &obd->u.lmv;
810         struct file *filp;
811         bool any_set = false;
812         struct kkuc_ct_data *kcd;
813         size_t kcd_size;
814         struct lu_tgt_desc *tgt;
815         __u32 i;
816         int err;
817         int rc = 0;
818
819         ENTRY;
820
821         filp = fget(lk->lk_wfd);
822         if (!filp)
823                 RETURN(-EBADF);
824
825         if (lk->lk_flags & LK_FLG_DATANR)
826                 kcd_size = offsetof(struct kkuc_ct_data,
827                                     kcd_archives[lk->lk_data_count]);
828         else
829                 kcd_size = sizeof(*kcd);
830
831         OBD_ALLOC(kcd, kcd_size);
832         if (kcd == NULL)
833                 GOTO(err_fput, rc = -ENOMEM);
834
835         kcd->kcd_nr_archives = lk->lk_data_count;
836         if (lk->lk_flags & LK_FLG_DATANR) {
837                 kcd->kcd_magic = KKUC_CT_DATA_ARRAY_MAGIC;
838                 if (lk->lk_data_count > 0)
839                         memcpy(kcd->kcd_archives, lk->lk_data,
840                                sizeof(*kcd->kcd_archives) * lk->lk_data_count);
841         } else {
842                 kcd->kcd_magic = KKUC_CT_DATA_BITMAP_MAGIC;
843         }
844
845         rc = libcfs_kkuc_group_add(filp, &obd->obd_uuid, lk->lk_uid,
846                                    lk->lk_group, kcd, kcd_size);
847         OBD_FREE(kcd, kcd_size);
848         if (rc)
849                 GOTO(err_fput, rc);
850
851         /* All or nothing: try to register to all MDS.
852          * In case of failure, unregister from previous MDS,
853          * except if it because of inactive target.
854          */
855         lmv_foreach_connected_tgt(lmv, tgt) {
856                 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
857                 if (err) {
858                         if (tgt->ltd_active) {
859                                 /* permanent error */
860                                 CERROR("%s: iocontrol MDC %s on MDT idx %d cmd %x: err = %d\n",
861                                        lmv2obd_dev(lmv)->obd_name,
862                                        tgt->ltd_uuid.uuid, tgt->ltd_index, cmd,
863                                        err);
864                                 rc = err;
865                                 lk->lk_flags |= LK_FLG_STOP;
866                                 i = tgt->ltd_index;
867                                 /* unregister from previous MDS */
868                                 lmv_foreach_connected_tgt(lmv, tgt) {
869                                         if (tgt->ltd_index >= i)
870                                                 break;
871
872                                         obd_iocontrol(cmd, tgt->ltd_exp, len,
873                                                       lk, uarg);
874                                 }
875                                 GOTO(err_kkuc_rem, rc);
876                         }
877                         /* else: transient error.
878                          * kuc will register to the missing MDT when it is back
879                          */
880                 } else {
881                         any_set = true;
882                 }
883         }
884
885         if (!any_set)
886                 /* no registration done: return error */
887                 GOTO(err_kkuc_rem, rc = -ENOTCONN);
888
889         RETURN(0);
890
891 err_kkuc_rem:
892         libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
893
894 err_fput:
895         fput(filp);
896         return rc;
897 }
898
899 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
900                          int len, void *karg, void __user *uarg)
901 {
902         struct obd_device *obd = class_exp2obd(exp);
903         struct lmv_obd *lmv = &obd->u.lmv;
904         struct lu_tgt_desc *tgt = NULL;
905         int set = 0;
906         __u32 count = lmv->lmv_mdt_count;
907         int rc = 0;
908
909         ENTRY;
910         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
911                exp->exp_obd->obd_name, cmd, len, karg, uarg);
912         if (count == 0)
913                 RETURN(-ENOTTY);
914
915         /* exit early for unknown ioctl types */
916         if (unlikely(_IOC_TYPE(cmd) != 'f' && !IOC_OSC_SET_ACTIVE_ALLOW(cmd)))
917                 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "unknown", -ENOTTY));
918
919         /* handle commands that don't use @karg first */
920         switch (cmd) {
921         case LL_IOC_GET_CONNECT_FLAGS:
922                 tgt = lmv_tgt(lmv, 0);
923                 rc = -ENODATA;
924                 if (tgt && tgt->ltd_exp)
925                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, NULL, uarg);
926                 RETURN(rc);
927         }
928
929         if (unlikely(karg == NULL))
930                 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", -EINVAL));
931
932         switch (cmd) {
933         case IOC_OBD_STATFS: {
934                 struct obd_ioctl_data *data = karg;
935                 struct obd_device *mdc_obd;
936                 struct obd_statfs stat_buf = {0};
937                 __u32 index;
938
939                 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
940
941                 if (index >= lmv->lmv_mdt_descs.ltd_tgts_size)
942                         RETURN(-ENODEV);
943
944                 tgt = lmv_tgt(lmv, index);
945                 if (!tgt)
946                         RETURN(-EAGAIN);
947
948                 if (!tgt->ltd_active)
949                         RETURN(-ENODATA);
950
951                 mdc_obd = class_exp2obd(tgt->ltd_exp);
952                 if (!mdc_obd)
953                         RETURN(-EINVAL);
954
955                 /* copy UUID */
956                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
957                                  min((int) data->ioc_plen2,
958                                      (int) sizeof(struct obd_uuid))))
959                         RETURN(-EFAULT);
960
961                 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
962                                 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
963                                 0);
964                 if (rc)
965                         RETURN(rc);
966                 lmv_statfs_update(lmv, tgt, &stat_buf);
967                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
968                                  min_t(int, data->ioc_plen1, sizeof(stat_buf))))
969                         RETURN(-EFAULT);
970                 break;
971         }
972         case OBD_IOC_QUOTACTL: {
973                 struct if_quotactl *qctl = karg;
974                 struct obd_quotactl *oqctl;
975                 struct obd_import *imp;
976
977                 if (qctl->qc_valid == QC_MDTIDX) {
978                         tgt = lmv_tgt(lmv, qctl->qc_idx);
979                 } else if (qctl->qc_valid == QC_UUID) {
980                         lmv_foreach_tgt(lmv, tgt) {
981                                 if (!obd_uuid_equals(&tgt->ltd_uuid,
982                                                      &qctl->obd_uuid))
983                                         continue;
984
985                                 if (!tgt->ltd_exp)
986                                         RETURN(-EINVAL);
987
988                                 break;
989                         }
990                 } else {
991                         RETURN(-EINVAL);
992                 }
993
994                 if (!tgt)
995                         RETURN(-ENODEV);
996
997                 if (!tgt->ltd_exp)
998                         RETURN(-EINVAL);
999
1000                 imp = class_exp2cliimp(tgt->ltd_exp);
1001                 if (!tgt->ltd_active && imp->imp_state != LUSTRE_IMP_IDLE) {
1002                         qctl->qc_valid = QC_MDTIDX;
1003                         qctl->obd_uuid = tgt->ltd_uuid;
1004                         RETURN(-ENODATA);
1005                 }
1006
1007                 OBD_ALLOC_PTR(oqctl);
1008                 if (!oqctl)
1009                         RETURN(-ENOMEM);
1010
1011                 QCTL_COPY(oqctl, qctl);
1012                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1013                 if (rc == 0) {
1014                         QCTL_COPY_NO_PNAME(qctl, oqctl);
1015                         qctl->qc_valid = QC_MDTIDX;
1016                         qctl->obd_uuid = tgt->ltd_uuid;
1017                 }
1018                 OBD_FREE_PTR(oqctl);
1019                 break;
1020         }
1021         case LL_IOC_FID2MDTIDX: {
1022                 struct lu_fid *fid = karg;
1023                 int             mdt_index;
1024
1025                 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1026                 if (rc != 0)
1027                         RETURN(rc);
1028
1029                 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1030                  * point to user space memory for FID2MDTIDX.
1031                  */
1032                 *(__u32 *)uarg = mdt_index;
1033                 break;
1034         }
1035         case OBD_IOC_FID2PATH: {
1036                 rc = lmv_fid2path(exp, len, karg, uarg);
1037                 break;
1038         }
1039         case LL_IOC_HSM_STATE_GET:
1040         case LL_IOC_HSM_STATE_SET:
1041         case LL_IOC_HSM_ACTION: {
1042                 struct md_op_data *op_data = karg;
1043
1044                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1045                 if (IS_ERR(tgt))
1046                         RETURN(PTR_ERR(tgt));
1047
1048                 if (tgt->ltd_exp == NULL)
1049                         RETURN(-EINVAL);
1050
1051                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1052                 break;
1053         }
1054         case LL_IOC_HSM_PROGRESS: {
1055                 const struct hsm_progress_kernel *hpk = karg;
1056
1057                 tgt = lmv_fid2tgt(lmv, &hpk->hpk_fid);
1058                 if (IS_ERR(tgt))
1059                         RETURN(PTR_ERR(tgt));
1060                 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1061                 break;
1062         }
1063         case LL_IOC_HSM_REQUEST: {
1064                 struct hsm_user_request *hur = karg;
1065                 unsigned int reqcount = hur->hur_request.hr_itemcount;
1066
1067                 if (reqcount == 0)
1068                         RETURN(0);
1069
1070                 /* if the request is about a single fid or if there is a single
1071                  * MDS, no need to split the request.
1072                  */
1073                 if (reqcount == 1 || count == 1) {
1074                         tgt = lmv_fid2tgt(lmv, &hur->hur_user_item[0].hui_fid);
1075                         if (IS_ERR(tgt))
1076                                 RETURN(PTR_ERR(tgt));
1077                         rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1078                 } else {
1079                         /* split fid list to their respective MDS */
1080                         lmv_foreach_connected_tgt(lmv, tgt) {
1081                                 int nr, rc1;
1082                                 size_t reqlen;
1083                                 struct hsm_user_request *req;
1084
1085                                 nr = lmv_hsm_req_count(lmv, hur, tgt);
1086                                 if (nr < 0)
1087                                         RETURN(nr);
1088                                 if (nr == 0) /* nothing for this MDS */
1089                                         continue;
1090
1091                                 /* build a request with fids for this MDS */
1092                                 reqlen = offsetof(typeof(*hur),
1093                                                   hur_user_item[nr])
1094                                                 + hur->hur_request.hr_data_len;
1095                                 OBD_ALLOC_LARGE(req, reqlen);
1096                                 if (req == NULL)
1097                                         RETURN(-ENOMEM);
1098                                 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1099                                 if (rc1 < 0)
1100                                         GOTO(hsm_req_err, rc1);
1101                                 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1102                                                     req, uarg);
1103 hsm_req_err:
1104                                 if (rc1 != 0 && rc == 0)
1105                                         rc = rc1;
1106                                 OBD_FREE_LARGE(req, reqlen);
1107                         }
1108                 }
1109                 break;
1110         }
1111         case LL_IOC_LOV_SWAP_LAYOUTS: {
1112                 struct md_op_data *op_data = karg;
1113                 struct lmv_tgt_desc *tgt1, *tgt2;
1114
1115                 tgt1 = lmv_fid2tgt(lmv, &op_data->op_fid1);
1116                 if (IS_ERR(tgt1))
1117                         RETURN(PTR_ERR(tgt1));
1118
1119                 tgt2 = lmv_fid2tgt(lmv, &op_data->op_fid2);
1120                 if (IS_ERR(tgt2))
1121                         RETURN(PTR_ERR(tgt2));
1122
1123                 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1124                         RETURN(-EINVAL);
1125
1126                 /* only files on same MDT can have their layouts swapped */
1127                 if (tgt1->ltd_index != tgt2->ltd_index)
1128                         RETURN(-EPERM);
1129
1130                 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1131                 break;
1132         }
1133         case LL_IOC_HSM_CT_START: {
1134                 struct lustre_kernelcomm *lk = karg;
1135
1136                 if (lk->lk_flags & LK_FLG_STOP)
1137                         rc = lmv_hsm_ct_unregister(obd, cmd, len, lk, uarg);
1138                 else
1139                         rc = lmv_hsm_ct_register(obd, cmd, len, lk, uarg);
1140                 break;
1141         }
1142         default:
1143                 lmv_foreach_connected_tgt(lmv, tgt) {
1144                         struct obd_device *mdc_obd;
1145                         int err;
1146
1147                         /* ll_umount_begin() sets force flag but for lmv, not
1148                          * mdc. Let's pass it through
1149                          */
1150                         mdc_obd = class_exp2obd(tgt->ltd_exp);
1151                         mdc_obd->obd_force = obd->obd_force;
1152                         err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1153                         if (err) {
1154                                 if (tgt->ltd_active) {
1155                                         OBD_IOC_ERROR(obd->obd_name, cmd,
1156                                                       tgt->ltd_uuid.uuid, err);
1157                                         if (!rc)
1158                                                 rc = err;
1159                                         if (unlikely(err == -ENOTTY))
1160                                                 break;
1161                                 }
1162                         } else {
1163                                 set = 1;
1164                         }
1165                 }
1166                 if (!set && !rc)
1167                         rc = -EIO;
1168                 break;
1169         }
1170         RETURN(rc);
1171 }
1172
1173 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1174                   struct lu_fid *fid, struct md_op_data *op_data)
1175 {
1176         struct obd_device *obd = class_exp2obd(exp);
1177         struct lmv_obd *lmv = &obd->u.lmv;
1178         struct lmv_tgt_desc *tgt;
1179         int rc;
1180
1181         ENTRY;
1182
1183         LASSERT(op_data);
1184         LASSERT(fid);
1185
1186         tgt = lmv_tgt(lmv, op_data->op_mds);
1187         if (!tgt)
1188                 RETURN(-ENODEV);
1189
1190         if (!tgt->ltd_active || !tgt->ltd_exp)
1191                 RETURN(-ENODEV);
1192
1193         /*
1194          * New seq alloc and FLD setup should be atomic. Otherwise we may find
1195          * on server that seq in new allocated fid is not yet known.
1196          */
1197         mutex_lock(&tgt->ltd_fid_mutex);
1198         rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1199         mutex_unlock(&tgt->ltd_fid_mutex);
1200         if (rc > 0) {
1201                 LASSERT(fid_is_sane(fid));
1202                 rc = 0;
1203         }
1204
1205         RETURN(rc);
1206 }
1207
1208 static u32 qos_exclude_hashfh(const void *data, u32 len, u32 seed)
1209 {
1210         const char *name = data;
1211
1212         return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
1213                                                name));
1214 }
1215
1216 static int qos_exclude_cmpfn(struct rhashtable_compare_arg *arg,
1217                              const void *obj)
1218 {
1219         const struct qos_exclude_prefix *prefix = obj;
1220         const char *name = arg->key;
1221
1222         return strcmp(name, prefix->qep_name);
1223 }
1224
1225 const struct rhashtable_params qos_exclude_hash_params = {
1226         .key_len        = 1, /* actually variable */
1227         .key_offset     = offsetof(struct qos_exclude_prefix, qep_name),
1228         .head_offset    = offsetof(struct qos_exclude_prefix, qep_hash),
1229         .hashfn         = qos_exclude_hashfh,
1230         .obj_cmpfn      = qos_exclude_cmpfn,
1231         .automatic_shrinking = true,
1232 };
1233
1234 void qos_exclude_prefix_free(void *vprefix, void *data)
1235 {
1236         struct qos_exclude_prefix *prefix = vprefix;
1237
1238         list_del(&prefix->qep_list);
1239         kfree(prefix);
1240 }
1241
1242 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1243 {
1244         struct lmv_obd *lmv = &obd->u.lmv;
1245         struct lmv_desc *desc;
1246         struct lnet_processid lnet_id;
1247         struct qos_exclude_prefix *prefix;
1248         int i = 0;
1249         int rc;
1250
1251         ENTRY;
1252
1253         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1254                 CERROR("LMV setup requires a descriptor\n");
1255                 RETURN(-EINVAL);
1256         }
1257
1258         desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1259         if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1260                 CERROR("Lmv descriptor size wrong: %d > %d\n",
1261                        (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1262                 RETURN(-EINVAL);
1263         }
1264
1265         obd_str2uuid(&lmv->lmv_mdt_descs.ltd_lmv_desc.ld_uuid,
1266                      desc->ld_uuid.uuid);
1267         lmv->lmv_mdt_descs.ltd_lmv_desc.ld_tgt_count = 0;
1268         lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count = 0;
1269         lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage =
1270                 LMV_DESC_QOS_MAXAGE_DEFAULT;
1271         lmv->max_def_easize = 0;
1272         lmv->max_easize = 0;
1273
1274         spin_lock_init(&lmv->lmv_lock);
1275         INIT_LIST_HEAD(&lmv->lmv_qos_exclude_list);
1276
1277         /*
1278          * initialize rr_index to lower 32bit of netid, so that client
1279          * can distribute subdirs evenly from the beginning.
1280          */
1281         while (LNetGetId(i++, &lnet_id, true) != -ENOENT) {
1282                 if (!nid_is_lo0(&lnet_id.nid)) {
1283                         lmv->lmv_qos_rr_index = nidhash(&lnet_id.nid);
1284                         break;
1285                 }
1286         }
1287
1288         rc = lmv_tunables_init(obd);
1289         if (rc)
1290                 CWARN("%s: error adding LMV sysfs/debugfs files: rc = %d\n",
1291                       obd->obd_name, rc);
1292
1293         rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1294                              LUSTRE_CLI_FLD_HASH_DHT);
1295         if (rc)
1296                 CERROR("Can't init FLD, err %d\n", rc);
1297
1298         rc = lu_tgt_descs_init(&lmv->lmv_mdt_descs, true);
1299         if (rc)
1300                 CWARN("%s: error initialize target table: rc = %d\n",
1301                       obd->obd_name, rc);
1302
1303         rc = rhashtable_init(&lmv->lmv_qos_exclude_hash,
1304                              &qos_exclude_hash_params);
1305         if (rc) {
1306                 CERROR("%s: qos exclude hash initalize failed: %d\n",
1307                        obd->obd_name, rc);
1308                 RETURN(rc);
1309         }
1310
1311         prefix = kmalloc(sizeof(*prefix), __GFP_ZERO);
1312         if (!prefix)
1313                 GOTO(out, rc = -ENOMEM);
1314         /* Apache Spark creates a _temporary directory for staging files */
1315         strcpy(prefix->qep_name, "_temporary");
1316         rc = rhashtable_insert_fast(&lmv->lmv_qos_exclude_hash,
1317                                     &prefix->qep_hash, qos_exclude_hash_params);
1318         if (rc) {
1319                 kfree(prefix);
1320                 GOTO(out, rc);
1321         }
1322
1323         list_add_tail(&prefix->qep_list, &lmv->lmv_qos_exclude_list);
1324         GOTO(out, rc);
1325 out:
1326         if (rc)
1327                 rhashtable_destroy(&lmv->lmv_qos_exclude_hash);
1328         return rc;
1329 }
1330
1331 static int lmv_cleanup(struct obd_device *obd)
1332 {
1333         struct lmv_obd *lmv = &obd->u.lmv;
1334         struct lu_tgt_desc *tgt;
1335         struct lu_tgt_desc *tmp;
1336
1337         ENTRY;
1338
1339         rhashtable_free_and_destroy(&lmv->lmv_qos_exclude_hash,
1340                                     qos_exclude_prefix_free, NULL);
1341         fld_client_fini(&lmv->lmv_fld);
1342         fld_client_debugfs_fini(&lmv->lmv_fld);
1343
1344         lprocfs_obd_cleanup(obd);
1345         lprocfs_free_md_stats(obd);
1346
1347         lmv_foreach_tgt_safe(lmv, tgt, tmp)
1348                 lmv_del_target(lmv, tgt);
1349         lu_tgt_descs_fini(&lmv->lmv_mdt_descs);
1350
1351         RETURN(0);
1352 }
1353
1354 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1355 {
1356         struct lustre_cfg       *lcfg = buf;
1357         struct obd_uuid         obd_uuid;
1358         int                     gen;
1359         __u32                   index;
1360         int                     rc;
1361
1362         ENTRY;
1363
1364         switch (lcfg->lcfg_command) {
1365         case LCFG_ADD_MDC:
1366                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
1367                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID
1368                  */
1369                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1370                         GOTO(out, rc = -EINVAL);
1371
1372                 obd_str2uuid(&obd_uuid,  lustre_cfg_buf(lcfg, 1));
1373
1374                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1375                         GOTO(out, rc = -EINVAL);
1376                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1377                         GOTO(out, rc = -EINVAL);
1378                 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1379                 GOTO(out, rc);
1380         default:
1381                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1382                 GOTO(out, rc = -EINVAL);
1383         }
1384 out:
1385         RETURN(rc);
1386 }
1387
1388 static int lmv_select_statfs_mdt(struct obd_export *exp, struct lmv_obd *lmv,
1389                                  u32 flags)
1390 {
1391         bool large_nid = exp_connect_flags2(exp) & OBD_CONNECT2_LARGE_NID;
1392         int i;
1393
1394         if (flags & OBD_STATFS_FOR_MDT0)
1395                 return 0;
1396
1397         if (lmv->lmv_statfs_start || lmv->lmv_mdt_count == 1)
1398                 return lmv->lmv_statfs_start;
1399
1400         /* choose initial MDT for this client */
1401         for (i = 0;; i++) {
1402                 struct lnet_processid lnet_id;
1403
1404                 if (LNetGetId(i, &lnet_id, large_nid) == -ENOENT)
1405                         break;
1406
1407                 if (!nid_is_lo0(&lnet_id.nid)) {
1408                         /* We dont need a full 64-bit modulus, just enough
1409                          * to distribute the requests across MDTs evenly.
1410                          */
1411                         lmv->lmv_statfs_start = nidhash(&lnet_id.nid) %
1412                                                 lmv->lmv_mdt_count;
1413                         break;
1414                 }
1415         }
1416
1417         return lmv->lmv_statfs_start;
1418 }
1419
1420 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1421                       struct obd_statfs *osfs, time64_t max_age, __u32 flags)
1422 {
1423         struct obd_device *obd = class_exp2obd(exp);
1424         struct lmv_obd *lmv = &obd->u.lmv;
1425         struct obd_statfs *temp;
1426         struct lu_tgt_desc *tgt;
1427         __u32 i;
1428         __u32 idx;
1429         int rc = 0;
1430         int err = 0;
1431
1432         ENTRY;
1433
1434         OBD_ALLOC(temp, sizeof(*temp));
1435         if (temp == NULL)
1436                 RETURN(-ENOMEM);
1437
1438         /* distribute statfs among MDTs */
1439         idx = lmv_select_statfs_mdt(exp, lmv, flags);
1440
1441         for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++, idx++) {
1442                 idx = idx % lmv->lmv_mdt_descs.ltd_tgts_size;
1443                 tgt = lmv_tgt(lmv, idx);
1444                 if (!tgt || !tgt->ltd_exp)
1445                         continue;
1446
1447                 rc = obd_statfs(env, tgt->ltd_exp, temp, max_age,
1448                                 flags | OBD_STATFS_NESTED);
1449                 if (rc) {
1450                         CERROR("%s: can't stat MDS #%d: rc = %d\n",
1451                                tgt->ltd_exp->exp_obd->obd_name, i, rc);
1452                         err = rc;
1453                         /* Try another MDT */
1454                         if (flags & OBD_STATFS_SUM)
1455                                 continue;
1456                         GOTO(out_free_temp, rc);
1457                 }
1458
1459                 if (temp->os_state & OS_STATFS_SUM ||
1460                     flags == OBD_STATFS_FOR_MDT0) {
1461                         /* reset to the last aggregated values and don't sum
1462                          * with non-aggrated data
1463                          *
1464                          * If the statfs is from mount, it needs to retrieve
1465                          * necessary info from MDT0. i.e. mount does not need
1466                          * the merged osfs from all of MDT. Also clients can be
1467                          * mounted as long as MDT0 is in service
1468                          */
1469                         *osfs = *temp;
1470                         GOTO(out_free_temp, rc);
1471                 }
1472
1473                 if (i == 0) {
1474                         *osfs = *temp;
1475                 } else {
1476                         osfs->os_bavail += temp->os_bavail;
1477                         osfs->os_blocks += temp->os_blocks;
1478                         osfs->os_ffree += temp->os_ffree;
1479                         osfs->os_files += temp->os_files;
1480                         osfs->os_granted += temp->os_granted;
1481                 }
1482         }
1483         /* There is no stats from some MDTs, data incomplete */
1484         if (err)
1485                 rc = err;
1486 out_free_temp:
1487         OBD_FREE(temp, sizeof(*temp));
1488         RETURN(rc);
1489 }
1490
1491 static int lmv_statfs_cb(void *cookie, int rc)
1492 {
1493         struct obd_info *oinfo = cookie;
1494         struct obd_device *obd = oinfo->oi_obd;
1495         struct lmv_obd *lmv = &obd->u.lmv;
1496         struct lmv_tgt_desc *tgt = oinfo->oi_tgt;
1497         struct obd_statfs *osfs = oinfo->oi_osfs;
1498
1499         /*
1500          * NB: don't deactivate TGT upon error, because we may not trigger async
1501          * statfs any longer, then there is no chance to activate TGT.
1502          */
1503         if (!rc)
1504                 lmv_statfs_update(lmv, tgt, osfs);
1505
1506         return rc;
1507 }
1508
1509 /* update tgt statfs async if it's ld_qos_maxage old */
1510 int lmv_statfs_check_update(struct obd_device *obd, struct lmv_tgt_desc *tgt)
1511 {
1512         struct obd_info oinfo = {
1513                 .oi_obd = obd,
1514                 .oi_tgt = tgt,
1515                 .oi_cb_up = lmv_statfs_cb,
1516         };
1517         int rc;
1518
1519         if (ktime_get_seconds() - tgt->ltd_statfs_age <
1520             obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage)
1521                 return 0;
1522
1523         rc = obd_statfs_async(tgt->ltd_exp, &oinfo, 0, NULL);
1524
1525         return rc;
1526 }
1527
1528 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1529                         struct lu_fid *fid)
1530 {
1531         struct obd_device *obd = exp->exp_obd;
1532         struct lmv_obd *lmv = &obd->u.lmv;
1533         struct lu_tgt_desc *tgt = lmv_tgt(lmv, 0);
1534         int rc;
1535
1536         ENTRY;
1537
1538         if (!tgt)
1539                 RETURN(-ENODEV);
1540
1541         rc = md_get_root(tgt->ltd_exp, fileset, fid);
1542         if (!rc)
1543                 lmv->lmv_setup_time = ktime_get_seconds();
1544         RETURN(rc);
1545 }
1546
1547 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1548                         u64 obd_md_valid, const char *name, size_t buf_size,
1549                         struct ptlrpc_request **req)
1550 {
1551         struct obd_device *obd = exp->exp_obd;
1552         struct lmv_obd *lmv = &obd->u.lmv;
1553         struct lmv_tgt_desc *tgt;
1554         int rc;
1555
1556         ENTRY;
1557
1558         tgt = lmv_fid2tgt(lmv, fid);
1559         if (IS_ERR(tgt))
1560                 RETURN(PTR_ERR(tgt));
1561
1562         rc = md_getxattr(tgt->ltd_exp, fid, obd_md_valid, name, buf_size, req);
1563
1564         RETURN(rc);
1565 }
1566
1567 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1568                         u64 obd_md_valid, const char *name,
1569                         const void *value, size_t value_size,
1570                         unsigned int xattr_flags, u32 suppgid,
1571                         struct ptlrpc_request **req)
1572 {
1573         struct obd_device *obd = exp->exp_obd;
1574         struct lmv_obd *lmv = &obd->u.lmv;
1575         struct lmv_tgt_desc *tgt;
1576         int rc;
1577
1578         ENTRY;
1579
1580         tgt = lmv_fid2tgt(lmv, fid);
1581         if (IS_ERR(tgt))
1582                 RETURN(PTR_ERR(tgt));
1583
1584         rc = md_setxattr(tgt->ltd_exp, fid, obd_md_valid, name,
1585                          value, value_size, xattr_flags, suppgid, req);
1586
1587         RETURN(rc);
1588 }
1589
1590 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1591                        struct ptlrpc_request **request)
1592 {
1593         struct obd_device *obd = exp->exp_obd;
1594         struct lmv_obd *lmv = &obd->u.lmv;
1595         struct lmv_tgt_desc *tgt;
1596         int rc;
1597
1598         ENTRY;
1599
1600         tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1601         if (IS_ERR(tgt))
1602                 RETURN(PTR_ERR(tgt));
1603
1604         if (op_data->op_flags & MF_GET_MDT_IDX) {
1605                 op_data->op_mds = tgt->ltd_index;
1606                 RETURN(0);
1607         }
1608
1609         rc = md_getattr(tgt->ltd_exp, op_data, request);
1610
1611         RETURN(rc);
1612 }
1613
1614 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1615 {
1616         struct obd_device *obd = exp->exp_obd;
1617         struct lmv_obd *lmv = &obd->u.lmv;
1618         struct lu_tgt_desc *tgt;
1619
1620         ENTRY;
1621
1622         CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1623
1624         /*
1625          * With DNE every object can have two locks in different namespaces:
1626          * lookup lock in space of MDT storing direntry and update/open lock in
1627          * space of MDT storing inode.
1628          */
1629         lmv_foreach_connected_tgt(lmv, tgt)
1630                 md_null_inode(tgt->ltd_exp, fid);
1631
1632         RETURN(0);
1633 }
1634
1635 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1636                      struct md_open_data *mod, struct ptlrpc_request **request)
1637 {
1638         struct obd_device *obd = exp->exp_obd;
1639         struct lmv_obd *lmv = &obd->u.lmv;
1640         struct lmv_tgt_desc *tgt;
1641         int rc;
1642
1643         ENTRY;
1644
1645         tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1646         if (IS_ERR(tgt))
1647                 RETURN(PTR_ERR(tgt));
1648
1649         CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1650         rc = md_close(tgt->ltd_exp, op_data, mod, request);
1651         RETURN(rc);
1652 }
1653
1654 static inline bool tgt_qos_is_usable(struct lmv_obd *lmv,
1655                                      struct lu_tgt_desc *tgt, time64_t now)
1656 {
1657         struct obd_import *imp = class_exp2cliimp(tgt->ltd_exp);
1658         u32 maxage = lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage;
1659
1660         return tgt->ltd_exp && tgt->ltd_active &&
1661                !(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE) &&
1662                (now - imp->imp_setup_time > (maxage >> 1) ||
1663                 now - lmv->lmv_setup_time < (maxage << 1));
1664 }
1665
1666 static struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv,
1667                                               struct md_op_data *op_data)
1668 {
1669         struct lu_tgt_desc *tgt, *cur = NULL;
1670         time64_t now = ktime_get_seconds();
1671         __u64 total_avail = 0;
1672         __u64 total_weight = 0;
1673         __u64 cur_weight = 0;
1674         int total_usable = 0;
1675         __u64 rand;
1676         int rc;
1677
1678         ENTRY;
1679
1680         if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1681                 RETURN(ERR_PTR(-EAGAIN));
1682
1683         down_write(&lmv->lmv_qos.lq_rw_sem);
1684
1685         if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1686                 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1687
1688         rc = ltd_qos_penalties_calc(&lmv->lmv_mdt_descs);
1689         if (rc)
1690                 GOTO(unlock, tgt = ERR_PTR(rc));
1691
1692         lmv_foreach_tgt(lmv, tgt) {
1693                 if (!tgt_qos_is_usable(lmv, tgt, now)) {
1694                         tgt->ltd_qos.ltq_usable = 0;
1695                         continue;
1696                 }
1697                 /* update one hour overdue statfs */
1698                 if (now - tgt->ltd_statfs_age >
1699                     60 * lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage)
1700                         lmv_statfs_check_update(lmv2obd_dev(lmv), tgt);
1701                 tgt->ltd_qos.ltq_usable = 1;
1702                 lu_tgt_qos_weight_calc(tgt, true);
1703                 if (tgt->ltd_index == op_data->op_mds)
1704                         cur = tgt;
1705                 total_avail += tgt->ltd_qos.ltq_avail;
1706                 total_weight += tgt->ltd_qos.ltq_weight;
1707                 total_usable++;
1708         }
1709
1710         /* If current MDT has above-average space and dir is not already using
1711          * round-robin to spread across more MDTs, stay on the parent MDT
1712          * to avoid creating needless remote MDT directories.  Remote dirs
1713          * close to the root balance space more effectively than bottom dirs,
1714          * so prefer to create remote dirs at top level of directory tree.
1715          * "16 / (dir_depth + 10)" is the factor to make it less likely
1716          * for top-level directories to stay local unless they have more than
1717          * average free space, while deep dirs prefer local until more full.
1718          *    depth=0 -> 160%, depth=3 -> 123%, depth=6 -> 100%,
1719          *    depth=9 -> 84%, depth=12 -> 73%, depth=15 -> 64%
1720          */
1721         if (!lmv_op_default_rr_mkdir(op_data)) {
1722                 rand = total_avail * 16 /
1723                         (total_usable * (op_data->op_dir_depth + 10));
1724                 if (cur && cur->ltd_qos.ltq_avail >= rand) {
1725                         tgt = cur;
1726                         GOTO(unlock, tgt);
1727                 }
1728         }
1729
1730         rand = lu_prandom_u64_max(total_weight);
1731
1732         lmv_foreach_connected_tgt(lmv, tgt) {
1733                 if (!tgt->ltd_qos.ltq_usable)
1734                         continue;
1735
1736                 cur_weight += tgt->ltd_qos.ltq_weight;
1737                 if (cur_weight < rand)
1738                         continue;
1739
1740                 ltd_qos_update(&lmv->lmv_mdt_descs, tgt, &total_weight);
1741                 GOTO(unlock, tgt);
1742         }
1743
1744         /* no proper target found */
1745         GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1746 unlock:
1747         up_write(&lmv->lmv_qos.lq_rw_sem);
1748
1749         return tgt;
1750 }
1751
1752 static struct lu_tgt_desc *lmv_locate_tgt_rr(struct lmv_obd *lmv)
1753 {
1754         time64_t now = ktime_get_seconds();
1755         struct lu_tgt_desc *tgt;
1756         int i;
1757         int index;
1758
1759         ENTRY;
1760
1761         spin_lock(&lmv->lmv_lock);
1762         for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++) {
1763                 index = (i + lmv->lmv_qos_rr_index) %
1764                         lmv->lmv_mdt_descs.ltd_tgts_size;
1765                 tgt = lmv_tgt(lmv, index);
1766                 if (!tgt || !tgt_qos_is_usable(lmv, tgt, now))
1767                         continue;
1768
1769                 lmv->lmv_qos_rr_index = (tgt->ltd_index + 1) %
1770                                         lmv->lmv_mdt_descs.ltd_tgts_size;
1771                 spin_unlock(&lmv->lmv_lock);
1772
1773                 RETURN(tgt);
1774         }
1775         spin_unlock(&lmv->lmv_lock);
1776
1777         RETURN(ERR_PTR(-ENODEV));
1778 }
1779
1780 /* locate MDT which is less full (avoid the most full MDT) */
1781 static struct lu_tgt_desc *lmv_locate_tgt_lf(struct lmv_obd *lmv)
1782 {
1783         struct lu_tgt_desc *min = NULL;
1784         struct lu_tgt_desc *tgt;
1785         __u64 avail = 0;
1786         __u64 rand;
1787
1788         ENTRY;
1789
1790         if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1791                 RETURN(ERR_PTR(-EAGAIN));
1792
1793         down_write(&lmv->lmv_qos.lq_rw_sem);
1794
1795         if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1796                 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1797
1798         lmv_foreach_tgt(lmv, tgt) {
1799                 if (!tgt->ltd_exp || !tgt->ltd_active ||
1800                     (tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE)) {
1801                         tgt->ltd_qos.ltq_usable = 0;
1802                         continue;
1803                 }
1804
1805                 tgt->ltd_qos.ltq_usable = 1;
1806                 lu_tgt_qos_weight_calc(tgt, true);
1807                 avail += tgt->ltd_qos.ltq_avail;
1808                 if (!min || min->ltd_qos.ltq_avail > tgt->ltd_qos.ltq_avail)
1809                         min = tgt;
1810         }
1811
1812         /* avoid the most full MDT */
1813         if (min)
1814                 avail -= min->ltd_qos.ltq_avail;
1815
1816         rand = lu_prandom_u64_max(avail);
1817         avail = 0;
1818         lmv_foreach_connected_tgt(lmv, tgt) {
1819                 if (!tgt->ltd_qos.ltq_usable)
1820                         continue;
1821
1822                 if (tgt == min)
1823                         continue;
1824
1825                 avail += tgt->ltd_qos.ltq_avail;
1826                 if (avail < rand)
1827                         continue;
1828
1829                 GOTO(unlock, tgt);
1830         }
1831
1832         /* no proper target found */
1833         GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1834 unlock:
1835         up_write(&lmv->lmv_qos.lq_rw_sem);
1836
1837         RETURN(tgt);
1838 }
1839
1840 /* locate MDT by file name, for striped directory, the file name hash decides
1841  * which stripe its dirent is stored.
1842  */
1843 static struct lmv_tgt_desc *
1844 lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_object *lso,
1845                        const char *name, int namelen, struct lu_fid *fid,
1846                        __u32 *mds, bool new_layout)
1847 {
1848         struct lmv_tgt_desc *tgt;
1849         const struct lmv_oinfo *oinfo;
1850
1851         if (!lmv_dir_striped(lso) || !namelen) {
1852                 tgt = lmv_fid2tgt(lmv, fid);
1853                 if (IS_ERR(tgt))
1854                         return tgt;
1855
1856                 *mds = tgt->ltd_index;
1857                 return tgt;
1858         }
1859
1860         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1861                 if (cfs_fail_val >= lso->lso_lsm.lsm_md_stripe_count)
1862                         return ERR_PTR(-EBADF);
1863                 oinfo = &lso->lso_lsm.lsm_md_oinfo[cfs_fail_val];
1864         } else {
1865                 oinfo = lsm_name_to_stripe_info(lso, name, namelen, new_layout);
1866                 if (IS_ERR(oinfo))
1867                         return ERR_CAST(oinfo);
1868         }
1869
1870         /* check stripe FID is sane */
1871         if (!fid_is_sane(&oinfo->lmo_fid))
1872                 return ERR_PTR(-ENODEV);
1873
1874         *fid = oinfo->lmo_fid;
1875         *mds = oinfo->lmo_mds;
1876         tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
1877
1878         CDEBUG(D_INODE, "locate MDT %u parent "DFID"\n", *mds, PFID(fid));
1879
1880         return tgt ? tgt : ERR_PTR(-ENODEV);
1881 }
1882
1883 /**
1884  * Locate MDT of op_data->op_fid1
1885  *
1886  * For striped directory, it will locate the stripe by name hash, if hash_type
1887  * is unknown, it will return the stripe specified by 'op_data->op_stripe_index'
1888  * which is set outside, and if dir is migrating, 'op_data->op_new_layout'
1889  * indicates whether old or new layout is used to locate.
1890  *
1891  * For plain directory, it just locate the MDT of op_data->op_fid1.
1892  *
1893  * \param[in] lmv               LMV device
1894  * \param[in/out] op_data       client MD stack parameters, name, namelen etc,
1895  *                              op_mds and op_fid1 will be updated if op_lso1
1896  *                              indicates fid1 represents a striped directory.
1897  *
1898  * retval               pointer to the lmv_tgt_desc if succeed.
1899  *                      ERR_PTR(errno) if failed.
1900  */
1901 struct lmv_tgt_desc *
1902 lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
1903 {
1904         struct lmv_stripe_md *lsm;
1905         struct lmv_oinfo *oinfo;
1906         struct lmv_tgt_desc *tgt;
1907
1908         if (lmv_dir_foreign(op_data->op_lso1))
1909                 return ERR_PTR(-ENODATA);
1910
1911         /* During creating VOLATILE file, it should honor the mdt
1912          * index if the file under striped dir is being restored, see
1913          * ct_restore().
1914          */
1915         if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1916             op_data->op_mds != LMV_OFFSET_DEFAULT) {
1917                 tgt = lmv_tgt(lmv, op_data->op_mds);
1918                 if (!tgt)
1919                         return ERR_PTR(-ENODEV);
1920
1921                 if (lmv_dir_striped(op_data->op_lso1)) {
1922                         int i;
1923
1924                         /* refill the right parent fid */
1925                         lsm = &op_data->op_lso1->lso_lsm;
1926                         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1927                                 oinfo = &lsm->lsm_md_oinfo[i];
1928                                 if (oinfo->lmo_mds == op_data->op_mds) {
1929                                         op_data->op_fid1 = oinfo->lmo_fid;
1930                                         break;
1931                                 }
1932                         }
1933
1934                         if (i == lsm->lsm_md_stripe_count)
1935                                 op_data->op_fid1 = lsm->lsm_md_oinfo[0].lmo_fid;
1936                 }
1937         } else if (lmv_dir_bad_hash(op_data->op_lso1)) {
1938                 lsm = &op_data->op_lso1->lso_lsm;
1939
1940                 LASSERT(op_data->op_stripe_index < lsm->lsm_md_stripe_count);
1941                 oinfo = &lsm->lsm_md_oinfo[op_data->op_stripe_index];
1942
1943                 op_data->op_fid1 = oinfo->lmo_fid;
1944                 op_data->op_mds = oinfo->lmo_mds;
1945                 tgt = lmv_tgt(lmv, oinfo->lmo_mds);
1946                 if (!tgt)
1947                         return ERR_PTR(-ENODEV);
1948         } else {
1949                 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_lso1,
1950                                 op_data->op_name, op_data->op_namelen,
1951                                 &op_data->op_fid1, &op_data->op_mds,
1952                                 op_data->op_new_layout);
1953         }
1954
1955         return tgt;
1956 }
1957
1958 /* Locate MDT of op_data->op_fid2 for link/rename */
1959 static struct lmv_tgt_desc *
1960 lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
1961 {
1962         struct lmv_tgt_desc *tgt;
1963         int rc;
1964
1965         LASSERT(op_data->op_name);
1966         if (lmv_dir_layout_changing(op_data->op_lso2)) {
1967                 struct lu_fid fid1 = op_data->op_fid1;
1968                 struct lmv_stripe_object *lso1 = op_data->op_lso1;
1969                 struct ptlrpc_request *request = NULL;
1970
1971                 /*
1972                  * avoid creating new file under old layout of migrating
1973                  * directory, check it here.
1974                  */
1975                 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_lso2,
1976                                 op_data->op_name, op_data->op_namelen,
1977                                 &op_data->op_fid2, &op_data->op_mds, false);
1978                 if (IS_ERR(tgt))
1979                         RETURN(tgt);
1980
1981                 op_data->op_fid1 = op_data->op_fid2;
1982                 op_data->op_lso1 = op_data->op_lso2;
1983                 rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
1984                 op_data->op_fid1 = fid1;
1985                 op_data->op_lso1 = lso1;
1986                 if (!rc) {
1987                         ptlrpc_req_put(request);
1988                         RETURN(ERR_PTR(-EEXIST));
1989                 }
1990
1991                 if (rc != -ENOENT)
1992                         RETURN(ERR_PTR(rc));
1993         }
1994
1995         return lmv_locate_tgt_by_name(lmv, op_data->op_lso2,
1996                                       op_data->op_name, op_data->op_namelen,
1997                                       &op_data->op_fid2, &op_data->op_mds,
1998                                       true);
1999 }
2000
2001 int lmv_old_layout_lookup(struct lmv_obd *lmv, struct md_op_data *op_data)
2002 {
2003         struct lu_tgt_desc *tgt;
2004         struct ptlrpc_request *request;
2005         int rc;
2006
2007         LASSERT(lmv_dir_layout_changing(op_data->op_lso1));
2008         LASSERT(!op_data->op_new_layout);
2009
2010         tgt = lmv_locate_tgt(lmv, op_data);
2011         if (IS_ERR(tgt))
2012                 return PTR_ERR(tgt);
2013
2014         rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
2015         if (!rc) {
2016                 ptlrpc_req_put(request);
2017                 return -EEXIST;
2018         }
2019
2020         return rc;
2021 }
2022
2023 /* mkdir by QoS upon 'lfs mkdir -i -1'.
2024  *
2025  * NB, mkdir by QoS only if parent is not striped, this is to avoid remote
2026  * directories under striped directory.
2027  */
2028 static inline bool lmv_op_user_qos_mkdir(const struct md_op_data *op_data)
2029 {
2030         const struct lmv_user_md *lum = op_data->op_data;
2031
2032         if (op_data->op_code != LUSTRE_OPC_MKDIR)
2033                 return false;
2034
2035         if (lmv_dir_striped(op_data->op_lso1))
2036                 return false;
2037
2038         return (op_data->op_cli_flags & CLI_SET_MEA) && lum &&
2039                le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC &&
2040                le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
2041 }
2042
2043 /* mkdir by QoS if either ROOT or parent default LMV is space balanced. */
2044 static inline bool lmv_op_default_qos_mkdir(const struct md_op_data *op_data)
2045 {
2046         const struct lmv_stripe_object *lso = op_data->op_default_lso1;
2047
2048         if (op_data->op_code != LUSTRE_OPC_MKDIR)
2049                 return false;
2050
2051         if (lmv_dir_striped(op_data->op_lso1))
2052                 return false;
2053
2054         return (op_data->op_flags & MF_QOS_MKDIR) ||
2055                (lso && lso->lso_lsm.lsm_md_master_mdt_index ==
2056                 LMV_OFFSET_DEFAULT);
2057 }
2058
2059 /* if parent default LMV is space balanced, and
2060  * 1. max_inherit_rr is set
2061  * 2. or parent is ROOT
2062  * mkdir roundrobin. Or if parent doesn't have default LMV, while ROOT default
2063  * LMV requests roundrobin mkdir, do the same.
2064  * NB, this needs to check server is balanced, which is done by caller.
2065  */
2066 static inline bool lmv_op_default_rr_mkdir(const struct md_op_data *op_data)
2067 {
2068         const struct lmv_stripe_object *lso = op_data->op_default_lso1;
2069
2070         return (op_data->op_flags & MF_RR_MKDIR) ||
2071                (lso && lso->lso_lsm.lsm_md_max_inherit_rr !=
2072                 LMV_INHERIT_RR_NONE) || fid_is_root(&op_data->op_fid1);
2073 }
2074
2075 /* 'lfs mkdir -i <specific_MDT>' */
2076 static inline bool lmv_op_user_specific_mkdir(const struct md_op_data *op_data)
2077 {
2078         const struct lmv_user_md *lum = op_data->op_data;
2079
2080         return op_data->op_code == LUSTRE_OPC_MKDIR &&
2081                op_data->op_cli_flags & CLI_SET_MEA && lum &&
2082                (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2083                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) &&
2084                le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2085 }
2086
2087 /* parent default LMV master_mdt_index is not -1. */
2088 static inline bool
2089 lmv_op_default_specific_mkdir(const struct md_op_data *op_data)
2090 {
2091         return op_data->op_code == LUSTRE_OPC_MKDIR &&
2092                op_data->op_default_lso1 &&
2093                op_data->op_default_lso1->lso_lsm.lsm_md_master_mdt_index !=
2094                         LMV_OFFSET_DEFAULT;
2095 }
2096
2097 /* locate MDT by space usage */
2098 static struct lu_tgt_desc *lmv_locate_tgt_by_space(struct lmv_obd *lmv,
2099                                                    struct md_op_data *op_data,
2100                                                    struct lmv_tgt_desc *tgt)
2101 {
2102         struct lmv_tgt_desc *tmp = tgt;
2103
2104         tgt = lmv_locate_tgt_qos(lmv, op_data);
2105         if (tgt == ERR_PTR(-EAGAIN)) {
2106                 if (ltd_qos_is_balanced(&lmv->lmv_mdt_descs) &&
2107                     !lmv_op_default_rr_mkdir(op_data) &&
2108                     !lmv_op_user_qos_mkdir(op_data) &&
2109                     !(tmp->ltd_statfs.os_state & OS_STATFS_NOCREATE)) {
2110                         /* if not necessary, don't create remote directory. */
2111                         tgt = tmp;
2112                 } else {
2113                         tgt = lmv_locate_tgt_rr(lmv);
2114                         /* if no MDT chosen, use parent MDT */
2115                         if (IS_ERR(tgt))
2116                                 tgt = tmp;
2117                 }
2118                 if (!IS_ERR(tgt))
2119                         lmv_statfs_check_update(lmv2obd_dev(lmv), tgt);
2120         }
2121
2122         if (!IS_ERR(tgt))
2123                 op_data->op_mds = tgt->ltd_index;
2124
2125         /* If space balance was called because the original target was marked
2126          * NOCREATE, periodically check whether the state has changed.
2127          */
2128         if (tmp != tgt && tmp->ltd_statfs.os_state & OS_STATFS_NOCREATE)
2129                 lmv_statfs_check_update(lmv2obd_dev(lmv), tmp);
2130
2131         return tgt;
2132 }
2133
2134 static bool lmv_qos_exclude(struct lmv_obd *lmv, struct md_op_data *op_data)
2135 {
2136         const char *name = op_data->op_name;
2137         size_t namelen = op_data->op_namelen;
2138         char buf[NAME_MAX + 1];
2139         struct qos_exclude_prefix *prefix;
2140         char *p;
2141
2142         /* skip encrypted files */
2143         if (op_data->op_file_encctx)
2144                 return false;
2145
2146         /* name length may not be validated yet */
2147         if (namelen > NAME_MAX)
2148                 return false;
2149
2150         p = strrchr(name, '.');
2151         if (p) {
2152                 namelen = p - name;
2153                 if (!namelen)
2154                         return false;
2155                 strncpy(buf, name, namelen);
2156                 buf[namelen] = '\0';
2157                 name = buf;
2158         }
2159
2160         prefix = rhashtable_lookup_fast(&lmv->lmv_qos_exclude_hash, name,
2161                                         qos_exclude_hash_params);
2162         return prefix != NULL;
2163 }
2164
2165 struct lmv_tgt_desc *lmv_locate_tgt_create(struct obd_device *obd,
2166                                            struct lmv_obd *lmv,
2167                                            struct md_op_data *op_data)
2168 {
2169         struct lmv_tgt_desc *tgt;
2170
2171         ENTRY;
2172
2173         tgt = lmv_locate_tgt(lmv, op_data);
2174         if (IS_ERR(tgt))
2175                 RETURN(tgt);
2176
2177         /* the order to apply policy in mkdir:
2178          * 1. is "lfs mkdir -i N"? mkdir on MDT N.
2179          * 2. is "lfs mkdir -i -1"? mkdir by space usage.
2180          * 3. is starting MDT specified in default LMV? mkdir on MDT N.
2181          * 4. is default LMV space balanced? mkdir by space usage.
2182          *
2183          * If the existing parent or specific MDT selected is deactivated
2184          * with OS_STATFS_NOCREATE then select a different MDT by QOS.
2185          */
2186         if (lmv_op_user_specific_mkdir(op_data)) {
2187                 struct lmv_user_md *lum = op_data->op_data;
2188
2189                 op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
2190                 tgt = lmv_tgt(lmv, op_data->op_mds);
2191                 if (!tgt)
2192                         RETURN(ERR_PTR(-ENODEV));
2193                 if (unlikely(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE))
2194                         GOTO(new_tgt, -EAGAIN);
2195         } else if (lmv_op_user_qos_mkdir(op_data)) {
2196                 tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
2197                 if (IS_ERR(tgt))
2198                         RETURN(tgt);
2199         } else if (lmv_op_default_specific_mkdir(op_data)) {
2200                 struct lmv_stripe_md *lsm = &op_data->op_default_lso1->lso_lsm;
2201
2202                 op_data->op_mds = lsm->lsm_md_master_mdt_index;
2203                 tgt = lmv_tgt(lmv, op_data->op_mds);
2204                 if (!tgt)
2205                         RETURN(ERR_PTR(-ENODEV));
2206                 if (unlikely(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE))
2207                         GOTO(new_tgt, -EAGAIN);
2208         } else if ((lmv_op_default_qos_mkdir(op_data) &&
2209                     !lmv_qos_exclude(lmv, op_data)) ||
2210                    tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE) {
2211 new_tgt:
2212                 tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
2213                 if (IS_ERR(tgt))
2214                         RETURN(tgt);
2215         }
2216
2217         RETURN(tgt);
2218 }
2219
2220 static int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
2221                       const void *data, size_t datalen, umode_t mode, uid_t uid,
2222                       gid_t gid, kernel_cap_t cap_effective, __u64 rdev,
2223                       struct ptlrpc_request **request)
2224 {
2225         struct obd_device *obd = exp->exp_obd;
2226         struct lmv_obd *lmv = &obd->u.lmv;
2227         struct lmv_tgt_desc *tgt;
2228         struct mdt_body *repbody;
2229         int rc;
2230
2231         ENTRY;
2232
2233         if (!lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count)
2234                 RETURN(-EIO);
2235
2236         if (lmv_dir_bad_hash(op_data->op_lso1))
2237                 RETURN(-EBADF);
2238
2239         if (lmv_dir_layout_changing(op_data->op_lso1)) {
2240                 /*
2241                  * if parent is migrating, create() needs to lookup existing
2242                  * name in both old and new layout, check old layout on client.
2243                  */
2244                 rc = lmv_old_layout_lookup(lmv, op_data);
2245                 if (rc != -ENOENT)
2246                         RETURN(rc);
2247
2248                 op_data->op_new_layout = true;
2249         }
2250
2251         tgt = lmv_locate_tgt_create(obd, lmv, op_data);
2252         if (IS_ERR(tgt))
2253                 RETURN(PTR_ERR(tgt));
2254
2255 retry:
2256         rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
2257         if (rc)
2258                 RETURN(rc);
2259
2260         CDEBUG(D_INODE, "CREATE name '%.*s' "DFID" on "DFID" -> mds #%x\n",
2261                 (int)op_data->op_namelen, op_data->op_name,
2262                 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1),
2263                 op_data->op_mds);
2264
2265         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2266         rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
2267                        cap_effective, rdev, request);
2268         if (rc == 0) {
2269                 if (*request == NULL)
2270                         RETURN(rc);
2271                 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
2272         }
2273
2274         /* dir restripe needs to send to MDT where dir is located */
2275         if (rc != -EREMOTE ||
2276             !(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH))
2277                 RETURN(rc);
2278
2279         repbody = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2280         if (repbody == NULL)
2281                 RETURN(-EPROTO);
2282
2283         /* Not cross-ref case, just get out of here. */
2284         if (likely(!(repbody->mbo_valid & OBD_MD_MDS)))
2285                 RETURN(rc);
2286
2287         op_data->op_fid2 = repbody->mbo_fid1;
2288         ptlrpc_req_put(*request);
2289         *request = NULL;
2290
2291         tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
2292         if (IS_ERR(tgt))
2293                 RETURN(PTR_ERR(tgt));
2294
2295         op_data->op_mds = tgt->ltd_index;
2296         goto retry;
2297 }
2298
2299 static int
2300 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
2301             const union ldlm_policy_data *policy, struct md_op_data *op_data,
2302             struct lustre_handle *lockh, __u64 extra_lock_flags)
2303 {
2304         struct obd_device *obd = exp->exp_obd;
2305         struct lmv_obd *lmv = &obd->u.lmv;
2306         struct lmv_tgt_desc *tgt;
2307         int rc;
2308
2309         ENTRY;
2310
2311         CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
2312
2313         tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2314         if (IS_ERR(tgt))
2315                 RETURN(PTR_ERR(tgt));
2316
2317         CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
2318                PFID(&op_data->op_fid1), tgt->ltd_index);
2319
2320         rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
2321                         extra_lock_flags);
2322
2323         RETURN(rc);
2324 }
2325
2326 int
2327 lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
2328                  struct ptlrpc_request **preq)
2329 {
2330         struct obd_device *obd = exp->exp_obd;
2331         struct lmv_obd *lmv = &obd->u.lmv;
2332         struct lmv_tgt_desc *tgt;
2333         struct mdt_body *body;
2334         int rc;
2335
2336         ENTRY;
2337
2338 retry:
2339         if (op_data->op_namelen == 2 &&
2340             op_data->op_name[0] == '.' && op_data->op_name[1] == '.')
2341                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2342         else
2343                 tgt = lmv_locate_tgt(lmv, op_data);
2344         if (IS_ERR(tgt))
2345                 RETURN(PTR_ERR(tgt));
2346
2347         CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
2348                 (int)op_data->op_namelen, op_data->op_name,
2349                 PFID(&op_data->op_fid1), tgt->ltd_index);
2350
2351         rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
2352         if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2353                 ptlrpc_req_put(*preq);
2354                 *preq = NULL;
2355                 goto retry;
2356         }
2357
2358         if (rc)
2359                 RETURN(rc);
2360
2361         body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
2362         LASSERT(body != NULL);
2363
2364         if (body->mbo_valid & OBD_MD_MDS) {
2365                 op_data->op_fid1 = body->mbo_fid1;
2366                 op_data->op_valid |= OBD_MD_FLCROSSREF;
2367                 op_data->op_namelen = 0;
2368                 op_data->op_name = NULL;
2369
2370                 ptlrpc_req_put(*preq);
2371                 *preq = NULL;
2372
2373                 goto retry;
2374         }
2375
2376         RETURN(rc);
2377 }
2378
2379 #define md_op_data_fid(op_data, fl)                     \
2380         (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
2381          fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
2382          fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
2383          fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
2384          NULL)
2385
2386 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
2387                             struct md_op_data *op_data, __u32 op_tgt,
2388                             enum ldlm_mode mode, int bits, int flag)
2389 {
2390         struct lu_fid *fid = md_op_data_fid(op_data, flag);
2391         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2392         union ldlm_policy_data policy = { { 0 } };
2393         int rc = 0;
2394
2395         ENTRY;
2396
2397         if (!fid_is_sane(fid))
2398                 RETURN(0);
2399
2400         if (tgt == NULL) {
2401                 tgt = lmv_fid2tgt(lmv, fid);
2402                 if (IS_ERR(tgt))
2403                         RETURN(PTR_ERR(tgt));
2404         }
2405
2406         if (tgt->ltd_index != op_tgt) {
2407                 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
2408                 policy.l_inodebits.bits = bits;
2409                 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
2410                                       mode, LCF_ASYNC, NULL);
2411         } else {
2412                 CDEBUG(D_INODE,
2413                        "EARLY_CANCEL skip operation target %d on "DFID"\n",
2414                        op_tgt, PFID(fid));
2415                 op_data->op_flags |= flag;
2416                 rc = 0;
2417         }
2418
2419         RETURN(rc);
2420 }
2421
2422 /*
2423  * llite passes fid of an target inode in op_data->op_fid1 and id of directory
2424  * in op_data->op_fid2
2425  */
2426 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
2427                     struct ptlrpc_request **request)
2428 {
2429         struct obd_device       *obd = exp->exp_obd;
2430         struct lmv_obd          *lmv = &obd->u.lmv;
2431         struct lmv_tgt_desc     *tgt;
2432         int                      rc;
2433
2434         ENTRY;
2435
2436         LASSERT(op_data->op_namelen != 0);
2437
2438         CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2439                PFID(&op_data->op_fid2), (int)op_data->op_namelen,
2440                op_data->op_name, PFID(&op_data->op_fid1));
2441
2442         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2443         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2444         op_data->op_cap = current_cap();
2445
2446         tgt = lmv_locate_tgt2(lmv, op_data);
2447         if (IS_ERR(tgt))
2448                 RETURN(PTR_ERR(tgt));
2449
2450         /* Cancel UPDATE lock on child (fid1).  */
2451         op_data->op_flags |= MF_MDC_CANCEL_FID2;
2452         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
2453                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2454         if (rc != 0)
2455                 RETURN(rc);
2456
2457         rc = md_link(tgt->ltd_exp, op_data, request);
2458
2459         RETURN(rc);
2460 }
2461
2462 /* migrate the top directory */
2463 static inline bool lmv_op_topdir_migrate(const struct md_op_data *op_data)
2464 {
2465         if (!S_ISDIR(op_data->op_mode))
2466                 return false;
2467
2468         if (lmv_dir_layout_changing(op_data->op_lso1))
2469                 return false;
2470
2471         return true;
2472 }
2473
2474 /* migrate top dir to specific MDTs */
2475 static inline bool lmv_topdir_specific_migrate(const struct md_op_data *op_data)
2476 {
2477         const struct lmv_user_md *lum = op_data->op_data;
2478
2479         if (!lmv_op_topdir_migrate(op_data))
2480                 return false;
2481
2482         return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2483 }
2484
2485 /* migrate top dir in QoS mode if user issued "lfs migrate -m -1..." */
2486 static inline bool lmv_topdir_qos_migrate(const struct md_op_data *op_data)
2487 {
2488         const struct lmv_user_md *lum = op_data->op_data;
2489
2490         if (!lmv_op_topdir_migrate(op_data))
2491                 return false;
2492
2493         return le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
2494 }
2495
2496 static inline bool lmv_subdir_specific_migrate(const struct md_op_data *op_data)
2497 {
2498         const struct lmv_user_md *lum = op_data->op_data;
2499
2500         if (!S_ISDIR(op_data->op_mode))
2501                 return false;
2502
2503         if (!lmv_dir_layout_changing(op_data->op_lso1))
2504                 return false;
2505
2506         return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2507 }
2508
2509 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
2510                         const char *name, size_t namelen,
2511                         struct ptlrpc_request **request)
2512 {
2513         struct obd_device *obd = exp->exp_obd;
2514         struct lmv_obd *lmv = &obd->u.lmv;
2515         struct lmv_stripe_object *lso = op_data->op_lso1;
2516         struct lmv_tgt_desc *parent_tgt;
2517         struct lmv_tgt_desc *sp_tgt;
2518         struct lmv_tgt_desc *tp_tgt = NULL;
2519         struct lmv_tgt_desc *child_tgt;
2520         struct lmv_tgt_desc *tgt;
2521         struct lu_fid target_fid = { 0 };
2522         int rc;
2523
2524         ENTRY;
2525
2526         LASSERT(op_data->op_cli_flags & CLI_MIGRATE);
2527
2528         CDEBUG(D_INODE, "MIGRATE "DFID"/%.*s\n",
2529                PFID(&op_data->op_fid1), (int)namelen, name);
2530
2531         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2532         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2533         op_data->op_cap = current_cap();
2534
2535         parent_tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2536         if (IS_ERR(parent_tgt))
2537                 RETURN(PTR_ERR(parent_tgt));
2538
2539         if (lmv_dir_striped(lso)) {
2540                 const struct lmv_oinfo *oinfo;
2541
2542                 oinfo = lsm_name_to_stripe_info(lso, name, namelen, false);
2543                 if (IS_ERR(oinfo))
2544                         RETURN(PTR_ERR(oinfo));
2545
2546                 /* save source stripe FID in fid4 temporarily for ELC */
2547                 op_data->op_fid4 = oinfo->lmo_fid;
2548                 sp_tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
2549                 if (!sp_tgt)
2550                         RETURN(-ENODEV);
2551
2552                 /*
2553                  * if parent is being migrated too, fill op_fid2 with target
2554                  * stripe fid, otherwise the target stripe is not created yet.
2555                  */
2556                 if (lmv_dir_layout_changing(lso)) {
2557                         oinfo = lsm_name_to_stripe_info(lso, name, namelen,
2558                                                         true);
2559                         if (IS_ERR(oinfo))
2560                                 RETURN(PTR_ERR(oinfo));
2561
2562                         op_data->op_fid2 = oinfo->lmo_fid;
2563                         tp_tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
2564                         if (!tp_tgt)
2565                                 RETURN(-ENODEV);
2566
2567                         /* parent unchanged and update namespace only */
2568                         if (lu_fid_eq(&op_data->op_fid4, &op_data->op_fid2) &&
2569                             op_data->op_bias & MDS_MIGRATE_NSONLY)
2570                                 RETURN(-EALREADY);
2571                 }
2572         } else {
2573                 sp_tgt = parent_tgt;
2574         }
2575
2576         child_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2577         if (IS_ERR(child_tgt))
2578                 RETURN(PTR_ERR(child_tgt));
2579
2580         if (lmv_topdir_specific_migrate(op_data)) {
2581                 struct lmv_user_md *lum = op_data->op_data;
2582
2583                 op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
2584         } else if (lmv_topdir_qos_migrate(op_data)) {
2585                 tgt = lmv_locate_tgt_lf(lmv);
2586                 if (tgt == ERR_PTR(-EAGAIN))
2587                         tgt = lmv_locate_tgt_rr(lmv);
2588                 if (IS_ERR(tgt))
2589                         RETURN(PTR_ERR(tgt));
2590
2591                 op_data->op_mds = tgt->ltd_index;
2592         } else if (lmv_subdir_specific_migrate(op_data)) {
2593                 struct lmv_user_md *lum = op_data->op_data;
2594                 __u32 i;
2595
2596                 LASSERT(tp_tgt);
2597                 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2598                         /* adjust MDTs in lum, since subdir is located on where
2599                          * its parent stripe is, not the first specified MDT.
2600                          */
2601                         for (i = 0; i < le32_to_cpu(lum->lum_stripe_count);
2602                              i++) {
2603                                 if (le32_to_cpu(lum->lum_objects[i].lum_mds) ==
2604                                     tp_tgt->ltd_index)
2605                                         break;
2606                         }
2607
2608                         if (i == le32_to_cpu(lum->lum_stripe_count))
2609                                 RETURN(-ENODEV);
2610
2611                         lum->lum_objects[i].lum_mds =
2612                                 lum->lum_objects[0].lum_mds;
2613                         lum->lum_objects[0].lum_mds =
2614                                 cpu_to_le32(tp_tgt->ltd_index);
2615                 }
2616                 /* NB, the above adjusts subdir migration for command like
2617                  * "lfs migrate -m 0,1,2 ...", but for migration like
2618                  * "lfs migrate -m 0 -c 2 ...", the top dir is migrated to MDT0
2619                  * and MDT1, however its subdir may be migrated to MDT1 and MDT2
2620                  */
2621
2622                 lum->lum_stripe_offset = cpu_to_le32(tp_tgt->ltd_index);
2623                 op_data->op_mds = tp_tgt->ltd_index;
2624         } else if (tp_tgt) {
2625                 op_data->op_mds = tp_tgt->ltd_index;
2626         } else {
2627                 op_data->op_mds = sp_tgt->ltd_index;
2628         }
2629
2630         rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
2631         if (rc)
2632                 RETURN(rc);
2633
2634         /*
2635          * for directory, send migrate request to the MDT where the object will
2636          * be migrated to, because we can't create a striped directory remotely.
2637          *
2638          * otherwise, send to the MDT where source is located because regular
2639          * file may open lease.
2640          *
2641          * NB. if MDT doesn't support DIR_MIGRATE, send to source MDT too for
2642          * backward compatibility.
2643          */
2644         if (S_ISDIR(op_data->op_mode) &&
2645             (exp_connect_flags2(exp) & OBD_CONNECT2_DIR_MIGRATE)) {
2646                 tgt = lmv_fid2tgt(lmv, &target_fid);
2647                 if (IS_ERR(tgt))
2648                         RETURN(PTR_ERR(tgt));
2649         } else {
2650                 tgt = child_tgt;
2651         }
2652
2653         /* cancel UPDATE lock of parent master object */
2654         rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index, LCK_EX,
2655                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2656         if (rc)
2657                 RETURN(rc);
2658
2659         /* cancel UPDATE lock of source parent */
2660         if (sp_tgt != parent_tgt) {
2661                 /*
2662                  * migrate RPC packs master object FID, because we can only pack
2663                  * two FIDs in reint RPC, but MDS needs to know both source
2664                  * parent and target parent, and it will obtain them from master
2665                  * FID and LMV, the other FID in RPC is kept for target.
2666                  *
2667                  * since this FID is not passed to MDC, cancel it anyway.
2668                  */
2669                 rc = lmv_early_cancel(exp, sp_tgt, op_data, -1, LCK_EX,
2670                                       MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID4);
2671                 if (rc)
2672                         RETURN(rc);
2673
2674                 op_data->op_flags &= ~MF_MDC_CANCEL_FID4;
2675         }
2676         op_data->op_fid4 = target_fid;
2677
2678         /* cancel UPDATE locks of target parent */
2679         rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2680                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2681         if (rc)
2682                 RETURN(rc);
2683
2684         /* cancel LOOKUP lock of source if source is remote object */
2685         if (child_tgt != sp_tgt) {
2686                 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index,
2687                                       LCK_EX, MDS_INODELOCK_LOOKUP,
2688                                       MF_MDC_CANCEL_FID3);
2689                 if (rc)
2690                         RETURN(rc);
2691         }
2692
2693         /* cancel ELC locks of source */
2694         rc = lmv_early_cancel(exp, child_tgt, op_data, tgt->ltd_index, LCK_EX,
2695                               MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2696         if (rc)
2697                 RETURN(rc);
2698
2699         rc = md_rename(tgt->ltd_exp, op_data, name, namelen, NULL, 0, request);
2700
2701         RETURN(rc);
2702 }
2703
2704 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2705                       const char *old, size_t oldlen,
2706                       const char *new, size_t newlen,
2707                       struct ptlrpc_request **request)
2708 {
2709         struct obd_device *obd = exp->exp_obd;
2710         struct lmv_obd *lmv = &obd->u.lmv;
2711         struct lmv_tgt_desc *sp_tgt;
2712         struct lmv_tgt_desc *tp_tgt = NULL;
2713         struct lmv_tgt_desc *src_tgt = NULL;
2714         struct lmv_tgt_desc *tgt;
2715         struct mdt_body *body;
2716         int rc;
2717
2718         ENTRY;
2719
2720         LASSERT(oldlen != 0);
2721
2722         if (op_data->op_cli_flags & CLI_MIGRATE) {
2723                 rc = lmv_migrate(exp, op_data, old, oldlen, request);
2724                 RETURN(rc);
2725         }
2726
2727         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2728         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2729         op_data->op_cap = current_cap();
2730
2731         op_data->op_name = new;
2732         op_data->op_namelen = newlen;
2733
2734         tp_tgt = lmv_locate_tgt2(lmv, op_data);
2735         if (IS_ERR(tp_tgt))
2736                 RETURN(PTR_ERR(tp_tgt));
2737
2738         /* Since the target child might be destroyed, and it might become
2739          * orphan, and we can only check orphan on the local MDT right now, so
2740          * we send rename request to the MDT where target child is located. If
2741          * target child does not exist, then it will send the request to the
2742          * target parent
2743          */
2744         if (fid_is_sane(&op_data->op_fid4)) {
2745                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2746                 if (IS_ERR(tgt))
2747                         RETURN(PTR_ERR(tgt));
2748         } else {
2749                 tgt = tp_tgt;
2750         }
2751
2752         op_data->op_flags |= MF_MDC_CANCEL_FID4;
2753
2754         /* cancel UPDATE locks of target parent */
2755         rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2756                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2757         if (rc != 0)
2758                 RETURN(rc);
2759
2760         if (fid_is_sane(&op_data->op_fid4)) {
2761                 /* cancel LOOKUP lock of target on target parent */
2762                 if (tgt != tp_tgt) {
2763                         rc = lmv_early_cancel(exp, tp_tgt, op_data,
2764                                               tgt->ltd_index, LCK_EX,
2765                                               MDS_INODELOCK_LOOKUP,
2766                                               MF_MDC_CANCEL_FID4);
2767                         if (rc != 0)
2768                                 RETURN(rc);
2769                 }
2770         }
2771
2772         if (fid_is_sane(&op_data->op_fid3)) {
2773                 src_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2774                 if (IS_ERR(src_tgt))
2775                         RETURN(PTR_ERR(src_tgt));
2776
2777                 /* cancel ELC locks of source */
2778                 rc = lmv_early_cancel(exp, src_tgt, op_data, tgt->ltd_index,
2779                                       LCK_EX, MDS_INODELOCK_ELC,
2780                                       MF_MDC_CANCEL_FID3);
2781                 if (rc != 0)
2782                         RETURN(rc);
2783         }
2784
2785         op_data->op_name = old;
2786         op_data->op_namelen = oldlen;
2787 retry:
2788         sp_tgt = lmv_locate_tgt(lmv, op_data);
2789         if (IS_ERR(sp_tgt))
2790                 RETURN(PTR_ERR(sp_tgt));
2791
2792         /* cancel UPDATE locks of source parent */
2793         rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index, LCK_EX,
2794                               MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2795         if (rc != 0)
2796                 RETURN(rc);
2797
2798         if (fid_is_sane(&op_data->op_fid3)) {
2799                 /* cancel LOOKUP lock of source on source parent */
2800                 if (src_tgt != sp_tgt) {
2801                         rc = lmv_early_cancel(exp, sp_tgt, op_data,
2802                                               tgt->ltd_index, LCK_EX,
2803                                               MDS_INODELOCK_LOOKUP,
2804                                               MF_MDC_CANCEL_FID3);
2805                         if (rc != 0)
2806                                 RETURN(rc);
2807                 }
2808         }
2809
2810 rename:
2811         CDEBUG(D_INODE, "RENAME "DFID"/%.*s to "DFID"/%.*s\n",
2812                 PFID(&op_data->op_fid1), (int)oldlen, old,
2813                 PFID(&op_data->op_fid2), (int)newlen, new);
2814
2815         rc = md_rename(tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2816                         request);
2817         if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2818                 ptlrpc_req_put(*request);
2819                 *request = NULL;
2820                 goto retry;
2821         }
2822
2823         if (rc && rc != -EXDEV)
2824                 RETURN(rc);
2825
2826         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2827         if (body == NULL)
2828                 RETURN(-EPROTO);
2829
2830         /* Not cross-ref case, just get out of here. */
2831         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2832                 RETURN(rc);
2833
2834         op_data->op_fid4 = body->mbo_fid1;
2835
2836         ptlrpc_req_put(*request);
2837         *request = NULL;
2838
2839         tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2840         if (IS_ERR(tgt))
2841                 RETURN(PTR_ERR(tgt));
2842
2843         if (fid_is_sane(&op_data->op_fid4)) {
2844                 /* cancel LOOKUP lock of target on target parent */
2845                 if (tgt != tp_tgt) {
2846                         rc = lmv_early_cancel(exp, tp_tgt, op_data,
2847                                               tgt->ltd_index, LCK_EX,
2848                                               MDS_INODELOCK_LOOKUP,
2849                                               MF_MDC_CANCEL_FID4);
2850                         if (rc != 0)
2851                                 RETURN(rc);
2852                 }
2853         }
2854
2855         goto rename;
2856 }
2857
2858 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2859                        void *ea, size_t ealen, struct ptlrpc_request **request)
2860 {
2861         struct obd_device *obd = exp->exp_obd;
2862         struct lmv_obd *lmv = &obd->u.lmv;
2863         struct lmv_tgt_desc *tgt;
2864         int rc = 0;
2865
2866         ENTRY;
2867
2868         CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x/0x%x\n",
2869                PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2870                op_data->op_xvalid);
2871
2872         op_data->op_flags |= MF_MDC_CANCEL_FID1;
2873         tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2874         if (IS_ERR(tgt))
2875                 RETURN(PTR_ERR(tgt));
2876
2877         rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2878
2879         RETURN(rc);
2880 }
2881
2882 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2883                      struct ptlrpc_request **request)
2884 {
2885         struct obd_device *obd = exp->exp_obd;
2886         struct lmv_obd *lmv = &obd->u.lmv;
2887         struct lmv_tgt_desc *tgt;
2888         int rc;
2889
2890         ENTRY;
2891
2892         tgt = lmv_fid2tgt(lmv, fid);
2893         if (IS_ERR(tgt))
2894                 RETURN(PTR_ERR(tgt));
2895
2896         rc = md_fsync(tgt->ltd_exp, fid, request);
2897         RETURN(rc);
2898 }
2899
2900 struct stripe_dirent {
2901         struct page             *sd_page;
2902         struct lu_dirpage       *sd_dp;
2903         struct lu_dirent        *sd_ent;
2904         bool                     sd_eof;
2905 };
2906
2907 struct lmv_dir_ctxt {
2908         struct lmv_obd          *ldc_lmv;
2909         struct md_op_data       *ldc_op_data;
2910         struct md_readdir_info  *ldc_mrinfo;
2911         __u64                    ldc_hash;
2912         int                      ldc_count;
2913         struct stripe_dirent     ldc_stripes[0];
2914 };
2915
2916 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
2917 {
2918         if (stripe->sd_page) {
2919                 kunmap(stripe->sd_page);
2920                 put_page(stripe->sd_page);
2921                 stripe->sd_page = NULL;
2922                 stripe->sd_ent = NULL;
2923         }
2924 }
2925
2926 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2927 {
2928         int i;
2929
2930         for (i = 0; i < ctxt->ldc_count; i++)
2931                 stripe_dirent_unload(&ctxt->ldc_stripes[i]);
2932 }
2933
2934 /* if @ent is dummy, or . .., get next */
2935 static struct lu_dirent *stripe_dirent_get(struct lmv_dir_ctxt *ctxt,
2936                                            struct lu_dirent *ent,
2937                                            int stripe_index)
2938 {
2939         for (; ent; ent = lu_dirent_next(ent)) {
2940                 /* Skip dummy entry */
2941                 if (le16_to_cpu(ent->lde_namelen) == 0)
2942                         continue;
2943
2944                 /* skip . and .. for other stripes */
2945                 if (stripe_index &&
2946                     (strncmp(ent->lde_name, ".",
2947                              le16_to_cpu(ent->lde_namelen)) == 0 ||
2948                      strncmp(ent->lde_name, "..",
2949                              le16_to_cpu(ent->lde_namelen)) == 0))
2950                         continue;
2951
2952                 if (le64_to_cpu(ent->lde_hash) >= ctxt->ldc_hash)
2953                         break;
2954         }
2955
2956         return ent;
2957 }
2958
2959 static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
2960                                             struct stripe_dirent *stripe,
2961                                             int stripe_index)
2962 {
2963         struct md_op_data *op_data = ctxt->ldc_op_data;
2964         struct lmv_oinfo *oinfo;
2965         struct lu_fid fid = op_data->op_fid1;
2966         struct inode *inode = op_data->op_data;
2967         struct lmv_tgt_desc *tgt;
2968         struct lu_dirent *ent = stripe->sd_ent;
2969         __u64 hash = ctxt->ldc_hash;
2970         int rc = 0;
2971
2972         ENTRY;
2973
2974         LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2975         LASSERT(!ent);
2976
2977         do {
2978                 if (stripe->sd_page) {
2979                         __u64 end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2980
2981                         /* @hash should be the last dirent hash */
2982                         LASSERTF(hash <= end,
2983                                  "ctxt@%px stripe@%px hash %llx end %llx\n",
2984                                  ctxt, stripe, hash, end);
2985                         /* unload last page */
2986                         stripe_dirent_unload(stripe);
2987                         /* eof */
2988                         if (end == MDS_DIR_END_OFF) {
2989                                 stripe->sd_eof = true;
2990                                 break;
2991                         }
2992                         hash = end;
2993                 }
2994
2995                 oinfo = &op_data->op_lso1->lso_lsm.lsm_md_oinfo[stripe_index];
2996                 if (!oinfo->lmo_root) {
2997                         rc = -ENOENT;
2998                         break;
2999                 }
3000
3001                 tgt = lmv_tgt_retry(ctxt->ldc_lmv, oinfo->lmo_mds);
3002                 if (!tgt) {
3003                         rc = -ENODEV;
3004                         break;
3005                 }
3006
3007                 /* op_data is shared by stripes, reset after use */
3008                 op_data->op_fid1 = oinfo->lmo_fid;
3009                 op_data->op_fid2 = oinfo->lmo_fid;
3010                 op_data->op_data = oinfo->lmo_root;
3011
3012                 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_mrinfo, hash,
3013                                   &stripe->sd_page);
3014
3015                 op_data->op_fid1 = fid;
3016                 op_data->op_fid2 = fid;
3017                 op_data->op_data = inode;
3018
3019                 if (rc)
3020                         break;
3021
3022                 stripe->sd_dp = page_address(stripe->sd_page);
3023                 ent = stripe_dirent_get(ctxt, lu_dirent_start(stripe->sd_dp),
3024                                         stripe_index);
3025                 /* in case a page filled with ., .. and dummy, read next */
3026         } while (!ent);
3027
3028         stripe->sd_ent = ent;
3029         if (rc) {
3030                 LASSERT(!ent);
3031                 /* treat error as eof, so dir can be partially accessed */
3032                 stripe->sd_eof = true;
3033                 ctxt->ldc_mrinfo->mr_partial_readdir_rc = rc;
3034                 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, directory is partially accessed!\n",
3035                               PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
3036                               rc);
3037         }
3038
3039         RETURN(ent);
3040 }
3041
3042 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
3043 {
3044         struct obd_device *obd = exp->exp_obd;
3045         struct lmv_obd *lmv = &obd->u.lmv;
3046         struct lmv_tgt_desc *tgt;
3047         int rc;
3048
3049         ENTRY;
3050
3051         rc = lmv_check_connect(obd);
3052         if (rc != 0)
3053                 RETURN(rc);
3054
3055         tgt = lmv_fid2tgt(lmv, &data->op_fid1);
3056         if (IS_ERR(tgt))
3057                 RETURN(PTR_ERR(tgt));
3058
3059         data->op_flags |= MF_MDC_CANCEL_FID1;
3060         rc = md_file_resync(tgt->ltd_exp, data);
3061         RETURN(rc);
3062 }
3063
3064 /**
3065  * Get dirent with the closest hash for striped directory
3066  *
3067  * This function will search the dir entry, whose hash value is the
3068  * closest(>=) to hash from all of sub-stripes, and it is only being called
3069  * for striped directory.
3070  *
3071  * \param[in] ctxt              dir read context
3072  *
3073  * \retval                      dirent get the entry successfully
3074  *                              NULL does not get the entry, normally it means
3075  *                              it reaches the end of the directory, while read
3076  *                              stripe dirent error is ignored to allow partial
3077  *                              access.
3078  */
3079 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
3080 {
3081         struct stripe_dirent *stripe;
3082         struct lu_dirent *ent = NULL;
3083         int i;
3084         int min = -1;
3085
3086         /* TODO: optimize with k-way merge sort */
3087         for (i = 0; i < ctxt->ldc_count; i++) {
3088                 stripe = &ctxt->ldc_stripes[i];
3089                 if (stripe->sd_eof)
3090                         continue;
3091
3092                 if (!stripe->sd_ent) {
3093                         stripe_dirent_load(ctxt, stripe, i);
3094                         if (!stripe->sd_ent) {
3095                                 LASSERT(stripe->sd_eof);
3096                                 continue;
3097                         }
3098                 }
3099
3100                 if (min == -1 ||
3101                     le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
3102                     le64_to_cpu(stripe->sd_ent->lde_hash)) {
3103                         min = i;
3104                         if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
3105                             ctxt->ldc_hash)
3106                                 break;
3107                 }
3108         }
3109
3110         if (min != -1) {
3111                 stripe = &ctxt->ldc_stripes[min];
3112                 ent = stripe->sd_ent;
3113                 /* pop found dirent */
3114                 stripe->sd_ent = stripe_dirent_get(ctxt, lu_dirent_next(ent),
3115                                                    min);
3116         }
3117
3118         return ent;
3119 }
3120
3121 /**
3122  * Build dir entry page for striped directory
3123  *
3124  * This function gets one entry by @offset from a striped directory. It will
3125  * read entries from all of stripes, and choose one closest to the required
3126  * offset(&offset). A few notes
3127  * 1. skip . and .. for non-zero stripes, because there can only have one .
3128  * and .. in a directory.
3129  * 2. op_data will be shared by all of stripes, instead of allocating new
3130  * one, so need to restore before reusing.
3131  *
3132  * \param[in] exp       obd export refer to LMV
3133  * \param[in] op_data   hold those MD parameters of read_entry
3134  * \param[in] mrinfo    ldlm callback being used in enqueue in mdc_read_entry,
3135  *                      and partial readdir result will be stored in it.
3136  * \param[in] offset    starting hash offset
3137  * \param[out] ppage    the page holding the entry. Note: because the entry
3138  *                      will be accessed in upper layer, so we need hold the
3139  *                      page until the usages of entry is finished, see
3140  *                      ll_dir_entry_next.
3141  *
3142  * retval               =0 if get entry successfully
3143  *                      <0 cannot get entry
3144  */
3145 static int lmv_striped_read_page(struct obd_export *exp,
3146                                  struct md_op_data *op_data,
3147                                  struct md_readdir_info *mrinfo, __u64 offset,
3148                                  struct page **ppage)
3149 {
3150         struct page *page = NULL;
3151         struct lu_dirpage *dp;
3152         void *start;
3153         struct lu_dirent *ent;
3154         struct lu_dirent *last_ent;
3155         int stripe_count;
3156         struct lmv_dir_ctxt *ctxt;
3157         struct lu_dirent *next = NULL;
3158         __u16 ent_size;
3159         size_t left_bytes;
3160         int rc = 0;
3161
3162         ENTRY;
3163
3164         /* Allocate a page and read entries from all of stripes and fill
3165          * the page by hash order
3166          */
3167         page = alloc_page(GFP_KERNEL);
3168         if (!page)
3169                 RETURN(-ENOMEM);
3170
3171         /* Initialize the entry page */
3172         dp = kmap(page);
3173         memset(dp, 0, sizeof(*dp));
3174         dp->ldp_hash_start = cpu_to_le64(offset);
3175
3176         start = dp + 1;
3177         left_bytes = PAGE_SIZE - sizeof(*dp);
3178         ent = start;
3179         last_ent = ent;
3180
3181         /* initalize dir read context */
3182         stripe_count = op_data->op_lso1->lso_lsm.lsm_md_stripe_count;
3183         OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
3184         if (!ctxt)
3185                 GOTO(free_page, rc = -ENOMEM);
3186         ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
3187         ctxt->ldc_op_data = op_data;
3188         ctxt->ldc_mrinfo = mrinfo;
3189         ctxt->ldc_hash = offset;
3190         ctxt->ldc_count = stripe_count;
3191
3192         while (1) {
3193                 next = lmv_dirent_next(ctxt);
3194
3195                 /* end of directory */
3196                 if (!next) {
3197                         ctxt->ldc_hash = MDS_DIR_END_OFF;
3198                         break;
3199                 }
3200                 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
3201
3202                 ent_size = le16_to_cpu(next->lde_reclen);
3203
3204                 /* the last entry lde_reclen is 0, but it might not be the last
3205                  * one of this temporay dir page
3206                  */
3207                 if (!ent_size)
3208                         ent_size = lu_dirent_calc_size(
3209                                         le16_to_cpu(next->lde_namelen),
3210                                         le32_to_cpu(next->lde_attrs));
3211                 /* page full */
3212                 if (ent_size > left_bytes)
3213                         break;
3214
3215                 memcpy(ent, next, ent_size);
3216
3217                 /* Replace . with master FID and Replace .. with the parent FID
3218                  * of master object
3219                  */
3220                 if (strncmp(ent->lde_name, ".",
3221                             le16_to_cpu(ent->lde_namelen)) == 0 &&
3222                     le16_to_cpu(ent->lde_namelen) == 1)
3223                         fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
3224                 else if (strncmp(ent->lde_name, "..",
3225                                    le16_to_cpu(ent->lde_namelen)) == 0 &&
3226                            le16_to_cpu(ent->lde_namelen) == 2)
3227                         fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
3228
3229                 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
3230                        le16_to_cpu(ent->lde_namelen), ent->lde_name,
3231                        le64_to_cpu(ent->lde_hash));
3232
3233                 left_bytes -= ent_size;
3234                 ent->lde_reclen = cpu_to_le16(ent_size);
3235                 last_ent = ent;
3236                 ent = (void *)ent + ent_size;
3237         };
3238
3239         last_ent->lde_reclen = 0;
3240
3241         if (ent == start)
3242                 dp->ldp_flags |= LDF_EMPTY;
3243         else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
3244                 dp->ldp_flags |= LDF_COLLIDE;
3245         dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
3246         dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
3247
3248         put_lmv_dir_ctxt(ctxt);
3249         OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
3250
3251         *ppage = page;
3252
3253         RETURN(0);
3254
3255 free_page:
3256         kunmap(page);
3257         __free_page(page);
3258
3259         return rc;
3260 }
3261
3262 static int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
3263                          struct md_readdir_info *mrinfo, __u64 offset,
3264                          struct page **ppage)
3265 {
3266         struct obd_device *obd = exp->exp_obd;
3267         struct lmv_obd *lmv = &obd->u.lmv;
3268         struct lmv_tgt_desc *tgt;
3269         int rc;
3270
3271         ENTRY;
3272
3273         if (unlikely(lmv_dir_foreign(op_data->op_lso1)))
3274                 RETURN(-ENODATA);
3275
3276         if (unlikely(lmv_dir_striped(op_data->op_lso1))) {
3277                 rc = lmv_striped_read_page(exp, op_data, mrinfo, offset, ppage);
3278                 RETURN(rc);
3279         }
3280
3281         tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
3282         if (IS_ERR(tgt))
3283                 RETURN(PTR_ERR(tgt));
3284
3285         rc = md_read_page(tgt->ltd_exp, op_data, mrinfo, offset, ppage);
3286
3287         RETURN(rc);
3288 }
3289
3290 /**
3291  * Unlink a file/directory
3292  *
3293  * Unlink a file or directory under the parent dir. The unlink request
3294  * usually will be sent to the MDT where the child is located, but if
3295  * the client does not have the child FID then request will be sent to the
3296  * MDT where the parent is located.
3297  *
3298  * If the parent is a striped directory then it also needs to locate which
3299  * stripe the name of the child is located, and replace the parent FID
3300  * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
3301  * it will walk through all of sub-stripes until the child is being
3302  * unlinked finally.
3303  *
3304  * \param[in] exp       export refer to LMV
3305  * \param[in] op_data   different parameters transferred beween client
3306  *                      MD stacks, name, namelen, FIDs etc.
3307  *                      op_fid1 is the parent FID, op_fid2 is the child
3308  *                      FID.
3309  * \param[out] request  point to the request of unlink.
3310  *
3311  * retval               0 if succeed
3312  *                      negative errno if failed.
3313  */
3314 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
3315                       struct ptlrpc_request **request)
3316 {
3317         struct obd_device *obd = exp->exp_obd;
3318         struct lmv_obd *lmv = &obd->u.lmv;
3319         struct lmv_tgt_desc *tgt;
3320         struct lmv_tgt_desc *parent_tgt;
3321         struct mdt_body *body;
3322         int rc;
3323
3324         ENTRY;
3325
3326         op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3327         op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3328         op_data->op_cap = current_cap();
3329
3330 retry:
3331         parent_tgt = lmv_locate_tgt(lmv, op_data);
3332         if (IS_ERR(parent_tgt))
3333                 RETURN(PTR_ERR(parent_tgt));
3334
3335         if (likely(!fid_is_zero(&op_data->op_fid2))) {
3336                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
3337                 if (IS_ERR(tgt))
3338                         RETURN(PTR_ERR(tgt));
3339         } else {
3340                 tgt = parent_tgt;
3341         }
3342
3343         /*
3344          * If child's fid is given, cancel unused locks for it if it is from
3345          * another export than parent.
3346          *
3347          * LOOKUP lock for child (fid3) should also be cancelled on parent
3348          * tgt_tgt in mdc_unlink().
3349          */
3350         op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
3351
3352         if (parent_tgt != tgt)
3353                 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index,
3354                                       LCK_EX, MDS_INODELOCK_LOOKUP,
3355                                       MF_MDC_CANCEL_FID3);
3356
3357         rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
3358                               MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
3359         if (rc)
3360                 RETURN(rc);
3361
3362         CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
3363                PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
3364                tgt->ltd_index);
3365
3366         rc = md_unlink(tgt->ltd_exp, op_data, request);
3367         if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
3368                 ptlrpc_req_put(*request);
3369                 *request = NULL;
3370                 goto retry;
3371         }
3372
3373         if (rc != -EREMOTE)
3374                 RETURN(rc);
3375
3376         body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
3377         if (body == NULL)
3378                 RETURN(-EPROTO);
3379
3380         /* Not cross-ref case, just get out of here. */
3381         if (likely(!(body->mbo_valid & OBD_MD_MDS)))
3382                 RETURN(rc);
3383
3384         /* This is a remote object, try remote MDT. */
3385         op_data->op_fid2 = body->mbo_fid1;
3386         ptlrpc_req_put(*request);
3387         *request = NULL;
3388
3389         tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
3390         if (IS_ERR(tgt))
3391                 RETURN(PTR_ERR(tgt));
3392
3393         goto retry;
3394 }
3395
3396 static int lmv_precleanup(struct obd_device *obd)
3397 {
3398         ENTRY;
3399         libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
3400         RETURN(0);
3401 }
3402
3403 /**
3404  * Get by key a value associated with a LMV device.
3405  *
3406  * Dispatch request to lower-layer devices as needed.
3407  *
3408  * \param[in] env               execution environment for this thread
3409  * \param[in] exp               export for the LMV device
3410  * \param[in] keylen            length of key identifier
3411  * \param[in] key               identifier of key to get value for
3412  * \param[in] vallen            size of \a val
3413  * \param[out] val              pointer to storage location for value
3414  * \param[in] lsm               optional striping metadata of object
3415  *
3416  * \retval 0            on success
3417  * \retval negative     negated errno on failure
3418  */
3419 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
3420                         __u32 keylen, void *key, __u32 *vallen, void *val)
3421 {
3422         struct obd_device *obd;
3423         struct lmv_obd *lmv;
3424         struct lu_tgt_desc *tgt;
3425         int rc = 0;
3426
3427         ENTRY;
3428
3429         obd = class_exp2obd(exp);
3430         if (obd == NULL) {
3431                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
3432                        exp->exp_handle.h_cookie);
3433                 RETURN(-EINVAL);
3434         }
3435
3436         lmv = &obd->u.lmv;
3437         if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
3438                 LASSERT(*vallen == sizeof(__u32));
3439                 lmv_foreach_connected_tgt(lmv, tgt) {
3440                         if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
3441                                           vallen, val))
3442                                 RETURN(0);
3443                 }
3444                 RETURN(-EINVAL);
3445         } else if (KEY_IS(KEY_MAX_EASIZE) ||
3446                    KEY_IS(KEY_DEFAULT_EASIZE) ||
3447                    KEY_IS(KEY_CONN_DATA)) {
3448                 /*
3449                  * Forwarding this request to first MDS, it should know LOV
3450                  * desc.
3451                  */
3452                 tgt = lmv_tgt(lmv, 0);
3453                 if (!tgt)
3454                         RETURN(-ENODEV);
3455
3456                 rc = obd_get_info(env, tgt->ltd_exp, keylen, key, vallen, val);
3457                 if (!rc && KEY_IS(KEY_CONN_DATA))
3458                         exp->exp_connect_data = *(struct obd_connect_data *)val;
3459                 RETURN(rc);
3460         } else if (KEY_IS(KEY_TGT_COUNT)) {
3461                 *((int *)val) = lmv->lmv_mdt_descs.ltd_tgts_size;
3462                 RETURN(0);
3463         }
3464
3465         CDEBUG(D_IOCTL, "Invalid key\n");
3466         RETURN(-EINVAL);
3467 }
3468
3469 static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
3470                      int *__rcs, struct ptlrpc_request_set *_set)
3471 {
3472         struct obd_device *obd = class_exp2obd(exp);
3473         struct ptlrpc_request_set *set = _set;
3474         struct lmv_obd *lmv = &obd->u.lmv;
3475         int tgt_count = lmv->lmv_mdt_count;
3476         struct lu_tgt_desc *tgt;
3477         struct fid_array *fat, **fas = NULL;
3478         int i, rc, **rcs = NULL;
3479
3480         if (!set) {
3481                 set = ptlrpc_prep_set();
3482                 if (!set)
3483                         RETURN(-ENOMEM);
3484         }
3485
3486         /* split FIDs by targets */
3487         OBD_ALLOC_PTR_ARRAY(fas, tgt_count);
3488         if (fas == NULL)
3489                 GOTO(out, rc = -ENOMEM);
3490         OBD_ALLOC_PTR_ARRAY(rcs, tgt_count);
3491         if (rcs == NULL)
3492                 GOTO(out_fas, rc = -ENOMEM);
3493
3494         for (i = 0; i < fa->fa_nr; i++) {
3495                 unsigned int idx;
3496
3497                 rc = lmv_fld_lookup(lmv, &fa->fa_fids[i], &idx);
3498                 if (rc) {
3499                         CDEBUG(D_OTHER, "can't lookup "DFID": rc = %d\n",
3500                                PFID(&fa->fa_fids[i]), rc);
3501                         continue;
3502                 }
3503                 LASSERT(idx < tgt_count);
3504                 if (!fas[idx])
3505                         OBD_ALLOC(fas[idx], offsetof(struct fid_array,
3506                                   fa_fids[fa->fa_nr]));
3507                 if (!fas[idx])
3508                         GOTO(out, rc = -ENOMEM);
3509                 if (!rcs[idx])
3510                         OBD_ALLOC_PTR_ARRAY(rcs[idx], fa->fa_nr);
3511                 if (!rcs[idx])
3512                         GOTO(out, rc = -ENOMEM);
3513
3514                 fat = fas[idx];
3515                 fat->fa_fids[fat->fa_nr++] = fa->fa_fids[i];
3516         }
3517
3518         lmv_foreach_connected_tgt(lmv, tgt) {
3519                 fat = fas[tgt->ltd_index];
3520                 if (!fat || fat->fa_nr == 0)
3521                         continue;
3522                 rc = md_rmfid(tgt->ltd_exp, fat, rcs[tgt->ltd_index], set);
3523         }
3524
3525         rc = ptlrpc_set_wait(NULL, set);
3526         if (rc == 0) {
3527                 int j = 0;
3528
3529                 for (i = 0; i < tgt_count; i++) {
3530                         fat = fas[i];
3531                         if (!fat || fat->fa_nr == 0)
3532                                 continue;
3533                         /* copy FIDs back */
3534                         memcpy(fa->fa_fids + j, fat->fa_fids,
3535                                fat->fa_nr * sizeof(struct lu_fid));
3536                         /* copy rcs back */
3537                         memcpy(__rcs + j, rcs[i], fat->fa_nr * sizeof(**rcs));
3538                         j += fat->fa_nr;
3539                 }
3540         }
3541         if (set != _set)
3542                 ptlrpc_set_destroy(set);
3543
3544 out:
3545         for (i = 0; i < tgt_count; i++) {
3546                 if (fas && fas[i])
3547                         OBD_FREE(fas[i], offsetof(struct fid_array,
3548                                                 fa_fids[fa->fa_nr]));
3549                 if (rcs && rcs[i])
3550                         OBD_FREE_PTR_ARRAY(rcs[i], fa->fa_nr);
3551         }
3552         if (rcs)
3553                 OBD_FREE_PTR_ARRAY(rcs, tgt_count);
3554 out_fas:
3555         if (fas)
3556                 OBD_FREE_PTR_ARRAY(fas, tgt_count);
3557
3558         RETURN(rc);
3559 }
3560
3561 /**
3562  * Asynchronously set by key a value associated with a LMV device.
3563  *
3564  * Dispatch request to lower-layer devices as needed.
3565  *
3566  * \param[in] env       execution environment for this thread
3567  * \param[in] exp       export for the LMV device
3568  * \param[in] keylen    length of key identifier
3569  * \param[in] key       identifier of key to store value for
3570  * \param[in] vallen    size of value to store
3571  * \param[in] val       pointer to data to be stored
3572  * \param[in] set       optional list of related ptlrpc requests
3573  *
3574  * \retval 0            on success
3575  * \retval negative     negated errno on failure
3576  */
3577 static int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
3578                               __u32 keylen, void *key, __u32 vallen, void *val,
3579                               struct ptlrpc_request_set *set)
3580 {
3581         struct lmv_tgt_desc *tgt;
3582         struct obd_device *obd;
3583         struct lmv_obd *lmv;
3584         int rc = 0;
3585
3586         ENTRY;
3587
3588         obd = class_exp2obd(exp);
3589         if (obd == NULL) {
3590                 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
3591                        exp->exp_handle.h_cookie);
3592                 RETURN(-EINVAL);
3593         }
3594         lmv = &obd->u.lmv;
3595
3596         if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
3597             KEY_IS(KEY_DEFAULT_EASIZE)) {
3598                 int err = 0;
3599
3600                 lmv_foreach_connected_tgt(lmv, tgt) {
3601                         err = obd_set_info_async(env, tgt->ltd_exp,
3602                                                  keylen, key, vallen, val, set);
3603                         if (err && rc == 0)
3604                                 rc = err;
3605                 }
3606
3607                 RETURN(rc);
3608         }
3609
3610         RETURN(-EINVAL);
3611 }
3612
3613 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
3614                             const struct lmv_mds_md_v1 *lmm1)
3615 {
3616         struct lmv_obd  *lmv = &exp->exp_obd->u.lmv;
3617         int             stripe_count;
3618         int             cplen;
3619         int             i;
3620         int             rc = 0;
3621
3622         ENTRY;
3623
3624         lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
3625         lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3626         lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
3627         if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE))
3628                 lsm->lsm_md_hash_type = cfs_fail_val ?: LMV_HASH_TYPE_UNKNOWN;
3629         else
3630                 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
3631         lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
3632         lsm->lsm_md_migrate_offset = le32_to_cpu(lmm1->lmv_migrate_offset);
3633         lsm->lsm_md_migrate_hash = le32_to_cpu(lmm1->lmv_migrate_hash);
3634         cplen = strscpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
3635                         sizeof(lsm->lsm_md_pool_name));
3636
3637         if (cplen < 0)
3638                 RETURN(cplen);
3639
3640         CDEBUG(D_INFO, "unpack lsm count %d/%d, master %d hash_type %#x/%#x layout_version %d\n",
3641                lsm->lsm_md_stripe_count,
3642                lsm->lsm_md_migrate_offset, lsm->lsm_md_master_mdt_index,
3643                lsm->lsm_md_hash_type, lsm->lsm_md_migrate_hash,
3644                lsm->lsm_md_layout_version);
3645
3646         stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3647         for (i = 0; i < stripe_count; i++) {
3648                 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
3649                               &lmm1->lmv_stripe_fids[i]);
3650                 /*
3651                  * set default value -1, so lmv_locate_tgt() knows this stripe
3652                  * target is not initialized.
3653                  */
3654                 lsm->lsm_md_oinfo[i].lmo_mds = LMV_OFFSET_DEFAULT;
3655                 if (!fid_is_sane(&lsm->lsm_md_oinfo[i].lmo_fid))
3656                         continue;
3657
3658                 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
3659                                     &lsm->lsm_md_oinfo[i].lmo_mds);
3660                 if (rc == -ENOENT)
3661                         continue;
3662
3663                 if (rc)
3664                         RETURN(rc);
3665
3666                 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
3667                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
3668         }
3669
3670         RETURN(rc);
3671 }
3672
3673 static inline int lmv_unpack_user_md(struct obd_export *exp,
3674                                      struct lmv_stripe_md *lsm,
3675                                      const struct lmv_user_md *lmu)
3676 {
3677         lsm->lsm_md_magic = le32_to_cpu(lmu->lum_magic);
3678         lsm->lsm_md_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
3679         lsm->lsm_md_master_mdt_index = le32_to_cpu(lmu->lum_stripe_offset);
3680         lsm->lsm_md_hash_type = le32_to_cpu(lmu->lum_hash_type);
3681         lsm->lsm_md_max_inherit = lmu->lum_max_inherit;
3682         lsm->lsm_md_max_inherit_rr = lmu->lum_max_inherit_rr;
3683         lsm->lsm_md_pool_name[LOV_MAXPOOLNAME] = 0;
3684
3685         return 0;
3686 }
3687
3688 struct lmv_stripe_object *lmv_stripe_object_alloc(__u32 magic,
3689                                                   const union lmv_mds_md *lmm,
3690                                                   size_t lmm_size)
3691 {
3692         struct lmv_stripe_object *lsm_obj;
3693         int size;
3694
3695         if (magic == LMV_MAGIC_FOREIGN) {
3696                 struct lmv_foreign_md *lfm;
3697
3698                 size = offsetof(typeof(*lfm), lfm_value[0]);
3699                 if (lmm_size < size)
3700                         RETURN(ERR_PTR(-EPROTO));
3701
3702                 size += le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3703                 if (lmm_size < size)
3704                         RETURN(ERR_PTR(-EPROTO));
3705
3706                 OBD_ALLOC_LARGE(lsm_obj, lmm_size +
3707                                 offsetof(typeof(*lsm_obj), lso_lfm));
3708         } else {
3709                 if (magic == LMV_MAGIC_V1) {
3710                         int count;
3711
3712                         size = offsetof(struct lmv_mds_md_v1,
3713                                         lmv_stripe_fids[0]);
3714                         if (lmm_size < size)
3715                                 RETURN(ERR_PTR(-EPROTO));
3716
3717                         count = lmv_mds_md_stripe_count_get(lmm);
3718                         size += count * sizeof(struct lu_fid);
3719                         if (lmm_size < size)
3720                                 RETURN(ERR_PTR(-EPROTO));
3721
3722                         size = lmv_stripe_md_size(count);
3723                 } else {
3724                         if (lmm && lmm_size < sizeof(struct lmv_user_md))
3725                                 RETURN(ERR_PTR(-EPROTO));
3726
3727                         /**
3728                          * Unpack default dirstripe(lmv_user_md) to
3729                          * lmv_stripe_md, stripecount should be 0 then.
3730                          */
3731                         size = lmv_stripe_md_size(0);
3732                 }
3733                 size += offsetof(typeof(*lsm_obj), lso_lsm);
3734                 OBD_ALLOC(lsm_obj, size);
3735         }
3736
3737         if (lsm_obj) {
3738                 atomic_set(&lsm_obj->lso_refs, 1);
3739                 RETURN(lsm_obj);
3740         }
3741
3742         RETURN(ERR_PTR(-ENOMEM));
3743 }
3744 EXPORT_SYMBOL(lmv_stripe_object_alloc);
3745
3746 static int lmv_stripe_object_create(struct obd_export *exp,
3747                                     struct lmv_stripe_object **lsop,
3748                                     const union lmv_mds_md *lmm,
3749                                     size_t lmm_size)
3750 {
3751         struct lmv_stripe_object *lsm_obj;
3752         __u32 magic;
3753         int rc;
3754
3755         ENTRY;
3756
3757         LASSERT(lsop != NULL && *lsop == NULL);
3758
3759         if (lmm_size == 0)
3760                 RETURN(-EPROTO);
3761
3762         magic = le32_to_cpu(lmm->lmv_magic);
3763         if (magic == LMV_MAGIC_STRIPE)
3764                 RETURN(-EPERM);
3765
3766         if (magic != LMV_MAGIC_V1 && magic != LMV_USER_MAGIC &&
3767             magic != LMV_MAGIC_FOREIGN) {
3768                 CERROR("%s: invalid lmv magic %x: rc = %d\n",
3769                        exp->exp_obd->obd_name, magic, -EIO);
3770                 RETURN(-EIO);
3771         }
3772
3773         /* foreign lmv case */
3774         if (magic == LMV_MAGIC_FOREIGN) {
3775                 struct lmv_foreign_md *lfm;
3776
3777                 lsm_obj = lmv_stripe_object_alloc(magic, lmm, lmm_size);
3778                 if (IS_ERR(lsm_obj))
3779                         RETURN(PTR_ERR(lsm_obj));
3780
3781                 *lsop = lsm_obj;
3782                 lfm = &lsm_obj->lso_lfm;
3783                 lfm->lfm_magic = magic;
3784                 lfm->lfm_length = le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3785                 lfm->lfm_type = le32_to_cpu(lmm->lmv_foreign_md.lfm_type);
3786                 lfm->lfm_flags = le32_to_cpu(lmm->lmv_foreign_md.lfm_flags);
3787                 memcpy(&lfm->lfm_value, &lmm->lmv_foreign_md.lfm_value,
3788                        lfm->lfm_length);
3789                 RETURN(0);
3790         }
3791
3792         /* Unpack memmd */
3793         lsm_obj = lmv_stripe_object_alloc(magic, lmm, lmm_size);
3794         if (IS_ERR(lsm_obj))
3795                 RETURN(PTR_ERR(lsm_obj));
3796
3797         switch (magic) {
3798         case LMV_MAGIC_V1:
3799                 rc = lmv_unpack_md_v1(exp, &lsm_obj->lso_lsm, &lmm->lmv_md_v1);
3800                 break;
3801         case LMV_USER_MAGIC:
3802                 rc = lmv_unpack_user_md(exp, &lsm_obj->lso_lsm,
3803                                         &lmm->lmv_user_md);
3804                 break;
3805         default:
3806                 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3807                        magic);
3808                 rc = -EINVAL;
3809                 break;
3810         }
3811
3812         if (rc != 0)
3813                 lmv_stripe_object_put(&lsm_obj);
3814
3815         *lsop = lsm_obj;
3816         RETURN(rc);
3817 }
3818
3819 struct lmv_stripe_object *
3820 lmv_stripe_object_get(struct lmv_stripe_object *lsm_obj)
3821 {
3822         if (lsm_obj == NULL)
3823                 return NULL;
3824
3825         atomic_inc(&lsm_obj->lso_refs);
3826         CDEBUG(D_INODE, "get %p %u\n", lsm_obj,
3827                atomic_read(&lsm_obj->lso_refs));
3828         return lsm_obj;
3829 }
3830 EXPORT_SYMBOL(lmv_stripe_object_get);
3831
3832 void lmv_stripe_object_put(struct lmv_stripe_object **lsop)
3833 {
3834         struct lmv_stripe_object *lsm_obj;
3835         size_t size;
3836         int i;
3837
3838         LASSERT(lsop != NULL);
3839
3840         lsm_obj = *lsop;
3841         if (lsm_obj == NULL)
3842                 return;
3843
3844         *lsop = NULL;
3845         CDEBUG(D_INODE, "put %p %u\n", lsm_obj,
3846                atomic_read(&lsm_obj->lso_refs) - 1);
3847
3848         if (!atomic_dec_and_test(&lsm_obj->lso_refs))
3849                 return;
3850
3851         if (lmv_dir_foreign(lsm_obj)) {
3852                 size = lsm_obj->lso_lfm.lfm_length +
3853                         offsetof(typeof(lsm_obj->lso_lfm), lfm_value[0]) +
3854                         offsetof(typeof(*lsm_obj), lso_lsm);
3855                 OBD_FREE_LARGE(lsm_obj, size);
3856                 return;
3857         }
3858
3859         if (lmv_dir_striped(lsm_obj)) {
3860                 struct lmv_stripe_md *lsm = &lsm_obj->lso_lsm;
3861
3862                 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
3863                         iput(lsm->lsm_md_oinfo[i].lmo_root);
3864                 size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
3865         } else {
3866                 size = lmv_stripe_md_size(0);
3867         }
3868         OBD_FREE(lsm_obj, size + offsetof(typeof(*lsm_obj), lso_lsm));
3869 }
3870 EXPORT_SYMBOL(lmv_stripe_object_put);
3871
3872 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3873                              union ldlm_policy_data *policy,
3874                              enum ldlm_mode mode, enum ldlm_cancel_flags flags,
3875                              void *opaque)
3876 {
3877         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3878         struct lu_tgt_desc *tgt;
3879         int err;
3880         int rc = 0;
3881
3882         ENTRY;
3883
3884         LASSERT(fid != NULL);
3885
3886         lmv_foreach_connected_tgt(lmv, tgt) {
3887                 if (!tgt->ltd_active)
3888                         continue;
3889
3890                 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3891                                        opaque);
3892                 if (!rc)
3893                         rc = err;
3894         }
3895         RETURN(rc);
3896 }
3897
3898 static int lmv_set_lock_data(struct obd_export *exp,
3899                              const struct lustre_handle *lockh,
3900                              void *data, __u64 *bits)
3901 {
3902         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3903         struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3904         int rc;
3905
3906         ENTRY;
3907
3908         if (tgt == NULL || tgt->ltd_exp == NULL)
3909                 RETURN(-EINVAL);
3910         rc =  md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3911         RETURN(rc);
3912 }
3913
3914 static enum ldlm_mode
3915 lmv_lock_match(struct obd_export *exp, __u64 flags,
3916                const struct lu_fid *fid, enum ldlm_type type,
3917                union ldlm_policy_data *policy,
3918                enum ldlm_mode mode, struct lustre_handle *lockh)
3919 {
3920         struct obd_device *obd = exp->exp_obd;
3921         struct lmv_obd *lmv = &obd->u.lmv;
3922         struct lu_tgt_desc *tgt;
3923         __u64 bits = policy->l_inodebits.bits;
3924         enum ldlm_mode rc = LCK_MINMODE;
3925         int index;
3926         int i;
3927
3928         /* only one bit is set */
3929         LASSERT(bits && !(bits & (bits - 1)));
3930         /* With DNE every object can have two locks in different namespaces:
3931          * lookup lock in space of MDT storing direntry and update/open lock in
3932          * space of MDT storing inode.  Try the MDT that the FID maps to first,
3933          * since this can be easily found, and only try others if that fails.
3934          */
3935         if (bits == MDS_INODELOCK_LOOKUP) {
3936                 for (i = 0, index = lmv_fid2tgt_index(lmv, fid);
3937                      i < lmv->lmv_mdt_descs.ltd_tgts_size; i++,
3938                      index = (index + 1) % lmv->lmv_mdt_descs.ltd_tgts_size) {
3939                         if (index < 0) {
3940                                 CDEBUG(D_HA,
3941                                        "%s: "DFID" is inaccessible: rc = %d\n",
3942                                        obd->obd_name, PFID(fid), index);
3943                                 index = 0;
3944                         }
3945                         tgt = lmv_tgt(lmv, index);
3946                         if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
3947                                 continue;
3948                         rc = md_lock_match(tgt->ltd_exp, flags, fid, type,
3949                                            policy, mode, lockh);
3950                         if (rc)
3951                                 break;
3952                 }
3953         } else {
3954                 tgt = lmv_fid2tgt(lmv, fid);
3955                 if (!IS_ERR(tgt) && tgt->ltd_exp && tgt->ltd_active)
3956                         rc = md_lock_match(tgt->ltd_exp, flags, fid, type,
3957                                            policy, mode, lockh);
3958         }
3959
3960         CDEBUG(D_INODE, "Lock match for "DFID": %d\n", PFID(fid), rc);
3961
3962         return rc;
3963 }
3964
3965 static int
3966 lmv_get_lustre_md(struct obd_export *exp, struct req_capsule *pill,
3967                   struct obd_export *dt_exp, struct obd_export *md_exp,
3968                   struct lustre_md *md)
3969 {
3970         struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3971         struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3972
3973         if (!tgt || !tgt->ltd_exp)
3974                 return -EINVAL;
3975
3976         return md_get_lustre_md(tgt->ltd_exp, pill, dt_exp, md_exp, md);
3977 }
3978
3979 static int lmv_put_lustre_md(struct obd_export *exp, struct lustre_md *md)
3980 {
3981         struct obd_device *obd = exp->exp_obd;
3982         struct lmv_obd *lmv = &obd->u.lmv;
3983         struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3984
3985         ENTRY;
3986
3987         lmv_stripe_object_put(&md->def_lsm_obj);
3988         lmv_stripe_object_put(&md->lsm_obj);
3989
3990         if (!tgt || !tgt->ltd_exp)
3991                 RETURN(-EINVAL);
3992         RETURN(0);
3993 }
3994
3995 static int lmv_set_open_replay_data(struct obd_export *exp,
3996                                     struct obd_client_handle *och,
3997                                     struct lookup_intent *it)
3998 {
3999         struct obd_device *obd = exp->exp_obd;
4000         struct lmv_obd *lmv = &obd->u.lmv;
4001         struct lmv_tgt_desc *tgt;
4002
4003         ENTRY;
4004
4005         tgt = lmv_fid2tgt(lmv, &och->och_fid);
4006         if (IS_ERR(tgt))
4007                 RETURN(PTR_ERR(tgt));
4008
4009         RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
4010 }
4011
4012 static int lmv_clear_open_replay_data(struct obd_export *exp,
4013                                       struct obd_client_handle *och)
4014 {
4015         struct obd_device *obd = exp->exp_obd;
4016         struct lmv_obd *lmv = &obd->u.lmv;
4017         struct lmv_tgt_desc *tgt;
4018
4019         ENTRY;
4020
4021         tgt = lmv_fid2tgt(lmv, &och->och_fid);
4022         if (IS_ERR(tgt))
4023                 RETURN(PTR_ERR(tgt));
4024
4025         RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
4026 }
4027
4028 static int lmv_intent_getattr_async(struct obd_export *exp,
4029                                     struct md_op_item *item)
4030 {
4031         struct md_op_data *op_data = &item->mop_data;
4032         struct obd_device *obd = exp->exp_obd;
4033         struct lmv_obd *lmv = &obd->u.lmv;
4034         struct lmv_tgt_desc *ptgt;
4035         struct lmv_tgt_desc *ctgt;
4036         int rc;
4037
4038         ENTRY;
4039
4040         if (!(fid_is_sane(&op_data->op_fid2) ||
4041               fid_is_zero(&op_data->op_fid2)))
4042                 RETURN(-EINVAL);
4043
4044         ptgt = lmv_locate_tgt(lmv, op_data);
4045         if (IS_ERR(ptgt))
4046                 RETURN(PTR_ERR(ptgt));
4047
4048         /*
4049          * Zeroed FID @op_fid2 means that the intent getattr() comes from
4050          * statahead by regularized file names. Currently only do statahead
4051          * for the children files located same as the parent directory.
4052          */
4053         if (!fid_is_zero(&op_data->op_fid2)) {
4054                 ctgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
4055                 if (IS_ERR(ctgt))
4056                         RETURN(PTR_ERR(ctgt));
4057
4058                 /*
4059                  * remote object needs two RPCs to lookup and getattr,
4060                  * considering the complexity don't support statahead for now.
4061                  */
4062                 if (ctgt != ptgt)
4063                         RETURN(-EREMOTE);
4064         }
4065
4066         rc = md_intent_getattr_async(ptgt->ltd_exp, item);
4067
4068         RETURN(rc);
4069 }
4070
4071 static int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
4072                                struct lu_fid *fid, __u64 *bits)
4073 {
4074         struct obd_device *obd = exp->exp_obd;
4075         struct lmv_obd *lmv = &obd->u.lmv;
4076         struct lmv_tgt_desc *tgt;
4077         int rc;
4078
4079         ENTRY;
4080
4081         tgt = lmv_fid2tgt(lmv, fid);
4082         if (IS_ERR(tgt))
4083                 RETURN(PTR_ERR(tgt));
4084
4085         rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
4086         RETURN(rc);
4087 }
4088
4089 static int lmv_get_fid_from_lsm(struct obd_export *exp,
4090                                 const struct lmv_stripe_object *lso,
4091                                 const char *name, int namelen,
4092                                 struct lu_fid *fid)
4093 {
4094         const struct lmv_oinfo *oinfo;
4095
4096         LASSERT(lmv_dir_striped(lso));
4097         oinfo = lsm_name_to_stripe_info(lso, name, namelen, false);
4098         if (IS_ERR(oinfo))
4099                 return PTR_ERR(oinfo);
4100
4101         *fid = oinfo->lmo_fid;
4102
4103         RETURN(0);
4104 }
4105
4106 /**
4107  * For lmv, only need to send request to master MDT, and the master MDT will
4108  * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
4109  * we directly fetch data from the slave MDTs.
4110  */
4111 static int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
4112                         struct obd_quotactl *oqctl)
4113 {
4114         struct obd_device *obd = class_exp2obd(exp);
4115         struct lmv_obd *lmv = &obd->u.lmv;
4116         struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
4117         __u64 curspace, curinodes;
4118         int rc = 0;
4119
4120         ENTRY;
4121
4122         if (!tgt || !tgt->ltd_exp || !tgt->ltd_active) {
4123                 CERROR("master lmv inactive\n");
4124                 RETURN(-EIO);
4125         }
4126
4127         if (oqctl->qc_cmd == LUSTRE_Q_ITERQUOTA ||
4128             oqctl->qc_cmd == LUSTRE_Q_ITEROQUOTA) {
4129                 struct list_head *lst = (struct list_head *)oqctl->qc_iter_list;
4130                 int err;
4131
4132                 if (oqctl->qc_cmd == LUSTRE_Q_ITERQUOTA)
4133                         RETURN(obd_quota_iter(tgt->ltd_exp, oqctl, lst));
4134
4135                 lmv_foreach_connected_tgt(lmv, tgt) {
4136                         if (!tgt->ltd_active)
4137                                 continue;
4138
4139                         err = obd_quota_iter(tgt->ltd_exp, oqctl, lst);
4140                         if (err) {
4141                                 CERROR("%s: getquota failed mdt %d: rc = %d\n",
4142                                        obd->obd_name, tgt->ltd_index, err);
4143                                 if (!rc)
4144                                         rc = err;
4145                         }
4146                 }
4147
4148                 RETURN(rc);
4149         }
4150
4151         if (oqctl->qc_cmd != Q_GETOQUOTA) {
4152                 rc = obd_quotactl(tgt->ltd_exp, oqctl);
4153                 RETURN(rc);
4154         }
4155
4156         curspace = curinodes = 0;
4157         lmv_foreach_connected_tgt(lmv, tgt) {
4158                 int err;
4159
4160                 if (!tgt->ltd_active)
4161                         continue;
4162
4163                 err = obd_quotactl(tgt->ltd_exp, oqctl);
4164                 if (err) {
4165                         CERROR("getquota on mdt %d failed. %d\n",
4166                                tgt->ltd_index, err);
4167                         if (!rc)
4168                                 rc = err;
4169                 } else {
4170                         curspace += oqctl->qc_dqblk.dqb_curspace;
4171                         curinodes += oqctl->qc_dqblk.dqb_curinodes;
4172                 }
4173         }
4174         oqctl->qc_dqblk.dqb_curspace = curspace;
4175         oqctl->qc_dqblk.dqb_curinodes = curinodes;
4176
4177         RETURN(rc);
4178 }
4179
4180 static int lmv_merge_attr(struct obd_export *exp,
4181                           const struct lmv_stripe_object *lso,
4182                           struct cl_attr *attr,
4183                           ldlm_blocking_callback cb_blocking)
4184 {
4185         const struct lmv_stripe_md *lsm = &lso->lso_lsm;
4186         int rc;
4187         int i;
4188
4189         if (!lmv_dir_striped(lso))
4190                 return 0;
4191
4192         rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
4193         if (rc < 0)
4194                 return rc;
4195
4196         for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
4197                 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
4198
4199                 if (!inode)
4200                         continue;
4201
4202                 CDEBUG(D_INFO,
4203                        "" DFID " size %llu, blocks %llu nlink %u, atime %lld ctime %lld, mtime %lld.\n",
4204                        PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
4205                        i_size_read(inode), (unsigned long long)inode->i_blocks,
4206                        inode->i_nlink, (s64)inode_get_atime_sec(inode),
4207                        (s64)inode_get_ctime_sec(inode),
4208                        (s64)inode_get_mtime_sec(inode));
4209
4210                 /* for slave stripe, it needs to subtract nlink for . and .. */
4211                 if (i != 0)
4212                         attr->cat_nlink += inode->i_nlink - 2;
4213                 else
4214                         attr->cat_nlink = inode->i_nlink;
4215
4216                 attr->cat_size += i_size_read(inode);
4217                 attr->cat_blocks += inode->i_blocks;
4218
4219                 if (attr->cat_atime < inode_get_atime_sec(inode))
4220                         attr->cat_atime = inode_get_atime_sec(inode);
4221
4222                 if (attr->cat_ctime < inode_get_ctime_sec(inode))
4223                         attr->cat_ctime = inode_get_ctime_sec(inode);
4224
4225                 if (attr->cat_mtime < inode_get_mtime_sec(inode))
4226                         attr->cat_mtime = inode_get_mtime_sec(inode);
4227         }
4228         return 0;
4229 }
4230
4231 static struct lu_batch *lmv_batch_create(struct obd_export *exp,
4232                                          enum lu_batch_flags flags,
4233                                          __u32 max_count)
4234 {
4235         struct lu_batch *bh;
4236         struct lmv_batch *lbh;
4237
4238         ENTRY;
4239         OBD_ALLOC_PTR(lbh);
4240         if (!lbh)
4241                 RETURN(ERR_PTR(-ENOMEM));
4242
4243         bh = &lbh->lbh_super;
4244         bh->lbt_flags = flags;
4245         bh->lbt_max_count = max_count;
4246
4247         if (flags & BATCH_FL_RQSET) {
4248                 bh->lbt_rqset = ptlrpc_prep_set();
4249                 if (bh->lbt_rqset == NULL) {
4250                         OBD_FREE_PTR(lbh);
4251                         RETURN(ERR_PTR(-ENOMEM));
4252                 }
4253         }
4254
4255         INIT_LIST_HEAD(&lbh->lbh_sub_batch_list);
4256         RETURN(bh);
4257 }
4258
4259 static int lmv_batch_stop(struct obd_export *exp, struct lu_batch *bh)
4260 {
4261         struct lmv_batch *lbh;
4262         struct lmvsub_batch *sub;
4263         struct lmvsub_batch *tmp;
4264         int rc = 0;
4265
4266         ENTRY;
4267
4268         lbh = container_of(bh, struct lmv_batch, lbh_super);
4269         list_for_each_entry_safe(sub, tmp, &lbh->lbh_sub_batch_list,
4270                                  sbh_sub_item) {
4271                 list_del(&sub->sbh_sub_item);
4272                 rc = md_batch_stop(sub->sbh_tgt->ltd_exp, sub->sbh_sub);
4273                 if (rc < 0) {
4274                         CERROR("%s: stop batch processing failed: rc = %d\n",
4275                                exp->exp_obd->obd_name, rc);
4276                         if (bh->lbt_result == 0)
4277                                 bh->lbt_result = rc;
4278                 }
4279                 OBD_FREE_PTR(sub);
4280         }
4281
4282         if (bh->lbt_flags & BATCH_FL_RQSET) {
4283                 rc = ptlrpc_set_wait(NULL, bh->lbt_rqset);
4284                 ptlrpc_set_destroy(bh->lbt_rqset);
4285         }
4286
4287         OBD_FREE_PTR(lbh);
4288         RETURN(rc);
4289 }
4290
4291 static int lmv_batch_flush(struct obd_export *exp, struct lu_batch *bh,
4292                            bool wait)
4293 {
4294         struct lmv_batch *lbh;
4295         struct lmvsub_batch *sub;
4296         int rc = 0;
4297         int rc1;
4298
4299         ENTRY;
4300
4301         lbh = container_of(bh, struct lmv_batch, lbh_super);
4302         list_for_each_entry(sub, &lbh->lbh_sub_batch_list, sbh_sub_item) {
4303                 rc1 = md_batch_flush(sub->sbh_tgt->ltd_exp, sub->sbh_sub, wait);
4304                 if (rc1 < 0) {
4305                         CERROR("%s: stop batch processing failed: rc = %d\n",
4306                                exp->exp_obd->obd_name, rc);
4307                         if (bh->lbt_result == 0)
4308                                 bh->lbt_result = rc;
4309
4310                         if (rc == 0)
4311                                 rc = rc1;
4312                 }
4313         }
4314
4315         if (wait && bh->lbt_flags & BATCH_FL_RQSET) {
4316                 rc1 = ptlrpc_set_wait(NULL, bh->lbt_rqset);
4317                 if (rc == 0)
4318                         rc = rc1;
4319         }
4320
4321         RETURN(rc);
4322 }
4323
4324 static inline struct lmv_tgt_desc *
4325 lmv_batch_locate_tgt(struct lmv_obd *lmv, struct md_op_item *item)
4326 {
4327         struct md_op_data *op_data = &item->mop_data;
4328         struct lmv_tgt_desc *tgt;
4329
4330         switch (item->mop_opc) {
4331         case MD_OP_GETATTR: {
4332                 struct lmv_tgt_desc *ptgt;
4333
4334                 if (!(fid_is_sane(&op_data->op_fid2) ||
4335                       fid_is_zero(&op_data->op_fid2)))
4336                         RETURN(ERR_PTR(-EINVAL));
4337
4338                 ptgt = lmv_locate_tgt(lmv, op_data);
4339                 if (IS_ERR(ptgt))
4340                         RETURN(ptgt);
4341
4342                 /*
4343                  * Zeroed @op_fid2 means that it is a statahead populating call
4344                  * in the file name pattern which is using file name format to
4345                  * prefetch the attributes. Thus it has no idea about the FID of
4346                  * the children file. The children file is considered to be
4347                  * located on the same storage target with the parent directory
4348                  * or the stripped directory.
4349                  */
4350                 if (fid_is_zero(&op_data->op_fid2)) {
4351                         tgt = ptgt;
4352                         break;
4353                 }
4354
4355                 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
4356                 if (IS_ERR(tgt))
4357                         RETURN(tgt);
4358
4359                 /*
4360                  * Remote object needs two RPCs to lookup and getattr,
4361                  * considering the complexity don't support statahead for now.
4362                  */
4363                 if (tgt != ptgt)
4364                         RETURN(ERR_PTR(-EREMOTE));
4365
4366                 break;
4367         }
4368         default:
4369                 tgt = ERR_PTR(-ENOTSUPP);
4370         }
4371
4372         return tgt;
4373 }
4374
4375 static struct lu_batch *lmv_batch_lookup_sub(struct lmv_batch *lbh,
4376                                              struct lmv_tgt_desc *tgt)
4377 {
4378         struct lmvsub_batch *sub;
4379
4380         list_for_each_entry(sub, &lbh->lbh_sub_batch_list, sbh_sub_item) {
4381                 if (sub->sbh_tgt == tgt)
4382                         return sub->sbh_sub;
4383         }
4384
4385         return NULL;
4386 }
4387
4388 static struct lu_batch *lmv_batch_get_sub(struct lmv_batch *lbh,
4389                                           struct lmv_tgt_desc *tgt)
4390 {
4391         struct lmvsub_batch *sbh;
4392         struct lu_batch *child_bh;
4393         struct lu_batch *bh;
4394
4395         ENTRY;
4396
4397         child_bh = lmv_batch_lookup_sub(lbh, tgt);
4398         if (child_bh != NULL)
4399                 RETURN(child_bh);
4400
4401         OBD_ALLOC_PTR(sbh);
4402         if (sbh == NULL)
4403                 RETURN(ERR_PTR(-ENOMEM));
4404
4405         INIT_LIST_HEAD(&sbh->sbh_sub_item);
4406         sbh->sbh_tgt = tgt;
4407
4408         bh = &lbh->lbh_super;
4409         child_bh = md_batch_create(tgt->ltd_exp, bh->lbt_flags,
4410                                    bh->lbt_max_count);
4411         if (IS_ERR(child_bh)) {
4412                 OBD_FREE_PTR(sbh);
4413                 RETURN(child_bh);
4414         }
4415
4416         child_bh->lbt_rqset = bh->lbt_rqset;
4417         sbh->sbh_sub = child_bh;
4418         list_add(&sbh->sbh_sub_item, &lbh->lbh_sub_batch_list);
4419         RETURN(child_bh);
4420 }
4421
4422 static int lmv_batch_add(struct obd_export *exp, struct lu_batch *bh,
4423                          struct md_op_item *item)
4424 {
4425         struct obd_device *obd = exp->exp_obd;
4426         struct lmv_obd *lmv = &obd->u.lmv;
4427         struct lmv_tgt_desc *tgt;
4428         struct lmv_batch *lbh;
4429         struct lu_batch *child_bh;
4430         int rc;
4431
4432         ENTRY;
4433
4434         tgt = lmv_batch_locate_tgt(lmv, item);
4435         if (IS_ERR(tgt))
4436                 RETURN(PTR_ERR(tgt));
4437
4438         lbh = container_of(bh, struct lmv_batch, lbh_super);
4439         child_bh = lmv_batch_get_sub(lbh, tgt);
4440         if (IS_ERR(child_bh))
4441                 RETURN(PTR_ERR(child_bh));
4442
4443         rc = md_batch_add(tgt->ltd_exp, child_bh, item);
4444         RETURN(rc);
4445 }
4446
4447 static const struct obd_ops lmv_obd_ops = {
4448         .o_owner                = THIS_MODULE,
4449         .o_setup                = lmv_setup,
4450         .o_cleanup              = lmv_cleanup,
4451         .o_precleanup           = lmv_precleanup,
4452         .o_process_config       = lmv_process_config,
4453         .o_connect              = lmv_connect,
4454         .o_disconnect           = lmv_disconnect,
4455         .o_statfs               = lmv_statfs,
4456         .o_get_info             = lmv_get_info,
4457         .o_set_info_async       = lmv_set_info_async,
4458         .o_notify               = lmv_notify,
4459         .o_get_uuid             = lmv_get_uuid,
4460         .o_fid_alloc            = lmv_fid_alloc,
4461         .o_iocontrol            = lmv_iocontrol,
4462         .o_quotactl             = lmv_quotactl
4463 };
4464
4465 static const struct md_ops lmv_md_ops = {
4466         .m_get_root             = lmv_get_root,
4467         .m_null_inode           = lmv_null_inode,
4468         .m_close                = lmv_close,
4469         .m_create               = lmv_create,
4470         .m_enqueue              = lmv_enqueue,
4471         .m_getattr              = lmv_getattr,
4472         .m_getxattr             = lmv_getxattr,
4473         .m_getattr_name         = lmv_getattr_name,
4474         .m_intent_lock          = lmv_intent_lock,
4475         .m_link                 = lmv_link,
4476         .m_rename               = lmv_rename,
4477         .m_setattr              = lmv_setattr,
4478         .m_setxattr             = lmv_setxattr,
4479         .m_fsync                = lmv_fsync,
4480         .m_file_resync          = lmv_file_resync,
4481         .m_read_page            = lmv_read_page,
4482         .m_unlink               = lmv_unlink,
4483         .m_init_ea_size         = lmv_init_ea_size,
4484         .m_cancel_unused        = lmv_cancel_unused,
4485         .m_set_lock_data        = lmv_set_lock_data,
4486         .m_lock_match           = lmv_lock_match,
4487         .m_get_lustre_md        = lmv_get_lustre_md,
4488         .m_put_lustre_md        = lmv_put_lustre_md,
4489         .m_merge_attr           = lmv_merge_attr,
4490         .m_set_open_replay_data = lmv_set_open_replay_data,
4491         .m_clear_open_replay_data = lmv_clear_open_replay_data,
4492         .m_intent_getattr_async = lmv_intent_getattr_async,
4493         .m_revalidate_lock      = lmv_revalidate_lock,
4494         .m_get_fid_from_lsm     = lmv_get_fid_from_lsm,
4495         .m_stripe_object_create = lmv_stripe_object_create,
4496         .m_rmfid                = lmv_rmfid,
4497         .m_batch_create         = lmv_batch_create,
4498         .m_batch_add            = lmv_batch_add,
4499         .m_batch_stop           = lmv_batch_stop,
4500         .m_batch_flush          = lmv_batch_flush,
4501 };
4502
4503 static int __init lmv_init(void)
4504 {
4505         int rc;
4506
4507         rc  = libcfs_setup();
4508         if (rc)
4509                 return rc;
4510
4511         return class_register_type(&lmv_obd_ops, &lmv_md_ops, true,
4512                                    LUSTRE_LMV_NAME, NULL);
4513 }
4514
4515 static void __exit lmv_exit(void)
4516 {
4517         class_unregister_type(LUSTRE_LMV_NAME);
4518 }
4519
4520 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
4521 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
4522 MODULE_VERSION(LUSTRE_VERSION_STRING);
4523 MODULE_LICENSE("GPL");
4524
4525 module_init(lmv_init);
4526 module_exit(lmv_exit);