4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LMV
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>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
45 #include <linux/math64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_lmv.h>
54 #include <lprocfs_status.h>
55 #include <cl_object.h>
56 #include <lustre_fid.h>
57 #include <uapi/linux/lustre/lustre_ioctl.h>
58 #include <lustre_kernelcomm.h>
59 #include "lmv_internal.h"
61 static int lmv_check_connect(struct obd_device *obd);
63 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
66 if (tgt->ltd_active == activate)
69 tgt->ltd_active = activate;
70 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
72 tgt->ltd_exp->exp_obd->obd_inactive = !activate;
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 (!)
82 static int lmv_set_mdc_active(struct lmv_obd *lmv,
83 const struct obd_uuid *uuid,
86 struct lu_tgt_desc *tgt = NULL;
87 struct obd_device *obd;
92 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
93 lmv, uuid->uuid, activate);
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);
101 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
106 GOTO(out_lmv_lock, rc = -EINVAL);
108 obd = class_exp2obd(tgt->ltd_exp);
110 GOTO(out_lmv_lock, rc = -ENOTCONN);
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);
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);
123 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
124 activate ? "" : "in");
125 lmv_activate_target(lmv, tgt, activate);
129 spin_unlock(&lmv->lmv_lock);
133 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
135 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
136 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
138 return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
141 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
142 enum obd_notify_event ev)
144 struct obd_connect_data *conn_data;
145 struct lmv_obd *lmv = &obd->u.lmv;
146 struct obd_uuid *uuid;
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,
157 uuid = &watched->u.cli.cl_target_uuid;
158 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
160 * Set MDC as active before notifying the observer, so the
161 * observer can use the MDC normally.
163 rc = lmv_set_mdc_active(lmv, uuid,
164 ev == OBD_NOTIFY_ACTIVE);
166 CERROR("%sactivation of %s failed: %d\n",
167 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
171 } else if (ev == OBD_NOTIFY_OCD) {
172 conn_data = &watched->u.cli.cl_import->imp_connect_data;
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
178 obd->obd_self_export->exp_connect_data = *conn_data;
182 * Pass the notification up the chain.
184 if (obd->obd_observer)
185 rc = obd_notify(obd->obd_observer, watched, ev);
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,
195 struct lmv_obd *lmv = &obd->u.lmv;
196 struct lustre_handle conn = { 0 };
197 struct obd_export *exp;
201 rc = class_connect(&conn, obd, cluuid);
203 CERROR("class_connection() returned %d\n", rc);
207 exp = class_conn2export(&conn);
210 lmv->conn_data = *data;
211 lmv->lmv_cache = localdata;
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);
220 rc = lmv_check_connect(obd);
229 if (lmv->lmv_tgts_kobj)
230 kobject_put(lmv->lmv_tgts_kobj);
232 class_disconnect(exp);
237 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
240 struct obd_device *obd = exp->exp_obd;
241 struct lmv_obd *lmv = &obd->u.lmv;
242 struct lmv_tgt_desc *tgt;
248 if (lmv->max_easize < easize) {
249 lmv->max_easize = easize;
252 if (lmv->max_def_easize < def_easize) {
253 lmv->max_def_easize = def_easize;
260 if (lmv->connected == 0)
263 lmv_foreach_connected_tgt(lmv, tgt) {
264 if (!tgt->ltd_active)
267 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
269 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
270 " rc = %d\n", obd->obd_name, tgt->ltd_index, rc);
277 #define MAX_STRING_SIZE 128
279 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
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;
288 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
291 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
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);
299 if (!mdc_obd->obd_set_up) {
300 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
304 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
305 &lmv->conn_data, lmv->lmv_cache);
307 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
312 * Init fid sequence client for this mdc and add new fld target.
314 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
318 target.ft_srv = NULL;
319 target.ft_exp = mdc_exp;
320 target.ft_idx = tgt->ltd_index;
322 fld_client_add_target(&lmv->lmv_fld, &target);
324 rc = obd_register_observer(mdc_obd, obd);
326 obd_disconnect(mdc_exp);
327 CERROR("target %s register_observer error %d\n",
328 tgt->ltd_uuid.uuid, rc);
332 if (obd->obd_observer) {
334 * Tell the observer about the new target.
336 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
339 obd_disconnect(mdc_exp);
345 tgt->ltd_exp = mdc_exp;
346 lmv->desc.ld_active_tgt_count++;
348 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
350 rc = lqos_add_tgt(&lmv->lmv_qos, tgt);
352 obd_disconnect(mdc_exp);
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));
360 lmv_statfs_check_update(obd, tgt);
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,
370 static void lmv_del_target(struct lmv_obd *lmv, struct lu_tgt_desc *tgt)
373 lqos_del_tgt(&lmv->lmv_qos, tgt);
374 lu_tgt_descs_del(&lmv->lmv_mdt_descs, tgt);
378 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
379 __u32 index, int gen)
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;
390 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
391 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
394 CERROR("%s: Target %s not attached: rc = %d\n",
395 obd->obd_name, uuidp->uuid, -EINVAL);
403 mutex_init(&tgt->ltd_fid_mutex);
404 tgt->ltd_index = index;
405 tgt->ltd_uuid = *uuidp;
408 mutex_lock(<d->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;
414 mutex_unlock(<d->ltd_mutex);
420 /* lmv_check_connect() will connect this target. */
423 rc = lmv_connect_mdc(obd, tgt);
425 mutex_lock(<d->ltd_mutex);
426 lmv->desc.ld_tgt_count = orig_tgt_count;
427 memset(tgt, 0, sizeof(*tgt));
428 mutex_unlock(<d->ltd_mutex);
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);
442 static int lmv_check_connect(struct obd_device *obd)
444 struct lmv_obd *lmv = &obd->u.lmv;
445 struct lmv_tgt_desc *tgt;
454 mutex_lock(&lmv->lmv_mdt_descs.ltd_mutex);
456 GOTO(unlock, rc = 0);
458 if (lmv->desc.ld_tgt_count == 0) {
459 CERROR("%s: no targets configured: rc = -EINVAL\n",
461 GOTO(unlock, rc = -EINVAL);
464 if (!lmv_mdt0_inited(lmv)) {
465 CERROR("%s: no target configured for index 0: rc = -EINVAL.\n",
467 GOTO(unlock, rc = -EINVAL);
470 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
471 obd->obd_uuid.uuid, obd->obd_name);
473 lmv_foreach_tgt(lmv, tgt) {
474 rc = lmv_connect_mdc(obd, tgt);
480 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
481 lmv_init_ea_size(obd->obd_self_export, easize, 0);
484 mutex_unlock(&lmv->lmv_mdt_descs.ltd_mutex);
489 lmv_foreach_tgt(lmv, tgt) {
494 --lmv->desc.ld_active_tgt_count;
495 obd_disconnect(tgt->ltd_exp);
501 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
503 struct lmv_obd *lmv = &obd->u.lmv;
504 struct obd_device *mdc_obd;
508 LASSERT(tgt != NULL);
509 LASSERT(obd != NULL);
511 mdc_obd = class_exp2obd(tgt->ltd_exp);
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;
518 if (lmv->lmv_tgts_kobj)
519 sysfs_remove_link(lmv->lmv_tgts_kobj,
523 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
525 CERROR("Can't finanize fids factory\n");
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);
531 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
532 rc = obd_disconnect(tgt->ltd_exp);
534 if (tgt->ltd_active) {
535 CERROR("Target %s disconnect error %d\n",
536 tgt->ltd_uuid.uuid, rc);
540 lmv_activate_target(lmv, tgt, 0);
545 static int lmv_disconnect(struct obd_export *exp)
547 struct obd_device *obd = class_exp2obd(exp);
548 struct lmv_obd *lmv = &obd->u.lmv;
549 struct lmv_tgt_desc *tgt;
554 lmv_foreach_connected_tgt(lmv, tgt)
555 lmv_disconnect_mdc(obd, tgt);
557 if (lmv->lmv_tgts_kobj)
558 kobject_put(lmv->lmv_tgts_kobj);
561 class_export_put(exp);
562 rc = class_disconnect(exp);
568 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
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;
581 tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
583 RETURN(PTR_ERR(tgt));
585 root_fid = *gf->gf_u.gf_root_fid;
586 LASSERT(fid_is_sane(&root_fid));
589 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
590 if (rc != 0 && rc != -EREMOTE)
591 GOTO(out_fid2path, rc);
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;
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);
605 ptr = ori_gf->gf_u.gf_path;
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);
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,
621 GOTO(out_fid2path, rc);
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;
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);
639 tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
641 GOTO(out_fid2path, rc = -EINVAL);
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;
649 goto repeat_fid2path;
652 if (remote_gf != NULL)
653 OBD_FREE(remote_gf, remote_gf_size);
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)
661 struct lmv_tgt_desc *curr_tgt;
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))
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)
682 struct lmv_tgt_desc *curr_tgt;
684 /* build the hsm_user_request for the given target */
685 hur_out->hur_request = hur_in->hur_request;
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];
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);
704 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
705 int len, struct lustre_kernelcomm *lk,
708 struct lmv_obd *lmv = &obd->u.lmv;
709 struct lu_tgt_desc *tgt;
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);
720 /* Whatever the result, remove copytool from kuc groups.
721 * Unreached coordinators will get EPIPE on next requests
722 * and will unregister automatically.
724 rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
729 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
730 int len, struct lustre_kernelcomm *lk,
733 struct lmv_obd *lmv = &obd->u.lmv;
735 bool any_set = false;
736 struct kkuc_ct_data *kcd;
738 struct lu_tgt_desc *tgt;
745 filp = fget(lk->lk_wfd);
749 if (lk->lk_flags & LK_FLG_DATANR)
750 kcd_size = offsetof(struct kkuc_ct_data,
751 kcd_archives[lk->lk_data_count]);
753 kcd_size = sizeof(*kcd);
755 OBD_ALLOC(kcd, kcd_size);
757 GOTO(err_fput, rc = -ENOMEM);
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);
766 kcd->kcd_magic = KKUC_CT_DATA_BITMAP_MAGIC;
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);
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);
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,
789 lk->lk_flags |= LK_FLG_STOP;
791 /* unregister from previous MDS */
792 lmv_foreach_connected_tgt(lmv, tgt) {
793 if (tgt->ltd_index >= i)
796 obd_iocontrol(cmd, tgt->ltd_exp, len,
799 GOTO(err_kkuc_rem, rc);
801 /* else: transient error.
802 * kuc will register to the missing MDT
810 /* no registration done: return error */
811 GOTO(err_kkuc_rem, rc = -ENOTCONN);
816 libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
823 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
824 int len, void *karg, void __user *uarg)
826 struct obd_device *obddev = class_exp2obd(exp);
827 struct lmv_obd *lmv = &obddev->u.lmv;
828 struct lu_tgt_desc *tgt = NULL;
830 __u32 count = lmv->desc.ld_tgt_count;
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};
845 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
849 tgt = lmv_tgt(lmv, index);
850 if (!tgt || !tgt->ltd_active)
853 mdc_obd = class_exp2obd(tgt->ltd_exp);
858 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
859 min((int) data->ioc_plen2,
860 (int) sizeof(struct obd_uuid))))
863 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
864 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
868 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
869 min((int) data->ioc_plen1,
870 (int) sizeof(stat_buf))))
874 case OBD_IOC_QUOTACTL: {
875 struct if_quotactl *qctl = karg;
876 struct obd_quotactl *oqctl;
878 if (qctl->qc_valid == QC_MDTIDX) {
879 if (count <= qctl->qc_idx)
882 tgt = lmv_tgt(lmv, qctl->qc_idx);
883 if (!tgt || !tgt->ltd_exp)
885 } else if (qctl->qc_valid == QC_UUID) {
886 lmv_foreach_tgt(lmv, tgt) {
887 if (!obd_uuid_equals(&tgt->ltd_uuid,
900 if (tgt->ltd_index >= count)
903 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
904 OBD_ALLOC_PTR(oqctl);
908 QCTL_COPY(oqctl, qctl);
909 rc = obd_quotactl(tgt->ltd_exp, oqctl);
911 QCTL_COPY(qctl, oqctl);
912 qctl->qc_valid = QC_MDTIDX;
913 qctl->obd_uuid = tgt->ltd_uuid;
918 case LL_IOC_GET_CONNECT_FLAGS: {
919 tgt = lmv_tgt(lmv, 0);
921 if (tgt && tgt->ltd_exp)
922 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
925 case LL_IOC_FID2MDTIDX: {
926 struct lu_fid *fid = karg;
929 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
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;
938 case OBD_IOC_FID2PATH: {
939 rc = lmv_fid2path(exp, len, karg, uarg);
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;
947 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
949 RETURN(PTR_ERR(tgt));
951 if (tgt->ltd_exp == NULL)
954 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
957 case LL_IOC_HSM_PROGRESS: {
958 const struct hsm_progress_kernel *hpk = karg;
960 tgt = lmv_fid2tgt(lmv, &hpk->hpk_fid);
962 RETURN(PTR_ERR(tgt));
963 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
966 case LL_IOC_HSM_REQUEST: {
967 struct hsm_user_request *hur = karg;
968 unsigned int reqcount = hur->hur_request.hr_itemcount;
973 /* if the request is about a single fid
974 * or if there is a single MDS, no need to split
976 if (reqcount == 1 || count == 1) {
977 tgt = lmv_fid2tgt(lmv, &hur->hur_user_item[0].hui_fid);
979 RETURN(PTR_ERR(tgt));
980 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
982 /* split fid list to their respective MDS */
983 lmv_foreach_connected_tgt(lmv, tgt) {
986 struct hsm_user_request *req;
988 nr = lmv_hsm_req_count(lmv, hur, tgt);
991 if (nr == 0) /* nothing for this MDS */
994 /* build a request with fids for this MDS */
995 reqlen = offsetof(typeof(*hur),
997 + hur->hur_request.hr_data_len;
998 OBD_ALLOC_LARGE(req, reqlen);
1001 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1003 GOTO(hsm_req_err, rc1);
1004 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1007 if (rc1 != 0 && rc == 0)
1009 OBD_FREE_LARGE(req, reqlen);
1014 case LL_IOC_LOV_SWAP_LAYOUTS: {
1015 struct md_op_data *op_data = karg;
1016 struct lmv_tgt_desc *tgt1, *tgt2;
1018 tgt1 = lmv_fid2tgt(lmv, &op_data->op_fid1);
1020 RETURN(PTR_ERR(tgt1));
1022 tgt2 = lmv_fid2tgt(lmv, &op_data->op_fid2);
1024 RETURN(PTR_ERR(tgt2));
1026 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1029 /* only files on same MDT can have their layouts swapped */
1030 if (tgt1->ltd_index != tgt2->ltd_index)
1033 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
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);
1041 rc = lmv_hsm_ct_register(obddev, cmd, len, lk, uarg);
1045 lmv_foreach_connected_tgt(lmv, tgt) {
1046 struct obd_device *mdc_obd;
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);
1055 if (tgt->ltd_active) {
1056 CERROR("error: iocontrol MDC %s on MDT"
1057 " idx %d cmd %x: err = %d\n",
1059 tgt->ltd_index, cmd, err);
1073 * This is _inode_ placement policy function (not name).
1075 static u32 lmv_placement_policy(struct obd_device *obd,
1076 struct md_op_data *op_data)
1078 struct lmv_obd *lmv = &obd->u.lmv;
1079 struct lmv_user_md *lum;
1084 if (lmv->desc.ld_tgt_count == 1)
1087 lum = op_data->op_data;
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()).
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.
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 !=
1114 mdt = op_data->op_default_mea1->lsm_md_master_mdt_index;
1115 op_data->op_mds = mdt;
1117 mdt = op_data->op_mds;
1123 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1125 struct lmv_tgt_desc *tgt;
1130 tgt = lmv_tgt(lmv, mds);
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.
1138 mutex_lock(&tgt->ltd_fid_mutex);
1140 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1141 GOTO(out, rc = -ENODEV);
1144 * Asking underlying tgt layer to allocate new fid.
1146 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1148 LASSERT(fid_is_sane(fid));
1154 mutex_unlock(&tgt->ltd_fid_mutex);
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)
1161 struct obd_device *obd = class_exp2obd(exp);
1162 struct lmv_obd *lmv = &obd->u.lmv;
1168 LASSERT(op_data != NULL);
1169 LASSERT(fid != NULL);
1171 mds = lmv_placement_policy(obd, op_data);
1173 rc = __lmv_fid_alloc(lmv, fid, mds);
1175 CERROR("Can't alloc new fid, rc %d\n", rc);
1180 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1182 struct lmv_obd *lmv = &obd->u.lmv;
1183 struct lmv_desc *desc;
1184 struct lnet_process_id lnet_id;
1190 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1191 CERROR("LMV setup requires a descriptor\n");
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));
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;
1209 spin_lock_init(&lmv->lmv_lock);
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;
1221 lu_qos_rr_init(&lmv->lmv_qos.lq_rr);
1224 * initialize rr_index to lower 32bit of netid, so that client
1225 * can distribute subdirs evenly from the beginning.
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;
1234 rc = lmv_tunables_init(obd);
1236 CWARN("%s: error adding LMV sysfs/debugfs files: rc = %d\n",
1239 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1240 LUSTRE_CLI_FLD_HASH_DHT);
1242 CERROR("Can't init FLD, err %d\n", rc);
1244 rc = lu_tgt_descs_init(&lmv->lmv_mdt_descs);
1246 CWARN("%s: error initialize target table: rc = %d\n",
1252 static int lmv_cleanup(struct obd_device *obd)
1254 struct lmv_obd *lmv = &obd->u.lmv;
1255 struct lu_tgt_desc *tgt;
1256 struct lu_tgt_desc *tmp;
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);
1268 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1270 struct lustre_cfg *lcfg = buf;
1271 struct obd_uuid obd_uuid;
1277 switch (lcfg->lcfg_command) {
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);
1284 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
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);
1293 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1294 GOTO(out, rc = -EINVAL);
1300 static int lmv_select_statfs_mdt(struct lmv_obd *lmv, __u32 flags)
1304 if (flags & OBD_STATFS_FOR_MDT0)
1307 if (lmv->lmv_statfs_start || lmv->desc.ld_tgt_count == 1)
1308 return lmv->lmv_statfs_start;
1310 /* choose initial MDT for this client */
1312 struct lnet_process_id lnet_id;
1313 if (LNetGetId(i, &lnet_id) == -ENOENT)
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.
1320 lmv->lmv_statfs_start =
1321 (u32)lnet_id.nid % lmv->desc.ld_tgt_count;
1326 return lmv->lmv_statfs_start;
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)
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;
1342 OBD_ALLOC(temp, sizeof(*temp));
1346 /* distribute statfs among MDTs */
1347 idx = lmv_select_statfs_mdt(lmv, flags);
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)
1355 rc = obd_statfs(env, tgt->ltd_exp, temp, max_age, flags);
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);
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
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;
1388 OBD_FREE(temp, sizeof(*temp));
1392 static int lmv_statfs_update(void *cookie, int rc)
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;
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.
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;
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)
1418 struct obd_info oinfo = {
1421 .oi_cb_up = lmv_statfs_update,
1425 if (ktime_get_seconds() - tgt->ltd_statfs_age <
1426 obd->u.lmv.desc.ld_qos_maxage)
1429 rc = obd_statfs_async(tgt->ltd_exp, &oinfo, 0, NULL);
1434 static int lmv_get_root(struct obd_export *exp, const char *fileset,
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);
1447 rc = md_get_root(tgt->ltd_exp, fileset, fid);
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)
1455 struct obd_device *obd = exp->exp_obd;
1456 struct lmv_obd *lmv = &obd->u.lmv;
1457 struct lmv_tgt_desc *tgt;
1462 tgt = lmv_fid2tgt(lmv, fid);
1464 RETURN(PTR_ERR(tgt));
1466 rc = md_getxattr(tgt->ltd_exp, fid, obd_md_valid, name, buf_size, req);
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)
1477 struct obd_device *obd = exp->exp_obd;
1478 struct lmv_obd *lmv = &obd->u.lmv;
1479 struct lmv_tgt_desc *tgt;
1484 tgt = lmv_fid2tgt(lmv, fid);
1486 RETURN(PTR_ERR(tgt));
1488 rc = md_setxattr(tgt->ltd_exp, fid, obd_md_valid, name,
1489 value, value_size, xattr_flags, suppgid, req);
1494 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1495 struct ptlrpc_request **request)
1497 struct obd_device *obd = exp->exp_obd;
1498 struct lmv_obd *lmv = &obd->u.lmv;
1499 struct lmv_tgt_desc *tgt;
1504 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1506 RETURN(PTR_ERR(tgt));
1508 if (op_data->op_flags & MF_GET_MDT_IDX) {
1509 op_data->op_mds = tgt->ltd_index;
1513 rc = md_getattr(tgt->ltd_exp, op_data, request);
1518 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1520 struct obd_device *obd = exp->exp_obd;
1521 struct lmv_obd *lmv = &obd->u.lmv;
1522 struct lu_tgt_desc *tgt;
1526 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
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.
1533 lmv_foreach_connected_tgt(lmv, tgt)
1534 md_null_inode(tgt->ltd_exp, fid);
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)
1542 struct obd_device *obd = exp->exp_obd;
1543 struct lmv_obd *lmv = &obd->u.lmv;
1544 struct lmv_tgt_desc *tgt;
1549 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1551 RETURN(PTR_ERR(tgt));
1553 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1554 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1558 static struct lmv_tgt_desc *
1559 lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1560 const char *name, int namelen, struct lu_fid *fid,
1561 __u32 *mds, bool post_migrate)
1563 struct lmv_tgt_desc *tgt;
1564 const struct lmv_oinfo *oinfo;
1566 if (!lmv_dir_striped(lsm) || !namelen) {
1567 tgt = lmv_fid2tgt(lmv, fid);
1571 *mds = tgt->ltd_index;
1575 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1576 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1577 return ERR_PTR(-EBADF);
1578 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1580 oinfo = lsm_name_to_stripe_info(lsm, name, namelen,
1583 return ERR_CAST(oinfo);
1586 *fid = oinfo->lmo_fid;
1587 *mds = oinfo->lmo_mds;
1588 tgt = lmv_tgt(lmv, oinfo->lmo_mds);
1590 CDEBUG(D_INODE, "locate MDT %u parent "DFID"\n", *mds, PFID(fid));
1592 return tgt ? tgt : ERR_PTR(-ENODEV);
1596 * Locate MDT of op_data->op_fid1
1598 * For striped directory, it will locate the stripe by name hash, if hash_type
1599 * is unknown, it will return the stripe specified by 'op_data->op_stripe_index'
1600 * which is set outside, and if dir is migrating, 'op_data->op_post_migrate'
1601 * indicates whether old or new layout is used to locate.
1603 * For plain direcotry, normally it will locate MDT by FID, but if this
1604 * directory has default LMV, and its hash type is "space", locate MDT with QoS.
1606 * \param[in] lmv LMV device
1607 * \param[in] op_data client MD stack parameters, name, namelen
1610 * retval pointer to the lmv_tgt_desc if succeed.
1611 * ERR_PTR(errno) if failed.
1613 struct lmv_tgt_desc *
1614 lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
1616 struct lmv_stripe_md *lsm = op_data->op_mea1;
1617 struct lmv_oinfo *oinfo;
1618 struct lmv_tgt_desc *tgt;
1620 if (lmv_dir_foreign(lsm))
1621 return ERR_PTR(-ENODATA);
1623 /* During creating VOLATILE file, it should honor the mdt
1624 * index if the file under striped dir is being restored, see
1626 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1627 (int)op_data->op_mds != -1) {
1628 tgt = lmv_tgt(lmv, op_data->op_mds);
1630 return ERR_PTR(-ENODEV);
1632 if (lmv_dir_striped(lsm)) {
1635 /* refill the right parent fid */
1636 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1637 oinfo = &lsm->lsm_md_oinfo[i];
1638 if (oinfo->lmo_mds == op_data->op_mds) {
1639 op_data->op_fid1 = oinfo->lmo_fid;
1644 if (i == lsm->lsm_md_stripe_count)
1645 op_data->op_fid1 = lsm->lsm_md_oinfo[0].lmo_fid;
1647 } else if (lmv_dir_bad_hash(lsm)) {
1648 LASSERT(op_data->op_stripe_index < lsm->lsm_md_stripe_count);
1649 oinfo = &lsm->lsm_md_oinfo[op_data->op_stripe_index];
1651 op_data->op_fid1 = oinfo->lmo_fid;
1652 op_data->op_mds = oinfo->lmo_mds;
1653 tgt = lmv_tgt(lmv, oinfo->lmo_mds);
1655 tgt = ERR_PTR(-ENODEV);
1656 } else if (op_data->op_code == LUSTRE_OPC_MKDIR &&
1657 lmv_dir_qos_mkdir(op_data->op_default_mea1) &&
1658 !lmv_dir_striped(lsm)) {
1659 tgt = lmv_locate_tgt_qos(lmv, &op_data->op_mds);
1660 if (tgt == ERR_PTR(-EAGAIN))
1661 tgt = lmv_locate_tgt_rr(lmv, &op_data->op_mds);
1663 * only update statfs when mkdir under dir with "space" hash,
1664 * this means the cached statfs may be stale, and current mkdir
1665 * may not follow QoS accurately, but it's not serious, and it
1666 * avoids periodic statfs when client doesn't mkdir under
1667 * "space" hashed directories.
1669 * TODO: after MDT support QoS object allocation, also update
1670 * statfs for 'lfs mkdir -i -1 ...", currently it's done in user
1674 struct obd_device *obd;
1676 obd = container_of(lmv, struct obd_device, u.lmv);
1677 lmv_statfs_check_update(obd, tgt);
1680 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_mea1,
1681 op_data->op_name, op_data->op_namelen,
1682 &op_data->op_fid1, &op_data->op_mds,
1683 op_data->op_post_migrate);
1689 /* Locate MDT of op_data->op_fid2 for link/rename */
1690 static struct lmv_tgt_desc *
1691 lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
1693 struct lmv_tgt_desc *tgt;
1696 LASSERT(op_data->op_name);
1697 if (lmv_dir_migrating(op_data->op_mea2)) {
1698 struct lu_fid fid1 = op_data->op_fid1;
1699 struct lmv_stripe_md *lsm1 = op_data->op_mea1;
1700 struct ptlrpc_request *request = NULL;
1703 * avoid creating new file under old layout of migrating
1704 * directory, check it here.
1706 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_mea2,
1707 op_data->op_name, op_data->op_namelen,
1708 &op_data->op_fid2, &op_data->op_mds, false);
1712 op_data->op_fid1 = op_data->op_fid2;
1713 op_data->op_mea1 = op_data->op_mea2;
1714 rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
1715 op_data->op_fid1 = fid1;
1716 op_data->op_mea1 = lsm1;
1718 ptlrpc_req_finished(request);
1719 RETURN(ERR_PTR(-EEXIST));
1723 RETURN(ERR_PTR(rc));
1726 return lmv_locate_tgt_by_name(lmv, op_data->op_mea2, op_data->op_name,
1727 op_data->op_namelen, &op_data->op_fid2,
1728 &op_data->op_mds, true);
1731 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1732 const void *data, size_t datalen, umode_t mode, uid_t uid,
1733 gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
1734 struct ptlrpc_request **request)
1736 struct obd_device *obd = exp->exp_obd;
1737 struct lmv_obd *lmv = &obd->u.lmv;
1738 struct lmv_tgt_desc *tgt;
1743 if (!lmv->desc.ld_active_tgt_count)
1746 if (lmv_dir_bad_hash(op_data->op_mea1))
1749 if (lmv_dir_migrating(op_data->op_mea1)) {
1751 * if parent is migrating, create() needs to lookup existing
1752 * name, to avoid creating new file under old layout of
1753 * migrating directory, check old layout here.
1755 tgt = lmv_locate_tgt(lmv, op_data);
1757 RETURN(PTR_ERR(tgt));
1759 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1761 ptlrpc_req_finished(*request);
1769 op_data->op_post_migrate = true;
1772 tgt = lmv_locate_tgt(lmv, op_data);
1774 RETURN(PTR_ERR(tgt));
1776 CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1777 (int)op_data->op_namelen, op_data->op_name,
1778 PFID(&op_data->op_fid1), op_data->op_mds);
1780 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1784 if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1785 /* Send the create request to the MDT where the object
1786 * will be located */
1787 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
1789 RETURN(PTR_ERR(tgt));
1791 op_data->op_mds = tgt->ltd_index;
1794 CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1795 PFID(&op_data->op_fid2), op_data->op_mds);
1797 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1798 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1799 cap_effective, rdev, request);
1801 if (*request == NULL)
1803 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1809 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1810 const union ldlm_policy_data *policy, struct md_op_data *op_data,
1811 struct lustre_handle *lockh, __u64 extra_lock_flags)
1813 struct obd_device *obd = exp->exp_obd;
1814 struct lmv_obd *lmv = &obd->u.lmv;
1815 struct lmv_tgt_desc *tgt;
1820 CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
1822 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1824 RETURN(PTR_ERR(tgt));
1826 CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
1827 PFID(&op_data->op_fid1), tgt->ltd_index);
1829 rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
1836 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1837 struct ptlrpc_request **preq)
1839 struct obd_device *obd = exp->exp_obd;
1840 struct lmv_obd *lmv = &obd->u.lmv;
1841 struct lmv_tgt_desc *tgt;
1842 struct mdt_body *body;
1848 tgt = lmv_locate_tgt(lmv, op_data);
1850 RETURN(PTR_ERR(tgt));
1852 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1853 (int)op_data->op_namelen, op_data->op_name,
1854 PFID(&op_data->op_fid1), tgt->ltd_index);
1856 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1857 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
1858 ptlrpc_req_finished(*preq);
1866 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1867 LASSERT(body != NULL);
1869 if (body->mbo_valid & OBD_MD_MDS) {
1870 op_data->op_fid1 = body->mbo_fid1;
1871 op_data->op_valid |= OBD_MD_FLCROSSREF;
1872 op_data->op_namelen = 0;
1873 op_data->op_name = NULL;
1875 ptlrpc_req_finished(*preq);
1884 #define md_op_data_fid(op_data, fl) \
1885 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1886 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1887 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1888 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1891 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1892 struct md_op_data *op_data, __u32 op_tgt,
1893 enum ldlm_mode mode, int bits, int flag)
1895 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1896 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
1897 union ldlm_policy_data policy = { { 0 } };
1901 if (!fid_is_sane(fid))
1905 tgt = lmv_fid2tgt(lmv, fid);
1907 RETURN(PTR_ERR(tgt));
1910 if (tgt->ltd_index != op_tgt) {
1911 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1912 policy.l_inodebits.bits = bits;
1913 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1914 mode, LCF_ASYNC, NULL);
1917 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1919 op_data->op_flags |= flag;
1927 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1930 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1931 struct ptlrpc_request **request)
1933 struct obd_device *obd = exp->exp_obd;
1934 struct lmv_obd *lmv = &obd->u.lmv;
1935 struct lmv_tgt_desc *tgt;
1939 LASSERT(op_data->op_namelen != 0);
1941 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1942 PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1943 op_data->op_name, PFID(&op_data->op_fid1));
1945 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1946 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1947 op_data->op_cap = cfs_curproc_cap_pack();
1949 tgt = lmv_locate_tgt2(lmv, op_data);
1951 RETURN(PTR_ERR(tgt));
1954 * Cancel UPDATE lock on child (fid1).
1956 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1957 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
1958 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1962 rc = md_link(tgt->ltd_exp, op_data, request);
1967 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
1968 const char *name, size_t namelen,
1969 struct ptlrpc_request **request)
1971 struct obd_device *obd = exp->exp_obd;
1972 struct lmv_obd *lmv = &obd->u.lmv;
1973 struct lmv_stripe_md *lsm = op_data->op_mea1;
1974 struct lmv_tgt_desc *parent_tgt;
1975 struct lmv_tgt_desc *sp_tgt;
1976 struct lmv_tgt_desc *tp_tgt = NULL;
1977 struct lmv_tgt_desc *child_tgt;
1978 struct lmv_tgt_desc *tgt;
1979 struct lu_fid target_fid;
1984 LASSERT(op_data->op_cli_flags & CLI_MIGRATE);
1986 CDEBUG(D_INODE, "MIGRATE "DFID"/%.*s\n",
1987 PFID(&op_data->op_fid1), (int)namelen, name);
1989 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1990 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1991 op_data->op_cap = cfs_curproc_cap_pack();
1993 parent_tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1994 if (IS_ERR(parent_tgt))
1995 RETURN(PTR_ERR(parent_tgt));
1997 if (lmv_dir_striped(lsm)) {
1998 __u32 hash_type = lsm->lsm_md_hash_type;
1999 __u32 stripe_count = lsm->lsm_md_stripe_count;
2002 * old stripes are appended after new stripes for migrating
2005 if (lmv_dir_migrating(lsm)) {
2006 hash_type = lsm->lsm_md_migrate_hash;
2007 stripe_count -= lsm->lsm_md_migrate_offset;
2010 rc = lmv_name_to_stripe_index(hash_type, stripe_count, name,
2015 if (lmv_dir_migrating(lsm))
2016 rc += lsm->lsm_md_migrate_offset;
2018 /* save it in fid4 temporarily for early cancel */
2019 op_data->op_fid4 = lsm->lsm_md_oinfo[rc].lmo_fid;
2020 sp_tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[rc].lmo_mds);
2025 * if parent is being migrated too, fill op_fid2 with target
2026 * stripe fid, otherwise the target stripe is not created yet.
2028 if (lmv_dir_migrating(lsm)) {
2029 hash_type = lsm->lsm_md_hash_type &
2030 ~LMV_HASH_FLAG_MIGRATION;
2031 stripe_count = lsm->lsm_md_migrate_offset;
2033 rc = lmv_name_to_stripe_index(hash_type, stripe_count,
2038 op_data->op_fid2 = lsm->lsm_md_oinfo[rc].lmo_fid;
2039 tp_tgt = lmv_tgt(lmv, lsm->lsm_md_oinfo[rc].lmo_mds);
2044 sp_tgt = parent_tgt;
2047 child_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2048 if (IS_ERR(child_tgt))
2049 RETURN(PTR_ERR(child_tgt));
2051 if (!S_ISDIR(op_data->op_mode) && tp_tgt)
2052 rc = __lmv_fid_alloc(lmv, &target_fid, tp_tgt->ltd_index);
2054 rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
2059 * for directory, send migrate request to the MDT where the object will
2060 * be migrated to, because we can't create a striped directory remotely.
2062 * otherwise, send to the MDT where source is located because regular
2063 * file may open lease.
2065 * NB. if MDT doesn't support DIR_MIGRATE, send to source MDT too for
2066 * backward compatibility.
2068 if (S_ISDIR(op_data->op_mode) &&
2069 (exp_connect_flags2(exp) & OBD_CONNECT2_DIR_MIGRATE)) {
2070 tgt = lmv_fid2tgt(lmv, &target_fid);
2072 RETURN(PTR_ERR(tgt));
2077 /* cancel UPDATE lock of parent master object */
2078 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index, LCK_EX,
2079 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2083 /* cancel UPDATE lock of source parent */
2084 if (sp_tgt != parent_tgt) {
2086 * migrate RPC packs master object FID, because we can only pack
2087 * two FIDs in reint RPC, but MDS needs to know both source
2088 * parent and target parent, and it will obtain them from master
2089 * FID and LMV, the other FID in RPC is kept for target.
2091 * since this FID is not passed to MDC, cancel it anyway.
2093 rc = lmv_early_cancel(exp, sp_tgt, op_data, -1, LCK_EX,
2094 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID4);
2098 op_data->op_flags &= ~MF_MDC_CANCEL_FID4;
2100 op_data->op_fid4 = target_fid;
2102 /* cancel UPDATE locks of target parent */
2103 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2104 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2108 /* cancel LOOKUP lock of source if source is remote object */
2109 if (child_tgt != sp_tgt) {
2110 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index,
2111 LCK_EX, MDS_INODELOCK_LOOKUP,
2112 MF_MDC_CANCEL_FID3);
2117 /* cancel ELC locks of source */
2118 rc = lmv_early_cancel(exp, child_tgt, op_data, tgt->ltd_index, LCK_EX,
2119 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2123 rc = md_rename(tgt->ltd_exp, op_data, name, namelen, NULL, 0, request);
2128 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2129 const char *old, size_t oldlen,
2130 const char *new, size_t newlen,
2131 struct ptlrpc_request **request)
2133 struct obd_device *obd = exp->exp_obd;
2134 struct lmv_obd *lmv = &obd->u.lmv;
2135 struct lmv_tgt_desc *sp_tgt;
2136 struct lmv_tgt_desc *tp_tgt = NULL;
2137 struct lmv_tgt_desc *src_tgt = NULL;
2138 struct lmv_tgt_desc *tgt;
2139 struct mdt_body *body;
2144 LASSERT(oldlen != 0);
2146 if (op_data->op_cli_flags & CLI_MIGRATE) {
2147 rc = lmv_migrate(exp, op_data, old, oldlen, request);
2151 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2152 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2153 op_data->op_cap = cfs_curproc_cap_pack();
2155 op_data->op_name = new;
2156 op_data->op_namelen = newlen;
2158 tp_tgt = lmv_locate_tgt2(lmv, op_data);
2160 RETURN(PTR_ERR(tp_tgt));
2162 /* Since the target child might be destroyed, and it might become
2163 * orphan, and we can only check orphan on the local MDT right now, so
2164 * we send rename request to the MDT where target child is located. If
2165 * target child does not exist, then it will send the request to the
2167 if (fid_is_sane(&op_data->op_fid4)) {
2168 tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2170 RETURN(PTR_ERR(tgt));
2175 op_data->op_flags |= MF_MDC_CANCEL_FID4;
2177 /* cancel UPDATE locks of target parent */
2178 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2179 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2183 if (fid_is_sane(&op_data->op_fid4)) {
2184 /* cancel LOOKUP lock of target on target parent */
2185 if (tgt != tp_tgt) {
2186 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2187 tgt->ltd_index, LCK_EX,
2188 MDS_INODELOCK_LOOKUP,
2189 MF_MDC_CANCEL_FID4);
2195 if (fid_is_sane(&op_data->op_fid3)) {
2196 src_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2197 if (IS_ERR(src_tgt))
2198 RETURN(PTR_ERR(src_tgt));
2200 /* cancel ELC locks of source */
2201 rc = lmv_early_cancel(exp, src_tgt, op_data, tgt->ltd_index,
2202 LCK_EX, MDS_INODELOCK_ELC,
2203 MF_MDC_CANCEL_FID3);
2208 op_data->op_name = old;
2209 op_data->op_namelen = oldlen;
2211 sp_tgt = lmv_locate_tgt(lmv, op_data);
2213 RETURN(PTR_ERR(sp_tgt));
2215 /* cancel UPDATE locks of source parent */
2216 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index, LCK_EX,
2217 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2221 if (fid_is_sane(&op_data->op_fid3)) {
2222 /* cancel LOOKUP lock of source on source parent */
2223 if (src_tgt != sp_tgt) {
2224 rc = lmv_early_cancel(exp, sp_tgt, op_data,
2225 tgt->ltd_index, LCK_EX,
2226 MDS_INODELOCK_LOOKUP,
2227 MF_MDC_CANCEL_FID3);
2234 CDEBUG(D_INODE, "RENAME "DFID"/%.*s to "DFID"/%.*s\n",
2235 PFID(&op_data->op_fid1), (int)oldlen, old,
2236 PFID(&op_data->op_fid2), (int)newlen, new);
2238 rc = md_rename(tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2240 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2241 ptlrpc_req_finished(*request);
2246 if (rc && rc != -EXDEV)
2249 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2253 /* Not cross-ref case, just get out of here. */
2254 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2257 op_data->op_fid4 = body->mbo_fid1;
2259 ptlrpc_req_finished(*request);
2262 tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2264 RETURN(PTR_ERR(tgt));
2266 if (fid_is_sane(&op_data->op_fid4)) {
2267 /* cancel LOOKUP lock of target on target parent */
2268 if (tgt != tp_tgt) {
2269 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2270 tgt->ltd_index, LCK_EX,
2271 MDS_INODELOCK_LOOKUP,
2272 MF_MDC_CANCEL_FID4);
2281 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2282 void *ea, size_t ealen, struct ptlrpc_request **request)
2284 struct obd_device *obd = exp->exp_obd;
2285 struct lmv_obd *lmv = &obd->u.lmv;
2286 struct lmv_tgt_desc *tgt;
2291 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x/0x%x\n",
2292 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2293 op_data->op_xvalid);
2295 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2296 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2298 RETURN(PTR_ERR(tgt));
2300 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2305 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2306 struct ptlrpc_request **request)
2308 struct obd_device *obd = exp->exp_obd;
2309 struct lmv_obd *lmv = &obd->u.lmv;
2310 struct lmv_tgt_desc *tgt;
2315 tgt = lmv_fid2tgt(lmv, fid);
2317 RETURN(PTR_ERR(tgt));
2319 rc = md_fsync(tgt->ltd_exp, fid, request);
2323 struct stripe_dirent {
2324 struct page *sd_page;
2325 struct lu_dirpage *sd_dp;
2326 struct lu_dirent *sd_ent;
2330 struct lmv_dir_ctxt {
2331 struct lmv_obd *ldc_lmv;
2332 struct md_op_data *ldc_op_data;
2333 struct md_callback *ldc_cb_op;
2336 struct stripe_dirent ldc_stripes[0];
2339 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
2341 if (stripe->sd_page) {
2342 kunmap(stripe->sd_page);
2343 put_page(stripe->sd_page);
2344 stripe->sd_page = NULL;
2345 stripe->sd_ent = NULL;
2349 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2353 for (i = 0; i < ctxt->ldc_count; i++)
2354 stripe_dirent_unload(&ctxt->ldc_stripes[i]);
2357 /* if @ent is dummy, or . .., get next */
2358 static struct lu_dirent *stripe_dirent_get(struct lmv_dir_ctxt *ctxt,
2359 struct lu_dirent *ent,
2362 for (; ent; ent = lu_dirent_next(ent)) {
2363 /* Skip dummy entry */
2364 if (le16_to_cpu(ent->lde_namelen) == 0)
2367 /* skip . and .. for other stripes */
2369 (strncmp(ent->lde_name, ".",
2370 le16_to_cpu(ent->lde_namelen)) == 0 ||
2371 strncmp(ent->lde_name, "..",
2372 le16_to_cpu(ent->lde_namelen)) == 0))
2375 if (le64_to_cpu(ent->lde_hash) >= ctxt->ldc_hash)
2382 static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
2383 struct stripe_dirent *stripe,
2386 struct md_op_data *op_data = ctxt->ldc_op_data;
2387 struct lmv_oinfo *oinfo;
2388 struct lu_fid fid = op_data->op_fid1;
2389 struct inode *inode = op_data->op_data;
2390 struct lmv_tgt_desc *tgt;
2391 struct lu_dirent *ent = stripe->sd_ent;
2392 __u64 hash = ctxt->ldc_hash;
2397 LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2401 if (stripe->sd_page) {
2402 __u64 end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2404 /* @hash should be the last dirent hash */
2405 LASSERTF(hash <= end,
2406 "ctxt@%p stripe@%p hash %llx end %llx\n",
2407 ctxt, stripe, hash, end);
2408 /* unload last page */
2409 stripe_dirent_unload(stripe);
2411 if (end == MDS_DIR_END_OFF) {
2412 stripe->sd_eof = true;
2418 oinfo = &op_data->op_mea1->lsm_md_oinfo[stripe_index];
2419 if (!oinfo->lmo_root) {
2424 tgt = lmv_tgt(ctxt->ldc_lmv, oinfo->lmo_mds);
2430 /* op_data is shared by stripes, reset after use */
2431 op_data->op_fid1 = oinfo->lmo_fid;
2432 op_data->op_fid2 = oinfo->lmo_fid;
2433 op_data->op_data = oinfo->lmo_root;
2435 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_cb_op, hash,
2438 op_data->op_fid1 = fid;
2439 op_data->op_fid2 = fid;
2440 op_data->op_data = inode;
2445 stripe->sd_dp = page_address(stripe->sd_page);
2446 ent = stripe_dirent_get(ctxt, lu_dirent_start(stripe->sd_dp),
2448 /* in case a page filled with ., .. and dummy, read next */
2451 stripe->sd_ent = ent;
2454 /* treat error as eof, so dir can be partially accessed */
2455 stripe->sd_eof = true;
2456 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, "
2457 "directory is partially accessed!\n",
2458 PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
2465 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
2467 struct obd_device *obd = exp->exp_obd;
2468 struct lmv_obd *lmv = &obd->u.lmv;
2469 struct lmv_tgt_desc *tgt;
2474 rc = lmv_check_connect(obd);
2478 tgt = lmv_fid2tgt(lmv, &data->op_fid1);
2480 RETURN(PTR_ERR(tgt));
2482 data->op_flags |= MF_MDC_CANCEL_FID1;
2483 rc = md_file_resync(tgt->ltd_exp, data);
2488 * Get dirent with the closest hash for striped directory
2490 * This function will search the dir entry, whose hash value is the
2491 * closest(>=) to hash from all of sub-stripes, and it is only being called
2492 * for striped directory.
2494 * \param[in] ctxt dir read context
2496 * \retval dirent get the entry successfully
2497 * NULL does not get the entry, normally it means
2498 * it reaches the end of the directory, while read
2499 * stripe dirent error is ignored to allow partial
2502 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
2504 struct stripe_dirent *stripe;
2505 struct lu_dirent *ent = NULL;
2509 /* TODO: optimize with k-way merge sort */
2510 for (i = 0; i < ctxt->ldc_count; i++) {
2511 stripe = &ctxt->ldc_stripes[i];
2515 if (!stripe->sd_ent) {
2516 stripe_dirent_load(ctxt, stripe, i);
2517 if (!stripe->sd_ent) {
2518 LASSERT(stripe->sd_eof);
2524 le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
2525 le64_to_cpu(stripe->sd_ent->lde_hash)) {
2527 if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
2534 stripe = &ctxt->ldc_stripes[min];
2535 ent = stripe->sd_ent;
2536 /* pop found dirent */
2537 stripe->sd_ent = stripe_dirent_get(ctxt, lu_dirent_next(ent),
2545 * Build dir entry page for striped directory
2547 * This function gets one entry by @offset from a striped directory. It will
2548 * read entries from all of stripes, and choose one closest to the required
2549 * offset(&offset). A few notes
2550 * 1. skip . and .. for non-zero stripes, because there can only have one .
2551 * and .. in a directory.
2552 * 2. op_data will be shared by all of stripes, instead of allocating new
2553 * one, so need to restore before reusing.
2555 * \param[in] exp obd export refer to LMV
2556 * \param[in] op_data hold those MD parameters of read_entry
2557 * \param[in] cb_op ldlm callback being used in enqueue in mdc_read_entry
2558 * \param[in] offset starting hash offset
2559 * \param[out] ppage the page holding the entry. Note: because the entry
2560 * will be accessed in upper layer, so we need hold the
2561 * page until the usages of entry is finished, see
2562 * ll_dir_entry_next.
2564 * retval =0 if get entry successfully
2565 * <0 cannot get entry
2567 static int lmv_striped_read_page(struct obd_export *exp,
2568 struct md_op_data *op_data,
2569 struct md_callback *cb_op,
2570 __u64 offset, struct page **ppage)
2572 struct page *page = NULL;
2573 struct lu_dirpage *dp;
2575 struct lu_dirent *ent;
2576 struct lu_dirent *last_ent;
2578 struct lmv_dir_ctxt *ctxt;
2579 struct lu_dirent *next = NULL;
2585 /* Allocate a page and read entries from all of stripes and fill
2586 * the page by hash order */
2587 page = alloc_page(GFP_KERNEL);
2591 /* Initialize the entry page */
2593 memset(dp, 0, sizeof(*dp));
2594 dp->ldp_hash_start = cpu_to_le64(offset);
2597 left_bytes = PAGE_SIZE - sizeof(*dp);
2601 /* initalize dir read context */
2602 stripe_count = op_data->op_mea1->lsm_md_stripe_count;
2603 OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2605 GOTO(free_page, rc = -ENOMEM);
2606 ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
2607 ctxt->ldc_op_data = op_data;
2608 ctxt->ldc_cb_op = cb_op;
2609 ctxt->ldc_hash = offset;
2610 ctxt->ldc_count = stripe_count;
2613 next = lmv_dirent_next(ctxt);
2615 /* end of directory */
2617 ctxt->ldc_hash = MDS_DIR_END_OFF;
2620 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
2622 ent_size = le16_to_cpu(next->lde_reclen);
2624 /* the last entry lde_reclen is 0, but it might not be the last
2625 * one of this temporay dir page */
2627 ent_size = lu_dirent_calc_size(
2628 le16_to_cpu(next->lde_namelen),
2629 le32_to_cpu(next->lde_attrs));
2631 if (ent_size > left_bytes)
2634 memcpy(ent, next, ent_size);
2636 /* Replace . with master FID and Replace .. with the parent FID
2637 * of master object */
2638 if (strncmp(ent->lde_name, ".",
2639 le16_to_cpu(ent->lde_namelen)) == 0 &&
2640 le16_to_cpu(ent->lde_namelen) == 1)
2641 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
2642 else if (strncmp(ent->lde_name, "..",
2643 le16_to_cpu(ent->lde_namelen)) == 0 &&
2644 le16_to_cpu(ent->lde_namelen) == 2)
2645 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2647 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
2648 le16_to_cpu(ent->lde_namelen), ent->lde_name,
2649 le64_to_cpu(ent->lde_hash));
2651 left_bytes -= ent_size;
2652 ent->lde_reclen = cpu_to_le16(ent_size);
2654 ent = (void *)ent + ent_size;
2657 last_ent->lde_reclen = 0;
2660 dp->ldp_flags |= LDF_EMPTY;
2661 else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
2662 dp->ldp_flags |= LDF_COLLIDE;
2663 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2664 dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
2666 put_lmv_dir_ctxt(ctxt);
2667 OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2680 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2681 struct md_callback *cb_op, __u64 offset,
2682 struct page **ppage)
2684 struct obd_device *obd = exp->exp_obd;
2685 struct lmv_obd *lmv = &obd->u.lmv;
2686 struct lmv_tgt_desc *tgt;
2691 if (unlikely(lmv_dir_foreign(op_data->op_mea1)))
2694 if (unlikely(lmv_dir_striped(op_data->op_mea1))) {
2695 rc = lmv_striped_read_page(exp, op_data, cb_op, offset, ppage);
2699 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2701 RETURN(PTR_ERR(tgt));
2703 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2709 * Unlink a file/directory
2711 * Unlink a file or directory under the parent dir. The unlink request
2712 * usually will be sent to the MDT where the child is located, but if
2713 * the client does not have the child FID then request will be sent to the
2714 * MDT where the parent is located.
2716 * If the parent is a striped directory then it also needs to locate which
2717 * stripe the name of the child is located, and replace the parent FID
2718 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2719 * it will walk through all of sub-stripes until the child is being
2722 * \param[in] exp export refer to LMV
2723 * \param[in] op_data different parameters transferred beween client
2724 * MD stacks, name, namelen, FIDs etc.
2725 * op_fid1 is the parent FID, op_fid2 is the child
2727 * \param[out] request point to the request of unlink.
2729 * retval 0 if succeed
2730 * negative errno if failed.
2732 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2733 struct ptlrpc_request **request)
2735 struct obd_device *obd = exp->exp_obd;
2736 struct lmv_obd *lmv = &obd->u.lmv;
2737 struct lmv_tgt_desc *tgt;
2738 struct lmv_tgt_desc *parent_tgt;
2739 struct mdt_body *body;
2744 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2745 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2746 op_data->op_cap = cfs_curproc_cap_pack();
2749 parent_tgt = lmv_locate_tgt(lmv, op_data);
2750 if (IS_ERR(parent_tgt))
2751 RETURN(PTR_ERR(parent_tgt));
2753 if (likely(!fid_is_zero(&op_data->op_fid2))) {
2754 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
2756 RETURN(PTR_ERR(tgt));
2762 * If child's fid is given, cancel unused locks for it if it is from
2763 * another export than parent.
2765 * LOOKUP lock for child (fid3) should also be cancelled on parent
2766 * tgt_tgt in mdc_unlink().
2768 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2770 if (parent_tgt != tgt)
2771 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index,
2772 LCK_EX, MDS_INODELOCK_LOOKUP,
2773 MF_MDC_CANCEL_FID3);
2775 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
2776 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2780 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2781 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
2784 rc = md_unlink(tgt->ltd_exp, op_data, request);
2785 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2786 ptlrpc_req_finished(*request);
2794 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2798 /* Not cross-ref case, just get out of here. */
2799 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2802 /* This is a remote object, try remote MDT. */
2803 op_data->op_fid2 = body->mbo_fid1;
2804 ptlrpc_req_finished(*request);
2807 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
2809 RETURN(PTR_ERR(tgt));
2814 static int lmv_precleanup(struct obd_device *obd)
2817 libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
2818 fld_client_debugfs_fini(&obd->u.lmv.lmv_fld);
2819 lprocfs_obd_cleanup(obd);
2820 lprocfs_free_md_stats(obd);
2825 * Get by key a value associated with a LMV device.
2827 * Dispatch request to lower-layer devices as needed.
2829 * \param[in] env execution environment for this thread
2830 * \param[in] exp export for the LMV device
2831 * \param[in] keylen length of key identifier
2832 * \param[in] key identifier of key to get value for
2833 * \param[in] vallen size of \a val
2834 * \param[out] val pointer to storage location for value
2835 * \param[in] lsm optional striping metadata of object
2837 * \retval 0 on success
2838 * \retval negative negated errno on failure
2840 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2841 __u32 keylen, void *key, __u32 *vallen, void *val)
2843 struct obd_device *obd;
2844 struct lmv_obd *lmv;
2845 struct lu_tgt_desc *tgt;
2850 obd = class_exp2obd(exp);
2852 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2853 exp->exp_handle.h_cookie);
2858 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2859 LASSERT(*vallen == sizeof(__u32));
2860 lmv_foreach_connected_tgt(lmv, tgt) {
2861 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2866 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2867 KEY_IS(KEY_DEFAULT_EASIZE) ||
2868 KEY_IS(KEY_CONN_DATA)) {
2870 * Forwarding this request to first MDS, it should know LOV
2873 tgt = lmv_tgt(lmv, 0);
2877 rc = obd_get_info(env, tgt->ltd_exp, keylen, key, vallen, val);
2878 if (!rc && KEY_IS(KEY_CONN_DATA))
2879 exp->exp_connect_data = *(struct obd_connect_data *)val;
2881 } else if (KEY_IS(KEY_TGT_COUNT)) {
2882 *((int *)val) = lmv->desc.ld_tgt_count;
2886 CDEBUG(D_IOCTL, "Invalid key\n");
2890 static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
2891 int *__rcs, struct ptlrpc_request_set *_set)
2893 struct obd_device *obddev = class_exp2obd(exp);
2894 struct ptlrpc_request_set *set = _set;
2895 struct lmv_obd *lmv = &obddev->u.lmv;
2896 int tgt_count = lmv->desc.ld_tgt_count;
2897 struct lu_tgt_desc *tgt;
2898 struct fid_array *fat, **fas = NULL;
2899 int i, rc, **rcs = NULL;
2902 set = ptlrpc_prep_set();
2907 /* split FIDs by targets */
2908 OBD_ALLOC(fas, sizeof(fas) * tgt_count);
2910 GOTO(out, rc = -ENOMEM);
2911 OBD_ALLOC(rcs, sizeof(int *) * tgt_count);
2913 GOTO(out_fas, rc = -ENOMEM);
2915 for (i = 0; i < fa->fa_nr; i++) {
2918 rc = lmv_fld_lookup(lmv, &fa->fa_fids[i], &idx);
2920 CDEBUG(D_OTHER, "can't lookup "DFID": rc = %d\n",
2921 PFID(&fa->fa_fids[i]), rc);
2924 LASSERT(idx < tgt_count);
2926 OBD_ALLOC(fas[idx], offsetof(struct fid_array,
2927 fa_fids[fa->fa_nr]));
2929 GOTO(out, rc = -ENOMEM);
2931 OBD_ALLOC(rcs[idx], sizeof(int) * fa->fa_nr);
2933 GOTO(out, rc = -ENOMEM);
2936 fat->fa_fids[fat->fa_nr++] = fa->fa_fids[i];
2939 lmv_foreach_connected_tgt(lmv, tgt) {
2940 fat = fas[tgt->ltd_index];
2941 if (!fat || fat->fa_nr == 0)
2943 rc = md_rmfid(tgt->ltd_exp, fat, rcs[tgt->ltd_index], set);
2946 rc = ptlrpc_set_wait(NULL, set);
2949 for (i = 0; i < tgt_count; i++) {
2951 if (!fat || fat->fa_nr == 0)
2953 /* copy FIDs back */
2954 memcpy(fa->fa_fids + j, fat->fa_fids,
2955 fat->fa_nr * sizeof(struct lu_fid));
2957 memcpy(__rcs + j, rcs[i], fat->fa_nr * sizeof(**rcs));
2962 ptlrpc_set_destroy(set);
2965 for (i = 0; i < tgt_count; i++) {
2967 OBD_FREE(fas[i], offsetof(struct fid_array,
2968 fa_fids[fa->fa_nr]));
2970 OBD_FREE(rcs[i], sizeof(int) * fa->fa_nr);
2973 OBD_FREE(rcs, sizeof(int *) * tgt_count);
2976 OBD_FREE(fas, sizeof(fas) * tgt_count);
2982 * Asynchronously set by key a value associated with a LMV device.
2984 * Dispatch request to lower-layer devices as needed.
2986 * \param[in] env execution environment for this thread
2987 * \param[in] exp export for the LMV device
2988 * \param[in] keylen length of key identifier
2989 * \param[in] key identifier of key to store value for
2990 * \param[in] vallen size of value to store
2991 * \param[in] val pointer to data to be stored
2992 * \param[in] set optional list of related ptlrpc requests
2994 * \retval 0 on success
2995 * \retval negative negated errno on failure
2997 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2998 __u32 keylen, void *key, __u32 vallen, void *val,
2999 struct ptlrpc_request_set *set)
3001 struct lmv_tgt_desc *tgt = NULL;
3002 struct obd_device *obd;
3003 struct lmv_obd *lmv;
3007 obd = class_exp2obd(exp);
3009 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
3010 exp->exp_handle.h_cookie);
3015 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
3016 KEY_IS(KEY_DEFAULT_EASIZE)) {
3019 lmv_foreach_connected_tgt(lmv, tgt) {
3020 err = obd_set_info_async(env, tgt->ltd_exp,
3021 keylen, key, vallen, val, set);
3032 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
3033 const struct lmv_mds_md_v1 *lmm1)
3035 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3042 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
3043 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3044 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
3045 if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
3046 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
3048 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
3049 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
3050 lsm->lsm_md_migrate_offset = le32_to_cpu(lmm1->lmv_migrate_offset);
3051 lsm->lsm_md_migrate_hash = le32_to_cpu(lmm1->lmv_migrate_hash);
3052 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
3053 sizeof(lsm->lsm_md_pool_name));
3055 if (cplen >= sizeof(lsm->lsm_md_pool_name))
3058 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %#x "
3059 "layout_version %d\n", lsm->lsm_md_stripe_count,
3060 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
3061 lsm->lsm_md_layout_version);
3063 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3064 for (i = 0; i < stripe_count; i++) {
3065 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
3066 &lmm1->lmv_stripe_fids[i]);
3068 * set default value -1, so lmv_locate_tgt() knows this stripe
3069 * target is not initialized.
3071 lsm->lsm_md_oinfo[i].lmo_mds = (u32)-1;
3072 if (!fid_is_sane(&lsm->lsm_md_oinfo[i].lmo_fid))
3075 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
3076 &lsm->lsm_md_oinfo[i].lmo_mds);
3083 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
3084 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
3090 static inline int lmv_unpack_user_md(struct obd_export *exp,
3091 struct lmv_stripe_md *lsm,
3092 const struct lmv_user_md *lmu)
3094 lsm->lsm_md_magic = le32_to_cpu(lmu->lum_magic);
3095 lsm->lsm_md_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
3096 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmu->lum_stripe_offset);
3097 lsm->lsm_md_hash_type = le32_to_cpu(lmu->lum_hash_type);
3102 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
3103 const union lmv_mds_md *lmm, size_t lmm_size)
3105 struct lmv_stripe_md *lsm;
3108 bool allocated = false;
3111 LASSERT(lsmp != NULL);
3115 if (lsm != NULL && lmm == NULL) {
3117 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lsm;
3119 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3122 lfm_size = lfm->lfm_length + offsetof(typeof(*lfm),
3124 OBD_FREE_LARGE(lfm, lfm_size);
3128 if (lmv_dir_striped(lsm)) {
3129 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3130 if (lsm->lsm_md_oinfo[i].lmo_root)
3131 iput(lsm->lsm_md_oinfo[i].lmo_root);
3133 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
3135 lsm_size = lmv_stripe_md_size(0);
3137 OBD_FREE(lsm, lsm_size);
3142 /* foreign lmv case */
3143 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_FOREIGN) {
3144 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lsm;
3147 OBD_ALLOC_LARGE(lfm, lmm_size);
3150 *lsmp = (struct lmv_stripe_md *)lfm;
3152 lfm->lfm_magic = le32_to_cpu(lmm->lmv_foreign_md.lfm_magic);
3153 lfm->lfm_length = le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3154 lfm->lfm_type = le32_to_cpu(lmm->lmv_foreign_md.lfm_type);
3155 lfm->lfm_flags = le32_to_cpu(lmm->lmv_foreign_md.lfm_flags);
3156 memcpy(&lfm->lfm_value, &lmm->lmv_foreign_md.lfm_value,
3161 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
3165 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
3166 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
3167 CERROR("%s: invalid lmv magic %x: rc = %d\n",
3168 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
3173 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
3174 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
3177 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
3178 * stripecount should be 0 then.
3180 lsm_size = lmv_stripe_md_size(0);
3183 OBD_ALLOC(lsm, lsm_size);
3190 switch (le32_to_cpu(lmm->lmv_magic)) {
3192 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
3194 case LMV_USER_MAGIC:
3195 rc = lmv_unpack_user_md(exp, lsm, &lmm->lmv_user_md);
3198 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3199 le32_to_cpu(lmm->lmv_magic));
3204 if (rc != 0 && allocated) {
3205 OBD_FREE(lsm, lsm_size);
3212 void lmv_free_memmd(struct lmv_stripe_md *lsm)
3214 lmv_unpackmd(NULL, &lsm, NULL, 0);
3216 EXPORT_SYMBOL(lmv_free_memmd);
3218 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3219 union ldlm_policy_data *policy,
3220 enum ldlm_mode mode, enum ldlm_cancel_flags flags,
3223 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3224 struct lu_tgt_desc *tgt;
3230 LASSERT(fid != NULL);
3232 lmv_foreach_connected_tgt(lmv, tgt) {
3233 if (!tgt->ltd_active)
3236 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3244 static int lmv_set_lock_data(struct obd_export *exp,
3245 const struct lustre_handle *lockh,
3246 void *data, __u64 *bits)
3248 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3249 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3254 if (tgt == NULL || tgt->ltd_exp == NULL)
3256 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3260 enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
3261 const struct lu_fid *fid, enum ldlm_type type,
3262 union ldlm_policy_data *policy,
3263 enum ldlm_mode mode, struct lustre_handle *lockh)
3265 struct obd_device *obd = exp->exp_obd;
3266 struct lmv_obd *lmv = &obd->u.lmv;
3268 struct lu_tgt_desc *tgt;
3274 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
3277 * With DNE every object can have two locks in different namespaces:
3278 * lookup lock in space of MDT storing direntry and update/open lock in
3279 * space of MDT storing inode. Try the MDT that the FID maps to first,
3280 * since this can be easily found, and only try others if that fails.
3282 for (i = 0, index = lmv_fid2tgt_index(lmv, fid);
3283 i < lmv->desc.ld_tgt_count;
3284 i++, index = (index + 1) % lmv->desc.ld_tgt_count) {
3286 CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
3287 obd->obd_name, PFID(fid), index);
3291 tgt = lmv_tgt(lmv, index);
3292 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
3295 rc = md_lock_match(tgt->ltd_exp, flags, fid, type, policy, mode,
3304 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
3305 struct obd_export *dt_exp, struct obd_export *md_exp,
3306 struct lustre_md *md)
3308 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3309 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3311 if (!tgt || !tgt->ltd_exp)
3314 return md_get_lustre_md(tgt->ltd_exp, req, dt_exp, md_exp, md);
3317 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3319 struct obd_device *obd = exp->exp_obd;
3320 struct lmv_obd *lmv = &obd->u.lmv;
3321 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3325 if (md->default_lmv) {
3326 lmv_free_memmd(md->default_lmv);
3327 md->default_lmv = NULL;
3329 if (md->lmv != NULL) {
3330 lmv_free_memmd(md->lmv);
3333 if (!tgt || !tgt->ltd_exp)
3335 RETURN(md_free_lustre_md(tgt->ltd_exp, md));
3338 int lmv_set_open_replay_data(struct obd_export *exp,
3339 struct obd_client_handle *och,
3340 struct lookup_intent *it)
3342 struct obd_device *obd = exp->exp_obd;
3343 struct lmv_obd *lmv = &obd->u.lmv;
3344 struct lmv_tgt_desc *tgt;
3348 tgt = lmv_fid2tgt(lmv, &och->och_fid);
3350 RETURN(PTR_ERR(tgt));
3352 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3355 int lmv_clear_open_replay_data(struct obd_export *exp,
3356 struct obd_client_handle *och)
3358 struct obd_device *obd = exp->exp_obd;
3359 struct lmv_obd *lmv = &obd->u.lmv;
3360 struct lmv_tgt_desc *tgt;
3364 tgt = lmv_fid2tgt(lmv, &och->och_fid);
3366 RETURN(PTR_ERR(tgt));
3368 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3371 int lmv_intent_getattr_async(struct obd_export *exp,
3372 struct md_enqueue_info *minfo)
3374 struct md_op_data *op_data = &minfo->mi_data;
3375 struct obd_device *obd = exp->exp_obd;
3376 struct lmv_obd *lmv = &obd->u.lmv;
3377 struct lmv_tgt_desc *ptgt;
3378 struct lmv_tgt_desc *ctgt;
3383 if (!fid_is_sane(&op_data->op_fid2))
3386 ptgt = lmv_locate_tgt(lmv, op_data);
3388 RETURN(PTR_ERR(ptgt));
3390 ctgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
3392 RETURN(PTR_ERR(ctgt));
3395 * remote object needs two RPCs to lookup and getattr, considering the
3396 * complexity don't support statahead for now.
3401 rc = md_intent_getattr_async(ptgt->ltd_exp, minfo);
3406 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3407 struct lu_fid *fid, __u64 *bits)
3409 struct obd_device *obd = exp->exp_obd;
3410 struct lmv_obd *lmv = &obd->u.lmv;
3411 struct lmv_tgt_desc *tgt;
3416 tgt = lmv_fid2tgt(lmv, fid);
3418 RETURN(PTR_ERR(tgt));
3420 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3424 int lmv_get_fid_from_lsm(struct obd_export *exp,
3425 const struct lmv_stripe_md *lsm,
3426 const char *name, int namelen, struct lu_fid *fid)
3428 const struct lmv_oinfo *oinfo;
3430 LASSERT(lmv_dir_striped(lsm));
3432 oinfo = lsm_name_to_stripe_info(lsm, name, namelen, false);
3434 return PTR_ERR(oinfo);
3436 *fid = oinfo->lmo_fid;
3442 * For lmv, only need to send request to master MDT, and the master MDT will
3443 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3444 * we directly fetch data from the slave MDTs.
3446 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3447 struct obd_quotactl *oqctl)
3449 struct obd_device *obd = class_exp2obd(exp);
3450 struct lmv_obd *lmv = &obd->u.lmv;
3451 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3452 __u64 curspace, curinodes;
3457 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active) {
3458 CERROR("master lmv inactive\n");
3462 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3463 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3467 curspace = curinodes = 0;
3468 lmv_foreach_connected_tgt(lmv, tgt) {
3471 if (!tgt->ltd_active)
3474 err = obd_quotactl(tgt->ltd_exp, oqctl);
3476 CERROR("getquota on mdt %d failed. %d\n",
3477 tgt->ltd_index, err);
3481 curspace += oqctl->qc_dqblk.dqb_curspace;
3482 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3485 oqctl->qc_dqblk.dqb_curspace = curspace;
3486 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3491 static int lmv_merge_attr(struct obd_export *exp,
3492 const struct lmv_stripe_md *lsm,
3493 struct cl_attr *attr,
3494 ldlm_blocking_callback cb_blocking)
3499 if (!lmv_dir_striped(lsm))
3502 rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3506 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3507 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3513 "" DFID " size %llu, blocks %llu nlink %u, atime %lld ctime %lld, mtime %lld.\n",
3514 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3515 i_size_read(inode), (unsigned long long)inode->i_blocks,
3516 inode->i_nlink, (s64)inode->i_atime.tv_sec,
3517 (s64)inode->i_ctime.tv_sec, (s64)inode->i_mtime.tv_sec);
3519 /* for slave stripe, it needs to subtract nlink for . and .. */
3521 attr->cat_nlink += inode->i_nlink - 2;
3523 attr->cat_nlink = inode->i_nlink;
3525 attr->cat_size += i_size_read(inode);
3526 attr->cat_blocks += inode->i_blocks;
3528 if (attr->cat_atime < inode->i_atime.tv_sec)
3529 attr->cat_atime = inode->i_atime.tv_sec;
3531 if (attr->cat_ctime < inode->i_ctime.tv_sec)
3532 attr->cat_ctime = inode->i_ctime.tv_sec;
3534 if (attr->cat_mtime < inode->i_mtime.tv_sec)
3535 attr->cat_mtime = inode->i_mtime.tv_sec;
3540 struct obd_ops lmv_obd_ops = {
3541 .o_owner = THIS_MODULE,
3542 .o_setup = lmv_setup,
3543 .o_cleanup = lmv_cleanup,
3544 .o_precleanup = lmv_precleanup,
3545 .o_process_config = lmv_process_config,
3546 .o_connect = lmv_connect,
3547 .o_disconnect = lmv_disconnect,
3548 .o_statfs = lmv_statfs,
3549 .o_get_info = lmv_get_info,
3550 .o_set_info_async = lmv_set_info_async,
3551 .o_notify = lmv_notify,
3552 .o_get_uuid = lmv_get_uuid,
3553 .o_fid_alloc = lmv_fid_alloc,
3554 .o_iocontrol = lmv_iocontrol,
3555 .o_quotactl = lmv_quotactl
3558 struct md_ops lmv_md_ops = {
3559 .m_get_root = lmv_get_root,
3560 .m_null_inode = lmv_null_inode,
3561 .m_close = lmv_close,
3562 .m_create = lmv_create,
3563 .m_enqueue = lmv_enqueue,
3564 .m_getattr = lmv_getattr,
3565 .m_getxattr = lmv_getxattr,
3566 .m_getattr_name = lmv_getattr_name,
3567 .m_intent_lock = lmv_intent_lock,
3569 .m_rename = lmv_rename,
3570 .m_setattr = lmv_setattr,
3571 .m_setxattr = lmv_setxattr,
3572 .m_fsync = lmv_fsync,
3573 .m_file_resync = lmv_file_resync,
3574 .m_read_page = lmv_read_page,
3575 .m_unlink = lmv_unlink,
3576 .m_init_ea_size = lmv_init_ea_size,
3577 .m_cancel_unused = lmv_cancel_unused,
3578 .m_set_lock_data = lmv_set_lock_data,
3579 .m_lock_match = lmv_lock_match,
3580 .m_get_lustre_md = lmv_get_lustre_md,
3581 .m_free_lustre_md = lmv_free_lustre_md,
3582 .m_merge_attr = lmv_merge_attr,
3583 .m_set_open_replay_data = lmv_set_open_replay_data,
3584 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3585 .m_intent_getattr_async = lmv_intent_getattr_async,
3586 .m_revalidate_lock = lmv_revalidate_lock,
3587 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
3588 .m_unpackmd = lmv_unpackmd,
3589 .m_rmfid = lmv_rmfid,
3592 static int __init lmv_init(void)
3594 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3595 LUSTRE_LMV_NAME, NULL);
3598 static void __exit lmv_exit(void)
3600 class_unregister_type(LUSTRE_LMV_NAME);
3603 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3604 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3605 MODULE_VERSION(LUSTRE_VERSION_STRING);
3606 MODULE_LICENSE("GPL");
3608 module_init(lmv_init);
3609 module_exit(lmv_exit);