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/
32 #define DEBUG_SUBSYSTEM S_LMV
34 #include <linux/file.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/user_namespace.h>
38 #include <linux/uidgid.h>
39 #include <linux/slab.h>
40 #include <linux/pagemap.h>
42 #include <linux/math64.h>
43 #include <linux/seq_file.h>
44 #include <linux/namei.h>
46 #include <obd_support.h>
47 #include <lustre_lib.h>
48 #include <lustre_net.h>
49 #include <obd_class.h>
50 #include <lustre_lmv.h>
51 #include <lprocfs_status.h>
52 #include <cl_object.h>
53 #include <lustre_fid.h>
54 #include <uapi/linux/lustre/lustre_ioctl.h>
55 #include <lustre_ioctl_old.h>
56 #include <lustre_kernelcomm.h>
57 #include "lmv_internal.h"
59 static int lmv_check_connect(struct obd_device *obd);
60 static inline bool lmv_op_default_rr_mkdir(const struct md_op_data *op_data);
62 void lmv_activate_target(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
65 if (tgt->ltd_active == activate)
68 tgt->ltd_active = activate;
69 lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count +=
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 lu_tgt_desc *lmv_tgt_retry(struct lmv_obd *lmv, __u32 index)
135 struct obd_device *obd = lmv2obd_dev(lmv);
136 struct lu_tgt_desc *tgt;
137 static time64_t next_print;
138 time64_t retry_limit = 0;
145 tgt = lmv_tgt(lmv, index);
146 if (likely(tgt && tgt->ltd_exp))
149 now = ktime_get_seconds();
150 if (retry_limit == 0) {
151 level = now > next_print ? D_WARNING : D_INFO;
152 retry_limit = now + RECONNECT_DELAY_MAX;
153 } else if (now > retry_limit) {
158 CDEBUG_LIMIT(level, index < lmv->lmv_mdt_count ?
159 "%s: MDT index %u/%u not configured\n" :
160 "%s: MDT index %u more than MDT count %u\n",
161 obd->obd_name, index, lmv->lmv_mdt_count);
163 if (index >= LOV_V1_INSANE_STRIPE_COUNT)
166 if (now > next_print) {
167 LCONSOLE_INFO("%s: wait %ds while client connects to new MDT\n",
168 obd->obd_name, (int)(retry_limit - now));
169 next_print = retry_limit + 600;
171 if (now < retry_limit) {
172 rc = schedule_timeout_interruptible(cfs_time_seconds(1));
180 static struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
182 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
183 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
185 return tgt ? obd_get_uuid(tgt->ltd_exp) : NULL;
188 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
189 enum obd_notify_event ev)
191 struct obd_connect_data *conn_data;
192 struct lmv_obd *lmv = &obd->u.lmv;
193 struct obd_uuid *uuid;
198 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
199 CERROR("unexpected notification of %s %s!\n",
200 watched->obd_type->typ_name,
205 uuid = &watched->u.cli.cl_target_uuid;
206 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
208 * Set MDC as active before notifying the observer, so the
209 * observer can use the MDC normally.
211 rc = lmv_set_mdc_active(lmv, uuid,
212 ev == OBD_NOTIFY_ACTIVE);
214 CERROR("%sactivation of %s failed: %d\n",
215 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
219 } else if (ev == OBD_NOTIFY_OCD) {
220 conn_data = &watched->u.cli.cl_import->imp_connect_data;
222 * XXX: Make sure that ocd_connect_flags from all targets are
223 * the same. Otherwise one of MDTs runs wrong version or
224 * something like this. --umka
226 obd->obd_self_export->exp_connect_data = *conn_data;
229 /* Pass the notification up the chain. */
230 if (obd->obd_observer)
231 rc = obd_notify(obd->obd_observer, watched, ev);
236 static int lmv_connect(const struct lu_env *env,
237 struct obd_export **pexp, struct obd_device *obd,
238 struct obd_uuid *cluuid, struct obd_connect_data *data,
241 struct lmv_obd *lmv = &obd->u.lmv;
242 struct lustre_handle conn = { 0 };
243 struct obd_export *exp;
248 rc = class_connect(&conn, obd, cluuid);
250 CERROR("class_connection() returned %d\n", rc);
254 exp = class_conn2export(&conn);
257 lmv->conn_data = *data;
258 lmv->lmv_cache = localdata;
260 lmv->lmv_tgts_kobj = kobject_create_and_add("target_obds",
261 &obd->obd_kset.kobj);
262 if (!lmv->lmv_tgts_kobj) {
263 CERROR("%s: cannot create /sys/fs/lustre/%s/%s/target_obds\n",
264 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
267 rc = lmv_check_connect(obd);
276 if (lmv->lmv_tgts_kobj)
277 kobject_put(lmv->lmv_tgts_kobj);
279 class_disconnect(exp);
284 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
287 struct obd_device *obd = exp->exp_obd;
288 struct lmv_obd *lmv = &obd->u.lmv;
289 struct lmv_tgt_desc *tgt;
295 if (lmv->max_easize < easize) {
296 lmv->max_easize = easize;
299 if (lmv->max_def_easize < def_easize) {
300 lmv->max_def_easize = def_easize;
307 if (lmv->connected == 0)
310 lmv_foreach_connected_tgt(lmv, tgt) {
311 if (!tgt->ltd_active)
314 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
316 CERROR("%s: obd_init_ea_size() failed on MDT target %d: rc = %d\n",
317 obd->obd_name, tgt->ltd_index, rc);
324 #define MAX_STRING_SIZE 128
326 static int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
328 struct lmv_obd *lmv = &obd->u.lmv;
329 struct obd_device *mdc_obd;
330 struct obd_export *mdc_exp;
331 struct lu_fld_target target;
336 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
339 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
343 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s\n",
344 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
345 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid);
347 if (!mdc_obd->obd_set_up) {
348 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
352 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
353 &lmv->conn_data, lmv->lmv_cache);
355 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
359 /* Init fid sequence client for this mdc and add new fld target. */
360 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
364 target.ft_srv = NULL;
365 target.ft_exp = mdc_exp;
366 target.ft_idx = tgt->ltd_index;
368 fld_client_add_target(&lmv->lmv_fld, &target);
370 rc = obd_register_observer(mdc_obd, obd);
372 obd_disconnect(mdc_exp);
373 CERROR("target %s register_observer error %d\n",
374 tgt->ltd_uuid.uuid, rc);
378 if (obd->obd_observer) {
379 /* Tell the observer about the new target. */
380 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
383 obd_disconnect(mdc_exp);
389 tgt->ltd_exp = mdc_exp;
390 lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count++;
392 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
394 rc = lu_qos_add_tgt(&lmv->lmv_qos, tgt);
396 obd_disconnect(mdc_exp);
400 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
401 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
402 kref_read(&obd->obd_refcount));
404 lmv_statfs_check_update(obd, tgt);
406 if (lmv->lmv_tgts_kobj)
407 /* Even if we failed to create the link, that's fine */
408 rc = sysfs_create_link(lmv->lmv_tgts_kobj,
409 &mdc_obd->obd_kset.kobj,
414 static void lmv_del_target(struct lmv_obd *lmv, struct lu_tgt_desc *tgt)
417 ltd_del_tgt(&lmv->lmv_mdt_descs, tgt);
421 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
422 __u32 index, int gen)
424 struct obd_device *mdc_obd;
425 struct lmv_obd *lmv = &obd->u.lmv;
426 struct lmv_tgt_desc *tgt;
427 struct lu_tgt_descs *ltd = &lmv->lmv_mdt_descs;
432 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
433 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
436 CERROR("%s: Target %s not attached: rc = %d\n",
437 obd->obd_name, uuidp->uuid, -EINVAL);
445 mutex_init(&tgt->ltd_fid_mutex);
446 tgt->ltd_index = index;
447 tgt->ltd_uuid = *uuidp;
450 mutex_lock(<d->ltd_mutex);
451 rc = ltd_add_tgt(ltd, tgt);
452 mutex_unlock(<d->ltd_mutex);
458 /* lmv_check_connect() will connect this target. */
461 rc = lmv_connect_mdc(obd, tgt);
463 int easize = sizeof(struct lmv_stripe_md) +
464 lmv->lmv_mdt_count * sizeof(struct lu_fid);
466 lmv_init_ea_size(obd->obd_self_export, easize, 0);
476 static int lmv_check_connect(struct obd_device *obd)
478 struct lmv_obd *lmv = &obd->u.lmv;
479 struct lmv_tgt_desc *tgt;
488 mutex_lock(&lmv->lmv_mdt_descs.ltd_mutex);
490 GOTO(unlock, rc = 0);
492 if (!lmv->lmv_mdt_count) {
493 CERROR("%s: no targets configured: rc = -EINVAL\n",
495 GOTO(unlock, rc = -EINVAL);
498 if (!lmv_mdt0_inited(lmv)) {
499 CERROR("%s: no target configured for index 0: rc = -EINVAL.\n",
501 GOTO(unlock, rc = -EINVAL);
504 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
505 obd->obd_uuid.uuid, obd->obd_name);
507 lmv_foreach_tgt(lmv, tgt) {
508 rc = lmv_connect_mdc(obd, tgt);
514 easize = lmv_mds_md_size(lmv->lmv_mdt_count, LMV_MAGIC);
515 lmv_init_ea_size(obd->obd_self_export, easize, 0);
518 mutex_unlock(&lmv->lmv_mdt_descs.ltd_mutex);
523 lmv_foreach_tgt(lmv, tgt) {
528 --lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count;
529 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
530 obd_disconnect(tgt->ltd_exp);
536 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
538 struct lmv_obd *lmv = &obd->u.lmv;
539 struct obd_device *mdc_obd;
544 LASSERT(tgt != NULL);
545 LASSERT(obd != NULL);
547 mdc_obd = class_exp2obd(tgt->ltd_exp);
550 mdc_obd->obd_force = obd->obd_force;
551 mdc_obd->obd_fail = obd->obd_fail;
552 mdc_obd->obd_no_recov = obd->obd_no_recov;
554 if (lmv->lmv_tgts_kobj)
555 sysfs_remove_link(lmv->lmv_tgts_kobj,
559 rc = lu_qos_del_tgt(&lmv->lmv_qos, tgt);
561 CERROR("%s: Can't del target from QoS table: rc = %d\n",
562 tgt->ltd_exp->exp_obd->obd_name, rc);
564 rc = fld_client_del_target(&lmv->lmv_fld, tgt->ltd_index);
566 CERROR("%s: Can't del fld targets: rc = %d\n",
567 tgt->ltd_exp->exp_obd->obd_name, rc);
569 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
571 CERROR("%s: Can't finalize fids factory: rc = %d\n",
572 tgt->ltd_exp->exp_obd->obd_name, rc);
574 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
575 tgt->ltd_exp->exp_obd->obd_name,
576 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
578 lmv_activate_target(lmv, tgt, 0);
579 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
580 rc = obd_disconnect(tgt->ltd_exp);
582 CERROR("%s: Target %s disconnect error: rc = %d\n",
583 tgt->ltd_exp->exp_obd->obd_name,
584 tgt->ltd_uuid.uuid, rc);
590 static int lmv_disconnect(struct obd_export *exp)
592 struct obd_device *obd = class_exp2obd(exp);
593 struct lmv_obd *lmv = &obd->u.lmv;
594 struct lmv_tgt_desc *tgt;
599 lmv_foreach_connected_tgt(lmv, tgt)
600 lmv_disconnect_mdc(obd, tgt);
602 if (lmv->lmv_tgts_kobj)
603 kobject_put(lmv->lmv_tgts_kobj);
606 rc = class_disconnect(exp);
611 static void lmv_statfs_update(struct lmv_obd *lmv, struct lmv_tgt_desc *tgt,
612 struct obd_statfs *osfs)
614 spin_lock(&lmv->lmv_lock);
615 tgt->ltd_statfs = *osfs;
616 tgt->ltd_statfs_age = ktime_get_seconds();
617 spin_unlock(&lmv->lmv_lock);
618 set_bit(LQ_DIRTY, &lmv->lmv_qos.lq_flags);
621 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
624 struct obd_device *obd = class_exp2obd(exp);
625 struct lmv_obd *lmv = &obd->u.lmv;
626 struct getinfo_fid2path *gf;
627 struct lmv_tgt_desc *tgt;
628 struct getinfo_fid2path *remote_gf = NULL;
629 struct lu_fid root_fid;
630 int remote_gf_size = 0;
631 int currentisenc = 0;
636 tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
638 RETURN(PTR_ERR(tgt));
640 root_fid = *gf->gf_u.gf_root_fid;
641 LASSERT(fid_is_sane(&root_fid));
644 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
645 if (rc != 0 && rc != -EREMOTE)
646 GOTO(out_fid2path, rc);
648 if (gf->gf_u.gf_path[0] == '/') {
649 /* by convention, server side (mdt_path_current()) puts
650 * a leading '/' to tell client that we are dealing with
659 /* If remote_gf != NULL, it means just building the
660 * path on the remote MDT, copy this path segment to gf.
662 if (remote_gf != NULL) {
663 struct getinfo_fid2path *ori_gf;
668 ori_gf = (struct getinfo_fid2path *)karg;
669 if (strlen(ori_gf->gf_u.gf_path) + 1 +
670 strlen(gf->gf_u.gf_path) + 1 > ori_gf->gf_pathlen)
671 GOTO(out_fid2path, rc = -EOVERFLOW);
673 ptr = ori_gf->gf_u.gf_path;
674 oldisenc = ptr[0] == '/';
676 len = strlen(gf->gf_u.gf_path);
678 /* move the current path to the right to release space
679 * for closer-to-root part
681 memmove(ptr + len - currentisenc + 1 + globalisenc,
683 strlen(ori_gf->gf_u.gf_path) - oldisenc + 1);
686 memcpy(ptr, gf->gf_u.gf_path + currentisenc,
688 ptr[len - currentisenc] = '/';
692 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
693 tgt->ltd_exp->exp_obd->obd_name,
694 gf->gf_u.gf_path, PFID(&gf->gf_fid), gf->gf_recno,
698 GOTO(out_fid2path, rc);
700 /* sigh, has to go to another MDT to do path building further */
701 if (remote_gf == NULL) {
702 remote_gf_size = sizeof(*remote_gf) + len - sizeof(*gf);
703 OBD_ALLOC(remote_gf, remote_gf_size);
704 if (remote_gf == NULL)
705 GOTO(out_fid2path, rc = -ENOMEM);
706 remote_gf->gf_pathlen = len - sizeof(*gf);
709 if (!fid_is_sane(&gf->gf_fid)) {
710 CERROR("%s: invalid FID "DFID": rc = %d\n",
711 tgt->ltd_exp->exp_obd->obd_name,
712 PFID(&gf->gf_fid), -EINVAL);
713 GOTO(out_fid2path, rc = -EINVAL);
716 tgt = lmv_fid2tgt(lmv, &gf->gf_fid);
718 GOTO(out_fid2path, rc = -EINVAL);
720 remote_gf->gf_fid = gf->gf_fid;
721 remote_gf->gf_recno = -1;
722 remote_gf->gf_linkno = -1;
723 memset(remote_gf->gf_u.gf_path, 0, remote_gf->gf_pathlen);
724 *remote_gf->gf_u.gf_root_fid = root_fid;
726 goto repeat_fid2path;
729 if (remote_gf != NULL)
730 OBD_FREE(remote_gf, remote_gf_size);
734 static int lmv_hsm_req_count(struct lmv_obd *lmv,
735 const struct hsm_user_request *hur,
736 const struct lmv_tgt_desc *tgt_mds)
738 struct lmv_tgt_desc *curr_tgt;
742 /* count how many requests must be sent to the given target */
743 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
744 curr_tgt = lmv_fid2tgt(lmv, &hur->hur_user_item[i].hui_fid);
745 if (IS_ERR(curr_tgt))
746 RETURN(PTR_ERR(curr_tgt));
747 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
753 static int lmv_hsm_req_build(struct lmv_obd *lmv,
754 struct hsm_user_request *hur_in,
755 const struct lmv_tgt_desc *tgt_mds,
756 struct hsm_user_request *hur_out)
759 struct lmv_tgt_desc *curr_tgt;
761 /* build the hsm_user_request for the given target */
762 hur_out->hur_request = hur_in->hur_request;
764 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
765 curr_tgt = lmv_fid2tgt(lmv, &hur_in->hur_user_item[i].hui_fid);
766 if (IS_ERR(curr_tgt))
767 RETURN(PTR_ERR(curr_tgt));
768 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
769 hur_out->hur_user_item[nr_out] =
770 hur_in->hur_user_item[i];
774 hur_out->hur_request.hr_itemcount = nr_out;
775 memcpy(hur_data(hur_out), hur_data(hur_in),
776 hur_in->hur_request.hr_data_len);
781 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
782 int len, struct lustre_kernelcomm *lk,
785 struct lmv_obd *lmv = &obd->u.lmv;
786 struct lu_tgt_desc *tgt;
791 /* unregister request (call from llapi_hsm_copytool_fini) */
792 lmv_foreach_connected_tgt(lmv, tgt)
793 /* try to clean as much as possible (continue on error) */
794 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
796 /* Whatever the result, remove copytool from kuc groups.
797 * Unreached coordinators will get EPIPE on next requests
798 * and will unregister automatically.
800 rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
805 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
806 int len, struct lustre_kernelcomm *lk,
809 struct lmv_obd *lmv = &obd->u.lmv;
811 bool any_set = false;
812 struct kkuc_ct_data *kcd;
814 struct lu_tgt_desc *tgt;
821 filp = fget(lk->lk_wfd);
825 if (lk->lk_flags & LK_FLG_DATANR)
826 kcd_size = offsetof(struct kkuc_ct_data,
827 kcd_archives[lk->lk_data_count]);
829 kcd_size = sizeof(*kcd);
831 OBD_ALLOC(kcd, kcd_size);
833 GOTO(err_fput, rc = -ENOMEM);
835 kcd->kcd_nr_archives = lk->lk_data_count;
836 if (lk->lk_flags & LK_FLG_DATANR) {
837 kcd->kcd_magic = KKUC_CT_DATA_ARRAY_MAGIC;
838 if (lk->lk_data_count > 0)
839 memcpy(kcd->kcd_archives, lk->lk_data,
840 sizeof(*kcd->kcd_archives) * lk->lk_data_count);
842 kcd->kcd_magic = KKUC_CT_DATA_BITMAP_MAGIC;
845 rc = libcfs_kkuc_group_add(filp, &obd->obd_uuid, lk->lk_uid,
846 lk->lk_group, kcd, kcd_size);
847 OBD_FREE(kcd, kcd_size);
851 /* All or nothing: try to register to all MDS.
852 * In case of failure, unregister from previous MDS,
853 * except if it because of inactive target.
855 lmv_foreach_connected_tgt(lmv, tgt) {
856 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
858 if (tgt->ltd_active) {
859 /* permanent error */
860 CERROR("%s: iocontrol MDC %s on MDT idx %d cmd %x: err = %d\n",
861 lmv2obd_dev(lmv)->obd_name,
862 tgt->ltd_uuid.uuid, tgt->ltd_index, cmd,
865 lk->lk_flags |= LK_FLG_STOP;
867 /* unregister from previous MDS */
868 lmv_foreach_connected_tgt(lmv, tgt) {
869 if (tgt->ltd_index >= i)
872 obd_iocontrol(cmd, tgt->ltd_exp, len,
875 GOTO(err_kkuc_rem, rc);
877 /* else: transient error.
878 * kuc will register to the missing MDT when it is back
886 /* no registration done: return error */
887 GOTO(err_kkuc_rem, rc = -ENOTCONN);
892 libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
899 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
900 int len, void *karg, void __user *uarg)
902 struct obd_device *obd = class_exp2obd(exp);
903 struct lmv_obd *lmv = &obd->u.lmv;
904 struct lu_tgt_desc *tgt = NULL;
906 __u32 count = lmv->lmv_mdt_count;
910 CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
911 exp->exp_obd->obd_name, cmd, len, karg, uarg);
915 /* exit early for unknown ioctl types */
916 if (unlikely(_IOC_TYPE(cmd) != 'f' && !IOC_OSC_SET_ACTIVE_ALLOW(cmd)))
917 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "unknown", -ENOTTY));
919 /* handle commands that don't use @karg first */
921 case LL_IOC_GET_CONNECT_FLAGS:
922 tgt = lmv_tgt(lmv, 0);
924 if (tgt && tgt->ltd_exp)
925 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, NULL, uarg);
929 if (unlikely(karg == NULL))
930 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", -EINVAL));
933 case IOC_OBD_STATFS: {
934 struct obd_ioctl_data *data = karg;
935 struct obd_device *mdc_obd;
936 struct obd_statfs stat_buf = {0};
939 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
941 if (index >= lmv->lmv_mdt_descs.ltd_tgts_size)
944 tgt = lmv_tgt(lmv, index);
948 if (!tgt->ltd_active)
951 mdc_obd = class_exp2obd(tgt->ltd_exp);
956 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
957 min((int) data->ioc_plen2,
958 (int) sizeof(struct obd_uuid))))
961 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
962 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
966 lmv_statfs_update(lmv, tgt, &stat_buf);
967 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
968 min_t(int, data->ioc_plen1, sizeof(stat_buf))))
972 case OBD_IOC_QUOTACTL: {
973 struct if_quotactl *qctl = karg;
974 struct obd_quotactl *oqctl;
975 struct obd_import *imp;
977 if (qctl->qc_valid == QC_MDTIDX) {
978 tgt = lmv_tgt(lmv, qctl->qc_idx);
979 } else if (qctl->qc_valid == QC_UUID) {
980 lmv_foreach_tgt(lmv, tgt) {
981 if (!obd_uuid_equals(&tgt->ltd_uuid,
1000 imp = class_exp2cliimp(tgt->ltd_exp);
1001 if (!tgt->ltd_active && imp->imp_state != LUSTRE_IMP_IDLE) {
1002 qctl->qc_valid = QC_MDTIDX;
1003 qctl->obd_uuid = tgt->ltd_uuid;
1007 OBD_ALLOC_PTR(oqctl);
1011 QCTL_COPY(oqctl, qctl);
1012 rc = obd_quotactl(tgt->ltd_exp, oqctl);
1014 QCTL_COPY_NO_PNAME(qctl, oqctl);
1015 qctl->qc_valid = QC_MDTIDX;
1016 qctl->obd_uuid = tgt->ltd_uuid;
1018 OBD_FREE_PTR(oqctl);
1021 case LL_IOC_FID2MDTIDX: {
1022 struct lu_fid *fid = karg;
1025 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1029 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1030 * point to user space memory for FID2MDTIDX.
1032 *(__u32 *)uarg = mdt_index;
1035 case OBD_IOC_FID2PATH: {
1036 rc = lmv_fid2path(exp, len, karg, uarg);
1039 case LL_IOC_HSM_STATE_GET:
1040 case LL_IOC_HSM_STATE_SET:
1041 case LL_IOC_HSM_ACTION: {
1042 struct md_op_data *op_data = karg;
1044 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1046 RETURN(PTR_ERR(tgt));
1048 if (tgt->ltd_exp == NULL)
1051 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1054 case LL_IOC_HSM_PROGRESS: {
1055 const struct hsm_progress_kernel *hpk = karg;
1057 tgt = lmv_fid2tgt(lmv, &hpk->hpk_fid);
1059 RETURN(PTR_ERR(tgt));
1060 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1063 case LL_IOC_HSM_REQUEST: {
1064 struct hsm_user_request *hur = karg;
1065 unsigned int reqcount = hur->hur_request.hr_itemcount;
1070 /* if the request is about a single fid or if there is a single
1071 * MDS, no need to split the request.
1073 if (reqcount == 1 || count == 1) {
1074 tgt = lmv_fid2tgt(lmv, &hur->hur_user_item[0].hui_fid);
1076 RETURN(PTR_ERR(tgt));
1077 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1079 /* split fid list to their respective MDS */
1080 lmv_foreach_connected_tgt(lmv, tgt) {
1083 struct hsm_user_request *req;
1085 nr = lmv_hsm_req_count(lmv, hur, tgt);
1088 if (nr == 0) /* nothing for this MDS */
1091 /* build a request with fids for this MDS */
1092 reqlen = offsetof(typeof(*hur),
1094 + hur->hur_request.hr_data_len;
1095 OBD_ALLOC_LARGE(req, reqlen);
1098 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1100 GOTO(hsm_req_err, rc1);
1101 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1104 if (rc1 != 0 && rc == 0)
1106 OBD_FREE_LARGE(req, reqlen);
1111 case LL_IOC_LOV_SWAP_LAYOUTS: {
1112 struct md_op_data *op_data = karg;
1113 struct lmv_tgt_desc *tgt1, *tgt2;
1115 tgt1 = lmv_fid2tgt(lmv, &op_data->op_fid1);
1117 RETURN(PTR_ERR(tgt1));
1119 tgt2 = lmv_fid2tgt(lmv, &op_data->op_fid2);
1121 RETURN(PTR_ERR(tgt2));
1123 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1126 /* only files on same MDT can have their layouts swapped */
1127 if (tgt1->ltd_index != tgt2->ltd_index)
1130 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1133 case LL_IOC_HSM_CT_START: {
1134 struct lustre_kernelcomm *lk = karg;
1136 if (lk->lk_flags & LK_FLG_STOP)
1137 rc = lmv_hsm_ct_unregister(obd, cmd, len, lk, uarg);
1139 rc = lmv_hsm_ct_register(obd, cmd, len, lk, uarg);
1143 lmv_foreach_connected_tgt(lmv, tgt) {
1144 struct obd_device *mdc_obd;
1147 /* ll_umount_begin() sets force flag but for lmv, not
1148 * mdc. Let's pass it through
1150 mdc_obd = class_exp2obd(tgt->ltd_exp);
1151 mdc_obd->obd_force = obd->obd_force;
1152 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1154 if (tgt->ltd_active) {
1155 OBD_IOC_ERROR(obd->obd_name, cmd,
1156 tgt->ltd_uuid.uuid, err);
1159 if (unlikely(err == -ENOTTY))
1173 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1174 struct lu_fid *fid, struct md_op_data *op_data)
1176 struct obd_device *obd = class_exp2obd(exp);
1177 struct lmv_obd *lmv = &obd->u.lmv;
1178 struct lmv_tgt_desc *tgt;
1186 tgt = lmv_tgt(lmv, op_data->op_mds);
1190 if (!tgt->ltd_active || !tgt->ltd_exp)
1194 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1195 * on server that seq in new allocated fid is not yet known.
1197 mutex_lock(&tgt->ltd_fid_mutex);
1198 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1199 mutex_unlock(&tgt->ltd_fid_mutex);
1201 LASSERT(fid_is_sane(fid));
1208 static u32 qos_exclude_hashfh(const void *data, u32 len, u32 seed)
1210 const char *name = data;
1212 return hashlen_hash(cfs_hashlen_string((void *)(unsigned long)seed,
1216 static int qos_exclude_cmpfn(struct rhashtable_compare_arg *arg,
1219 const struct qos_exclude_prefix *prefix = obj;
1220 const char *name = arg->key;
1222 return strcmp(name, prefix->qep_name);
1225 const struct rhashtable_params qos_exclude_hash_params = {
1226 .key_len = 1, /* actually variable */
1227 .key_offset = offsetof(struct qos_exclude_prefix, qep_name),
1228 .head_offset = offsetof(struct qos_exclude_prefix, qep_hash),
1229 .hashfn = qos_exclude_hashfh,
1230 .obj_cmpfn = qos_exclude_cmpfn,
1231 .automatic_shrinking = true,
1234 void qos_exclude_prefix_free(void *vprefix, void *data)
1236 struct qos_exclude_prefix *prefix = vprefix;
1238 list_del(&prefix->qep_list);
1242 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1244 struct lmv_obd *lmv = &obd->u.lmv;
1245 struct lmv_desc *desc;
1246 struct lnet_processid lnet_id;
1247 struct qos_exclude_prefix *prefix;
1253 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1254 CERROR("LMV setup requires a descriptor\n");
1258 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1259 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1260 CERROR("Lmv descriptor size wrong: %d > %d\n",
1261 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1265 obd_str2uuid(&lmv->lmv_mdt_descs.ltd_lmv_desc.ld_uuid,
1266 desc->ld_uuid.uuid);
1267 lmv->lmv_mdt_descs.ltd_lmv_desc.ld_tgt_count = 0;
1268 lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count = 0;
1269 lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage =
1270 LMV_DESC_QOS_MAXAGE_DEFAULT;
1271 lmv->max_def_easize = 0;
1272 lmv->max_easize = 0;
1274 spin_lock_init(&lmv->lmv_lock);
1275 INIT_LIST_HEAD(&lmv->lmv_qos_exclude_list);
1278 * initialize rr_index to lower 32bit of netid, so that client
1279 * can distribute subdirs evenly from the beginning.
1281 while (LNetGetId(i++, &lnet_id, true) != -ENOENT) {
1282 if (!nid_is_lo0(&lnet_id.nid)) {
1283 lmv->lmv_qos_rr_index = nidhash(&lnet_id.nid);
1288 rc = lmv_tunables_init(obd);
1290 CWARN("%s: error adding LMV sysfs/debugfs files: rc = %d\n",
1293 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1294 LUSTRE_CLI_FLD_HASH_DHT);
1296 CERROR("Can't init FLD, err %d\n", rc);
1298 rc = lu_tgt_descs_init(&lmv->lmv_mdt_descs, true);
1300 CWARN("%s: error initialize target table: rc = %d\n",
1303 rc = rhashtable_init(&lmv->lmv_qos_exclude_hash,
1304 &qos_exclude_hash_params);
1306 CERROR("%s: qos exclude hash initalize failed: %d\n",
1311 prefix = kmalloc(sizeof(*prefix), __GFP_ZERO);
1313 GOTO(out, rc = -ENOMEM);
1314 /* Apache Spark creates a _temporary directory for staging files */
1315 strcpy(prefix->qep_name, "_temporary");
1316 rc = rhashtable_insert_fast(&lmv->lmv_qos_exclude_hash,
1317 &prefix->qep_hash, qos_exclude_hash_params);
1323 list_add_tail(&prefix->qep_list, &lmv->lmv_qos_exclude_list);
1327 rhashtable_destroy(&lmv->lmv_qos_exclude_hash);
1331 static int lmv_cleanup(struct obd_device *obd)
1333 struct lmv_obd *lmv = &obd->u.lmv;
1334 struct lu_tgt_desc *tgt;
1335 struct lu_tgt_desc *tmp;
1339 rhashtable_free_and_destroy(&lmv->lmv_qos_exclude_hash,
1340 qos_exclude_prefix_free, NULL);
1341 fld_client_fini(&lmv->lmv_fld);
1342 fld_client_debugfs_fini(&lmv->lmv_fld);
1344 lprocfs_obd_cleanup(obd);
1345 lprocfs_free_md_stats(obd);
1347 lmv_foreach_tgt_safe(lmv, tgt, tmp)
1348 lmv_del_target(lmv, tgt);
1349 lu_tgt_descs_fini(&lmv->lmv_mdt_descs);
1354 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1356 struct lustre_cfg *lcfg = buf;
1357 struct obd_uuid obd_uuid;
1364 switch (lcfg->lcfg_command) {
1366 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1367 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID
1369 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1370 GOTO(out, rc = -EINVAL);
1372 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1374 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1375 GOTO(out, rc = -EINVAL);
1376 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1377 GOTO(out, rc = -EINVAL);
1378 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1381 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1382 GOTO(out, rc = -EINVAL);
1388 static int lmv_select_statfs_mdt(struct obd_export *exp, struct lmv_obd *lmv,
1391 bool large_nid = exp_connect_flags2(exp) & OBD_CONNECT2_LARGE_NID;
1394 if (flags & OBD_STATFS_FOR_MDT0)
1397 if (lmv->lmv_statfs_start || lmv->lmv_mdt_count == 1)
1398 return lmv->lmv_statfs_start;
1400 /* choose initial MDT for this client */
1402 struct lnet_processid lnet_id;
1404 if (LNetGetId(i, &lnet_id, large_nid) == -ENOENT)
1407 if (!nid_is_lo0(&lnet_id.nid)) {
1408 /* We dont need a full 64-bit modulus, just enough
1409 * to distribute the requests across MDTs evenly.
1411 lmv->lmv_statfs_start = nidhash(&lnet_id.nid) %
1417 return lmv->lmv_statfs_start;
1420 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1421 struct obd_statfs *osfs, time64_t max_age, __u32 flags)
1423 struct obd_device *obd = class_exp2obd(exp);
1424 struct lmv_obd *lmv = &obd->u.lmv;
1425 struct obd_statfs *temp;
1426 struct lu_tgt_desc *tgt;
1434 OBD_ALLOC(temp, sizeof(*temp));
1438 /* distribute statfs among MDTs */
1439 idx = lmv_select_statfs_mdt(exp, lmv, flags);
1441 for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++, idx++) {
1442 idx = idx % lmv->lmv_mdt_descs.ltd_tgts_size;
1443 tgt = lmv_tgt(lmv, idx);
1444 if (!tgt || !tgt->ltd_exp)
1447 rc = obd_statfs(env, tgt->ltd_exp, temp, max_age,
1448 flags | OBD_STATFS_NESTED);
1450 CERROR("%s: can't stat MDS #%d: rc = %d\n",
1451 tgt->ltd_exp->exp_obd->obd_name, i, rc);
1453 /* Try another MDT */
1454 if (flags & OBD_STATFS_SUM)
1456 GOTO(out_free_temp, rc);
1459 if (temp->os_state & OS_STATFS_SUM ||
1460 flags == OBD_STATFS_FOR_MDT0) {
1461 /* reset to the last aggregated values and don't sum
1462 * with non-aggrated data
1464 * If the statfs is from mount, it needs to retrieve
1465 * necessary info from MDT0. i.e. mount does not need
1466 * the merged osfs from all of MDT. Also clients can be
1467 * mounted as long as MDT0 is in service
1470 GOTO(out_free_temp, rc);
1476 osfs->os_bavail += temp->os_bavail;
1477 osfs->os_blocks += temp->os_blocks;
1478 osfs->os_ffree += temp->os_ffree;
1479 osfs->os_files += temp->os_files;
1480 osfs->os_granted += temp->os_granted;
1483 /* There is no stats from some MDTs, data incomplete */
1487 OBD_FREE(temp, sizeof(*temp));
1491 static int lmv_statfs_cb(void *cookie, int rc)
1493 struct obd_info *oinfo = cookie;
1494 struct obd_device *obd = oinfo->oi_obd;
1495 struct lmv_obd *lmv = &obd->u.lmv;
1496 struct lmv_tgt_desc *tgt = oinfo->oi_tgt;
1497 struct obd_statfs *osfs = oinfo->oi_osfs;
1500 * NB: don't deactivate TGT upon error, because we may not trigger async
1501 * statfs any longer, then there is no chance to activate TGT.
1504 lmv_statfs_update(lmv, tgt, osfs);
1509 /* update tgt statfs async if it's ld_qos_maxage old */
1510 int lmv_statfs_check_update(struct obd_device *obd, struct lmv_tgt_desc *tgt)
1512 struct obd_info oinfo = {
1515 .oi_cb_up = lmv_statfs_cb,
1519 if (ktime_get_seconds() - tgt->ltd_statfs_age <
1520 obd->u.lmv.lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage)
1523 rc = obd_statfs_async(tgt->ltd_exp, &oinfo, 0, NULL);
1528 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1531 struct obd_device *obd = exp->exp_obd;
1532 struct lmv_obd *lmv = &obd->u.lmv;
1533 struct lu_tgt_desc *tgt = lmv_tgt(lmv, 0);
1541 rc = md_get_root(tgt->ltd_exp, fileset, fid);
1543 lmv->lmv_setup_time = ktime_get_seconds();
1547 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1548 u64 obd_md_valid, const char *name, size_t buf_size,
1549 struct ptlrpc_request **req)
1551 struct obd_device *obd = exp->exp_obd;
1552 struct lmv_obd *lmv = &obd->u.lmv;
1553 struct lmv_tgt_desc *tgt;
1558 tgt = lmv_fid2tgt(lmv, fid);
1560 RETURN(PTR_ERR(tgt));
1562 rc = md_getxattr(tgt->ltd_exp, fid, obd_md_valid, name, buf_size, req);
1567 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1568 u64 obd_md_valid, const char *name,
1569 const void *value, size_t value_size,
1570 unsigned int xattr_flags, u32 suppgid,
1571 struct ptlrpc_request **req)
1573 struct obd_device *obd = exp->exp_obd;
1574 struct lmv_obd *lmv = &obd->u.lmv;
1575 struct lmv_tgt_desc *tgt;
1580 tgt = lmv_fid2tgt(lmv, fid);
1582 RETURN(PTR_ERR(tgt));
1584 rc = md_setxattr(tgt->ltd_exp, fid, obd_md_valid, name,
1585 value, value_size, xattr_flags, suppgid, req);
1590 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1591 struct ptlrpc_request **request)
1593 struct obd_device *obd = exp->exp_obd;
1594 struct lmv_obd *lmv = &obd->u.lmv;
1595 struct lmv_tgt_desc *tgt;
1600 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1602 RETURN(PTR_ERR(tgt));
1604 if (op_data->op_flags & MF_GET_MDT_IDX) {
1605 op_data->op_mds = tgt->ltd_index;
1609 rc = md_getattr(tgt->ltd_exp, op_data, request);
1614 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1616 struct obd_device *obd = exp->exp_obd;
1617 struct lmv_obd *lmv = &obd->u.lmv;
1618 struct lu_tgt_desc *tgt;
1622 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1625 * With DNE every object can have two locks in different namespaces:
1626 * lookup lock in space of MDT storing direntry and update/open lock in
1627 * space of MDT storing inode.
1629 lmv_foreach_connected_tgt(lmv, tgt)
1630 md_null_inode(tgt->ltd_exp, fid);
1635 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1636 struct md_open_data *mod, struct ptlrpc_request **request)
1638 struct obd_device *obd = exp->exp_obd;
1639 struct lmv_obd *lmv = &obd->u.lmv;
1640 struct lmv_tgt_desc *tgt;
1645 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
1647 RETURN(PTR_ERR(tgt));
1649 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1650 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1654 static inline bool tgt_qos_is_usable(struct lmv_obd *lmv,
1655 struct lu_tgt_desc *tgt, time64_t now)
1657 struct obd_import *imp = class_exp2cliimp(tgt->ltd_exp);
1658 u32 maxage = lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage;
1660 return tgt->ltd_exp && tgt->ltd_active &&
1661 !(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE) &&
1662 (now - imp->imp_setup_time > (maxage >> 1) ||
1663 now - lmv->lmv_setup_time < (maxage << 1));
1666 static struct lu_tgt_desc *lmv_locate_tgt_qos(struct lmv_obd *lmv,
1667 struct md_op_data *op_data)
1669 struct lu_tgt_desc *tgt, *cur = NULL;
1670 time64_t now = ktime_get_seconds();
1671 __u64 total_avail = 0;
1672 __u64 total_weight = 0;
1673 __u64 cur_weight = 0;
1674 int total_usable = 0;
1680 if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1681 RETURN(ERR_PTR(-EAGAIN));
1683 down_write(&lmv->lmv_qos.lq_rw_sem);
1685 if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1686 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1688 rc = ltd_qos_penalties_calc(&lmv->lmv_mdt_descs);
1690 GOTO(unlock, tgt = ERR_PTR(rc));
1692 lmv_foreach_tgt(lmv, tgt) {
1693 if (!tgt_qos_is_usable(lmv, tgt, now)) {
1694 tgt->ltd_qos.ltq_usable = 0;
1697 /* update one hour overdue statfs */
1698 if (now - tgt->ltd_statfs_age >
1699 60 * lmv->lmv_mdt_descs.ltd_lmv_desc.ld_qos_maxage)
1700 lmv_statfs_check_update(lmv2obd_dev(lmv), tgt);
1701 tgt->ltd_qos.ltq_usable = 1;
1702 lu_tgt_qos_weight_calc(tgt, true);
1703 if (tgt->ltd_index == op_data->op_mds)
1705 total_avail += tgt->ltd_qos.ltq_avail;
1706 total_weight += tgt->ltd_qos.ltq_weight;
1710 /* If current MDT has above-average space and dir is not already using
1711 * round-robin to spread across more MDTs, stay on the parent MDT
1712 * to avoid creating needless remote MDT directories. Remote dirs
1713 * close to the root balance space more effectively than bottom dirs,
1714 * so prefer to create remote dirs at top level of directory tree.
1715 * "16 / (dir_depth + 10)" is the factor to make it less likely
1716 * for top-level directories to stay local unless they have more than
1717 * average free space, while deep dirs prefer local until more full.
1718 * depth=0 -> 160%, depth=3 -> 123%, depth=6 -> 100%,
1719 * depth=9 -> 84%, depth=12 -> 73%, depth=15 -> 64%
1721 if (!lmv_op_default_rr_mkdir(op_data)) {
1722 rand = total_avail * 16 /
1723 (total_usable * (op_data->op_dir_depth + 10));
1724 if (cur && cur->ltd_qos.ltq_avail >= rand) {
1730 rand = lu_prandom_u64_max(total_weight);
1732 lmv_foreach_connected_tgt(lmv, tgt) {
1733 if (!tgt->ltd_qos.ltq_usable)
1736 cur_weight += tgt->ltd_qos.ltq_weight;
1737 if (cur_weight < rand)
1740 ltd_qos_update(&lmv->lmv_mdt_descs, tgt, &total_weight);
1744 /* no proper target found */
1745 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1747 up_write(&lmv->lmv_qos.lq_rw_sem);
1752 static struct lu_tgt_desc *lmv_locate_tgt_rr(struct lmv_obd *lmv)
1754 time64_t now = ktime_get_seconds();
1755 struct lu_tgt_desc *tgt;
1761 spin_lock(&lmv->lmv_lock);
1762 for (i = 0; i < lmv->lmv_mdt_descs.ltd_tgts_size; i++) {
1763 index = (i + lmv->lmv_qos_rr_index) %
1764 lmv->lmv_mdt_descs.ltd_tgts_size;
1765 tgt = lmv_tgt(lmv, index);
1766 if (!tgt || !tgt_qos_is_usable(lmv, tgt, now))
1769 lmv->lmv_qos_rr_index = (tgt->ltd_index + 1) %
1770 lmv->lmv_mdt_descs.ltd_tgts_size;
1771 spin_unlock(&lmv->lmv_lock);
1775 spin_unlock(&lmv->lmv_lock);
1777 RETURN(ERR_PTR(-ENODEV));
1780 /* locate MDT which is less full (avoid the most full MDT) */
1781 static struct lu_tgt_desc *lmv_locate_tgt_lf(struct lmv_obd *lmv)
1783 struct lu_tgt_desc *min = NULL;
1784 struct lu_tgt_desc *tgt;
1790 if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1791 RETURN(ERR_PTR(-EAGAIN));
1793 down_write(&lmv->lmv_qos.lq_rw_sem);
1795 if (!ltd_qos_is_usable(&lmv->lmv_mdt_descs))
1796 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1798 lmv_foreach_tgt(lmv, tgt) {
1799 if (!tgt->ltd_exp || !tgt->ltd_active ||
1800 (tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE)) {
1801 tgt->ltd_qos.ltq_usable = 0;
1805 tgt->ltd_qos.ltq_usable = 1;
1806 lu_tgt_qos_weight_calc(tgt, true);
1807 avail += tgt->ltd_qos.ltq_avail;
1808 if (!min || min->ltd_qos.ltq_avail > tgt->ltd_qos.ltq_avail)
1812 /* avoid the most full MDT */
1814 avail -= min->ltd_qos.ltq_avail;
1816 rand = lu_prandom_u64_max(avail);
1818 lmv_foreach_connected_tgt(lmv, tgt) {
1819 if (!tgt->ltd_qos.ltq_usable)
1825 avail += tgt->ltd_qos.ltq_avail;
1832 /* no proper target found */
1833 GOTO(unlock, tgt = ERR_PTR(-EAGAIN));
1835 up_write(&lmv->lmv_qos.lq_rw_sem);
1840 /* locate MDT by file name, for striped directory, the file name hash decides
1841 * which stripe its dirent is stored.
1843 static struct lmv_tgt_desc *
1844 lmv_locate_tgt_by_name(struct lmv_obd *lmv, struct lmv_stripe_object *lso,
1845 const char *name, int namelen, struct lu_fid *fid,
1846 __u32 *mds, bool new_layout)
1848 struct lmv_tgt_desc *tgt;
1849 const struct lmv_oinfo *oinfo;
1851 if (!lmv_dir_striped(lso) || !namelen) {
1852 tgt = lmv_fid2tgt(lmv, fid);
1856 *mds = tgt->ltd_index;
1860 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1861 if (cfs_fail_val >= lso->lso_lsm.lsm_md_stripe_count)
1862 return ERR_PTR(-EBADF);
1863 oinfo = &lso->lso_lsm.lsm_md_oinfo[cfs_fail_val];
1865 oinfo = lsm_name_to_stripe_info(lso, name, namelen, new_layout);
1867 return ERR_CAST(oinfo);
1870 /* check stripe FID is sane */
1871 if (!fid_is_sane(&oinfo->lmo_fid))
1872 return ERR_PTR(-ENODEV);
1874 *fid = oinfo->lmo_fid;
1875 *mds = oinfo->lmo_mds;
1876 tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
1878 CDEBUG(D_INODE, "locate MDT %u parent "DFID"\n", *mds, PFID(fid));
1880 return tgt ? tgt : ERR_PTR(-ENODEV);
1884 * Locate MDT of op_data->op_fid1
1886 * For striped directory, it will locate the stripe by name hash, if hash_type
1887 * is unknown, it will return the stripe specified by 'op_data->op_stripe_index'
1888 * which is set outside, and if dir is migrating, 'op_data->op_new_layout'
1889 * indicates whether old or new layout is used to locate.
1891 * For plain directory, it just locate the MDT of op_data->op_fid1.
1893 * \param[in] lmv LMV device
1894 * \param[in/out] op_data client MD stack parameters, name, namelen etc,
1895 * op_mds and op_fid1 will be updated if op_lso1
1896 * indicates fid1 represents a striped directory.
1898 * retval pointer to the lmv_tgt_desc if succeed.
1899 * ERR_PTR(errno) if failed.
1901 struct lmv_tgt_desc *
1902 lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data)
1904 struct lmv_stripe_md *lsm;
1905 struct lmv_oinfo *oinfo;
1906 struct lmv_tgt_desc *tgt;
1908 if (lmv_dir_foreign(op_data->op_lso1))
1909 return ERR_PTR(-ENODATA);
1911 /* During creating VOLATILE file, it should honor the mdt
1912 * index if the file under striped dir is being restored, see
1915 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1916 op_data->op_mds != LMV_OFFSET_DEFAULT) {
1917 tgt = lmv_tgt(lmv, op_data->op_mds);
1919 return ERR_PTR(-ENODEV);
1921 if (lmv_dir_striped(op_data->op_lso1)) {
1924 /* refill the right parent fid */
1925 lsm = &op_data->op_lso1->lso_lsm;
1926 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1927 oinfo = &lsm->lsm_md_oinfo[i];
1928 if (oinfo->lmo_mds == op_data->op_mds) {
1929 op_data->op_fid1 = oinfo->lmo_fid;
1934 if (i == lsm->lsm_md_stripe_count)
1935 op_data->op_fid1 = lsm->lsm_md_oinfo[0].lmo_fid;
1937 } else if (lmv_dir_bad_hash(op_data->op_lso1)) {
1938 lsm = &op_data->op_lso1->lso_lsm;
1940 LASSERT(op_data->op_stripe_index < lsm->lsm_md_stripe_count);
1941 oinfo = &lsm->lsm_md_oinfo[op_data->op_stripe_index];
1943 op_data->op_fid1 = oinfo->lmo_fid;
1944 op_data->op_mds = oinfo->lmo_mds;
1945 tgt = lmv_tgt(lmv, oinfo->lmo_mds);
1947 return ERR_PTR(-ENODEV);
1949 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_lso1,
1950 op_data->op_name, op_data->op_namelen,
1951 &op_data->op_fid1, &op_data->op_mds,
1952 op_data->op_new_layout);
1958 /* Locate MDT of op_data->op_fid2 for link/rename */
1959 static struct lmv_tgt_desc *
1960 lmv_locate_tgt2(struct lmv_obd *lmv, struct md_op_data *op_data)
1962 struct lmv_tgt_desc *tgt;
1965 LASSERT(op_data->op_name);
1966 if (lmv_dir_layout_changing(op_data->op_lso2)) {
1967 struct lu_fid fid1 = op_data->op_fid1;
1968 struct lmv_stripe_object *lso1 = op_data->op_lso1;
1969 struct ptlrpc_request *request = NULL;
1972 * avoid creating new file under old layout of migrating
1973 * directory, check it here.
1975 tgt = lmv_locate_tgt_by_name(lmv, op_data->op_lso2,
1976 op_data->op_name, op_data->op_namelen,
1977 &op_data->op_fid2, &op_data->op_mds, false);
1981 op_data->op_fid1 = op_data->op_fid2;
1982 op_data->op_lso1 = op_data->op_lso2;
1983 rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
1984 op_data->op_fid1 = fid1;
1985 op_data->op_lso1 = lso1;
1987 ptlrpc_req_put(request);
1988 RETURN(ERR_PTR(-EEXIST));
1992 RETURN(ERR_PTR(rc));
1995 return lmv_locate_tgt_by_name(lmv, op_data->op_lso2,
1996 op_data->op_name, op_data->op_namelen,
1997 &op_data->op_fid2, &op_data->op_mds,
2001 int lmv_old_layout_lookup(struct lmv_obd *lmv, struct md_op_data *op_data)
2003 struct lu_tgt_desc *tgt;
2004 struct ptlrpc_request *request;
2007 LASSERT(lmv_dir_layout_changing(op_data->op_lso1));
2008 LASSERT(!op_data->op_new_layout);
2010 tgt = lmv_locate_tgt(lmv, op_data);
2012 return PTR_ERR(tgt);
2014 rc = md_getattr_name(tgt->ltd_exp, op_data, &request);
2016 ptlrpc_req_put(request);
2023 /* mkdir by QoS upon 'lfs mkdir -i -1'.
2025 * NB, mkdir by QoS only if parent is not striped, this is to avoid remote
2026 * directories under striped directory.
2028 static inline bool lmv_op_user_qos_mkdir(const struct md_op_data *op_data)
2030 const struct lmv_user_md *lum = op_data->op_data;
2032 if (op_data->op_code != LUSTRE_OPC_MKDIR)
2035 if (lmv_dir_striped(op_data->op_lso1))
2038 return (op_data->op_cli_flags & CLI_SET_MEA) && lum &&
2039 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC &&
2040 le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
2043 /* mkdir by QoS if either ROOT or parent default LMV is space balanced. */
2044 static inline bool lmv_op_default_qos_mkdir(const struct md_op_data *op_data)
2046 const struct lmv_stripe_object *lso = op_data->op_default_lso1;
2048 if (op_data->op_code != LUSTRE_OPC_MKDIR)
2051 if (lmv_dir_striped(op_data->op_lso1))
2054 return (op_data->op_flags & MF_QOS_MKDIR) ||
2055 (lso && lso->lso_lsm.lsm_md_master_mdt_index ==
2056 LMV_OFFSET_DEFAULT);
2059 /* if parent default LMV is space balanced, and
2060 * 1. max_inherit_rr is set
2061 * 2. or parent is ROOT
2062 * mkdir roundrobin. Or if parent doesn't have default LMV, while ROOT default
2063 * LMV requests roundrobin mkdir, do the same.
2064 * NB, this needs to check server is balanced, which is done by caller.
2066 static inline bool lmv_op_default_rr_mkdir(const struct md_op_data *op_data)
2068 const struct lmv_stripe_object *lso = op_data->op_default_lso1;
2070 return (op_data->op_flags & MF_RR_MKDIR) ||
2071 (lso && lso->lso_lsm.lsm_md_max_inherit_rr !=
2072 LMV_INHERIT_RR_NONE) || fid_is_root(&op_data->op_fid1);
2075 /* 'lfs mkdir -i <specific_MDT>' */
2076 static inline bool lmv_op_user_specific_mkdir(const struct md_op_data *op_data)
2078 const struct lmv_user_md *lum = op_data->op_data;
2080 return op_data->op_code == LUSTRE_OPC_MKDIR &&
2081 op_data->op_cli_flags & CLI_SET_MEA && lum &&
2082 (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
2083 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) &&
2084 le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2087 /* parent default LMV master_mdt_index is not -1. */
2089 lmv_op_default_specific_mkdir(const struct md_op_data *op_data)
2091 return op_data->op_code == LUSTRE_OPC_MKDIR &&
2092 op_data->op_default_lso1 &&
2093 op_data->op_default_lso1->lso_lsm.lsm_md_master_mdt_index !=
2097 /* locate MDT by space usage */
2098 static struct lu_tgt_desc *lmv_locate_tgt_by_space(struct lmv_obd *lmv,
2099 struct md_op_data *op_data,
2100 struct lmv_tgt_desc *tgt)
2102 struct lmv_tgt_desc *tmp = tgt;
2104 tgt = lmv_locate_tgt_qos(lmv, op_data);
2105 if (tgt == ERR_PTR(-EAGAIN)) {
2106 if (ltd_qos_is_balanced(&lmv->lmv_mdt_descs) &&
2107 !lmv_op_default_rr_mkdir(op_data) &&
2108 !lmv_op_user_qos_mkdir(op_data) &&
2109 !(tmp->ltd_statfs.os_state & OS_STATFS_NOCREATE)) {
2110 /* if not necessary, don't create remote directory. */
2113 tgt = lmv_locate_tgt_rr(lmv);
2114 /* if no MDT chosen, use parent MDT */
2119 lmv_statfs_check_update(lmv2obd_dev(lmv), tgt);
2123 op_data->op_mds = tgt->ltd_index;
2125 /* If space balance was called because the original target was marked
2126 * NOCREATE, periodically check whether the state has changed.
2128 if (tmp != tgt && tmp->ltd_statfs.os_state & OS_STATFS_NOCREATE)
2129 lmv_statfs_check_update(lmv2obd_dev(lmv), tmp);
2134 static bool lmv_qos_exclude(struct lmv_obd *lmv, struct md_op_data *op_data)
2136 const char *name = op_data->op_name;
2137 size_t namelen = op_data->op_namelen;
2138 char buf[NAME_MAX + 1];
2139 struct qos_exclude_prefix *prefix;
2142 /* skip encrypted files */
2143 if (op_data->op_file_encctx)
2146 /* name length may not be validated yet */
2147 if (namelen > NAME_MAX)
2150 p = strrchr(name, '.');
2155 strncpy(buf, name, namelen);
2156 buf[namelen] = '\0';
2160 prefix = rhashtable_lookup_fast(&lmv->lmv_qos_exclude_hash, name,
2161 qos_exclude_hash_params);
2162 return prefix != NULL;
2165 struct lmv_tgt_desc *lmv_locate_tgt_create(struct obd_device *obd,
2166 struct lmv_obd *lmv,
2167 struct md_op_data *op_data)
2169 struct lmv_tgt_desc *tgt;
2173 tgt = lmv_locate_tgt(lmv, op_data);
2177 /* the order to apply policy in mkdir:
2178 * 1. is "lfs mkdir -i N"? mkdir on MDT N.
2179 * 2. is "lfs mkdir -i -1"? mkdir by space usage.
2180 * 3. is starting MDT specified in default LMV? mkdir on MDT N.
2181 * 4. is default LMV space balanced? mkdir by space usage.
2183 * If the existing parent or specific MDT selected is deactivated
2184 * with OS_STATFS_NOCREATE then select a different MDT by QOS.
2186 if (lmv_op_user_specific_mkdir(op_data)) {
2187 struct lmv_user_md *lum = op_data->op_data;
2189 op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
2190 tgt = lmv_tgt(lmv, op_data->op_mds);
2192 RETURN(ERR_PTR(-ENODEV));
2193 if (unlikely(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE))
2194 GOTO(new_tgt, -EAGAIN);
2195 } else if (lmv_op_user_qos_mkdir(op_data)) {
2196 tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
2199 } else if (lmv_op_default_specific_mkdir(op_data)) {
2200 struct lmv_stripe_md *lsm = &op_data->op_default_lso1->lso_lsm;
2202 op_data->op_mds = lsm->lsm_md_master_mdt_index;
2203 tgt = lmv_tgt(lmv, op_data->op_mds);
2205 RETURN(ERR_PTR(-ENODEV));
2206 if (unlikely(tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE))
2207 GOTO(new_tgt, -EAGAIN);
2208 } else if ((lmv_op_default_qos_mkdir(op_data) &&
2209 !lmv_qos_exclude(lmv, op_data)) ||
2210 tgt->ltd_statfs.os_state & OS_STATFS_NOCREATE) {
2212 tgt = lmv_locate_tgt_by_space(lmv, op_data, tgt);
2220 static int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
2221 const void *data, size_t datalen, umode_t mode, uid_t uid,
2222 gid_t gid, kernel_cap_t cap_effective, __u64 rdev,
2223 struct ptlrpc_request **request)
2225 struct obd_device *obd = exp->exp_obd;
2226 struct lmv_obd *lmv = &obd->u.lmv;
2227 struct lmv_tgt_desc *tgt;
2228 struct mdt_body *repbody;
2233 if (!lmv->lmv_mdt_descs.ltd_lmv_desc.ld_active_tgt_count)
2236 if (lmv_dir_bad_hash(op_data->op_lso1))
2239 if (lmv_dir_layout_changing(op_data->op_lso1)) {
2241 * if parent is migrating, create() needs to lookup existing
2242 * name in both old and new layout, check old layout on client.
2244 rc = lmv_old_layout_lookup(lmv, op_data);
2248 op_data->op_new_layout = true;
2251 tgt = lmv_locate_tgt_create(obd, lmv, op_data);
2253 RETURN(PTR_ERR(tgt));
2256 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
2260 CDEBUG(D_INODE, "CREATE name '%.*s' "DFID" on "DFID" -> mds #%x\n",
2261 (int)op_data->op_namelen, op_data->op_name,
2262 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1),
2265 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2266 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
2267 cap_effective, rdev, request);
2269 if (*request == NULL)
2271 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
2274 /* dir restripe needs to send to MDT where dir is located */
2275 if (rc != -EREMOTE ||
2276 !(exp_connect_flags2(exp) & OBD_CONNECT2_CRUSH))
2279 repbody = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2280 if (repbody == NULL)
2283 /* Not cross-ref case, just get out of here. */
2284 if (likely(!(repbody->mbo_valid & OBD_MD_MDS)))
2287 op_data->op_fid2 = repbody->mbo_fid1;
2288 ptlrpc_req_put(*request);
2291 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
2293 RETURN(PTR_ERR(tgt));
2295 op_data->op_mds = tgt->ltd_index;
2300 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
2301 const union ldlm_policy_data *policy, struct md_op_data *op_data,
2302 struct lustre_handle *lockh, __u64 extra_lock_flags)
2304 struct obd_device *obd = exp->exp_obd;
2305 struct lmv_obd *lmv = &obd->u.lmv;
2306 struct lmv_tgt_desc *tgt;
2311 CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
2313 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2315 RETURN(PTR_ERR(tgt));
2317 CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
2318 PFID(&op_data->op_fid1), tgt->ltd_index);
2320 rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
2327 lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
2328 struct ptlrpc_request **preq)
2330 struct obd_device *obd = exp->exp_obd;
2331 struct lmv_obd *lmv = &obd->u.lmv;
2332 struct lmv_tgt_desc *tgt;
2333 struct mdt_body *body;
2339 if (op_data->op_namelen == 2 &&
2340 op_data->op_name[0] == '.' && op_data->op_name[1] == '.')
2341 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2343 tgt = lmv_locate_tgt(lmv, op_data);
2345 RETURN(PTR_ERR(tgt));
2347 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
2348 (int)op_data->op_namelen, op_data->op_name,
2349 PFID(&op_data->op_fid1), tgt->ltd_index);
2351 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
2352 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2353 ptlrpc_req_put(*preq);
2361 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
2362 LASSERT(body != NULL);
2364 if (body->mbo_valid & OBD_MD_MDS) {
2365 op_data->op_fid1 = body->mbo_fid1;
2366 op_data->op_valid |= OBD_MD_FLCROSSREF;
2367 op_data->op_namelen = 0;
2368 op_data->op_name = NULL;
2370 ptlrpc_req_put(*preq);
2379 #define md_op_data_fid(op_data, fl) \
2380 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
2381 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
2382 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
2383 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
2386 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
2387 struct md_op_data *op_data, __u32 op_tgt,
2388 enum ldlm_mode mode, int bits, int flag)
2390 struct lu_fid *fid = md_op_data_fid(op_data, flag);
2391 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2392 union ldlm_policy_data policy = { { 0 } };
2397 if (!fid_is_sane(fid))
2401 tgt = lmv_fid2tgt(lmv, fid);
2403 RETURN(PTR_ERR(tgt));
2406 if (tgt->ltd_index != op_tgt) {
2407 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
2408 policy.l_inodebits.bits = bits;
2409 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
2410 mode, LCF_ASYNC, NULL);
2413 "EARLY_CANCEL skip operation target %d on "DFID"\n",
2415 op_data->op_flags |= flag;
2423 * llite passes fid of an target inode in op_data->op_fid1 and id of directory
2424 * in op_data->op_fid2
2426 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
2427 struct ptlrpc_request **request)
2429 struct obd_device *obd = exp->exp_obd;
2430 struct lmv_obd *lmv = &obd->u.lmv;
2431 struct lmv_tgt_desc *tgt;
2436 LASSERT(op_data->op_namelen != 0);
2438 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2439 PFID(&op_data->op_fid2), (int)op_data->op_namelen,
2440 op_data->op_name, PFID(&op_data->op_fid1));
2442 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2443 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2444 op_data->op_cap = current_cap();
2446 tgt = lmv_locate_tgt2(lmv, op_data);
2448 RETURN(PTR_ERR(tgt));
2450 /* Cancel UPDATE lock on child (fid1). */
2451 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2452 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
2453 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2457 rc = md_link(tgt->ltd_exp, op_data, request);
2462 /* migrate the top directory */
2463 static inline bool lmv_op_topdir_migrate(const struct md_op_data *op_data)
2465 if (!S_ISDIR(op_data->op_mode))
2468 if (lmv_dir_layout_changing(op_data->op_lso1))
2474 /* migrate top dir to specific MDTs */
2475 static inline bool lmv_topdir_specific_migrate(const struct md_op_data *op_data)
2477 const struct lmv_user_md *lum = op_data->op_data;
2479 if (!lmv_op_topdir_migrate(op_data))
2482 return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2485 /* migrate top dir in QoS mode if user issued "lfs migrate -m -1..." */
2486 static inline bool lmv_topdir_qos_migrate(const struct md_op_data *op_data)
2488 const struct lmv_user_md *lum = op_data->op_data;
2490 if (!lmv_op_topdir_migrate(op_data))
2493 return le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
2496 static inline bool lmv_subdir_specific_migrate(const struct md_op_data *op_data)
2498 const struct lmv_user_md *lum = op_data->op_data;
2500 if (!S_ISDIR(op_data->op_mode))
2503 if (!lmv_dir_layout_changing(op_data->op_lso1))
2506 return le32_to_cpu(lum->lum_stripe_offset) != LMV_OFFSET_DEFAULT;
2509 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
2510 const char *name, size_t namelen,
2511 struct ptlrpc_request **request)
2513 struct obd_device *obd = exp->exp_obd;
2514 struct lmv_obd *lmv = &obd->u.lmv;
2515 struct lmv_stripe_object *lso = op_data->op_lso1;
2516 struct lmv_tgt_desc *parent_tgt;
2517 struct lmv_tgt_desc *sp_tgt;
2518 struct lmv_tgt_desc *tp_tgt = NULL;
2519 struct lmv_tgt_desc *child_tgt;
2520 struct lmv_tgt_desc *tgt;
2521 struct lu_fid target_fid = { 0 };
2526 LASSERT(op_data->op_cli_flags & CLI_MIGRATE);
2528 CDEBUG(D_INODE, "MIGRATE "DFID"/%.*s\n",
2529 PFID(&op_data->op_fid1), (int)namelen, name);
2531 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2532 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2533 op_data->op_cap = current_cap();
2535 parent_tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2536 if (IS_ERR(parent_tgt))
2537 RETURN(PTR_ERR(parent_tgt));
2539 if (lmv_dir_striped(lso)) {
2540 const struct lmv_oinfo *oinfo;
2542 oinfo = lsm_name_to_stripe_info(lso, name, namelen, false);
2544 RETURN(PTR_ERR(oinfo));
2546 /* save source stripe FID in fid4 temporarily for ELC */
2547 op_data->op_fid4 = oinfo->lmo_fid;
2548 sp_tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
2553 * if parent is being migrated too, fill op_fid2 with target
2554 * stripe fid, otherwise the target stripe is not created yet.
2556 if (lmv_dir_layout_changing(lso)) {
2557 oinfo = lsm_name_to_stripe_info(lso, name, namelen,
2560 RETURN(PTR_ERR(oinfo));
2562 op_data->op_fid2 = oinfo->lmo_fid;
2563 tp_tgt = lmv_tgt_retry(lmv, oinfo->lmo_mds);
2567 /* parent unchanged and update namespace only */
2568 if (lu_fid_eq(&op_data->op_fid4, &op_data->op_fid2) &&
2569 op_data->op_bias & MDS_MIGRATE_NSONLY)
2573 sp_tgt = parent_tgt;
2576 child_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2577 if (IS_ERR(child_tgt))
2578 RETURN(PTR_ERR(child_tgt));
2580 if (lmv_topdir_specific_migrate(op_data)) {
2581 struct lmv_user_md *lum = op_data->op_data;
2583 op_data->op_mds = le32_to_cpu(lum->lum_stripe_offset);
2584 } else if (lmv_topdir_qos_migrate(op_data)) {
2585 tgt = lmv_locate_tgt_lf(lmv);
2586 if (tgt == ERR_PTR(-EAGAIN))
2587 tgt = lmv_locate_tgt_rr(lmv);
2589 RETURN(PTR_ERR(tgt));
2591 op_data->op_mds = tgt->ltd_index;
2592 } else if (lmv_subdir_specific_migrate(op_data)) {
2593 struct lmv_user_md *lum = op_data->op_data;
2597 if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
2598 /* adjust MDTs in lum, since subdir is located on where
2599 * its parent stripe is, not the first specified MDT.
2601 for (i = 0; i < le32_to_cpu(lum->lum_stripe_count);
2603 if (le32_to_cpu(lum->lum_objects[i].lum_mds) ==
2608 if (i == le32_to_cpu(lum->lum_stripe_count))
2611 lum->lum_objects[i].lum_mds =
2612 lum->lum_objects[0].lum_mds;
2613 lum->lum_objects[0].lum_mds =
2614 cpu_to_le32(tp_tgt->ltd_index);
2616 /* NB, the above adjusts subdir migration for command like
2617 * "lfs migrate -m 0,1,2 ...", but for migration like
2618 * "lfs migrate -m 0 -c 2 ...", the top dir is migrated to MDT0
2619 * and MDT1, however its subdir may be migrated to MDT1 and MDT2
2622 lum->lum_stripe_offset = cpu_to_le32(tp_tgt->ltd_index);
2623 op_data->op_mds = tp_tgt->ltd_index;
2624 } else if (tp_tgt) {
2625 op_data->op_mds = tp_tgt->ltd_index;
2627 op_data->op_mds = sp_tgt->ltd_index;
2630 rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
2635 * for directory, send migrate request to the MDT where the object will
2636 * be migrated to, because we can't create a striped directory remotely.
2638 * otherwise, send to the MDT where source is located because regular
2639 * file may open lease.
2641 * NB. if MDT doesn't support DIR_MIGRATE, send to source MDT too for
2642 * backward compatibility.
2644 if (S_ISDIR(op_data->op_mode) &&
2645 (exp_connect_flags2(exp) & OBD_CONNECT2_DIR_MIGRATE)) {
2646 tgt = lmv_fid2tgt(lmv, &target_fid);
2648 RETURN(PTR_ERR(tgt));
2653 /* cancel UPDATE lock of parent master object */
2654 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index, LCK_EX,
2655 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2659 /* cancel UPDATE lock of source parent */
2660 if (sp_tgt != parent_tgt) {
2662 * migrate RPC packs master object FID, because we can only pack
2663 * two FIDs in reint RPC, but MDS needs to know both source
2664 * parent and target parent, and it will obtain them from master
2665 * FID and LMV, the other FID in RPC is kept for target.
2667 * since this FID is not passed to MDC, cancel it anyway.
2669 rc = lmv_early_cancel(exp, sp_tgt, op_data, -1, LCK_EX,
2670 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID4);
2674 op_data->op_flags &= ~MF_MDC_CANCEL_FID4;
2676 op_data->op_fid4 = target_fid;
2678 /* cancel UPDATE locks of target parent */
2679 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2680 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2684 /* cancel LOOKUP lock of source if source is remote object */
2685 if (child_tgt != sp_tgt) {
2686 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index,
2687 LCK_EX, MDS_INODELOCK_LOOKUP,
2688 MF_MDC_CANCEL_FID3);
2693 /* cancel ELC locks of source */
2694 rc = lmv_early_cancel(exp, child_tgt, op_data, tgt->ltd_index, LCK_EX,
2695 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2699 rc = md_rename(tgt->ltd_exp, op_data, name, namelen, NULL, 0, request);
2704 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2705 const char *old, size_t oldlen,
2706 const char *new, size_t newlen,
2707 struct ptlrpc_request **request)
2709 struct obd_device *obd = exp->exp_obd;
2710 struct lmv_obd *lmv = &obd->u.lmv;
2711 struct lmv_tgt_desc *sp_tgt;
2712 struct lmv_tgt_desc *tp_tgt = NULL;
2713 struct lmv_tgt_desc *src_tgt = NULL;
2714 struct lmv_tgt_desc *tgt;
2715 struct mdt_body *body;
2720 LASSERT(oldlen != 0);
2722 if (op_data->op_cli_flags & CLI_MIGRATE) {
2723 rc = lmv_migrate(exp, op_data, old, oldlen, request);
2727 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2728 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2729 op_data->op_cap = current_cap();
2731 op_data->op_name = new;
2732 op_data->op_namelen = newlen;
2734 tp_tgt = lmv_locate_tgt2(lmv, op_data);
2736 RETURN(PTR_ERR(tp_tgt));
2738 /* Since the target child might be destroyed, and it might become
2739 * orphan, and we can only check orphan on the local MDT right now, so
2740 * we send rename request to the MDT where target child is located. If
2741 * target child does not exist, then it will send the request to the
2744 if (fid_is_sane(&op_data->op_fid4)) {
2745 tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2747 RETURN(PTR_ERR(tgt));
2752 op_data->op_flags |= MF_MDC_CANCEL_FID4;
2754 /* cancel UPDATE locks of target parent */
2755 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_index, LCK_EX,
2756 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2760 if (fid_is_sane(&op_data->op_fid4)) {
2761 /* cancel LOOKUP lock of target on target parent */
2762 if (tgt != tp_tgt) {
2763 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2764 tgt->ltd_index, LCK_EX,
2765 MDS_INODELOCK_LOOKUP,
2766 MF_MDC_CANCEL_FID4);
2772 if (fid_is_sane(&op_data->op_fid3)) {
2773 src_tgt = lmv_fid2tgt(lmv, &op_data->op_fid3);
2774 if (IS_ERR(src_tgt))
2775 RETURN(PTR_ERR(src_tgt));
2777 /* cancel ELC locks of source */
2778 rc = lmv_early_cancel(exp, src_tgt, op_data, tgt->ltd_index,
2779 LCK_EX, MDS_INODELOCK_ELC,
2780 MF_MDC_CANCEL_FID3);
2785 op_data->op_name = old;
2786 op_data->op_namelen = oldlen;
2788 sp_tgt = lmv_locate_tgt(lmv, op_data);
2790 RETURN(PTR_ERR(sp_tgt));
2792 /* cancel UPDATE locks of source parent */
2793 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_index, LCK_EX,
2794 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2798 if (fid_is_sane(&op_data->op_fid3)) {
2799 /* cancel LOOKUP lock of source on source parent */
2800 if (src_tgt != sp_tgt) {
2801 rc = lmv_early_cancel(exp, sp_tgt, op_data,
2802 tgt->ltd_index, LCK_EX,
2803 MDS_INODELOCK_LOOKUP,
2804 MF_MDC_CANCEL_FID3);
2811 CDEBUG(D_INODE, "RENAME "DFID"/%.*s to "DFID"/%.*s\n",
2812 PFID(&op_data->op_fid1), (int)oldlen, old,
2813 PFID(&op_data->op_fid2), (int)newlen, new);
2815 rc = md_rename(tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2817 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2818 ptlrpc_req_put(*request);
2823 if (rc && rc != -EXDEV)
2826 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2830 /* Not cross-ref case, just get out of here. */
2831 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2834 op_data->op_fid4 = body->mbo_fid1;
2836 ptlrpc_req_put(*request);
2839 tgt = lmv_fid2tgt(lmv, &op_data->op_fid4);
2841 RETURN(PTR_ERR(tgt));
2843 if (fid_is_sane(&op_data->op_fid4)) {
2844 /* cancel LOOKUP lock of target on target parent */
2845 if (tgt != tp_tgt) {
2846 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2847 tgt->ltd_index, LCK_EX,
2848 MDS_INODELOCK_LOOKUP,
2849 MF_MDC_CANCEL_FID4);
2858 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2859 void *ea, size_t ealen, struct ptlrpc_request **request)
2861 struct obd_device *obd = exp->exp_obd;
2862 struct lmv_obd *lmv = &obd->u.lmv;
2863 struct lmv_tgt_desc *tgt;
2868 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x/0x%x\n",
2869 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2870 op_data->op_xvalid);
2872 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2873 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
2875 RETURN(PTR_ERR(tgt));
2877 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2882 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2883 struct ptlrpc_request **request)
2885 struct obd_device *obd = exp->exp_obd;
2886 struct lmv_obd *lmv = &obd->u.lmv;
2887 struct lmv_tgt_desc *tgt;
2892 tgt = lmv_fid2tgt(lmv, fid);
2894 RETURN(PTR_ERR(tgt));
2896 rc = md_fsync(tgt->ltd_exp, fid, request);
2900 struct stripe_dirent {
2901 struct page *sd_page;
2902 struct lu_dirpage *sd_dp;
2903 struct lu_dirent *sd_ent;
2907 struct lmv_dir_ctxt {
2908 struct lmv_obd *ldc_lmv;
2909 struct md_op_data *ldc_op_data;
2910 struct md_readdir_info *ldc_mrinfo;
2913 struct stripe_dirent ldc_stripes[0];
2916 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
2918 if (stripe->sd_page) {
2919 kunmap(stripe->sd_page);
2920 put_page(stripe->sd_page);
2921 stripe->sd_page = NULL;
2922 stripe->sd_ent = NULL;
2926 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2930 for (i = 0; i < ctxt->ldc_count; i++)
2931 stripe_dirent_unload(&ctxt->ldc_stripes[i]);
2934 /* if @ent is dummy, or . .., get next */
2935 static struct lu_dirent *stripe_dirent_get(struct lmv_dir_ctxt *ctxt,
2936 struct lu_dirent *ent,
2939 for (; ent; ent = lu_dirent_next(ent)) {
2940 /* Skip dummy entry */
2941 if (le16_to_cpu(ent->lde_namelen) == 0)
2944 /* skip . and .. for other stripes */
2946 (strncmp(ent->lde_name, ".",
2947 le16_to_cpu(ent->lde_namelen)) == 0 ||
2948 strncmp(ent->lde_name, "..",
2949 le16_to_cpu(ent->lde_namelen)) == 0))
2952 if (le64_to_cpu(ent->lde_hash) >= ctxt->ldc_hash)
2959 static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
2960 struct stripe_dirent *stripe,
2963 struct md_op_data *op_data = ctxt->ldc_op_data;
2964 struct lmv_oinfo *oinfo;
2965 struct lu_fid fid = op_data->op_fid1;
2966 struct inode *inode = op_data->op_data;
2967 struct lmv_tgt_desc *tgt;
2968 struct lu_dirent *ent = stripe->sd_ent;
2969 __u64 hash = ctxt->ldc_hash;
2974 LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2978 if (stripe->sd_page) {
2979 __u64 end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2981 /* @hash should be the last dirent hash */
2982 LASSERTF(hash <= end,
2983 "ctxt@%px stripe@%px hash %llx end %llx\n",
2984 ctxt, stripe, hash, end);
2985 /* unload last page */
2986 stripe_dirent_unload(stripe);
2988 if (end == MDS_DIR_END_OFF) {
2989 stripe->sd_eof = true;
2995 oinfo = &op_data->op_lso1->lso_lsm.lsm_md_oinfo[stripe_index];
2996 if (!oinfo->lmo_root) {
3001 tgt = lmv_tgt_retry(ctxt->ldc_lmv, oinfo->lmo_mds);
3007 /* op_data is shared by stripes, reset after use */
3008 op_data->op_fid1 = oinfo->lmo_fid;
3009 op_data->op_fid2 = oinfo->lmo_fid;
3010 op_data->op_data = oinfo->lmo_root;
3012 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_mrinfo, hash,
3015 op_data->op_fid1 = fid;
3016 op_data->op_fid2 = fid;
3017 op_data->op_data = inode;
3022 stripe->sd_dp = page_address(stripe->sd_page);
3023 ent = stripe_dirent_get(ctxt, lu_dirent_start(stripe->sd_dp),
3025 /* in case a page filled with ., .. and dummy, read next */
3028 stripe->sd_ent = ent;
3031 /* treat error as eof, so dir can be partially accessed */
3032 stripe->sd_eof = true;
3033 ctxt->ldc_mrinfo->mr_partial_readdir_rc = rc;
3034 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, directory is partially accessed!\n",
3035 PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
3042 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
3044 struct obd_device *obd = exp->exp_obd;
3045 struct lmv_obd *lmv = &obd->u.lmv;
3046 struct lmv_tgt_desc *tgt;
3051 rc = lmv_check_connect(obd);
3055 tgt = lmv_fid2tgt(lmv, &data->op_fid1);
3057 RETURN(PTR_ERR(tgt));
3059 data->op_flags |= MF_MDC_CANCEL_FID1;
3060 rc = md_file_resync(tgt->ltd_exp, data);
3065 * Get dirent with the closest hash for striped directory
3067 * This function will search the dir entry, whose hash value is the
3068 * closest(>=) to hash from all of sub-stripes, and it is only being called
3069 * for striped directory.
3071 * \param[in] ctxt dir read context
3073 * \retval dirent get the entry successfully
3074 * NULL does not get the entry, normally it means
3075 * it reaches the end of the directory, while read
3076 * stripe dirent error is ignored to allow partial
3079 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
3081 struct stripe_dirent *stripe;
3082 struct lu_dirent *ent = NULL;
3086 /* TODO: optimize with k-way merge sort */
3087 for (i = 0; i < ctxt->ldc_count; i++) {
3088 stripe = &ctxt->ldc_stripes[i];
3092 if (!stripe->sd_ent) {
3093 stripe_dirent_load(ctxt, stripe, i);
3094 if (!stripe->sd_ent) {
3095 LASSERT(stripe->sd_eof);
3101 le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
3102 le64_to_cpu(stripe->sd_ent->lde_hash)) {
3104 if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
3111 stripe = &ctxt->ldc_stripes[min];
3112 ent = stripe->sd_ent;
3113 /* pop found dirent */
3114 stripe->sd_ent = stripe_dirent_get(ctxt, lu_dirent_next(ent),
3122 * Build dir entry page for striped directory
3124 * This function gets one entry by @offset from a striped directory. It will
3125 * read entries from all of stripes, and choose one closest to the required
3126 * offset(&offset). A few notes
3127 * 1. skip . and .. for non-zero stripes, because there can only have one .
3128 * and .. in a directory.
3129 * 2. op_data will be shared by all of stripes, instead of allocating new
3130 * one, so need to restore before reusing.
3132 * \param[in] exp obd export refer to LMV
3133 * \param[in] op_data hold those MD parameters of read_entry
3134 * \param[in] mrinfo ldlm callback being used in enqueue in mdc_read_entry,
3135 * and partial readdir result will be stored in it.
3136 * \param[in] offset starting hash offset
3137 * \param[out] ppage the page holding the entry. Note: because the entry
3138 * will be accessed in upper layer, so we need hold the
3139 * page until the usages of entry is finished, see
3140 * ll_dir_entry_next.
3142 * retval =0 if get entry successfully
3143 * <0 cannot get entry
3145 static int lmv_striped_read_page(struct obd_export *exp,
3146 struct md_op_data *op_data,
3147 struct md_readdir_info *mrinfo, __u64 offset,
3148 struct page **ppage)
3150 struct page *page = NULL;
3151 struct lu_dirpage *dp;
3153 struct lu_dirent *ent;
3154 struct lu_dirent *last_ent;
3156 struct lmv_dir_ctxt *ctxt;
3157 struct lu_dirent *next = NULL;
3164 /* Allocate a page and read entries from all of stripes and fill
3165 * the page by hash order
3167 page = alloc_page(GFP_KERNEL);
3171 /* Initialize the entry page */
3173 memset(dp, 0, sizeof(*dp));
3174 dp->ldp_hash_start = cpu_to_le64(offset);
3177 left_bytes = PAGE_SIZE - sizeof(*dp);
3181 /* initalize dir read context */
3182 stripe_count = op_data->op_lso1->lso_lsm.lsm_md_stripe_count;
3183 OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
3185 GOTO(free_page, rc = -ENOMEM);
3186 ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
3187 ctxt->ldc_op_data = op_data;
3188 ctxt->ldc_mrinfo = mrinfo;
3189 ctxt->ldc_hash = offset;
3190 ctxt->ldc_count = stripe_count;
3193 next = lmv_dirent_next(ctxt);
3195 /* end of directory */
3197 ctxt->ldc_hash = MDS_DIR_END_OFF;
3200 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
3202 ent_size = le16_to_cpu(next->lde_reclen);
3204 /* the last entry lde_reclen is 0, but it might not be the last
3205 * one of this temporay dir page
3208 ent_size = lu_dirent_calc_size(
3209 le16_to_cpu(next->lde_namelen),
3210 le32_to_cpu(next->lde_attrs));
3212 if (ent_size > left_bytes)
3215 memcpy(ent, next, ent_size);
3217 /* Replace . with master FID and Replace .. with the parent FID
3220 if (strncmp(ent->lde_name, ".",
3221 le16_to_cpu(ent->lde_namelen)) == 0 &&
3222 le16_to_cpu(ent->lde_namelen) == 1)
3223 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
3224 else if (strncmp(ent->lde_name, "..",
3225 le16_to_cpu(ent->lde_namelen)) == 0 &&
3226 le16_to_cpu(ent->lde_namelen) == 2)
3227 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
3229 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
3230 le16_to_cpu(ent->lde_namelen), ent->lde_name,
3231 le64_to_cpu(ent->lde_hash));
3233 left_bytes -= ent_size;
3234 ent->lde_reclen = cpu_to_le16(ent_size);
3236 ent = (void *)ent + ent_size;
3239 last_ent->lde_reclen = 0;
3242 dp->ldp_flags |= LDF_EMPTY;
3243 else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
3244 dp->ldp_flags |= LDF_COLLIDE;
3245 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
3246 dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
3248 put_lmv_dir_ctxt(ctxt);
3249 OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
3262 static int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
3263 struct md_readdir_info *mrinfo, __u64 offset,
3264 struct page **ppage)
3266 struct obd_device *obd = exp->exp_obd;
3267 struct lmv_obd *lmv = &obd->u.lmv;
3268 struct lmv_tgt_desc *tgt;
3273 if (unlikely(lmv_dir_foreign(op_data->op_lso1)))
3276 if (unlikely(lmv_dir_striped(op_data->op_lso1))) {
3277 rc = lmv_striped_read_page(exp, op_data, mrinfo, offset, ppage);
3281 tgt = lmv_fid2tgt(lmv, &op_data->op_fid1);
3283 RETURN(PTR_ERR(tgt));
3285 rc = md_read_page(tgt->ltd_exp, op_data, mrinfo, offset, ppage);
3291 * Unlink a file/directory
3293 * Unlink a file or directory under the parent dir. The unlink request
3294 * usually will be sent to the MDT where the child is located, but if
3295 * the client does not have the child FID then request will be sent to the
3296 * MDT where the parent is located.
3298 * If the parent is a striped directory then it also needs to locate which
3299 * stripe the name of the child is located, and replace the parent FID
3300 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
3301 * it will walk through all of sub-stripes until the child is being
3304 * \param[in] exp export refer to LMV
3305 * \param[in] op_data different parameters transferred beween client
3306 * MD stacks, name, namelen, FIDs etc.
3307 * op_fid1 is the parent FID, op_fid2 is the child
3309 * \param[out] request point to the request of unlink.
3311 * retval 0 if succeed
3312 * negative errno if failed.
3314 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
3315 struct ptlrpc_request **request)
3317 struct obd_device *obd = exp->exp_obd;
3318 struct lmv_obd *lmv = &obd->u.lmv;
3319 struct lmv_tgt_desc *tgt;
3320 struct lmv_tgt_desc *parent_tgt;
3321 struct mdt_body *body;
3326 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
3327 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
3328 op_data->op_cap = current_cap();
3331 parent_tgt = lmv_locate_tgt(lmv, op_data);
3332 if (IS_ERR(parent_tgt))
3333 RETURN(PTR_ERR(parent_tgt));
3335 if (likely(!fid_is_zero(&op_data->op_fid2))) {
3336 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
3338 RETURN(PTR_ERR(tgt));
3344 * If child's fid is given, cancel unused locks for it if it is from
3345 * another export than parent.
3347 * LOOKUP lock for child (fid3) should also be cancelled on parent
3348 * tgt_tgt in mdc_unlink().
3350 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
3352 if (parent_tgt != tgt)
3353 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_index,
3354 LCK_EX, MDS_INODELOCK_LOOKUP,
3355 MF_MDC_CANCEL_FID3);
3357 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_index, LCK_EX,
3358 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
3362 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
3363 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2),
3366 rc = md_unlink(tgt->ltd_exp, op_data, request);
3367 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
3368 ptlrpc_req_put(*request);
3376 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
3380 /* Not cross-ref case, just get out of here. */
3381 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
3384 /* This is a remote object, try remote MDT. */
3385 op_data->op_fid2 = body->mbo_fid1;
3386 ptlrpc_req_put(*request);
3389 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
3391 RETURN(PTR_ERR(tgt));
3396 static int lmv_precleanup(struct obd_device *obd)
3399 libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
3404 * Get by key a value associated with a LMV device.
3406 * Dispatch request to lower-layer devices as needed.
3408 * \param[in] env execution environment for this thread
3409 * \param[in] exp export for the LMV device
3410 * \param[in] keylen length of key identifier
3411 * \param[in] key identifier of key to get value for
3412 * \param[in] vallen size of \a val
3413 * \param[out] val pointer to storage location for value
3414 * \param[in] lsm optional striping metadata of object
3416 * \retval 0 on success
3417 * \retval negative negated errno on failure
3419 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
3420 __u32 keylen, void *key, __u32 *vallen, void *val)
3422 struct obd_device *obd;
3423 struct lmv_obd *lmv;
3424 struct lu_tgt_desc *tgt;
3429 obd = class_exp2obd(exp);
3431 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
3432 exp->exp_handle.h_cookie);
3437 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
3438 LASSERT(*vallen == sizeof(__u32));
3439 lmv_foreach_connected_tgt(lmv, tgt) {
3440 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
3445 } else if (KEY_IS(KEY_MAX_EASIZE) ||
3446 KEY_IS(KEY_DEFAULT_EASIZE) ||
3447 KEY_IS(KEY_CONN_DATA)) {
3449 * Forwarding this request to first MDS, it should know LOV
3452 tgt = lmv_tgt(lmv, 0);
3456 rc = obd_get_info(env, tgt->ltd_exp, keylen, key, vallen, val);
3457 if (!rc && KEY_IS(KEY_CONN_DATA))
3458 exp->exp_connect_data = *(struct obd_connect_data *)val;
3460 } else if (KEY_IS(KEY_TGT_COUNT)) {
3461 *((int *)val) = lmv->lmv_mdt_descs.ltd_tgts_size;
3465 CDEBUG(D_IOCTL, "Invalid key\n");
3469 static int lmv_rmfid(struct obd_export *exp, struct fid_array *fa,
3470 int *__rcs, struct ptlrpc_request_set *_set)
3472 struct obd_device *obd = class_exp2obd(exp);
3473 struct ptlrpc_request_set *set = _set;
3474 struct lmv_obd *lmv = &obd->u.lmv;
3475 int tgt_count = lmv->lmv_mdt_count;
3476 struct lu_tgt_desc *tgt;
3477 struct fid_array *fat, **fas = NULL;
3478 int i, rc, **rcs = NULL;
3481 set = ptlrpc_prep_set();
3486 /* split FIDs by targets */
3487 OBD_ALLOC_PTR_ARRAY(fas, tgt_count);
3489 GOTO(out, rc = -ENOMEM);
3490 OBD_ALLOC_PTR_ARRAY(rcs, tgt_count);
3492 GOTO(out_fas, rc = -ENOMEM);
3494 for (i = 0; i < fa->fa_nr; i++) {
3497 rc = lmv_fld_lookup(lmv, &fa->fa_fids[i], &idx);
3499 CDEBUG(D_OTHER, "can't lookup "DFID": rc = %d\n",
3500 PFID(&fa->fa_fids[i]), rc);
3503 LASSERT(idx < tgt_count);
3505 OBD_ALLOC(fas[idx], offsetof(struct fid_array,
3506 fa_fids[fa->fa_nr]));
3508 GOTO(out, rc = -ENOMEM);
3510 OBD_ALLOC_PTR_ARRAY(rcs[idx], fa->fa_nr);
3512 GOTO(out, rc = -ENOMEM);
3515 fat->fa_fids[fat->fa_nr++] = fa->fa_fids[i];
3518 lmv_foreach_connected_tgt(lmv, tgt) {
3519 fat = fas[tgt->ltd_index];
3520 if (!fat || fat->fa_nr == 0)
3522 rc = md_rmfid(tgt->ltd_exp, fat, rcs[tgt->ltd_index], set);
3525 rc = ptlrpc_set_wait(NULL, set);
3529 for (i = 0; i < tgt_count; i++) {
3531 if (!fat || fat->fa_nr == 0)
3533 /* copy FIDs back */
3534 memcpy(fa->fa_fids + j, fat->fa_fids,
3535 fat->fa_nr * sizeof(struct lu_fid));
3537 memcpy(__rcs + j, rcs[i], fat->fa_nr * sizeof(**rcs));
3542 ptlrpc_set_destroy(set);
3545 for (i = 0; i < tgt_count; i++) {
3547 OBD_FREE(fas[i], offsetof(struct fid_array,
3548 fa_fids[fa->fa_nr]));
3550 OBD_FREE_PTR_ARRAY(rcs[i], fa->fa_nr);
3553 OBD_FREE_PTR_ARRAY(rcs, tgt_count);
3556 OBD_FREE_PTR_ARRAY(fas, tgt_count);
3562 * Asynchronously set by key a value associated with a LMV device.
3564 * Dispatch request to lower-layer devices as needed.
3566 * \param[in] env execution environment for this thread
3567 * \param[in] exp export for the LMV device
3568 * \param[in] keylen length of key identifier
3569 * \param[in] key identifier of key to store value for
3570 * \param[in] vallen size of value to store
3571 * \param[in] val pointer to data to be stored
3572 * \param[in] set optional list of related ptlrpc requests
3574 * \retval 0 on success
3575 * \retval negative negated errno on failure
3577 static int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
3578 __u32 keylen, void *key, __u32 vallen, void *val,
3579 struct ptlrpc_request_set *set)
3581 struct lmv_tgt_desc *tgt;
3582 struct obd_device *obd;
3583 struct lmv_obd *lmv;
3588 obd = class_exp2obd(exp);
3590 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
3591 exp->exp_handle.h_cookie);
3596 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
3597 KEY_IS(KEY_DEFAULT_EASIZE)) {
3600 lmv_foreach_connected_tgt(lmv, tgt) {
3601 err = obd_set_info_async(env, tgt->ltd_exp,
3602 keylen, key, vallen, val, set);
3613 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
3614 const struct lmv_mds_md_v1 *lmm1)
3616 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3624 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
3625 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3626 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
3627 if (CFS_FAIL_CHECK(OBD_FAIL_LMV_UNKNOWN_STRIPE))
3628 lsm->lsm_md_hash_type = cfs_fail_val ?: LMV_HASH_TYPE_UNKNOWN;
3630 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
3631 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
3632 lsm->lsm_md_migrate_offset = le32_to_cpu(lmm1->lmv_migrate_offset);
3633 lsm->lsm_md_migrate_hash = le32_to_cpu(lmm1->lmv_migrate_hash);
3634 cplen = strscpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
3635 sizeof(lsm->lsm_md_pool_name));
3640 CDEBUG(D_INFO, "unpack lsm count %d/%d, master %d hash_type %#x/%#x layout_version %d\n",
3641 lsm->lsm_md_stripe_count,
3642 lsm->lsm_md_migrate_offset, lsm->lsm_md_master_mdt_index,
3643 lsm->lsm_md_hash_type, lsm->lsm_md_migrate_hash,
3644 lsm->lsm_md_layout_version);
3646 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3647 for (i = 0; i < stripe_count; i++) {
3648 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
3649 &lmm1->lmv_stripe_fids[i]);
3651 * set default value -1, so lmv_locate_tgt() knows this stripe
3652 * target is not initialized.
3654 lsm->lsm_md_oinfo[i].lmo_mds = LMV_OFFSET_DEFAULT;
3655 if (!fid_is_sane(&lsm->lsm_md_oinfo[i].lmo_fid))
3658 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
3659 &lsm->lsm_md_oinfo[i].lmo_mds);
3666 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
3667 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
3673 static inline int lmv_unpack_user_md(struct obd_export *exp,
3674 struct lmv_stripe_md *lsm,
3675 const struct lmv_user_md *lmu)
3677 lsm->lsm_md_magic = le32_to_cpu(lmu->lum_magic);
3678 lsm->lsm_md_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
3679 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmu->lum_stripe_offset);
3680 lsm->lsm_md_hash_type = le32_to_cpu(lmu->lum_hash_type);
3681 lsm->lsm_md_max_inherit = lmu->lum_max_inherit;
3682 lsm->lsm_md_max_inherit_rr = lmu->lum_max_inherit_rr;
3683 lsm->lsm_md_pool_name[LOV_MAXPOOLNAME] = 0;
3688 struct lmv_stripe_object *lmv_stripe_object_alloc(__u32 magic,
3689 const union lmv_mds_md *lmm,
3692 struct lmv_stripe_object *lsm_obj;
3695 if (magic == LMV_MAGIC_FOREIGN) {
3696 struct lmv_foreign_md *lfm;
3698 size = offsetof(typeof(*lfm), lfm_value[0]);
3699 if (lmm_size < size)
3700 RETURN(ERR_PTR(-EPROTO));
3702 size += le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3703 if (lmm_size < size)
3704 RETURN(ERR_PTR(-EPROTO));
3706 OBD_ALLOC_LARGE(lsm_obj, lmm_size +
3707 offsetof(typeof(*lsm_obj), lso_lfm));
3709 if (magic == LMV_MAGIC_V1) {
3712 size = offsetof(struct lmv_mds_md_v1,
3713 lmv_stripe_fids[0]);
3714 if (lmm_size < size)
3715 RETURN(ERR_PTR(-EPROTO));
3717 count = lmv_mds_md_stripe_count_get(lmm);
3718 size += count * sizeof(struct lu_fid);
3719 if (lmm_size < size)
3720 RETURN(ERR_PTR(-EPROTO));
3722 size = lmv_stripe_md_size(count);
3724 if (lmm && lmm_size < sizeof(struct lmv_user_md))
3725 RETURN(ERR_PTR(-EPROTO));
3728 * Unpack default dirstripe(lmv_user_md) to
3729 * lmv_stripe_md, stripecount should be 0 then.
3731 size = lmv_stripe_md_size(0);
3733 size += offsetof(typeof(*lsm_obj), lso_lsm);
3734 OBD_ALLOC(lsm_obj, size);
3738 atomic_set(&lsm_obj->lso_refs, 1);
3742 RETURN(ERR_PTR(-ENOMEM));
3744 EXPORT_SYMBOL(lmv_stripe_object_alloc);
3746 static int lmv_stripe_object_create(struct obd_export *exp,
3747 struct lmv_stripe_object **lsop,
3748 const union lmv_mds_md *lmm,
3751 struct lmv_stripe_object *lsm_obj;
3757 LASSERT(lsop != NULL && *lsop == NULL);
3762 magic = le32_to_cpu(lmm->lmv_magic);
3763 if (magic == LMV_MAGIC_STRIPE)
3766 if (magic != LMV_MAGIC_V1 && magic != LMV_USER_MAGIC &&
3767 magic != LMV_MAGIC_FOREIGN) {
3768 CERROR("%s: invalid lmv magic %x: rc = %d\n",
3769 exp->exp_obd->obd_name, magic, -EIO);
3773 /* foreign lmv case */
3774 if (magic == LMV_MAGIC_FOREIGN) {
3775 struct lmv_foreign_md *lfm;
3777 lsm_obj = lmv_stripe_object_alloc(magic, lmm, lmm_size);
3778 if (IS_ERR(lsm_obj))
3779 RETURN(PTR_ERR(lsm_obj));
3782 lfm = &lsm_obj->lso_lfm;
3783 lfm->lfm_magic = magic;
3784 lfm->lfm_length = le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3785 lfm->lfm_type = le32_to_cpu(lmm->lmv_foreign_md.lfm_type);
3786 lfm->lfm_flags = le32_to_cpu(lmm->lmv_foreign_md.lfm_flags);
3787 memcpy(&lfm->lfm_value, &lmm->lmv_foreign_md.lfm_value,
3793 lsm_obj = lmv_stripe_object_alloc(magic, lmm, lmm_size);
3794 if (IS_ERR(lsm_obj))
3795 RETURN(PTR_ERR(lsm_obj));
3799 rc = lmv_unpack_md_v1(exp, &lsm_obj->lso_lsm, &lmm->lmv_md_v1);
3801 case LMV_USER_MAGIC:
3802 rc = lmv_unpack_user_md(exp, &lsm_obj->lso_lsm,
3806 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3813 lmv_stripe_object_put(&lsm_obj);
3819 struct lmv_stripe_object *
3820 lmv_stripe_object_get(struct lmv_stripe_object *lsm_obj)
3822 if (lsm_obj == NULL)
3825 atomic_inc(&lsm_obj->lso_refs);
3826 CDEBUG(D_INODE, "get %p %u\n", lsm_obj,
3827 atomic_read(&lsm_obj->lso_refs));
3830 EXPORT_SYMBOL(lmv_stripe_object_get);
3832 void lmv_stripe_object_put(struct lmv_stripe_object **lsop)
3834 struct lmv_stripe_object *lsm_obj;
3838 LASSERT(lsop != NULL);
3841 if (lsm_obj == NULL)
3845 CDEBUG(D_INODE, "put %p %u\n", lsm_obj,
3846 atomic_read(&lsm_obj->lso_refs) - 1);
3848 if (!atomic_dec_and_test(&lsm_obj->lso_refs))
3851 if (lmv_dir_foreign(lsm_obj)) {
3852 size = lsm_obj->lso_lfm.lfm_length +
3853 offsetof(typeof(lsm_obj->lso_lfm), lfm_value[0]) +
3854 offsetof(typeof(*lsm_obj), lso_lsm);
3855 OBD_FREE_LARGE(lsm_obj, size);
3859 if (lmv_dir_striped(lsm_obj)) {
3860 struct lmv_stripe_md *lsm = &lsm_obj->lso_lsm;
3862 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
3863 iput(lsm->lsm_md_oinfo[i].lmo_root);
3864 size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
3866 size = lmv_stripe_md_size(0);
3868 OBD_FREE(lsm_obj, size + offsetof(typeof(*lsm_obj), lso_lsm));
3870 EXPORT_SYMBOL(lmv_stripe_object_put);
3872 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3873 union ldlm_policy_data *policy,
3874 enum ldlm_mode mode, enum ldlm_cancel_flags flags,
3877 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3878 struct lu_tgt_desc *tgt;
3884 LASSERT(fid != NULL);
3886 lmv_foreach_connected_tgt(lmv, tgt) {
3887 if (!tgt->ltd_active)
3890 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3898 static int lmv_set_lock_data(struct obd_export *exp,
3899 const struct lustre_handle *lockh,
3900 void *data, __u64 *bits)
3902 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3903 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3908 if (tgt == NULL || tgt->ltd_exp == NULL)
3910 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3914 static enum ldlm_mode
3915 lmv_lock_match(struct obd_export *exp, __u64 flags,
3916 const struct lu_fid *fid, enum ldlm_type type,
3917 union ldlm_policy_data *policy,
3918 enum ldlm_mode mode, struct lustre_handle *lockh)
3920 struct obd_device *obd = exp->exp_obd;
3921 struct lmv_obd *lmv = &obd->u.lmv;
3922 struct lu_tgt_desc *tgt;
3923 __u64 bits = policy->l_inodebits.bits;
3924 enum ldlm_mode rc = LCK_MINMODE;
3928 /* only one bit is set */
3929 LASSERT(bits && !(bits & (bits - 1)));
3930 /* With DNE every object can have two locks in different namespaces:
3931 * lookup lock in space of MDT storing direntry and update/open lock in
3932 * space of MDT storing inode. Try the MDT that the FID maps to first,
3933 * since this can be easily found, and only try others if that fails.
3935 if (bits == MDS_INODELOCK_LOOKUP) {
3936 for (i = 0, index = lmv_fid2tgt_index(lmv, fid);
3937 i < lmv->lmv_mdt_descs.ltd_tgts_size; i++,
3938 index = (index + 1) % lmv->lmv_mdt_descs.ltd_tgts_size) {
3941 "%s: "DFID" is inaccessible: rc = %d\n",
3942 obd->obd_name, PFID(fid), index);
3945 tgt = lmv_tgt(lmv, index);
3946 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
3948 rc = md_lock_match(tgt->ltd_exp, flags, fid, type,
3949 policy, mode, lockh);
3954 tgt = lmv_fid2tgt(lmv, fid);
3955 if (!IS_ERR(tgt) && tgt->ltd_exp && tgt->ltd_active)
3956 rc = md_lock_match(tgt->ltd_exp, flags, fid, type,
3957 policy, mode, lockh);
3960 CDEBUG(D_INODE, "Lock match for "DFID": %d\n", PFID(fid), rc);
3966 lmv_get_lustre_md(struct obd_export *exp, struct req_capsule *pill,
3967 struct obd_export *dt_exp, struct obd_export *md_exp,
3968 struct lustre_md *md)
3970 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3971 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3973 if (!tgt || !tgt->ltd_exp)
3976 return md_get_lustre_md(tgt->ltd_exp, pill, dt_exp, md_exp, md);
3979 static int lmv_put_lustre_md(struct obd_export *exp, struct lustre_md *md)
3981 struct obd_device *obd = exp->exp_obd;
3982 struct lmv_obd *lmv = &obd->u.lmv;
3983 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
3987 lmv_stripe_object_put(&md->def_lsm_obj);
3988 lmv_stripe_object_put(&md->lsm_obj);
3990 if (!tgt || !tgt->ltd_exp)
3995 static int lmv_set_open_replay_data(struct obd_export *exp,
3996 struct obd_client_handle *och,
3997 struct lookup_intent *it)
3999 struct obd_device *obd = exp->exp_obd;
4000 struct lmv_obd *lmv = &obd->u.lmv;
4001 struct lmv_tgt_desc *tgt;
4005 tgt = lmv_fid2tgt(lmv, &och->och_fid);
4007 RETURN(PTR_ERR(tgt));
4009 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
4012 static int lmv_clear_open_replay_data(struct obd_export *exp,
4013 struct obd_client_handle *och)
4015 struct obd_device *obd = exp->exp_obd;
4016 struct lmv_obd *lmv = &obd->u.lmv;
4017 struct lmv_tgt_desc *tgt;
4021 tgt = lmv_fid2tgt(lmv, &och->och_fid);
4023 RETURN(PTR_ERR(tgt));
4025 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
4028 static int lmv_intent_getattr_async(struct obd_export *exp,
4029 struct md_op_item *item)
4031 struct md_op_data *op_data = &item->mop_data;
4032 struct obd_device *obd = exp->exp_obd;
4033 struct lmv_obd *lmv = &obd->u.lmv;
4034 struct lmv_tgt_desc *ptgt;
4035 struct lmv_tgt_desc *ctgt;
4040 if (!(fid_is_sane(&op_data->op_fid2) ||
4041 fid_is_zero(&op_data->op_fid2)))
4044 ptgt = lmv_locate_tgt(lmv, op_data);
4046 RETURN(PTR_ERR(ptgt));
4049 * Zeroed FID @op_fid2 means that the intent getattr() comes from
4050 * statahead by regularized file names. Currently only do statahead
4051 * for the children files located same as the parent directory.
4053 if (!fid_is_zero(&op_data->op_fid2)) {
4054 ctgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
4056 RETURN(PTR_ERR(ctgt));
4059 * remote object needs two RPCs to lookup and getattr,
4060 * considering the complexity don't support statahead for now.
4066 rc = md_intent_getattr_async(ptgt->ltd_exp, item);
4071 static int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
4072 struct lu_fid *fid, __u64 *bits)
4074 struct obd_device *obd = exp->exp_obd;
4075 struct lmv_obd *lmv = &obd->u.lmv;
4076 struct lmv_tgt_desc *tgt;
4081 tgt = lmv_fid2tgt(lmv, fid);
4083 RETURN(PTR_ERR(tgt));
4085 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
4089 static int lmv_get_fid_from_lsm(struct obd_export *exp,
4090 const struct lmv_stripe_object *lso,
4091 const char *name, int namelen,
4094 const struct lmv_oinfo *oinfo;
4096 LASSERT(lmv_dir_striped(lso));
4097 oinfo = lsm_name_to_stripe_info(lso, name, namelen, false);
4099 return PTR_ERR(oinfo);
4101 *fid = oinfo->lmo_fid;
4107 * For lmv, only need to send request to master MDT, and the master MDT will
4108 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
4109 * we directly fetch data from the slave MDTs.
4111 static int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
4112 struct obd_quotactl *oqctl)
4114 struct obd_device *obd = class_exp2obd(exp);
4115 struct lmv_obd *lmv = &obd->u.lmv;
4116 struct lmv_tgt_desc *tgt = lmv_tgt(lmv, 0);
4117 __u64 curspace, curinodes;
4122 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active) {
4123 CERROR("master lmv inactive\n");
4127 if (oqctl->qc_cmd == LUSTRE_Q_ITERQUOTA ||
4128 oqctl->qc_cmd == LUSTRE_Q_ITEROQUOTA) {
4129 struct list_head *lst = (struct list_head *)oqctl->qc_iter_list;
4132 if (oqctl->qc_cmd == LUSTRE_Q_ITERQUOTA)
4133 RETURN(obd_quota_iter(tgt->ltd_exp, oqctl, lst));
4135 lmv_foreach_connected_tgt(lmv, tgt) {
4136 if (!tgt->ltd_active)
4139 err = obd_quota_iter(tgt->ltd_exp, oqctl, lst);
4141 CERROR("%s: getquota failed mdt %d: rc = %d\n",
4142 obd->obd_name, tgt->ltd_index, err);
4151 if (oqctl->qc_cmd != Q_GETOQUOTA) {
4152 rc = obd_quotactl(tgt->ltd_exp, oqctl);
4156 curspace = curinodes = 0;
4157 lmv_foreach_connected_tgt(lmv, tgt) {
4160 if (!tgt->ltd_active)
4163 err = obd_quotactl(tgt->ltd_exp, oqctl);
4165 CERROR("getquota on mdt %d failed. %d\n",
4166 tgt->ltd_index, err);
4170 curspace += oqctl->qc_dqblk.dqb_curspace;
4171 curinodes += oqctl->qc_dqblk.dqb_curinodes;
4174 oqctl->qc_dqblk.dqb_curspace = curspace;
4175 oqctl->qc_dqblk.dqb_curinodes = curinodes;
4180 static int lmv_merge_attr(struct obd_export *exp,
4181 const struct lmv_stripe_object *lso,
4182 struct cl_attr *attr,
4183 ldlm_blocking_callback cb_blocking)
4185 const struct lmv_stripe_md *lsm = &lso->lso_lsm;
4189 if (!lmv_dir_striped(lso))
4192 rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
4196 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
4197 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
4203 "" DFID " size %llu, blocks %llu nlink %u, atime %lld ctime %lld, mtime %lld.\n",
4204 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
4205 i_size_read(inode), (unsigned long long)inode->i_blocks,
4206 inode->i_nlink, (s64)inode_get_atime_sec(inode),
4207 (s64)inode_get_ctime_sec(inode),
4208 (s64)inode_get_mtime_sec(inode));
4210 /* for slave stripe, it needs to subtract nlink for . and .. */
4212 attr->cat_nlink += inode->i_nlink - 2;
4214 attr->cat_nlink = inode->i_nlink;
4216 attr->cat_size += i_size_read(inode);
4217 attr->cat_blocks += inode->i_blocks;
4219 if (attr->cat_atime < inode_get_atime_sec(inode))
4220 attr->cat_atime = inode_get_atime_sec(inode);
4222 if (attr->cat_ctime < inode_get_ctime_sec(inode))
4223 attr->cat_ctime = inode_get_ctime_sec(inode);
4225 if (attr->cat_mtime < inode_get_mtime_sec(inode))
4226 attr->cat_mtime = inode_get_mtime_sec(inode);
4231 static struct lu_batch *lmv_batch_create(struct obd_export *exp,
4232 enum lu_batch_flags flags,
4235 struct lu_batch *bh;
4236 struct lmv_batch *lbh;
4241 RETURN(ERR_PTR(-ENOMEM));
4243 bh = &lbh->lbh_super;
4244 bh->lbt_flags = flags;
4245 bh->lbt_max_count = max_count;
4247 if (flags & BATCH_FL_RQSET) {
4248 bh->lbt_rqset = ptlrpc_prep_set();
4249 if (bh->lbt_rqset == NULL) {
4251 RETURN(ERR_PTR(-ENOMEM));
4255 INIT_LIST_HEAD(&lbh->lbh_sub_batch_list);
4259 static int lmv_batch_stop(struct obd_export *exp, struct lu_batch *bh)
4261 struct lmv_batch *lbh;
4262 struct lmvsub_batch *sub;
4263 struct lmvsub_batch *tmp;
4268 lbh = container_of(bh, struct lmv_batch, lbh_super);
4269 list_for_each_entry_safe(sub, tmp, &lbh->lbh_sub_batch_list,
4271 list_del(&sub->sbh_sub_item);
4272 rc = md_batch_stop(sub->sbh_tgt->ltd_exp, sub->sbh_sub);
4274 CERROR("%s: stop batch processing failed: rc = %d\n",
4275 exp->exp_obd->obd_name, rc);
4276 if (bh->lbt_result == 0)
4277 bh->lbt_result = rc;
4282 if (bh->lbt_flags & BATCH_FL_RQSET) {
4283 rc = ptlrpc_set_wait(NULL, bh->lbt_rqset);
4284 ptlrpc_set_destroy(bh->lbt_rqset);
4291 static int lmv_batch_flush(struct obd_export *exp, struct lu_batch *bh,
4294 struct lmv_batch *lbh;
4295 struct lmvsub_batch *sub;
4301 lbh = container_of(bh, struct lmv_batch, lbh_super);
4302 list_for_each_entry(sub, &lbh->lbh_sub_batch_list, sbh_sub_item) {
4303 rc1 = md_batch_flush(sub->sbh_tgt->ltd_exp, sub->sbh_sub, wait);
4305 CERROR("%s: stop batch processing failed: rc = %d\n",
4306 exp->exp_obd->obd_name, rc);
4307 if (bh->lbt_result == 0)
4308 bh->lbt_result = rc;
4315 if (wait && bh->lbt_flags & BATCH_FL_RQSET) {
4316 rc1 = ptlrpc_set_wait(NULL, bh->lbt_rqset);
4324 static inline struct lmv_tgt_desc *
4325 lmv_batch_locate_tgt(struct lmv_obd *lmv, struct md_op_item *item)
4327 struct md_op_data *op_data = &item->mop_data;
4328 struct lmv_tgt_desc *tgt;
4330 switch (item->mop_opc) {
4331 case MD_OP_GETATTR: {
4332 struct lmv_tgt_desc *ptgt;
4334 if (!(fid_is_sane(&op_data->op_fid2) ||
4335 fid_is_zero(&op_data->op_fid2)))
4336 RETURN(ERR_PTR(-EINVAL));
4338 ptgt = lmv_locate_tgt(lmv, op_data);
4343 * Zeroed @op_fid2 means that it is a statahead populating call
4344 * in the file name pattern which is using file name format to
4345 * prefetch the attributes. Thus it has no idea about the FID of
4346 * the children file. The children file is considered to be
4347 * located on the same storage target with the parent directory
4348 * or the stripped directory.
4350 if (fid_is_zero(&op_data->op_fid2)) {
4355 tgt = lmv_fid2tgt(lmv, &op_data->op_fid2);
4360 * Remote object needs two RPCs to lookup and getattr,
4361 * considering the complexity don't support statahead for now.
4364 RETURN(ERR_PTR(-EREMOTE));
4369 tgt = ERR_PTR(-ENOTSUPP);
4375 static struct lu_batch *lmv_batch_lookup_sub(struct lmv_batch *lbh,
4376 struct lmv_tgt_desc *tgt)
4378 struct lmvsub_batch *sub;
4380 list_for_each_entry(sub, &lbh->lbh_sub_batch_list, sbh_sub_item) {
4381 if (sub->sbh_tgt == tgt)
4382 return sub->sbh_sub;
4388 static struct lu_batch *lmv_batch_get_sub(struct lmv_batch *lbh,
4389 struct lmv_tgt_desc *tgt)
4391 struct lmvsub_batch *sbh;
4392 struct lu_batch *child_bh;
4393 struct lu_batch *bh;
4397 child_bh = lmv_batch_lookup_sub(lbh, tgt);
4398 if (child_bh != NULL)
4403 RETURN(ERR_PTR(-ENOMEM));
4405 INIT_LIST_HEAD(&sbh->sbh_sub_item);
4408 bh = &lbh->lbh_super;
4409 child_bh = md_batch_create(tgt->ltd_exp, bh->lbt_flags,
4411 if (IS_ERR(child_bh)) {
4416 child_bh->lbt_rqset = bh->lbt_rqset;
4417 sbh->sbh_sub = child_bh;
4418 list_add(&sbh->sbh_sub_item, &lbh->lbh_sub_batch_list);
4422 static int lmv_batch_add(struct obd_export *exp, struct lu_batch *bh,
4423 struct md_op_item *item)
4425 struct obd_device *obd = exp->exp_obd;
4426 struct lmv_obd *lmv = &obd->u.lmv;
4427 struct lmv_tgt_desc *tgt;
4428 struct lmv_batch *lbh;
4429 struct lu_batch *child_bh;
4434 tgt = lmv_batch_locate_tgt(lmv, item);
4436 RETURN(PTR_ERR(tgt));
4438 lbh = container_of(bh, struct lmv_batch, lbh_super);
4439 child_bh = lmv_batch_get_sub(lbh, tgt);
4440 if (IS_ERR(child_bh))
4441 RETURN(PTR_ERR(child_bh));
4443 rc = md_batch_add(tgt->ltd_exp, child_bh, item);
4447 static const struct obd_ops lmv_obd_ops = {
4448 .o_owner = THIS_MODULE,
4449 .o_setup = lmv_setup,
4450 .o_cleanup = lmv_cleanup,
4451 .o_precleanup = lmv_precleanup,
4452 .o_process_config = lmv_process_config,
4453 .o_connect = lmv_connect,
4454 .o_disconnect = lmv_disconnect,
4455 .o_statfs = lmv_statfs,
4456 .o_get_info = lmv_get_info,
4457 .o_set_info_async = lmv_set_info_async,
4458 .o_notify = lmv_notify,
4459 .o_get_uuid = lmv_get_uuid,
4460 .o_fid_alloc = lmv_fid_alloc,
4461 .o_iocontrol = lmv_iocontrol,
4462 .o_quotactl = lmv_quotactl
4465 static const struct md_ops lmv_md_ops = {
4466 .m_get_root = lmv_get_root,
4467 .m_null_inode = lmv_null_inode,
4468 .m_close = lmv_close,
4469 .m_create = lmv_create,
4470 .m_enqueue = lmv_enqueue,
4471 .m_getattr = lmv_getattr,
4472 .m_getxattr = lmv_getxattr,
4473 .m_getattr_name = lmv_getattr_name,
4474 .m_intent_lock = lmv_intent_lock,
4476 .m_rename = lmv_rename,
4477 .m_setattr = lmv_setattr,
4478 .m_setxattr = lmv_setxattr,
4479 .m_fsync = lmv_fsync,
4480 .m_file_resync = lmv_file_resync,
4481 .m_read_page = lmv_read_page,
4482 .m_unlink = lmv_unlink,
4483 .m_init_ea_size = lmv_init_ea_size,
4484 .m_cancel_unused = lmv_cancel_unused,
4485 .m_set_lock_data = lmv_set_lock_data,
4486 .m_lock_match = lmv_lock_match,
4487 .m_get_lustre_md = lmv_get_lustre_md,
4488 .m_put_lustre_md = lmv_put_lustre_md,
4489 .m_merge_attr = lmv_merge_attr,
4490 .m_set_open_replay_data = lmv_set_open_replay_data,
4491 .m_clear_open_replay_data = lmv_clear_open_replay_data,
4492 .m_intent_getattr_async = lmv_intent_getattr_async,
4493 .m_revalidate_lock = lmv_revalidate_lock,
4494 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
4495 .m_stripe_object_create = lmv_stripe_object_create,
4496 .m_rmfid = lmv_rmfid,
4497 .m_batch_create = lmv_batch_create,
4498 .m_batch_add = lmv_batch_add,
4499 .m_batch_stop = lmv_batch_stop,
4500 .m_batch_flush = lmv_batch_flush,
4503 static int __init lmv_init(void)
4507 rc = libcfs_setup();
4511 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true,
4512 LUSTRE_LMV_NAME, NULL);
4515 static void __exit lmv_exit(void)
4517 class_unregister_type(LUSTRE_LMV_NAME);
4520 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
4521 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
4522 MODULE_VERSION(LUSTRE_VERSION_STRING);
4523 MODULE_LICENSE("GPL");
4525 module_init(lmv_init);
4526 module_exit(lmv_exit);