4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LMV
35 #include <linux/file.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/user_namespace.h>
39 #ifdef HAVE_UIDGID_HEADER
40 # include <linux/uidgid.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
45 #include <linux/math64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
49 #include <obd_support.h>
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <obd_class.h>
53 #include <lustre_lmv.h>
54 #include <lprocfs_status.h>
55 #include <cl_object.h>
56 #include <lustre_fid.h>
57 #include <uapi/linux/lustre/lustre_ioctl.h>
58 #include <lustre_kernelcomm.h>
59 #include "lmv_internal.h"
61 static int lmv_check_connect(struct obd_device *obd);
63 static void lmv_activate_target(struct lmv_obd *lmv,
64 struct lmv_tgt_desc *tgt,
67 if (tgt->ltd_active == activate)
70 tgt->ltd_active = activate;
71 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
73 tgt->ltd_exp->exp_obd->obd_inactive = !activate;
79 * -EINVAL : UUID can't be found in the LMV's target list
80 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
81 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
83 static int lmv_set_mdc_active(struct lmv_obd *lmv,
84 const struct obd_uuid *uuid,
87 struct lmv_tgt_desc *tgt = NULL;
88 struct obd_device *obd;
93 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
94 lmv, uuid->uuid, activate);
96 spin_lock(&lmv->lmv_lock);
97 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
99 if (tgt == NULL || tgt->ltd_exp == NULL)
102 CDEBUG(D_INFO, "Target idx %d is %s conn %#llx\n", i,
103 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
105 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
109 if (i == lmv->desc.ld_tgt_count)
110 GOTO(out_lmv_lock, rc = -EINVAL);
112 obd = class_exp2obd(tgt->ltd_exp);
114 GOTO(out_lmv_lock, rc = -ENOTCONN);
116 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
117 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
118 obd->obd_type->typ_name, i);
119 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
121 if (tgt->ltd_active == activate) {
122 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
123 activate ? "" : "in");
124 GOTO(out_lmv_lock, rc);
127 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
128 activate ? "" : "in");
129 lmv_activate_target(lmv, tgt, activate);
133 spin_unlock(&lmv->lmv_lock);
137 struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
139 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
140 struct lmv_tgt_desc *tgt = lmv->tgts[0];
142 return (tgt == NULL) ? NULL : obd_get_uuid(tgt->ltd_exp);
145 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
146 enum obd_notify_event ev)
148 struct obd_connect_data *conn_data;
149 struct lmv_obd *lmv = &obd->u.lmv;
150 struct obd_uuid *uuid;
154 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
155 CERROR("unexpected notification of %s %s!\n",
156 watched->obd_type->typ_name,
161 uuid = &watched->u.cli.cl_target_uuid;
162 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
164 * Set MDC as active before notifying the observer, so the
165 * observer can use the MDC normally.
167 rc = lmv_set_mdc_active(lmv, uuid,
168 ev == OBD_NOTIFY_ACTIVE);
170 CERROR("%sactivation of %s failed: %d\n",
171 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
175 } else if (ev == OBD_NOTIFY_OCD) {
176 conn_data = &watched->u.cli.cl_import->imp_connect_data;
178 * XXX: Make sure that ocd_connect_flags from all targets are
179 * the same. Otherwise one of MDTs runs wrong version or
180 * something like this. --umka
182 obd->obd_self_export->exp_connect_data = *conn_data;
186 * Pass the notification up the chain.
188 if (obd->obd_observer)
189 rc = obd_notify(obd->obd_observer, watched, ev);
194 static int lmv_connect(const struct lu_env *env,
195 struct obd_export **pexp, struct obd_device *obd,
196 struct obd_uuid *cluuid, struct obd_connect_data *data,
199 struct lmv_obd *lmv = &obd->u.lmv;
200 struct lustre_handle conn = { 0 };
201 struct obd_export *exp;
205 rc = class_connect(&conn, obd, cluuid);
207 CERROR("class_connection() returned %d\n", rc);
211 exp = class_conn2export(&conn);
214 lmv->conn_data = *data;
215 lmv->lmv_cache = localdata;
217 lmv->lmv_tgts_kobj = kobject_create_and_add("target_obds",
218 &obd->obd_kset.kobj);
219 if (!lmv->lmv_tgts_kobj) {
220 CERROR("%s: cannot create /sys/fs/lustre/%s/%s/target_obds\n",
221 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
224 rc = lmv_check_connect(obd);
233 if (lmv->lmv_tgts_kobj)
234 kobject_put(lmv->lmv_tgts_kobj);
236 class_disconnect(exp);
241 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
244 struct obd_device *obd = exp->exp_obd;
245 struct lmv_obd *lmv = &obd->u.lmv;
251 if (lmv->max_easize < easize) {
252 lmv->max_easize = easize;
255 if (lmv->max_def_easize < def_easize) {
256 lmv->max_def_easize = def_easize;
263 if (lmv->connected == 0)
266 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
267 struct lmv_tgt_desc *tgt = lmv->tgts[i];
269 if (tgt == NULL || tgt->ltd_exp == NULL) {
270 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
273 if (!tgt->ltd_active)
276 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
278 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
279 " rc = %d\n", obd->obd_name, i, rc);
286 #define MAX_STRING_SIZE 128
288 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
290 struct lmv_obd *lmv = &obd->u.lmv;
291 struct obd_device *mdc_obd;
292 struct obd_export *mdc_exp;
293 struct lu_fld_target target;
297 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
300 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
304 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s\n",
305 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
306 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid);
308 if (!mdc_obd->obd_set_up) {
309 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
313 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
314 &lmv->conn_data, lmv->lmv_cache);
316 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
321 * Init fid sequence client for this mdc and add new fld target.
323 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
327 target.ft_srv = NULL;
328 target.ft_exp = mdc_exp;
329 target.ft_idx = tgt->ltd_idx;
331 fld_client_add_target(&lmv->lmv_fld, &target);
333 rc = obd_register_observer(mdc_obd, obd);
335 obd_disconnect(mdc_exp);
336 CERROR("target %s register_observer error %d\n",
337 tgt->ltd_uuid.uuid, rc);
341 if (obd->obd_observer) {
343 * Tell the observer about the new target.
345 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
348 obd_disconnect(mdc_exp);
354 tgt->ltd_exp = mdc_exp;
355 lmv->desc.ld_active_tgt_count++;
357 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
359 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
360 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
361 atomic_read(&obd->obd_refcount));
363 if (lmv->lmv_tgts_kobj)
364 /* Even if we failed to create the link, that's fine */
365 rc = sysfs_create_link(lmv->lmv_tgts_kobj,
366 &mdc_obd->obd_kset.kobj,
371 static void lmv_del_target(struct lmv_obd *lmv, int index)
373 if (lmv->tgts[index] == NULL)
376 OBD_FREE_PTR(lmv->tgts[index]);
377 lmv->tgts[index] = NULL;
381 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
382 __u32 index, int gen)
384 struct obd_device *mdc_obd;
385 struct lmv_obd *lmv = &obd->u.lmv;
386 struct lmv_tgt_desc *tgt;
387 int orig_tgt_count = 0;
391 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
392 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
395 CERROR("%s: Target %s not attached: rc = %d\n",
396 obd->obd_name, uuidp->uuid, -EINVAL);
400 mutex_lock(&lmv->lmv_init_mutex);
401 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
402 tgt = lmv->tgts[index];
403 CERROR("%s: UUID %s already assigned at LMV target index %d:"
404 " rc = %d\n", obd->obd_name,
405 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
406 mutex_unlock(&lmv->lmv_init_mutex);
410 if (index >= lmv->tgts_size) {
411 /* We need to reallocate the lmv target array. */
412 struct lmv_tgt_desc **newtgts, **old = NULL;
416 while (newsize < index + 1)
417 newsize = newsize << 1;
418 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
419 if (newtgts == NULL) {
420 mutex_unlock(&lmv->lmv_init_mutex);
424 if (lmv->tgts_size) {
425 memcpy(newtgts, lmv->tgts,
426 sizeof(*newtgts) * lmv->tgts_size);
428 oldsize = lmv->tgts_size;
432 lmv->tgts_size = newsize;
435 OBD_FREE(old, sizeof(*old) * oldsize);
437 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
443 mutex_unlock(&lmv->lmv_init_mutex);
447 mutex_init(&tgt->ltd_fid_mutex);
448 tgt->ltd_idx = index;
449 tgt->ltd_uuid = *uuidp;
451 lmv->tgts[index] = tgt;
452 if (index >= lmv->desc.ld_tgt_count) {
453 orig_tgt_count = lmv->desc.ld_tgt_count;
454 lmv->desc.ld_tgt_count = index + 1;
457 if (lmv->connected == 0) {
458 /* lmv_check_connect() will connect this target. */
459 mutex_unlock(&lmv->lmv_init_mutex);
463 /* Otherwise let's connect it ourselves */
464 mutex_unlock(&lmv->lmv_init_mutex);
465 rc = lmv_connect_mdc(obd, tgt);
467 spin_lock(&lmv->lmv_lock);
468 if (lmv->desc.ld_tgt_count == index + 1)
469 lmv->desc.ld_tgt_count = orig_tgt_count;
470 memset(tgt, 0, sizeof(*tgt));
471 spin_unlock(&lmv->lmv_lock);
473 int easize = sizeof(struct lmv_stripe_md) +
474 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
475 lmv_init_ea_size(obd->obd_self_export, easize, 0);
481 static int lmv_check_connect(struct obd_device *obd)
483 struct lmv_obd *lmv = &obd->u.lmv;
484 struct lmv_tgt_desc *tgt;
493 mutex_lock(&lmv->lmv_init_mutex);
494 if (lmv->connected) {
495 mutex_unlock(&lmv->lmv_init_mutex);
499 if (lmv->desc.ld_tgt_count == 0) {
500 mutex_unlock(&lmv->lmv_init_mutex);
501 CERROR("%s: no targets configured.\n", obd->obd_name);
505 LASSERT(lmv->tgts != NULL);
507 if (lmv->tgts[0] == NULL) {
508 mutex_unlock(&lmv->lmv_init_mutex);
509 CERROR("%s: no target configured for index 0.\n",
514 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
515 obd->obd_uuid.uuid, obd->obd_name);
517 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
521 rc = lmv_connect_mdc(obd, tgt);
527 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
528 lmv_init_ea_size(obd->obd_self_export, easize, 0);
529 mutex_unlock(&lmv->lmv_init_mutex);
540 --lmv->desc.ld_active_tgt_count;
541 rc2 = obd_disconnect(tgt->ltd_exp);
543 CERROR("LMV target %s disconnect on "
544 "MDC idx %d: error %d\n",
545 tgt->ltd_uuid.uuid, i, rc2);
550 mutex_unlock(&lmv->lmv_init_mutex);
555 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
557 struct lmv_obd *lmv = &obd->u.lmv;
558 struct obd_device *mdc_obd;
562 LASSERT(tgt != NULL);
563 LASSERT(obd != NULL);
565 mdc_obd = class_exp2obd(tgt->ltd_exp);
568 mdc_obd->obd_force = obd->obd_force;
569 mdc_obd->obd_fail = obd->obd_fail;
570 mdc_obd->obd_no_recov = obd->obd_no_recov;
572 if (lmv->lmv_tgts_kobj)
573 sysfs_remove_link(lmv->lmv_tgts_kobj,
577 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
579 CERROR("Can't finanize fids factory\n");
581 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
582 tgt->ltd_exp->exp_obd->obd_name,
583 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
585 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
586 rc = obd_disconnect(tgt->ltd_exp);
588 if (tgt->ltd_active) {
589 CERROR("Target %s disconnect error %d\n",
590 tgt->ltd_uuid.uuid, rc);
594 lmv_activate_target(lmv, tgt, 0);
599 static int lmv_disconnect(struct obd_export *exp)
601 struct obd_device *obd = class_exp2obd(exp);
602 struct lmv_obd *lmv = &obd->u.lmv;
610 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
611 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
614 lmv_disconnect_mdc(obd, lmv->tgts[i]);
617 if (lmv->lmv_tgts_kobj)
618 kobject_put(lmv->lmv_tgts_kobj);
622 * This is the case when no real connection is established by
623 * lmv_check_connect().
626 class_export_put(exp);
627 rc = class_disconnect(exp);
633 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
636 struct obd_device *obddev = class_exp2obd(exp);
637 struct lmv_obd *lmv = &obddev->u.lmv;
638 struct getinfo_fid2path *gf;
639 struct lmv_tgt_desc *tgt;
640 struct getinfo_fid2path *remote_gf = NULL;
641 struct lu_fid root_fid;
642 int remote_gf_size = 0;
646 tgt = lmv_find_target(lmv, &gf->gf_fid);
648 RETURN(PTR_ERR(tgt));
650 root_fid = *gf->gf_u.gf_root_fid;
651 LASSERT(fid_is_sane(&root_fid));
654 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
655 if (rc != 0 && rc != -EREMOTE)
656 GOTO(out_fid2path, rc);
658 /* If remote_gf != NULL, it means just building the
659 * path on the remote MDT, copy this path segement to gf */
660 if (remote_gf != NULL) {
661 struct getinfo_fid2path *ori_gf;
665 ori_gf = (struct getinfo_fid2path *)karg;
666 if (strlen(ori_gf->gf_u.gf_path) + 1 +
667 strlen(gf->gf_u.gf_path) + 1 > ori_gf->gf_pathlen)
668 GOTO(out_fid2path, rc = -EOVERFLOW);
670 ptr = ori_gf->gf_u.gf_path;
672 len = strlen(gf->gf_u.gf_path);
673 /* move the current path to the right to release space
674 * for closer-to-root part */
675 memmove(ptr + len + 1, ptr, strlen(ori_gf->gf_u.gf_path));
676 memcpy(ptr, gf->gf_u.gf_path, len);
680 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
681 tgt->ltd_exp->exp_obd->obd_name,
682 gf->gf_u.gf_path, PFID(&gf->gf_fid), gf->gf_recno,
686 GOTO(out_fid2path, rc);
688 /* sigh, has to go to another MDT to do path building further */
689 if (remote_gf == NULL) {
690 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
691 OBD_ALLOC(remote_gf, remote_gf_size);
692 if (remote_gf == NULL)
693 GOTO(out_fid2path, rc = -ENOMEM);
694 remote_gf->gf_pathlen = PATH_MAX;
697 if (!fid_is_sane(&gf->gf_fid)) {
698 CERROR("%s: invalid FID "DFID": rc = %d\n",
699 tgt->ltd_exp->exp_obd->obd_name,
700 PFID(&gf->gf_fid), -EINVAL);
701 GOTO(out_fid2path, rc = -EINVAL);
704 tgt = lmv_find_target(lmv, &gf->gf_fid);
706 GOTO(out_fid2path, rc = -EINVAL);
708 remote_gf->gf_fid = gf->gf_fid;
709 remote_gf->gf_recno = -1;
710 remote_gf->gf_linkno = -1;
711 memset(remote_gf->gf_u.gf_path, 0, remote_gf->gf_pathlen);
712 *remote_gf->gf_u.gf_root_fid = root_fid;
714 goto repeat_fid2path;
717 if (remote_gf != NULL)
718 OBD_FREE(remote_gf, remote_gf_size);
722 static int lmv_hsm_req_count(struct lmv_obd *lmv,
723 const struct hsm_user_request *hur,
724 const struct lmv_tgt_desc *tgt_mds)
728 struct lmv_tgt_desc *curr_tgt;
730 /* count how many requests must be sent to the given target */
731 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
732 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
733 if (IS_ERR(curr_tgt))
734 RETURN(PTR_ERR(curr_tgt));
735 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
741 static int lmv_hsm_req_build(struct lmv_obd *lmv,
742 struct hsm_user_request *hur_in,
743 const struct lmv_tgt_desc *tgt_mds,
744 struct hsm_user_request *hur_out)
747 struct lmv_tgt_desc *curr_tgt;
749 /* build the hsm_user_request for the given target */
750 hur_out->hur_request = hur_in->hur_request;
752 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
753 curr_tgt = lmv_find_target(lmv,
754 &hur_in->hur_user_item[i].hui_fid);
755 if (IS_ERR(curr_tgt))
756 RETURN(PTR_ERR(curr_tgt));
757 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
758 hur_out->hur_user_item[nr_out] =
759 hur_in->hur_user_item[i];
763 hur_out->hur_request.hr_itemcount = nr_out;
764 memcpy(hur_data(hur_out), hur_data(hur_in),
765 hur_in->hur_request.hr_data_len);
770 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
771 int len, struct lustre_kernelcomm *lk,
774 struct lmv_obd *lmv = &obd->u.lmv;
779 /* unregister request (call from llapi_hsm_copytool_fini) */
780 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
781 struct lmv_tgt_desc *tgt = lmv->tgts[i];
783 if (tgt == NULL || tgt->ltd_exp == NULL)
785 /* best effort: try to clean as much as possible
786 * (continue on error) */
787 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
790 /* Whatever the result, remove copytool from kuc groups.
791 * Unreached coordinators will get EPIPE on next requests
792 * and will unregister automatically.
794 rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
799 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
800 int len, struct lustre_kernelcomm *lk,
803 struct lmv_obd *lmv = &obd->u.lmv;
807 bool any_set = false;
808 struct kkuc_ct_data *kcd;
813 filp = fget(lk->lk_wfd);
817 if (lk->lk_flags & LK_FLG_DATANR)
818 kcd_size = offsetof(struct kkuc_ct_data,
819 kcd_archives[lk->lk_data_count]);
821 kcd_size = sizeof(*kcd);
823 OBD_ALLOC(kcd, kcd_size);
825 GOTO(err_fput, rc = -ENOMEM);
827 kcd->kcd_nr_archives = lk->lk_data_count;
828 if (lk->lk_flags & LK_FLG_DATANR) {
829 kcd->kcd_magic = KKUC_CT_DATA_ARRAY_MAGIC;
830 if (lk->lk_data_count > 0)
831 memcpy(kcd->kcd_archives, lk->lk_data,
832 sizeof(*kcd->kcd_archives) * lk->lk_data_count);
834 kcd->kcd_magic = KKUC_CT_DATA_BITMAP_MAGIC;
837 rc = libcfs_kkuc_group_add(filp, &obd->obd_uuid, lk->lk_uid,
838 lk->lk_group, kcd, kcd_size);
839 OBD_FREE(kcd, kcd_size);
843 /* All or nothing: try to register to all MDS.
844 * In case of failure, unregister from previous MDS,
845 * except if it because of inactive target. */
846 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
847 struct lmv_tgt_desc *tgt = lmv->tgts[i];
849 if (tgt == NULL || tgt->ltd_exp == NULL)
852 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
854 if (tgt->ltd_active) {
855 /* permanent error */
856 CERROR("%s: iocontrol MDC %s on MDT"
857 " idx %d cmd %x: err = %d\n",
858 lmv2obd_dev(lmv)->obd_name,
859 tgt->ltd_uuid.uuid, i, cmd, err);
861 lk->lk_flags |= LK_FLG_STOP;
862 /* unregister from previous MDS */
863 for (j = 0; j < i; j++) {
865 if (tgt == NULL || tgt->ltd_exp == NULL)
867 obd_iocontrol(cmd, tgt->ltd_exp, len,
870 GOTO(err_kkuc_rem, rc);
872 /* else: transient error.
873 * kuc will register to the missing MDT
881 /* no registration done: return error */
882 GOTO(err_kkuc_rem, rc = -ENOTCONN);
887 libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
897 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
898 int len, void *karg, void __user *uarg)
900 struct obd_device *obddev = class_exp2obd(exp);
901 struct lmv_obd *lmv = &obddev->u.lmv;
902 struct lmv_tgt_desc *tgt = NULL;
906 __u32 count = lmv->desc.ld_tgt_count;
913 case IOC_OBD_STATFS: {
914 struct obd_ioctl_data *data = karg;
915 struct obd_device *mdc_obd;
916 struct obd_statfs stat_buf = {0};
919 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
920 if ((index >= count))
923 tgt = lmv->tgts[index];
924 if (tgt == NULL || !tgt->ltd_active)
927 mdc_obd = class_exp2obd(tgt->ltd_exp);
932 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
933 min((int) data->ioc_plen2,
934 (int) sizeof(struct obd_uuid))))
937 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
938 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
942 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
943 min((int) data->ioc_plen1,
944 (int) sizeof(stat_buf))))
948 case OBD_IOC_QUOTACTL: {
949 struct if_quotactl *qctl = karg;
950 struct obd_quotactl *oqctl;
952 if (qctl->qc_valid == QC_MDTIDX) {
953 if (count <= qctl->qc_idx)
956 tgt = lmv->tgts[qctl->qc_idx];
957 if (tgt == NULL || tgt->ltd_exp == NULL)
959 } else if (qctl->qc_valid == QC_UUID) {
960 for (i = 0; i < count; i++) {
964 if (!obd_uuid_equals(&tgt->ltd_uuid,
968 if (tgt->ltd_exp == NULL)
980 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
981 OBD_ALLOC_PTR(oqctl);
985 QCTL_COPY(oqctl, qctl);
986 rc = obd_quotactl(tgt->ltd_exp, oqctl);
988 QCTL_COPY(qctl, oqctl);
989 qctl->qc_valid = QC_MDTIDX;
990 qctl->obd_uuid = tgt->ltd_uuid;
995 case LL_IOC_GET_CONNECT_FLAGS: {
997 if (tgt == NULL || tgt->ltd_exp == NULL)
999 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1002 case LL_IOC_FID2MDTIDX: {
1003 struct lu_fid *fid = karg;
1006 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
1010 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
1011 * point to user space memory for FID2MDTIDX. */
1012 *(__u32 *)uarg = mdt_index;
1015 case OBD_IOC_FID2PATH: {
1016 rc = lmv_fid2path(exp, len, karg, uarg);
1019 case LL_IOC_HSM_STATE_GET:
1020 case LL_IOC_HSM_STATE_SET:
1021 case LL_IOC_HSM_ACTION: {
1022 struct md_op_data *op_data = karg;
1024 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1026 RETURN(PTR_ERR(tgt));
1028 if (tgt->ltd_exp == NULL)
1031 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1034 case LL_IOC_HSM_PROGRESS: {
1035 const struct hsm_progress_kernel *hpk = karg;
1037 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1039 RETURN(PTR_ERR(tgt));
1040 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1043 case LL_IOC_HSM_REQUEST: {
1044 struct hsm_user_request *hur = karg;
1045 unsigned int reqcount = hur->hur_request.hr_itemcount;
1050 /* if the request is about a single fid
1051 * or if there is a single MDS, no need to split
1053 if (reqcount == 1 || count == 1) {
1054 tgt = lmv_find_target(lmv,
1055 &hur->hur_user_item[0].hui_fid);
1057 RETURN(PTR_ERR(tgt));
1058 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1060 /* split fid list to their respective MDS */
1061 for (i = 0; i < count; i++) {
1064 struct hsm_user_request *req;
1067 if (tgt == NULL || tgt->ltd_exp == NULL)
1070 nr = lmv_hsm_req_count(lmv, hur, tgt);
1073 if (nr == 0) /* nothing for this MDS */
1076 /* build a request with fids for this MDS */
1077 reqlen = offsetof(typeof(*hur),
1079 + hur->hur_request.hr_data_len;
1080 OBD_ALLOC_LARGE(req, reqlen);
1083 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1085 GOTO(hsm_req_err, rc1);
1086 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1089 if (rc1 != 0 && rc == 0)
1091 OBD_FREE_LARGE(req, reqlen);
1096 case LL_IOC_LOV_SWAP_LAYOUTS: {
1097 struct md_op_data *op_data = karg;
1098 struct lmv_tgt_desc *tgt1, *tgt2;
1100 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1102 RETURN(PTR_ERR(tgt1));
1104 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1106 RETURN(PTR_ERR(tgt2));
1108 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1111 /* only files on same MDT can have their layouts swapped */
1112 if (tgt1->ltd_idx != tgt2->ltd_idx)
1115 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1118 case LL_IOC_HSM_CT_START: {
1119 struct lustre_kernelcomm *lk = karg;
1120 if (lk->lk_flags & LK_FLG_STOP)
1121 rc = lmv_hsm_ct_unregister(obddev, cmd, len, lk, uarg);
1123 rc = lmv_hsm_ct_register(obddev, cmd, len, lk, uarg);
1127 for (i = 0; i < count; i++) {
1128 struct obd_device *mdc_obd;
1132 if (tgt == NULL || tgt->ltd_exp == NULL)
1134 /* ll_umount_begin() sets force flag but for lmv, not
1135 * mdc. Let's pass it through */
1136 mdc_obd = class_exp2obd(tgt->ltd_exp);
1137 mdc_obd->obd_force = obddev->obd_force;
1138 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1140 if (tgt->ltd_active) {
1141 CERROR("error: iocontrol MDC %s on MDT"
1142 " idx %d cmd %x: err = %d\n",
1143 tgt->ltd_uuid.uuid, i, cmd, err);
1157 * This is _inode_ placement policy function (not name).
1159 static int lmv_placement_policy(struct obd_device *obd,
1160 struct md_op_data *op_data, u32 *mds)
1162 struct lmv_obd *lmv = &obd->u.lmv;
1163 struct lmv_user_md *lum;
1167 LASSERT(mds != NULL);
1169 if (lmv->desc.ld_tgt_count == 1) {
1174 lum = op_data->op_data;
1176 * 1. See if the stripe offset is specified by lum.
1177 * 2. Then check if there is default stripe offset.
1178 * 3. Finally choose MDS by name hash if the parent
1179 * is striped directory. (see lmv_locate_tgt()).
1181 * presently explicit MDT location is not supported
1182 * for foreign dirs (as it can't be embedded into free
1183 * format LMV, like with lum_stripe_offset), so we only
1184 * rely on default stripe offset or then name hashing.
1186 if (op_data->op_cli_flags & CLI_SET_MEA && lum != NULL &&
1187 le32_to_cpu(lum->lum_magic != LMV_MAGIC_FOREIGN) &&
1188 le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1189 *mds = le32_to_cpu(lum->lum_stripe_offset);
1190 } else if (op_data->op_default_stripe_offset != (__u32)-1) {
1191 *mds = op_data->op_default_stripe_offset;
1192 op_data->op_mds = *mds;
1193 /* Correct the stripe offset in lum */
1195 le32_to_cpu(lum->lum_magic != LMV_MAGIC_FOREIGN))
1196 lum->lum_stripe_offset = cpu_to_le32(*mds);
1198 *mds = op_data->op_mds;
1204 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1206 struct lmv_tgt_desc *tgt;
1210 tgt = lmv_get_target(lmv, mds, NULL);
1212 RETURN(PTR_ERR(tgt));
1215 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1216 * on server that seq in new allocated fid is not yet known.
1218 mutex_lock(&tgt->ltd_fid_mutex);
1220 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1221 GOTO(out, rc = -ENODEV);
1224 * Asking underlying tgt layer to allocate new fid.
1226 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1228 LASSERT(fid_is_sane(fid));
1234 mutex_unlock(&tgt->ltd_fid_mutex);
1238 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1239 struct lu_fid *fid, struct md_op_data *op_data)
1241 struct obd_device *obd = class_exp2obd(exp);
1242 struct lmv_obd *lmv = &obd->u.lmv;
1247 LASSERT(op_data != NULL);
1248 LASSERT(fid != NULL);
1250 rc = lmv_placement_policy(obd, op_data, &mds);
1252 CERROR("Can't get target for allocating fid, "
1257 rc = __lmv_fid_alloc(lmv, fid, mds);
1259 CERROR("Can't alloc new fid, rc %d\n", rc);
1266 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1268 struct lmv_obd *lmv = &obd->u.lmv;
1269 struct lmv_desc *desc;
1273 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1274 CERROR("LMV setup requires a descriptor\n");
1278 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1279 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1280 CERROR("Lmv descriptor size wrong: %d > %d\n",
1281 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1285 lmv->tgts_size = 32U;
1286 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1287 if (lmv->tgts == NULL)
1290 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1291 lmv->desc.ld_tgt_count = 0;
1292 lmv->desc.ld_active_tgt_count = 0;
1293 lmv->max_def_easize = 0;
1294 lmv->max_easize = 0;
1296 spin_lock_init(&lmv->lmv_lock);
1297 mutex_init(&lmv->lmv_init_mutex);
1299 rc = lmv_tunables_init(obd);
1301 CWARN("%s: error adding LMV sysfs/debugfs files: rc = %d\n",
1304 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1305 LUSTRE_CLI_FLD_HASH_DHT);
1307 CERROR("Can't init FLD, err %d\n", rc);
1317 static int lmv_cleanup(struct obd_device *obd)
1319 struct lmv_obd *lmv = &obd->u.lmv;
1322 fld_client_fini(&lmv->lmv_fld);
1323 if (lmv->tgts != NULL) {
1325 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1326 if (lmv->tgts[i] == NULL)
1328 lmv_del_target(lmv, i);
1330 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1336 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1338 struct lustre_cfg *lcfg = buf;
1339 struct obd_uuid obd_uuid;
1345 switch (lcfg->lcfg_command) {
1347 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1348 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1349 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1350 GOTO(out, rc = -EINVAL);
1352 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1354 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1355 GOTO(out, rc = -EINVAL);
1356 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1357 GOTO(out, rc = -EINVAL);
1358 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1361 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1362 GOTO(out, rc = -EINVAL);
1368 static int lmv_select_statfs_mdt(struct lmv_obd *lmv, __u32 flags)
1372 if (flags & OBD_STATFS_FOR_MDT0)
1375 if (lmv->lmv_statfs_start || lmv->desc.ld_tgt_count == 1)
1376 return lmv->lmv_statfs_start;
1378 /* choose initial MDT for this client */
1380 struct lnet_process_id lnet_id;
1381 if (LNetGetId(i, &lnet_id) == -ENOENT)
1384 if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
1385 /* We dont need a full 64-bit modulus, just enough
1386 * to distribute the requests across MDTs evenly.
1388 lmv->lmv_statfs_start =
1389 (u32)lnet_id.nid % lmv->desc.ld_tgt_count;
1394 return lmv->lmv_statfs_start;
1397 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1398 struct obd_statfs *osfs, time64_t max_age, __u32 flags)
1400 struct obd_device *obd = class_exp2obd(exp);
1401 struct lmv_obd *lmv = &obd->u.lmv;
1402 struct obd_statfs *temp;
1407 OBD_ALLOC(temp, sizeof(*temp));
1411 /* distribute statfs among MDTs */
1412 idx = lmv_select_statfs_mdt(lmv, flags);
1414 for (i = 0; i < lmv->desc.ld_tgt_count; i++, idx++) {
1415 idx = idx % lmv->desc.ld_tgt_count;
1416 if (lmv->tgts[idx] == NULL || lmv->tgts[idx]->ltd_exp == NULL)
1419 rc = obd_statfs(env, lmv->tgts[idx]->ltd_exp, temp,
1422 CERROR("%s: can't stat MDS #%d: rc = %d\n",
1423 lmv->tgts[idx]->ltd_exp->exp_obd->obd_name, i,
1425 GOTO(out_free_temp, rc);
1428 if (temp->os_state & OS_STATE_SUM ||
1429 flags == OBD_STATFS_FOR_MDT0) {
1430 /* reset to the last aggregated values
1431 * and don't sum with non-aggrated data */
1432 /* If the statfs is from mount, it needs to retrieve
1433 * necessary information from MDT0. i.e. mount does
1434 * not need the merged osfs from all of MDT. Also
1435 * clients can be mounted as long as MDT0 is in
1444 osfs->os_bavail += temp->os_bavail;
1445 osfs->os_blocks += temp->os_blocks;
1446 osfs->os_ffree += temp->os_ffree;
1447 osfs->os_files += temp->os_files;
1448 osfs->os_granted += temp->os_granted;
1454 OBD_FREE(temp, sizeof(*temp));
1458 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1461 struct obd_device *obd = exp->exp_obd;
1462 struct lmv_obd *lmv = &obd->u.lmv;
1466 rc = md_get_root(lmv->tgts[0]->ltd_exp, fileset, fid);
1470 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1471 u64 obd_md_valid, const char *name, size_t buf_size,
1472 struct ptlrpc_request **req)
1474 struct obd_device *obd = exp->exp_obd;
1475 struct lmv_obd *lmv = &obd->u.lmv;
1476 struct lmv_tgt_desc *tgt;
1480 tgt = lmv_find_target(lmv, fid);
1482 RETURN(PTR_ERR(tgt));
1484 rc = md_getxattr(tgt->ltd_exp, fid, obd_md_valid, name, buf_size, req);
1489 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1490 u64 obd_md_valid, const char *name,
1491 const void *value, size_t value_size,
1492 unsigned int xattr_flags, u32 suppgid,
1493 struct ptlrpc_request **req)
1495 struct obd_device *obd = exp->exp_obd;
1496 struct lmv_obd *lmv = &obd->u.lmv;
1497 struct lmv_tgt_desc *tgt;
1501 tgt = lmv_find_target(lmv, fid);
1503 RETURN(PTR_ERR(tgt));
1505 rc = md_setxattr(tgt->ltd_exp, fid, obd_md_valid, name,
1506 value, value_size, xattr_flags, suppgid, req);
1511 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1512 struct ptlrpc_request **request)
1514 struct obd_device *obd = exp->exp_obd;
1515 struct lmv_obd *lmv = &obd->u.lmv;
1516 struct lmv_tgt_desc *tgt;
1520 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1522 RETURN(PTR_ERR(tgt));
1524 if (op_data->op_flags & MF_GET_MDT_IDX) {
1525 op_data->op_mds = tgt->ltd_idx;
1529 rc = md_getattr(tgt->ltd_exp, op_data, request);
1534 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1536 struct obd_device *obd = exp->exp_obd;
1537 struct lmv_obd *lmv = &obd->u.lmv;
1541 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1544 * With DNE every object can have two locks in different namespaces:
1545 * lookup lock in space of MDT storing direntry and update/open lock in
1546 * space of MDT storing inode.
1548 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1549 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1551 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1557 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1558 struct md_open_data *mod, struct ptlrpc_request **request)
1560 struct obd_device *obd = exp->exp_obd;
1561 struct lmv_obd *lmv = &obd->u.lmv;
1562 struct lmv_tgt_desc *tgt;
1566 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1568 RETURN(PTR_ERR(tgt));
1570 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1571 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1575 struct lmv_tgt_desc*
1576 __lmv_locate_tgt(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1577 const char *name, int namelen, struct lu_fid *fid, u32 *mds,
1580 struct lmv_tgt_desc *tgt;
1581 const struct lmv_oinfo *oinfo;
1583 if (lsm == NULL || namelen == 0) {
1584 tgt = lmv_find_target(lmv, fid);
1589 *mds = tgt->ltd_idx;
1593 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1594 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1595 return ERR_PTR(-EBADF);
1596 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1598 oinfo = lsm_name_to_stripe_info(lsm, name, namelen,
1601 return ERR_CAST(oinfo);
1605 *fid = oinfo->lmo_fid;
1607 *mds = oinfo->lmo_mds;
1609 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1611 CDEBUG(D_INFO, "locate on mds %u "DFID"\n", oinfo->lmo_mds,
1612 PFID(&oinfo->lmo_fid));
1619 * Locate mdt by fid or name
1621 * For striped directory, it will locate the stripe by name hash, if hash_type
1622 * is unknown, it will return the stripe specified by 'op_data->op_stripe_index'
1623 * which is set outside, and if dir is migrating, 'op_data->op_post_migrate'
1624 * indicates whether old or new layout is used to locate.
1626 * For normal direcotry, it will locate MDS by FID directly.
1628 * \param[in] lmv LMV device
1629 * \param[in] op_data client MD stack parameters, name, namelen
1631 * \param[in] fid object FID used to locate MDS.
1633 * retval pointer to the lmv_tgt_desc if succeed.
1634 * ERR_PTR(errno) if failed.
1636 struct lmv_tgt_desc*
1637 lmv_locate_tgt(struct lmv_obd *lmv, struct md_op_data *op_data,
1640 struct lmv_stripe_md *lsm = op_data->op_mea1;
1641 struct lmv_oinfo *oinfo;
1642 struct lmv_tgt_desc *tgt;
1644 /* foreign dir is not striped dir */
1645 if (lsm && lsm->lsm_md_magic == LMV_MAGIC_FOREIGN)
1646 return ERR_PTR(-ENODATA);
1648 /* During creating VOLATILE file, it should honor the mdt
1649 * index if the file under striped dir is being restored, see
1651 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1652 (int)op_data->op_mds != -1) {
1653 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1660 /* refill the right parent fid */
1661 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1662 oinfo = &lsm->lsm_md_oinfo[i];
1663 if (oinfo->lmo_mds == op_data->op_mds) {
1664 *fid = oinfo->lmo_fid;
1669 if (i == lsm->lsm_md_stripe_count)
1670 *fid = lsm->lsm_md_oinfo[0].lmo_fid;
1672 } else if (lmv_is_dir_bad_hash(lsm)) {
1673 LASSERT(op_data->op_stripe_index < lsm->lsm_md_stripe_count);
1674 oinfo = &lsm->lsm_md_oinfo[op_data->op_stripe_index];
1676 *fid = oinfo->lmo_fid;
1677 op_data->op_mds = oinfo->lmo_mds;
1678 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1680 tgt = __lmv_locate_tgt(lmv, lsm, op_data->op_name,
1681 op_data->op_namelen, fid,
1683 op_data->op_post_migrate);
1689 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1690 const void *data, size_t datalen, umode_t mode, uid_t uid,
1691 gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
1692 struct ptlrpc_request **request)
1694 struct obd_device *obd = exp->exp_obd;
1695 struct lmv_obd *lmv = &obd->u.lmv;
1696 struct lmv_tgt_desc *tgt;
1700 if (!lmv->desc.ld_active_tgt_count)
1703 if (lmv_is_dir_bad_hash(op_data->op_mea1))
1706 if (lmv_is_dir_migrating(op_data->op_mea1)) {
1708 * if parent is migrating, create() needs to lookup existing
1709 * name, to avoid creating new file under old layout of
1710 * migrating directory, check old layout here.
1712 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
1714 RETURN(PTR_ERR(tgt));
1716 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1718 ptlrpc_req_finished(*request);
1726 op_data->op_post_migrate = true;
1729 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
1731 RETURN(PTR_ERR(tgt));
1733 CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1734 (int)op_data->op_namelen, op_data->op_name,
1735 PFID(&op_data->op_fid1), op_data->op_mds);
1737 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1741 if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1742 /* Send the create request to the MDT where the object
1743 * will be located */
1744 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1746 RETURN(PTR_ERR(tgt));
1748 op_data->op_mds = tgt->ltd_idx;
1750 CDEBUG(D_CONFIG, "Server doesn't support striped dirs\n");
1753 CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1754 PFID(&op_data->op_fid2), op_data->op_mds);
1756 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1757 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1758 cap_effective, rdev, request);
1760 if (*request == NULL)
1762 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1768 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1769 const union ldlm_policy_data *policy, struct md_op_data *op_data,
1770 struct lustre_handle *lockh, __u64 extra_lock_flags)
1772 struct obd_device *obd = exp->exp_obd;
1773 struct lmv_obd *lmv = &obd->u.lmv;
1774 struct lmv_tgt_desc *tgt;
1778 CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
1780 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1782 RETURN(PTR_ERR(tgt));
1784 CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
1785 PFID(&op_data->op_fid1), tgt->ltd_idx);
1787 rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
1794 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1795 struct ptlrpc_request **preq)
1797 struct obd_device *obd = exp->exp_obd;
1798 struct lmv_obd *lmv = &obd->u.lmv;
1799 struct lmv_tgt_desc *tgt;
1800 struct mdt_body *body;
1806 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
1808 RETURN(PTR_ERR(tgt));
1810 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1811 (int)op_data->op_namelen, op_data->op_name,
1812 PFID(&op_data->op_fid1), tgt->ltd_idx);
1814 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1815 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
1816 ptlrpc_req_finished(*preq);
1824 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1825 LASSERT(body != NULL);
1827 if (body->mbo_valid & OBD_MD_MDS) {
1828 op_data->op_fid1 = body->mbo_fid1;
1829 op_data->op_valid |= OBD_MD_FLCROSSREF;
1830 op_data->op_namelen = 0;
1831 op_data->op_name = NULL;
1833 ptlrpc_req_finished(*preq);
1842 #define md_op_data_fid(op_data, fl) \
1843 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1844 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1845 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1846 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1849 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1850 struct md_op_data *op_data, __u32 op_tgt,
1851 enum ldlm_mode mode, int bits, int flag)
1853 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1854 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
1855 union ldlm_policy_data policy = { { 0 } };
1859 if (!fid_is_sane(fid))
1863 tgt = lmv_find_target(lmv, fid);
1865 RETURN(PTR_ERR(tgt));
1868 if (tgt->ltd_idx != op_tgt) {
1869 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1870 policy.l_inodebits.bits = bits;
1871 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1872 mode, LCF_ASYNC, NULL);
1875 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1877 op_data->op_flags |= flag;
1885 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1888 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1889 struct ptlrpc_request **request)
1891 struct obd_device *obd = exp->exp_obd;
1892 struct lmv_obd *lmv = &obd->u.lmv;
1893 struct lmv_tgt_desc *tgt;
1897 LASSERT(op_data->op_namelen != 0);
1899 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1900 PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1901 op_data->op_name, PFID(&op_data->op_fid1));
1903 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1904 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1905 op_data->op_cap = cfs_curproc_cap_pack();
1907 if (lmv_is_dir_migrating(op_data->op_mea2)) {
1908 struct lu_fid fid1 = op_data->op_fid1;
1909 struct lmv_stripe_md *lsm1 = op_data->op_mea1;
1912 * avoid creating new file under old layout of migrating
1913 * directory, check it here.
1915 tgt = __lmv_locate_tgt(lmv, op_data->op_mea2, op_data->op_name,
1916 op_data->op_namelen, &op_data->op_fid2,
1917 &op_data->op_mds, false);
1918 tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
1920 RETURN(PTR_ERR(tgt));
1922 op_data->op_fid1 = op_data->op_fid2;
1923 op_data->op_mea1 = op_data->op_mea2;
1924 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1925 op_data->op_fid1 = fid1;
1926 op_data->op_mea1 = lsm1;
1928 ptlrpc_req_finished(*request);
1937 tgt = __lmv_locate_tgt(lmv, op_data->op_mea2, op_data->op_name,
1938 op_data->op_namelen, &op_data->op_fid2,
1939 &op_data->op_mds, true);
1941 RETURN(PTR_ERR(tgt));
1944 * Cancel UPDATE lock on child (fid1).
1946 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1947 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
1948 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1952 rc = md_link(tgt->ltd_exp, op_data, request);
1957 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
1958 const char *name, size_t namelen,
1959 struct ptlrpc_request **request)
1961 struct obd_device *obd = exp->exp_obd;
1962 struct lmv_obd *lmv = &obd->u.lmv;
1963 struct lmv_stripe_md *lsm = op_data->op_mea1;
1964 struct lmv_tgt_desc *parent_tgt;
1965 struct lmv_tgt_desc *sp_tgt;
1966 struct lmv_tgt_desc *tp_tgt = NULL;
1967 struct lmv_tgt_desc *child_tgt;
1968 struct lmv_tgt_desc *tgt;
1969 struct lu_fid target_fid;
1974 LASSERT(op_data->op_cli_flags & CLI_MIGRATE);
1976 CDEBUG(D_INODE, "MIGRATE "DFID"/%.*s\n",
1977 PFID(&op_data->op_fid1), (int)namelen, name);
1979 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1980 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1981 op_data->op_cap = cfs_curproc_cap_pack();
1983 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
1984 if (IS_ERR(parent_tgt))
1985 RETURN(PTR_ERR(parent_tgt));
1988 __u32 hash_type = lsm->lsm_md_hash_type;
1989 __u32 stripe_count = lsm->lsm_md_stripe_count;
1992 * old stripes are appended after new stripes for migrating
1995 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
1996 hash_type = lsm->lsm_md_migrate_hash;
1997 stripe_count -= lsm->lsm_md_migrate_offset;
2000 rc = lmv_name_to_stripe_index(hash_type, stripe_count, name,
2005 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION)
2006 rc += lsm->lsm_md_migrate_offset;
2008 /* save it in fid4 temporarily for early cancel */
2009 op_data->op_fid4 = lsm->lsm_md_oinfo[rc].lmo_fid;
2010 sp_tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[rc].lmo_mds,
2013 RETURN(PTR_ERR(sp_tgt));
2016 * if parent is being migrated too, fill op_fid2 with target
2017 * stripe fid, otherwise the target stripe is not created yet.
2019 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
2020 hash_type = lsm->lsm_md_hash_type &
2021 ~LMV_HASH_FLAG_MIGRATION;
2022 stripe_count = lsm->lsm_md_migrate_offset;
2024 rc = lmv_name_to_stripe_index(hash_type, stripe_count,
2029 op_data->op_fid2 = lsm->lsm_md_oinfo[rc].lmo_fid;
2030 tp_tgt = lmv_get_target(lmv,
2031 lsm->lsm_md_oinfo[rc].lmo_mds,
2034 RETURN(PTR_ERR(tp_tgt));
2037 sp_tgt = parent_tgt;
2040 child_tgt = lmv_find_target(lmv, &op_data->op_fid3);
2041 if (IS_ERR(child_tgt))
2042 RETURN(PTR_ERR(child_tgt));
2044 if (!S_ISDIR(op_data->op_mode) && tp_tgt)
2045 rc = __lmv_fid_alloc(lmv, &target_fid, tp_tgt->ltd_idx);
2047 rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
2052 * for directory, send migrate request to the MDT where the object will
2053 * be migrated to, because we can't create a striped directory remotely.
2055 * otherwise, send to the MDT where source is located because regular
2056 * file may open lease.
2058 * NB. if MDT doesn't support DIR_MIGRATE, send to source MDT too for
2059 * backward compatibility.
2061 if (S_ISDIR(op_data->op_mode) &&
2062 (exp_connect_flags2(exp) & OBD_CONNECT2_DIR_MIGRATE)) {
2063 tgt = lmv_find_target(lmv, &target_fid);
2065 RETURN(PTR_ERR(tgt));
2070 /* cancel UPDATE lock of parent master object */
2071 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx, LCK_EX,
2072 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2076 /* cancel UPDATE lock of source parent */
2077 if (sp_tgt != parent_tgt) {
2079 * migrate RPC packs master object FID, because we can only pack
2080 * two FIDs in reint RPC, but MDS needs to know both source
2081 * parent and target parent, and it will obtain them from master
2082 * FID and LMV, the other FID in RPC is kept for target.
2084 * since this FID is not passed to MDC, cancel it anyway.
2086 rc = lmv_early_cancel(exp, sp_tgt, op_data, -1, LCK_EX,
2087 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID4);
2091 op_data->op_flags &= ~MF_MDC_CANCEL_FID4;
2093 op_data->op_fid4 = target_fid;
2095 /* cancel UPDATE locks of target parent */
2096 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2097 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2101 /* cancel LOOKUP lock of source if source is remote object */
2102 if (child_tgt != sp_tgt) {
2103 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_idx,
2104 LCK_EX, MDS_INODELOCK_LOOKUP,
2105 MF_MDC_CANCEL_FID3);
2110 /* cancel ELC locks of source */
2111 rc = lmv_early_cancel(exp, child_tgt, op_data, tgt->ltd_idx, LCK_EX,
2112 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2116 rc = md_rename(tgt->ltd_exp, op_data, name, namelen, NULL, 0, request);
2121 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2122 const char *old, size_t oldlen,
2123 const char *new, size_t newlen,
2124 struct ptlrpc_request **request)
2126 struct obd_device *obd = exp->exp_obd;
2127 struct lmv_obd *lmv = &obd->u.lmv;
2128 struct lmv_tgt_desc *sp_tgt;
2129 struct lmv_tgt_desc *tp_tgt = NULL;
2130 struct lmv_tgt_desc *src_tgt = NULL;
2131 struct lmv_tgt_desc *tgt;
2132 struct mdt_body *body;
2137 LASSERT(oldlen != 0);
2139 if (op_data->op_cli_flags & CLI_MIGRATE) {
2140 rc = lmv_migrate(exp, op_data, old, oldlen, request);
2144 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2145 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2146 op_data->op_cap = cfs_curproc_cap_pack();
2148 if (lmv_is_dir_migrating(op_data->op_mea2)) {
2149 struct lu_fid fid1 = op_data->op_fid1;
2150 struct lmv_stripe_md *lsm1 = op_data->op_mea1;
2153 * we avoid creating new file under old layout of migrating
2154 * directory, if there is an existing file with new name under
2155 * old layout, we can't unlink file in old layout and rename to
2156 * new layout in one transaction, so return -EBUSY here.`
2158 tgt = __lmv_locate_tgt(lmv, op_data->op_mea2, new, newlen,
2159 &op_data->op_fid2, &op_data->op_mds,
2162 RETURN(PTR_ERR(tgt));
2164 op_data->op_fid1 = op_data->op_fid2;
2165 op_data->op_mea1 = op_data->op_mea2;
2166 op_data->op_name = new;
2167 op_data->op_namelen = newlen;
2168 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
2169 op_data->op_fid1 = fid1;
2170 op_data->op_mea1 = lsm1;
2171 op_data->op_name = NULL;
2172 op_data->op_namelen = 0;
2174 ptlrpc_req_finished(*request);
2183 /* rename to new layout for migrating directory */
2184 tp_tgt = __lmv_locate_tgt(lmv, op_data->op_mea2, new, newlen,
2185 &op_data->op_fid2, &op_data->op_mds, true);
2187 RETURN(PTR_ERR(tp_tgt));
2189 /* Since the target child might be destroyed, and it might become
2190 * orphan, and we can only check orphan on the local MDT right now, so
2191 * we send rename request to the MDT where target child is located. If
2192 * target child does not exist, then it will send the request to the
2194 if (fid_is_sane(&op_data->op_fid4)) {
2195 tgt = lmv_find_target(lmv, &op_data->op_fid4);
2197 RETURN(PTR_ERR(tgt));
2202 op_data->op_flags |= MF_MDC_CANCEL_FID4;
2204 /* cancel UPDATE locks of target parent */
2205 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2206 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2210 if (fid_is_sane(&op_data->op_fid4)) {
2211 /* cancel LOOKUP lock of target on target parent */
2212 if (tgt != tp_tgt) {
2213 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2214 tgt->ltd_idx, LCK_EX,
2215 MDS_INODELOCK_LOOKUP,
2216 MF_MDC_CANCEL_FID4);
2222 if (fid_is_sane(&op_data->op_fid3)) {
2223 src_tgt = lmv_find_target(lmv, &op_data->op_fid3);
2224 if (IS_ERR(src_tgt))
2225 RETURN(PTR_ERR(src_tgt));
2227 /* cancel ELC locks of source */
2228 rc = lmv_early_cancel(exp, src_tgt, op_data, tgt->ltd_idx,
2229 LCK_EX, MDS_INODELOCK_ELC,
2230 MF_MDC_CANCEL_FID3);
2236 sp_tgt = __lmv_locate_tgt(lmv, op_data->op_mea1, old, oldlen,
2237 &op_data->op_fid1, &op_data->op_mds,
2238 op_data->op_post_migrate);
2240 RETURN(PTR_ERR(sp_tgt));
2242 /* cancel UPDATE locks of source parent */
2243 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2244 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2248 if (fid_is_sane(&op_data->op_fid3)) {
2249 /* cancel LOOKUP lock of source on source parent */
2250 if (src_tgt != sp_tgt) {
2251 rc = lmv_early_cancel(exp, sp_tgt, op_data,
2252 tgt->ltd_idx, LCK_EX,
2253 MDS_INODELOCK_LOOKUP,
2254 MF_MDC_CANCEL_FID3);
2261 CDEBUG(D_INODE, "RENAME "DFID"/%.*s to "DFID"/%.*s\n",
2262 PFID(&op_data->op_fid1), (int)oldlen, old,
2263 PFID(&op_data->op_fid2), (int)newlen, new);
2265 rc = md_rename(tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2267 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2268 ptlrpc_req_finished(*request);
2273 if (rc && rc != -EXDEV)
2276 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2280 /* Not cross-ref case, just get out of here. */
2281 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2284 op_data->op_fid4 = body->mbo_fid1;
2286 ptlrpc_req_finished(*request);
2289 tgt = lmv_find_target(lmv, &op_data->op_fid4);
2291 RETURN(PTR_ERR(tgt));
2293 if (fid_is_sane(&op_data->op_fid4)) {
2294 /* cancel LOOKUP lock of target on target parent */
2295 if (tgt != tp_tgt) {
2296 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2297 tgt->ltd_idx, LCK_EX,
2298 MDS_INODELOCK_LOOKUP,
2299 MF_MDC_CANCEL_FID4);
2308 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2309 void *ea, size_t ealen, struct ptlrpc_request **request)
2311 struct obd_device *obd = exp->exp_obd;
2312 struct lmv_obd *lmv = &obd->u.lmv;
2313 struct lmv_tgt_desc *tgt;
2317 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x/0x%x\n",
2318 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2319 op_data->op_xvalid);
2321 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2322 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2324 RETURN(PTR_ERR(tgt));
2326 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2331 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2332 struct ptlrpc_request **request)
2334 struct obd_device *obd = exp->exp_obd;
2335 struct lmv_obd *lmv = &obd->u.lmv;
2336 struct lmv_tgt_desc *tgt;
2340 tgt = lmv_find_target(lmv, fid);
2342 RETURN(PTR_ERR(tgt));
2344 rc = md_fsync(tgt->ltd_exp, fid, request);
2348 struct stripe_dirent {
2349 struct page *sd_page;
2350 struct lu_dirpage *sd_dp;
2351 struct lu_dirent *sd_ent;
2355 struct lmv_dir_ctxt {
2356 struct lmv_obd *ldc_lmv;
2357 struct md_op_data *ldc_op_data;
2358 struct md_callback *ldc_cb_op;
2361 struct stripe_dirent ldc_stripes[0];
2364 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
2366 if (stripe->sd_page) {
2367 kunmap(stripe->sd_page);
2368 put_page(stripe->sd_page);
2369 stripe->sd_page = NULL;
2370 stripe->sd_ent = NULL;
2374 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2378 for (i = 0; i < ctxt->ldc_count; i++)
2379 stripe_dirent_unload(&ctxt->ldc_stripes[i]);
2382 /* if @ent is dummy, or . .., get next */
2383 static struct lu_dirent *stripe_dirent_get(struct lmv_dir_ctxt *ctxt,
2384 struct lu_dirent *ent,
2387 for (; ent; ent = lu_dirent_next(ent)) {
2388 /* Skip dummy entry */
2389 if (le16_to_cpu(ent->lde_namelen) == 0)
2392 /* skip . and .. for other stripes */
2394 (strncmp(ent->lde_name, ".",
2395 le16_to_cpu(ent->lde_namelen)) == 0 ||
2396 strncmp(ent->lde_name, "..",
2397 le16_to_cpu(ent->lde_namelen)) == 0))
2400 if (le64_to_cpu(ent->lde_hash) >= ctxt->ldc_hash)
2407 static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
2408 struct stripe_dirent *stripe,
2411 struct md_op_data *op_data = ctxt->ldc_op_data;
2412 struct lmv_oinfo *oinfo;
2413 struct lu_fid fid = op_data->op_fid1;
2414 struct inode *inode = op_data->op_data;
2415 struct lmv_tgt_desc *tgt;
2416 struct lu_dirent *ent = stripe->sd_ent;
2417 __u64 hash = ctxt->ldc_hash;
2422 LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2426 if (stripe->sd_page) {
2427 __u64 end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2429 /* @hash should be the last dirent hash */
2430 LASSERTF(hash <= end,
2431 "ctxt@%p stripe@%p hash %llx end %llx\n",
2432 ctxt, stripe, hash, end);
2433 /* unload last page */
2434 stripe_dirent_unload(stripe);
2436 if (end == MDS_DIR_END_OFF) {
2437 stripe->sd_eof = true;
2443 oinfo = &op_data->op_mea1->lsm_md_oinfo[stripe_index];
2444 if (!oinfo->lmo_root) {
2449 tgt = lmv_get_target(ctxt->ldc_lmv, oinfo->lmo_mds, NULL);
2455 /* op_data is shared by stripes, reset after use */
2456 op_data->op_fid1 = oinfo->lmo_fid;
2457 op_data->op_fid2 = oinfo->lmo_fid;
2458 op_data->op_data = oinfo->lmo_root;
2460 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_cb_op, hash,
2463 op_data->op_fid1 = fid;
2464 op_data->op_fid2 = fid;
2465 op_data->op_data = inode;
2470 stripe->sd_dp = page_address(stripe->sd_page);
2471 ent = stripe_dirent_get(ctxt, lu_dirent_start(stripe->sd_dp),
2473 /* in case a page filled with ., .. and dummy, read next */
2476 stripe->sd_ent = ent;
2479 /* treat error as eof, so dir can be partially accessed */
2480 stripe->sd_eof = true;
2481 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, "
2482 "directory is partially accessed!\n",
2483 PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
2490 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
2492 struct obd_device *obd = exp->exp_obd;
2493 struct lmv_obd *lmv = &obd->u.lmv;
2494 struct lmv_tgt_desc *tgt;
2498 rc = lmv_check_connect(obd);
2502 tgt = lmv_find_target(lmv, &data->op_fid1);
2504 RETURN(PTR_ERR(tgt));
2506 data->op_flags |= MF_MDC_CANCEL_FID1;
2507 rc = md_file_resync(tgt->ltd_exp, data);
2512 * Get dirent with the closest hash for striped directory
2514 * This function will search the dir entry, whose hash value is the
2515 * closest(>=) to hash from all of sub-stripes, and it is only being called
2516 * for striped directory.
2518 * \param[in] ctxt dir read context
2520 * \retval dirent get the entry successfully
2521 * NULL does not get the entry, normally it means
2522 * it reaches the end of the directory, while read
2523 * stripe dirent error is ignored to allow partial
2526 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
2528 struct stripe_dirent *stripe;
2529 struct lu_dirent *ent = NULL;
2533 /* TODO: optimize with k-way merge sort */
2534 for (i = 0; i < ctxt->ldc_count; i++) {
2535 stripe = &ctxt->ldc_stripes[i];
2539 if (!stripe->sd_ent) {
2540 stripe_dirent_load(ctxt, stripe, i);
2541 if (!stripe->sd_ent) {
2542 LASSERT(stripe->sd_eof);
2548 le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
2549 le64_to_cpu(stripe->sd_ent->lde_hash)) {
2551 if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
2558 stripe = &ctxt->ldc_stripes[min];
2559 ent = stripe->sd_ent;
2560 /* pop found dirent */
2561 stripe->sd_ent = stripe_dirent_get(ctxt, lu_dirent_next(ent),
2569 * Build dir entry page for striped directory
2571 * This function gets one entry by @offset from a striped directory. It will
2572 * read entries from all of stripes, and choose one closest to the required
2573 * offset(&offset). A few notes
2574 * 1. skip . and .. for non-zero stripes, because there can only have one .
2575 * and .. in a directory.
2576 * 2. op_data will be shared by all of stripes, instead of allocating new
2577 * one, so need to restore before reusing.
2579 * \param[in] exp obd export refer to LMV
2580 * \param[in] op_data hold those MD parameters of read_entry
2581 * \param[in] cb_op ldlm callback being used in enqueue in mdc_read_entry
2582 * \param[in] offset starting hash offset
2583 * \param[out] ppage the page holding the entry. Note: because the entry
2584 * will be accessed in upper layer, so we need hold the
2585 * page until the usages of entry is finished, see
2586 * ll_dir_entry_next.
2588 * retval =0 if get entry successfully
2589 * <0 cannot get entry
2591 static int lmv_striped_read_page(struct obd_export *exp,
2592 struct md_op_data *op_data,
2593 struct md_callback *cb_op,
2594 __u64 offset, struct page **ppage)
2596 struct page *page = NULL;
2597 struct lu_dirpage *dp;
2599 struct lu_dirent *ent;
2600 struct lu_dirent *last_ent;
2602 struct lmv_dir_ctxt *ctxt;
2603 struct lu_dirent *next = NULL;
2609 /* Allocate a page and read entries from all of stripes and fill
2610 * the page by hash order */
2611 page = alloc_page(GFP_KERNEL);
2615 /* Initialize the entry page */
2617 memset(dp, 0, sizeof(*dp));
2618 dp->ldp_hash_start = cpu_to_le64(offset);
2621 left_bytes = PAGE_SIZE - sizeof(*dp);
2625 /* initalize dir read context */
2626 stripe_count = op_data->op_mea1->lsm_md_stripe_count;
2627 OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2629 GOTO(free_page, rc = -ENOMEM);
2630 ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
2631 ctxt->ldc_op_data = op_data;
2632 ctxt->ldc_cb_op = cb_op;
2633 ctxt->ldc_hash = offset;
2634 ctxt->ldc_count = stripe_count;
2637 next = lmv_dirent_next(ctxt);
2639 /* end of directory */
2641 ctxt->ldc_hash = MDS_DIR_END_OFF;
2644 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
2646 ent_size = le16_to_cpu(next->lde_reclen);
2648 /* the last entry lde_reclen is 0, but it might not be the last
2649 * one of this temporay dir page */
2651 ent_size = lu_dirent_calc_size(
2652 le16_to_cpu(next->lde_namelen),
2653 le32_to_cpu(next->lde_attrs));
2655 if (ent_size > left_bytes)
2658 memcpy(ent, next, ent_size);
2660 /* Replace . with master FID and Replace .. with the parent FID
2661 * of master object */
2662 if (strncmp(ent->lde_name, ".",
2663 le16_to_cpu(ent->lde_namelen)) == 0 &&
2664 le16_to_cpu(ent->lde_namelen) == 1)
2665 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
2666 else if (strncmp(ent->lde_name, "..",
2667 le16_to_cpu(ent->lde_namelen)) == 0 &&
2668 le16_to_cpu(ent->lde_namelen) == 2)
2669 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2671 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
2672 le16_to_cpu(ent->lde_namelen), ent->lde_name,
2673 le64_to_cpu(ent->lde_hash));
2675 left_bytes -= ent_size;
2676 ent->lde_reclen = cpu_to_le16(ent_size);
2678 ent = (void *)ent + ent_size;
2681 last_ent->lde_reclen = 0;
2684 dp->ldp_flags |= LDF_EMPTY;
2685 else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
2686 dp->ldp_flags |= LDF_COLLIDE;
2687 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2688 dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
2690 put_lmv_dir_ctxt(ctxt);
2691 OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2704 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2705 struct md_callback *cb_op, __u64 offset,
2706 struct page **ppage)
2708 struct obd_device *obd = exp->exp_obd;
2709 struct lmv_obd *lmv = &obd->u.lmv;
2710 struct lmv_stripe_md *lsm = op_data->op_mea1;
2711 struct lmv_tgt_desc *tgt;
2715 if (unlikely(lsm != NULL)) {
2716 /* foreign dir is not striped dir */
2717 if (lsm->lsm_md_magic == LMV_MAGIC_FOREIGN)
2720 rc = lmv_striped_read_page(exp, op_data, cb_op, offset, ppage);
2724 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2726 RETURN(PTR_ERR(tgt));
2728 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2734 * Unlink a file/directory
2736 * Unlink a file or directory under the parent dir. The unlink request
2737 * usually will be sent to the MDT where the child is located, but if
2738 * the client does not have the child FID then request will be sent to the
2739 * MDT where the parent is located.
2741 * If the parent is a striped directory then it also needs to locate which
2742 * stripe the name of the child is located, and replace the parent FID
2743 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2744 * it will walk through all of sub-stripes until the child is being
2747 * \param[in] exp export refer to LMV
2748 * \param[in] op_data different parameters transferred beween client
2749 * MD stacks, name, namelen, FIDs etc.
2750 * op_fid1 is the parent FID, op_fid2 is the child
2752 * \param[out] request point to the request of unlink.
2754 * retval 0 if succeed
2755 * negative errno if failed.
2757 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2758 struct ptlrpc_request **request)
2760 struct obd_device *obd = exp->exp_obd;
2761 struct lmv_obd *lmv = &obd->u.lmv;
2762 struct lmv_tgt_desc *tgt;
2763 struct lmv_tgt_desc *parent_tgt;
2764 struct mdt_body *body;
2769 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2770 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2771 op_data->op_cap = cfs_curproc_cap_pack();
2774 parent_tgt = lmv_locate_tgt(lmv, op_data, &op_data->op_fid1);
2775 if (IS_ERR(parent_tgt))
2776 RETURN(PTR_ERR(parent_tgt));
2778 if (likely(!fid_is_zero(&op_data->op_fid2))) {
2779 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2781 RETURN(PTR_ERR(tgt));
2787 * If child's fid is given, cancel unused locks for it if it is from
2788 * another export than parent.
2790 * LOOKUP lock for child (fid3) should also be cancelled on parent
2791 * tgt_tgt in mdc_unlink().
2793 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2795 if (parent_tgt != tgt)
2796 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2797 LCK_EX, MDS_INODELOCK_LOOKUP,
2798 MF_MDC_CANCEL_FID3);
2800 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2801 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2805 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2806 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2808 rc = md_unlink(tgt->ltd_exp, op_data, request);
2809 if (rc == -ENOENT && lmv_dir_retry_check_update(op_data)) {
2810 ptlrpc_req_finished(*request);
2818 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2822 /* Not cross-ref case, just get out of here. */
2823 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2826 /* This is a remote object, try remote MDT. */
2827 op_data->op_fid2 = body->mbo_fid1;
2828 ptlrpc_req_finished(*request);
2831 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2833 RETURN(PTR_ERR(tgt));
2838 static int lmv_precleanup(struct obd_device *obd)
2841 libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
2842 fld_client_debugfs_fini(&obd->u.lmv.lmv_fld);
2843 lprocfs_obd_cleanup(obd);
2844 lprocfs_free_md_stats(obd);
2849 * Get by key a value associated with a LMV device.
2851 * Dispatch request to lower-layer devices as needed.
2853 * \param[in] env execution environment for this thread
2854 * \param[in] exp export for the LMV device
2855 * \param[in] keylen length of key identifier
2856 * \param[in] key identifier of key to get value for
2857 * \param[in] vallen size of \a val
2858 * \param[out] val pointer to storage location for value
2859 * \param[in] lsm optional striping metadata of object
2861 * \retval 0 on success
2862 * \retval negative negated errno on failure
2864 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2865 __u32 keylen, void *key, __u32 *vallen, void *val)
2867 struct obd_device *obd;
2868 struct lmv_obd *lmv;
2872 obd = class_exp2obd(exp);
2874 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2875 exp->exp_handle.h_cookie);
2880 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2883 LASSERT(*vallen == sizeof(__u32));
2884 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2885 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2887 * All tgts should be connected when this gets called.
2889 if (tgt == NULL || tgt->ltd_exp == NULL)
2892 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2897 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2898 KEY_IS(KEY_DEFAULT_EASIZE) ||
2899 KEY_IS(KEY_CONN_DATA)) {
2901 * Forwarding this request to first MDS, it should know LOV
2904 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2906 if (!rc && KEY_IS(KEY_CONN_DATA))
2907 exp->exp_connect_data = *(struct obd_connect_data *)val;
2909 } else if (KEY_IS(KEY_TGT_COUNT)) {
2910 *((int *)val) = lmv->desc.ld_tgt_count;
2914 CDEBUG(D_IOCTL, "Invalid key\n");
2919 * Asynchronously set by key a value associated with a LMV device.
2921 * Dispatch request to lower-layer devices as needed.
2923 * \param[in] env execution environment for this thread
2924 * \param[in] exp export for the LMV device
2925 * \param[in] keylen length of key identifier
2926 * \param[in] key identifier of key to store value for
2927 * \param[in] vallen size of value to store
2928 * \param[in] val pointer to data to be stored
2929 * \param[in] set optional list of related ptlrpc requests
2931 * \retval 0 on success
2932 * \retval negative negated errno on failure
2934 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2935 __u32 keylen, void *key, __u32 vallen, void *val,
2936 struct ptlrpc_request_set *set)
2938 struct lmv_tgt_desc *tgt = NULL;
2939 struct obd_device *obd;
2940 struct lmv_obd *lmv;
2944 obd = class_exp2obd(exp);
2946 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2947 exp->exp_handle.h_cookie);
2952 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2953 KEY_IS(KEY_DEFAULT_EASIZE)) {
2956 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2959 if (tgt == NULL || tgt->ltd_exp == NULL)
2962 err = obd_set_info_async(env, tgt->ltd_exp,
2963 keylen, key, vallen, val, set);
2974 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2975 const struct lmv_mds_md_v1 *lmm1)
2977 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2984 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2985 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2986 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2987 if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2988 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2990 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2991 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2992 lsm->lsm_md_migrate_offset = le32_to_cpu(lmm1->lmv_migrate_offset);
2993 lsm->lsm_md_migrate_hash = le32_to_cpu(lmm1->lmv_migrate_hash);
2994 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2995 sizeof(lsm->lsm_md_pool_name));
2997 if (cplen >= sizeof(lsm->lsm_md_pool_name))
3000 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %#x "
3001 "layout_version %d\n", lsm->lsm_md_stripe_count,
3002 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
3003 lsm->lsm_md_layout_version);
3005 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
3006 for (i = 0; i < stripe_count; i++) {
3007 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
3008 &lmm1->lmv_stripe_fids[i]);
3010 * set default value -1, so lmv_locate_tgt() knows this stripe
3011 * target is not initialized.
3013 lsm->lsm_md_oinfo[i].lmo_mds = (u32)-1;
3014 if (!fid_is_sane(&lsm->lsm_md_oinfo[i].lmo_fid))
3017 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
3018 &lsm->lsm_md_oinfo[i].lmo_mds);
3025 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
3026 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
3032 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
3033 const union lmv_mds_md *lmm, size_t lmm_size)
3035 struct lmv_stripe_md *lsm;
3038 bool allocated = false;
3041 LASSERT(lsmp != NULL);
3045 if (lsm != NULL && lmm == NULL) {
3047 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lsm;
3049 if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
3052 lfm_size = lfm->lfm_length + offsetof(typeof(*lfm),
3054 OBD_FREE_LARGE(lfm, lfm_size);
3058 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3059 if (lsm->lsm_md_oinfo[i].lmo_root)
3060 iput(lsm->lsm_md_oinfo[i].lmo_root);
3062 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
3063 OBD_FREE(lsm, lsm_size);
3068 /* foreign lmv case */
3069 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_FOREIGN) {
3070 struct lmv_foreign_md *lfm = (struct lmv_foreign_md *)lsm;
3073 OBD_ALLOC_LARGE(lfm, lmm_size);
3076 *lsmp = (struct lmv_stripe_md *)lfm;
3078 lfm->lfm_magic = le32_to_cpu(lmm->lmv_foreign_md.lfm_magic);
3079 lfm->lfm_length = le32_to_cpu(lmm->lmv_foreign_md.lfm_length);
3080 lfm->lfm_type = le32_to_cpu(lmm->lmv_foreign_md.lfm_type);
3081 lfm->lfm_flags = le32_to_cpu(lmm->lmv_foreign_md.lfm_flags);
3082 memcpy(&lfm->lfm_value, &lmm->lmv_foreign_md.lfm_value,
3087 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
3091 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
3092 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
3093 CERROR("%s: invalid lmv magic %x: rc = %d\n",
3094 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
3099 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
3100 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
3103 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
3104 * stripecount should be 0 then.
3106 lsm_size = lmv_stripe_md_size(0);
3108 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
3110 OBD_ALLOC(lsm, lsm_size);
3117 switch (le32_to_cpu(lmm->lmv_magic)) {
3119 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
3122 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3123 le32_to_cpu(lmm->lmv_magic));
3128 if (rc != 0 && allocated) {
3129 OBD_FREE(lsm, lsm_size);
3136 void lmv_free_memmd(struct lmv_stripe_md *lsm)
3138 lmv_unpackmd(NULL, &lsm, NULL, 0);
3140 EXPORT_SYMBOL(lmv_free_memmd);
3142 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3143 union ldlm_policy_data *policy,
3144 enum ldlm_mode mode, enum ldlm_cancel_flags flags,
3147 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3152 LASSERT(fid != NULL);
3154 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3155 struct lmv_tgt_desc *tgt = lmv->tgts[i];
3158 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3161 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3169 static int lmv_set_lock_data(struct obd_export *exp,
3170 const struct lustre_handle *lockh,
3171 void *data, __u64 *bits)
3173 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3174 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3178 if (tgt == NULL || tgt->ltd_exp == NULL)
3180 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3184 enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
3185 const struct lu_fid *fid, enum ldlm_type type,
3186 union ldlm_policy_data *policy,
3187 enum ldlm_mode mode, struct lustre_handle *lockh)
3189 struct obd_device *obd = exp->exp_obd;
3190 struct lmv_obd *lmv = &obd->u.lmv;
3196 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
3199 * With DNE every object can have two locks in different namespaces:
3200 * lookup lock in space of MDT storing direntry and update/open lock in
3201 * space of MDT storing inode. Try the MDT that the FID maps to first,
3202 * since this can be easily found, and only try others if that fails.
3204 for (i = 0, tgt = lmv_find_target_index(lmv, fid);
3205 i < lmv->desc.ld_tgt_count;
3206 i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
3208 CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
3209 obd->obd_name, PFID(fid), tgt);
3213 if (lmv->tgts[tgt] == NULL ||
3214 lmv->tgts[tgt]->ltd_exp == NULL ||
3215 lmv->tgts[tgt]->ltd_active == 0)
3218 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
3219 type, policy, mode, lockh);
3227 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
3228 struct obd_export *dt_exp, struct obd_export *md_exp,
3229 struct lustre_md *md)
3231 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3232 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3234 if (tgt == NULL || tgt->ltd_exp == NULL)
3237 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
3240 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3242 struct obd_device *obd = exp->exp_obd;
3243 struct lmv_obd *lmv = &obd->u.lmv;
3244 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3247 if (md->lmv != NULL) {
3248 lmv_free_memmd(md->lmv);
3251 if (tgt == NULL || tgt->ltd_exp == NULL)
3253 RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
3256 int lmv_set_open_replay_data(struct obd_export *exp,
3257 struct obd_client_handle *och,
3258 struct lookup_intent *it)
3260 struct obd_device *obd = exp->exp_obd;
3261 struct lmv_obd *lmv = &obd->u.lmv;
3262 struct lmv_tgt_desc *tgt;
3265 tgt = lmv_find_target(lmv, &och->och_fid);
3267 RETURN(PTR_ERR(tgt));
3269 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3272 int lmv_clear_open_replay_data(struct obd_export *exp,
3273 struct obd_client_handle *och)
3275 struct obd_device *obd = exp->exp_obd;
3276 struct lmv_obd *lmv = &obd->u.lmv;
3277 struct lmv_tgt_desc *tgt;
3280 tgt = lmv_find_target(lmv, &och->och_fid);
3282 RETURN(PTR_ERR(tgt));
3284 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3287 int lmv_intent_getattr_async(struct obd_export *exp,
3288 struct md_enqueue_info *minfo)
3290 struct md_op_data *op_data = &minfo->mi_data;
3291 struct obd_device *obd = exp->exp_obd;
3292 struct lmv_obd *lmv = &obd->u.lmv;
3293 struct lmv_tgt_desc *tgt = NULL;
3297 if (!fid_is_sane(&op_data->op_fid2))
3300 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3302 RETURN(PTR_ERR(tgt));
3305 * no special handle for remote dir, which needs to fetch both LOOKUP
3306 * lock on parent, and then UPDATE lock on child MDT, which makes all
3307 * complicated because this is done async. So only LOOKUP lock is
3308 * fetched for remote dir, but considering remote dir is rare case,
3309 * and not supporting it in statahead won't cause any issue, just leave
3313 rc = md_intent_getattr_async(tgt->ltd_exp, minfo);
3317 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3318 struct lu_fid *fid, __u64 *bits)
3320 struct obd_device *obd = exp->exp_obd;
3321 struct lmv_obd *lmv = &obd->u.lmv;
3322 struct lmv_tgt_desc *tgt;
3326 tgt = lmv_find_target(lmv, fid);
3328 RETURN(PTR_ERR(tgt));
3330 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3334 int lmv_get_fid_from_lsm(struct obd_export *exp,
3335 const struct lmv_stripe_md *lsm,
3336 const char *name, int namelen, struct lu_fid *fid)
3338 const struct lmv_oinfo *oinfo;
3340 LASSERT(lsm != NULL);
3341 oinfo = lsm_name_to_stripe_info(lsm, name, namelen, false);
3343 return PTR_ERR(oinfo);
3345 *fid = oinfo->lmo_fid;
3351 * For lmv, only need to send request to master MDT, and the master MDT will
3352 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3353 * we directly fetch data from the slave MDTs.
3355 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3356 struct obd_quotactl *oqctl)
3358 struct obd_device *obd = class_exp2obd(exp);
3359 struct lmv_obd *lmv = &obd->u.lmv;
3360 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3363 __u64 curspace, curinodes;
3367 tgt->ltd_exp == NULL ||
3369 lmv->desc.ld_tgt_count == 0) {
3370 CERROR("master lmv inactive\n");
3374 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3375 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3379 curspace = curinodes = 0;
3380 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3384 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3387 err = obd_quotactl(tgt->ltd_exp, oqctl);
3389 CERROR("getquota on mdt %d failed. %d\n", i, err);
3393 curspace += oqctl->qc_dqblk.dqb_curspace;
3394 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3397 oqctl->qc_dqblk.dqb_curspace = curspace;
3398 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3403 static int lmv_merge_attr(struct obd_export *exp,
3404 const struct lmv_stripe_md *lsm,
3405 struct cl_attr *attr,
3406 ldlm_blocking_callback cb_blocking)
3411 /* foreign dir is not striped dir */
3412 if (lsm->lsm_md_magic == LMV_MAGIC_FOREIGN)
3415 rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3419 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3420 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3426 "" DFID " size %llu, blocks %llu nlink %u, atime %lld ctime %lld, mtime %lld.\n",
3427 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3428 i_size_read(inode), (unsigned long long)inode->i_blocks,
3429 inode->i_nlink, (s64)inode->i_atime.tv_sec,
3430 (s64)inode->i_ctime.tv_sec, (s64)inode->i_mtime.tv_sec);
3432 /* for slave stripe, it needs to subtract nlink for . and .. */
3434 attr->cat_nlink += inode->i_nlink - 2;
3436 attr->cat_nlink = inode->i_nlink;
3438 attr->cat_size += i_size_read(inode);
3439 attr->cat_blocks += inode->i_blocks;
3441 if (attr->cat_atime < inode->i_atime.tv_sec)
3442 attr->cat_atime = inode->i_atime.tv_sec;
3444 if (attr->cat_ctime < inode->i_ctime.tv_sec)
3445 attr->cat_ctime = inode->i_ctime.tv_sec;
3447 if (attr->cat_mtime < inode->i_mtime.tv_sec)
3448 attr->cat_mtime = inode->i_mtime.tv_sec;
3453 struct obd_ops lmv_obd_ops = {
3454 .o_owner = THIS_MODULE,
3455 .o_setup = lmv_setup,
3456 .o_cleanup = lmv_cleanup,
3457 .o_precleanup = lmv_precleanup,
3458 .o_process_config = lmv_process_config,
3459 .o_connect = lmv_connect,
3460 .o_disconnect = lmv_disconnect,
3461 .o_statfs = lmv_statfs,
3462 .o_get_info = lmv_get_info,
3463 .o_set_info_async = lmv_set_info_async,
3464 .o_notify = lmv_notify,
3465 .o_get_uuid = lmv_get_uuid,
3466 .o_iocontrol = lmv_iocontrol,
3467 .o_quotactl = lmv_quotactl
3470 struct md_ops lmv_md_ops = {
3471 .m_get_root = lmv_get_root,
3472 .m_null_inode = lmv_null_inode,
3473 .m_close = lmv_close,
3474 .m_create = lmv_create,
3475 .m_enqueue = lmv_enqueue,
3476 .m_getattr = lmv_getattr,
3477 .m_getxattr = lmv_getxattr,
3478 .m_getattr_name = lmv_getattr_name,
3479 .m_intent_lock = lmv_intent_lock,
3481 .m_rename = lmv_rename,
3482 .m_setattr = lmv_setattr,
3483 .m_setxattr = lmv_setxattr,
3484 .m_fsync = lmv_fsync,
3485 .m_file_resync = lmv_file_resync,
3486 .m_read_page = lmv_read_page,
3487 .m_unlink = lmv_unlink,
3488 .m_init_ea_size = lmv_init_ea_size,
3489 .m_cancel_unused = lmv_cancel_unused,
3490 .m_set_lock_data = lmv_set_lock_data,
3491 .m_lock_match = lmv_lock_match,
3492 .m_get_lustre_md = lmv_get_lustre_md,
3493 .m_free_lustre_md = lmv_free_lustre_md,
3494 .m_merge_attr = lmv_merge_attr,
3495 .m_set_open_replay_data = lmv_set_open_replay_data,
3496 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3497 .m_intent_getattr_async = lmv_intent_getattr_async,
3498 .m_revalidate_lock = lmv_revalidate_lock,
3499 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
3500 .m_unpackmd = lmv_unpackmd,
3503 static int __init lmv_init(void)
3505 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3506 LUSTRE_LMV_NAME, NULL);
3509 static void __exit lmv_exit(void)
3511 class_unregister_type(LUSTRE_LMV_NAME);
3514 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3515 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3516 MODULE_VERSION(LUSTRE_VERSION_STRING);
3517 MODULE_LICENSE("GPL");
3519 module_init(lmv_init);
3520 module_exit(lmv_exit);