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;
216 lmv->lmv_tgts_kobj = kobject_create_and_add("target_obds",
217 &obd->obd_kset.kobj);
218 if (!lmv->lmv_tgts_kobj) {
219 CERROR("%s: cannot create /sys/fs/lustre/%s/%s/target_obds\n",
220 obd->obd_name, obd->obd_type->typ_name, obd->obd_name);
223 rc = lmv_check_connect(obd);
232 if (lmv->lmv_tgts_kobj)
233 kobject_put(lmv->lmv_tgts_kobj);
235 class_disconnect(exp);
240 static int lmv_init_ea_size(struct obd_export *exp, __u32 easize,
243 struct obd_device *obd = exp->exp_obd;
244 struct lmv_obd *lmv = &obd->u.lmv;
250 if (lmv->max_easize < easize) {
251 lmv->max_easize = easize;
254 if (lmv->max_def_easize < def_easize) {
255 lmv->max_def_easize = def_easize;
262 if (lmv->connected == 0)
265 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
266 struct lmv_tgt_desc *tgt = lmv->tgts[i];
268 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active) {
269 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
273 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
275 CERROR("%s: obd_init_ea_size() failed on MDT target %d:"
276 " rc = %d\n", obd->obd_name, i, rc);
283 #define MAX_STRING_SIZE 128
285 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
287 struct lmv_obd *lmv = &obd->u.lmv;
288 struct obd_device *mdc_obd;
289 struct obd_export *mdc_exp;
290 struct lu_fld_target target;
294 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
297 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
301 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s\n",
302 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
303 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid);
305 if (!mdc_obd->obd_set_up) {
306 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
310 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &obd->obd_uuid,
311 &lmv->conn_data, NULL);
313 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
318 * Init fid sequence client for this mdc and add new fld target.
320 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
324 target.ft_srv = NULL;
325 target.ft_exp = mdc_exp;
326 target.ft_idx = tgt->ltd_idx;
328 fld_client_add_target(&lmv->lmv_fld, &target);
330 rc = obd_register_observer(mdc_obd, obd);
332 obd_disconnect(mdc_exp);
333 CERROR("target %s register_observer error %d\n",
334 tgt->ltd_uuid.uuid, rc);
338 if (obd->obd_observer) {
340 * Tell the observer about the new target.
342 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
345 obd_disconnect(mdc_exp);
351 tgt->ltd_exp = mdc_exp;
352 lmv->desc.ld_active_tgt_count++;
354 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
356 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
357 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
358 atomic_read(&obd->obd_refcount));
360 if (lmv->lmv_tgts_kobj)
361 /* Even if we failed to create the link, that's fine */
362 rc = sysfs_create_link(lmv->lmv_tgts_kobj,
363 &mdc_obd->obd_kset.kobj,
368 static void lmv_del_target(struct lmv_obd *lmv, int index)
370 if (lmv->tgts[index] == NULL)
373 OBD_FREE_PTR(lmv->tgts[index]);
374 lmv->tgts[index] = NULL;
378 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
379 __u32 index, int gen)
381 struct obd_device *mdc_obd;
382 struct lmv_obd *lmv = &obd->u.lmv;
383 struct lmv_tgt_desc *tgt;
384 int orig_tgt_count = 0;
388 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
389 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
392 CERROR("%s: Target %s not attached: rc = %d\n",
393 obd->obd_name, uuidp->uuid, -EINVAL);
397 mutex_lock(&lmv->lmv_init_mutex);
398 if ((index < lmv->tgts_size) && (lmv->tgts[index] != NULL)) {
399 tgt = lmv->tgts[index];
400 CERROR("%s: UUID %s already assigned at LMV target index %d:"
401 " rc = %d\n", obd->obd_name,
402 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
403 mutex_unlock(&lmv->lmv_init_mutex);
407 if (index >= lmv->tgts_size) {
408 /* We need to reallocate the lmv target array. */
409 struct lmv_tgt_desc **newtgts, **old = NULL;
413 while (newsize < index + 1)
414 newsize = newsize << 1;
415 OBD_ALLOC(newtgts, sizeof(*newtgts) * newsize);
416 if (newtgts == NULL) {
417 mutex_unlock(&lmv->lmv_init_mutex);
421 if (lmv->tgts_size) {
422 memcpy(newtgts, lmv->tgts,
423 sizeof(*newtgts) * lmv->tgts_size);
425 oldsize = lmv->tgts_size;
429 lmv->tgts_size = newsize;
432 OBD_FREE(old, sizeof(*old) * oldsize);
434 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
440 mutex_unlock(&lmv->lmv_init_mutex);
444 mutex_init(&tgt->ltd_fid_mutex);
445 tgt->ltd_idx = index;
446 tgt->ltd_uuid = *uuidp;
448 lmv->tgts[index] = tgt;
449 if (index >= lmv->desc.ld_tgt_count) {
450 orig_tgt_count = lmv->desc.ld_tgt_count;
451 lmv->desc.ld_tgt_count = index + 1;
454 if (lmv->connected == 0) {
455 /* lmv_check_connect() will connect this target. */
456 mutex_unlock(&lmv->lmv_init_mutex);
460 /* Otherwise let's connect it ourselves */
461 mutex_unlock(&lmv->lmv_init_mutex);
462 rc = lmv_connect_mdc(obd, tgt);
464 spin_lock(&lmv->lmv_lock);
465 if (lmv->desc.ld_tgt_count == index + 1)
466 lmv->desc.ld_tgt_count = orig_tgt_count;
467 memset(tgt, 0, sizeof(*tgt));
468 spin_unlock(&lmv->lmv_lock);
470 int easize = sizeof(struct lmv_stripe_md) +
471 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
472 lmv_init_ea_size(obd->obd_self_export, easize, 0);
478 static int lmv_check_connect(struct obd_device *obd)
480 struct lmv_obd *lmv = &obd->u.lmv;
481 struct lmv_tgt_desc *tgt;
490 mutex_lock(&lmv->lmv_init_mutex);
491 if (lmv->connected) {
492 mutex_unlock(&lmv->lmv_init_mutex);
496 if (lmv->desc.ld_tgt_count == 0) {
497 mutex_unlock(&lmv->lmv_init_mutex);
498 CERROR("%s: no targets configured.\n", obd->obd_name);
502 LASSERT(lmv->tgts != NULL);
504 if (lmv->tgts[0] == NULL) {
505 mutex_unlock(&lmv->lmv_init_mutex);
506 CERROR("%s: no target configured for index 0.\n",
511 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
512 obd->obd_uuid.uuid, obd->obd_name);
514 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
518 rc = lmv_connect_mdc(obd, tgt);
524 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
525 lmv_init_ea_size(obd->obd_self_export, easize, 0);
526 mutex_unlock(&lmv->lmv_init_mutex);
537 --lmv->desc.ld_active_tgt_count;
538 rc2 = obd_disconnect(tgt->ltd_exp);
540 CERROR("LMV target %s disconnect on "
541 "MDC idx %d: error %d\n",
542 tgt->ltd_uuid.uuid, i, rc2);
547 mutex_unlock(&lmv->lmv_init_mutex);
552 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
554 struct lmv_obd *lmv = &obd->u.lmv;
555 struct obd_device *mdc_obd;
559 LASSERT(tgt != NULL);
560 LASSERT(obd != NULL);
562 mdc_obd = class_exp2obd(tgt->ltd_exp);
565 mdc_obd->obd_force = obd->obd_force;
566 mdc_obd->obd_fail = obd->obd_fail;
567 mdc_obd->obd_no_recov = obd->obd_no_recov;
569 if (lmv->lmv_tgts_kobj)
570 sysfs_remove_link(lmv->lmv_tgts_kobj,
574 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
576 CERROR("Can't finanize fids factory\n");
578 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
579 tgt->ltd_exp->exp_obd->obd_name,
580 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
582 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
583 rc = obd_disconnect(tgt->ltd_exp);
585 if (tgt->ltd_active) {
586 CERROR("Target %s disconnect error %d\n",
587 tgt->ltd_uuid.uuid, rc);
591 lmv_activate_target(lmv, tgt, 0);
596 static int lmv_disconnect(struct obd_export *exp)
598 struct obd_device *obd = class_exp2obd(exp);
599 struct lmv_obd *lmv = &obd->u.lmv;
607 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
608 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
611 lmv_disconnect_mdc(obd, lmv->tgts[i]);
614 if (lmv->lmv_tgts_kobj)
615 kobject_put(lmv->lmv_tgts_kobj);
619 * This is the case when no real connection is established by
620 * lmv_check_connect().
623 class_export_put(exp);
624 rc = class_disconnect(exp);
630 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
633 struct obd_device *obddev = class_exp2obd(exp);
634 struct lmv_obd *lmv = &obddev->u.lmv;
635 struct getinfo_fid2path *gf;
636 struct lmv_tgt_desc *tgt;
637 struct getinfo_fid2path *remote_gf = NULL;
638 struct lu_fid root_fid;
639 int remote_gf_size = 0;
643 tgt = lmv_find_target(lmv, &gf->gf_fid);
645 RETURN(PTR_ERR(tgt));
647 root_fid = *gf->gf_u.gf_root_fid;
648 LASSERT(fid_is_sane(&root_fid));
651 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
652 if (rc != 0 && rc != -EREMOTE)
653 GOTO(out_fid2path, rc);
655 /* If remote_gf != NULL, it means just building the
656 * path on the remote MDT, copy this path segement to gf */
657 if (remote_gf != NULL) {
658 struct getinfo_fid2path *ori_gf;
661 ori_gf = (struct getinfo_fid2path *)karg;
662 if (strlen(ori_gf->gf_u.gf_path) + 1 +
663 strlen(gf->gf_u.gf_path) + 1 > ori_gf->gf_pathlen)
664 GOTO(out_fid2path, rc = -EOVERFLOW);
666 ptr = ori_gf->gf_u.gf_path;
668 memmove(ptr + strlen(gf->gf_u.gf_path) + 1, ptr,
669 strlen(ori_gf->gf_u.gf_path));
671 strncpy(ptr, gf->gf_u.gf_path,
672 strlen(gf->gf_u.gf_path));
673 ptr += strlen(gf->gf_u.gf_path);
677 CDEBUG(D_INFO, "%s: get path %s "DFID" rec: %llu ln: %u\n",
678 tgt->ltd_exp->exp_obd->obd_name,
679 gf->gf_u.gf_path, PFID(&gf->gf_fid), gf->gf_recno,
683 GOTO(out_fid2path, rc);
685 /* sigh, has to go to another MDT to do path building further */
686 if (remote_gf == NULL) {
687 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
688 OBD_ALLOC(remote_gf, remote_gf_size);
689 if (remote_gf == NULL)
690 GOTO(out_fid2path, rc = -ENOMEM);
691 remote_gf->gf_pathlen = PATH_MAX;
694 if (!fid_is_sane(&gf->gf_fid)) {
695 CERROR("%s: invalid FID "DFID": rc = %d\n",
696 tgt->ltd_exp->exp_obd->obd_name,
697 PFID(&gf->gf_fid), -EINVAL);
698 GOTO(out_fid2path, rc = -EINVAL);
701 tgt = lmv_find_target(lmv, &gf->gf_fid);
703 GOTO(out_fid2path, rc = -EINVAL);
705 remote_gf->gf_fid = gf->gf_fid;
706 remote_gf->gf_recno = -1;
707 remote_gf->gf_linkno = -1;
708 memset(remote_gf->gf_u.gf_path, 0, remote_gf->gf_pathlen);
709 *remote_gf->gf_u.gf_root_fid = root_fid;
711 goto repeat_fid2path;
714 if (remote_gf != NULL)
715 OBD_FREE(remote_gf, remote_gf_size);
719 static int lmv_hsm_req_count(struct lmv_obd *lmv,
720 const struct hsm_user_request *hur,
721 const struct lmv_tgt_desc *tgt_mds)
725 struct lmv_tgt_desc *curr_tgt;
727 /* count how many requests must be sent to the given target */
728 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
729 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
730 if (IS_ERR(curr_tgt))
731 RETURN(PTR_ERR(curr_tgt));
732 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
738 static int lmv_hsm_req_build(struct lmv_obd *lmv,
739 struct hsm_user_request *hur_in,
740 const struct lmv_tgt_desc *tgt_mds,
741 struct hsm_user_request *hur_out)
744 struct lmv_tgt_desc *curr_tgt;
746 /* build the hsm_user_request for the given target */
747 hur_out->hur_request = hur_in->hur_request;
749 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
750 curr_tgt = lmv_find_target(lmv,
751 &hur_in->hur_user_item[i].hui_fid);
752 if (IS_ERR(curr_tgt))
753 RETURN(PTR_ERR(curr_tgt));
754 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
755 hur_out->hur_user_item[nr_out] =
756 hur_in->hur_user_item[i];
760 hur_out->hur_request.hr_itemcount = nr_out;
761 memcpy(hur_data(hur_out), hur_data(hur_in),
762 hur_in->hur_request.hr_data_len);
767 static int lmv_hsm_ct_unregister(struct obd_device *obd, unsigned int cmd,
768 int len, struct lustre_kernelcomm *lk,
771 struct lmv_obd *lmv = &obd->u.lmv;
776 /* unregister request (call from llapi_hsm_copytool_fini) */
777 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
778 struct lmv_tgt_desc *tgt = lmv->tgts[i];
780 if (tgt == NULL || tgt->ltd_exp == NULL)
782 /* best effort: try to clean as much as possible
783 * (continue on error) */
784 obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
787 /* Whatever the result, remove copytool from kuc groups.
788 * Unreached coordinators will get EPIPE on next requests
789 * and will unregister automatically.
791 rc = libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
796 static int lmv_hsm_ct_register(struct obd_device *obd, unsigned int cmd,
797 int len, struct lustre_kernelcomm *lk,
800 struct lmv_obd *lmv = &obd->u.lmv;
804 bool any_set = false;
805 struct kkuc_ct_data kcd = {
806 .kcd_magic = KKUC_CT_DATA_MAGIC,
807 .kcd_archive = lk->lk_data,
812 filp = fget(lk->lk_wfd);
816 rc = libcfs_kkuc_group_add(filp, &obd->obd_uuid, lk->lk_uid,
817 lk->lk_group, &kcd, sizeof(kcd));
821 /* All or nothing: try to register to all MDS.
822 * In case of failure, unregister from previous MDS,
823 * except if it because of inactive target. */
824 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
825 struct lmv_tgt_desc *tgt = lmv->tgts[i];
827 if (tgt == NULL || tgt->ltd_exp == NULL)
830 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
832 if (tgt->ltd_active) {
833 /* permanent error */
834 CERROR("%s: iocontrol MDC %s on MDT"
835 " idx %d cmd %x: err = %d\n",
836 lmv2obd_dev(lmv)->obd_name,
837 tgt->ltd_uuid.uuid, i, cmd, err);
839 lk->lk_flags |= LK_FLG_STOP;
840 /* unregister from previous MDS */
841 for (j = 0; j < i; j++) {
843 if (tgt == NULL || tgt->ltd_exp == NULL)
845 obd_iocontrol(cmd, tgt->ltd_exp, len,
848 GOTO(err_kkuc_rem, rc);
850 /* else: transient error.
851 * kuc will register to the missing MDT
859 /* no registration done: return error */
860 GOTO(err_kkuc_rem, rc = -ENOTCONN);
865 libcfs_kkuc_group_rem(&obd->obd_uuid, lk->lk_uid, lk->lk_group);
875 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
876 int len, void *karg, void __user *uarg)
878 struct obd_device *obddev = class_exp2obd(exp);
879 struct lmv_obd *lmv = &obddev->u.lmv;
880 struct lmv_tgt_desc *tgt = NULL;
884 __u32 count = lmv->desc.ld_tgt_count;
891 case IOC_OBD_STATFS: {
892 struct obd_ioctl_data *data = karg;
893 struct obd_device *mdc_obd;
894 struct obd_statfs stat_buf = {0};
897 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
898 if ((index >= count))
901 tgt = lmv->tgts[index];
902 if (tgt == NULL || !tgt->ltd_active)
905 mdc_obd = class_exp2obd(tgt->ltd_exp);
910 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
911 min((int) data->ioc_plen2,
912 (int) sizeof(struct obd_uuid))))
915 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
916 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
920 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
921 min((int) data->ioc_plen1,
922 (int) sizeof(stat_buf))))
926 case OBD_IOC_QUOTACTL: {
927 struct if_quotactl *qctl = karg;
928 struct obd_quotactl *oqctl;
930 if (qctl->qc_valid == QC_MDTIDX) {
931 if (count <= qctl->qc_idx)
934 tgt = lmv->tgts[qctl->qc_idx];
935 if (tgt == NULL || tgt->ltd_exp == NULL)
937 } else if (qctl->qc_valid == QC_UUID) {
938 for (i = 0; i < count; i++) {
942 if (!obd_uuid_equals(&tgt->ltd_uuid,
946 if (tgt->ltd_exp == NULL)
958 LASSERT(tgt != NULL && tgt->ltd_exp != NULL);
959 OBD_ALLOC_PTR(oqctl);
963 QCTL_COPY(oqctl, qctl);
964 rc = obd_quotactl(tgt->ltd_exp, oqctl);
966 QCTL_COPY(qctl, oqctl);
967 qctl->qc_valid = QC_MDTIDX;
968 qctl->obd_uuid = tgt->ltd_uuid;
973 case LL_IOC_GET_CONNECT_FLAGS: {
975 if (tgt == NULL || tgt->ltd_exp == NULL)
977 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
980 case LL_IOC_FID2MDTIDX: {
981 struct lu_fid *fid = karg;
984 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
988 /* Note: this is from llite(see ll_dir_ioctl()), @uarg does not
989 * point to user space memory for FID2MDTIDX. */
990 *(__u32 *)uarg = mdt_index;
993 case OBD_IOC_FID2PATH: {
994 rc = lmv_fid2path(exp, len, karg, uarg);
997 case LL_IOC_HSM_STATE_GET:
998 case LL_IOC_HSM_STATE_SET:
999 case LL_IOC_HSM_ACTION: {
1000 struct md_op_data *op_data = karg;
1002 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1004 RETURN(PTR_ERR(tgt));
1006 if (tgt->ltd_exp == NULL)
1009 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1012 case LL_IOC_HSM_PROGRESS: {
1013 const struct hsm_progress_kernel *hpk = karg;
1015 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
1017 RETURN(PTR_ERR(tgt));
1018 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1021 case LL_IOC_HSM_REQUEST: {
1022 struct hsm_user_request *hur = karg;
1023 unsigned int reqcount = hur->hur_request.hr_itemcount;
1028 /* if the request is about a single fid
1029 * or if there is a single MDS, no need to split
1031 if (reqcount == 1 || count == 1) {
1032 tgt = lmv_find_target(lmv,
1033 &hur->hur_user_item[0].hui_fid);
1035 RETURN(PTR_ERR(tgt));
1036 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1038 /* split fid list to their respective MDS */
1039 for (i = 0; i < count; i++) {
1042 struct hsm_user_request *req;
1045 if (tgt == NULL || tgt->ltd_exp == NULL)
1048 nr = lmv_hsm_req_count(lmv, hur, tgt);
1051 if (nr == 0) /* nothing for this MDS */
1054 /* build a request with fids for this MDS */
1055 reqlen = offsetof(typeof(*hur),
1057 + hur->hur_request.hr_data_len;
1058 OBD_ALLOC_LARGE(req, reqlen);
1061 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1063 GOTO(hsm_req_err, rc1);
1064 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1067 if (rc1 != 0 && rc == 0)
1069 OBD_FREE_LARGE(req, reqlen);
1074 case LL_IOC_LOV_SWAP_LAYOUTS: {
1075 struct md_op_data *op_data = karg;
1076 struct lmv_tgt_desc *tgt1, *tgt2;
1078 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1080 RETURN(PTR_ERR(tgt1));
1082 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1084 RETURN(PTR_ERR(tgt2));
1086 if ((tgt1->ltd_exp == NULL) || (tgt2->ltd_exp == NULL))
1089 /* only files on same MDT can have their layouts swapped */
1090 if (tgt1->ltd_idx != tgt2->ltd_idx)
1093 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1096 case LL_IOC_HSM_CT_START: {
1097 struct lustre_kernelcomm *lk = karg;
1098 if (lk->lk_flags & LK_FLG_STOP)
1099 rc = lmv_hsm_ct_unregister(obddev, cmd, len, lk, uarg);
1101 rc = lmv_hsm_ct_register(obddev, cmd, len, lk, uarg);
1105 for (i = 0; i < count; i++) {
1106 struct obd_device *mdc_obd;
1110 if (tgt == NULL || tgt->ltd_exp == NULL)
1112 /* ll_umount_begin() sets force flag but for lmv, not
1113 * mdc. Let's pass it through */
1114 mdc_obd = class_exp2obd(tgt->ltd_exp);
1115 mdc_obd->obd_force = obddev->obd_force;
1116 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1118 if (tgt->ltd_active) {
1119 CERROR("error: iocontrol MDC %s on MDT"
1120 " idx %d cmd %x: err = %d\n",
1121 tgt->ltd_uuid.uuid, i, cmd, err);
1135 * This is _inode_ placement policy function (not name).
1137 static int lmv_placement_policy(struct obd_device *obd,
1138 struct md_op_data *op_data, u32 *mds)
1140 struct lmv_obd *lmv = &obd->u.lmv;
1141 struct lmv_user_md *lum;
1145 LASSERT(mds != NULL);
1147 if (lmv->desc.ld_tgt_count == 1) {
1152 lum = op_data->op_data;
1154 * 1. See if the stripe offset is specified by lum.
1155 * 2. Then check if there is default stripe offset.
1156 * 3. Finally choose MDS by name hash if the parent
1157 * is striped directory. (see lmv_locate_mds()). */
1158 if (op_data->op_cli_flags & CLI_SET_MEA && lum != NULL &&
1159 le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1160 *mds = le32_to_cpu(lum->lum_stripe_offset);
1161 } else if (op_data->op_default_stripe_offset != (__u32)-1) {
1162 *mds = op_data->op_default_stripe_offset;
1163 op_data->op_mds = *mds;
1164 /* Correct the stripe offset in lum */
1166 lum->lum_stripe_offset = cpu_to_le32(*mds);
1168 *mds = op_data->op_mds;
1174 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1176 struct lmv_tgt_desc *tgt;
1180 tgt = lmv_get_target(lmv, mds, NULL);
1182 RETURN(PTR_ERR(tgt));
1185 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1186 * on server that seq in new allocated fid is not yet known.
1188 mutex_lock(&tgt->ltd_fid_mutex);
1190 if (tgt->ltd_active == 0 || tgt->ltd_exp == NULL)
1191 GOTO(out, rc = -ENODEV);
1194 * Asking underlying tgt layer to allocate new fid.
1196 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1198 LASSERT(fid_is_sane(fid));
1204 mutex_unlock(&tgt->ltd_fid_mutex);
1208 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1209 struct lu_fid *fid, struct md_op_data *op_data)
1211 struct obd_device *obd = class_exp2obd(exp);
1212 struct lmv_obd *lmv = &obd->u.lmv;
1217 LASSERT(op_data != NULL);
1218 LASSERT(fid != NULL);
1220 rc = lmv_placement_policy(obd, op_data, &mds);
1222 CERROR("Can't get target for allocating fid, "
1227 rc = __lmv_fid_alloc(lmv, fid, mds);
1229 CERROR("Can't alloc new fid, rc %d\n", rc);
1236 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1238 struct lmv_obd *lmv = &obd->u.lmv;
1239 struct lmv_desc *desc;
1243 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1244 CERROR("LMV setup requires a descriptor\n");
1248 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1249 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1250 CERROR("Lmv descriptor size wrong: %d > %d\n",
1251 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1255 lmv->tgts_size = 32U;
1256 OBD_ALLOC(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1257 if (lmv->tgts == NULL)
1260 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1261 lmv->desc.ld_tgt_count = 0;
1262 lmv->desc.ld_active_tgt_count = 0;
1263 lmv->max_def_easize = 0;
1264 lmv->max_easize = 0;
1266 spin_lock_init(&lmv->lmv_lock);
1267 mutex_init(&lmv->lmv_init_mutex);
1269 rc = lmv_tunables_init(obd);
1271 CWARN("%s: error adding LMV sysfs/debugfs files: rc = %d\n",
1274 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1275 LUSTRE_CLI_FLD_HASH_DHT);
1277 CERROR("Can't init FLD, err %d\n", rc);
1287 static int lmv_cleanup(struct obd_device *obd)
1289 struct lmv_obd *lmv = &obd->u.lmv;
1292 fld_client_fini(&lmv->lmv_fld);
1293 if (lmv->tgts != NULL) {
1295 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1296 if (lmv->tgts[i] == NULL)
1298 lmv_del_target(lmv, i);
1300 OBD_FREE(lmv->tgts, sizeof(*lmv->tgts) * lmv->tgts_size);
1306 static int lmv_process_config(struct obd_device *obd, size_t len, void *buf)
1308 struct lustre_cfg *lcfg = buf;
1309 struct obd_uuid obd_uuid;
1315 switch (lcfg->lcfg_command) {
1317 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1318 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID */
1319 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid))
1320 GOTO(out, rc = -EINVAL);
1322 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1324 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1)
1325 GOTO(out, rc = -EINVAL);
1326 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1327 GOTO(out, rc = -EINVAL);
1328 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1331 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1332 GOTO(out, rc = -EINVAL);
1338 static int lmv_select_statfs_mdt(struct lmv_obd *lmv, __u32 flags)
1342 if (flags & OBD_STATFS_FOR_MDT0)
1345 if (lmv->lmv_statfs_start || lmv->desc.ld_tgt_count == 1)
1346 return lmv->lmv_statfs_start;
1348 /* choose initial MDT for this client */
1350 struct lnet_process_id lnet_id;
1351 if (LNetGetId(i, &lnet_id) == -ENOENT)
1354 if (LNET_NETTYP(LNET_NIDNET(lnet_id.nid)) != LOLND) {
1355 lmv->lmv_statfs_start =
1356 lnet_id.nid % lmv->desc.ld_tgt_count;
1361 return lmv->lmv_statfs_start;
1364 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1365 struct obd_statfs *osfs, time64_t max_age, __u32 flags)
1367 struct obd_device *obd = class_exp2obd(exp);
1368 struct lmv_obd *lmv = &obd->u.lmv;
1369 struct obd_statfs *temp;
1374 OBD_ALLOC(temp, sizeof(*temp));
1378 /* distribute statfs among MDTs */
1379 idx = lmv_select_statfs_mdt(lmv, flags);
1381 for (i = 0; i < lmv->desc.ld_tgt_count; i++, idx++) {
1382 idx = idx % lmv->desc.ld_tgt_count;
1383 if (lmv->tgts[idx] == NULL || lmv->tgts[idx]->ltd_exp == NULL)
1386 rc = obd_statfs(env, lmv->tgts[idx]->ltd_exp, temp,
1389 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1390 lmv->tgts[idx]->ltd_exp->exp_obd->obd_name,
1392 GOTO(out_free_temp, rc);
1395 if (temp->os_state & OS_STATE_SUM ||
1396 flags == OBD_STATFS_FOR_MDT0) {
1397 /* reset to the last aggregated values
1398 * and don't sum with non-aggrated data */
1399 /* If the statfs is from mount, it needs to retrieve
1400 * necessary information from MDT0. i.e. mount does
1401 * not need the merged osfs from all of MDT. Also
1402 * clients can be mounted as long as MDT0 is in
1411 osfs->os_bavail += temp->os_bavail;
1412 osfs->os_blocks += temp->os_blocks;
1413 osfs->os_ffree += temp->os_ffree;
1414 osfs->os_files += temp->os_files;
1415 osfs->os_granted += temp->os_granted;
1421 OBD_FREE(temp, sizeof(*temp));
1425 static int lmv_get_root(struct obd_export *exp, const char *fileset,
1428 struct obd_device *obd = exp->exp_obd;
1429 struct lmv_obd *lmv = &obd->u.lmv;
1433 rc = md_get_root(lmv->tgts[0]->ltd_exp, fileset, fid);
1437 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1438 u64 obd_md_valid, const char *name, size_t buf_size,
1439 struct ptlrpc_request **req)
1441 struct obd_device *obd = exp->exp_obd;
1442 struct lmv_obd *lmv = &obd->u.lmv;
1443 struct lmv_tgt_desc *tgt;
1447 tgt = lmv_find_target(lmv, fid);
1449 RETURN(PTR_ERR(tgt));
1451 rc = md_getxattr(tgt->ltd_exp, fid, obd_md_valid, name, buf_size, req);
1456 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1457 u64 obd_md_valid, const char *name,
1458 const void *value, size_t value_size,
1459 unsigned int xattr_flags, u32 suppgid,
1460 struct ptlrpc_request **req)
1462 struct obd_device *obd = exp->exp_obd;
1463 struct lmv_obd *lmv = &obd->u.lmv;
1464 struct lmv_tgt_desc *tgt;
1468 tgt = lmv_find_target(lmv, fid);
1470 RETURN(PTR_ERR(tgt));
1472 rc = md_setxattr(tgt->ltd_exp, fid, obd_md_valid, name,
1473 value, value_size, xattr_flags, suppgid, req);
1478 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1479 struct ptlrpc_request **request)
1481 struct obd_device *obd = exp->exp_obd;
1482 struct lmv_obd *lmv = &obd->u.lmv;
1483 struct lmv_tgt_desc *tgt;
1487 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1489 RETURN(PTR_ERR(tgt));
1491 if (op_data->op_flags & MF_GET_MDT_IDX) {
1492 op_data->op_mds = tgt->ltd_idx;
1496 rc = md_getattr(tgt->ltd_exp, op_data, request);
1501 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1503 struct obd_device *obd = exp->exp_obd;
1504 struct lmv_obd *lmv = &obd->u.lmv;
1508 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1511 * With DNE every object can have two locks in different namespaces:
1512 * lookup lock in space of MDT storing direntry and update/open lock in
1513 * space of MDT storing inode.
1515 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1516 if (lmv->tgts[i] == NULL || lmv->tgts[i]->ltd_exp == NULL)
1518 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1524 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1525 struct md_open_data *mod, struct ptlrpc_request **request)
1527 struct obd_device *obd = exp->exp_obd;
1528 struct lmv_obd *lmv = &obd->u.lmv;
1529 struct lmv_tgt_desc *tgt;
1533 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1535 RETURN(PTR_ERR(tgt));
1537 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1538 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1543 * Choosing the MDT by name or FID in @op_data.
1544 * For non-striped directory, it will locate MDT by fid.
1545 * For striped-directory, it will locate MDT by name. And also
1546 * it will reset op_fid1 with the FID of the choosen stripe.
1548 struct lmv_tgt_desc *
1549 lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1550 const char *name, int namelen, struct lu_fid *fid,
1553 struct lmv_tgt_desc *tgt;
1554 const struct lmv_oinfo *oinfo;
1556 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1557 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1558 RETURN(ERR_PTR(-EBADF));
1559 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1561 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
1563 RETURN(ERR_CAST(oinfo));
1567 *fid = oinfo->lmo_fid;
1569 *mds = oinfo->lmo_mds;
1571 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1573 CDEBUG(D_INFO, "locate on mds %u "DFID"\n", oinfo->lmo_mds,
1574 PFID(&oinfo->lmo_fid));
1579 * Locate mds by fid or name
1581 * For striped directory (lsm != NULL), it will locate the stripe
1582 * by name hash (see lsm_name_to_stripe_info()). Note: if the hash_type
1583 * is unknown, it will return -EBADFD, and lmv_intent_lookup might need
1584 * walk through all of stripes to locate the entry.
1586 * For normal direcotry, it will locate MDS by FID directly.
1587 * \param[in] lmv LMV device
1588 * \param[in] op_data client MD stack parameters, name, namelen
1590 * \param[in] fid object FID used to locate MDS.
1592 * retval pointer to the lmv_tgt_desc if succeed.
1593 * ERR_PTR(errno) if failed.
1595 struct lmv_tgt_desc*
1596 lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1599 struct lmv_stripe_md *lsm = op_data->op_mea1;
1600 struct lmv_tgt_desc *tgt;
1602 /* During creating VOLATILE file, it should honor the mdt
1603 * index if the file under striped dir is being restored, see
1605 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1606 (int)op_data->op_mds != -1) {
1608 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1613 /* refill the right parent fid */
1614 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1615 struct lmv_oinfo *oinfo;
1617 oinfo = &lsm->lsm_md_oinfo[i];
1618 if (oinfo->lmo_mds == op_data->op_mds) {
1619 *fid = oinfo->lmo_fid;
1624 if (i == lsm->lsm_md_stripe_count)
1625 *fid = lsm->lsm_md_oinfo[0].lmo_fid;
1631 if (lsm == NULL || op_data->op_namelen == 0) {
1632 tgt = lmv_find_target(lmv, fid);
1636 op_data->op_mds = tgt->ltd_idx;
1640 return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1641 op_data->op_namelen, fid,
1645 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1646 const void *data, size_t datalen, umode_t mode, uid_t uid,
1647 gid_t gid, cfs_cap_t cap_effective, __u64 rdev,
1648 struct ptlrpc_request **request)
1650 struct obd_device *obd = exp->exp_obd;
1651 struct lmv_obd *lmv = &obd->u.lmv;
1652 struct lmv_tgt_desc *tgt;
1656 if (!lmv->desc.ld_active_tgt_count)
1659 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1661 RETURN(PTR_ERR(tgt));
1663 CDEBUG(D_INODE, "CREATE name '%.*s' on "DFID" -> mds #%x\n",
1664 (int)op_data->op_namelen, op_data->op_name,
1665 PFID(&op_data->op_fid1), op_data->op_mds);
1667 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1670 if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1671 /* Send the create request to the MDT where the object
1672 * will be located */
1673 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1675 RETURN(PTR_ERR(tgt));
1677 op_data->op_mds = tgt->ltd_idx;
1679 CDEBUG(D_CONFIG, "Server doesn't support striped dirs\n");
1682 CDEBUG(D_INODE, "CREATE obj "DFID" -> mds #%x\n",
1683 PFID(&op_data->op_fid2), op_data->op_mds);
1685 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1686 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1687 cap_effective, rdev, request);
1689 if (*request == NULL)
1691 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1697 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1698 const union ldlm_policy_data *policy, struct md_op_data *op_data,
1699 struct lustre_handle *lockh, __u64 extra_lock_flags)
1701 struct obd_device *obd = exp->exp_obd;
1702 struct lmv_obd *lmv = &obd->u.lmv;
1703 struct lmv_tgt_desc *tgt;
1707 CDEBUG(D_INODE, "ENQUEUE on "DFID"\n", PFID(&op_data->op_fid1));
1709 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1711 RETURN(PTR_ERR(tgt));
1713 CDEBUG(D_INODE, "ENQUEUE on "DFID" -> mds #%u\n",
1714 PFID(&op_data->op_fid1), tgt->ltd_idx);
1716 rc = md_enqueue(tgt->ltd_exp, einfo, policy, op_data, lockh,
1723 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1724 struct ptlrpc_request **preq)
1726 struct ptlrpc_request *req = NULL;
1727 struct obd_device *obd = exp->exp_obd;
1728 struct lmv_obd *lmv = &obd->u.lmv;
1729 struct lmv_tgt_desc *tgt;
1730 struct mdt_body *body;
1734 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1736 RETURN(PTR_ERR(tgt));
1738 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" -> mds #%d\n",
1739 (int)op_data->op_namelen, op_data->op_name,
1740 PFID(&op_data->op_fid1), tgt->ltd_idx);
1742 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1746 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1747 LASSERT(body != NULL);
1749 if (body->mbo_valid & OBD_MD_MDS) {
1750 struct lu_fid rid = body->mbo_fid1;
1751 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1754 tgt = lmv_find_target(lmv, &rid);
1756 ptlrpc_req_finished(*preq);
1758 RETURN(PTR_ERR(tgt));
1761 op_data->op_fid1 = rid;
1762 op_data->op_valid |= OBD_MD_FLCROSSREF;
1763 op_data->op_namelen = 0;
1764 op_data->op_name = NULL;
1765 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1766 ptlrpc_req_finished(*preq);
1773 #define md_op_data_fid(op_data, fl) \
1774 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1775 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1776 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1777 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1780 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1781 struct md_op_data *op_data, __u32 op_tgt,
1782 enum ldlm_mode mode, int bits, int flag)
1784 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1785 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
1786 union ldlm_policy_data policy = { { 0 } };
1790 if (!fid_is_sane(fid))
1794 tgt = lmv_find_target(lmv, fid);
1796 RETURN(PTR_ERR(tgt));
1799 if (tgt->ltd_idx != op_tgt) {
1800 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1801 policy.l_inodebits.bits = bits;
1802 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1803 mode, LCF_ASYNC, NULL);
1806 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1808 op_data->op_flags |= flag;
1816 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1819 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1820 struct ptlrpc_request **request)
1822 struct obd_device *obd = exp->exp_obd;
1823 struct lmv_obd *lmv = &obd->u.lmv;
1824 struct lmv_tgt_desc *tgt;
1828 LASSERT(op_data->op_namelen != 0);
1830 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1831 PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1832 op_data->op_name, PFID(&op_data->op_fid1));
1834 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1835 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1836 op_data->op_cap = cfs_curproc_cap_pack();
1837 if (op_data->op_mea2 != NULL) {
1838 struct lmv_stripe_md *lsm = op_data->op_mea2;
1839 const struct lmv_oinfo *oinfo;
1841 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
1842 op_data->op_namelen);
1844 RETURN(PTR_ERR(oinfo));
1846 op_data->op_fid2 = oinfo->lmo_fid;
1849 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1851 RETURN(PTR_ERR(tgt));
1854 * Cancel UPDATE lock on child (fid1).
1856 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1857 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
1858 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1862 rc = md_link(tgt->ltd_exp, op_data, request);
1867 static int lmv_migrate(struct obd_export *exp, struct md_op_data *op_data,
1868 const char *name, size_t namelen,
1869 struct ptlrpc_request **request)
1871 struct obd_device *obd = exp->exp_obd;
1872 struct lmv_obd *lmv = &obd->u.lmv;
1873 struct lmv_stripe_md *lsm = op_data->op_mea1;
1874 struct lmv_tgt_desc *parent_tgt;
1875 struct lmv_tgt_desc *sp_tgt;
1876 struct lmv_tgt_desc *tp_tgt = NULL;
1877 struct lmv_tgt_desc *child_tgt;
1878 struct lmv_tgt_desc *tgt;
1879 struct lu_fid target_fid;
1884 LASSERT(op_data->op_cli_flags & CLI_MIGRATE);
1885 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID "DFID"\n",
1886 PFID(&op_data->op_fid3));
1888 CDEBUG(D_INODE, "MIGRATE "DFID"/%.*s\n",
1889 PFID(&op_data->op_fid1), (int)namelen, name);
1891 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1892 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1893 op_data->op_cap = cfs_curproc_cap_pack();
1895 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
1896 if (IS_ERR(parent_tgt))
1897 RETURN(PTR_ERR(parent_tgt));
1900 __u32 hash_type = lsm->lsm_md_hash_type;
1901 __u32 stripe_count = lsm->lsm_md_stripe_count;
1904 * old stripes are appended after new stripes for migrating
1907 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
1908 hash_type = lsm->lsm_md_migrate_hash;
1909 stripe_count -= lsm->lsm_md_migrate_offset;
1912 rc = lmv_name_to_stripe_index(hash_type, stripe_count, name,
1917 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION)
1918 rc += lsm->lsm_md_migrate_offset;
1920 /* save it in fid4 temporarily for early cancel */
1921 op_data->op_fid4 = lsm->lsm_md_oinfo[rc].lmo_fid;
1922 sp_tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[rc].lmo_mds,
1925 RETURN(PTR_ERR(sp_tgt));
1928 * if parent is being migrated too, fill op_fid2 with target
1929 * stripe fid, otherwise the target stripe is not created yet.
1931 if (lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION) {
1932 hash_type = lsm->lsm_md_hash_type &
1933 ~LMV_HASH_FLAG_MIGRATION;
1934 stripe_count = lsm->lsm_md_migrate_offset;
1936 rc = lmv_name_to_stripe_index(hash_type, stripe_count,
1941 op_data->op_fid2 = lsm->lsm_md_oinfo[rc].lmo_fid;
1942 tp_tgt = lmv_get_target(lmv,
1943 lsm->lsm_md_oinfo[rc].lmo_mds,
1946 RETURN(PTR_ERR(tp_tgt));
1949 sp_tgt = parent_tgt;
1952 child_tgt = lmv_find_target(lmv, &op_data->op_fid3);
1953 if (IS_ERR(child_tgt))
1954 RETURN(PTR_ERR(child_tgt));
1956 rc = lmv_fid_alloc(NULL, exp, &target_fid, op_data);
1961 * for directory, send migrate request to the MDT where the object will
1962 * be migrated to, because we can't create a striped directory remotely.
1964 * otherwise, send to the MDT where source is located because regular
1965 * file may open lease.
1967 * NB. if MDT doesn't support DIR_MIGRATE, send to source MDT too for
1968 * backward compatibility.
1970 if (S_ISDIR(op_data->op_mode) &&
1971 (exp_connect_flags2(exp) & OBD_CONNECT2_DIR_MIGRATE)) {
1972 tgt = lmv_find_target(lmv, &target_fid);
1974 RETURN(PTR_ERR(tgt));
1979 /* cancel UPDATE lock of parent master object */
1980 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx, LCK_EX,
1981 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1985 /* cancel UPDATE lock of source parent */
1986 if (sp_tgt != parent_tgt) {
1988 * migrate RPC packs master object FID, because we can only pack
1989 * two FIDs in reint RPC, but MDS needs to know both source
1990 * parent and target parent, and it will obtain them from master
1991 * FID and LMV, the other FID in RPC is kept for target.
1993 * since this FID is not passed to MDC, cancel it anyway.
1995 rc = lmv_early_cancel(exp, sp_tgt, op_data, -1, LCK_EX,
1996 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID4);
2000 op_data->op_flags &= ~MF_MDC_CANCEL_FID4;
2002 op_data->op_fid4 = target_fid;
2004 /* cancel UPDATE locks of target parent */
2005 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2006 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2010 /* cancel LOOKUP lock of source if source is remote object */
2011 if (child_tgt != sp_tgt) {
2012 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_idx,
2013 LCK_EX, MDS_INODELOCK_LOOKUP,
2014 MF_MDC_CANCEL_FID3);
2019 /* cancel ELC locks of source */
2020 rc = lmv_early_cancel(exp, child_tgt, op_data, tgt->ltd_idx, LCK_EX,
2021 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2025 rc = md_rename(tgt->ltd_exp, op_data, name, namelen, NULL, 0, request);
2030 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2031 const char *old, size_t oldlen,
2032 const char *new, size_t newlen,
2033 struct ptlrpc_request **request)
2035 struct obd_device *obd = exp->exp_obd;
2036 struct lmv_obd *lmv = &obd->u.lmv;
2037 struct lmv_stripe_md *lsm = op_data->op_mea1;
2038 struct lmv_tgt_desc *sp_tgt;
2039 struct lmv_tgt_desc *tp_tgt = NULL;
2040 struct lmv_tgt_desc *tgt;
2041 struct mdt_body *body;
2046 LASSERT(oldlen != 0);
2048 if (op_data->op_cli_flags & CLI_MIGRATE) {
2049 rc = lmv_migrate(exp, op_data, old, oldlen, request);
2053 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2054 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2055 op_data->op_cap = cfs_curproc_cap_pack();
2057 CDEBUG(D_INODE, "RENAME "DFID"/%.*s to "DFID"/%.*s\n",
2058 PFID(&op_data->op_fid1), (int)oldlen, old,
2059 PFID(&op_data->op_fid2), (int)newlen, new);
2062 sp_tgt = lmv_locate_target_for_name(lmv, lsm, old, oldlen,
2066 sp_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2068 RETURN(PTR_ERR(sp_tgt));
2070 lsm = op_data->op_mea2;
2072 tp_tgt = lmv_locate_target_for_name(lmv, lsm, new, newlen,
2076 tp_tgt = lmv_find_target(lmv, &op_data->op_fid2);
2078 RETURN(PTR_ERR(tp_tgt));
2080 /* Since the target child might be destroyed, and it might become
2081 * orphan, and we can only check orphan on the local MDT right now, so
2082 * we send rename request to the MDT where target child is located. If
2083 * target child does not exist, then it will send the request to the
2085 if (fid_is_sane(&op_data->op_fid4)) {
2086 tgt = lmv_find_target(lmv, &op_data->op_fid4);
2088 RETURN(PTR_ERR(tgt));
2093 op_data->op_flags |= MF_MDC_CANCEL_FID4;
2095 /* cancel UPDATE locks of source parent */
2096 rc = lmv_early_cancel(exp, sp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2097 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2101 /* cancel UPDATE locks of target parent */
2102 rc = lmv_early_cancel(exp, tp_tgt, op_data, tgt->ltd_idx, LCK_EX,
2103 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2107 if (fid_is_sane(&op_data->op_fid3)) {
2108 struct lmv_tgt_desc *src_tgt;
2110 src_tgt = lmv_find_target(lmv, &op_data->op_fid3);
2111 if (IS_ERR(src_tgt))
2112 RETURN(PTR_ERR(src_tgt));
2114 /* cancel LOOKUP lock of source on source parent */
2115 if (src_tgt != sp_tgt) {
2116 rc = lmv_early_cancel(exp, sp_tgt, op_data,
2117 tgt->ltd_idx, LCK_EX,
2118 MDS_INODELOCK_LOOKUP,
2119 MF_MDC_CANCEL_FID3);
2124 /* cancel ELC locks of source */
2125 rc = lmv_early_cancel(exp, src_tgt, op_data, tgt->ltd_idx,
2126 LCK_EX, MDS_INODELOCK_ELC,
2127 MF_MDC_CANCEL_FID3);
2133 if (fid_is_sane(&op_data->op_fid4)) {
2134 /* cancel LOOKUP lock of target on target parent */
2135 if (tgt != tp_tgt) {
2136 rc = lmv_early_cancel(exp, tp_tgt, op_data,
2137 tgt->ltd_idx, LCK_EX,
2138 MDS_INODELOCK_LOOKUP,
2139 MF_MDC_CANCEL_FID4);
2145 rc = md_rename(tgt->ltd_exp, op_data, old, oldlen, new, newlen,
2148 if (rc != 0 && rc != -EXDEV)
2151 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2155 /* Not cross-ref case, just get out of here. */
2156 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2159 CDEBUG(D_INODE, "%s: try rename to another MDT for "DFID"\n",
2160 exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2162 op_data->op_fid4 = body->mbo_fid1;
2163 ptlrpc_req_finished(*request);
2166 tgt = lmv_find_target(lmv, &op_data->op_fid4);
2168 RETURN(PTR_ERR(tgt));
2173 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2174 void *ea, size_t ealen, struct ptlrpc_request **request)
2176 struct obd_device *obd = exp->exp_obd;
2177 struct lmv_obd *lmv = &obd->u.lmv;
2178 struct lmv_tgt_desc *tgt;
2182 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x\n",
2183 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
2185 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2186 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2188 RETURN(PTR_ERR(tgt));
2190 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2195 static int lmv_fsync(struct obd_export *exp, const struct lu_fid *fid,
2196 struct ptlrpc_request **request)
2198 struct obd_device *obd = exp->exp_obd;
2199 struct lmv_obd *lmv = &obd->u.lmv;
2200 struct lmv_tgt_desc *tgt;
2204 tgt = lmv_find_target(lmv, fid);
2206 RETURN(PTR_ERR(tgt));
2208 rc = md_fsync(tgt->ltd_exp, fid, request);
2212 struct stripe_dirent {
2213 struct page *sd_page;
2214 struct lu_dirpage *sd_dp;
2215 struct lu_dirent *sd_ent;
2219 struct lmv_dir_ctxt {
2220 struct lmv_obd *ldc_lmv;
2221 struct md_op_data *ldc_op_data;
2222 struct md_callback *ldc_cb_op;
2225 struct stripe_dirent ldc_stripes[0];
2228 static inline void stripe_dirent_unload(struct stripe_dirent *stripe)
2230 if (stripe->sd_page) {
2231 kunmap(stripe->sd_page);
2232 put_page(stripe->sd_page);
2233 stripe->sd_page = NULL;
2234 stripe->sd_ent = NULL;
2238 static inline void put_lmv_dir_ctxt(struct lmv_dir_ctxt *ctxt)
2242 for (i = 0; i < ctxt->ldc_count; i++)
2243 stripe_dirent_unload(&ctxt->ldc_stripes[i]);
2246 /* if @ent is dummy, or . .., get next */
2247 static struct lu_dirent *stripe_dirent_get(struct lmv_dir_ctxt *ctxt,
2248 struct lu_dirent *ent,
2251 for (; ent; ent = lu_dirent_next(ent)) {
2252 /* Skip dummy entry */
2253 if (le16_to_cpu(ent->lde_namelen) == 0)
2256 /* skip . and .. for other stripes */
2258 (strncmp(ent->lde_name, ".",
2259 le16_to_cpu(ent->lde_namelen)) == 0 ||
2260 strncmp(ent->lde_name, "..",
2261 le16_to_cpu(ent->lde_namelen)) == 0))
2264 if (le64_to_cpu(ent->lde_hash) >= ctxt->ldc_hash)
2271 static struct lu_dirent *stripe_dirent_load(struct lmv_dir_ctxt *ctxt,
2272 struct stripe_dirent *stripe,
2275 struct md_op_data *op_data = ctxt->ldc_op_data;
2276 struct lmv_oinfo *oinfo;
2277 struct lu_fid fid = op_data->op_fid1;
2278 struct inode *inode = op_data->op_data;
2279 struct lmv_tgt_desc *tgt;
2280 struct lu_dirent *ent = stripe->sd_ent;
2281 __u64 hash = ctxt->ldc_hash;
2286 LASSERT(stripe == &ctxt->ldc_stripes[stripe_index]);
2290 if (stripe->sd_page) {
2291 __u64 end = le64_to_cpu(stripe->sd_dp->ldp_hash_end);
2293 /* @hash should be the last dirent hash */
2294 LASSERTF(hash <= end,
2295 "ctxt@%p stripe@%p hash %llx end %llx\n",
2296 ctxt, stripe, hash, end);
2297 /* unload last page */
2298 stripe_dirent_unload(stripe);
2300 if (end == MDS_DIR_END_OFF) {
2301 stripe->sd_eof = true;
2307 oinfo = &op_data->op_mea1->lsm_md_oinfo[stripe_index];
2308 tgt = lmv_get_target(ctxt->ldc_lmv, oinfo->lmo_mds, NULL);
2314 /* op_data is shared by stripes, reset after use */
2315 op_data->op_fid1 = oinfo->lmo_fid;
2316 op_data->op_fid2 = oinfo->lmo_fid;
2317 op_data->op_data = oinfo->lmo_root;
2319 rc = md_read_page(tgt->ltd_exp, op_data, ctxt->ldc_cb_op, hash,
2322 op_data->op_fid1 = fid;
2323 op_data->op_fid2 = fid;
2324 op_data->op_data = inode;
2329 stripe->sd_dp = page_address(stripe->sd_page);
2330 ent = stripe_dirent_get(ctxt, lu_dirent_start(stripe->sd_dp),
2332 /* in case a page filled with ., .. and dummy, read next */
2335 stripe->sd_ent = ent;
2338 /* treat error as eof, so dir can be partially accessed */
2339 stripe->sd_eof = true;
2340 LCONSOLE_WARN("dir "DFID" stripe %d readdir failed: %d, "
2341 "directory is partially accessed!\n",
2342 PFID(&ctxt->ldc_op_data->op_fid1), stripe_index,
2349 static int lmv_file_resync(struct obd_export *exp, struct md_op_data *data)
2351 struct obd_device *obd = exp->exp_obd;
2352 struct lmv_obd *lmv = &obd->u.lmv;
2353 struct lmv_tgt_desc *tgt;
2357 rc = lmv_check_connect(obd);
2361 tgt = lmv_find_target(lmv, &data->op_fid1);
2363 RETURN(PTR_ERR(tgt));
2365 data->op_flags |= MF_MDC_CANCEL_FID1;
2366 rc = md_file_resync(tgt->ltd_exp, data);
2371 * Get dirent with the closest hash for striped directory
2373 * This function will search the dir entry, whose hash value is the
2374 * closest(>=) to hash from all of sub-stripes, and it is only being called
2375 * for striped directory.
2377 * \param[in] ctxt dir read context
2379 * \retval dirent get the entry successfully
2380 * NULL does not get the entry, normally it means
2381 * it reaches the end of the directory, while read
2382 * stripe dirent error is ignored to allow partial
2385 static struct lu_dirent *lmv_dirent_next(struct lmv_dir_ctxt *ctxt)
2387 struct stripe_dirent *stripe;
2388 struct lu_dirent *ent = NULL;
2392 /* TODO: optimize with k-way merge sort */
2393 for (i = 0; i < ctxt->ldc_count; i++) {
2394 stripe = &ctxt->ldc_stripes[i];
2398 if (!stripe->sd_ent) {
2399 stripe_dirent_load(ctxt, stripe, i);
2400 if (!stripe->sd_ent) {
2401 LASSERT(stripe->sd_eof);
2407 le64_to_cpu(ctxt->ldc_stripes[min].sd_ent->lde_hash) >
2408 le64_to_cpu(stripe->sd_ent->lde_hash)) {
2410 if (le64_to_cpu(stripe->sd_ent->lde_hash) ==
2417 stripe = &ctxt->ldc_stripes[min];
2418 ent = stripe->sd_ent;
2419 /* pop found dirent */
2420 stripe->sd_ent = stripe_dirent_get(ctxt, lu_dirent_next(ent),
2428 * Build dir entry page for striped directory
2430 * This function gets one entry by @offset from a striped directory. It will
2431 * read entries from all of stripes, and choose one closest to the required
2432 * offset(&offset). A few notes
2433 * 1. skip . and .. for non-zero stripes, because there can only have one .
2434 * and .. in a directory.
2435 * 2. op_data will be shared by all of stripes, instead of allocating new
2436 * one, so need to restore before reusing.
2438 * \param[in] exp obd export refer to LMV
2439 * \param[in] op_data hold those MD parameters of read_entry
2440 * \param[in] cb_op ldlm callback being used in enqueue in mdc_read_entry
2441 * \param[in] offset starting hash offset
2442 * \param[out] ppage the page holding the entry. Note: because the entry
2443 * will be accessed in upper layer, so we need hold the
2444 * page until the usages of entry is finished, see
2445 * ll_dir_entry_next.
2447 * retval =0 if get entry successfully
2448 * <0 cannot get entry
2450 static int lmv_striped_read_page(struct obd_export *exp,
2451 struct md_op_data *op_data,
2452 struct md_callback *cb_op,
2453 __u64 offset, struct page **ppage)
2455 struct page *page = NULL;
2456 struct lu_dirpage *dp;
2458 struct lu_dirent *ent;
2459 struct lu_dirent *last_ent;
2461 struct lmv_dir_ctxt *ctxt;
2462 struct lu_dirent *next = NULL;
2468 /* Allocate a page and read entries from all of stripes and fill
2469 * the page by hash order */
2470 page = alloc_page(GFP_KERNEL);
2474 /* Initialize the entry page */
2476 memset(dp, 0, sizeof(*dp));
2477 dp->ldp_hash_start = cpu_to_le64(offset);
2480 left_bytes = PAGE_SIZE - sizeof(*dp);
2484 /* initalize dir read context */
2485 stripe_count = op_data->op_mea1->lsm_md_stripe_count;
2486 OBD_ALLOC(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2488 GOTO(free_page, rc = -ENOMEM);
2489 ctxt->ldc_lmv = &exp->exp_obd->u.lmv;
2490 ctxt->ldc_op_data = op_data;
2491 ctxt->ldc_cb_op = cb_op;
2492 ctxt->ldc_hash = offset;
2493 ctxt->ldc_count = stripe_count;
2496 next = lmv_dirent_next(ctxt);
2498 /* end of directory */
2500 ctxt->ldc_hash = MDS_DIR_END_OFF;
2503 ctxt->ldc_hash = le64_to_cpu(next->lde_hash);
2505 ent_size = le16_to_cpu(next->lde_reclen);
2507 /* the last entry lde_reclen is 0, but it might not be the last
2508 * one of this temporay dir page */
2510 ent_size = lu_dirent_calc_size(
2511 le16_to_cpu(next->lde_namelen),
2512 le32_to_cpu(next->lde_attrs));
2514 if (ent_size > left_bytes)
2517 memcpy(ent, next, ent_size);
2519 /* Replace . with master FID and Replace .. with the parent FID
2520 * of master object */
2521 if (strncmp(ent->lde_name, ".",
2522 le16_to_cpu(ent->lde_namelen)) == 0 &&
2523 le16_to_cpu(ent->lde_namelen) == 1)
2524 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid1);
2525 else if (strncmp(ent->lde_name, "..",
2526 le16_to_cpu(ent->lde_namelen)) == 0 &&
2527 le16_to_cpu(ent->lde_namelen) == 2)
2528 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2530 CDEBUG(D_INODE, "entry %.*s hash %#llx\n",
2531 le16_to_cpu(ent->lde_namelen), ent->lde_name,
2532 le64_to_cpu(ent->lde_hash));
2534 left_bytes -= ent_size;
2535 ent->lde_reclen = cpu_to_le16(ent_size);
2537 ent = (void *)ent + ent_size;
2540 last_ent->lde_reclen = 0;
2543 dp->ldp_flags |= LDF_EMPTY;
2544 else if (ctxt->ldc_hash == le64_to_cpu(last_ent->lde_hash))
2545 dp->ldp_flags |= LDF_COLLIDE;
2546 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
2547 dp->ldp_hash_end = cpu_to_le64(ctxt->ldc_hash);
2549 put_lmv_dir_ctxt(ctxt);
2550 OBD_FREE(ctxt, offsetof(typeof(*ctxt), ldc_stripes[stripe_count]));
2563 int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2564 struct md_callback *cb_op, __u64 offset,
2565 struct page **ppage)
2567 struct obd_device *obd = exp->exp_obd;
2568 struct lmv_obd *lmv = &obd->u.lmv;
2569 struct lmv_stripe_md *lsm = op_data->op_mea1;
2570 struct lmv_tgt_desc *tgt;
2574 if (unlikely(lsm != NULL)) {
2575 rc = lmv_striped_read_page(exp, op_data, cb_op, offset, ppage);
2579 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2581 RETURN(PTR_ERR(tgt));
2583 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2589 * Unlink a file/directory
2591 * Unlink a file or directory under the parent dir. The unlink request
2592 * usually will be sent to the MDT where the child is located, but if
2593 * the client does not have the child FID then request will be sent to the
2594 * MDT where the parent is located.
2596 * If the parent is a striped directory then it also needs to locate which
2597 * stripe the name of the child is located, and replace the parent FID
2598 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2599 * it will walk through all of sub-stripes until the child is being
2602 * \param[in] exp export refer to LMV
2603 * \param[in] op_data different parameters transferred beween client
2604 * MD stacks, name, namelen, FIDs etc.
2605 * op_fid1 is the parent FID, op_fid2 is the child
2607 * \param[out] request point to the request of unlink.
2609 * retval 0 if succeed
2610 * negative errno if failed.
2612 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2613 struct ptlrpc_request **request)
2615 struct obd_device *obd = exp->exp_obd;
2616 struct lmv_obd *lmv = &obd->u.lmv;
2617 struct lmv_tgt_desc *tgt = NULL;
2618 struct lmv_tgt_desc *parent_tgt = NULL;
2619 struct mdt_body *body;
2621 int stripe_index = 0;
2622 struct lmv_stripe_md *lsm = op_data->op_mea1;
2626 /* For striped dir, we need to locate the parent as well */
2628 struct lmv_tgt_desc *tmp;
2630 LASSERT(op_data->op_name != NULL &&
2631 op_data->op_namelen != 0);
2633 tmp = lmv_locate_target_for_name(lmv, lsm,
2635 op_data->op_namelen,
2639 /* return -EBADFD means unknown hash type, might
2640 * need try all sub-stripe here */
2641 if (IS_ERR(tmp) && PTR_ERR(tmp) != -EBADFD)
2642 RETURN(PTR_ERR(tmp));
2644 /* Note: both migrating dir and unknown hash dir need to
2645 * try all of sub-stripes, so we need start search the
2646 * name from stripe 0, but migrating dir is already handled
2647 * inside lmv_locate_target_for_name(), so we only check
2648 * unknown hash type directory here */
2649 if (!lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
2650 struct lmv_oinfo *oinfo;
2652 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2654 op_data->op_fid1 = oinfo->lmo_fid;
2655 op_data->op_mds = oinfo->lmo_mds;
2660 /* Send unlink requests to the MDT where the child is located */
2661 if (likely(!fid_is_zero(&op_data->op_fid2)))
2662 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2663 else if (lsm != NULL)
2664 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
2666 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2669 RETURN(PTR_ERR(tgt));
2671 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2672 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2673 op_data->op_cap = cfs_curproc_cap_pack();
2676 * If child's fid is given, cancel unused locks for it if it is from
2677 * another export than parent.
2679 * LOOKUP lock for child (fid3) should also be cancelled on parent
2680 * tgt_tgt in mdc_unlink().
2682 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2685 * Cancel FULL locks on child (fid3).
2687 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2688 if (IS_ERR(parent_tgt))
2689 RETURN(PTR_ERR(parent_tgt));
2691 if (parent_tgt != tgt) {
2692 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2693 LCK_EX, MDS_INODELOCK_LOOKUP,
2694 MF_MDC_CANCEL_FID3);
2697 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2698 MDS_INODELOCK_ELC, MF_MDC_CANCEL_FID3);
2702 CDEBUG(D_INODE, "unlink with fid="DFID"/"DFID" -> mds #%u\n",
2703 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2705 rc = md_unlink(tgt->ltd_exp, op_data, request);
2706 if (rc != 0 && rc != -EREMOTE && rc != -ENOENT)
2709 /* Try next stripe if it is needed. */
2710 if (rc == -ENOENT && lsm != NULL && lmv_need_try_all_stripes(lsm)) {
2711 struct lmv_oinfo *oinfo;
2714 if (stripe_index >= lsm->lsm_md_stripe_count)
2717 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2719 op_data->op_fid1 = oinfo->lmo_fid;
2720 op_data->op_mds = oinfo->lmo_mds;
2722 ptlrpc_req_finished(*request);
2725 goto try_next_stripe;
2728 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2732 /* Not cross-ref case, just get out of here. */
2733 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2736 CDEBUG(D_INODE, "%s: try unlink to another MDT for "DFID"\n",
2737 exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2739 /* This is a remote object, try remote MDT, Note: it may
2740 * try more than 1 time here, Considering following case
2741 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2742 * 1. Initially A does not know where remote1 is, it send
2743 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2744 * resend unlink RPC to MDT1 (retry 1st time).
2746 * 2. During the unlink RPC in flight,
2747 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2748 * and create new remote1, but on MDT0
2750 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2751 * /mnt/lustre, then lookup get fid of remote1, and find
2752 * it is remote dir again, and replay -EREMOTE again.
2754 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2756 * In theory, it might try unlimited time here, but it should
2757 * be very rare case. */
2758 op_data->op_fid2 = body->mbo_fid1;
2759 ptlrpc_req_finished(*request);
2765 static int lmv_precleanup(struct obd_device *obd)
2768 libcfs_kkuc_group_rem(&obd->obd_uuid, 0, KUC_GRP_HSM);
2769 fld_client_debugfs_fini(&obd->u.lmv.lmv_fld);
2770 lprocfs_obd_cleanup(obd);
2771 lprocfs_free_md_stats(obd);
2776 * Get by key a value associated with a LMV device.
2778 * Dispatch request to lower-layer devices as needed.
2780 * \param[in] env execution environment for this thread
2781 * \param[in] exp export for the LMV device
2782 * \param[in] keylen length of key identifier
2783 * \param[in] key identifier of key to get value for
2784 * \param[in] vallen size of \a val
2785 * \param[out] val pointer to storage location for value
2786 * \param[in] lsm optional striping metadata of object
2788 * \retval 0 on success
2789 * \retval negative negated errno on failure
2791 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2792 __u32 keylen, void *key, __u32 *vallen, void *val)
2794 struct obd_device *obd;
2795 struct lmv_obd *lmv;
2799 obd = class_exp2obd(exp);
2801 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2802 exp->exp_handle.h_cookie);
2807 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2810 LASSERT(*vallen == sizeof(__u32));
2811 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2812 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2814 * All tgts should be connected when this gets called.
2816 if (tgt == NULL || tgt->ltd_exp == NULL)
2819 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2824 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2825 KEY_IS(KEY_DEFAULT_EASIZE) ||
2826 KEY_IS(KEY_CONN_DATA)) {
2828 * Forwarding this request to first MDS, it should know LOV
2831 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2833 if (!rc && KEY_IS(KEY_CONN_DATA))
2834 exp->exp_connect_data = *(struct obd_connect_data *)val;
2836 } else if (KEY_IS(KEY_TGT_COUNT)) {
2837 *((int *)val) = lmv->desc.ld_tgt_count;
2841 CDEBUG(D_IOCTL, "Invalid key\n");
2846 * Asynchronously set by key a value associated with a LMV device.
2848 * Dispatch request to lower-layer devices as needed.
2850 * \param[in] env execution environment for this thread
2851 * \param[in] exp export for the LMV device
2852 * \param[in] keylen length of key identifier
2853 * \param[in] key identifier of key to store value for
2854 * \param[in] vallen size of value to store
2855 * \param[in] val pointer to data to be stored
2856 * \param[in] set optional list of related ptlrpc requests
2858 * \retval 0 on success
2859 * \retval negative negated errno on failure
2861 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2862 __u32 keylen, void *key, __u32 vallen, void *val,
2863 struct ptlrpc_request_set *set)
2865 struct lmv_tgt_desc *tgt = NULL;
2866 struct obd_device *obd;
2867 struct lmv_obd *lmv;
2871 obd = class_exp2obd(exp);
2873 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2874 exp->exp_handle.h_cookie);
2879 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2880 KEY_IS(KEY_DEFAULT_EASIZE)) {
2883 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2886 if (tgt == NULL || tgt->ltd_exp == NULL)
2889 err = obd_set_info_async(env, tgt->ltd_exp,
2890 keylen, key, vallen, val, set);
2901 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2902 const struct lmv_mds_md_v1 *lmm1)
2904 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2911 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2912 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2913 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2914 if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2915 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2917 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2918 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2919 lsm->lsm_md_migrate_offset = le32_to_cpu(lmm1->lmv_migrate_offset);
2920 lsm->lsm_md_migrate_hash = le32_to_cpu(lmm1->lmv_migrate_hash);
2921 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2922 sizeof(lsm->lsm_md_pool_name));
2924 if (cplen >= sizeof(lsm->lsm_md_pool_name))
2927 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %#x "
2928 "layout_version %d\n", lsm->lsm_md_stripe_count,
2929 lsm->lsm_md_master_mdt_index, lsm->lsm_md_hash_type,
2930 lsm->lsm_md_layout_version);
2932 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2933 for (i = 0; i < stripe_count; i++) {
2934 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2935 &lmm1->lmv_stripe_fids[i]);
2936 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2937 &lsm->lsm_md_oinfo[i].lmo_mds);
2940 CDEBUG(D_INFO, "unpack fid #%d "DFID"\n", i,
2941 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2947 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2948 const union lmv_mds_md *lmm, size_t lmm_size)
2950 struct lmv_stripe_md *lsm;
2953 bool allocated = false;
2956 LASSERT(lsmp != NULL);
2960 if (lsm != NULL && lmm == NULL) {
2963 for (i = 0; i < lsm->lsm_md_stripe_count; i++)
2964 iput(lsm->lsm_md_oinfo[i].lmo_root);
2965 lsm_size = lmv_stripe_md_size(lsm->lsm_md_stripe_count);
2966 OBD_FREE(lsm, lsm_size);
2971 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
2975 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2976 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2977 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2978 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2983 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2984 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2987 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2988 * stripecount should be 0 then.
2990 lsm_size = lmv_stripe_md_size(0);
2992 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2994 OBD_ALLOC(lsm, lsm_size);
3001 switch (le32_to_cpu(lmm->lmv_magic)) {
3003 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
3006 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
3007 le32_to_cpu(lmm->lmv_magic));
3012 if (rc != 0 && allocated) {
3013 OBD_FREE(lsm, lsm_size);
3020 void lmv_free_memmd(struct lmv_stripe_md *lsm)
3022 lmv_unpackmd(NULL, &lsm, NULL, 0);
3024 EXPORT_SYMBOL(lmv_free_memmd);
3026 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
3027 union ldlm_policy_data *policy,
3028 enum ldlm_mode mode, enum ldlm_cancel_flags flags,
3031 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3036 LASSERT(fid != NULL);
3038 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3039 struct lmv_tgt_desc *tgt = lmv->tgts[i];
3042 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3045 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
3053 static int lmv_set_lock_data(struct obd_export *exp,
3054 const struct lustre_handle *lockh,
3055 void *data, __u64 *bits)
3057 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3058 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3062 if (tgt == NULL || tgt->ltd_exp == NULL)
3064 rc = md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
3068 enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
3069 const struct lu_fid *fid, enum ldlm_type type,
3070 union ldlm_policy_data *policy,
3071 enum ldlm_mode mode, struct lustre_handle *lockh)
3073 struct obd_device *obd = exp->exp_obd;
3074 struct lmv_obd *lmv = &obd->u.lmv;
3080 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
3083 * With DNE every object can have two locks in different namespaces:
3084 * lookup lock in space of MDT storing direntry and update/open lock in
3085 * space of MDT storing inode. Try the MDT that the FID maps to first,
3086 * since this can be easily found, and only try others if that fails.
3088 for (i = 0, tgt = lmv_find_target_index(lmv, fid);
3089 i < lmv->desc.ld_tgt_count;
3090 i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
3092 CDEBUG(D_HA, "%s: "DFID" is inaccessible: rc = %d\n",
3093 obd->obd_name, PFID(fid), tgt);
3097 if (lmv->tgts[tgt] == NULL ||
3098 lmv->tgts[tgt]->ltd_exp == NULL ||
3099 lmv->tgts[tgt]->ltd_active == 0)
3102 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
3103 type, policy, mode, lockh);
3111 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
3112 struct obd_export *dt_exp, struct obd_export *md_exp,
3113 struct lustre_md *md)
3115 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
3116 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3118 if (tgt == NULL || tgt->ltd_exp == NULL)
3121 return md_get_lustre_md(lmv->tgts[0]->ltd_exp, req, dt_exp, md_exp, md);
3124 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
3126 struct obd_device *obd = exp->exp_obd;
3127 struct lmv_obd *lmv = &obd->u.lmv;
3128 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3131 if (md->lmv != NULL) {
3132 lmv_free_memmd(md->lmv);
3135 if (tgt == NULL || tgt->ltd_exp == NULL)
3137 RETURN(md_free_lustre_md(lmv->tgts[0]->ltd_exp, md));
3140 int lmv_set_open_replay_data(struct obd_export *exp,
3141 struct obd_client_handle *och,
3142 struct lookup_intent *it)
3144 struct obd_device *obd = exp->exp_obd;
3145 struct lmv_obd *lmv = &obd->u.lmv;
3146 struct lmv_tgt_desc *tgt;
3149 tgt = lmv_find_target(lmv, &och->och_fid);
3151 RETURN(PTR_ERR(tgt));
3153 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, it));
3156 int lmv_clear_open_replay_data(struct obd_export *exp,
3157 struct obd_client_handle *och)
3159 struct obd_device *obd = exp->exp_obd;
3160 struct lmv_obd *lmv = &obd->u.lmv;
3161 struct lmv_tgt_desc *tgt;
3164 tgt = lmv_find_target(lmv, &och->och_fid);
3166 RETURN(PTR_ERR(tgt));
3168 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
3171 int lmv_intent_getattr_async(struct obd_export *exp,
3172 struct md_enqueue_info *minfo)
3174 struct md_op_data *op_data = &minfo->mi_data;
3175 struct obd_device *obd = exp->exp_obd;
3176 struct lmv_obd *lmv = &obd->u.lmv;
3177 struct lmv_tgt_desc *tgt = NULL;
3181 if (!fid_is_sane(&op_data->op_fid2))
3184 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
3186 RETURN(PTR_ERR(tgt));
3189 * no special handle for remote dir, which needs to fetch both LOOKUP
3190 * lock on parent, and then UPDATE lock on child MDT, which makes all
3191 * complicated because this is done async. So only LOOKUP lock is
3192 * fetched for remote dir, but considering remote dir is rare case,
3193 * and not supporting it in statahead won't cause any issue, just leave
3197 rc = md_intent_getattr_async(tgt->ltd_exp, minfo);
3201 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3202 struct lu_fid *fid, __u64 *bits)
3204 struct obd_device *obd = exp->exp_obd;
3205 struct lmv_obd *lmv = &obd->u.lmv;
3206 struct lmv_tgt_desc *tgt;
3210 tgt = lmv_find_target(lmv, fid);
3212 RETURN(PTR_ERR(tgt));
3214 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3218 int lmv_get_fid_from_lsm(struct obd_export *exp,
3219 const struct lmv_stripe_md *lsm,
3220 const char *name, int namelen, struct lu_fid *fid)
3222 const struct lmv_oinfo *oinfo;
3224 LASSERT(lsm != NULL);
3225 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
3227 return PTR_ERR(oinfo);
3229 *fid = oinfo->lmo_fid;
3235 * For lmv, only need to send request to master MDT, and the master MDT will
3236 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3237 * we directly fetch data from the slave MDTs.
3239 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3240 struct obd_quotactl *oqctl)
3242 struct obd_device *obd = class_exp2obd(exp);
3243 struct lmv_obd *lmv = &obd->u.lmv;
3244 struct lmv_tgt_desc *tgt = lmv->tgts[0];
3247 __u64 curspace, curinodes;
3251 tgt->ltd_exp == NULL ||
3253 lmv->desc.ld_tgt_count == 0) {
3254 CERROR("master lmv inactive\n");
3258 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3259 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3263 curspace = curinodes = 0;
3264 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3268 if (tgt == NULL || tgt->ltd_exp == NULL || !tgt->ltd_active)
3271 err = obd_quotactl(tgt->ltd_exp, oqctl);
3273 CERROR("getquota on mdt %d failed. %d\n", i, err);
3277 curspace += oqctl->qc_dqblk.dqb_curspace;
3278 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3281 oqctl->qc_dqblk.dqb_curspace = curspace;
3282 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3287 static int lmv_merge_attr(struct obd_export *exp,
3288 const struct lmv_stripe_md *lsm,
3289 struct cl_attr *attr,
3290 ldlm_blocking_callback cb_blocking)
3295 rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3299 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3300 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3302 CDEBUG(D_INFO, ""DFID" size %llu, blocks %llu nlink %u,"
3303 " atime %lu ctime %lu, mtime %lu.\n",
3304 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3305 i_size_read(inode), (unsigned long long)inode->i_blocks,
3306 inode->i_nlink, LTIME_S(inode->i_atime),
3307 LTIME_S(inode->i_ctime), LTIME_S(inode->i_mtime));
3309 /* for slave stripe, it needs to subtract nlink for . and .. */
3311 attr->cat_nlink += inode->i_nlink - 2;
3313 attr->cat_nlink = inode->i_nlink;
3315 attr->cat_size += i_size_read(inode);
3316 attr->cat_blocks += inode->i_blocks;
3318 if (attr->cat_atime < LTIME_S(inode->i_atime))
3319 attr->cat_atime = LTIME_S(inode->i_atime);
3321 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3322 attr->cat_ctime = LTIME_S(inode->i_ctime);
3324 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3325 attr->cat_mtime = LTIME_S(inode->i_mtime);
3330 struct obd_ops lmv_obd_ops = {
3331 .o_owner = THIS_MODULE,
3332 .o_setup = lmv_setup,
3333 .o_cleanup = lmv_cleanup,
3334 .o_precleanup = lmv_precleanup,
3335 .o_process_config = lmv_process_config,
3336 .o_connect = lmv_connect,
3337 .o_disconnect = lmv_disconnect,
3338 .o_statfs = lmv_statfs,
3339 .o_get_info = lmv_get_info,
3340 .o_set_info_async = lmv_set_info_async,
3341 .o_notify = lmv_notify,
3342 .o_get_uuid = lmv_get_uuid,
3343 .o_iocontrol = lmv_iocontrol,
3344 .o_quotactl = lmv_quotactl
3347 struct md_ops lmv_md_ops = {
3348 .m_get_root = lmv_get_root,
3349 .m_null_inode = lmv_null_inode,
3350 .m_close = lmv_close,
3351 .m_create = lmv_create,
3352 .m_enqueue = lmv_enqueue,
3353 .m_getattr = lmv_getattr,
3354 .m_getxattr = lmv_getxattr,
3355 .m_getattr_name = lmv_getattr_name,
3356 .m_intent_lock = lmv_intent_lock,
3358 .m_rename = lmv_rename,
3359 .m_setattr = lmv_setattr,
3360 .m_setxattr = lmv_setxattr,
3361 .m_fsync = lmv_fsync,
3362 .m_file_resync = lmv_file_resync,
3363 .m_read_page = lmv_read_page,
3364 .m_unlink = lmv_unlink,
3365 .m_init_ea_size = lmv_init_ea_size,
3366 .m_cancel_unused = lmv_cancel_unused,
3367 .m_set_lock_data = lmv_set_lock_data,
3368 .m_lock_match = lmv_lock_match,
3369 .m_get_lustre_md = lmv_get_lustre_md,
3370 .m_free_lustre_md = lmv_free_lustre_md,
3371 .m_merge_attr = lmv_merge_attr,
3372 .m_set_open_replay_data = lmv_set_open_replay_data,
3373 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3374 .m_intent_getattr_async = lmv_intent_getattr_async,
3375 .m_revalidate_lock = lmv_revalidate_lock,
3376 .m_get_fid_from_lsm = lmv_get_fid_from_lsm,
3377 .m_unpackmd = lmv_unpackmd,
3380 static int __init lmv_init(void)
3382 return class_register_type(&lmv_obd_ops, &lmv_md_ops, true, NULL,
3383 LUSTRE_LMV_NAME, NULL);
3386 static void __exit lmv_exit(void)
3388 class_unregister_type(LUSTRE_LMV_NAME);
3391 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3392 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3393 MODULE_VERSION(LUSTRE_VERSION_STRING);
3394 MODULE_LICENSE("GPL");
3396 module_init(lmv_init);
3397 module_exit(lmv_exit);