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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_handler.c
33 * Lustre Metadata Target (mdt) request handler
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
47 #include <linux/pagemap.h>
49 #include <dt_object.h>
50 #include <lustre_acl.h>
51 #include <lustre_export.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lfsck.h>
54 #include <lustre_log.h>
55 #include <lustre_nodemap.h>
56 #include <lustre_mds.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <lustre_quota.h>
59 #include <lustre_swab.h>
60 #include <lustre_lmv.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
66 #include <lustre_crypto.h>
68 #include "mdt_internal.h"
70 #if OBD_OCD_VERSION(3, 0, 53, 0) > LUSTRE_VERSION_CODE
71 static int mdt_max_mod_rpcs_per_client_set(const char *val,
72 cfs_kernel_param_arg_t *kp)
77 rc = kstrtouint(val, 0, &num);
81 if (num < 1 || num > OBD_MAX_RIF_MAX)
84 CWARN("max_mod_rpcs_per_client is deprecated, set mdt.*.max_mod_rpcs_in_flight parameter instead\n");
86 max_mod_rpcs_per_client = num;
89 static const struct kernel_param_ops
90 param_ops_max_mod_rpcs_per_client = {
91 .set = mdt_max_mod_rpcs_per_client_set,
92 .get = param_get_uint,
95 #define param_check_max_mod_rpcs_per_client(name, p) \
96 __param_check(name, p, unsigned int)
98 module_param_cb(max_mod_rpcs_per_client,
99 ¶m_ops_max_mod_rpcs_per_client,
100 &max_mod_rpcs_per_client, 0644);
102 MODULE_PARM_DESC(max_mod_rpcs_per_client,
103 "maximum number of modify RPCs in flight allowed per client (Deprecated)");
106 mdl_mode_t mdt_mdl_lock_modes[] = {
107 [LCK_MINMODE] = MDL_MINMODE,
114 [LCK_GROUP] = MDL_GROUP
117 enum ldlm_mode mdt_dlm_lock_modes[] = {
118 [MDL_MINMODE] = LCK_MINMODE,
125 [MDL_GROUP] = LCK_GROUP
128 static struct mdt_device *mdt_dev(struct lu_device *d);
130 static const struct lu_object_operations mdt_obj_ops;
132 /* Slab for MDT object allocation */
133 static struct kmem_cache *mdt_object_kmem;
135 /* For HSM restore handles */
136 struct kmem_cache *mdt_hsm_cdt_kmem;
138 /* For HSM request handles */
139 struct kmem_cache *mdt_hsm_car_kmem;
141 static struct lu_kmem_descr mdt_caches[] = {
143 .ckd_cache = &mdt_object_kmem,
144 .ckd_name = "mdt_obj",
145 .ckd_size = sizeof(struct mdt_object)
148 .ckd_cache = &mdt_hsm_cdt_kmem,
149 .ckd_name = "mdt_cdt_restore_handle",
150 .ckd_size = sizeof(struct cdt_restore_handle)
153 .ckd_cache = &mdt_hsm_car_kmem,
154 .ckd_name = "mdt_cdt_agent_req",
155 .ckd_size = sizeof(struct cdt_agent_req)
162 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
166 return rep->lock_policy_res1 & op_flag;
169 void mdt_clear_disposition(struct mdt_thread_info *info,
170 struct ldlm_reply *rep, __u64 op_flag)
173 info->mti_opdata &= ~op_flag;
174 tgt_opdata_clear(info->mti_env, op_flag);
177 rep->lock_policy_res1 &= ~op_flag;
180 void mdt_set_disposition(struct mdt_thread_info *info,
181 struct ldlm_reply *rep, __u64 op_flag)
184 info->mti_opdata |= op_flag;
185 tgt_opdata_set(info->mti_env, op_flag);
188 rep->lock_policy_res1 |= op_flag;
191 /* assert lock is unlocked before reuse */
192 static inline void mdt_lock_handle_assert(struct mdt_lock_handle *lh)
194 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
195 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
196 LASSERT(!lustre_handle_is_used(&lh->mlh_rreg_lh));
199 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
201 mdt_lock_handle_assert(lh);
202 lh->mlh_pdo_hash = 0;
203 lh->mlh_reg_mode = lm;
204 lh->mlh_rreg_mode = lm;
205 lh->mlh_type = MDT_REG_LOCK;
208 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
210 mdt_lock_reg_init(lh, lock->l_req_mode);
211 if (lock->l_req_mode == LCK_GROUP)
212 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
215 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
216 const struct lu_name *lname)
218 mdt_lock_handle_assert(lh);
219 lh->mlh_reg_mode = lock_mode;
220 lh->mlh_pdo_mode = LCK_MINMODE;
221 lh->mlh_rreg_mode = lock_mode;
222 lh->mlh_type = MDT_PDO_LOCK;
224 if (lu_name_is_valid(lname)) {
225 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
227 /* XXX Workaround for LU-2856
229 * Zero is a valid return value of full_name_hash, but several
230 * users of mlh_pdo_hash assume a non-zero hash value. We
231 * therefore map zero onto an arbitrary, but consistent
232 * value (1) to avoid problems further down the road.
234 if (unlikely(lh->mlh_pdo_hash == 0))
235 lh->mlh_pdo_hash = 1;
237 lh->mlh_pdo_hash = 0;
241 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
242 struct mdt_lock_handle *lh)
249 * Any dir access needs couple of locks:
251 * 1) on part of dir we gonna take lookup/modify;
253 * 2) on whole dir to protect it from concurrent splitting and/or to
254 * flush client's cache for readdir().
256 * so, for a given mode and object this routine decides what lock mode
257 * to use for lock #2:
259 * 1) if caller's gonna lookup in dir then we need to protect dir from
260 * being splitted only - LCK_CR
262 * 2) if caller's gonna modify dir then we need to protect dir from
263 * being splitted and to flush cache - LCK_CW
265 * 3) if caller's gonna modify dir and that dir seems ready for
266 * splitting then we need to protect it from any type of access
267 * (lookup/modify/split) - LCK_EX --bzzz
270 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
271 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
274 * Ask underlaying level its opinion about preferable PDO lock mode
275 * having access type passed as regular lock mode:
277 * - MDL_MINMODE means that lower layer does not want to specify lock
280 * - MDL_NL means that no PDO lock should be taken. This is used in some
281 * cases. Say, for non-splittable directories no need to use PDO locks
284 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
285 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
287 if (mode != MDL_MINMODE) {
288 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
291 * Lower layer does not want to specify locking mode. We do it
292 * our selves. No special protection is needed, just flush
293 * client's cache on modification and allow concurrent
296 switch (lh->mlh_reg_mode) {
298 lh->mlh_pdo_mode = LCK_EX;
301 lh->mlh_pdo_mode = LCK_CR;
304 lh->mlh_pdo_mode = LCK_CW;
307 CERROR("Not expected lock type (0x%x)\n",
308 (int)lh->mlh_reg_mode);
313 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
318 * Check whether \a o is directory stripe object.
320 * \param[in] info thread environment
321 * \param[in] o MDT object
323 * \retval 1 is directory stripe.
324 * \retval 0 isn't directory stripe.
325 * \retval < 1 error code
327 static int mdt_is_dir_stripe(struct mdt_thread_info *info,
328 struct mdt_object *o)
330 struct md_attr *ma = &info->mti_attr;
331 struct lmv_mds_md_v1 *lmv;
334 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
338 if (!(ma->ma_valid & MA_LMV))
341 lmv = &ma->ma_lmv->lmv_md_v1;
343 if (!lmv_is_sane2(lmv))
346 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE)
352 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
355 struct mdt_device *mdt = info->mti_mdt;
356 struct lu_name *lname = &info->mti_name;
357 const char *start = fileset;
358 char *filename = info->mti_filename;
359 struct mdt_object *obj;
362 LASSERT(!info->mti_cross_ref);
365 * We may want to allow this to mount a completely separate
366 * fileset from the MDT in the future, but keeping it to
367 * ROOT/ only for now avoid potential security issues.
369 *fid = mdt->mdt_md_root_fid;
371 while (rc == 0 && start != NULL && *start != '\0') {
372 const char *s1 = start;
378 while (*s2 != '/' && *s2 != '\0')
386 lname->ln_namelen = s2 - s1;
387 if (lname->ln_namelen > NAME_MAX) {
392 /* reject .. as a path component */
393 if (lname->ln_namelen == 2 && strncmp(s1, "..", 2) == 0) {
398 strncpy(filename, s1, lname->ln_namelen);
399 filename[lname->ln_namelen] = '\0';
400 lname->ln_name = filename;
402 obj = mdt_object_find(info->mti_env, mdt, fid);
407 /* Only got the fid of this obj by name */
409 rc = mdo_lookup(info->mti_env, mdt_object_child(obj), lname,
410 fid, &info->mti_spec);
411 if (!rc && !S_ISDIR(lu_object_attr(&obj->mot_obj)))
413 mdt_object_put(info->mti_env, obj);
419 static int mdt_get_root(struct tgt_session_info *tsi)
421 struct mdt_thread_info *info = tsi2mdt_info(tsi);
422 struct mdt_device *mdt = info->mti_mdt;
423 struct mdt_body *repbody;
424 char *fileset = NULL, *buffer = NULL;
426 struct obd_export *exp = info->mti_exp;
427 char *nodemap_fileset;
431 rc = mdt_check_ucred(info);
433 GOTO(out, rc = err_serious(rc));
435 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
436 GOTO(out, rc = err_serious(-ENOMEM));
438 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
439 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
440 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
442 GOTO(out, rc = err_serious(-EFAULT));
445 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
446 if (nodemap_fileset && nodemap_fileset[0]) {
447 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
449 /* consider fileset from client as a sub-fileset
452 OBD_ALLOC(buffer, PATH_MAX + 1);
454 GOTO(out, rc = err_serious(-ENOMEM));
455 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
456 nodemap_fileset, fileset) >= PATH_MAX + 1)
457 GOTO(out, rc = err_serious(-EINVAL));
460 /* enforce fileset as specified in the nodemap */
461 fileset = nodemap_fileset;
466 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
467 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
469 GOTO(out, rc = err_serious(rc));
471 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
473 exp->exp_root_fid = repbody->mbo_fid1;
474 repbody->mbo_valid |= OBD_MD_FLID;
478 mdt_thread_info_fini(info);
480 OBD_FREE(buffer, PATH_MAX+1);
484 static int mdt_statfs(struct tgt_session_info *tsi)
486 struct ptlrpc_request *req = tgt_ses_req(tsi);
487 struct mdt_thread_info *info = tsi2mdt_info(tsi);
488 struct mdt_device *mdt = info->mti_mdt;
489 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
490 struct md_device *next = mdt->mdt_child;
491 struct ptlrpc_service_part *svcpt;
492 struct obd_statfs *osfs;
493 struct mdt_body *reqbody = NULL;
494 struct mdt_statfs_cache *msf;
495 ktime_t kstart = ktime_get();
496 int current_blockbits;
502 svcpt = req->rq_rqbd->rqbd_svcpt;
504 /* This will trigger a watchdog timeout */
505 at_est = obd_at_get(mdt->mdt_lu_dev.ld_obd, &svcpt->scp_at_estimate);
506 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
507 (MDT_SERVICE_WATCHDOG_FACTOR * at_est) + 1);
509 rc = mdt_check_ucred(info);
511 GOTO(out, rc = err_serious(rc));
513 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
514 GOTO(out, rc = err_serious(-ENOMEM));
516 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
518 GOTO(out, rc = -EPROTO);
520 if (mdt_is_sum_statfs_client(req->rq_export) &&
521 lustre_packed_msg_size(req->rq_reqmsg) ==
522 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
523 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
524 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
525 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
528 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
529 msf = &mdt->mdt_sum_osfs;
531 msf = &mdt->mdt_osfs;
533 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
534 /** statfs data is too old, get up-to-date one */
535 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
536 rc = next->md_ops->mdo_statfs(info->mti_env, next,
539 rc = dt_statfs(info->mti_env, mdt->mdt_bottom, osfs);
542 spin_lock(&mdt->mdt_lock);
543 msf->msf_osfs = *osfs;
544 msf->msf_age = ktime_get_seconds();
545 spin_unlock(&mdt->mdt_lock);
547 /** use cached statfs data */
548 spin_lock(&mdt->mdt_lock);
549 *osfs = msf->msf_osfs;
550 spin_unlock(&mdt->mdt_lock);
553 /* tgd_blockbit is recordsize bits set during mkfs.
554 * This once set does not change. However, 'zfs set'
555 * can be used to change the MDT blocksize. Instead
556 * of using cached value of 'tgd_blockbit' always
557 * calculate the blocksize bits which may have
560 current_blockbits = fls64(osfs->os_bsize) - 1;
562 /* Account for cached pages. its still racy and might be under-reporting
563 * if clients haven't announced their caches with brw recently
565 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu pending %llu free %llu avail %llu\n",
566 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
567 tgd->tgd_tot_pending,
568 osfs->os_bfree << current_blockbits,
569 osfs->os_bavail << current_blockbits);
571 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
572 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
573 osfs->os_bsize - 1) >> current_blockbits));
575 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
576 if (mdt->mdt_lut.lut_no_create)
577 osfs->os_state |= OS_STATFS_NOCREATE;
578 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
579 "%llu objects: %llu free; state %x\n",
580 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
581 osfs->os_files, osfs->os_ffree, osfs->os_state);
583 if (!exp_grant_param_supp(tsi->tsi_exp) &&
584 current_blockbits > COMPAT_BSIZE_SHIFT) {
585 /* clients which don't support OBD_CONNECT_GRANT_PARAM
586 * should not see a block size > page size, otherwise
587 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
588 * block size which is the biggest block size known to work
589 * with all client's page size.
591 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
592 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
593 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
594 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
597 mdt_counter_incr(req, LPROC_MDT_STATFS,
598 ktime_us_delta(ktime_get(), kstart));
600 mdt_thread_info_fini(info);
604 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
606 struct lov_comp_md_v1 *comp_v1;
607 struct lov_mds_md *v1;
609 __u32 dom_stripesize = 0;
611 bool has_ost_stripes = false;
618 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
621 comp_v1 = (struct lov_comp_md_v1 *)lmm;
622 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
623 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
625 /* Fast check for DoM entry with no mirroring, should be the first */
626 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
627 !(lov_pattern(le32_to_cpu(v1->lmm_pattern)) & LOV_PATTERN_MDT))
630 /* check all entries otherwise */
631 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
632 struct lov_comp_md_entry_v1 *lcme;
634 lcme = &comp_v1->lcm_entries[i];
635 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
638 off = le32_to_cpu(lcme->lcme_offset);
639 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
641 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) &
643 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
645 has_ost_stripes = true;
647 if (dom_stripesize && has_ost_stripes)
648 RETURN(dom_stripesize);
650 /* DoM-only case exits here */
651 if (is_dom_only && dom_stripesize)
653 RETURN(dom_stripesize);
656 /* Pack size attributes into the reply. */
657 int mdt_pack_size2body(struct mdt_thread_info *info,
658 const struct lu_fid *fid, struct lustre_handle *lh)
661 struct md_attr *ma = &info->mti_attr;
663 bool dom_lock = false;
667 LASSERT(ma->ma_attr.la_valid & LA_MODE);
669 if (!S_ISREG(ma->ma_attr.la_mode) ||
670 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
673 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
674 /* no DoM stripe, no size in reply */
678 if (lustre_handle_is_used(lh)) {
679 struct ldlm_lock *lock;
681 lock = ldlm_handle2lock(lh);
683 dom_lock = ldlm_has_dom(lock);
688 /* no DoM lock, no size in reply */
692 /* Either DoM lock exists or LMM has only DoM stripe then
693 * return size on body.
695 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
697 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
701 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
703 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
705 * \param info thread info object
706 * \param repbody reply to pack ACLs into
707 * \param o mdt object of file to examine
708 * \param nodemap nodemap of client to reply to
710 * \retval -errno error getting or parsing ACL from disk
712 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
713 struct mdt_object *o, struct lu_nodemap *nodemap)
715 const struct lu_env *env = info->mti_env;
716 struct md_object *next = mdt_object_child(o);
717 struct lu_buf *buf = &info->mti_buf;
718 struct mdt_device *mdt = info->mti_mdt;
719 struct req_capsule *pill = info->mti_pill;
724 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
725 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
726 if (buf->lb_len == 0)
729 LASSERT(!info->mti_big_acl_used);
731 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
733 if (rc == -ENODATA) {
734 repbody->mbo_aclsize = 0;
735 repbody->mbo_valid |= OBD_MD_FLACL;
737 } else if (rc == -EOPNOTSUPP) {
739 } else if (rc == -ERANGE) {
740 if (exp_connect_large_acl(info->mti_exp) &&
741 !info->mti_big_acl_used) {
742 if (info->mti_big_acl == NULL) {
743 info->mti_big_aclsize =
745 mdt->mdt_max_ea_size,
747 OBD_ALLOC_LARGE(info->mti_big_acl,
748 info->mti_big_aclsize);
749 if (info->mti_big_acl == NULL) {
750 info->mti_big_aclsize = 0;
751 CERROR("%s: unable to grow "
754 PFID(mdt_object_fid(o)));
759 CDEBUG(D_INODE, "%s: grow the "DFID
760 " ACL buffer to size %d\n",
762 PFID(mdt_object_fid(o)),
763 info->mti_big_aclsize);
765 buf->lb_buf = info->mti_big_acl;
766 buf->lb_len = info->mti_big_aclsize;
767 info->mti_big_acl_used = 1;
770 /* FS has ACL bigger that our limits */
771 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
772 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
773 info->mti_big_aclsize);
776 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
777 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
780 rc = nodemap_map_acl(nodemap, buf->lb_buf,
781 rc, NODEMAP_FS_TO_CLIENT);
782 /* if all ACLs mapped out, rc is still >= 0 */
784 CERROR("%s: nodemap_map_acl unable to parse "DFID
785 " ACL: rc = %d\n", mdt_obd_name(mdt),
786 PFID(mdt_object_fid(o)), rc);
787 repbody->mbo_aclsize = 0;
788 repbody->mbo_valid &= ~OBD_MD_FLACL;
790 repbody->mbo_aclsize = rc;
791 repbody->mbo_valid |= OBD_MD_FLACL;
800 /* XXX Look into layout in MDT layer. */
801 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
803 struct lov_comp_md_v1 *comp_v1;
804 struct lov_mds_md *v1;
807 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
808 comp_v1 = (struct lov_comp_md_v1 *)lmm;
810 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
811 v1 = (struct lov_mds_md *)((char *)comp_v1 +
812 comp_v1->lcm_entries[i].lcme_offset);
813 /* We don't support partial release for now */
814 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
819 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
824 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
825 const struct lu_attr *attr, const struct lu_fid *fid)
827 struct mdt_device *mdt = info->mti_mdt;
828 struct obd_export *exp = info->mti_exp;
829 struct md_attr *ma = &info->mti_attr;
830 struct lu_nodemap *nodemap = NULL;
832 LASSERT(ma->ma_valid & MA_INODE);
834 if (attr->la_valid & LA_ATIME) {
835 b->mbo_atime = attr->la_atime;
836 b->mbo_valid |= OBD_MD_FLATIME;
838 if (attr->la_valid & LA_MTIME) {
839 b->mbo_mtime = attr->la_mtime;
840 b->mbo_valid |= OBD_MD_FLMTIME;
842 if (attr->la_valid & LA_CTIME) {
843 b->mbo_ctime = attr->la_ctime;
844 b->mbo_valid |= OBD_MD_FLCTIME;
846 if (attr->la_valid & LA_BTIME) {
847 b->mbo_btime = attr->la_btime;
848 b->mbo_valid |= OBD_MD_FLBTIME;
850 if (attr->la_valid & LA_FLAGS) {
851 b->mbo_flags = attr->la_flags;
852 b->mbo_valid |= OBD_MD_FLFLAGS;
854 if (attr->la_valid & LA_NLINK) {
855 b->mbo_nlink = attr->la_nlink;
856 b->mbo_valid |= OBD_MD_FLNLINK;
858 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
859 nodemap = nodemap_get_from_exp(exp);
863 if (attr->la_valid & LA_UID) {
864 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
865 NODEMAP_FS_TO_CLIENT,
867 b->mbo_valid |= OBD_MD_FLUID;
869 if (attr->la_valid & LA_GID) {
870 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
871 NODEMAP_FS_TO_CLIENT,
873 b->mbo_valid |= OBD_MD_FLGID;
876 if (attr->la_valid & LA_PROJID) {
877 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
878 NODEMAP_FS_TO_CLIENT,
880 b->mbo_valid |= OBD_MD_FLPROJID;
883 b->mbo_mode = attr->la_mode;
884 if (attr->la_valid & LA_MODE)
885 b->mbo_valid |= OBD_MD_FLMODE;
886 if (attr->la_valid & LA_TYPE)
887 b->mbo_valid |= OBD_MD_FLTYPE;
891 b->mbo_valid |= OBD_MD_FLID;
892 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
893 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
896 if (!(attr->la_valid & LA_TYPE))
899 b->mbo_rdev = attr->la_rdev;
900 b->mbo_size = attr->la_size;
901 b->mbo_blocks = attr->la_blocks;
903 if (!S_ISREG(attr->la_mode)) {
904 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
905 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
906 /* means no objects are allocated on osts. */
907 LASSERT(!(ma->ma_valid & MA_LOV));
908 /* just ignore blocks occupied by extend attributes on MDS */
910 /* if no object is allocated on osts, the size on mds is valid.
913 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
914 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
915 if (mdt_hsm_is_released(ma->ma_lmm)) {
916 /* A released file stores its size on MDS. */
917 /* But return 1 block for released file, unless tools
918 * like tar will consider it fully sparse. (LU-3864)
920 if (unlikely(b->mbo_size == 0))
924 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
925 } else if (info->mti_som_strict && mdt->mdt_enable_strict_som) {
926 /* use SOM for size*/
927 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
928 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
929 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
930 b->mbo_size = ma->ma_som.ms_size;
931 b->mbo_blocks = ma->ma_som.ms_blocks;
935 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
936 b->mbo_valid & OBD_MD_FLLAZYSIZE))
937 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
938 PFID(fid), (unsigned long long)b->mbo_size);
941 if (!IS_ERR_OR_NULL(nodemap))
942 nodemap_putref(nodemap);
945 static inline int mdt_body_has_lov(const struct lu_attr *la,
946 const struct mdt_body *body)
948 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
949 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
952 void mdt_client_compatibility(struct mdt_thread_info *info)
954 struct mdt_body *body;
955 struct ptlrpc_request *req = mdt_info_req(info);
956 struct obd_export *exp = req->rq_export;
957 struct md_attr *ma = &info->mti_attr;
958 struct lu_attr *la = &ma->ma_attr;
962 if (exp_connect_layout(exp))
963 /* the client can deal with 16-bit lmm_stripe_count */
966 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
968 if (!mdt_body_has_lov(la, body))
971 /* now we have a reply with a lov for a client not compatible with the
972 * layout lock so we have to clean the layout generation number
974 if (S_ISREG(la->la_mode))
975 ma->ma_lmm->lmm_layout_gen = 0;
979 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
980 struct mdt_object *o)
982 const struct lu_env *env = info->mti_env;
985 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
994 /* Is it a directory? Let's check for the LMV as well */
995 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
996 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
1000 rc2 = mo_xattr_get(env, mdt_object_child(o),
1002 XATTR_NAME_DEFAULT_LMV);
1004 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
1012 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
1015 const struct lu_env *env = info->mti_env;
1020 LASSERT(info->mti_big_lmm_used == 0);
1021 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
1025 /* big_lmm may need to be grown */
1026 if (info->mti_big_lmmsize < rc) {
1027 int size = size_roundup_power2(rc);
1029 if (info->mti_big_lmmsize > 0) {
1030 /* free old buffer */
1031 LASSERT(info->mti_big_lmm);
1032 OBD_FREE_LARGE(info->mti_big_lmm,
1033 info->mti_big_lmmsize);
1034 info->mti_big_lmm = NULL;
1035 info->mti_big_lmmsize = 0;
1038 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1039 if (info->mti_big_lmm == NULL)
1041 info->mti_big_lmmsize = size;
1043 LASSERT(info->mti_big_lmmsize >= rc);
1045 info->mti_buf.lb_buf = info->mti_big_lmm;
1046 info->mti_buf.lb_len = info->mti_big_lmmsize;
1047 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1052 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1053 struct md_attr *ma, const char *name)
1055 struct md_object *next = mdt_object_child(o);
1056 struct lu_buf *buf = &info->mti_buf;
1059 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1060 buf->lb_buf = ma->ma_lmm;
1061 buf->lb_len = ma->ma_lmm_size;
1062 LASSERT(!(ma->ma_valid & MA_LOV));
1063 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1064 buf->lb_buf = ma->ma_lmv;
1065 buf->lb_len = ma->ma_lmv_size;
1066 LASSERT(!(ma->ma_valid & MA_LMV));
1067 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1068 buf->lb_buf = ma->ma_default_lmv;
1069 buf->lb_len = ma->ma_default_lmv_size;
1070 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1075 LASSERT(buf->lb_buf);
1077 if (!mdt_object_exists(o))
1080 if (mdt_object_remote(o) && S_ISDIR(lu_object_attr(&o->mot_obj)))
1081 /* force reload layout for remote dir in case layout changed */
1082 mo_invalidate(info->mti_env, mdt_object_child(o));
1084 rc = mo_xattr_get(info->mti_env, next, buf, name);
1088 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1089 if (info->mti_big_lmm_used)
1090 ma->ma_lmm = info->mti_big_lmm;
1092 /* NOT return LOV EA with hole to old client. */
1093 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1094 LOV_PATTERN_F_HOLE) &&
1095 !(exp_connect_flags(info->mti_exp) &
1096 OBD_CONNECT_LFSCK)) {
1099 ma->ma_lmm_size = rc;
1100 ma->ma_valid |= MA_LOV;
1101 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1102 if (info->mti_big_lmm_used)
1103 ma->ma_lmv = info->mti_big_lmm;
1105 ma->ma_lmv_size = rc;
1106 ma->ma_valid |= MA_LMV;
1107 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1108 ma->ma_default_lmv_size = rc;
1109 ma->ma_valid |= MA_LMV_DEF;
1112 /* Update mdt_max_mdsize so all clients will be aware that */
1113 if (info->mti_mdt->mdt_max_mdsize < rc)
1114 info->mti_mdt->mdt_max_mdsize = rc;
1117 } else if (rc == -ENODATA) {
1120 } else if (rc == -ERANGE) {
1121 /* Default LMV has fixed size, so it must be able to fit
1122 * in the original buffer
1124 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1126 rc = mdt_big_xattr_get(info, o, name);
1128 info->mti_big_lmm_used = 1;
1136 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1137 struct md_attr *ma, const char *name)
1141 if (!info->mti_big_lmm) {
1142 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1143 if (!info->mti_big_lmm)
1145 info->mti_big_lmmsize = PAGE_SIZE;
1148 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1149 ma->ma_lmm = info->mti_big_lmm;
1150 ma->ma_lmm_size = info->mti_big_lmmsize;
1151 ma->ma_valid &= ~MA_LOV;
1152 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1153 ma->ma_lmv = info->mti_big_lmm;
1154 ma->ma_lmv_size = info->mti_big_lmmsize;
1155 ma->ma_valid &= ~MA_LMV;
1160 LASSERT(!info->mti_big_lmm_used);
1161 rc = __mdt_stripe_get(info, o, ma, name);
1162 /* since big_lmm is always used here, clear 'used' flag to avoid
1163 * assertion in mdt_big_xattr_get().
1165 info->mti_big_lmm_used = 0;
1170 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1171 struct lu_fid *pfid)
1173 struct lu_buf *buf = &info->mti_buf;
1174 struct link_ea_header *leh;
1175 struct link_ea_entry *lee;
1180 buf->lb_buf = info->mti_big_lmm;
1181 buf->lb_len = info->mti_big_lmmsize;
1182 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1183 buf, XATTR_NAME_LINK);
1184 /* ignore errors, MA_PFID won't be set and it is
1185 * up to the caller to treat this as an error
1187 if (rc == -ERANGE || buf->lb_len == 0) {
1188 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1189 buf->lb_buf = info->mti_big_lmm;
1190 buf->lb_len = info->mti_big_lmmsize;
1195 if (rc < sizeof(*leh)) {
1196 CERROR("short LinkEA on "DFID": rc = %d\n",
1197 PFID(mdt_object_fid(o)), rc);
1201 leh = (struct link_ea_header *) buf->lb_buf;
1202 lee = (struct link_ea_entry *)(leh + 1);
1203 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1204 leh->leh_magic = LINK_EA_MAGIC;
1205 leh->leh_reccount = __swab32(leh->leh_reccount);
1206 leh->leh_len = __swab64(leh->leh_len);
1208 if (leh->leh_magic != LINK_EA_MAGIC)
1210 if (leh->leh_reccount == 0)
1213 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1214 fid_be_to_cpu(pfid, pfid);
1219 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1220 struct lu_fid *pfid, struct lu_name *lname)
1222 struct lu_buf *buf = &info->mti_buf;
1223 struct link_ea_header *leh;
1224 struct link_ea_entry *lee;
1228 buf->lb_buf = info->mti_xattr_buf;
1229 buf->lb_len = sizeof(info->mti_xattr_buf);
1230 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1232 if (rc == -ERANGE) {
1233 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1234 buf->lb_buf = info->mti_big_lmm;
1235 buf->lb_len = info->mti_big_lmmsize;
1240 if (rc < sizeof(*leh)) {
1241 CERROR("short LinkEA on "DFID": rc = %d\n",
1242 PFID(mdt_object_fid(o)), rc);
1246 leh = (struct link_ea_header *)buf->lb_buf;
1247 lee = (struct link_ea_entry *)(leh + 1);
1248 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1249 leh->leh_magic = LINK_EA_MAGIC;
1250 leh->leh_reccount = __swab32(leh->leh_reccount);
1251 leh->leh_len = __swab64(leh->leh_len);
1253 if (leh->leh_magic != LINK_EA_MAGIC)
1256 if (leh->leh_reccount == 0)
1259 linkea_entry_unpack(lee, &reclen, lname, pfid);
1264 int mdt_attr_get_complex(struct mdt_thread_info *info,
1265 struct mdt_object *o, struct md_attr *ma)
1267 const struct lu_env *env = info->mti_env;
1268 struct md_object *next = mdt_object_child(o);
1269 struct lu_buf *buf = &info->mti_buf;
1270 int need = ma->ma_need;
1278 if (mdt_object_exists(o) == 0)
1279 GOTO(out, rc = -ENOENT);
1280 mode = lu_object_attr(&next->mo_lu);
1282 if (need & MA_INODE) {
1283 ma->ma_need = MA_INODE;
1284 if (need & MA_DIRENT_CNT)
1285 ma->ma_attr.la_valid |= LA_DIRENT_CNT;
1287 ma->ma_attr.la_valid &= ~LA_DIRENT_CNT;
1288 rc = mo_attr_get(env, next, ma);
1293 (void) mdt_get_som(info, o, ma);
1294 ma->ma_valid |= MA_INODE;
1297 if (need & MA_PFID) {
1298 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1300 ma->ma_valid |= MA_PFID;
1301 /* ignore this error, parent fid is not mandatory */
1305 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1306 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1311 if (need & MA_LMV && S_ISDIR(mode)) {
1312 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1317 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1318 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1323 /* In the handle of MA_INODE, we may already get the SOM attr. */
1324 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1325 rc = mdt_get_som(info, o, ma);
1330 if (need & MA_HSM && S_ISREG(mode)) {
1331 buf->lb_buf = info->mti_xattr_buf;
1332 buf->lb_len = sizeof(info->mti_xattr_buf);
1333 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1334 sizeof(info->mti_xattr_buf));
1335 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1336 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1338 ma->ma_valid |= MA_HSM;
1339 else if (rc2 < 0 && rc2 != -ENODATA)
1340 GOTO(out, rc = rc2);
1343 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1344 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1345 buf->lb_buf = ma->ma_acl;
1346 buf->lb_len = ma->ma_acl_size;
1347 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1349 ma->ma_acl_size = rc2;
1350 ma->ma_valid |= MA_ACL_DEF;
1351 } else if (rc2 == -ENODATA) {
1353 ma->ma_acl_size = 0;
1355 GOTO(out, rc = rc2);
1360 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1361 rc, ma->ma_valid, ma->ma_lmm);
1365 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
1367 struct req_capsule *pill = info->mti_pill;
1370 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
1372 /* pre-set size in server part with max size */
1373 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
1375 info->mti_mdt->mdt_max_mdsize);
1379 static int mdt_getattr_internal(struct mdt_thread_info *info,
1380 struct mdt_object *o, int ma_need)
1382 struct mdt_device *mdt = info->mti_mdt;
1383 struct md_object *next = mdt_object_child(o);
1384 const struct mdt_body *reqbody = info->mti_body;
1385 struct ptlrpc_request *req = mdt_info_req(info);
1386 struct md_attr *ma = &info->mti_attr;
1387 struct lu_attr *la = &ma->ma_attr;
1388 struct req_capsule *pill = info->mti_pill;
1389 const struct lu_env *env = info->mti_env;
1390 struct mdt_body *repbody;
1391 struct lu_buf *buffer = &info->mti_buf;
1392 struct obd_export *exp = info->mti_exp;
1393 ktime_t kstart = ktime_get();
1398 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1399 RETURN(err_serious(-ENOMEM));
1401 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1405 if (mdt_object_remote(o)) {
1406 /* obj is located on remote node Return -ENOTSUPP(old client) */
1407 if (!mdt_is_dne_client(req->rq_export))
1408 GOTO(out, rc = -ENOTSUPP);
1410 repbody->mbo_fid1 = *mdt_object_fid(o);
1411 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1415 if (reqbody->mbo_eadatasize > 0) {
1416 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1417 if (buffer->lb_buf == NULL)
1418 GOTO(out, rc = -EPROTO);
1419 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1422 buffer->lb_buf = NULL;
1424 ma_need &= ~(MA_LOV | MA_LMV);
1425 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1426 mdt_obd_name(info->mti_mdt),
1427 req->rq_export->exp_client_uuid.uuid);
1430 /* from 2.12.58 intent_getattr pack default LMV in reply */
1431 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1432 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1433 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1434 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1436 ma->ma_lmv = buffer->lb_buf;
1437 ma->ma_lmv_size = buffer->lb_len;
1438 ma->ma_default_lmv = req_capsule_server_get(pill,
1439 &RMF_DEFAULT_MDT_MD);
1440 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1441 &RMF_DEFAULT_MDT_MD,
1443 ma->ma_need = MA_INODE;
1444 if (ma->ma_lmv_size > 0)
1445 ma->ma_need |= MA_LMV;
1446 if (ma->ma_default_lmv_size > 0)
1447 ma->ma_need |= MA_LMV_DEF;
1448 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1449 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1450 /* If it is dir and client require MEA, then we got MEA */
1451 /* Assumption: MDT_MD size is enough for lmv size. */
1452 ma->ma_lmv = buffer->lb_buf;
1453 ma->ma_lmv_size = buffer->lb_len;
1454 ma->ma_need = MA_INODE;
1455 if (ma->ma_lmv_size > 0) {
1456 if (reqbody->mbo_valid & OBD_MD_MEA) {
1457 ma->ma_need |= MA_LMV;
1458 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1459 ma->ma_need |= MA_LMV_DEF;
1460 ma->ma_default_lmv = buffer->lb_buf;
1462 ma->ma_default_lmv_size = buffer->lb_len;
1463 ma->ma_lmv_size = 0;
1467 ma->ma_lmm = buffer->lb_buf;
1468 ma->ma_lmm_size = buffer->lb_len;
1469 ma->ma_need = MA_INODE | MA_HSM;
1470 if (ma->ma_lmm_size > 0) {
1471 ma->ma_need |= MA_LOV;
1472 /* Older clients may crash if they getattr overstriped
1475 if (!exp_connect_overstriping(exp) &&
1476 mdt_lmm_is_overstriping(ma->ma_lmm))
1477 RETURN(-EOPNOTSUPP);
1481 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1482 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1483 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1484 /* get default stripe info for this dir. */
1485 ma->ma_need |= MA_LOV_DEF;
1487 ma->ma_need |= ma_need;
1489 rc = mdt_attr_get_complex(info, o, ma);
1491 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1492 "%s: getattr error for "DFID": rc = %d\n",
1493 mdt_obd_name(info->mti_mdt),
1494 PFID(mdt_object_fid(o)), rc);
1498 /* return immutable attr on fscrypt metadata files
1499 * if fscrypt admin is not permitted
1501 if (o->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD &&
1502 !mdt_ucred(info)->uc_rbac_fscrypt_admin)
1503 la->la_flags |= LUSTRE_IMMUTABLE_FL;
1505 /* if file is released, check if a restore is running */
1506 if (ma->ma_valid & MA_HSM) {
1507 repbody->mbo_valid |= OBD_MD_TSTATE;
1508 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1509 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1510 repbody->mbo_t_state = MS_RESTORE;
1513 if (unlikely(!(ma->ma_valid & MA_INODE)))
1516 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1518 if (mdt_body_has_lov(la, reqbody)) {
1519 u32 stripe_count = 1;
1520 bool fixed_layout = false;
1522 if (ma->ma_valid & MA_LOV) {
1523 LASSERT(ma->ma_lmm_size);
1524 repbody->mbo_eadatasize = ma->ma_lmm_size;
1525 if (S_ISDIR(la->la_mode))
1526 repbody->mbo_valid |= OBD_MD_FLDIREA;
1528 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1529 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1531 if (ma->ma_valid & MA_LMV) {
1532 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1533 u32 magic = le32_to_cpu(lmv->lmv_magic);
1535 /* Return -ENOTSUPP for old client */
1536 if (!mdt_is_striped_client(req->rq_export))
1539 LASSERT(S_ISDIR(la->la_mode));
1540 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1541 repbody->mbo_eadatasize = ma->ma_lmv_size;
1542 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1544 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1545 fixed_layout = lmv_is_fixed(lmv);
1546 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1547 mdt_restripe_migrate_add(info, o);
1548 else if (magic == LMV_MAGIC_V1 &&
1549 lmv_is_restriping(lmv))
1550 mdt_restripe_update_add(info, o);
1552 if (ma->ma_valid & MA_LMV_DEF) {
1553 /* Return -ENOTSUPP for old client */
1554 if (!mdt_is_striped_client(req->rq_export))
1556 LASSERT(S_ISDIR(la->la_mode));
1558 * when ll_dir_getstripe() gets default LMV, it
1559 * checks mbo_eadatasize.
1561 if (!(ma->ma_valid & MA_LMV))
1562 repbody->mbo_eadatasize =
1563 ma->ma_default_lmv_size;
1564 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1565 OBD_MD_DEFAULT_MEA);
1568 "dirent count %llu stripe count %u MDT count %d\n",
1569 ma->ma_attr.la_dirent_count, stripe_count,
1570 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1571 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1572 ma->ma_attr.la_dirent_count >
1573 mdt->mdt_restriper.mdr_dir_split_count &&
1574 !fid_is_root(mdt_object_fid(o)) &&
1575 mdt->mdt_enable_dir_auto_split &&
1576 !o->mot_restriping &&
1577 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1579 mdt_auto_split_add(info, o);
1580 } else if (S_ISLNK(la->la_mode) &&
1581 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1582 buffer->lb_buf = ma->ma_lmm;
1583 /* eadatasize from client includes NULL-terminator, so
1584 * there is no need to read it
1586 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1587 rc = mo_readlink(env, next, buffer);
1588 if (unlikely(rc <= 0)) {
1589 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1590 mdt_obd_name(info->mti_mdt),
1591 PFID(mdt_object_fid(o)), rc);
1594 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1596 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1598 repbody->mbo_valid |= OBD_MD_LINKNAME;
1599 /* we need to report back size with NULL-terminator
1600 * because client expects that
1602 repbody->mbo_eadatasize = rc + 1;
1603 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1604 CDEBUG(D_INODE, "%s: Read shorter symlink %d on "
1605 DFID ", expected %d\n",
1606 mdt_obd_name(info->mti_mdt),
1607 rc, PFID(mdt_object_fid(o)),
1608 reqbody->mbo_eadatasize - 1);
1609 /* NULL terminate */
1610 ((char *)ma->ma_lmm)[rc] = 0;
1612 /* If the total CDEBUG() size is larger than a page, it
1613 * will print a warning to the console, avoid this by
1614 * printing just the last part of the symlink.
1616 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1617 print_limit < rc ? "..." : "", print_limit,
1618 (char *)ma->ma_lmm + rc - print_limit, rc);
1623 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1624 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1625 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1626 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1627 repbody->mbo_max_mdsize);
1630 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1631 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1632 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1633 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1635 if (IS_ERR(nodemap))
1636 RETURN(PTR_ERR(nodemap));
1638 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1639 nodemap_putref(nodemap);
1645 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1646 ktime_us_delta(ktime_get(), kstart));
1651 static int mdt_getattr(struct tgt_session_info *tsi)
1653 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1654 struct mdt_object *obj = info->mti_object;
1655 struct req_capsule *pill = info->mti_pill;
1656 struct mdt_body *reqbody;
1657 struct mdt_body *repbody;
1662 if (unlikely(info->mti_object == NULL))
1665 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1667 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1669 /* Special case for Data-on-MDT files to get data version */
1670 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1671 rc = mdt_data_version_get(tsi);
1675 /* Unlike intent case where we need to pre-fill out buffers early on
1676 * in intent policy for ldlm reasons, here we can have a much better
1677 * guess at EA size by just reading it from disk.
1678 * Exceptions are readdir and (missing) directory striping
1680 if (reqbody->mbo_valid & OBD_MD_LINKNAME) { /* Readlink */
1681 /* No easy way to know how long is the symlink, but it cannot
1682 * be more than PATH_MAX, so we allocate +1
1685 /* A special case for fs ROOT: getattr there might fetch
1686 * default EA for entire fs, not just for this dir!
1688 } else if (lu_fid_eq(mdt_object_fid(obj),
1689 &info->mti_mdt->mdt_md_root_fid) &&
1690 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1691 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1693 /* Should the default strping be bigger, mdt_fix_reply
1696 rc = DEF_REP_MD_SIZE;
1698 /* Read the actual EA size from disk */
1699 rc = mdt_attr_get_eabuf_size(info, obj);
1703 GOTO(out, rc = err_serious(rc));
1705 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1707 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1708 * by default. If the target object has more ACL entries, then
1709 * enlarge the buffer when necessary.
1711 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1712 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1713 mdt_preset_encctx_size(info);
1715 rc = req_capsule_server_pack(pill);
1716 if (unlikely(rc != 0))
1717 GOTO(out, rc = err_serious(rc));
1719 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1720 LASSERT(repbody != NULL);
1721 repbody->mbo_eadatasize = 0;
1722 repbody->mbo_aclsize = 0;
1724 rc = mdt_check_ucred(info);
1726 GOTO(out_shrink, rc);
1728 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1730 rc = mdt_init_ucred(info, reqbody);
1732 GOTO(out_shrink, rc);
1734 rc = mdt_getattr_internal(info, obj, 0);
1736 GOTO(out_ucred, rc);
1738 rc = mdt_pack_encctx_in_reply(info, obj);
1741 mdt_exit_ucred(info);
1743 mdt_client_compatibility(info);
1744 rc2 = mdt_fix_reply(info);
1748 mdt_thread_info_fini(info);
1753 * Handler of layout intent RPC requiring the layout modification
1755 * \param[in] info thread environment
1756 * \param[in] obj object
1757 * \param[out] lhc object ldlm lock handle
1758 * \param[in] layout layout change descriptor
1760 * \retval 0 on success
1761 * \retval < 0 error code
1763 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1764 struct mdt_lock_handle *lhc,
1765 struct md_layout_change *layout)
1771 if (!mdt_object_exists(obj))
1774 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1777 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1782 rc = mdt_check_resent_lock(info, obj, lhc);
1788 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1790 /* take layout lock to prepare layout change */
1791 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1792 lockpart |= MDS_INODELOCK_UPDATE;
1794 rc = mdt_object_lock(info, obj, lhc, lockpart, LCK_EX);
1799 mutex_lock(&obj->mot_som_mutex);
1800 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1801 mutex_unlock(&obj->mot_som_mutex);
1804 mdt_object_unlock(info, obj, lhc, 1);
1810 * Exchange MOF_LOV_CREATED flags between two objects after a
1811 * layout swap. No assumption is made on whether o1 or o2 have
1812 * created objects or not.
1814 * \param[in,out] o1 First swap layout object
1815 * \param[in,out] o2 Second swap layout object
1817 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1819 unsigned int o1_lov_created = o1->mot_lov_created;
1821 mutex_lock(&o1->mot_lov_mutex);
1822 mutex_lock(&o2->mot_lov_mutex);
1824 o1->mot_lov_created = o2->mot_lov_created;
1825 o2->mot_lov_created = o1_lov_created;
1827 mutex_unlock(&o2->mot_lov_mutex);
1828 mutex_unlock(&o1->mot_lov_mutex);
1831 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1833 struct mdt_thread_info *info;
1834 struct ptlrpc_request *req = tgt_ses_req(tsi);
1835 struct obd_export *exp = req->rq_export;
1836 struct mdt_object *o1, *o2, *o;
1837 struct mdt_lock_handle *lh1, *lh2;
1838 struct mdc_swap_layouts *msl;
1843 /* client does not support layout lock, so layout swaping
1845 * FIXME: there is a problem for old clients which don't support
1846 * layout lock yet. If those clients have already opened the file
1847 * they won't be notified at all so that old layout may still be
1848 * used to do IO. This can be fixed after file release is landed by
1849 * doing exclusive open and taking full EX ibits lock. - Jinshan
1851 if (!exp_connect_layout(exp))
1852 RETURN(-EOPNOTSUPP);
1854 info = tsi2mdt_info(tsi);
1855 if (unlikely(info->mti_object == NULL))
1858 if (info->mti_dlm_req != NULL)
1859 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1861 o1 = info->mti_object;
1862 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1863 &info->mti_body->mbo_fid2);
1865 GOTO(out, rc = PTR_ERR(o));
1867 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1868 GOTO(put, rc = -ENOENT);
1870 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1871 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1877 /* permission check. Make sure the calling process having permission
1878 * to write both files.
1880 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1885 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1890 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1892 GOTO(put, rc = -EPROTO);
1894 lh1 = &info->mti_lh[MDT_LH_NEW];
1895 lh2 = &info->mti_lh[MDT_LH_OLD];
1896 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1897 MDS_INODELOCK_XATTR, LCK_EX);
1901 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1902 MDS_INODELOCK_XATTR, LCK_EX);
1906 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1907 mdt_object_child(o2), msl->msl_flags);
1911 mdt_swap_lov_flag(o1, o2);
1914 mdt_object_unlock(info, o2, lh2, rc);
1916 mdt_object_unlock(info, o1, lh1, rc);
1918 mdt_object_put(info->mti_env, o);
1920 mdt_thread_info_fini(info);
1924 static int mdt_raw_lookup(struct mdt_thread_info *info,
1925 struct mdt_object *parent,
1926 const struct lu_name *lname)
1928 struct lu_fid *fid = &info->mti_tmp_fid1;
1929 struct mdt_body *repbody;
1930 bool is_dotdot = false;
1931 bool is_old_parent_stripe = false;
1932 bool is_new_parent_checked = false;
1937 LASSERT(!info->mti_cross_ref);
1938 /* Always allow to lookup ".." */
1939 if (lname->ln_namelen == 2 &&
1940 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1941 info->mti_spec.sp_permitted = 1;
1943 if (mdt_is_dir_stripe(info, parent) == 1)
1944 is_old_parent_stripe = true;
1947 mdt_object_get(info->mti_env, parent);
1949 /* Only got the fid of this obj by name */
1951 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1953 mdt_object_put(info->mti_env, parent);
1957 /* getattr_name("..") should return master object FID for striped dir */
1958 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1959 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1961 RETURN(PTR_ERR(parent));
1963 /* old client getattr_name("..") with stripe FID */
1964 if (unlikely(is_old_parent_stripe)) {
1965 is_old_parent_stripe = false;
1969 /* ".." may be a stripe */
1970 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1971 is_new_parent_checked = true;
1975 mdt_object_put(info->mti_env, parent);
1978 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1979 repbody->mbo_fid1 = *fid;
1980 repbody->mbo_valid = OBD_MD_FLID;
1986 * Find name matching hash
1988 * We search \a child LinkEA for a name whose hash matches \a lname
1989 * (it contains an encoded hash).
1991 * \param info mdt thread info
1992 * \param lname encoded hash to find
1993 * \param parent parent object
1994 * \param child object to search with LinkEA
1996 * \retval 1 match found
1997 * \retval 0 no match found
1998 * \retval -ev negative errno upon error
2000 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
2001 struct mdt_object *parent, struct mdt_object *child)
2003 /* Here, lname is an encoded hash of on-disk name, and
2004 * client is doing access without encryption key.
2005 * So we need to get LinkEA, check parent fid is correct and
2006 * compare name hash with the one in the request.
2008 struct lu_buf *buf = &info->mti_big_buf;
2009 struct lu_name name;
2011 struct linkea_data ldata = { NULL };
2012 struct link_ea_header *leh;
2013 struct link_ea_entry *lee;
2014 struct lu_buf link = { 0 };
2016 int reclen, count, rc;
2019 if (lname->ln_namelen < LL_CRYPTO_BLOCK_SIZE)
2022 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2027 rc = mdt_links_read(info, child, &ldata);
2031 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
2034 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
2037 lee = (struct link_ea_entry *)(leh + 1);
2038 for (count = 0; count < leh->leh_reccount; count++) {
2039 linkea_entry_unpack(lee, &reclen, &name, &pfid);
2040 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
2041 lu_buf_check_and_alloc(&link, name.ln_namelen);
2043 GOTO(out_match, rc = -ENOMEM);
2044 rc = critical_decode(name.ln_name, name.ln_namelen,
2047 if (memcmp(LLCRYPT_EXTRACT_DIGEST(link.lb_buf, rc),
2048 hash, LL_CRYPTO_BLOCK_SIZE) == 0) {
2053 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2055 if (count == leh->leh_reccount)
2068 * UPDATE lock should be taken against parent, and be released before exit;
2069 * child_bits lock should be taken against child, and be returned back:
2070 * (1)normal request should release the child lock;
2071 * (2)intent request will grant the lock to client.
2073 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2074 struct mdt_lock_handle *lhc,
2076 struct ldlm_reply *ldlm_rep)
2078 struct ptlrpc_request *req = mdt_info_req(info);
2079 struct mdt_body *reqbody = NULL;
2080 struct mdt_object *parent = info->mti_object;
2081 struct mdt_object *child = NULL;
2082 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2083 struct lu_name *lname = NULL;
2084 struct mdt_lock_handle *lhp = NULL;
2085 struct ldlm_lock *lock;
2086 struct req_capsule *pill = info->mti_pill;
2087 bool fscrypt_md = false;
2095 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2096 LASSERT(ergo(is_resent,
2097 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2102 if (info->mti_mdt->mdt_enable_dir_auto_split)
2103 ma_need |= MA_DIRENT_CNT;
2105 if (info->mti_cross_ref) {
2106 /* Only getattr on the child. Parent is on another node. */
2107 mdt_set_disposition(info, ldlm_rep,
2108 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2110 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", ldlm_rep = %p\n",
2111 PFID(mdt_object_fid(child)), ldlm_rep);
2113 rc = mdt_check_resent_lock(info, child, lhc);
2116 } else if (rc > 0) {
2118 * Object's name entry is on another MDS, it will
2119 * request PERM lock only because LOOKUP lock is owned
2120 * by the MDS where name entry resides.
2122 * TODO: it should try layout lock too. - Jinshan
2124 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2125 MDS_INODELOCK_LAYOUT);
2126 child_bits |= MDS_INODELOCK_PERM;
2127 rc = mdt_object_lock(info, child, lhc, child_bits,
2133 /* Finally, we can get attr for child. */
2134 if (!mdt_object_exists(child)) {
2135 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2137 "remote object doesn't exist.");
2138 mdt_object_unlock(info, child, lhc, 1);
2142 rc = mdt_getattr_internal(info, child, ma_need);
2143 if (unlikely(rc != 0)) {
2144 mdt_object_unlock(info, child, lhc, 1);
2148 rc = mdt_pack_secctx_in_reply(info, child);
2150 mdt_object_unlock(info, child, lhc, 1);
2154 rc = mdt_pack_encctx_in_reply(info, child);
2156 mdt_object_unlock(info, child, lhc, 1);
2160 lname = &info->mti_name;
2161 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2163 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2164 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2165 if (unlikely(reqbody == NULL))
2166 RETURN(err_serious(-EPROTO));
2168 *child_fid = reqbody->mbo_fid2;
2169 if (unlikely(!fid_is_sane(child_fid)))
2170 RETURN(err_serious(-EINVAL));
2172 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2173 mdt_object_get(info->mti_env, parent);
2176 child = mdt_object_find(info->mti_env, info->mti_mdt,
2179 RETURN(PTR_ERR(child));
2182 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", ldlm_rep = %p\n",
2183 PFID(mdt_object_fid(parent)),
2184 PFID(&reqbody->mbo_fid2), ldlm_rep);
2185 } else if (lu_name_is_valid(lname)) {
2186 if (mdt_object_remote(parent)) {
2187 CERROR("%s: parent "DFID" is on remote target\n",
2188 mdt_obd_name(info->mti_mdt),
2189 PFID(mdt_object_fid(parent)));
2193 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", ldlm_rep = %p\n",
2194 PFID(mdt_object_fid(parent)),
2195 PNAME(lname), ldlm_rep);
2197 if (parent->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD ||
2198 (fid_is_root(mdt_object_fid(parent)) &&
2199 lname->ln_namelen == strlen(dot_fscrypt_name) &&
2200 strncmp(lname->ln_name, dot_fscrypt_name,
2201 lname->ln_namelen) == 0))
2204 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2205 if (unlikely(reqbody == NULL))
2206 RETURN(err_serious(-EPROTO));
2208 *child_fid = reqbody->mbo_fid2;
2209 if (unlikely(!fid_is_sane(child_fid)))
2210 RETURN(err_serious(-EINVAL));
2212 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2213 mdt_object_get(info->mti_env, parent);
2216 child = mdt_object_find(info->mti_env, info->mti_mdt,
2219 RETURN(PTR_ERR(child));
2222 if (mdt_object_remote(child)) {
2223 CERROR("%s: child "DFID" is on remote target\n",
2224 mdt_obd_name(info->mti_mdt),
2225 PFID(mdt_object_fid(child)));
2226 GOTO(out_child, rc = -EPROTO);
2229 /* don't fetch LOOKUP lock if it's remote object */
2230 rc = mdt_is_remote_object(info, parent, child);
2232 GOTO(out_child, rc);
2234 child_bits &= ~MDS_INODELOCK_LOOKUP;
2236 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", ldlm_rep = %p\n",
2237 PFID(mdt_object_fid(parent)),
2238 PFID(&reqbody->mbo_fid2), ldlm_rep);
2241 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2243 if (unlikely(!mdt_object_exists(parent)) &&
2244 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2245 lu_name_is_valid(lname)) {
2246 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2248 "Parent doesn't exist!");
2249 GOTO(out_child, rc = -ESTALE);
2252 if (!child && is_resent) {
2253 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2255 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2256 * a resend case, however, it could be already destroyed
2257 * due to client eviction or a raced cancel RPC.
2259 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2260 lhc->mlh_reg_lh.cookie);
2263 fid_extract_from_res_name(child_fid,
2264 &lock->l_resource->lr_name);
2265 LDLM_LOCK_PUT(lock);
2266 child = mdt_object_find(info->mti_env, info->mti_mdt,
2269 RETURN(PTR_ERR(child));
2270 } else if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2271 lu_name_is_valid(lname)) {
2272 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2273 rc = mdt_raw_lookup(info, parent, lname);
2278 /* step 1: lock parent only if parent is a directory */
2279 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2280 lhp = &info->mti_lh[MDT_LH_PARENT];
2281 rc = mdt_parent_lock(info, parent, lhp, lname, LCK_PR);
2282 if (unlikely(rc != 0))
2286 /* step 2: lookup child's fid by name */
2287 fid_zero(child_fid);
2288 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2289 child_fid, &info->mti_spec);
2291 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2294 GOTO(unlock_parent, rc);
2296 child = mdt_object_find(info->mti_env, info->mti_mdt,
2298 if (unlikely(IS_ERR(child)))
2299 GOTO(unlock_parent, rc = PTR_ERR(child));
2302 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2304 /* step 3: lock child regardless if it is local or remote. */
2307 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2308 /* Here, lname is an encoded hash of on-disk name, and
2309 * client is doing access without encryption key.
2310 * So we need to compare name hash with the one in the request.
2312 if (!find_name_matching_hash(info, lname, parent,
2314 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2315 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2316 GOTO(out_child, rc = -ENOENT);
2320 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2321 if (!mdt_object_exists(child)) {
2322 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2324 "Object doesn't exist!");
2325 GOTO(out_child, rc = -ENOENT);
2328 rc = mdt_check_resent_lock(info, child, lhc);
2330 GOTO(out_child, rc);
2331 } else if (rc > 0) {
2332 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2333 !mdt_object_remote(child)) {
2334 struct md_attr *ma = &info->mti_attr;
2337 ma->ma_need = MA_INODE;
2338 rc = mdt_attr_get_complex(info, child, ma);
2339 if (unlikely(rc != 0))
2340 GOTO(out_child, rc);
2342 /* If the file has not been changed for some time, we
2343 * return not only a LOOKUP lock, but also an UPDATE
2344 * lock and this might save us RPC on later STAT. For
2345 * directories, it also let negative dentry cache start
2346 * working for this dir.
2348 if (ma->ma_valid & MA_INODE &&
2349 ma->ma_attr.la_valid & LA_CTIME &&
2350 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2351 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2352 child_bits |= MDS_INODELOCK_UPDATE;
2355 /* layout lock must be granted in a best-effort way
2358 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2359 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2360 !mdt_object_remote(child) && ldlm_rep != NULL) {
2361 if (!CFS_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2362 exp_connect_layout(info->mti_exp)) {
2363 /* try to grant layout lock for regular file. */
2364 try_bits = MDS_INODELOCK_LAYOUT;
2366 /* Acquire DOM lock in advance for data-on-mdt file */
2367 if (child != parent)
2368 try_bits |= MDS_INODELOCK_DOM;
2372 * To avoid possible deadlock between batched statahead RPC
2373 * and rename()/migrate() operation, it should use trylock to
2374 * obtain the DLM PR ibits lock for file attributes in a
2375 * batched statahead RPC. A failed trylock means that other
2376 * users maybe modify the directory simultaneously as in current
2377 * Lustre design the server only grants read lock to a client.
2379 * When a trylock failed, the MDT reports the conflict with
2380 * error code -EBUSY, and stops statahead immediately.
2382 if (info->mti_batch_env) {
2384 * This is a sub stat-ahead request in a batched RPC.
2385 * However, the @child is a remote object, we just
2386 * return -EREMOTE here to forbid stat-ahead on it.
2388 if (mdt_object_remote(child))
2389 GOTO(out_child, rc = -EREMOTE);
2391 try_bits |= child_bits;
2395 if (try_bits != 0) {
2396 /* try layout lock, it may fail to be granted due to
2397 * contention at LOOKUP or UPDATE
2399 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2401 if (child_bits & MDS_INODELOCK_LAYOUT)
2404 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2405 * client will enqueue the lock to the remote MDT
2407 if (mdt_object_remote(child))
2408 rc = mdt_object_lookup_lock(info, NULL, child,
2411 rc = mdt_object_lock(info, child, lhc,
2412 child_bits, LCK_PR);
2414 if (unlikely(rc != 0))
2415 GOTO(out_child, rc);
2416 if (info->mti_batch_env && child_bits == 0) {
2418 mdt_object_unlock(info, child, lhc, 1);
2419 GOTO(out_child, rc = -EBUSY);
2424 child->mot_obj.lo_header->loh_attr |= LOHA_FSCRYPT_MD;
2426 /* finally, we can get attr for child. */
2427 rc = mdt_getattr_internal(info, child, ma_need);
2428 if (unlikely(rc != 0)) {
2430 mdt_object_unlock(info, child, lhc, 1);
2431 GOTO(out_child, rc);
2434 rc = mdt_pack_secctx_in_reply(info, child);
2437 mdt_object_unlock(info, child, lhc, 1);
2438 GOTO(out_child, rc);
2441 rc = mdt_pack_encctx_in_reply(info, child);
2444 mdt_object_unlock(info, child, lhc, 1);
2445 GOTO(out_child, rc);
2448 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2450 /* Debugging code. */
2451 LDLM_DEBUG(lock, "Returning lock to client");
2452 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2453 &lock->l_resource->lr_name),
2454 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2455 PLDLMRES(lock->l_resource),
2456 PFID(mdt_object_fid(child)));
2458 if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2459 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2460 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2462 req->rq_arrival_time.tv_sec +
2464 /* Put the lock to the waiting list and force the cancel */
2465 ldlm_set_ast_sent(lock);
2468 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2469 !mdt_object_remote(child) && child != parent) {
2470 mdt_object_put(info->mti_env, child);
2471 rc = mdt_pack_size2body(info, child_fid,
2473 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2474 /* DOM lock was taken in advance but this is
2475 * not DoM file. Drop the lock.
2477 lock_res_and_lock(lock);
2478 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2479 unlock_res_and_lock(lock);
2481 LDLM_LOCK_PUT(lock);
2482 GOTO(unlock_parent, rc = 0);
2484 LDLM_LOCK_PUT(lock);
2490 mdt_object_put(info->mti_env, child);
2493 mdt_object_unlock(info, parent, lhp, 1);
2494 if (rc == -ENOENT) {
2495 /* return -ENOKEY instead of -ENOENT to encryption-unaware
2496 * client if trying to access an encrypted file
2498 int rc2 = mdt_check_enc(info, parent);
2506 /* normal handler: should release the child lock */
2507 static int mdt_getattr_name(struct tgt_session_info *tsi)
2509 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2510 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2511 struct mdt_body *reqbody;
2512 struct mdt_body *repbody;
2517 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2518 LASSERT(reqbody != NULL);
2519 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2520 LASSERT(repbody != NULL);
2522 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2523 repbody->mbo_eadatasize = 0;
2524 repbody->mbo_aclsize = 0;
2526 rc = mdt_init_ucred(info, reqbody);
2528 GOTO(out_shrink, rc);
2530 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2531 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2532 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2533 lhc->mlh_reg_lh.cookie = 0;
2535 mdt_exit_ucred(info);
2538 mdt_client_compatibility(info);
2539 rc2 = mdt_fix_reply(info);
2542 mdt_thread_info_fini(info);
2546 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2547 const struct lu_fid *pfid,
2548 const struct lu_name *name,
2549 struct mdt_object *obj, s64 ctime)
2551 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2552 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
2553 struct mdt_device *mdt = info->mti_mdt;
2554 struct md_attr *ma = &info->mti_attr;
2555 struct mdt_lock_handle *parent_lh;
2556 struct mdt_lock_handle *child_lh;
2557 struct mdt_object *pobj;
2562 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2564 GOTO(out, rc = PTR_ERR(pobj));
2566 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2567 rc = mdt_parent_lock(info, pobj, parent_lh, name, LCK_PW);
2569 GOTO(put_parent, rc);
2571 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2572 name, child_fid, &info->mti_spec);
2574 GOTO(unlock_parent, rc);
2576 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2577 GOTO(unlock_parent, rc = -EREMCHG);
2579 child_lh = &info->mti_lh[MDT_LH_CHILD];
2580 rc = mdt_object_stripes_lock(info, pobj, obj, child_lh, einfo,
2581 MDS_INODELOCK_LOOKUP |
2582 MDS_INODELOCK_UPDATE, LCK_EX);
2584 GOTO(unlock_parent, rc);
2586 if (atomic_read(&obj->mot_open_count)) {
2587 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2588 PFID(mdt_object_fid(obj)));
2589 GOTO(unlock_child, rc = -EBUSY);
2593 ma->ma_valid = MA_INODE;
2594 ma->ma_attr.la_valid = LA_CTIME;
2595 ma->ma_attr.la_ctime = ctime;
2597 mutex_lock(&obj->mot_lov_mutex);
2599 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2600 mdt_object_child(obj), name, ma, 0);
2602 mutex_unlock(&obj->mot_lov_mutex);
2605 mdt_object_stripes_unlock(info, obj, child_lh, einfo, 1);
2607 mdt_object_unlock(info, pobj, parent_lh, 1);
2609 mdt_object_put(info->mti_env, pobj);
2614 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2615 struct mdt_object *obj)
2617 struct lu_ucred *uc = lu_ucred(info->mti_env);
2618 struct md_attr *ma = &info->mti_attr;
2619 struct lu_attr *la = &ma->ma_attr;
2624 ma->ma_need = MA_INODE;
2625 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2629 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2632 /* we want rbac roles to have precedence over any other
2633 * permission or capability checks
2635 if (!uc->uc_rbac_byfid_ops)
2637 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2639 if (uc->uc_fsuid == la->la_uid) {
2640 if ((la->la_mode & 0200) == 0)
2642 } else if (uc->uc_fsgid == la->la_gid) {
2643 if ((la->la_mode & 0020) == 0)
2645 } else if ((la->la_mode & 0002) == 0) {
2653 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2656 struct mdt_device *mdt = info->mti_mdt;
2657 struct mdt_object *obj = NULL;
2658 struct linkea_data ldata = { NULL };
2659 struct lu_buf *buf = &info->mti_big_buf;
2660 struct lu_name *name = &info->mti_name;
2661 struct lu_fid *pfid = &info->mti_tmp_fid1;
2662 struct link_ea_header *leh;
2663 struct link_ea_entry *lee;
2664 int reclen, count, rc = 0;
2668 if (!fid_is_sane(fid))
2669 GOTO(out, rc = -EINVAL);
2671 if (!fid_is_namespace_visible(fid))
2672 GOTO(out, rc = -EINVAL);
2674 obj = mdt_object_find(info->mti_env, mdt, fid);
2676 GOTO(out, rc = PTR_ERR(obj));
2678 if (mdt_object_remote(obj))
2679 GOTO(out, rc = -EREMOTE);
2680 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2681 GOTO(out, rc = -ENOENT);
2683 rc = mdt_rmfid_check_permission(info, obj);
2688 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2690 GOTO(out, rc = -ENOMEM);
2693 rc = mdt_links_read(info, obj, &ldata);
2698 lee = (struct link_ea_entry *)(leh + 1);
2699 for (count = 0; count < leh->leh_reccount; count++) {
2700 /* remove every hardlink */
2701 linkea_entry_unpack(lee, &reclen, name, pfid);
2702 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2703 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2709 if (obj && !IS_ERR(obj))
2710 mdt_object_put(info->mti_env, obj);
2711 if (info->mti_big_buf.lb_buf)
2712 lu_buf_free(&info->mti_big_buf);
2717 static int mdt_rmfid(struct tgt_session_info *tsi)
2719 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2720 struct mdt_body *reqbody;
2721 struct lu_fid *fids, *rfids;
2728 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2729 if (reqbody == NULL)
2731 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2733 nr = bufsize / sizeof(struct lu_fid);
2734 if (nr * sizeof(struct lu_fid) != bufsize)
2736 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2737 RCL_SERVER, nr * sizeof(__u32));
2738 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2739 RCL_SERVER, nr * sizeof(struct lu_fid));
2740 rc = req_capsule_server_pack(tsi->tsi_pill);
2742 GOTO(out, rc = err_serious(rc));
2743 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2746 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2748 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2751 mdt_init_ucred(mti, reqbody);
2752 for (i = 0; i < nr; i++) {
2754 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2756 mdt_exit_ucred(mti);
2762 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2763 void *karg, void __user *uarg);
2765 int mdt_io_set_info(struct tgt_session_info *tsi)
2767 struct ptlrpc_request *req = tgt_ses_req(tsi);
2768 struct ost_body *body = NULL, *repbody;
2769 void *key, *val = NULL;
2770 int keylen, vallen, rc = 0;
2771 bool is_grant_shrink;
2775 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2777 DEBUG_REQ(D_HA, req, "no set_info key");
2778 RETURN(err_serious(-EFAULT));
2780 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2783 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2785 DEBUG_REQ(D_HA, req, "no set_info val");
2786 RETURN(err_serious(-EFAULT));
2788 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2791 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
2792 if (is_grant_shrink)
2793 /* In this case the value is actually an RMF_OST_BODY, so we
2794 * transmutate the type of this PTLRPC
2796 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
2798 rc = req_capsule_server_pack(tsi->tsi_pill);
2802 if (is_grant_shrink) {
2803 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
2805 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2808 /** handle grant shrink, similar to a read request */
2809 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
2812 CERROR("%s: Unsupported key %s\n",
2813 tgt_name(tsi->tsi_tgt), (char *)key);
2821 static int mdt_set_info(struct tgt_session_info *tsi)
2823 struct ptlrpc_request *req = tgt_ses_req(tsi);
2826 int keylen, vallen, rc = 0;
2830 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2832 DEBUG_REQ(D_HA, req, "no set_info key");
2833 RETURN(err_serious(-EFAULT));
2836 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2839 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2841 DEBUG_REQ(D_HA, req, "no set_info val");
2842 RETURN(err_serious(-EFAULT));
2845 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2848 /* Swab any part of val you need to here */
2849 if (KEY_IS(KEY_READ_ONLY)) {
2850 spin_lock(&req->rq_export->exp_lock);
2852 *exp_connect_flags_ptr(req->rq_export) |=
2855 *exp_connect_flags_ptr(req->rq_export) &=
2856 ~OBD_CONNECT_RDONLY;
2857 spin_unlock(&req->rq_export->exp_lock);
2858 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2859 struct changelog_setinfo *cs = val;
2861 if (vallen != sizeof(*cs)) {
2862 CERROR("%s: bad changelog_clear setinfo size %d\n",
2863 tgt_name(tsi->tsi_tgt), vallen);
2866 if (req_capsule_req_need_swab(&req->rq_pill)) {
2867 __swab64s(&cs->cs_recno);
2868 __swab32s(&cs->cs_id);
2871 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
2873 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2875 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2877 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2884 static int mdt_readpage(struct tgt_session_info *tsi)
2886 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2887 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2888 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2889 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2890 struct mdt_body *repbody;
2896 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2897 RETURN(err_serious(-ENOMEM));
2899 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2900 if (repbody == NULL || reqbody == NULL)
2901 RETURN(err_serious(-EFAULT));
2904 * prepare @rdpg before calling lower layers and transfer itself. Here
2905 * reqbody->size contains offset of where to start to read and
2906 * reqbody->nlink contains number bytes to read.
2908 rdpg->rp_hash = reqbody->mbo_size;
2909 if (rdpg->rp_hash != reqbody->mbo_size) {
2910 CERROR("Invalid hash: %#llx != %#llx\n",
2911 rdpg->rp_hash, reqbody->mbo_size);
2915 rdpg->rp_attrs = reqbody->mbo_mode;
2916 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2917 rdpg->rp_attrs |= LUDA_64BITHASH;
2918 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2919 exp_max_brw_size(tsi->tsi_exp));
2920 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2922 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2923 if (rdpg->rp_pages == NULL)
2926 for (i = 0; i < rdpg->rp_npages; ++i) {
2927 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2928 if (rdpg->rp_pages[i] == NULL)
2929 GOTO(free_rdpg, rc = -ENOMEM);
2932 /* call lower layers to fill allocated pages with directory data */
2933 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2935 GOTO(free_rdpg, rc);
2937 /* send pages to client */
2938 rc = tgt_sendpage(tsi, rdpg, rc);
2943 for (i = 0; i < rdpg->rp_npages; i++)
2944 if (rdpg->rp_pages[i] != NULL)
2945 __free_page(rdpg->rp_pages[i]);
2946 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2948 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2954 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2956 struct lu_ucred *uc = mdt_ucred_check(info);
2957 struct lu_attr *attr = &info->mti_attr.ma_attr;
2962 if (op != REINT_SETATTR) {
2963 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2964 attr->la_uid = uc->uc_fsuid;
2965 /* for S_ISGID, inherit gid from his parent, such work will be
2966 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid.
2968 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2969 attr->la_gid = uc->uc_fsgid;
2975 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2977 return op == REINT_OPEN &&
2978 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2981 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2983 struct req_capsule *pill = info->mti_pill;
2985 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2987 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2989 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2991 /* pre-set size in server part with max size */
2992 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2994 req_capsule_ptlreq(pill) ?
2995 OBD_MAX_DEFAULT_EA_SIZE :
2998 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
3003 static int mdt_reint_internal(struct mdt_thread_info *info,
3004 struct mdt_lock_handle *lhc,
3007 struct req_capsule *pill = info->mti_pill;
3008 struct mdt_body *repbody;
3013 rc = mdt_reint_unpack(info, op);
3015 CERROR("Can't unpack reint, rc %d\n", rc);
3016 RETURN(err_serious(rc));
3020 /* check if the file system is set to readonly. O_RDONLY open
3021 * is still allowed even the file system is set to readonly mode
3023 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
3024 RETURN(err_serious(-EROFS));
3026 /* for replay (no_create) lmm is not needed, client has it already */
3027 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3028 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3031 /* llog cookies are always 0, the field is kept for compatibility */
3032 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3033 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
3035 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3036 * by default. If the target object has more ACL entries, then
3037 * enlarge the buffer when necessary.
3039 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3040 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3041 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3043 mdt_preset_secctx_size(info);
3044 mdt_preset_encctx_size(info);
3046 rc = req_capsule_server_pack(pill);
3048 CERROR("Can't pack response, rc %d\n", rc);
3049 RETURN(err_serious(rc));
3052 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
3053 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
3055 repbody->mbo_eadatasize = 0;
3056 repbody->mbo_aclsize = 0;
3059 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
3061 /* for replay no cookkie / lmm need, because client have this already */
3062 if (info->mti_spec.no_create)
3063 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3064 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
3066 rc = mdt_init_ucred_reint(info);
3068 GOTO(out_shrink, rc);
3070 rc = mdt_fix_attr_ucred(info, op);
3072 GOTO(out_ucred, rc = err_serious(rc));
3074 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
3076 GOTO(out_ucred, rc);
3077 } else if (rc == 1) {
3078 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
3079 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
3080 GOTO(out_ucred, rc);
3082 rc = mdt_reint_rec(info, lhc);
3085 mdt_exit_ucred(info);
3087 mdt_client_compatibility(info);
3089 rc2 = mdt_fix_reply(info);
3094 * Data-on-MDT optimization - read data along with OPEN and return it
3095 * in reply when possible.
3097 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
3098 rc = mdt_dom_read_on_open(info, info->mti_mdt,
3104 static long mdt_reint_opcode(struct ptlrpc_request *req,
3105 const struct req_format **fmt)
3107 struct mdt_device *mdt;
3108 struct mdt_rec_reint *rec;
3111 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
3113 opc = rec->rr_opcode;
3114 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
3115 if (opc < REINT_MAX && fmt[opc] != NULL)
3116 req_capsule_extend(&req->rq_pill, fmt[opc]);
3118 mdt = mdt_exp2dev(req->rq_export);
3119 CERROR("%s: Unsupported opcode '%ld' from client '%s': rc = %d\n",
3120 req->rq_export->exp_obd->obd_name,
3121 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
3122 opc = err_serious(-EFAULT);
3125 opc = err_serious(-EFAULT);
3130 static int mdt_reint(struct tgt_session_info *tsi)
3134 static const struct req_format *reint_fmts[REINT_MAX] = {
3135 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
3136 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
3137 [REINT_LINK] = &RQF_MDS_REINT_LINK,
3138 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
3139 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
3140 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
3141 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
3142 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
3143 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
3144 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
3149 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
3151 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3153 * No lock possible here from client to pass it to reint code
3156 rc = mdt_reint_internal(info, NULL, opc);
3157 mdt_thread_info_fini(info);
3162 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
3166 /* this should sync the whole device */
3167 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
3169 struct dt_device *dt = mdt->mdt_bottom;
3174 rc = dt->dd_ops->dt_sync(env, dt);
3178 /* this should sync this object */
3179 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
3180 struct mdt_object *mo)
3186 if (!mdt_object_exists(mo)) {
3187 CWARN("%s: non existing object "DFID": rc = %d\n",
3188 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3193 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3194 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3195 dt_obj_version_t version;
3197 version = dt_version_get(env, mdt_obj2dt(mo));
3198 if (version > tgt->lut_obd->obd_last_committed)
3199 rc = mo_object_sync(env, mdt_object_child(mo));
3201 rc = mo_object_sync(env, mdt_object_child(mo));
3207 static int mdt_sync(struct tgt_session_info *tsi)
3209 struct ptlrpc_request *req = tgt_ses_req(tsi);
3210 struct req_capsule *pill = tsi->tsi_pill;
3211 struct mdt_body *body;
3212 ktime_t kstart = ktime_get();
3217 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3218 RETURN(err_serious(-ENOMEM));
3220 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3221 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3223 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3225 if (unlikely(info->mti_object == NULL))
3228 /* sync an object */
3229 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3232 const struct lu_fid *fid;
3233 struct lu_attr *la = &info->mti_attr.ma_attr;
3235 info->mti_attr.ma_need = MA_INODE;
3236 info->mti_attr.ma_valid = 0;
3237 rc = mdt_attr_get_complex(info, info->mti_object,
3240 body = req_capsule_server_get(pill,
3242 fid = mdt_object_fid(info->mti_object);
3243 mdt_pack_attr2body(info, body, la, fid);
3246 mdt_thread_info_fini(info);
3249 mdt_counter_incr(req, LPROC_MDT_SYNC,
3250 ktime_us_delta(ktime_get(), kstart));
3255 static int mdt_data_sync(struct tgt_session_info *tsi)
3257 struct mdt_thread_info *info;
3258 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3259 struct ost_body *body = tsi->tsi_ost_body;
3260 struct ost_body *repbody;
3261 struct mdt_object *mo = NULL;
3267 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3269 /* device sync is done via MDS_SYNC. NOOP if no fid is specified */
3270 if (fid_is_zero(&tsi->tsi_fid))
3273 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3275 RETURN(PTR_ERR(mo));
3277 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3281 repbody->oa.o_oi = body->oa.o_oi;
3282 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3284 info = tsi2mdt_info(tsi);
3285 ma = &info->mti_attr;
3286 ma->ma_need = MA_INODE;
3288 rc = mdt_attr_get_complex(info, mo, ma);
3290 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3293 mdt_thread_info_fini(info);
3298 mdt_object_put(tsi->tsi_env, mo);
3303 * Handle quota control requests to consult current usage/limit, but also
3304 * to configure quota enforcement
3306 static int mdt_quotactl(struct tgt_session_info *tsi)
3308 struct obd_export *exp = tsi->tsi_exp;
3309 struct req_capsule *pill = tsi->tsi_pill;
3310 struct obd_quotactl *oqctl, *repoqc;
3312 struct mdt_device *mdt = mdt_exp2dev(exp);
3313 struct lu_device *qmt = mdt->mdt_qmt_dev;
3314 struct lu_nodemap *nodemap;
3318 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3320 RETURN(err_serious(-EPROTO));
3322 rc = req_capsule_server_pack(pill);
3324 RETURN(err_serious(rc));
3326 nodemap = nodemap_get_from_exp(exp);
3327 if (IS_ERR(nodemap))
3328 RETURN(PTR_ERR(nodemap));
3330 switch (oqctl->qc_cmd) {
3331 /* master quotactl */
3334 case LUSTRE_Q_SETDEFAULT:
3335 case LUSTRE_Q_SETQUOTAPOOL:
3336 case LUSTRE_Q_SETINFOPOOL:
3337 case LUSTRE_Q_SETDEFAULT_POOL:
3338 case LUSTRE_Q_DELETEQID:
3339 case LUSTRE_Q_RESETQID:
3340 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3342 GOTO(out_nodemap, rc = -EPERM);
3346 case LUSTRE_Q_GETDEFAULT:
3347 case LUSTRE_Q_GETQUOTAPOOL:
3348 case LUSTRE_Q_GETINFOPOOL:
3349 case LUSTRE_Q_GETDEFAULT_POOL:
3351 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3352 /* slave quotactl */
3359 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3360 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3361 GOTO(out_nodemap, rc);
3365 switch (oqctl->qc_type) {
3367 id = nodemap_map_id(nodemap, NODEMAP_UID,
3368 NODEMAP_CLIENT_TO_FS, id);
3371 id = nodemap_map_id(nodemap, NODEMAP_GID,
3372 NODEMAP_CLIENT_TO_FS, id);
3375 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3376 NODEMAP_CLIENT_TO_FS, id);
3379 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3381 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3383 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3385 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3386 barrier_exit(tsi->tsi_tgt->lut_bottom);
3388 if (oqctl->qc_id != id)
3389 swap(oqctl->qc_id, id);
3391 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3392 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3393 RETURN(-EINPROGRESS);
3396 switch (oqctl->qc_cmd) {
3402 case LUSTRE_Q_SETDEFAULT:
3403 case LUSTRE_Q_GETDEFAULT:
3404 case LUSTRE_Q_SETQUOTAPOOL:
3405 case LUSTRE_Q_GETQUOTAPOOL:
3406 case LUSTRE_Q_SETINFOPOOL:
3407 case LUSTRE_Q_GETINFOPOOL:
3408 case LUSTRE_Q_SETDEFAULT_POOL:
3409 case LUSTRE_Q_GETDEFAULT_POOL:
3410 case LUSTRE_Q_DELETEQID:
3411 case LUSTRE_Q_RESETQID:
3412 /* forward quotactl request to QMT */
3413 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3418 /* slave quotactl */
3419 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3424 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3425 GOTO(out_nodemap, rc = -EFAULT);
3428 if (oqctl->qc_id != id)
3429 swap(oqctl->qc_id, id);
3431 QCTL_COPY_NO_PNAME(repoqc, oqctl);
3435 nodemap_putref(nodemap);
3440 /** clone llog ctxt from child (mdd)
3441 * This allows remote llog (replicator) access.
3442 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3443 * context was originally set up, or we can handle them directly.
3444 * I choose the latter, but that means I need any llog
3445 * contexts set up by child to be accessable by the mdt. So we clone the
3446 * context into our context list here.
3448 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3451 struct md_device *next = mdt->mdt_child;
3452 struct llog_ctxt *ctxt;
3455 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3458 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3459 if (rc || ctxt == NULL)
3462 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3464 CERROR("Can't set mdt ctxt %d\n", rc);
3469 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3470 struct mdt_device *mdt, int idx)
3472 struct llog_ctxt *ctxt;
3474 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3477 /* Put once for the get we just did, and once for the clone */
3478 llog_ctxt_put(ctxt);
3479 llog_ctxt_put(ctxt);
3484 * sec context handlers
3486 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3488 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3494 * quota request handlers
3496 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3498 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3499 struct lu_device *qmt = mdt->mdt_qmt_dev;
3505 RETURN(err_serious(-EOPNOTSUPP));
3507 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3511 struct mdt_object *mdt_object_new(const struct lu_env *env,
3512 struct mdt_device *d,
3513 const struct lu_fid *f)
3515 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3516 struct lu_object *o;
3517 struct mdt_object *m;
3521 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3522 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3523 if (unlikely(IS_ERR(o)))
3524 m = (struct mdt_object *)o;
3530 struct mdt_object *mdt_object_find(const struct lu_env *env,
3531 struct mdt_device *d,
3532 const struct lu_fid *f)
3534 struct lu_object *o;
3535 struct mdt_object *m;
3539 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3540 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3541 if (unlikely(IS_ERR(o)))
3542 m = (struct mdt_object *)o;
3550 * Asyncronous commit for mdt device.
3552 * Pass asynchonous commit call down the MDS stack.
3554 * \param env environment
3555 * \param mdt the mdt device
3557 static void mdt_device_commit_async(const struct lu_env *env,
3558 struct mdt_device *mdt)
3560 struct dt_device *dt = mdt->mdt_bottom;
3565 rc = dt->dd_ops->dt_commit_async(env, dt);
3566 if (unlikely(rc != 0))
3567 CWARN("%s: async commit start failed: rc = %d\n",
3568 mdt_obd_name(mdt), rc);
3569 atomic_inc(&mdt->mdt_async_commit_count);
3574 * Mark the lock as "synchonous".
3576 * Mark the lock to deffer transaction commit to the unlock time.
3578 * \param lock the lock to mark as "synchonous"
3580 * \see mdt_is_lock_sync
3581 * \see mdt_save_lock
3583 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3585 lock->l_ast_data = (void *)1;
3589 * Check whehter the lock "synchonous" or not.
3591 * \param lock the lock to check
3592 * \retval 1 the lock is "synchonous"
3593 * \retval 0 the lock isn't "synchronous"
3595 * \see mdt_set_lock_sync
3596 * \see mdt_save_lock
3598 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3600 return lock->l_ast_data != NULL;
3604 * Blocking AST for mdt locks.
3606 * Starts transaction commit if in case of COS lock conflict or
3607 * deffers such a commit to the mdt_save_lock.
3609 * \param lock the lock which blocks a request or cancelling lock
3610 * \param desc unused
3611 * \param data unused
3612 * \param flag indicates whether this cancelling or blocking callback
3614 * \see ldlm_blocking_ast_nocheck
3616 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3617 void *data, int flag)
3619 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3620 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3621 struct ldlm_cb_set_arg *arg = data;
3622 bool commit_async = false;
3626 if (flag == LDLM_CB_CANCELING)
3629 lock_res_and_lock(lock);
3630 if (lock->l_blocking_ast != mdt_blocking_ast) {
3631 unlock_res_and_lock(lock);
3635 /* A blocking ast may be sent from ldlm_lock_decref_internal
3636 * when the last reference to a local lock was released and
3637 * during blocking event from ldlm_work_bl_ast_lock().
3638 * The 'data' parameter is l_ast_data in the first case and
3639 * callback arguments in the second one. Distinguish them by that.
3641 if (data && data != lock->l_ast_data && arg->bl_desc) {
3642 if (lock->l_req_mode & (LCK_COS | LCK_TXN))
3643 commit_async = true;
3644 else if ((lock->l_req_mode & (LCK_PW | LCK_EX)) &&
3645 ((mdt_cos_is_enabled(mdt) &&
3646 !arg->bl_desc->bl_same_client) ||
3647 (mdt_slc_is_enabled(mdt) &&
3648 arg->bl_desc->bl_txn_dependent)))
3649 mdt_set_lock_sync(lock);
3652 rc = ldlm_blocking_ast_nocheck(lock);
3657 rc = lu_env_init(&env, LCT_LOCAL);
3658 if (unlikely(rc != 0))
3659 CWARN("%s: lu_env initialization failed, cannot start asynchronous commit: rc = %d\n",
3662 mdt_device_commit_async(&env, mdt);
3669 * Blocking AST for cross-MDT lock
3671 * Discard lock from uncommitted_slc_locks and cancel it.
3673 * \param lock the lock which blocks a request or cancelling lock
3674 * \param desc unused
3675 * \param data unused
3676 * \param flag indicates whether this cancelling or blocking callback
3677 * \retval 0 on success
3678 * \retval negative number on error
3680 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3681 void *data, int flag)
3688 case LDLM_CB_BLOCKING: {
3689 struct lustre_handle lockh;
3691 ldlm_lock2handle(lock, &lockh);
3692 rc = ldlm_cli_cancel(&lockh,
3693 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3695 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3700 case LDLM_CB_CANCELING: {
3701 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3702 struct mdt_device *mdt =
3703 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3705 LDLM_DEBUG(lock, "Revoke remote lock");
3707 /* discard slc lock here so that it can be cleaned anytime,
3708 * especially for cleanup_resource()
3710 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3712 /* once we cache lock, l_ast_data is set to mdt_object */
3713 if (lock->l_ast_data != NULL) {
3714 struct mdt_object *mo = lock->l_ast_data;
3717 rc = lu_env_init(&env, LCT_MD_THREAD);
3718 if (unlikely(rc != 0)) {
3719 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3721 PFID(mdt_object_fid(mo)), rc);
3725 if (lock->l_policy_data.l_inodebits.bits &
3726 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3727 rc = mo_invalidate(&env, mdt_object_child(mo));
3728 mo->mot_cache_attr = 0;
3730 mdt_object_put(&env, mo);
3742 int mdt_check_resent_lock(struct mdt_thread_info *info,
3743 struct mdt_object *mo,
3744 struct mdt_lock_handle *lhc)
3746 /* the lock might already be gotten in ldlm_handle_enqueue() */
3747 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3748 struct ptlrpc_request *req = mdt_info_req(info);
3749 struct ldlm_lock *lock;
3751 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3752 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3754 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3755 * a resend case, however, it could be already destroyed
3756 * due to client eviction or a raced cancel RPC.
3758 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3759 lhc->mlh_reg_lh.cookie);
3763 if (!fid_res_name_eq(mdt_object_fid(mo),
3764 &lock->l_resource->lr_name)) {
3765 CWARN("%s: Although resent, but still not get child lock:"
3767 info->mti_exp->exp_obd->obd_name,
3768 PFID(mdt_object_fid(mo)));
3769 LDLM_LOCK_PUT(lock);
3772 LDLM_LOCK_PUT(lock);
3778 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3780 mdt_object_get(NULL, lock->l_ast_data);
3783 static int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3784 struct mdt_object *obj,
3785 struct lustre_handle *lh,
3786 enum ldlm_mode mode,
3787 union ldlm_policy_data *policy,
3788 struct ldlm_res_id *res_id,
3791 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3794 LASSERT(mdt_object_remote(obj));
3796 memset(einfo, 0, sizeof(*einfo));
3797 einfo->ei_type = LDLM_IBITS;
3798 einfo->ei_mode = mode;
3799 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3800 einfo->ei_cb_cp = ldlm_completion_ast;
3801 einfo->ei_enq_slave = 0;
3802 einfo->ei_res_id = res_id;
3803 einfo->ei_req_slot = 1;
3806 * if we cache lock, couple lock with mdt_object, so that object
3807 * can be easily found in lock ASTs.
3809 einfo->ei_cbdata = obj;
3810 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3813 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), lh, einfo,
3820 /* other components like LFSCK can use lockless access
3821 * and populate cache, so we better invalidate it
3823 if (policy->l_inodebits.bits &
3824 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR))
3825 mo_invalidate(mti->mti_env, mdt_object_child(obj));
3831 * Helper function to take PDO and hash lock.
3833 * if \a pdo_lock is false, don't take PDO lock, this is case in rename.
3835 int mdt_object_pdo_lock(struct mdt_thread_info *info, struct mdt_object *obj,
3836 struct mdt_lock_handle *lh, const struct lu_name *name,
3837 enum ldlm_mode mode, bool pdo_lock)
3839 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3840 union ldlm_policy_data *policy = &info->mti_policy;
3841 struct ldlm_res_id *res_id = &info->mti_res_id;
3843 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it is never going to
3844 * be sent to client and we do not want it slowed down due to possible
3847 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
3848 __u64 *cookie = NULL;
3853 /* check for exists after object is locked */
3854 if (!mdt_object_exists(obj))
3855 /* Non-existent object shouldn't have PDO lock */
3858 /* Non-dir object shouldn't have PDO lock */
3859 if (!S_ISDIR(lu_object_attr(&obj->mot_obj)))
3862 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
3863 policy->l_inodebits.try_bits = 0;
3864 policy->l_inodebits.li_gid = 0;
3865 policy->l_inodebits.li_initiator_id = mdt_node_id(info->mti_mdt);
3866 fid_build_reg_res_name(mdt_object_fid(obj), res_id);
3868 cookie = &info->mti_exp->exp_handle.h_cookie;
3870 mdt_lock_pdo_init(lh, mode, name);
3871 mdt_lock_pdo_mode(info, obj, lh);
3872 if (lh->mlh_pdo_mode != LCK_NL) {
3874 if (mdt_object_remote(obj)) {
3875 rc = mdt_remote_object_lock_try(info, obj,
3876 &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
3877 policy, res_id, false);
3878 lh->mlh_pdo_remote = 1;
3880 rc = mdt_fid_lock(info->mti_env, ns,
3881 &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
3882 policy, res_id, dlmflags, cookie);
3885 mdt_object_unlock(info, obj, lh, 1);
3889 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3892 if (mdt_object_remote(obj))
3893 rc = mdt_remote_object_lock_try(info, obj, &lh->mlh_rreg_lh,
3894 lh->mlh_rreg_mode, policy, res_id, false);
3896 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh,
3897 lh->mlh_reg_mode, policy, res_id, dlmflags, cookie);
3899 mdt_object_unlock(info, obj, lh, 1);
3900 else if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK) &&
3901 lh->mlh_pdo_hash != 0 &&
3902 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3903 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3908 int mdt_object_lock_internal(struct mdt_thread_info *info,
3909 struct mdt_object *obj, const struct lu_fid *fid,
3910 struct mdt_lock_handle *lh, __u64 *ibits,
3911 __u64 trybits, bool cache)
3913 union ldlm_policy_data *policy = &info->mti_policy;
3914 struct ldlm_res_id *res_id = &info->mti_res_id;
3915 struct lustre_handle *handle;
3918 policy->l_inodebits.bits = *ibits;
3919 policy->l_inodebits.try_bits = trybits;
3920 policy->l_inodebits.li_gid = lh->mlh_gid;
3921 policy->l_inodebits.li_initiator_id = mdt_node_id(info->mti_mdt);
3922 fid_build_reg_res_name(fid, res_id);
3924 if (obj && mdt_object_remote(obj)) {
3925 handle = &lh->mlh_rreg_lh;
3926 LASSERT(!lustre_handle_is_used(handle));
3927 LASSERT(lh->mlh_rreg_mode != LCK_MINMODE);
3928 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3929 rc = mdt_remote_object_lock_try(info, obj, handle,
3930 lh->mlh_rreg_mode, policy,
3933 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3935 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if
3936 * it is going to be sent to client. If it is -
3937 * mdt_intent_policy() path will fix it up and turn FL_LOCAL
3940 __u64 dlmflags = LDLM_FL_ATOMIC_CB | LDLM_FL_LOCAL_ONLY;
3941 __u64 *cookie = NULL;
3943 handle = &lh->mlh_reg_lh;
3944 LASSERT(!lustre_handle_is_used(handle));
3945 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3946 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3948 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3949 if (lh->mlh_type == MDT_REG_LOCK &&
3950 lh->mlh_reg_mode == LCK_EX && *ibits == MDS_INODELOCK_OPEN)
3951 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3955 cookie = &info->mti_exp->exp_handle.h_cookie;
3957 rc = mdt_fid_lock(info->mti_env, ns, handle, lh->mlh_reg_mode,
3958 policy, res_id, dlmflags, cookie);
3960 mdt_object_unlock(info, obj, lh, 1);
3964 struct ldlm_lock *lock;
3966 /* Return successfully acquired bits to a caller */
3967 lock = ldlm_handle2lock(handle);
3969 *ibits = lock->l_policy_data.l_inodebits.bits;
3970 LDLM_LOCK_PUT(lock);
3977 * MDT object locking functions:
3978 * mdt_object_lock(): lock object, this is used in most places, and normally
3979 * lock ibits doesn't contain LOOKUP, unless the caller knows it's not
3981 * mdt_object_check_lock(): lock object with LOOKUP and other ibits, it needs
3982 * to check whether parent is on remote MDT, if so, take LOOKUP on parent
3983 * MDT separately, and then lock other ibits on child object.
3984 * mdt_parent_lock(): take parent UPDATE lock with specific mode, if parent is
3985 * local, take PDO lock by name hash, otherwise take regular lock.
3986 * mdt_object_stripes_lock(): lock object which should be local, and if it's a
3987 * striped directory, lock its stripes, this is called in operations which
3988 * modify both object and stripes.
3989 * mdt_object_lock_try(): lock object with trybits, the trybits contains
3990 * optional inode lock bits that can be granted. This is called by
3991 * getattr/open to fetch more inode lock bits to client, and is also called
3992 * by dir migration to lock link parent in non-block mode to avoid
3999 * this is used to lock object in most places, and normally lock ibits doesn't
4000 * contain LOOKUP, unless the caller knows it's not remote object.
4002 * \param info struct mdt_thread_info
4004 * \param lh lock handle
4005 * \param ibits MDS inode lock bits
4006 * \param mode lock mode
4008 * \retval 0 on success, -ev on error.
4010 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *obj,
4011 struct mdt_lock_handle *lh, __u64 ibits,
4012 enum ldlm_mode mode)
4017 mdt_lock_reg_init(lh, mode);
4018 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh,
4024 * lock object with LOOKUP and other ibits
4026 * it will check whether parent and child are on different MDTs, if so, take
4027 * LOOKUP lock on parent MDT, and lock other ibits on child MDT, otherwise lock
4028 * all ibits on child MDT. Note, parent and child shouldn't be both on remote
4029 * MDTs, in which case specific lock function should be used, and it's in
4030 * rename and migrate only.
4032 * \param info struct mdt_thread_info
4033 * \param parent parent object
4034 * \param child child object
4035 * \param lh lock handle
4036 * \param ibits MDS inode lock bits
4037 * \param mode lock mode
4039 * \retval 0 on success, -ev on error.
4041 int mdt_object_check_lock(struct mdt_thread_info *info,
4042 struct mdt_object *parent, struct mdt_object *child,
4043 struct mdt_lock_handle *lh, __u64 ibits,
4044 enum ldlm_mode mode)
4049 /* if LOOKUP ibit is not set, use mdt_object_lock() */
4050 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
4051 /* if only LOOKUP ibit is needed, use mdt_object_lookup_lock() */
4052 LASSERT(ibits != MDS_INODELOCK_LOOKUP);
4054 /* @parent and @child shouldn't both be on remote MDTs */
4055 LASSERT(!(mdt_object_remote(parent) && mdt_object_remote(child)));
4057 mdt_lock_reg_init(lh, mode);
4058 if (mdt_object_remote(parent) ^ mdt_object_remote(child)) {
4059 __u64 lookup_ibits = MDS_INODELOCK_LOOKUP;
4061 rc = mdt_object_lock_internal(info, parent,
4062 mdt_object_fid(child), lh,
4063 &lookup_ibits, 0, false);
4067 ibits &= ~MDS_INODELOCK_LOOKUP;
4070 rc = mdt_object_lock_internal(info, child, mdt_object_fid(child), lh,
4072 if (rc && !(ibits & MDS_INODELOCK_LOOKUP))
4073 mdt_object_unlock(info, NULL, lh, 1);
4079 * take parent UPDATE lock
4081 * if parent is local or mode is LCK_PW, take PDO lock, otherwise take regular
4084 * \param info struct mdt_thread_info
4085 * \param obj parent object
4086 * \param lh lock handle
4087 * \param lname child name
4088 * \param mode lock mode
4090 * \retval 0 on success, -ev on error.
4092 int mdt_parent_lock(struct mdt_thread_info *info, struct mdt_object *obj,
4093 struct mdt_lock_handle *lh, const struct lu_name *lname,
4094 enum ldlm_mode mode)
4099 LASSERT(obj && lname);
4100 LASSERT(mode == LCK_PW || mode == LCK_PR);
4101 if (mdt_object_remote(obj) && mode == LCK_PR) {
4102 __u64 ibits = MDS_INODELOCK_UPDATE;
4104 mdt_lock_reg_init(lh, mode);
4105 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj),
4106 lh, &ibits, 0, false);
4108 rc = mdt_object_pdo_lock(info, obj, lh, lname, mode, true);
4114 * lock object with trybits
4116 * the trybits contains optional inode lock bits that can be granted. This is
4117 * called by getattr/open to fetch more inode lock bits to client, and is also
4118 * called by dir migration to lock link parent in non-block mode to avoid
4121 * \param info struct mdt_thread_info
4123 * \param lh lock handle
4124 * \param ibits MDS inode lock bits
4125 * \param trybits optional inode lock bits
4126 * \param mode lock mode
4128 * \retval 0 on success, -ev on error.
4130 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *obj,
4131 struct mdt_lock_handle *lh, __u64 *ibits,
4132 __u64 trybits, enum ldlm_mode mode)
4134 bool trylock_only = *ibits == 0;
4138 LASSERT(!(*ibits & trybits));
4139 mdt_lock_reg_init(lh, mode);
4140 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh, ibits,
4142 if (rc && trylock_only) { /* clear error for try ibits lock only */
4143 LASSERT(*ibits == 0);
4150 * Helper function to take \a obj LOOKUP lock.
4152 * Both \a pobj and \a obj may be located on remote MDTs.
4154 int mdt_object_lookup_lock(struct mdt_thread_info *info,
4155 struct mdt_object *pobj, struct mdt_object *obj,
4156 struct mdt_lock_handle *lh, enum ldlm_mode mode)
4158 __u64 ibits = MDS_INODELOCK_LOOKUP;
4162 /* if @parent is NULL, it's on local MDT, and @child is remote,
4163 * this is case in getattr/unlink/open by name.
4165 LASSERT(ergo(!pobj, mdt_object_remote(obj)));
4166 mdt_lock_reg_init(lh, mode);
4167 rc = mdt_object_lock_internal(info, pobj, mdt_object_fid(obj), lh,
4173 * Save a lock within request object.
4175 * Keep the lock referenced until whether client ACK or transaction
4176 * commit happens or release the lock immediately depending on input
4177 * parameters. If COS is ON, a write lock is converted to COS lock
4180 * \param info thead info object
4181 * \param h lock handle
4182 * \param mode lock mode
4183 * \param decref force immediate lock releasing
4185 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
4186 enum ldlm_mode mode, int decref)
4188 struct tgt_session_info *tsi = info->mti_env->le_ses ?
4189 tgt_ses_info(info->mti_env) : NULL;
4193 if (lustre_handle_is_used(h)) {
4194 bool has_trans = tsi && tsi->tsi_has_trans;
4196 if (decref || !has_trans || !(mode & (LCK_PW | LCK_EX))) {
4197 mdt_fid_unlock(h, mode);
4199 struct mdt_device *mdt = info->mti_mdt;
4200 struct ldlm_lock *lock = ldlm_handle2lock(h);
4201 struct ptlrpc_request *req = mdt_info_req(info);
4202 bool no_ack = false;
4204 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
4207 /* there is no request if mdt_object_unlock() is called
4208 * from mdt_export_cleanup()->mdt_add_dirty_flag()
4210 if (likely(req != NULL)) {
4211 LDLM_DEBUG(lock, "save lock request %p reply state %p transno %lld",
4213 req->rq_reply_state, req->rq_transno);
4214 if (mdt_cos_is_enabled(mdt)) {
4217 ldlm_lock_mode_downgrade(lock, mode);
4218 } else if (mdt_slc_is_enabled(mdt)) {
4220 if (mode != LCK_TXN) {
4222 ldlm_lock_mode_downgrade(lock,
4226 if (req->rq_export->exp_disconnected)
4227 mdt_fid_unlock(h, mode);
4229 ptlrpc_save_lock(req, h, mode, no_ack);
4231 mdt_fid_unlock(h, mode);
4234 if (mdt_is_lock_sync(lock)) {
4235 CDEBUG(D_HA, "sync_lock, do async commit\n");
4236 mdt_device_commit_async(info->mti_env, mdt);
4238 LDLM_LOCK_PUT(lock);
4247 * Save cross-MDT lock in uncommitted_slc_locks
4249 * Keep the lock referenced until transaction commit happens or release the lock
4250 * immediately depending on input parameters.
4252 * \param info thead info object
4253 * \param h lock handle
4254 * \param mode lock mode
4255 * \param decref force immediate lock releasing
4257 static void mdt_save_remote_lock(struct mdt_thread_info *info,
4258 struct mdt_object *o, struct lustre_handle *h,
4259 enum ldlm_mode mode, int decref)
4263 if (lustre_handle_is_used(h)) {
4264 struct ldlm_lock *lock = ldlm_handle2lock(h);
4265 struct ptlrpc_request *req = mdt_info_req(info);
4268 (lock->l_policy_data.l_inodebits.bits &
4269 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
4270 mo_invalidate(info->mti_env, mdt_object_child(o));
4272 if (decref || !req || !(mode & (LCK_PW | LCK_EX)) ||
4273 !tgt_ses_info(info->mti_env)->tsi_has_trans) {
4274 ldlm_lock_decref_and_cancel(h, mode);
4275 LDLM_LOCK_PUT(lock);
4277 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
4279 ldlm_lock_decref(h, mode);
4288 * Unlock mdt object.
4290 * Immeditely release the regular lock and the PDO lock or save the
4291 * lock in request and keep them referenced until client ACK or
4292 * transaction commit.
4294 * \param info thread info object
4295 * \param o mdt object
4296 * \param lh mdt lock handle referencing regular and PDO locks
4297 * \param decref force immediate lock releasing
4299 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
4301 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
4302 struct mdt_lock_handle *lh, int decref)
4306 if (lh->mlh_pdo_remote)
4307 mdt_save_remote_lock(info, o, &lh->mlh_pdo_lh,
4308 lh->mlh_pdo_mode, decref);
4310 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
4311 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
4312 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4318 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4319 const struct lu_fid *f,
4320 struct mdt_lock_handle *lh,
4321 __u64 ibits, enum ldlm_mode mode)
4323 struct mdt_object *o;
4325 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4329 rc = mdt_object_lock(info, o, lh, ibits, mode);
4331 mdt_object_put(info->mti_env, o);
4338 void mdt_object_unlock_put(struct mdt_thread_info *info,
4339 struct mdt_object *o,
4340 struct mdt_lock_handle *lh,
4343 mdt_object_unlock(info, o, lh, decref);
4344 mdt_object_put(info->mti_env, o);
4348 * Generic code handling requests that have struct mdt_body passed in:
4350 * - extract mdt_body from request and save it in @info, if present;
4352 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4355 * - if HAS_BODY flag is set for this request type check whether object
4356 * actually exists on storage (lu_object_exists()).
4359 static int mdt_body_unpack(struct mdt_thread_info *info,
4360 enum tgt_handler_flags flags)
4362 const struct mdt_body *body;
4363 struct mdt_object *obj;
4364 const struct lu_env *env;
4365 struct req_capsule *pill;
4370 env = info->mti_env;
4371 pill = info->mti_pill;
4373 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4377 if (!(body->mbo_valid & OBD_MD_FLID))
4380 if (!fid_is_sane(&body->mbo_fid1)) {
4381 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4385 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4387 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4388 mdt_object_put(env, obj);
4391 info->mti_object = obj;
4400 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4401 enum tgt_handler_flags flags)
4403 struct req_capsule *pill = info->mti_pill;
4408 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4409 rc = mdt_body_unpack(info, flags);
4413 if (rc == 0 && (flags & HAS_REPLY)) {
4415 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4416 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4417 req_capsule_ptlreq(pill) ?
4418 DEF_REP_MD_SIZE : MAX_MD_SIZE_OLD);
4420 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4421 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4424 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4425 * by default. If the target object has more ACL entries, then
4426 * enlarge the buffer when necessary.
4428 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4429 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4430 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4432 mdt_preset_secctx_size(info);
4433 mdt_preset_encctx_size(info);
4435 rc = req_capsule_server_pack(pill);
4437 CWARN("%s: cannot pack response: rc = %d\n",
4438 mdt_obd_name(info->mti_mdt), rc);
4443 void mdt_thread_info_reset(struct mdt_thread_info *info)
4445 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4446 info->mti_body = NULL;
4447 info->mti_dlm_req = NULL;
4448 info->mti_cross_ref = 0;
4449 info->mti_opdata = 0;
4450 info->mti_big_lmm_used = 0;
4451 info->mti_big_acl_used = 0;
4452 info->mti_som_strict = 0;
4454 info->mti_spec.no_create = 0;
4455 info->mti_spec.sp_rm_entry = 0;
4456 info->mti_spec.sp_permitted = 0;
4458 info->mti_spec.u.sp_ea.eadata = NULL;
4459 info->mti_spec.u.sp_ea.eadatalen = 0;
4461 if (info->mti_batch_env && info->mti_object != NULL) {
4462 mdt_object_put(info->mti_env, info->mti_object);
4463 info->mti_object = NULL;
4468 * Initialize fields of struct mdt_thread_info. Other fields are left in
4469 * uninitialized state, because it's too expensive to zero out whole
4470 * mdt_thread_info (> 1K) on each request arrival.
4472 void mdt_thread_info_init(struct ptlrpc_request *req,
4473 struct mdt_thread_info *info)
4475 info->mti_pill = &req->rq_pill;
4477 /* mdt device: it can be NULL while CONNECT */
4478 if (req->rq_export) {
4479 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4480 info->mti_exp = req->rq_export;
4482 info->mti_mdt = NULL;
4483 info->mti_env = req->rq_svc_thread->t_env;
4484 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4485 info->mti_big_buf = LU_BUF_NULL;
4486 info->mti_batch_env = 0;
4487 info->mti_object = NULL;
4489 mdt_thread_info_reset(info);
4492 void mdt_thread_info_fini(struct mdt_thread_info *info)
4496 if (info->mti_object != NULL) {
4497 mdt_object_put(info->mti_env, info->mti_object);
4498 info->mti_object = NULL;
4501 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4502 mdt_lock_handle_assert(&info->mti_lh[i]);
4503 info->mti_env = NULL;
4504 info->mti_pill = NULL;
4505 info->mti_exp = NULL;
4506 info->mti_mdt = NULL;
4508 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4509 lu_buf_free(&info->mti_big_buf);
4512 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4514 struct mdt_thread_info *mti;
4516 mti = mdt_th_info(tsi->tsi_env);
4517 LASSERT(mti != NULL);
4519 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4520 if (tsi->tsi_corpus != NULL) {
4521 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4522 lu_object_get(tsi->tsi_corpus);
4524 mti->mti_body = tsi->tsi_mdt_body;
4525 mti->mti_dlm_req = tsi->tsi_dlm_req;
4530 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4532 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4534 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4535 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4537 return tgt_connect(tsi);
4540 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4541 struct mdt_thread_info *info,
4542 struct ldlm_lock **lockp, __u64 flags)
4544 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4547 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4548 struct mdt_thread_info *info,
4549 struct ldlm_lock **lockp, __u64 flags)
4551 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4555 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4556 struct ldlm_lock **lockp,
4557 struct mdt_lock_handle *lh,
4558 __u64 flags, int result)
4560 struct ptlrpc_request *req = mdt_info_req(info);
4561 struct ldlm_lock *lock = *lockp;
4562 struct ldlm_lock *new_lock;
4564 /* If possible resent found a lock, @lh is set to its handle */
4565 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4567 if (new_lock == NULL) {
4568 if (flags & LDLM_FL_INTENT_ONLY) {
4570 } else if (flags & LDLM_FL_RESENT) {
4571 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4572 * resend case, however, it could be already destroyed
4573 * due to client eviction or a raced cancel RPC.
4575 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4576 lh->mlh_reg_lh.cookie);
4579 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4580 mdt_obd_name(info->mti_mdt),
4581 lh->mlh_reg_lh.cookie, flags,
4582 PFID(&info->mti_tmp_fid1),
4583 PFID(&info->mti_tmp_fid2), result);
4586 lh->mlh_reg_lh.cookie = 0;
4591 * If we've already given this lock to a client once, then we should
4592 * have no readers or writers. Otherwise, we should have one reader
4593 * _or_ writer ref (which will be zeroed below) before returning the
4596 if (new_lock->l_export == req->rq_export) {
4597 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4599 LASSERT(new_lock->l_export == NULL);
4600 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4605 if (new_lock->l_export == req->rq_export) {
4607 * Already gave this to the client, which means that we
4608 * reconstructed a reply.
4610 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4613 LDLM_LOCK_RELEASE(new_lock);
4614 lh->mlh_reg_lh.cookie = 0;
4615 RETURN(ELDLM_LOCK_REPLACED);
4619 * Fixup the lock to be given to the client.
4621 lock_res_and_lock(new_lock);
4622 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4623 * possible blocking AST.
4625 while (new_lock->l_readers > 0) {
4626 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4627 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4628 new_lock->l_readers--;
4630 while (new_lock->l_writers > 0) {
4631 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4632 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4633 new_lock->l_writers--;
4636 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4637 new_lock->l_blocking_ast = lock->l_blocking_ast;
4638 new_lock->l_completion_ast = lock->l_completion_ast;
4639 if (ldlm_has_dom(new_lock))
4640 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4641 new_lock->l_remote_handle = lock->l_remote_handle;
4642 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4644 unlock_res_and_lock(new_lock);
4646 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4647 &new_lock->l_remote_handle,
4648 &new_lock->l_exp_hash);
4650 LDLM_LOCK_RELEASE(new_lock);
4651 lh->mlh_reg_lh.cookie = 0;
4653 RETURN(ELDLM_LOCK_REPLACED);
4656 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4657 struct ldlm_lock *new_lock,
4658 struct mdt_lock_handle *lh, __u64 flags)
4660 struct ptlrpc_request *req = mdt_info_req(info);
4661 struct ldlm_request *dlmreq;
4663 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4666 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4668 /* if this is a resend case (MSG_RESENT is set on RPC) and a lock was
4669 * found by ldlm_handle_enqueue(); if so @lh must be initialized.
4671 if (flags & LDLM_FL_RESENT) {
4672 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4673 lh->mlh_reg_mode = new_lock->l_granted_mode;
4675 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4676 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4677 lh->mlh_reg_lh.cookie);
4682 * If the xid matches, then we know this is a resent request, and allow
4683 * it. (It's probably an OPEN, for which we don't send a lock.
4685 if (req_can_reconstruct(req, NULL) != 0)
4689 * This remote handle isn't enqueued, so we never received or processed
4690 * this request. Clear MSG_RESENT, because it can be handled like any
4691 * normal request now.
4693 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4695 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4696 dlmreq->lock_handle[0].cookie);
4699 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4700 struct mdt_thread_info *info,
4701 struct ldlm_lock **lockp,
4704 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4705 struct ldlm_reply *ldlm_rep = NULL;
4711 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4712 * (for the resend case) or a new lock. Below we will use it to
4713 * replace the original lock.
4715 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4716 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4717 rc = mdt_object_lock(info, info->mti_object, lhc,
4718 MDS_INODELOCK_XATTR, (*lockp)->l_req_mode);
4723 rc = mdt_getxattr(info);
4725 if (mdt_info_req(info)->rq_repmsg != NULL)
4726 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4728 if (ldlm_rep == NULL ||
4729 CFS_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4730 mdt_object_unlock(info, info->mti_object, lhc, 1);
4734 RETURN(err_serious(-EFAULT));
4737 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4739 /* This is for interop instead of adding a new interop flag. LU-7433 */
4740 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4741 if (ldlm_rep->lock_policy_res2) {
4742 mdt_object_unlock(info, info->mti_object, lhc, 1);
4743 RETURN(ELDLM_LOCK_ABORTED);
4747 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4751 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4752 struct mdt_thread_info *info,
4753 struct ldlm_lock **lockp,
4756 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4758 struct ldlm_reply *ldlm_rep;
4759 struct mdt_body *reqbody;
4760 struct mdt_body *repbody;
4765 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4768 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4771 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4772 repbody->mbo_eadatasize = 0;
4773 repbody->mbo_aclsize = 0;
4777 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4780 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4784 CERROR("%s: unsupported intent %#x\n",
4785 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4786 GOTO(out_shrink, rc = -EINVAL);
4789 rc = mdt_init_ucred(info, reqbody);
4791 GOTO(out_shrink, rc);
4793 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4794 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4796 /* Get lock from request for possible resent case. */
4797 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4799 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4800 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4802 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG) &&
4803 ldlm_rep->lock_policy_res2 != -ENOKEY)
4804 ldlm_rep->lock_policy_res2 = 0;
4805 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4806 ldlm_rep->lock_policy_res2) {
4807 lhc->mlh_reg_lh.cookie = 0ull;
4808 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4811 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4814 mdt_exit_ucred(info);
4816 mdt_client_compatibility(info);
4817 rc2 = mdt_fix_reply(info);
4823 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4824 struct mdt_thread_info *info,
4825 struct ldlm_lock **lockp,
4828 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4829 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4830 struct layout_intent *intent;
4831 struct ldlm_reply *ldlm_rep;
4832 struct lu_fid *fid = &info->mti_tmp_fid2;
4833 struct mdt_object *obj = NULL;
4834 int layout_size = 0;
4835 struct lu_buf *buf = &layout.mlc_buf;
4840 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4842 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4846 CDEBUG(D_INFO, DFID "got layout change request from client: opc:%u flags:%#x extent "
4848 PFID(fid), intent->lai_opc, intent->lai_flags,
4849 PEXT(&intent->lai_extent));
4851 switch (intent->lai_opc) {
4852 case LAYOUT_INTENT_TRUNC:
4853 case LAYOUT_INTENT_WRITE:
4854 case LAYOUT_INTENT_PCCRO_SET:
4855 case LAYOUT_INTENT_PCCRO_CLEAR:
4856 layout.mlc_opc = MD_LAYOUT_WRITE;
4857 layout.mlc_intent = intent;
4859 case LAYOUT_INTENT_ACCESS:
4861 case LAYOUT_INTENT_READ:
4862 case LAYOUT_INTENT_GLIMPSE:
4863 case LAYOUT_INTENT_RELEASE:
4864 case LAYOUT_INTENT_RESTORE:
4865 CERROR("%s: Unsupported layout intent opc %d\n",
4866 mdt_obd_name(info->mti_mdt), intent->lai_opc);
4869 CERROR("%s: Unknown layout intent opc %d\n",
4870 mdt_obd_name(info->mti_mdt), intent->lai_opc);
4874 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4876 RETURN(PTR_ERR(obj));
4878 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4879 /* if layout is going to be changed don't use the current EA
4880 * size but the maximum one. That buffer will be shrinked
4881 * to the actual size in req_capsule_shrink() before reply.
4883 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4884 layout_size = info->mti_mdt->mdt_max_mdsize;
4886 layout_size = mdt_attr_get_eabuf_size(info, obj);
4887 if (layout_size < 0)
4888 GOTO(out, rc = layout_size);
4890 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4891 info->mti_mdt->mdt_max_mdsize = layout_size;
4893 CDEBUG(D_INFO, "%s: layout_size %d\n",
4894 mdt_obd_name(info->mti_mdt), layout_size);
4898 * set reply buffer size, so that ldlm_handle_enqueue0()->
4899 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4901 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4903 rc = req_capsule_server_pack(info->mti_pill);
4907 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4909 GOTO(out, rc = -EPROTO);
4911 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4913 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4914 if (layout.mlc_opc == MD_LAYOUT_NOP)
4917 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4921 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4922 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4928 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4929 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4931 buf->lb_len = req_capsule_get_size(info->mti_pill,
4932 &RMF_EADATA, RCL_CLIENT);
4934 * If it's a replay of layout write intent RPC, the client has
4935 * saved the extended lovea when it get reply then.
4937 if (buf->lb_len > 0)
4938 mdt_fix_lov_magic(info, buf->lb_buf);
4941 /* Get lock from request for possible resent case. */
4942 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4943 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4946 * Instantiate some layout components, if @buf contains lovea, then it's
4947 * a replay of the layout intent write RPC.
4949 rc = mdt_layout_change(info, obj, lhc, &layout);
4950 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4952 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4953 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4954 if (rc == ELDLM_LOCK_REPLACED &&
4955 (*lockp)->l_granted_mode == LCK_EX)
4956 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4961 mdt_object_put(info->mti_env, obj);
4965 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4966 struct mdt_thread_info *info,
4967 struct ldlm_lock **lockp,
4970 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4971 struct ldlm_reply *rep = NULL;
4974 struct ptlrpc_request *req = mdt_info_req(info);
4976 static const struct req_format *intent_fmts[REINT_MAX] = {
4977 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4978 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4983 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4987 /* Get lock from request for possible resent case. */
4988 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4990 rc = mdt_reint_internal(info, lhc, opc);
4992 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4993 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4995 /* Check whether the reply has been packed successfully. */
4996 if (mdt_info_req(info)->rq_repmsg != NULL)
4997 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
5002 RETURN(err_serious(-EFAULT));
5005 /* MDC expects this in any case */
5007 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
5009 /* the open lock or the lock for cross-ref object should be
5010 * returned to the client
5012 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
5013 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
5014 rep->lock_policy_res2 = 0;
5015 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
5019 rep->lock_policy_res2 = clear_serious(rc);
5021 if (rep->lock_policy_res2 == -ENOENT &&
5022 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
5023 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
5024 rep->lock_policy_res2 = 0;
5026 lhc->mlh_reg_lh.cookie = 0ull;
5027 if (rc == -ENOTCONN || rc == -ENODEV ||
5028 rc == -EOVERFLOW) { /**< if VBR failure then return error */
5030 * If it is the disconnect error (ENODEV & ENOCONN), the error
5031 * will be returned by rq_status, and client at ptlrpc layer
5032 * will detect this, then disconnect, reconnect the import
5033 * immediately, instead of impacting the following the rpc.
5038 * For other cases, the error will be returned by intent, and client
5039 * will retrieve the result from intent.
5041 RETURN(ELDLM_LOCK_ABORTED);
5044 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
5045 struct mdt_thread_info *info,
5046 struct ldlm_lock **lockp,
5047 u64 flags /* LDLM_FL_* */)
5049 struct req_capsule *pill = info->mti_pill;
5050 struct ptlrpc_request *req = mdt_info_req(info);
5051 const struct req_format *it_format;
5052 int (*it_handler)(enum ldlm_intent_flags,
5053 struct mdt_thread_info *,
5054 struct ldlm_lock **,
5056 enum tgt_handler_flags it_handler_flags = 0;
5057 struct ldlm_reply *rep;
5058 bool check_mdt_object = false;
5065 case IT_OPEN|IT_CREAT:
5067 * OCREAT is not a IS_MUTABLE request since the file may
5068 * already exist. We do the extra check of
5069 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
5070 * really need to create the object.
5072 it_format = &RQF_LDLM_INTENT;
5073 it_handler = &mdt_intent_open;
5076 check_mdt_object = true;
5079 it_format = &RQF_LDLM_INTENT_GETATTR;
5080 it_handler = &mdt_intent_getattr;
5081 it_handler_flags = HAS_REPLY;
5084 check_mdt_object = true;
5085 it_format = &RQF_LDLM_INTENT_GETXATTR;
5086 it_handler = &mdt_intent_getxattr;
5087 it_handler_flags = HAS_BODY;
5090 it_format = &RQF_LDLM_INTENT_LAYOUT;
5091 it_handler = &mdt_intent_layout;
5094 it_format = &RQF_LDLM_INTENT;
5095 it_handler = &mdt_intent_glimpse;
5098 it_format = &RQF_LDLM_INTENT;
5099 it_handler = &mdt_intent_brw;
5101 case IT_QUOTA_DQACQ:
5102 case IT_QUOTA_CONN: {
5103 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
5106 RETURN(-EOPNOTSUPP);
5108 if (mdt_rdonly(req->rq_export))
5111 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
5112 /* pass the request to quota master */
5113 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
5114 mdt_info_req(info), lockp,
5119 CERROR("%s: unknown intent code %#x\n",
5120 mdt_obd_name(info->mti_mdt), it_opc);
5124 if (!info->mti_batch_env)
5125 req_capsule_extend(pill, it_format);
5127 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
5131 if (unlikely(info->mti_object == NULL && check_mdt_object))
5134 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
5137 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
5139 /* execute policy */
5140 rc = (*it_handler)(it_opc, info, lockp, flags);
5142 /* Check whether the reply has been packed successfully. */
5143 if (info->mti_batch_env || req->rq_repmsg != NULL) {
5144 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
5145 rep->lock_policy_res2 =
5146 ptlrpc_status_hton(rep->lock_policy_res2);
5152 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
5153 enum ldlm_intent_flags it_opc)
5155 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
5157 /* update stats when IT code is known */
5158 if (srv_stats != NULL)
5159 lprocfs_counter_incr(srv_stats,
5160 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
5161 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
5164 static int mdt_intent_policy(const struct lu_env *env,
5165 struct ldlm_namespace *ns,
5166 struct ldlm_lock **lockp,
5168 enum ldlm_mode mode,
5169 __u64 flags, void *data)
5171 struct tgt_session_info *tsi;
5172 struct mdt_thread_info *info;
5173 struct ptlrpc_request *req = req_cookie;
5174 struct ldlm_intent *it;
5175 struct req_capsule *pill;
5176 const struct ldlm_lock_desc *ldesc;
5181 LASSERT(req != NULL);
5183 tsi = tgt_ses_info(env);
5185 info = mdt_th_info(env);
5186 LASSERT(info != NULL);
5188 /* Check whether it is a sub request processing in a batch request */
5189 if (info->mti_batch_env) {
5190 pill = info->mti_pill;
5191 LASSERT(pill == &info->mti_sub_pill);
5193 info = tsi2mdt_info(tsi);
5194 pill = info->mti_pill;
5197 LASSERT(pill->rc_req == req);
5198 ldesc = &info->mti_dlm_req->lock_desc;
5200 if (info->mti_batch_env ||
5201 req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
5203 * For batch processing environment, the request format has
5206 if (!info->mti_batch_env)
5207 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
5209 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
5211 mdt_ptlrpc_stats_update(req, it->opc);
5212 rc = mdt_intent_opc(it->opc, info, lockp, flags);
5216 /* Lock without inodebits makes no sense and will oops
5217 * later in ldlm. Let's check it now to see if we have
5218 * ibits corrupted somewhere in mdt_intent_opc().
5219 * The case for client miss to set ibits has been
5220 * processed by others.
5222 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
5223 ldesc->l_policy_data.l_inodebits.bits != 0));
5225 rc = err_serious(-EFAULT);
5227 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
5228 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
5229 struct ldlm_reply *rep;
5231 /* No intent was provided but INTENT flag is set along with
5232 * DOM bit, this is considered as GLIMPSE request.
5233 * This logic is common for MDT and OST glimpse
5235 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
5236 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
5237 /* Check whether the reply has been packed successfully. */
5238 if (req->rq_repmsg != NULL) {
5239 rep = req_capsule_server_get(info->mti_pill,
5241 rep->lock_policy_res2 =
5242 ptlrpc_status_hton(rep->lock_policy_res2);
5245 /* No intent was provided */
5246 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
5247 rc = req_capsule_server_pack(pill);
5249 rc = err_serious(rc);
5252 if (!info->mti_batch_env)
5253 mdt_thread_info_fini(info);
5257 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
5259 struct seq_server_site *ss = mdt_seq_site(mdt);
5261 if (ss->ss_node_id == 0)
5264 if (ss->ss_client_seq != NULL) {
5265 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5266 ss->ss_client_seq->lcs_exp = NULL;
5269 if (ss->ss_server_fld != NULL) {
5270 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
5271 ss->ss_server_fld->lsf_control_exp = NULL;
5275 static void mdt_seq_fini_cli(struct mdt_device *mdt)
5277 struct seq_server_site *ss = mdt_seq_site(mdt);
5282 if (ss->ss_server_seq != NULL)
5283 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
5286 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
5288 mdt_seq_fini_cli(mdt);
5289 mdt_deregister_seq_exp(mdt);
5291 return seq_site_fini(env, mdt_seq_site(mdt));
5295 * It will retrieve its FLDB entries from MDT0, and it only happens
5296 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
5297 * and it needs to refresh FLDB from the MDT0.
5299 static int mdt_register_lwp_callback(void *data)
5302 struct mdt_device *mdt = data;
5303 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
5308 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
5310 rc = lu_env_init(&env, LCT_MD_THREAD);
5312 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
5316 /* Allocate new sequence now to avoid creating local transaction
5317 * in the normal transaction process
5319 rc = seq_server_check_and_alloc_super(&env,
5320 mdt_seq_site(mdt)->ss_server_seq);
5325 rc = fld_update_from_controller(&env, fld);
5327 CERROR("%s: cannot update controller: rc = %d\n",
5328 mdt_obd_name(mdt), rc);
5337 static int mdt_register_seq_exp(struct mdt_device *mdt)
5339 struct seq_server_site *ss = mdt_seq_site(mdt);
5340 char *lwp_name = NULL;
5343 if (ss->ss_node_id == 0)
5346 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5347 if (lwp_name == NULL)
5348 GOTO(out_free, rc = -ENOMEM);
5350 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5354 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5359 rc = lustre_register_lwp_item(lwp_name,
5360 &ss->ss_server_fld->lsf_control_exp,
5361 mdt_register_lwp_callback, mdt);
5363 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5364 ss->ss_client_seq->lcs_exp = NULL;
5368 if (lwp_name != NULL)
5369 OBD_FREE(lwp_name, MAX_OBD_NAME);
5375 * Init client sequence manager which is used by local MDS to talk to sequence
5376 * controller on remote node.
5378 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5380 struct seq_server_site *ss = mdt_seq_site(mdt);
5385 /* check if this is adding the first MDC and controller is not yet
5388 OBD_ALLOC_PTR(ss->ss_client_seq);
5389 if (ss->ss_client_seq == NULL)
5392 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5393 if (prefix == NULL) {
5394 OBD_FREE_PTR(ss->ss_client_seq);
5395 ss->ss_client_seq = NULL;
5399 /* Note: seq_client_fini will be called in seq_site_fini */
5400 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5401 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5402 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5404 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5406 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5409 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5411 struct seq_server_site *ss;
5416 ss = mdt_seq_site(mdt);
5417 /* init sequence controller server(MDT0) */
5418 if (ss->ss_node_id == 0) {
5419 OBD_ALLOC_PTR(ss->ss_control_seq);
5420 if (ss->ss_control_seq == NULL)
5423 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5424 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5427 GOTO(out_seq_fini, rc);
5430 /* Init normal sequence server */
5431 OBD_ALLOC_PTR(ss->ss_server_seq);
5432 if (ss->ss_server_seq == NULL)
5433 GOTO(out_seq_fini, rc = -ENOMEM);
5435 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5436 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5438 GOTO(out_seq_fini, rc);
5440 /* init seq client for seq server to talk to seq controller(MDT0) */
5441 rc = mdt_seq_init_cli(env, mdt);
5443 GOTO(out_seq_fini, rc);
5445 if (ss->ss_node_id != 0)
5446 /* register controller export through lwp */
5447 rc = mdt_register_seq_exp(mdt);
5452 mdt_seq_fini(env, mdt);
5460 static int mdt_fld_fini(const struct lu_env *env,
5461 struct mdt_device *m)
5463 struct seq_server_site *ss = mdt_seq_site(m);
5467 if (ss && ss->ss_server_fld) {
5468 fld_server_fini(env, ss->ss_server_fld);
5469 OBD_FREE_PTR(ss->ss_server_fld);
5470 ss->ss_server_fld = NULL;
5476 static int mdt_fld_init(const struct lu_env *env,
5478 struct mdt_device *m)
5480 struct seq_server_site *ss;
5485 ss = mdt_seq_site(m);
5487 OBD_ALLOC_PTR(ss->ss_server_fld);
5488 if (ss->ss_server_fld == NULL)
5489 RETURN(rc = -ENOMEM);
5491 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5494 OBD_FREE_PTR(ss->ss_server_fld);
5495 ss->ss_server_fld = NULL;
5502 static void mdt_stack_pre_fini(const struct lu_env *env,
5503 struct mdt_device *m, struct lu_device *top)
5505 struct lustre_cfg_bufs *bufs;
5506 struct lustre_cfg *lcfg;
5507 struct mdt_thread_info *info;
5513 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5514 LASSERT(info != NULL);
5516 bufs = &info->mti_u.bufs;
5518 LASSERT(m->mdt_child_exp);
5519 LASSERT(m->mdt_child_exp->exp_obd);
5521 /* process cleanup, pass mdt obd name to get obd umount flags */
5522 /* XXX: this is needed because all layers are referenced by
5523 * objects (some of them are pinned by osd, for example *
5524 * the proper solution should be a model where object used
5525 * by osd only doesn't have mdt/mdd slices -bzzz
5527 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5528 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5529 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5532 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5534 top->ld_ops->ldo_process_config(env, top, lcfg);
5535 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5539 static void mdt_stack_fini(const struct lu_env *env,
5540 struct mdt_device *m, struct lu_device *top)
5542 struct obd_device *obd = mdt2obd_dev(m);
5543 struct lustre_cfg_bufs *bufs;
5544 struct lustre_cfg *lcfg;
5545 struct mdt_thread_info *info;
5550 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5551 LASSERT(info != NULL);
5553 lu_dev_del_linkage(top->ld_site, top);
5555 lu_site_purge(env, top->ld_site, -1);
5557 bufs = &info->mti_u.bufs;
5558 /* process cleanup, pass mdt obd name to get obd umount flags */
5559 /* another purpose is to let all layers to release their objects */
5560 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5565 lustre_cfg_bufs_set_string(bufs, 1, flags);
5566 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5569 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5572 top->ld_ops->ldo_process_config(env, top, lcfg);
5573 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5575 lu_site_purge(env, top->ld_site, -1);
5577 m->mdt_child = NULL;
5578 m->mdt_bottom = NULL;
5580 obd_disconnect(m->mdt_child_exp);
5581 m->mdt_child_exp = NULL;
5583 obd_disconnect(m->mdt_bottom_exp);
5584 m->mdt_child_exp = NULL;
5587 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5588 const char *next, struct obd_export **exp)
5590 struct obd_connect_data *data = NULL;
5591 struct obd_device *obd;
5596 OBD_ALLOC_PTR(data);
5598 GOTO(out, rc = -ENOMEM);
5600 obd = class_name2obd(next);
5602 CERROR("%s: can't locate next device: %s\n",
5603 mdt_obd_name(m), next);
5604 GOTO(out, rc = -ENOTCONN);
5607 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5608 data->ocd_version = LUSTRE_VERSION_CODE;
5610 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5612 CERROR("%s: cannot connect to next dev %s (%d)\n",
5613 mdt_obd_name(m), next, rc);
5623 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5624 struct lustre_cfg *cfg)
5626 char *dev = lustre_cfg_string(cfg, 0);
5627 int rc, name_size, uuid_size;
5628 char *name, *uuid, *p;
5629 struct lustre_cfg_bufs *bufs;
5630 struct lustre_cfg *lcfg;
5631 struct obd_device *obd;
5632 struct lustre_profile *lprof;
5633 struct lu_site *site;
5637 /* in 1.8 we had the only device in the stack - MDS.
5638 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5639 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5640 * to generate names and setup MDT, MDD. MDT will be using
5641 * generated name to connect to MDD. for MDD the next device
5642 * will be LOD with name taken from so called "profile" which
5643 * is generated by mount_option line
5645 * 1.8 MGS generates config. commands like this:
5646 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5647 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5648 * 2.0 MGS generates config. commands like this:
5649 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5650 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5651 * 3:lustre-MDT0000-mdtlov 4:f
5653 * we generate MDD name from MDT one, just replacing T with D
5655 * after all the preparations, the logical equivalent will be
5656 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5657 * 3:lustre-MDT0000-mdtlov 4:f
5658 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5659 * 3:lustre-MDD0000 4:f
5661 * notice we build the stack from down to top: MDD first, then MDT
5664 name_size = MAX_OBD_NAME;
5665 uuid_size = MAX_OBD_NAME;
5667 OBD_ALLOC(name, name_size);
5668 OBD_ALLOC(uuid, uuid_size);
5669 if (name == NULL || uuid == NULL)
5670 GOTO(cleanup_mem, rc = -ENOMEM);
5672 OBD_ALLOC_PTR(bufs);
5674 GOTO(cleanup_mem, rc = -ENOMEM);
5677 p = strstr(name, "-MDT");
5679 GOTO(free_bufs, rc = -ENOMEM);
5682 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5684 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5685 if (lprof == NULL || lprof->lp_dt == NULL) {
5686 CERROR("can't find the profile: %s\n",
5687 lustre_cfg_string(cfg, 0));
5688 GOTO(free_bufs, rc = -EINVAL);
5691 lustre_cfg_bufs_reset(bufs, name);
5692 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5693 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5694 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5696 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5698 GOTO(put_profile, rc = -ENOMEM);
5699 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5701 rc = class_attach(lcfg);
5703 GOTO(lcfg_cleanup, rc);
5705 obd = class_name2obd(name);
5707 CERROR("Can not find obd %s (%s in config)\n",
5708 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5709 GOTO(lcfg_cleanup, rc = -EINVAL);
5712 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5714 lustre_cfg_bufs_reset(bufs, name);
5715 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5716 lustre_cfg_bufs_set_string(bufs, 2, dev);
5717 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5719 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5721 GOTO(class_detach, rc = -ENOMEM);
5722 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5724 rc = class_setup(obd, lcfg);
5726 GOTO(class_detach, rc);
5728 /* connect to MDD we just setup */
5729 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5731 GOTO(class_detach, rc);
5733 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5735 LASSERT(mdt_lu_site(mdt) == NULL);
5736 mdt->mdt_lu_dev.ld_site = site;
5737 site->ls_top_dev = &mdt->mdt_lu_dev;
5738 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5740 /* now connect to bottom OSD */
5741 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5742 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5744 GOTO(class_detach, rc);
5746 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5748 rc = lu_env_refill((struct lu_env *)env);
5750 CERROR("Failure to refill session: '%d'\n", rc);
5752 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5757 class_detach(obd, lcfg);
5759 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5761 class_put_profile(lprof);
5766 OBD_FREE(name, name_size);
5768 OBD_FREE(uuid, uuid_size);
5772 /* setup quota master target on MDT0 */
5773 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5774 struct lustre_cfg *cfg)
5776 struct obd_device *obd;
5777 char *dev = lustre_cfg_string(cfg, 0);
5778 char *qmtname, *uuid, *p;
5779 struct lustre_cfg_bufs *bufs;
5780 struct lustre_cfg *lcfg;
5781 struct lustre_profile *lprof;
5782 struct obd_connect_data *data;
5787 LASSERT(mdt->mdt_qmt_exp == NULL);
5788 LASSERT(mdt->mdt_qmt_dev == NULL);
5790 /* quota master is on MDT0 only for now */
5791 if (mdt->mdt_seq_site.ss_node_id != 0)
5794 /* MGS generates config commands which look as follows:
5795 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5796 * 3:lustre-MDT0000-mdtlov 4:f
5798 * We generate the QMT name from the MDT one, just replacing MD with QM
5799 * after all the preparations, the logical equivalent will be:
5800 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5801 * 3:lustre-MDT0000-osd 4:f
5803 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5804 OBD_ALLOC(uuid, UUID_MAX);
5805 OBD_ALLOC_PTR(bufs);
5806 OBD_ALLOC_PTR(data);
5807 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5808 GOTO(cleanup_mem, rc = -ENOMEM);
5810 strcpy(qmtname, dev);
5811 p = strstr(qmtname, "-MDT");
5813 GOTO(cleanup_mem, rc = -ENOMEM);
5814 /* replace MD with QM */
5818 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5820 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5821 if (lprof == NULL || lprof->lp_dt == NULL) {
5822 CERROR("can't find profile for %s\n",
5823 lustre_cfg_string(cfg, 0));
5824 GOTO(cleanup_mem, rc = -EINVAL);
5827 lustre_cfg_bufs_reset(bufs, qmtname);
5828 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5829 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5830 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5832 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5834 GOTO(put_profile, rc = -ENOMEM);
5835 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5837 rc = class_attach(lcfg);
5839 GOTO(lcfg_cleanup, rc);
5841 obd = class_name2obd(qmtname);
5843 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5844 lustre_cfg_string(cfg, 0));
5845 GOTO(lcfg_cleanup, rc = -EINVAL);
5848 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5850 lustre_cfg_bufs_reset(bufs, qmtname);
5851 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5852 lustre_cfg_bufs_set_string(bufs, 2, dev);
5854 /* for quota, the next device should be the OSD device */
5855 lustre_cfg_bufs_set_string(bufs, 3,
5856 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5858 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5860 GOTO(class_detach, rc = -ENOMEM);
5861 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5863 rc = class_setup(obd, lcfg);
5865 GOTO(class_detach, rc);
5867 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5869 /* configure local quota objects */
5870 if (CFS_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5873 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5877 GOTO(class_cleanup, rc);
5879 /* connect to quota master target */
5880 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5881 data->ocd_version = LUSTRE_VERSION_CODE;
5882 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5885 CERROR("cannot connect to quota master device %s (%d)\n",
5887 GOTO(class_cleanup, rc);
5893 class_manual_cleanup(obd);
5894 mdt->mdt_qmt_dev = NULL;
5895 GOTO(lcfg_cleanup, rc);
5899 class_detach(obd, lcfg);
5901 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5903 class_put_profile(lprof);
5908 OBD_FREE(qmtname, MAX_OBD_NAME);
5910 OBD_FREE(uuid, UUID_MAX);
5916 /* Shutdown quota master target associated with mdt */
5917 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5921 if (mdt->mdt_qmt_exp == NULL)
5923 LASSERT(mdt->mdt_qmt_dev != NULL);
5925 /* the qmt automatically shuts down when the mdt disconnects */
5926 obd_disconnect(mdt->mdt_qmt_exp);
5927 mdt->mdt_qmt_exp = NULL;
5928 mdt->mdt_qmt_dev = NULL;
5932 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5933 * for now. This will be removed along with converting rest of MDT code
5934 * to use tgt_session_info
5936 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5938 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5941 if (unlikely(info->mti_object == NULL))
5944 rc = mdt_getxattr(info);
5946 mdt_thread_info_fini(info);
5950 static int mdt_llog_open(struct tgt_session_info *tsi)
5954 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
5955 RETURN(err_serious(-EACCES));
5957 RETURN(tgt_llog_open(tsi));
5960 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5961 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5962 #define OST_BRW_READ OST_READ
5963 #define OST_BRW_WRITE OST_WRITE
5965 static struct tgt_handler mdt_tgt_handlers[] = {
5966 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5967 0, MDS_CONNECT, mdt_tgt_connect,
5968 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5969 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5970 0, MDS_DISCONNECT, tgt_disconnect,
5971 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5972 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5973 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5974 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5975 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5976 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5977 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5978 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5980 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5981 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5982 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5983 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5984 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5985 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5986 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5987 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5989 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5990 mdt_hsm_ct_register),
5991 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5992 mdt_hsm_ct_unregister),
5993 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5995 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5997 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5998 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
6000 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
6003 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
6004 TGT_MDT_HDL(IS_MUTABLE, MDS_BATCH, mdt_batch),
6007 static struct tgt_handler mdt_io_ops[] = {
6008 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
6010 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
6012 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
6013 OST_PUNCH, mdt_punch_hdl,
6015 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
6016 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
6018 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
6019 TGT_RPC_HANDLER(OST_FIRST_OPC,
6020 0, OST_SET_INFO, mdt_io_set_info,
6021 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
6024 static struct tgt_handler mdt_sec_ctx_ops[] = {
6025 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
6026 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT, mdt_sec_ctx_handle),
6027 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
6030 static struct tgt_handler mdt_quota_ops[] = {
6031 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
6034 static struct tgt_handler mdt_llog_handlers[] = {
6035 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
6036 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
6037 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
6038 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
6041 static struct tgt_opc_slice mdt_common_slice[] = {
6043 .tos_opc_start = MDS_FIRST_OPC,
6044 .tos_opc_end = MDS_LAST_OPC,
6045 .tos_hs = mdt_tgt_handlers
6048 .tos_opc_start = OBD_FIRST_OPC,
6049 .tos_opc_end = OBD_LAST_OPC,
6050 .tos_hs = tgt_obd_handlers
6053 .tos_opc_start = LDLM_FIRST_OPC,
6054 .tos_opc_end = LDLM_LAST_OPC,
6055 .tos_hs = tgt_dlm_handlers
6058 .tos_opc_start = SEC_FIRST_OPC,
6059 .tos_opc_end = SEC_LAST_OPC,
6060 .tos_hs = mdt_sec_ctx_ops
6063 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
6064 .tos_opc_end = OUT_UPDATE_LAST_OPC,
6065 .tos_hs = tgt_out_handlers
6068 .tos_opc_start = FLD_FIRST_OPC,
6069 .tos_opc_end = FLD_LAST_OPC,
6070 .tos_hs = fld_handlers
6073 .tos_opc_start = SEQ_FIRST_OPC,
6074 .tos_opc_end = SEQ_LAST_OPC,
6075 .tos_hs = seq_handlers
6078 .tos_opc_start = QUOTA_DQACQ,
6079 .tos_opc_end = QUOTA_LAST_OPC,
6080 .tos_hs = mdt_quota_ops
6083 .tos_opc_start = LLOG_FIRST_OPC,
6084 .tos_opc_end = LLOG_LAST_OPC,
6085 .tos_hs = mdt_llog_handlers
6088 .tos_opc_start = LFSCK_FIRST_OPC,
6089 .tos_opc_end = LFSCK_LAST_OPC,
6090 .tos_hs = tgt_lfsck_handlers
6093 .tos_opc_start = OST_FIRST_OPC,
6094 .tos_opc_end = OST_LAST_OPC,
6095 .tos_hs = mdt_io_ops
6102 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
6104 struct md_device *next = m->mdt_child;
6105 struct lu_device *d = &m->mdt_lu_dev;
6106 struct obd_device *obd = mdt2obd_dev(m);
6107 struct lfsck_stop stop;
6110 stop.ls_status = LS_PAUSED;
6112 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
6114 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
6116 mdt_restriper_stop(m);
6117 ping_evictor_stop();
6119 /* Remove the HSM /proc entry so the coordinator cannot be
6120 * restarted by a user while it's shutting down.
6122 mdt_hsm_cdt_stop(m);
6124 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
6125 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
6127 if (m->mdt_namespace != NULL)
6128 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
6129 d->ld_obd->obd_force);
6131 obd_exports_barrier(obd);
6132 obd_zombie_barrier();
6134 mdt_quota_fini(env, m);
6136 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
6138 /* Calling the cleanup functions in the same order as in the mdt_init0
6141 mdt_tunables_fini(m);
6143 target_recovery_fini(obd);
6144 upcall_cache_cleanup(m->mdt_identity_cache);
6145 m->mdt_identity_cache = NULL;
6147 tgt_fini(env, &m->mdt_lut);
6149 mdt_hsm_cdt_fini(m);
6151 if (m->mdt_los != NULL) {
6152 local_oid_storage_fini(env, m->mdt_los);
6156 if (m->mdt_namespace != NULL) {
6157 ldlm_namespace_free_post(m->mdt_namespace);
6158 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
6161 if (m->mdt_md_root != NULL) {
6162 mdt_object_put(env, m->mdt_md_root);
6163 m->mdt_md_root = NULL;
6166 mdt_seq_fini(env, m);
6168 mdt_fld_fini(env, m);
6173 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6175 LASSERT(atomic_read(&d->ld_ref) == 0);
6177 server_put_mount(mdt_obd_name(m), true);
6182 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
6184 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
6185 struct lu_device_type *ldt, struct lustre_cfg *cfg)
6187 const struct dt_device_param *dt_conf;
6188 struct mdt_thread_info *info;
6189 struct obd_device *obd;
6190 const char *dev = lustre_cfg_string(cfg, 0);
6191 const char *num = lustre_cfg_string(cfg, 2);
6192 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
6193 struct lustre_mount_info *lmi = NULL;
6194 struct lustre_sb_info *lsi;
6196 struct seq_server_site *ss_site;
6197 const char *identity_upcall = "NONE";
6198 struct md_device *next;
6206 lu_device_init(&m->mdt_lu_dev, ldt);
6208 * Environment (env) might be missing mdt_thread_key values at that
6209 * point, if device is allocated when mdt_thread_key is in QUIESCENT
6212 * Usually device allocation path doesn't use module key values, but
6213 * mdt has to do a lot of work here, so allocate key value.
6215 rc = lu_env_refill((struct lu_env *)env);
6219 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
6220 LASSERT(info != NULL);
6222 obd = class_name2obd(dev);
6223 LASSERT(obd != NULL);
6225 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
6226 m->mdt_evict_tgt_nids = 1;
6227 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
6229 lmi = server_get_mount(dev);
6231 CERROR("Cannot get mount info for %s!\n", dev);
6234 lsi = s2lsi(lmi->lmi_sb);
6235 LASSERT(lsi->lsi_lmd);
6236 /* CMD is supported only in IAM mode */
6238 rc = kstrtol(num, 10, &node_id);
6243 if (test_bit(LMD_FLG_SKIP_LFSCK, lsi->lsi_lmd->lmd_flags))
6244 m->mdt_skip_lfsck = 1;
6245 if (test_bit(LMD_FLG_NO_CREATE, lsi->lsi_lmd->lmd_flags))
6246 m->mdt_lut.lut_no_create = 1;
6249 /* Just try to get a DoM lock by default. Otherwise, having a group
6250 * lock granted, it may get blocked for a long time.
6252 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
6253 /* DoM files are read at open and data is packed in the reply */
6254 m->mdt_dom_read_open = 1;
6256 m->mdt_squash.rsi_uid = 0;
6257 m->mdt_squash.rsi_gid = 0;
6258 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
6259 spin_lock_init(&m->mdt_squash.rsi_lock);
6260 spin_lock_init(&m->mdt_lock);
6261 m->mdt_enable_chprojid_gid = 0;
6262 m->mdt_enable_dir_migration = 1;
6263 m->mdt_enable_dir_restripe = 0;
6264 m->mdt_enable_dir_auto_split = 0;
6265 m->mdt_enable_parallel_rename_dir = 1;
6266 m->mdt_enable_parallel_rename_file = 1;
6267 m->mdt_enable_parallel_rename_crossdir = 1;
6268 m->mdt_enable_remote_dir = 1;
6269 m->mdt_enable_remote_dir_gid = 0;
6270 m->mdt_enable_remote_rename = 1;
6271 m->mdt_enable_striped_dir = 1;
6272 m->mdt_enable_dmv_implicit_inherit = 1;
6273 m->mdt_dir_restripe_nsonly = 1;
6274 m->mdt_max_mod_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
6276 atomic_set(&m->mdt_mds_mds_conns, 0);
6277 atomic_set(&m->mdt_async_commit_count, 0);
6278 atomic_set(&m->mdt_dmv_old_client_count, 0);
6280 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
6281 m->mdt_lu_dev.ld_obd = obd;
6282 /* Set this lu_device to obd for error handling purposes. */
6283 obd->obd_lu_dev = &m->mdt_lu_dev;
6285 strncpy(m->mdt_job_xattr, XATTR_NAME_JOB_DEFAULT, XATTR_JOB_MAX_LEN);
6287 /* init the stack */
6288 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
6290 CERROR("%s: Can't init device stack, rc %d\n",
6291 mdt_obd_name(m), rc);
6296 ss_site = mdt_seq_site(m);
6297 s->ld_seq_site = ss_site;
6300 /* set server index */
6301 ss_site->ss_node_id = node_id;
6303 /* failover is the default
6304 * FIXME: we do not failout mds0/mgs, which may cause some problems.
6305 * assumed whose ss_node_id == 0 XXX
6307 obd->obd_replayable = 1;
6308 /* No connection accepted until configurations will finish */
6309 obd->obd_no_conn = 1;
6311 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
6312 char *str = lustre_cfg_string(cfg, 4);
6314 if (strchr(str, 'n')) {
6315 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
6316 obd->obd_replayable = 0;
6320 rc = mdt_fld_init(env, mdt_obd_name(m), m);
6322 GOTO(err_fini_stack, rc);
6324 rc = mdt_seq_init(env, m);
6326 GOTO(err_fini_fld, rc);
6328 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
6329 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
6330 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
6331 LDLM_NAMESPACE_SERVER,
6332 LDLM_NAMESPACE_GREEDY,
6334 if (IS_ERR(m->mdt_namespace)) {
6335 rc = PTR_ERR(m->mdt_namespace);
6336 CERROR("%s: unable to create server namespace: rc = %d\n",
6338 m->mdt_namespace = NULL;
6339 GOTO(err_fini_seq, rc);
6342 m->mdt_namespace->ns_lvbp = m;
6343 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
6345 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
6346 /* set obd_namespace for compatibility with old code */
6347 obd->obd_namespace = m->mdt_namespace;
6349 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
6350 OBD_FAIL_MDS_ALL_REQUEST_NET,
6351 OBD_FAIL_MDS_ALL_REPLY_NET);
6353 GOTO(err_free_ns, rc);
6355 /* Amount of available space excluded from granting and reserved
6356 * for metadata. It is a percentage of the total MDT size.
6358 tgd->tgd_reserved_pcnt = 10;
6360 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
6361 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
6363 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6365 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
6366 GOTO(err_tgt, rc = -ENOENT);
6368 fid.f_seq = FID_SEQ_LOCAL_NAME;
6371 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6375 rc = mdt_hsm_cdt_init(m);
6377 CERROR("%s: error initializing coordinator, rc %d\n",
6378 mdt_obd_name(m), rc);
6379 GOTO(err_los_fini, rc);
6382 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6384 next = m->mdt_child;
6385 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6387 mntopts = dt_conf->ddp_mntopts;
6389 if (mntopts & MNTOPT_USERXATTR)
6390 m->mdt_opts.mo_user_xattr = 1;
6392 m->mdt_opts.mo_user_xattr = 0;
6394 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6396 if (mntopts & MNTOPT_ACL)
6397 m->mdt_opts.mo_acl = 1;
6399 m->mdt_opts.mo_acl = 0;
6401 m->mdt_enable_strict_som = 1;
6403 /* XXX: to support suppgid for ACL, we enable identity_upcall
6404 * by default, otherwise, maybe got unexpected -EACCESS.
6406 if (m->mdt_opts.mo_acl)
6407 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6409 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6411 UC_IDCACHE_HASH_SIZE,
6412 1200, /* entry expire: 20 mn */
6413 30, /* acquire expire: 30 s */
6414 true, /* acquire can replay */
6415 &mdt_identity_upcall_cache_ops);
6416 if (IS_ERR(m->mdt_identity_cache)) {
6417 rc = PTR_ERR(m->mdt_identity_cache);
6418 m->mdt_identity_cache = NULL;
6419 GOTO(err_free_hsm, rc);
6422 rc = mdt_tunables_init(m, dev);
6424 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
6425 GOTO(err_recovery, rc);
6428 rc = mdt_quota_init(env, m, cfg);
6430 GOTO(err_procfs, rc);
6432 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
6433 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
6434 "mdt_ldlm_client", m->mdt_ldlm_client);
6436 ping_evictor_start();
6438 /* recovery will be started upon mdt_prepare() when the whole stack is
6439 * complete and ready to serve the requests
6442 /* Reduce the initial timeout on an MDS because it doesn't need such
6443 * a long timeout as an OST does. Adaptive timeouts will adjust this
6444 * value appropriately.
6446 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
6447 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
6449 if (test_bit(LMD_FLG_LOCAL_RECOV, lsi->lsi_lmd->lmd_flags))
6450 m->mdt_lut.lut_local_recovery = 1;
6452 rc = mdt_restriper_start(m);
6454 GOTO(err_ping_evictor, rc);
6459 ping_evictor_stop();
6461 mdt_tunables_fini(m);
6463 upcall_cache_cleanup(m->mdt_identity_cache);
6464 m->mdt_identity_cache = NULL;
6466 mdt_hsm_cdt_fini(m);
6468 local_oid_storage_fini(env, m->mdt_los);
6471 /* keep recoverable clients */
6473 target_recovery_fini(obd);
6474 obd_exports_barrier(obd);
6475 obd_zombie_barrier();
6476 tgt_fini(env, &m->mdt_lut);
6478 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
6479 obd->obd_namespace = m->mdt_namespace = NULL;
6481 mdt_seq_fini(env, m);
6483 mdt_fld_fini(env, m);
6485 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6488 server_put_mount(dev, true);
6492 /* For interoperability, the left element is old parameter, the right one
6493 * is the new version of the parameter, if some parameter is deprecated,
6494 * the new version should be set as NULL.
6496 static struct cfg_interop_param mdt_interop_param[] = {
6497 { "mdt.group_upcall", NULL },
6498 { "mdt.quota_type", NULL },
6499 { "mdd.quota_type", NULL },
6500 { "mdt.som", NULL },
6501 { "mdt.rootsquash", "mdt.root_squash" },
6502 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
6506 /* used by MGS to process specific configurations */
6507 static int mdt_process_config(const struct lu_env *env,
6508 struct lu_device *d, struct lustre_cfg *cfg)
6510 struct mdt_device *m = mdt_dev(d);
6511 struct md_device *md_next = m->mdt_child;
6512 struct lu_device *next = md2lu_dev(md_next);
6517 switch (cfg->lcfg_command) {
6519 struct obd_device *obd = d->ld_obd;
6520 /* For interoperability */
6521 struct cfg_interop_param *ptr = NULL;
6522 struct lustre_cfg *old_cfg = NULL;
6526 param = lustre_cfg_string(cfg, 1);
6527 if (param == NULL) {
6528 CERROR("param is empty\n");
6533 ptr = class_find_old_param(param, mdt_interop_param);
6535 if (ptr->new_param == NULL) {
6537 CWARN("For interoperability, skip this %s. It is obsolete.\n",
6542 CWARN("Found old param %s, changed it to %s.\n",
6543 ptr->old_param, ptr->new_param);
6546 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
6553 count = class_modify_config(cfg, PARAM_MDT,
6554 &obd->obd_kset.kobj);
6556 struct coordinator *cdt = &m->mdt_coordinator;
6558 /* is it an HSM var ? */
6559 count = class_modify_config(cfg, PARAM_HSM,
6560 &cdt->cdt_hsm_kobj);
6562 /* we don't understand; pass it on */
6563 rc = next->ld_ops->ldo_process_config(env, next,
6566 rc = count > 0 ? 0 : count;
6568 rc = count > 0 ? 0 : count;
6572 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
6573 cfg->lcfg_buflens));
6577 /* others are passed further */
6578 rc = next->ld_ops->ldo_process_config(env, next, cfg);
6584 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
6585 const struct lu_object_header *hdr,
6586 struct lu_device *d)
6588 struct mdt_object *mo;
6592 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6594 struct lu_object *o;
6595 struct lu_object_header *h;
6598 h = &mo->mot_header;
6599 lu_object_header_init(h);
6600 lu_object_init(o, h, d);
6601 lu_object_add_top(h, o);
6602 o->lo_ops = &mdt_obj_ops;
6603 spin_lock_init(&mo->mot_write_lock);
6604 mutex_init(&mo->mot_som_mutex);
6605 mutex_init(&mo->mot_lov_mutex);
6606 init_rwsem(&mo->mot_dom_sem);
6607 init_rwsem(&mo->mot_open_sem);
6608 atomic_set(&mo->mot_open_count, 0);
6609 mo->mot_restripe_offset = 0;
6610 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6611 mo->mot_lsom_size = 0;
6612 mo->mot_lsom_blocks = 0;
6613 mo->mot_lsom_inited = false;
6619 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6620 const struct lu_object_conf *unused)
6622 struct mdt_device *d = mdt_dev(o->lo_dev);
6623 struct lu_device *under;
6624 struct lu_object *below;
6629 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6630 PFID(lu_object_fid(o)));
6632 under = &d->mdt_child->md_lu_dev;
6633 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6635 lu_object_add(o, below);
6642 static void mdt_object_free_rcu(struct rcu_head *head)
6644 struct mdt_object *mo = container_of(head, struct mdt_object,
6645 mot_header.loh_rcu);
6647 kmem_cache_free(mdt_object_kmem, mo);
6650 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6652 struct mdt_object *mo = mdt_obj(o);
6653 struct lu_object_header *h;
6658 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6659 PFID(lu_object_fid(o)));
6661 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6662 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6665 lu_object_header_fini(h);
6666 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6667 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6672 static int mdt_object_print(const struct lu_env *env, void *cookie,
6673 lu_printer_t p, const struct lu_object *o)
6675 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6677 return (*p)(env, cookie,
6678 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6679 mdto, mdto->mot_lov_created ? "lov_created" : "",
6680 mdto->mot_cache_attr ? "cache_attr" : "",
6681 mdto->mot_write_count);
6684 static int mdt_prepare(const struct lu_env *env,
6685 struct lu_device *pdev,
6686 struct lu_device *cdev)
6688 struct mdt_device *mdt = mdt_dev(cdev);
6689 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6690 struct obd_device *obd = cdev->ld_obd;
6697 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6701 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6705 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6709 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6710 /* The LFSCK instance is registered just now, so it must be there when
6711 * register the namespace to such instance.
6713 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6715 if (mdt->mdt_seq_site.ss_node_id == 0) {
6716 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6717 &mdt->mdt_md_root_fid);
6722 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6724 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6725 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6726 LASSERT(obd->obd_no_conn);
6727 spin_lock(&obd->obd_dev_lock);
6728 obd->obd_no_conn = 0;
6729 spin_unlock(&obd->obd_dev_lock);
6731 if (obd->obd_recovering == 0)
6732 mdt_postrecov(env, mdt);
6737 const struct lu_device_operations mdt_lu_ops = {
6738 .ldo_object_alloc = mdt_object_alloc,
6739 .ldo_process_config = mdt_process_config,
6740 .ldo_prepare = mdt_prepare,
6743 static const struct lu_object_operations mdt_obj_ops = {
6744 .loo_object_init = mdt_object_init,
6745 .loo_object_free = mdt_object_free,
6746 .loo_object_print = mdt_object_print
6749 static int mdt_obd_set_info_async(const struct lu_env *env,
6750 struct obd_export *exp,
6751 __u32 keylen, void *key,
6752 __u32 vallen, void *val,
6753 struct ptlrpc_request_set *set)
6759 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6760 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6767 static inline void mdt_enable_slc(struct mdt_device *mdt)
6769 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6770 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6773 static inline void mdt_disable_slc(struct mdt_device *mdt)
6775 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6776 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6780 * Match client and server connection feature flags.
6782 * Compute the compatibility flags for a connection request based on
6783 * features mutually supported by client and server.
6785 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6786 * must not be updated here, otherwise a partially initialized value may
6787 * be exposed. After the connection request is successfully processed,
6788 * the top-level MDT connect request handler atomically updates the export
6789 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6790 * reply. \see mdt_connect().
6792 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6793 * full struct obd_connect_data. So care must be taken when accessing fields
6794 * that are not present in struct obd_connect_data_v1. See LU-16.
6796 * \param exp the obd_export associated with this client/target pair
6797 * \param mdt the target device for the connection
6798 * \param data stores data for this connect request
6801 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6802 * \retval -EBADE client and server feature requirements are incompatible
6804 static int mdt_connect_internal(const struct lu_env *env,
6805 struct obd_export *exp,
6806 struct mdt_device *mdt,
6807 struct obd_connect_data *data, bool reconnect)
6809 const char *obd_name = mdt_obd_name(mdt);
6811 LASSERT(data != NULL);
6813 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6815 if (mdt->mdt_bottom->dd_rdonly &&
6816 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6817 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6820 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6821 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6823 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6825 if (!mdt->mdt_opts.mo_acl)
6826 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6828 if (!mdt->mdt_opts.mo_user_xattr)
6829 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6831 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6832 data->ocd_brw_size = min(data->ocd_brw_size,
6834 if (data->ocd_brw_size == 0) {
6835 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx ocd_version: %x ocd_grant: %d ocd_index: %u ocd_brw_size unexpectedly zero, network data corruption? Refusing to connect this client\n", obd_name, exp->exp_client_uuid.uuid,
6836 exp, data->ocd_connect_flags, data->ocd_version,
6837 data->ocd_grant, data->ocd_index);
6842 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6843 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6845 /* client is reporting its page size, for future use */
6846 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6847 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6848 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6849 * it's LDISKFS_DIR_REC_LEN(20) = 28.
6851 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6852 /* ocd_grant_tax_kb is in 1K byte blocks */
6853 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6854 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6857 /* Save connect_data we have so far because tgt_grant_connect()
6858 * uses it to calculate grant, and we want to save the client
6859 * version before it is overwritten by LUSTRE_VERSION_CODE.
6861 exp->exp_connect_data = *data;
6862 if (OCD_HAS_FLAG(data, GRANT))
6863 tgt_grant_connect(env, exp, data, !reconnect);
6865 if (OCD_HAS_FLAG(data, MAXBYTES))
6866 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6868 /* NB: Disregard the rule against updating
6869 * exp_connect_data.ocd_connect_flags in this case, since
6870 * tgt_client_new() needs to know if this is a lightweight
6871 * connection, and it is safe to expose this flag before
6872 * connection processing completes.
6874 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6875 spin_lock(&exp->exp_lock);
6876 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6877 spin_unlock(&exp->exp_lock);
6880 data->ocd_version = LUSTRE_VERSION_CODE;
6882 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6883 CWARN("%s: MDS requires FID support, but client not\n",
6888 if (OCD_HAS_FLAG(data, PINGLESS)) {
6889 if (ptlrpc_pinger_suppress_pings()) {
6890 spin_lock(&exp->exp_obd->obd_dev_lock);
6891 list_del_init(&exp->exp_obd_chain_timed);
6892 spin_unlock(&exp->exp_obd->obd_dev_lock);
6894 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6898 data->ocd_max_easize = mdt->mdt_max_ea_size;
6900 /* NB: Disregard the rule against updating
6901 * exp_connect_data.ocd_connect_flags in this case, since
6902 * tgt_client_new() needs to know if this is client supports
6903 * multiple modify RPCs, and it is safe to expose this flag before
6904 * connection processing completes.
6906 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6907 if (mdt_max_mod_rpcs_changed(mdt))
6908 /* The new mdt.*.max_mod_rpcs_in_flight parameter
6909 * has not changed since initialization, but the
6910 * deprecated module parameter was changed,
6911 * so use that instead.
6913 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6915 data->ocd_maxmodrpcs = mdt->mdt_max_mod_rpcs_in_flight;
6916 spin_lock(&exp->exp_lock);
6917 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6918 spin_unlock(&exp->exp_lock);
6921 if (OCD_HAS_FLAG(data, CKSUM)) {
6922 __u32 cksum_types = data->ocd_cksum_types;
6924 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6926 if (unlikely(data->ocd_cksum_types == 0)) {
6927 CERROR("%s: Connect with checksum support but no ocd_cksum_types is set\n",
6928 exp->exp_obd->obd_name);
6932 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return %x\n",
6933 exp->exp_obd->obd_name, obd_export_nid2str(exp),
6934 cksum_types, data->ocd_cksum_types);
6936 /* Client not support OBD_CONNECT_CKSUM? fall back to CRC32 */
6937 CDEBUG(D_RPCTRACE, "%s: cli %s does not support OBD_CONNECT_CKSUM, CRC32 will be used\n",
6938 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6941 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6942 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6943 atomic_inc(&mdt->mdt_mds_mds_conns);
6944 mdt_enable_slc(mdt);
6947 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6948 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6950 if (!OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
6951 !OCD_HAS_FLAG2(data, DMV_IMP_INHERIT)) {
6952 atomic_inc(&mdt->mdt_dmv_old_client_count);
6953 mdt->mdt_enable_dmv_implicit_inherit = 0;
6959 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6960 struct mdt_thread_info *info,
6961 struct mdt_file_data *mfd)
6963 struct lu_context ses;
6968 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6973 lu_context_enter(&ses);
6975 mdt_ucred(info)->uc_valid = UCRED_OLD;
6976 /* do not let rbac interfere with dirty flag internal system event */
6977 mdt_ucred(info)->uc_rbac_file_perms = 1;
6978 mdt_ucred(info)->uc_rbac_dne_ops = 1;
6979 mdt_ucred(info)->uc_rbac_quota_ops = 1;
6980 mdt_ucred(info)->uc_rbac_byfid_ops = 1;
6981 mdt_ucred(info)->uc_rbac_chlg_ops = 1;
6982 mdt_ucred(info)->uc_rbac_fscrypt_admin = 1;
6983 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6985 lu_context_exit(&ses);
6986 lu_context_fini(&ses);
6992 static int mdt_export_cleanup(struct obd_export *exp)
6994 LIST_HEAD(closing_list);
6995 struct mdt_export_data *med = &exp->exp_mdt_data;
6996 struct obd_device *obd = exp->exp_obd;
6997 struct mdt_device *mdt;
6998 struct mdt_thread_info *info;
7000 struct mdt_file_data *mfd, *n;
7005 spin_lock(&med->med_open_lock);
7006 while (!list_empty(&med->med_open_head)) {
7007 struct list_head *tmp = med->med_open_head.next;
7009 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
7011 /* Remove mfd handle so it can't be found again.
7012 * We are consuming the mfd_list reference here.
7014 class_handle_unhash(&mfd->mfd_open_handle);
7015 list_move_tail(&mfd->mfd_list, &closing_list);
7017 spin_unlock(&med->med_open_lock);
7018 mdt = mdt_dev(obd->obd_lu_dev);
7019 LASSERT(mdt != NULL);
7021 rc = lu_env_init(&env, LCT_MD_THREAD);
7025 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7026 LASSERT(info != NULL);
7027 memset(info, 0, sizeof(*info));
7028 info->mti_env = &env;
7029 info->mti_mdt = mdt;
7030 info->mti_exp = exp;
7032 if (!list_empty(&closing_list)) {
7033 struct md_attr *ma = &info->mti_attr;
7035 /* Close any open files (which may cause orphan unlinking). */
7036 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
7037 list_del_init(&mfd->mfd_list);
7038 ma->ma_need = ma->ma_valid = 0;
7040 /* This file is being closed due to an eviction, it
7041 * could have been modified and now dirty regarding to
7042 * HSM archive, check this!
7043 * The logic here is to mark a file dirty if there's a
7044 * chance it was dirtied before the client was evicted,
7045 * so that we don't have to wait for a release attempt
7046 * before finding out the file was actually dirty and
7047 * fail the release. Aggressively marking it dirty here
7048 * will cause the policy engine to attempt to
7049 * re-archive it; when rearchiving, we can compare the
7050 * current version to the HSM data_version and make the
7051 * archive request into a noop if it's not actually
7054 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
7055 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
7057 /* Don't unlink orphan on failover umount, LU-184 */
7058 if (exp->exp_flags & OBD_OPT_FAILOVER ||
7059 exp->exp_obd->obd_stopping) {
7060 ma->ma_valid = MA_FLAGS;
7061 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
7063 ma->ma_valid |= MA_FORCE_LOG;
7064 mdt_mfd_close(info, mfd);
7067 info->mti_mdt = NULL;
7068 /* cleanup client slot early */
7069 /* Do not erase record for recoverable client. */
7070 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
7071 tgt_client_del(&env, exp);
7077 static int mdt_obd_disconnect(struct obd_export *exp)
7079 struct obd_connect_data *data = &exp->exp_connect_data;
7080 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
7086 class_export_get(exp);
7088 if (OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
7089 atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
7090 mdt_disable_slc(mdt);
7092 if (!OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
7093 !OCD_HAS_FLAG2(data, DMV_IMP_INHERIT) &&
7094 atomic_dec_and_test(&mdt->mdt_dmv_old_client_count))
7095 mdt->mdt_enable_dmv_implicit_inherit = 1;
7097 rc = server_disconnect_export(exp);
7099 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
7101 tgt_grant_discard(exp);
7103 if (!(exp->exp_flags & OBD_OPT_FORCE))
7104 tgt_grant_sanity_check(exp->exp_obd, __func__);
7106 rc = mdt_export_cleanup(exp);
7107 nodemap_del_member(exp);
7108 class_export_put(exp);
7112 /* mds_connect copy */
7113 static int mdt_obd_connect(const struct lu_env *env,
7114 struct obd_export **exp, struct obd_device *obd,
7115 struct obd_uuid *cluuid,
7116 struct obd_connect_data *data,
7119 struct obd_export *lexp;
7120 struct lustre_handle conn = { 0 };
7121 struct mdt_device *mdt;
7123 struct lnet_nid *client_nid = localdata;
7127 LASSERT(env != NULL);
7128 LASSERT(data != NULL);
7130 if (!exp || !obd || !cluuid)
7133 mdt = mdt_dev(obd->obd_lu_dev);
7136 * first, check whether the stack is ready to handle requests
7137 * XXX: probably not very appropriate method is used now
7138 * at some point we should find a better one
7140 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
7141 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
7142 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
7143 rc = obd_get_info(env, mdt->mdt_child_exp,
7144 sizeof(KEY_OSP_CONNECTED),
7145 KEY_OSP_CONNECTED, NULL, NULL);
7148 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
7151 rc = class_connect(&conn, obd, cluuid);
7155 lexp = class_conn2export(&conn);
7156 LASSERT(lexp != NULL);
7158 rc = nodemap_add_member(client_nid, lexp);
7159 if (rc != 0 && rc != -EEXIST)
7162 rc = mdt_connect_internal(env, lexp, mdt, data, false);
7164 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
7167 memcpy(lcd->lcd_uuid, cluuid, sizeof(lcd->lcd_uuid));
7168 rc = tgt_client_new(env, lexp);
7170 mdt_export_stats_init(obd, lexp, localdata);
7174 class_disconnect(lexp);
7175 nodemap_del_member(lexp);
7179 /* Because we do not want this export to be evicted by pinger,
7180 * let's not add this export to the timed chain list.
7182 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
7183 spin_lock(&lexp->exp_obd->obd_dev_lock);
7184 list_del_init(&lexp->exp_obd_chain_timed);
7185 spin_unlock(&lexp->exp_obd->obd_dev_lock);
7192 static int mdt_obd_reconnect(const struct lu_env *env,
7193 struct obd_export *exp, struct obd_device *obd,
7194 struct obd_uuid *cluuid,
7195 struct obd_connect_data *data,
7198 struct lnet_nid *client_nid = localdata;
7203 if (exp == NULL || obd == NULL || cluuid == NULL)
7206 rc = nodemap_add_member(client_nid, exp);
7207 if (rc != 0 && rc != -EEXIST)
7210 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
7213 mdt_export_stats_init(obd, exp, localdata);
7215 nodemap_del_member(exp);
7220 /* FIXME: Can we avoid using these two interfaces? */
7221 static int mdt_init_export(struct obd_export *exp)
7223 struct mdt_export_data *med = &exp->exp_mdt_data;
7228 INIT_LIST_HEAD(&med->med_open_head);
7229 spin_lock_init(&med->med_open_lock);
7230 spin_lock(&exp->exp_lock);
7231 exp->exp_connecting = 1;
7232 spin_unlock(&exp->exp_lock);
7234 OBD_ALLOC(exp->exp_used_slots,
7235 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7236 if (exp->exp_used_slots == NULL)
7239 /* self-export doesn't need client data and ldlm initialization */
7240 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
7241 &exp->exp_client_uuid)))
7244 rc = tgt_client_alloc(exp);
7248 rc = ldlm_init_export(exp);
7255 tgt_client_free(exp);
7257 OBD_FREE(exp->exp_used_slots,
7258 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7259 exp->exp_used_slots = NULL;
7261 CERROR("%s: Failed to initialize export: rc = %d\n",
7262 exp->exp_obd->obd_name, rc);
7266 static int mdt_destroy_export(struct obd_export *exp)
7270 target_destroy_export(exp);
7271 if (exp->exp_used_slots)
7272 OBD_FREE(exp->exp_used_slots,
7273 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7275 /* destroy can be called from failed obd_setup, so
7276 * checking uuid is safer than obd_self_export
7278 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
7279 &exp->exp_client_uuid)))
7282 ldlm_destroy_export(exp);
7283 tgt_client_free(exp);
7285 LASSERT(list_empty(&exp->exp_outstanding_replies));
7286 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
7289 * discard grants once we're sure no more
7290 * interaction with the client is possible
7292 tgt_grant_discard(exp);
7293 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
7294 obd2obt(exp->exp_obd)->obt_lut->lut_tgd.tgd_tot_granted_clients--;
7296 if (!(exp->exp_flags & OBD_OPT_FORCE))
7297 tgt_grant_sanity_check(exp->exp_obd, __func__);
7302 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
7303 struct linkea_data *ldata)
7307 LASSERT(ldata->ld_buf->lb_buf != NULL);
7309 if (!mdt_object_exists(mdt_obj))
7312 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7313 ldata->ld_buf, XATTR_NAME_LINK);
7314 if (rc == -ERANGE) {
7315 /* Buf was too small, figure out what we need. */
7316 lu_buf_free(ldata->ld_buf);
7317 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7318 ldata->ld_buf, XATTR_NAME_LINK);
7321 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
7322 if (ldata->ld_buf->lb_buf == NULL)
7324 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7325 ldata->ld_buf, XATTR_NAME_LINK);
7330 return linkea_init_with_rec(ldata);
7334 * Given an MDT object, try to look up the full path to the object.
7335 * Part of the MDT layer implementation of lfs fid2path.
7337 * \param[in] info Per-thread common data shared by MDT level handlers.
7338 * \param[in] obj Object to do path lookup of
7339 * \param[in,out] fp User-provided struct to store path information
7340 * \param[in] root_fid Root FID of current path should reach
7342 * \retval 0 Lookup successful, path information stored in fp
7343 * \retval -EAGAIN Lookup failed, usually because object is being moved
7344 * \retval negative errno if there was a problem
7346 static int mdt_path_current(struct mdt_thread_info *info,
7347 struct mdt_object *obj,
7348 struct getinfo_fid2path *fp,
7349 struct lu_fid *root_fid)
7351 struct mdt_device *mdt = info->mti_mdt;
7352 struct lu_name *tmpname = &info->mti_name;
7353 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
7354 struct lu_buf *buf = &info->mti_big_buf;
7355 struct linkea_data ldata = { NULL };
7357 struct mdt_object *mdt_obj;
7358 struct link_ea_header *leh;
7359 struct link_ea_entry *lee;
7360 bool worthchecking = true;
7361 bool needsfid = false;
7362 bool supported = false;
7370 /* temp buffer for path element, the buffer will be finally freed
7371 * in mdt_thread_info_fini
7373 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
7374 if (buf->lb_buf == NULL)
7378 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
7381 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
7383 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
7384 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
7385 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
7386 GOTO(out, rc = -ENOENT);
7388 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
7390 mdt_object_get(info->mti_env, mdt_obj);
7392 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
7393 if (IS_ERR(mdt_obj))
7394 GOTO(out, rc = PTR_ERR(mdt_obj));
7397 if (!mdt_object_exists(mdt_obj)) {
7398 mdt_object_put(info->mti_env, mdt_obj);
7399 GOTO(out, rc = -ENOENT);
7402 if (mdt_object_remote(mdt_obj)) {
7403 mdt_object_put(info->mti_env, mdt_obj);
7404 GOTO(remote_out, rc = -EREMOTE);
7407 if (worthchecking) {
7408 /* need to know if FID being looked up is encrypted */
7409 struct lu_attr la = { 0 };
7410 struct dt_object *dt = mdt_obj2dt(mdt_obj);
7412 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
7413 dt_attr_get(info->mti_env, dt, &la);
7414 if (la.la_valid & LA_FLAGS &&
7415 la.la_flags & LUSTRE_ENCRYPT_FL) {
7416 if (!supported && mdt_info_req(info) &&
7417 !exp_connect_encrypt_fid2path(
7418 mdt_info_req(info)->rq_export)) {
7419 /* client does not support fid2path
7420 * for encrypted files
7422 mdt_object_put(info->mti_env, mdt_obj);
7423 GOTO(out, rc = -ENODATA);
7431 worthchecking = false;
7438 rc = mdt_links_read(info, mdt_obj, &ldata);
7440 mdt_object_put(info->mti_env, mdt_obj);
7445 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
7446 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
7447 /* If set, use link #linkno for path lookup, otherwise use
7448 * link #0. Only do this for the final path ement.
7450 if (first && fp->gf_linkno < leh->leh_reccount) {
7453 for (count = 0; count < fp->gf_linkno; count++) {
7454 lee = (struct link_ea_entry *)
7455 ((char *)lee + reclen);
7456 linkea_entry_unpack(lee, &reclen, tmpname,
7459 if (fp->gf_linkno < leh->leh_reccount - 1)
7460 /* indicate to user there are more links */
7464 /* Check if it is slave stripes */
7465 rc = mdt_is_dir_stripe(info, mdt_obj);
7466 mdt_object_put(info->mti_env, mdt_obj);
7470 fp->gf_fid = *tmpfid;
7474 /* Pack the name in the end of the buffer */
7475 ptr -= tmpname->ln_namelen;
7476 if (ptr - 1 <= fp->gf_u.gf_path)
7477 GOTO(out, rc = -ENAMETOOLONG);
7478 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
7480 /* Pack FID before file name, so that client can build
7481 * encoded/digested form.
7483 char fidstr[FID_LEN + 1];
7485 snprintf(fidstr, sizeof(fidstr), DFID,
7487 ptr -= strlen(fidstr);
7488 if (ptr - 1 <= fp->gf_u.gf_path)
7489 GOTO(out, rc = -ENAMETOOLONG);
7490 strncpy(ptr, fidstr, strlen(fidstr));
7494 /* keep the last resolved fid to the client, so the client will
7495 * build the left path on another MDT for remote object
7497 fp->gf_fid = *tmpfid;
7502 /* non-zero will be treated as an error */
7507 ptr++; /* skip leading / unless this is an encrypted file */
7508 memmove(fp->gf_u.gf_path, ptr,
7509 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
7516 * Given an MDT object, use mdt_path_current to get the path.
7517 * Essentially a wrapper to retry mdt_path_current a set number of times
7518 * if -EAGAIN is returned (usually because an object is being moved).
7520 * Part of the MDT layer implementation of lfs fid2path.
7522 * \param[in] info Per-thread common data shared by mdt level handlers.
7523 * \param[in] obj Object to do path lookup of
7524 * \param[in,out] fp User-provided struct for arguments and to store path
7527 * \retval 0 Lookup successful, path information stored in fp
7528 * \retval negative errno if there was a problem
7530 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
7531 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
7533 struct mdt_device *mdt = info->mti_mdt;
7539 if (fp->gf_pathlen < 3)
7542 if (root_fid == NULL)
7543 root_fid = &mdt->mdt_md_root_fid;
7545 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
7546 fp->gf_u.gf_path[0] = '\0';
7550 /* Retry multiple times in case file is being moved */
7551 while (tries-- && rc == -EAGAIN)
7552 rc = mdt_path_current(info, obj, fp, root_fid);
7558 * Get the full path of the provided FID, as of changelog record recno.
7560 * This checks sanity and looks up object for user provided FID
7561 * before calling the actual path lookup code.
7563 * Part of the MDT layer implementation of lfs fid2path.
7565 * \param[in] info Per-thread common data shared by mdt level handlers.
7566 * \param[in,out] fp User-provided struct for arguments and to store path
7569 * \retval 0 Lookup successful, path information and recno stored in fp
7570 * \retval -ENOENT, object does not exist
7571 * \retval negative errno if there was a problem
7573 static int mdt_fid2path(struct mdt_thread_info *info,
7574 struct lu_fid *root_fid,
7575 struct getinfo_fid2path *fp)
7577 struct mdt_device *mdt = info->mti_mdt;
7578 struct mdt_object *obj;
7583 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
7584 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
7586 if (!fid_is_sane(&fp->gf_fid))
7589 if (!fid_is_namespace_visible(&fp->gf_fid)) {
7590 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx, or f_oid != 0, or f_ver == 0\n",
7592 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
7596 /* return error if client-provided root fid is not the one stored in
7599 if (root_fid && !fid_is_zero(&info->mti_exp->exp_root_fid) &&
7600 !lu_fid_eq(root_fid, &info->mti_exp->exp_root_fid)) {
7602 "%s: root fid from client "DFID" but "DFID" stored in export\n",
7603 mdt_obd_name(mdt), PFID(root_fid),
7604 PFID(&info->mti_exp->exp_root_fid));
7608 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
7611 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
7612 PFID(&fp->gf_fid), rc);
7616 if (mdt_object_remote(obj))
7618 else if (!mdt_object_exists(obj))
7624 mdt_object_put(info->mti_env, obj);
7625 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
7626 PFID(&fp->gf_fid), rc);
7630 rc = mdt_path(info, obj, fp, root_fid);
7632 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
7633 PFID(&fp->gf_fid), fp->gf_u.gf_path,
7634 fp->gf_recno, fp->gf_linkno);
7636 mdt_object_put(info->mti_env, obj);
7641 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
7642 void *val, int vallen)
7644 struct getinfo_fid2path *fpout, *fpin;
7645 struct lu_fid *root_fid = NULL;
7648 fpin = key + round_up(sizeof(KEY_FID2PATH), 8);
7651 if (req_capsule_req_need_swab(info->mti_pill))
7652 lustre_swab_fid2path(fpin);
7654 memcpy(fpout, fpin, sizeof(*fpin));
7655 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
7658 if (keylen >= round_up(sizeof(KEY_FID2PATH), 8) + sizeof(*fpin) +
7659 sizeof(struct lu_fid)) {
7660 /* client sent its root FID, which is normally fileset FID */
7661 root_fid = fpin->gf_u.gf_root_fid;
7662 if (req_capsule_req_need_swab(info->mti_pill))
7663 lustre_swab_lu_fid(root_fid);
7665 if (root_fid != NULL && !fid_is_sane(root_fid))
7669 rc = mdt_fid2path(info, root_fid, fpout);
7673 int mdt_get_info(struct tgt_session_info *tsi)
7683 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7685 CDEBUG(D_IOCTL, "No GETINFO key\n");
7686 RETURN(err_serious(-EFAULT));
7688 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7691 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7692 if (vallen == NULL) {
7693 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7694 tgt_name(tsi->tsi_tgt));
7695 RETURN(err_serious(-EFAULT));
7698 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7700 rc = req_capsule_server_pack(tsi->tsi_pill);
7702 RETURN(err_serious(rc));
7704 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7705 if (valout == NULL) {
7706 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7707 tgt_name(tsi->tsi_tgt));
7708 RETURN(err_serious(-EFAULT));
7711 if (KEY_IS(KEY_FID2PATH)) {
7712 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7714 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7715 mdt_thread_info_fini(info);
7722 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7724 struct obd_ioctl_data *data = karg;
7727 struct mdt_object *obj;
7728 struct mdt_lock_handle *lh;
7733 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7734 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7737 fid = (struct lu_fid *)data->ioc_inlbuf1;
7739 if (!fid_is_sane(fid))
7742 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7744 lh = &mti->mti_lh[MDT_LH_PARENT];
7745 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE, LCK_CR);
7747 RETURN(PTR_ERR(obj));
7749 if (mdt_object_remote(obj)) {
7752 * before calling version get the correct MDS should be
7753 * fid, this is error to find remote object here
7755 CERROR("nonlocal object "DFID"\n", PFID(fid));
7756 } else if (!mdt_object_exists(obj)) {
7757 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7760 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7761 *(__u64 *)data->ioc_inlbuf2 = version;
7764 mdt_object_unlock_put(mti, obj, lh, 1);
7768 /* ioctls on obd dev */
7769 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7770 void *karg, void __user *uarg)
7772 struct obd_device *obd = exp->exp_obd;
7773 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7774 struct dt_device *dt = mdt->mdt_bottom;
7775 struct obd_ioctl_data *data;
7780 CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
7781 obd->obd_name, cmd, len, karg, uarg);
7783 rc = lu_env_init(&env, LCT_MD_THREAD);
7787 /* handle commands that don't use @karg first */
7790 rc = mdt_device_sync(&env, mdt);
7792 case OBD_IOC_SET_READONLY:
7793 rc = dt_sync(&env, dt);
7795 rc = dt_ro(&env, dt);
7799 if (unlikely(karg == NULL)) {
7800 OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", rc = -EINVAL);
7806 case OBD_IOC_ABORT_RECOVERY: {
7807 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
7808 LCONSOLE_WARN("%s: Aborting MDT recovery\n",
7810 obd->obd_abort_mdt_recovery = 1;
7811 wake_up(&obd->obd_next_transno_waitq);
7812 } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7813 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7814 LCONSOLE_WARN("%s: Aborting client recovery\n",
7816 obd->obd_abort_recovery = 1;
7817 target_stop_recovery_thread(obd);
7822 case OBD_IOC_CHANGELOG_REG:
7823 case OBD_IOC_CHANGELOG_DEREG:
7824 case OBD_IOC_CHANGELOG_CLEAR:
7825 case OBD_IOC_LLOG_PRINT:
7826 case OBD_IOC_LLOG_CANCEL:
7827 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env, mdt->mdt_child,
7830 case OBD_IOC_START_LFSCK: {
7831 struct md_device *next = mdt->mdt_child;
7832 struct lfsck_start_param lsp;
7834 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7835 lsp.lsp_index_valid = 0;
7836 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7839 case OBD_IOC_STOP_LFSCK: {
7840 struct md_device *next = mdt->mdt_child;
7841 struct lfsck_stop stop;
7843 stop.ls_status = LS_STOPPED;
7844 /* Old lfsck utils may pass NULL @stop. */
7845 if (data->ioc_inlbuf1 == NULL)
7849 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7851 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7854 case OBD_IOC_QUERY_LFSCK: {
7855 struct md_device *next = mdt->mdt_child;
7857 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7861 case OBD_IOC_GET_OBJ_VERSION: {
7862 struct mdt_thread_info *mti;
7864 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7865 memset(mti, 0, sizeof(*mti));
7866 mti->mti_env = &env;
7870 rc = mdt_ioc_version_get(mti, karg);
7873 case OBD_IOC_CATLOGLIST: {
7874 struct mdt_thread_info *mti;
7876 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7877 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7878 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7879 &mti->mti_tmp_fid1);
7883 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
7891 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7893 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7898 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7899 struct lfsck_start_param lsp;
7901 lsp.lsp_start = NULL;
7902 lsp.lsp_index_valid = 0;
7903 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7904 OBD_IOC_START_LFSCK,
7906 if (rc != 0 && rc != -EALREADY)
7907 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7908 mdt_obd_name(mdt), rc);
7911 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7915 static int mdt_obd_postrecov(struct obd_device *obd)
7920 rc = lu_env_init(&env, LCT_MD_THREAD);
7923 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7928 static const struct obd_ops mdt_obd_device_ops = {
7929 .o_owner = THIS_MODULE,
7930 .o_set_info_async = mdt_obd_set_info_async,
7931 .o_connect = mdt_obd_connect,
7932 .o_reconnect = mdt_obd_reconnect,
7933 .o_disconnect = mdt_obd_disconnect,
7934 .o_init_export = mdt_init_export,
7935 .o_destroy_export = mdt_destroy_export,
7936 .o_iocontrol = mdt_iocontrol,
7937 .o_postrecov = mdt_obd_postrecov,
7938 /* Data-on-MDT IO methods */
7939 .o_preprw = mdt_obd_preprw,
7940 .o_commitrw = mdt_obd_commitrw,
7943 static struct lu_device *mdt_device_fini(const struct lu_env *env,
7944 struct lu_device *d)
7946 struct mdt_device *m = mdt_dev(d);
7954 static struct lu_device *mdt_device_free(const struct lu_env *env,
7955 struct lu_device *d)
7957 struct mdt_device *m = mdt_dev(d);
7961 lu_device_fini(&m->mdt_lu_dev);
7967 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7968 struct lu_device_type *t,
7969 struct lustre_cfg *cfg)
7971 struct lu_device *l;
7972 struct mdt_device *m;
7979 rc = mdt_init0(env, m, t, cfg);
7981 mdt_device_free(env, l);
7986 l = ERR_PTR(-ENOMEM);
7990 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7991 LU_KEY_INIT(mdt, struct mdt_thread_info);
7993 static void mdt_key_fini(const struct lu_context *ctx,
7994 struct lu_context_key *key, void *data)
7996 struct mdt_thread_info *info = data;
7998 if (info->mti_big_lmm) {
7999 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
8000 info->mti_big_lmm = NULL;
8001 info->mti_big_lmmsize = 0;
8004 if (info->mti_big_acl) {
8005 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
8006 info->mti_big_acl = NULL;
8007 info->mti_big_aclsize = 0;
8013 /* context key: mdt_thread_key */
8014 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
8016 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
8018 return lu_ucred(info->mti_env);
8021 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
8023 return lu_ucred_check(info->mti_env);
8027 * Enable/disable COS (Commit On Sharing).
8029 * Set/Clear the COS flag in mdt options.
8031 * \param mdt mdt device
8032 * \param val 0 disables COS, other values enable COS
8034 void mdt_enable_cos(struct mdt_device *mdt, bool val)
8039 mdt->mdt_opts.mo_cos = val;
8040 rc = lu_env_init(&env, LCT_LOCAL);
8041 if (unlikely(rc != 0)) {
8042 CWARN("%s: lu_env initialization failed, cannot sync: rc = %d\n",
8043 mdt_obd_name(mdt), rc);
8046 mdt_device_sync(&env, mdt);
8051 * Check COS (Commit On Sharing) status.
8053 * Return COS flag status.
8055 * \param mdt mdt device
8057 int mdt_cos_is_enabled(struct mdt_device *mdt)
8059 return mdt->mdt_opts.mo_cos != 0;
8062 static const struct lu_device_type_operations mdt_device_type_ops = {
8063 .ldto_device_alloc = mdt_device_alloc,
8064 .ldto_device_free = mdt_device_free,
8065 .ldto_device_fini = mdt_device_fini
8068 static struct lu_device_type mdt_device_type = {
8069 .ldt_tags = LU_DEVICE_MD,
8070 .ldt_name = LUSTRE_MDT_NAME,
8071 .ldt_ops = &mdt_device_type_ops,
8072 .ldt_ctx_tags = LCT_MD_THREAD
8075 static int __init mdt_init(void)
8079 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
8080 FID_NOBRACE_LEN + 1);
8081 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
8084 rc = libcfs_setup();
8088 rc = lu_kmem_init(mdt_caches);
8092 rc = mds_mod_init();
8096 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
8097 LUSTRE_MDT_NAME, &mdt_device_type);
8102 lu_kmem_fini(mdt_caches);
8109 static void __exit mdt_exit(void)
8111 class_unregister_type(LUSTRE_MDT_NAME);
8113 lu_kmem_fini(mdt_caches);
8116 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8117 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
8118 MODULE_VERSION(LUSTRE_VERSION_STRING);
8119 MODULE_LICENSE("GPL");
8121 module_init(mdt_init);
8122 module_exit(mdt_exit);