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