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
230 * several users of mlh_pdo_hash assume a non-zero
231 * hash value. We therefore map zero onto an
232 * arbitrary, but consistent value (1) to avoid
233 * 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)
248 * Any dir access needs couple of locks:
250 * 1) on part of dir we gonna take lookup/modify;
252 * 2) on whole dir to protect it from concurrent splitting and/or to
253 * flush client's cache for readdir().
255 * so, for a given mode and object this routine decides what lock mode
256 * to use for lock #2:
258 * 1) if caller's gonna lookup in dir then we need to protect dir from
259 * being splitted only - LCK_CR
261 * 2) if caller's gonna modify dir then we need to protect dir from
262 * being splitted and to flush cache - LCK_CW
264 * 3) if caller's gonna modify dir and that dir seems ready for
265 * splitting then we need to protect it from any type of access
266 * (lookup/modify/split) - LCK_EX --bzzz
269 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
270 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
273 * Ask underlaying level its opinion about preferable PDO lock mode
274 * having access type passed as regular lock mode:
276 * - MDL_MINMODE means that lower layer does not want to specify lock
279 * - MDL_NL means that no PDO lock should be taken. This is used in some
280 * cases. Say, for non-splittable directories no need to use PDO locks
283 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
284 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
286 if (mode != MDL_MINMODE) {
287 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
290 * Lower layer does not want to specify locking mode. We do it
291 * our selves. No special protection is needed, just flush
292 * client's cache on modification and allow concurrent
295 switch (lh->mlh_reg_mode) {
297 lh->mlh_pdo_mode = LCK_EX;
300 lh->mlh_pdo_mode = LCK_CR;
303 lh->mlh_pdo_mode = LCK_CW;
306 CERROR("Not expected lock type (0x%x)\n",
307 (int)lh->mlh_reg_mode);
312 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
317 * Check whether \a o is directory stripe object.
319 * \param[in] info thread environment
320 * \param[in] o MDT object
322 * \retval 1 is directory stripe.
323 * \retval 0 isn't directory stripe.
324 * \retval < 1 error code
326 static int mdt_is_dir_stripe(struct mdt_thread_info *info,
327 struct mdt_object *o)
329 struct md_attr *ma = &info->mti_attr;
330 struct lmv_mds_md_v1 *lmv;
333 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
337 if (!(ma->ma_valid & MA_LMV))
340 lmv = &ma->ma_lmv->lmv_md_v1;
342 if (!lmv_is_sane2(lmv))
345 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE)
351 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
354 struct mdt_device *mdt = info->mti_mdt;
355 struct lu_name *lname = &info->mti_name;
356 const char *start = fileset;
357 char *filename = info->mti_filename;
358 struct mdt_object *obj;
361 LASSERT(!info->mti_cross_ref);
364 * We may want to allow this to mount a completely separate
365 * fileset from the MDT in the future, but keeping it to
366 * ROOT/ only for now avoid potential security issues.
368 *fid = mdt->mdt_md_root_fid;
370 while (rc == 0 && start != NULL && *start != '\0') {
371 const char *s1 = start;
377 while (*s2 != '/' && *s2 != '\0')
385 lname->ln_namelen = s2 - s1;
386 if (lname->ln_namelen > NAME_MAX) {
391 /* reject .. as a path component */
392 if (lname->ln_namelen == 2 && strncmp(s1, "..", 2) == 0) {
397 strncpy(filename, s1, lname->ln_namelen);
398 filename[lname->ln_namelen] = '\0';
399 lname->ln_name = filename;
401 obj = mdt_object_find(info->mti_env, mdt, fid);
406 /* Only got the fid of this obj by name */
408 rc = mdo_lookup(info->mti_env, mdt_object_child(obj), lname,
409 fid, &info->mti_spec);
410 if (!rc && !S_ISDIR(lu_object_attr(&obj->mot_obj)))
412 mdt_object_put(info->mti_env, obj);
418 static int mdt_get_root(struct tgt_session_info *tsi)
420 struct mdt_thread_info *info = tsi2mdt_info(tsi);
421 struct mdt_device *mdt = info->mti_mdt;
422 struct mdt_body *repbody;
423 char *fileset = NULL, *buffer = NULL;
425 struct obd_export *exp = info->mti_exp;
426 char *nodemap_fileset;
430 rc = mdt_check_ucred(info);
432 GOTO(out, rc = err_serious(rc));
434 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
435 GOTO(out, rc = err_serious(-ENOMEM));
437 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
438 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
439 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
441 GOTO(out, rc = err_serious(-EFAULT));
444 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
445 if (nodemap_fileset && nodemap_fileset[0]) {
446 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
448 /* consider fileset from client as a sub-fileset
449 * of the nodemap one */
450 OBD_ALLOC(buffer, PATH_MAX + 1);
452 GOTO(out, rc = err_serious(-ENOMEM));
453 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
454 nodemap_fileset, fileset) >= PATH_MAX + 1)
455 GOTO(out, rc = err_serious(-EINVAL));
458 /* enforce fileset as specified in the nodemap */
459 fileset = nodemap_fileset;
464 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
465 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
467 GOTO(out, rc = err_serious(rc));
469 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
471 exp->exp_root_fid = repbody->mbo_fid1;
472 repbody->mbo_valid |= OBD_MD_FLID;
476 mdt_thread_info_fini(info);
478 OBD_FREE(buffer, PATH_MAX+1);
482 static int mdt_statfs(struct tgt_session_info *tsi)
484 struct ptlrpc_request *req = tgt_ses_req(tsi);
485 struct mdt_thread_info *info = tsi2mdt_info(tsi);
486 struct mdt_device *mdt = info->mti_mdt;
487 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
488 struct md_device *next = mdt->mdt_child;
489 struct ptlrpc_service_part *svcpt;
490 struct obd_statfs *osfs;
491 struct mdt_body *reqbody = NULL;
492 struct mdt_statfs_cache *msf;
493 ktime_t kstart = ktime_get();
494 int current_blockbits;
499 svcpt = req->rq_rqbd->rqbd_svcpt;
501 /* This will trigger a watchdog timeout */
502 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
503 (MDT_SERVICE_WATCHDOG_FACTOR *
504 at_get(&svcpt->scp_at_estimate)) + 1);
506 rc = mdt_check_ucred(info);
508 GOTO(out, rc = err_serious(rc));
510 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
511 GOTO(out, rc = err_serious(-ENOMEM));
513 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
515 GOTO(out, rc = -EPROTO);
517 if (mdt_is_sum_statfs_client(req->rq_export) &&
518 lustre_packed_msg_size(req->rq_reqmsg) ==
519 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
520 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
521 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
522 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
525 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
526 msf = &mdt->mdt_sum_osfs;
528 msf = &mdt->mdt_osfs;
530 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
531 /** statfs data is too old, get up-to-date one */
532 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
533 rc = next->md_ops->mdo_statfs(info->mti_env,
536 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
540 spin_lock(&mdt->mdt_lock);
541 msf->msf_osfs = *osfs;
542 msf->msf_age = ktime_get_seconds();
543 spin_unlock(&mdt->mdt_lock);
545 /** use cached statfs data */
546 spin_lock(&mdt->mdt_lock);
547 *osfs = msf->msf_osfs;
548 spin_unlock(&mdt->mdt_lock);
551 /* tgd_blockbit is recordsize bits set during mkfs.
552 * This once set does not change. However, 'zfs set'
553 * can be used to change the MDT blocksize. Instead
554 * of using cached value of 'tgd_blockbit' always
555 * calculate the blocksize bits which may have
558 current_blockbits = fls64(osfs->os_bsize) - 1;
560 /* at least try to account for cached pages. its still racy and
561 * might be under-reporting if clients haven't announced their
562 * caches with brw recently */
563 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
564 " pending %llu free %llu avail %llu\n",
565 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
566 tgd->tgd_tot_pending,
567 osfs->os_bfree << current_blockbits,
568 osfs->os_bavail << current_blockbits);
570 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
571 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
572 osfs->os_bsize - 1) >> current_blockbits));
574 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
575 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
576 "%llu objects: %llu free; state %x\n",
577 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
578 osfs->os_files, osfs->os_ffree, osfs->os_state);
580 if (!exp_grant_param_supp(tsi->tsi_exp) &&
581 current_blockbits > COMPAT_BSIZE_SHIFT) {
582 /* clients which don't support OBD_CONNECT_GRANT_PARAM
583 * should not see a block size > page size, otherwise
584 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
585 * block size which is the biggest block size known to work
586 * with all client's page size. */
587 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
588 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
589 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
590 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
593 mdt_counter_incr(req, LPROC_MDT_STATFS,
594 ktime_us_delta(ktime_get(), kstart));
596 mdt_thread_info_fini(info);
600 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
602 struct lov_comp_md_v1 *comp_v1;
603 struct lov_mds_md *v1;
605 __u32 dom_stripesize = 0;
607 bool has_ost_stripes = false;
614 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
617 comp_v1 = (struct lov_comp_md_v1 *)lmm;
618 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
619 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
621 /* Fast check for DoM entry with no mirroring, should be the first */
622 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
623 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
626 /* check all entries otherwise */
627 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
628 struct lov_comp_md_entry_v1 *lcme;
630 lcme = &comp_v1->lcm_entries[i];
631 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
634 off = le32_to_cpu(lcme->lcme_offset);
635 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
637 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
639 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
641 has_ost_stripes = true;
643 if (dom_stripesize && has_ost_stripes)
644 RETURN(dom_stripesize);
646 /* DoM-only case exits here */
647 if (is_dom_only && dom_stripesize)
649 RETURN(dom_stripesize);
653 * Pack size attributes into the reply.
655 int mdt_pack_size2body(struct mdt_thread_info *info,
656 const struct lu_fid *fid, struct lustre_handle *lh)
659 struct md_attr *ma = &info->mti_attr;
661 bool dom_lock = false;
665 LASSERT(ma->ma_attr.la_valid & LA_MODE);
667 if (!S_ISREG(ma->ma_attr.la_mode) ||
668 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
671 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
672 /* no DoM stripe, no size in reply */
676 if (lustre_handle_is_used(lh)) {
677 struct ldlm_lock *lock;
679 lock = ldlm_handle2lock(lh);
681 dom_lock = ldlm_has_dom(lock);
686 /* no DoM lock, no size in reply */
690 /* Either DoM lock exists or LMM has only DoM stripe then
691 * return size on body. */
692 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
694 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
698 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
700 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
702 * \param info thread info object
703 * \param repbody reply to pack ACLs into
704 * \param o mdt object of file to examine
705 * \param nodemap nodemap of client to reply to
707 * \retval -errno error getting or parsing ACL from disk
709 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
710 struct mdt_object *o, struct lu_nodemap *nodemap)
712 const struct lu_env *env = info->mti_env;
713 struct md_object *next = mdt_object_child(o);
714 struct lu_buf *buf = &info->mti_buf;
715 struct mdt_device *mdt = info->mti_mdt;
716 struct req_capsule *pill = info->mti_pill;
721 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
722 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
723 if (buf->lb_len == 0)
726 LASSERT(!info->mti_big_acl_used);
728 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
730 if (rc == -ENODATA) {
731 repbody->mbo_aclsize = 0;
732 repbody->mbo_valid |= OBD_MD_FLACL;
734 } else if (rc == -EOPNOTSUPP) {
736 } else if (rc == -ERANGE) {
737 if (exp_connect_large_acl(info->mti_exp) &&
738 !info->mti_big_acl_used) {
739 if (info->mti_big_acl == NULL) {
740 info->mti_big_aclsize =
742 mdt->mdt_max_ea_size,
744 OBD_ALLOC_LARGE(info->mti_big_acl,
745 info->mti_big_aclsize);
746 if (info->mti_big_acl == NULL) {
747 info->mti_big_aclsize = 0;
748 CERROR("%s: unable to grow "
751 PFID(mdt_object_fid(o)));
756 CDEBUG(D_INODE, "%s: grow the "DFID
757 " ACL buffer to size %d\n",
759 PFID(mdt_object_fid(o)),
760 info->mti_big_aclsize);
762 buf->lb_buf = info->mti_big_acl;
763 buf->lb_len = info->mti_big_aclsize;
764 info->mti_big_acl_used = 1;
767 /* FS has ACL bigger that our limits */
768 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
769 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
770 info->mti_big_aclsize);
773 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
774 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
777 rc = nodemap_map_acl(nodemap, buf->lb_buf,
778 rc, NODEMAP_FS_TO_CLIENT);
779 /* if all ACLs mapped out, rc is still >= 0 */
781 CERROR("%s: nodemap_map_acl unable to parse "DFID
782 " ACL: rc = %d\n", mdt_obd_name(mdt),
783 PFID(mdt_object_fid(o)), rc);
784 repbody->mbo_aclsize = 0;
785 repbody->mbo_valid &= ~OBD_MD_FLACL;
787 repbody->mbo_aclsize = rc;
788 repbody->mbo_valid |= OBD_MD_FLACL;
797 /* XXX Look into layout in MDT layer. */
798 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
800 struct lov_comp_md_v1 *comp_v1;
801 struct lov_mds_md *v1;
804 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
805 comp_v1 = (struct lov_comp_md_v1 *)lmm;
807 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
808 v1 = (struct lov_mds_md *)((char *)comp_v1 +
809 comp_v1->lcm_entries[i].lcme_offset);
810 /* We don't support partial release for now */
811 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
816 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
821 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
822 const struct lu_attr *attr, const struct lu_fid *fid)
824 struct mdt_device *mdt = info->mti_mdt;
825 struct obd_export *exp = info->mti_exp;
826 struct md_attr *ma = &info->mti_attr;
827 struct lu_nodemap *nodemap = NULL;
829 LASSERT(ma->ma_valid & MA_INODE);
831 if (attr->la_valid & LA_ATIME) {
832 b->mbo_atime = attr->la_atime;
833 b->mbo_valid |= OBD_MD_FLATIME;
835 if (attr->la_valid & LA_MTIME) {
836 b->mbo_mtime = attr->la_mtime;
837 b->mbo_valid |= OBD_MD_FLMTIME;
839 if (attr->la_valid & LA_CTIME) {
840 b->mbo_ctime = attr->la_ctime;
841 b->mbo_valid |= OBD_MD_FLCTIME;
843 if (attr->la_valid & LA_BTIME) {
844 b->mbo_btime = attr->la_btime;
845 b->mbo_valid |= OBD_MD_FLBTIME;
847 if (attr->la_valid & LA_FLAGS) {
848 b->mbo_flags = attr->la_flags;
849 b->mbo_valid |= OBD_MD_FLFLAGS;
851 if (attr->la_valid & LA_NLINK) {
852 b->mbo_nlink = attr->la_nlink;
853 b->mbo_valid |= OBD_MD_FLNLINK;
855 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
856 nodemap = nodemap_get_from_exp(exp);
860 if (attr->la_valid & LA_UID) {
861 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
862 NODEMAP_FS_TO_CLIENT,
864 b->mbo_valid |= OBD_MD_FLUID;
866 if (attr->la_valid & LA_GID) {
867 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
868 NODEMAP_FS_TO_CLIENT,
870 b->mbo_valid |= OBD_MD_FLGID;
873 if (attr->la_valid & LA_PROJID) {
874 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
875 NODEMAP_FS_TO_CLIENT,
877 b->mbo_valid |= OBD_MD_FLPROJID;
880 b->mbo_mode = attr->la_mode;
881 if (attr->la_valid & LA_MODE)
882 b->mbo_valid |= OBD_MD_FLMODE;
883 if (attr->la_valid & LA_TYPE)
884 b->mbo_valid |= OBD_MD_FLTYPE;
888 b->mbo_valid |= OBD_MD_FLID;
889 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
890 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
893 if (!(attr->la_valid & LA_TYPE))
896 b->mbo_rdev = attr->la_rdev;
897 b->mbo_size = attr->la_size;
898 b->mbo_blocks = attr->la_blocks;
900 if (!S_ISREG(attr->la_mode)) {
901 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
902 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
903 /* means no objects are allocated on osts. */
904 LASSERT(!(ma->ma_valid & MA_LOV));
905 /* just ignore blocks occupied by extend attributes on MDS */
907 /* if no object is allocated on osts, the size on mds is valid.
909 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
910 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
911 if (mdt_hsm_is_released(ma->ma_lmm)) {
912 /* A released file stores its size on MDS. */
913 /* But return 1 block for released file, unless tools
914 * like tar will consider it fully sparse. (LU-3864)
916 if (unlikely(b->mbo_size == 0))
920 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
921 } else if (info->mti_som_strict &&
922 mdt->mdt_opts.mo_enable_strict_som) {
923 /* use SOM for size*/
924 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
925 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
926 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
927 b->mbo_size = ma->ma_som.ms_size;
928 b->mbo_blocks = ma->ma_som.ms_blocks;
932 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
933 b->mbo_valid & OBD_MD_FLLAZYSIZE))
934 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
935 PFID(fid), (unsigned long long)b->mbo_size);
938 if (!IS_ERR_OR_NULL(nodemap))
939 nodemap_putref(nodemap);
942 static inline int mdt_body_has_lov(const struct lu_attr *la,
943 const struct mdt_body *body)
945 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
946 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
949 void mdt_client_compatibility(struct mdt_thread_info *info)
951 struct mdt_body *body;
952 struct ptlrpc_request *req = mdt_info_req(info);
953 struct obd_export *exp = req->rq_export;
954 struct md_attr *ma = &info->mti_attr;
955 struct lu_attr *la = &ma->ma_attr;
958 if (exp_connect_layout(exp))
959 /* the client can deal with 16-bit lmm_stripe_count */
962 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
964 if (!mdt_body_has_lov(la, body))
967 /* now we have a reply with a lov for a client not compatible with the
968 * layout lock so we have to clean the layout generation number */
969 if (S_ISREG(la->la_mode))
970 ma->ma_lmm->lmm_layout_gen = 0;
974 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
975 struct mdt_object *o)
977 const struct lu_env *env = info->mti_env;
980 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
989 /* Is it a directory? Let's check for the LMV as well */
990 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
991 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
995 rc2 = mo_xattr_get(env, mdt_object_child(o),
997 XATTR_NAME_DEFAULT_LMV);
999 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
1007 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
1010 const struct lu_env *env = info->mti_env;
1014 LASSERT(info->mti_big_lmm_used == 0);
1015 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
1019 /* big_lmm may need to be grown */
1020 if (info->mti_big_lmmsize < rc) {
1021 int size = size_roundup_power2(rc);
1023 if (info->mti_big_lmmsize > 0) {
1024 /* free old buffer */
1025 LASSERT(info->mti_big_lmm);
1026 OBD_FREE_LARGE(info->mti_big_lmm,
1027 info->mti_big_lmmsize);
1028 info->mti_big_lmm = NULL;
1029 info->mti_big_lmmsize = 0;
1032 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1033 if (info->mti_big_lmm == NULL)
1035 info->mti_big_lmmsize = size;
1037 LASSERT(info->mti_big_lmmsize >= rc);
1039 info->mti_buf.lb_buf = info->mti_big_lmm;
1040 info->mti_buf.lb_len = info->mti_big_lmmsize;
1041 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1046 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1047 struct md_attr *ma, const char *name)
1049 struct md_object *next = mdt_object_child(o);
1050 struct lu_buf *buf = &info->mti_buf;
1053 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1054 buf->lb_buf = ma->ma_lmm;
1055 buf->lb_len = ma->ma_lmm_size;
1056 LASSERT(!(ma->ma_valid & MA_LOV));
1057 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1058 buf->lb_buf = ma->ma_lmv;
1059 buf->lb_len = ma->ma_lmv_size;
1060 LASSERT(!(ma->ma_valid & MA_LMV));
1061 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1062 buf->lb_buf = ma->ma_default_lmv;
1063 buf->lb_len = ma->ma_default_lmv_size;
1064 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1069 LASSERT(buf->lb_buf);
1071 if (!mdt_object_exists(o))
1074 if (mdt_object_remote(o) && S_ISDIR(lu_object_attr(&o->mot_obj)))
1075 /* force reload layout for remote dir in case layout changed */
1076 mo_invalidate(info->mti_env, mdt_object_child(o));
1078 rc = mo_xattr_get(info->mti_env, next, buf, name);
1082 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1083 if (info->mti_big_lmm_used)
1084 ma->ma_lmm = info->mti_big_lmm;
1086 /* NOT return LOV EA with hole to old client. */
1087 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1088 LOV_PATTERN_F_HOLE) &&
1089 !(exp_connect_flags(info->mti_exp) &
1090 OBD_CONNECT_LFSCK)) {
1093 ma->ma_lmm_size = rc;
1094 ma->ma_valid |= MA_LOV;
1096 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1097 if (info->mti_big_lmm_used)
1098 ma->ma_lmv = info->mti_big_lmm;
1100 ma->ma_lmv_size = rc;
1101 ma->ma_valid |= MA_LMV;
1102 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1103 ma->ma_default_lmv_size = rc;
1104 ma->ma_valid |= MA_LMV_DEF;
1107 /* Update mdt_max_mdsize so all clients will be aware that */
1108 if (info->mti_mdt->mdt_max_mdsize < rc)
1109 info->mti_mdt->mdt_max_mdsize = rc;
1112 } else if (rc == -ENODATA) {
1115 } else if (rc == -ERANGE) {
1116 /* Default LMV has fixed size, so it must be able to fit
1117 * in the original buffer */
1118 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1120 rc = mdt_big_xattr_get(info, o, name);
1122 info->mti_big_lmm_used = 1;
1130 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1131 struct md_attr *ma, const char *name)
1135 if (!info->mti_big_lmm) {
1136 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1137 if (!info->mti_big_lmm)
1139 info->mti_big_lmmsize = PAGE_SIZE;
1142 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1143 ma->ma_lmm = info->mti_big_lmm;
1144 ma->ma_lmm_size = info->mti_big_lmmsize;
1145 ma->ma_valid &= ~MA_LOV;
1146 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1147 ma->ma_lmv = info->mti_big_lmm;
1148 ma->ma_lmv_size = info->mti_big_lmmsize;
1149 ma->ma_valid &= ~MA_LMV;
1154 LASSERT(!info->mti_big_lmm_used);
1155 rc = __mdt_stripe_get(info, o, ma, name);
1156 /* since big_lmm is always used here, clear 'used' flag to avoid
1157 * assertion in mdt_big_xattr_get().
1159 info->mti_big_lmm_used = 0;
1164 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1165 struct lu_fid *pfid)
1167 struct lu_buf *buf = &info->mti_buf;
1168 struct link_ea_header *leh;
1169 struct link_ea_entry *lee;
1173 buf->lb_buf = info->mti_big_lmm;
1174 buf->lb_len = info->mti_big_lmmsize;
1175 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1176 buf, XATTR_NAME_LINK);
1177 /* ignore errors, MA_PFID won't be set and it is
1178 * up to the caller to treat this as an error */
1179 if (rc == -ERANGE || buf->lb_len == 0) {
1180 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1181 buf->lb_buf = info->mti_big_lmm;
1182 buf->lb_len = info->mti_big_lmmsize;
1187 if (rc < sizeof(*leh)) {
1188 CERROR("short LinkEA on "DFID": rc = %d\n",
1189 PFID(mdt_object_fid(o)), rc);
1193 leh = (struct link_ea_header *) buf->lb_buf;
1194 lee = (struct link_ea_entry *)(leh + 1);
1195 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1196 leh->leh_magic = LINK_EA_MAGIC;
1197 leh->leh_reccount = __swab32(leh->leh_reccount);
1198 leh->leh_len = __swab64(leh->leh_len);
1200 if (leh->leh_magic != LINK_EA_MAGIC)
1202 if (leh->leh_reccount == 0)
1205 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1206 fid_be_to_cpu(pfid, pfid);
1211 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1212 struct lu_fid *pfid, struct lu_name *lname)
1214 struct lu_buf *buf = &info->mti_buf;
1215 struct link_ea_header *leh;
1216 struct link_ea_entry *lee;
1220 buf->lb_buf = info->mti_xattr_buf;
1221 buf->lb_len = sizeof(info->mti_xattr_buf);
1222 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1224 if (rc == -ERANGE) {
1225 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1226 buf->lb_buf = info->mti_big_lmm;
1227 buf->lb_len = info->mti_big_lmmsize;
1232 if (rc < sizeof(*leh)) {
1233 CERROR("short LinkEA on "DFID": rc = %d\n",
1234 PFID(mdt_object_fid(o)), rc);
1238 leh = (struct link_ea_header *)buf->lb_buf;
1239 lee = (struct link_ea_entry *)(leh + 1);
1240 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1241 leh->leh_magic = LINK_EA_MAGIC;
1242 leh->leh_reccount = __swab32(leh->leh_reccount);
1243 leh->leh_len = __swab64(leh->leh_len);
1245 if (leh->leh_magic != LINK_EA_MAGIC)
1248 if (leh->leh_reccount == 0)
1251 linkea_entry_unpack(lee, &reclen, lname, pfid);
1256 int mdt_attr_get_complex(struct mdt_thread_info *info,
1257 struct mdt_object *o, struct md_attr *ma)
1259 const struct lu_env *env = info->mti_env;
1260 struct md_object *next = mdt_object_child(o);
1261 struct lu_buf *buf = &info->mti_buf;
1262 int need = ma->ma_need;
1269 if (mdt_object_exists(o) == 0)
1270 GOTO(out, rc = -ENOENT);
1271 mode = lu_object_attr(&next->mo_lu);
1273 if (need & MA_INODE) {
1274 ma->ma_need = MA_INODE;
1275 rc = mo_attr_get(env, next, ma);
1280 (void) mdt_get_som(info, o, ma);
1281 ma->ma_valid |= MA_INODE;
1284 if (need & MA_PFID) {
1285 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1287 ma->ma_valid |= MA_PFID;
1288 /* ignore this error, parent fid is not mandatory */
1292 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1293 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1298 if (need & MA_LMV && S_ISDIR(mode)) {
1299 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1304 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1305 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1311 * In the handle of MA_INODE, we may already get the SOM attr.
1313 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1314 rc = mdt_get_som(info, o, ma);
1319 if (need & MA_HSM && S_ISREG(mode)) {
1320 buf->lb_buf = info->mti_xattr_buf;
1321 buf->lb_len = sizeof(info->mti_xattr_buf);
1322 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1323 sizeof(info->mti_xattr_buf));
1324 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1325 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1327 ma->ma_valid |= MA_HSM;
1328 else if (rc2 < 0 && rc2 != -ENODATA)
1329 GOTO(out, rc = rc2);
1332 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1333 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1334 buf->lb_buf = ma->ma_acl;
1335 buf->lb_len = ma->ma_acl_size;
1336 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1338 ma->ma_acl_size = rc2;
1339 ma->ma_valid |= MA_ACL_DEF;
1340 } else if (rc2 == -ENODATA) {
1342 ma->ma_acl_size = 0;
1344 GOTO(out, rc = rc2);
1349 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1350 rc, ma->ma_valid, ma->ma_lmm);
1354 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
1356 struct req_capsule *pill = info->mti_pill;
1359 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
1361 /* pre-set size in server part with max size */
1362 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
1364 info->mti_mdt->mdt_max_mdsize);
1368 static int mdt_getattr_internal(struct mdt_thread_info *info,
1369 struct mdt_object *o, int ma_need)
1371 struct mdt_device *mdt = info->mti_mdt;
1372 struct md_object *next = mdt_object_child(o);
1373 const struct mdt_body *reqbody = info->mti_body;
1374 struct ptlrpc_request *req = mdt_info_req(info);
1375 struct md_attr *ma = &info->mti_attr;
1376 struct lu_attr *la = &ma->ma_attr;
1377 struct req_capsule *pill = info->mti_pill;
1378 const struct lu_env *env = info->mti_env;
1379 struct mdt_body *repbody;
1380 struct lu_buf *buffer = &info->mti_buf;
1381 struct obd_export *exp = info->mti_exp;
1382 ktime_t kstart = ktime_get();
1387 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1388 RETURN(err_serious(-ENOMEM));
1390 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1394 if (mdt_object_remote(o)) {
1395 /* This object is located on remote node.*/
1396 /* Return -ENOTSUPP for old client */
1397 if (!mdt_is_dne_client(req->rq_export))
1398 GOTO(out, rc = -ENOTSUPP);
1400 repbody->mbo_fid1 = *mdt_object_fid(o);
1401 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1405 if (reqbody->mbo_eadatasize > 0) {
1406 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1407 if (buffer->lb_buf == NULL)
1408 GOTO(out, rc = -EPROTO);
1409 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1412 buffer->lb_buf = NULL;
1414 ma_need &= ~(MA_LOV | MA_LMV);
1415 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1416 mdt_obd_name(info->mti_mdt),
1417 req->rq_export->exp_client_uuid.uuid);
1420 /* from 2.12.58 intent_getattr pack default LMV in reply */
1421 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1422 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1423 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1424 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1426 ma->ma_lmv = buffer->lb_buf;
1427 ma->ma_lmv_size = buffer->lb_len;
1428 ma->ma_default_lmv = req_capsule_server_get(pill,
1429 &RMF_DEFAULT_MDT_MD);
1430 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1431 &RMF_DEFAULT_MDT_MD,
1433 ma->ma_need = MA_INODE;
1434 if (ma->ma_lmv_size > 0)
1435 ma->ma_need |= MA_LMV;
1436 if (ma->ma_default_lmv_size > 0)
1437 ma->ma_need |= MA_LMV_DEF;
1438 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1439 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1440 /* If it is dir and client require MEA, then we got MEA */
1441 /* Assumption: MDT_MD size is enough for lmv size. */
1442 ma->ma_lmv = buffer->lb_buf;
1443 ma->ma_lmv_size = buffer->lb_len;
1444 ma->ma_need = MA_INODE;
1445 if (ma->ma_lmv_size > 0) {
1446 if (reqbody->mbo_valid & OBD_MD_MEA) {
1447 ma->ma_need |= MA_LMV;
1448 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1449 ma->ma_need |= MA_LMV_DEF;
1450 ma->ma_default_lmv = buffer->lb_buf;
1452 ma->ma_default_lmv_size = buffer->lb_len;
1453 ma->ma_lmv_size = 0;
1457 ma->ma_lmm = buffer->lb_buf;
1458 ma->ma_lmm_size = buffer->lb_len;
1459 ma->ma_need = MA_INODE | MA_HSM;
1460 if (ma->ma_lmm_size > 0) {
1461 ma->ma_need |= MA_LOV;
1462 /* Older clients may crash if they getattr overstriped
1465 if (!exp_connect_overstriping(exp) &&
1466 mdt_lmm_is_overstriping(ma->ma_lmm))
1467 RETURN(-EOPNOTSUPP);
1471 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1472 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1473 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1474 /* get default stripe info for this dir. */
1475 ma->ma_need |= MA_LOV_DEF;
1477 ma->ma_need |= ma_need;
1479 rc = mdt_attr_get_complex(info, o, ma);
1481 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1482 "%s: getattr error for "DFID": rc = %d\n",
1483 mdt_obd_name(info->mti_mdt),
1484 PFID(mdt_object_fid(o)), rc);
1488 /* return immutable attr on fscrypt metadata files
1489 * if fscrypt admin is not permitted
1491 if (o->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD &&
1492 !mdt_ucred(info)->uc_rbac_fscrypt_admin)
1493 la->la_flags |= LUSTRE_IMMUTABLE_FL;
1495 /* if file is released, check if a restore is running */
1496 if (ma->ma_valid & MA_HSM) {
1497 repbody->mbo_valid |= OBD_MD_TSTATE;
1498 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1499 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1500 repbody->mbo_t_state = MS_RESTORE;
1503 if (unlikely(!(ma->ma_valid & MA_INODE)))
1506 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1508 if (mdt_body_has_lov(la, reqbody)) {
1509 u32 stripe_count = 1;
1510 bool fixed_layout = false;
1512 if (ma->ma_valid & MA_LOV) {
1513 LASSERT(ma->ma_lmm_size);
1514 repbody->mbo_eadatasize = ma->ma_lmm_size;
1515 if (S_ISDIR(la->la_mode))
1516 repbody->mbo_valid |= OBD_MD_FLDIREA;
1518 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1519 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1521 if (ma->ma_valid & MA_LMV) {
1522 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1523 u32 magic = le32_to_cpu(lmv->lmv_magic);
1525 /* Return -ENOTSUPP for old client */
1526 if (!mdt_is_striped_client(req->rq_export))
1529 LASSERT(S_ISDIR(la->la_mode));
1530 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1531 repbody->mbo_eadatasize = ma->ma_lmv_size;
1532 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1534 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1535 fixed_layout = lmv_is_fixed(lmv);
1536 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1537 mdt_restripe_migrate_add(info, o);
1538 else if (magic == LMV_MAGIC_V1 &&
1539 lmv_is_restriping(lmv))
1540 mdt_restripe_update_add(info, o);
1542 if (ma->ma_valid & MA_LMV_DEF) {
1543 /* Return -ENOTSUPP for old client */
1544 if (!mdt_is_striped_client(req->rq_export))
1546 LASSERT(S_ISDIR(la->la_mode));
1548 * when ll_dir_getstripe() gets default LMV, it
1549 * checks mbo_eadatasize.
1551 if (!(ma->ma_valid & MA_LMV))
1552 repbody->mbo_eadatasize =
1553 ma->ma_default_lmv_size;
1554 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1555 OBD_MD_DEFAULT_MEA);
1558 "dirent count %llu stripe count %u MDT count %d\n",
1559 ma->ma_attr.la_dirent_count, stripe_count,
1560 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1561 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1562 ma->ma_attr.la_dirent_count >
1563 mdt->mdt_restriper.mdr_dir_split_count &&
1564 !fid_is_root(mdt_object_fid(o)) &&
1565 mdt->mdt_enable_dir_auto_split &&
1566 !o->mot_restriping &&
1567 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1569 mdt_auto_split_add(info, o);
1570 } else if (S_ISLNK(la->la_mode) &&
1571 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1572 buffer->lb_buf = ma->ma_lmm;
1573 /* eadatasize from client includes NULL-terminator, so
1574 * there is no need to read it */
1575 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1576 rc = mo_readlink(env, next, buffer);
1577 if (unlikely(rc <= 0)) {
1578 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1579 mdt_obd_name(info->mti_mdt),
1580 PFID(mdt_object_fid(o)), rc);
1583 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1585 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1587 repbody->mbo_valid |= OBD_MD_LINKNAME;
1588 /* we need to report back size with NULL-terminator
1589 * because client expects that */
1590 repbody->mbo_eadatasize = rc + 1;
1591 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1592 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1593 "on "DFID ", expected %d\n",
1594 mdt_obd_name(info->mti_mdt),
1595 rc, PFID(mdt_object_fid(o)),
1596 reqbody->mbo_eadatasize - 1);
1597 /* NULL terminate */
1598 ((char *)ma->ma_lmm)[rc] = 0;
1600 /* If the total CDEBUG() size is larger than a page, it
1601 * will print a warning to the console, avoid this by
1602 * printing just the last part of the symlink. */
1603 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1604 print_limit < rc ? "..." : "", print_limit,
1605 (char *)ma->ma_lmm + rc - print_limit, rc);
1610 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1611 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1612 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1613 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1614 repbody->mbo_max_mdsize);
1617 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1618 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1619 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1620 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1621 if (IS_ERR(nodemap))
1622 RETURN(PTR_ERR(nodemap));
1624 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1625 nodemap_putref(nodemap);
1631 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1632 ktime_us_delta(ktime_get(), kstart));
1637 static int mdt_getattr(struct tgt_session_info *tsi)
1639 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1640 struct mdt_object *obj = info->mti_object;
1641 struct req_capsule *pill = info->mti_pill;
1642 struct mdt_body *reqbody;
1643 struct mdt_body *repbody;
1647 if (unlikely(info->mti_object == NULL))
1650 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1652 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1654 /* Special case for Data-on-MDT files to get data version */
1655 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1656 rc = mdt_data_version_get(tsi);
1660 /* Unlike intent case where we need to pre-fill out buffers early on
1661 * in intent policy for ldlm reasons, here we can have a much better
1662 * guess at EA size by just reading it from disk.
1663 * Exceptions are readdir and (missing) directory striping */
1665 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1666 /* No easy way to know how long is the symlink, but it cannot
1667 * be more than PATH_MAX, so we allocate +1 */
1669 /* A special case for fs ROOT: getattr there might fetch
1670 * default EA for entire fs, not just for this dir!
1672 } else if (lu_fid_eq(mdt_object_fid(obj),
1673 &info->mti_mdt->mdt_md_root_fid) &&
1674 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1675 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1677 /* Should the default strping be bigger, mdt_fix_reply
1678 * will reallocate */
1679 rc = DEF_REP_MD_SIZE;
1681 /* Read the actual EA size from disk */
1682 rc = mdt_attr_get_eabuf_size(info, obj);
1686 GOTO(out, rc = err_serious(rc));
1688 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1690 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1691 * by default. If the target object has more ACL entries, then
1692 * enlarge the buffer when necessary. */
1693 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1694 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1695 mdt_preset_encctx_size(info);
1697 rc = req_capsule_server_pack(pill);
1698 if (unlikely(rc != 0))
1699 GOTO(out, rc = err_serious(rc));
1701 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1702 LASSERT(repbody != NULL);
1703 repbody->mbo_eadatasize = 0;
1704 repbody->mbo_aclsize = 0;
1706 rc = mdt_check_ucred(info);
1708 GOTO(out_shrink, rc);
1710 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1712 rc = mdt_init_ucred(info, reqbody);
1714 GOTO(out_shrink, rc);
1716 rc = mdt_getattr_internal(info, obj, 0);
1718 GOTO(out_ucred, rc);
1720 rc = mdt_pack_encctx_in_reply(info, obj);
1723 mdt_exit_ucred(info);
1725 mdt_client_compatibility(info);
1726 rc2 = mdt_fix_reply(info);
1730 mdt_thread_info_fini(info);
1735 * Handler of layout intent RPC requiring the layout modification
1737 * \param[in] info thread environment
1738 * \param[in] obj object
1739 * \param[out] lhc object ldlm lock handle
1740 * \param[in] layout layout change descriptor
1742 * \retval 0 on success
1743 * \retval < 0 error code
1745 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1746 struct mdt_lock_handle *lhc,
1747 struct md_layout_change *layout)
1753 if (!mdt_object_exists(obj))
1756 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1759 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1764 rc = mdt_check_resent_lock(info, obj, lhc);
1770 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1772 /* take layout lock to prepare layout change */
1773 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1774 lockpart |= MDS_INODELOCK_UPDATE;
1776 rc = mdt_object_lock(info, obj, lhc, lockpart, LCK_EX, false);
1781 mutex_lock(&obj->mot_som_mutex);
1782 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1783 mutex_unlock(&obj->mot_som_mutex);
1786 mdt_object_unlock(info, obj, lhc, 1);
1792 * Exchange MOF_LOV_CREATED flags between two objects after a
1793 * layout swap. No assumption is made on whether o1 or o2 have
1794 * created objects or not.
1796 * \param[in,out] o1 First swap layout object
1797 * \param[in,out] o2 Second swap layout object
1799 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1801 unsigned int o1_lov_created = o1->mot_lov_created;
1803 mutex_lock(&o1->mot_lov_mutex);
1804 mutex_lock(&o2->mot_lov_mutex);
1806 o1->mot_lov_created = o2->mot_lov_created;
1807 o2->mot_lov_created = o1_lov_created;
1809 mutex_unlock(&o2->mot_lov_mutex);
1810 mutex_unlock(&o1->mot_lov_mutex);
1813 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1815 struct mdt_thread_info *info;
1816 struct ptlrpc_request *req = tgt_ses_req(tsi);
1817 struct obd_export *exp = req->rq_export;
1818 struct mdt_object *o1, *o2, *o;
1819 struct mdt_lock_handle *lh1, *lh2;
1820 struct mdc_swap_layouts *msl;
1824 /* client does not support layout lock, so layout swaping
1826 * FIXME: there is a problem for old clients which don't support
1827 * layout lock yet. If those clients have already opened the file
1828 * they won't be notified at all so that old layout may still be
1829 * used to do IO. This can be fixed after file release is landed by
1830 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1831 if (!exp_connect_layout(exp))
1832 RETURN(-EOPNOTSUPP);
1834 info = tsi2mdt_info(tsi);
1835 if (unlikely(info->mti_object == NULL))
1838 if (info->mti_dlm_req != NULL)
1839 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1841 o1 = info->mti_object;
1842 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1843 &info->mti_body->mbo_fid2);
1845 GOTO(out, rc = PTR_ERR(o));
1847 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1848 GOTO(put, rc = -ENOENT);
1850 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1851 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1857 /* permission check. Make sure the calling process having permission
1858 * to write both files. */
1859 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1864 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1869 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1871 GOTO(put, rc = -EPROTO);
1873 lh1 = &info->mti_lh[MDT_LH_NEW];
1874 lh2 = &info->mti_lh[MDT_LH_OLD];
1875 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1876 MDS_INODELOCK_XATTR, LCK_EX, false);
1880 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1881 MDS_INODELOCK_XATTR, LCK_EX, false);
1885 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1886 mdt_object_child(o2), msl->msl_flags);
1890 mdt_swap_lov_flag(o1, o2);
1893 mdt_object_unlock(info, o2, lh2, rc);
1895 mdt_object_unlock(info, o1, lh1, rc);
1897 mdt_object_put(info->mti_env, o);
1899 mdt_thread_info_fini(info);
1903 static int mdt_raw_lookup(struct mdt_thread_info *info,
1904 struct mdt_object *parent,
1905 const struct lu_name *lname)
1907 struct lu_fid *fid = &info->mti_tmp_fid1;
1908 struct mdt_body *repbody;
1909 bool is_dotdot = false;
1910 bool is_old_parent_stripe = false;
1911 bool is_new_parent_checked = false;
1916 LASSERT(!info->mti_cross_ref);
1917 /* Always allow to lookup ".." */
1918 if (lname->ln_namelen == 2 &&
1919 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1920 info->mti_spec.sp_permitted = 1;
1922 if (mdt_is_dir_stripe(info, parent) == 1)
1923 is_old_parent_stripe = true;
1926 mdt_object_get(info->mti_env, parent);
1928 /* Only got the fid of this obj by name */
1930 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1932 mdt_object_put(info->mti_env, parent);
1936 /* getattr_name("..") should return master object FID for striped dir */
1937 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1938 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1940 RETURN(PTR_ERR(parent));
1942 /* old client getattr_name("..") with stripe FID */
1943 if (unlikely(is_old_parent_stripe)) {
1944 is_old_parent_stripe = false;
1948 /* ".." may be a stripe */
1949 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1950 is_new_parent_checked = true;
1954 mdt_object_put(info->mti_env, parent);
1957 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1958 repbody->mbo_fid1 = *fid;
1959 repbody->mbo_valid = OBD_MD_FLID;
1965 * Find name matching hash
1967 * We search \a child LinkEA for a name whose hash matches \a lname
1968 * (it contains an encoded hash).
1970 * \param info mdt thread info
1971 * \param lname encoded hash to find
1972 * \param parent parent object
1973 * \param child object to search with LinkEA
1975 * \retval 1 match found
1976 * \retval 0 no match found
1977 * \retval -ev negative errno upon error
1979 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
1980 struct mdt_object *parent, struct mdt_object *child)
1982 /* Here, lname is an encoded hash of on-disk name, and
1983 * client is doing access without encryption key.
1984 * So we need to get LinkEA, check parent fid is correct and
1985 * compare name hash with the one in the request.
1987 struct lu_buf *buf = &info->mti_big_buf;
1988 struct lu_name name;
1990 struct linkea_data ldata = { NULL };
1991 struct link_ea_header *leh;
1992 struct link_ea_entry *lee;
1993 struct lu_buf link = { 0 };
1995 int reclen, count, rc;
1998 if (lname->ln_namelen < LL_CRYPTO_BLOCK_SIZE)
2001 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2006 rc = mdt_links_read(info, child, &ldata);
2010 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
2013 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
2016 lee = (struct link_ea_entry *)(leh + 1);
2017 for (count = 0; count < leh->leh_reccount; count++) {
2018 linkea_entry_unpack(lee, &reclen, &name, &pfid);
2019 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
2020 lu_buf_check_and_alloc(&link, name.ln_namelen);
2022 GOTO(out_match, rc = -ENOMEM);
2023 rc = critical_decode(name.ln_name, name.ln_namelen,
2026 if (memcmp(LLCRYPT_EXTRACT_DIGEST(link.lb_buf, rc),
2027 hash, LL_CRYPTO_BLOCK_SIZE) == 0) {
2032 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2034 if (count == leh->leh_reccount)
2047 * UPDATE lock should be taken against parent, and be released before exit;
2048 * child_bits lock should be taken against child, and be returned back:
2049 * (1)normal request should release the child lock;
2050 * (2)intent request will grant the lock to client.
2052 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2053 struct mdt_lock_handle *lhc,
2055 struct ldlm_reply *ldlm_rep)
2057 struct ptlrpc_request *req = mdt_info_req(info);
2058 struct mdt_body *reqbody = NULL;
2059 struct mdt_object *parent = info->mti_object;
2060 struct mdt_object *child = NULL;
2061 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2062 struct lu_name *lname = NULL;
2063 struct mdt_lock_handle *lhp = NULL;
2064 struct ldlm_lock *lock;
2065 struct req_capsule *pill = info->mti_pill;
2066 bool fscrypt_md = false;
2074 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2075 LASSERT(ergo(is_resent,
2076 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2081 if (info->mti_cross_ref) {
2082 /* Only getattr on the child. Parent is on another node. */
2083 mdt_set_disposition(info, ldlm_rep,
2084 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2086 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
2088 PFID(mdt_object_fid(child)), ldlm_rep);
2090 rc = mdt_check_resent_lock(info, child, lhc);
2093 } else if (rc > 0) {
2095 * Object's name entry is on another MDS, it will
2096 * request PERM lock only because LOOKUP lock is owned
2097 * by the MDS where name entry resides.
2099 * TODO: it should try layout lock too. - Jinshan
2101 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2102 MDS_INODELOCK_LAYOUT);
2103 child_bits |= MDS_INODELOCK_PERM;
2104 rc = mdt_object_lock(info, child, lhc, child_bits,
2110 /* Finally, we can get attr for child. */
2111 if (!mdt_object_exists(child)) {
2112 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2114 "remote object doesn't exist.");
2115 mdt_object_unlock(info, child, lhc, 1);
2119 rc = mdt_getattr_internal(info, child, 0);
2120 if (unlikely(rc != 0)) {
2121 mdt_object_unlock(info, child, lhc, 1);
2125 rc = mdt_pack_secctx_in_reply(info, child);
2127 mdt_object_unlock(info, child, lhc, 1);
2131 rc = mdt_pack_encctx_in_reply(info, child);
2133 mdt_object_unlock(info, child, lhc, 1);
2137 lname = &info->mti_name;
2138 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2140 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2141 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2142 if (unlikely(reqbody == NULL))
2143 RETURN(err_serious(-EPROTO));
2145 *child_fid = reqbody->mbo_fid2;
2146 if (unlikely(!fid_is_sane(child_fid)))
2147 RETURN(err_serious(-EINVAL));
2149 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2150 mdt_object_get(info->mti_env, parent);
2153 child = mdt_object_find(info->mti_env, info->mti_mdt,
2156 RETURN(PTR_ERR(child));
2159 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2161 PFID(mdt_object_fid(parent)),
2162 PFID(&reqbody->mbo_fid2), ldlm_rep);
2163 } else if (lu_name_is_valid(lname)) {
2164 if (mdt_object_remote(parent)) {
2165 CERROR("%s: parent "DFID" is on remote target\n",
2166 mdt_obd_name(info->mti_mdt),
2167 PFID(mdt_object_fid(parent)));
2171 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
2172 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
2173 PNAME(lname), ldlm_rep);
2175 if (parent->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD ||
2176 (fid_is_root(mdt_object_fid(parent)) &&
2177 lname->ln_namelen == strlen(dot_fscrypt_name) &&
2178 strncmp(lname->ln_name, dot_fscrypt_name,
2179 lname->ln_namelen) == 0))
2182 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2183 if (unlikely(reqbody == NULL))
2184 RETURN(err_serious(-EPROTO));
2186 *child_fid = reqbody->mbo_fid2;
2187 if (unlikely(!fid_is_sane(child_fid)))
2188 RETURN(err_serious(-EINVAL));
2190 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2191 mdt_object_get(info->mti_env, parent);
2194 child = mdt_object_find(info->mti_env, info->mti_mdt,
2197 RETURN(PTR_ERR(child));
2200 if (mdt_object_remote(child)) {
2201 CERROR("%s: child "DFID" is on remote target\n",
2202 mdt_obd_name(info->mti_mdt),
2203 PFID(mdt_object_fid(child)));
2204 GOTO(out_child, rc = -EPROTO);
2207 /* don't fetch LOOKUP lock if it's remote object */
2208 rc = mdt_is_remote_object(info, parent, child);
2210 GOTO(out_child, rc);
2212 child_bits &= ~MDS_INODELOCK_LOOKUP;
2214 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2216 PFID(mdt_object_fid(parent)),
2217 PFID(&reqbody->mbo_fid2), ldlm_rep);
2220 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2222 if (unlikely(!mdt_object_exists(parent)) &&
2223 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2224 lu_name_is_valid(lname)) {
2225 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2227 "Parent doesn't exist!");
2228 GOTO(out_child, rc = -ESTALE);
2231 if (!child && is_resent) {
2232 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2234 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2235 * a resend case, however, it could be already destroyed
2236 * due to client eviction or a raced cancel RPC.
2238 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2239 lhc->mlh_reg_lh.cookie);
2242 fid_extract_from_res_name(child_fid,
2243 &lock->l_resource->lr_name);
2244 LDLM_LOCK_PUT(lock);
2245 child = mdt_object_find(info->mti_env, info->mti_mdt,
2248 RETURN(PTR_ERR(child));
2249 } else if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2250 lu_name_is_valid(lname)) {
2251 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2252 rc = mdt_raw_lookup(info, parent, lname);
2257 /* step 1: lock parent only if parent is a directory */
2258 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2259 lhp = &info->mti_lh[MDT_LH_PARENT];
2260 rc = mdt_parent_lock(info, parent, lhp, lname, LCK_PR,
2262 if (unlikely(rc != 0))
2266 /* step 2: lookup child's fid by name */
2267 fid_zero(child_fid);
2268 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2269 child_fid, &info->mti_spec);
2271 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2274 GOTO(unlock_parent, rc);
2276 child = mdt_object_find(info->mti_env, info->mti_mdt,
2278 if (unlikely(IS_ERR(child)))
2279 GOTO(unlock_parent, rc = PTR_ERR(child));
2282 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2284 /* step 3: lock child regardless if it is local or remote. */
2287 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2288 /* Here, lname is an encoded hash of on-disk name, and
2289 * client is doing access without encryption key.
2290 * So we need to compare name hash with the one in the request.
2292 if (!find_name_matching_hash(info, lname, parent,
2294 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2295 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2296 GOTO(out_child, rc = -ENOENT);
2300 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2301 if (!mdt_object_exists(child)) {
2302 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2304 "Object doesn't exist!");
2305 GOTO(out_child, rc = -ENOENT);
2308 rc = mdt_check_resent_lock(info, child, lhc);
2310 GOTO(out_child, rc);
2311 } else if (rc > 0) {
2312 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2313 !mdt_object_remote(child)) {
2314 struct md_attr *ma = &info->mti_attr;
2317 ma->ma_need = MA_INODE;
2318 rc = mdt_attr_get_complex(info, child, ma);
2319 if (unlikely(rc != 0))
2320 GOTO(out_child, rc);
2322 /* If the file has not been changed for some time, we
2323 * return not only a LOOKUP lock, but also an UPDATE
2324 * lock and this might save us RPC on later STAT. For
2325 * directories, it also let negative dentry cache start
2326 * working for this dir. */
2327 if (ma->ma_valid & MA_INODE &&
2328 ma->ma_attr.la_valid & LA_CTIME &&
2329 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2330 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2331 child_bits |= MDS_INODELOCK_UPDATE;
2334 /* layout lock must be granted in a best-effort way
2335 * for IT operations */
2336 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2337 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2338 !mdt_object_remote(child) && ldlm_rep != NULL) {
2339 if (!CFS_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2340 exp_connect_layout(info->mti_exp)) {
2341 /* try to grant layout lock for regular file. */
2342 try_bits = MDS_INODELOCK_LAYOUT;
2344 /* Acquire DOM lock in advance for data-on-mdt file */
2345 if (child != parent)
2346 try_bits |= MDS_INODELOCK_DOM;
2350 * To avoid possible deadlock between batched statahead RPC
2351 * and rename()/migrate() operation, it should use trylock to
2352 * obtain the DLM PR ibits lock for file attributes in a
2353 * batched statahead RPC. A failed trylock means that other
2354 * users maybe modify the directory simultaneously as in current
2355 * Lustre design the server only grants read lock to a client.
2357 * When a trylock failed, the MDT reports the conflict with
2358 * error code -EBUSY, and stops statahead immediately.
2360 if (info->mti_batch_env) {
2362 * This is a sub stat-ahead request in a batched RPC.
2363 * However, the @child is a remote object, we just
2364 * return -EREMOTE here to forbid stat-ahead on it.
2366 if (mdt_object_remote(child))
2367 GOTO(out_child, rc = -EREMOTE);
2368 try_bits |= child_bits;
2372 if (try_bits != 0) {
2373 /* try layout lock, it may fail to be granted due to
2374 * contention at LOOKUP or UPDATE */
2375 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2376 try_bits, LCK_PR, false);
2377 if (child_bits & MDS_INODELOCK_LAYOUT)
2380 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2381 * client will enqueue the lock to the remote MDT */
2382 if (mdt_object_remote(child))
2383 rc = mdt_object_lookup_lock(info, NULL, child,
2384 lhc, LCK_PR, false);
2386 rc = mdt_object_lock(info, child, lhc,
2387 child_bits, LCK_PR, false);
2389 if (unlikely(rc != 0))
2390 GOTO(out_child, rc);
2391 if (info->mti_batch_env && child_bits == 0) {
2393 mdt_object_unlock(info, child, lhc, 1);
2394 GOTO(out_child, rc = -EBUSY);
2399 child->mot_obj.lo_header->loh_attr |= LOHA_FSCRYPT_MD;
2401 /* finally, we can get attr for child. */
2402 rc = mdt_getattr_internal(info, child, ma_need);
2403 if (unlikely(rc != 0)) {
2405 mdt_object_unlock(info, child, lhc, 1);
2406 GOTO(out_child, rc);
2409 rc = mdt_pack_secctx_in_reply(info, child);
2412 mdt_object_unlock(info, child, lhc, 1);
2413 GOTO(out_child, rc);
2416 rc = mdt_pack_encctx_in_reply(info, child);
2419 mdt_object_unlock(info, child, lhc, 1);
2420 GOTO(out_child, rc);
2423 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2425 /* Debugging code. */
2426 LDLM_DEBUG(lock, "Returning lock to client");
2427 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2428 &lock->l_resource->lr_name),
2429 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2430 PLDLMRES(lock->l_resource),
2431 PFID(mdt_object_fid(child)));
2433 if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2434 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2435 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2437 req->rq_arrival_time.tv_sec +
2439 /* Put the lock to the waiting list and force the cancel */
2440 ldlm_set_ast_sent(lock);
2443 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2444 !mdt_object_remote(child) && child != parent) {
2445 mdt_object_put(info->mti_env, child);
2446 rc = mdt_pack_size2body(info, child_fid,
2448 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2449 /* DOM lock was taken in advance but this is
2450 * not DoM file. Drop the lock.
2452 lock_res_and_lock(lock);
2453 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2454 unlock_res_and_lock(lock);
2456 LDLM_LOCK_PUT(lock);
2457 GOTO(unlock_parent, rc = 0);
2459 LDLM_LOCK_PUT(lock);
2465 mdt_object_put(info->mti_env, child);
2468 mdt_object_unlock(info, parent, lhp, 1);
2469 if (rc == -ENOENT) {
2470 /* return -ENOKEY instead of -ENOENT to encryption-unaware
2471 * client if trying to access an encrypted file
2473 int rc2 = mdt_check_enc(info, parent);
2481 /* normal handler: should release the child lock */
2482 static int mdt_getattr_name(struct tgt_session_info *tsi)
2484 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2485 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2486 struct mdt_body *reqbody;
2487 struct mdt_body *repbody;
2492 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2493 LASSERT(reqbody != NULL);
2494 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2495 LASSERT(repbody != NULL);
2497 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2498 repbody->mbo_eadatasize = 0;
2499 repbody->mbo_aclsize = 0;
2501 rc = mdt_init_ucred(info, reqbody);
2503 GOTO(out_shrink, rc);
2505 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2506 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2507 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2508 lhc->mlh_reg_lh.cookie = 0;
2510 mdt_exit_ucred(info);
2513 mdt_client_compatibility(info);
2514 rc2 = mdt_fix_reply(info);
2517 mdt_thread_info_fini(info);
2521 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2522 const struct lu_fid *pfid,
2523 const struct lu_name *name,
2524 struct mdt_object *obj, s64 ctime)
2526 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2527 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
2528 struct mdt_device *mdt = info->mti_mdt;
2529 struct md_attr *ma = &info->mti_attr;
2530 struct mdt_lock_handle *parent_lh;
2531 struct mdt_lock_handle *child_lh;
2532 struct mdt_object *pobj;
2533 bool cos_incompat = false;
2537 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2539 GOTO(out, rc = PTR_ERR(pobj));
2541 if (mdt_object_remote(pobj))
2542 cos_incompat = true;
2544 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2545 rc = mdt_parent_lock(info, pobj, parent_lh, name, LCK_PW, cos_incompat);
2547 GOTO(put_parent, rc);
2549 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2550 name, child_fid, &info->mti_spec);
2552 GOTO(unlock_parent, rc);
2554 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2555 GOTO(unlock_parent, rc = -EREMCHG);
2557 child_lh = &info->mti_lh[MDT_LH_CHILD];
2558 rc = mdt_object_stripes_lock(info, pobj, obj, child_lh, einfo,
2559 MDS_INODELOCK_LOOKUP |
2560 MDS_INODELOCK_UPDATE,
2561 LCK_EX, cos_incompat);
2563 GOTO(unlock_parent, rc);
2565 if (atomic_read(&obj->mot_open_count)) {
2566 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2567 PFID(mdt_object_fid(obj)));
2568 GOTO(unlock_child, rc = -EBUSY);
2572 ma->ma_valid = MA_INODE;
2573 ma->ma_attr.la_valid = LA_CTIME;
2574 ma->ma_attr.la_ctime = ctime;
2576 mutex_lock(&obj->mot_lov_mutex);
2578 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2579 mdt_object_child(obj), name, ma, 0);
2581 mutex_unlock(&obj->mot_lov_mutex);
2584 mdt_object_stripes_unlock(info, obj, child_lh, einfo, 1);
2586 mdt_object_unlock(info, pobj, parent_lh, 1);
2588 mdt_object_put(info->mti_env, pobj);
2593 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2594 struct mdt_object *obj)
2596 struct lu_ucred *uc = lu_ucred(info->mti_env);
2597 struct md_attr *ma = &info->mti_attr;
2598 struct lu_attr *la = &ma->ma_attr;
2602 ma->ma_need = MA_INODE;
2603 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2607 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2610 /* we want rbac roles to have precedence over any other
2611 * permission or capability checks
2613 if (!uc->uc_rbac_byfid_ops)
2615 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2617 if (uc->uc_fsuid == la->la_uid) {
2618 if ((la->la_mode & S_IWUSR) == 0)
2620 } else if (uc->uc_fsgid == la->la_gid) {
2621 if ((la->la_mode & S_IWGRP) == 0)
2623 } else if ((la->la_mode & S_IWOTH) == 0) {
2631 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2634 struct mdt_device *mdt = info->mti_mdt;
2635 struct mdt_object *obj = NULL;
2636 struct linkea_data ldata = { NULL };
2637 struct lu_buf *buf = &info->mti_big_buf;
2638 struct lu_name *name = &info->mti_name;
2639 struct lu_fid *pfid = &info->mti_tmp_fid1;
2640 struct link_ea_header *leh;
2641 struct link_ea_entry *lee;
2642 int reclen, count, rc = 0;
2645 if (!fid_is_sane(fid))
2646 GOTO(out, rc = -EINVAL);
2648 if (!fid_is_namespace_visible(fid))
2649 GOTO(out, rc = -EINVAL);
2651 obj = mdt_object_find(info->mti_env, mdt, fid);
2653 GOTO(out, rc = PTR_ERR(obj));
2655 if (mdt_object_remote(obj))
2656 GOTO(out, rc = -EREMOTE);
2657 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2658 GOTO(out, rc = -ENOENT);
2660 rc = mdt_rmfid_check_permission(info, obj);
2665 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2667 GOTO(out, rc = -ENOMEM);
2670 rc = mdt_links_read(info, obj, &ldata);
2675 lee = (struct link_ea_entry *)(leh + 1);
2676 for (count = 0; count < leh->leh_reccount; count++) {
2677 /* remove every hardlink */
2678 linkea_entry_unpack(lee, &reclen, name, pfid);
2679 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2680 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2686 if (obj && !IS_ERR(obj))
2687 mdt_object_put(info->mti_env, obj);
2688 if (info->mti_big_buf.lb_buf)
2689 lu_buf_free(&info->mti_big_buf);
2694 static int mdt_rmfid(struct tgt_session_info *tsi)
2696 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2697 struct mdt_body *reqbody;
2698 struct lu_fid *fids, *rfids;
2704 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2705 if (reqbody == NULL)
2707 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2709 nr = bufsize / sizeof(struct lu_fid);
2710 if (nr * sizeof(struct lu_fid) != bufsize)
2712 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2713 RCL_SERVER, nr * sizeof(__u32));
2714 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2715 RCL_SERVER, nr * sizeof(struct lu_fid));
2716 rc = req_capsule_server_pack(tsi->tsi_pill);
2718 GOTO(out, rc = err_serious(rc));
2719 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2722 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2724 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2727 mdt_init_ucred(mti, reqbody);
2728 for (i = 0; i < nr; i++) {
2730 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2732 mdt_exit_ucred(mti);
2738 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2739 void *karg, void __user *uarg);
2741 int mdt_io_set_info(struct tgt_session_info *tsi)
2743 struct ptlrpc_request *req = tgt_ses_req(tsi);
2744 struct ost_body *body = NULL, *repbody;
2745 void *key, *val = NULL;
2746 int keylen, vallen, rc = 0;
2747 bool is_grant_shrink;
2751 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2753 DEBUG_REQ(D_HA, req, "no set_info key");
2754 RETURN(err_serious(-EFAULT));
2756 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2759 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2761 DEBUG_REQ(D_HA, req, "no set_info val");
2762 RETURN(err_serious(-EFAULT));
2764 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2767 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
2768 if (is_grant_shrink)
2769 /* In this case the value is actually an RMF_OST_BODY, so we
2770 * transmutate the type of this PTLRPC */
2771 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
2773 rc = req_capsule_server_pack(tsi->tsi_pill);
2777 if (is_grant_shrink) {
2778 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
2780 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2783 /** handle grant shrink, similar to a read request */
2784 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
2787 CERROR("%s: Unsupported key %s\n",
2788 tgt_name(tsi->tsi_tgt), (char *)key);
2796 static int mdt_set_info(struct tgt_session_info *tsi)
2798 struct ptlrpc_request *req = tgt_ses_req(tsi);
2801 int keylen, vallen, rc = 0;
2805 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2807 DEBUG_REQ(D_HA, req, "no set_info key");
2808 RETURN(err_serious(-EFAULT));
2811 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2814 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2816 DEBUG_REQ(D_HA, req, "no set_info val");
2817 RETURN(err_serious(-EFAULT));
2820 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2823 /* Swab any part of val you need to here */
2824 if (KEY_IS(KEY_READ_ONLY)) {
2825 spin_lock(&req->rq_export->exp_lock);
2827 *exp_connect_flags_ptr(req->rq_export) |=
2830 *exp_connect_flags_ptr(req->rq_export) &=
2831 ~OBD_CONNECT_RDONLY;
2832 spin_unlock(&req->rq_export->exp_lock);
2833 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2834 struct changelog_setinfo *cs = val;
2836 if (vallen != sizeof(*cs)) {
2837 CERROR("%s: bad changelog_clear setinfo size %d\n",
2838 tgt_name(tsi->tsi_tgt), vallen);
2841 if (req_capsule_req_need_swab(&req->rq_pill)) {
2842 __swab64s(&cs->cs_recno);
2843 __swab32s(&cs->cs_id);
2846 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
2848 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2850 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2852 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2859 static int mdt_readpage(struct tgt_session_info *tsi)
2861 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2862 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2863 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2864 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2865 struct mdt_body *repbody;
2871 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2872 RETURN(err_serious(-ENOMEM));
2874 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2875 if (repbody == NULL || reqbody == NULL)
2876 RETURN(err_serious(-EFAULT));
2879 * prepare @rdpg before calling lower layers and transfer itself. Here
2880 * reqbody->size contains offset of where to start to read and
2881 * reqbody->nlink contains number bytes to read.
2883 rdpg->rp_hash = reqbody->mbo_size;
2884 if (rdpg->rp_hash != reqbody->mbo_size) {
2885 CERROR("Invalid hash: %#llx != %#llx\n",
2886 rdpg->rp_hash, reqbody->mbo_size);
2890 rdpg->rp_attrs = reqbody->mbo_mode;
2891 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2892 rdpg->rp_attrs |= LUDA_64BITHASH;
2893 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2894 exp_max_brw_size(tsi->tsi_exp));
2895 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2897 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2898 if (rdpg->rp_pages == NULL)
2901 for (i = 0; i < rdpg->rp_npages; ++i) {
2902 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2903 if (rdpg->rp_pages[i] == NULL)
2904 GOTO(free_rdpg, rc = -ENOMEM);
2907 /* call lower layers to fill allocated pages with directory data */
2908 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2910 GOTO(free_rdpg, rc);
2912 /* send pages to client */
2913 rc = tgt_sendpage(tsi, rdpg, rc);
2918 for (i = 0; i < rdpg->rp_npages; i++)
2919 if (rdpg->rp_pages[i] != NULL)
2920 __free_page(rdpg->rp_pages[i]);
2921 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2923 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2929 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2931 struct lu_ucred *uc = mdt_ucred_check(info);
2932 struct lu_attr *attr = &info->mti_attr.ma_attr;
2937 if (op != REINT_SETATTR) {
2938 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2939 attr->la_uid = uc->uc_fsuid;
2940 /* for S_ISGID, inherit gid from his parent, such work will be
2941 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2942 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2943 attr->la_gid = uc->uc_fsgid;
2949 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2951 return op == REINT_OPEN &&
2952 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2955 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2957 struct req_capsule *pill = info->mti_pill;
2959 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2961 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2963 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2965 /* pre-set size in server part with max size */
2966 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2968 req_capsule_ptlreq(pill) ?
2969 OBD_MAX_DEFAULT_EA_SIZE :
2972 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2977 static int mdt_reint_internal(struct mdt_thread_info *info,
2978 struct mdt_lock_handle *lhc,
2981 struct req_capsule *pill = info->mti_pill;
2982 struct mdt_body *repbody;
2987 rc = mdt_reint_unpack(info, op);
2989 CERROR("Can't unpack reint, rc %d\n", rc);
2990 RETURN(err_serious(rc));
2994 /* check if the file system is set to readonly. O_RDONLY open
2995 * is still allowed even the file system is set to readonly mode */
2996 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2997 RETURN(err_serious(-EROFS));
2999 /* for replay (no_create) lmm is not needed, client has it already */
3000 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3001 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3004 /* llog cookies are always 0, the field is kept for compatibility */
3005 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3006 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
3008 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3009 * by default. If the target object has more ACL entries, then
3010 * enlarge the buffer when necessary. */
3011 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3012 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3013 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3015 mdt_preset_secctx_size(info);
3016 mdt_preset_encctx_size(info);
3018 rc = req_capsule_server_pack(pill);
3020 CERROR("Can't pack response, rc %d\n", rc);
3021 RETURN(err_serious(rc));
3024 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
3025 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
3027 repbody->mbo_eadatasize = 0;
3028 repbody->mbo_aclsize = 0;
3031 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
3033 /* for replay no cookkie / lmm need, because client have this already */
3034 if (info->mti_spec.no_create)
3035 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3036 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
3038 rc = mdt_init_ucred_reint(info);
3040 GOTO(out_shrink, rc);
3042 rc = mdt_fix_attr_ucred(info, op);
3044 GOTO(out_ucred, rc = err_serious(rc));
3046 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
3048 GOTO(out_ucred, rc);
3049 } else if (rc == 1) {
3050 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
3051 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
3052 GOTO(out_ucred, rc);
3054 rc = mdt_reint_rec(info, lhc);
3057 mdt_exit_ucred(info);
3059 mdt_client_compatibility(info);
3061 rc2 = mdt_fix_reply(info);
3066 * Data-on-MDT optimization - read data along with OPEN and return it
3067 * in reply when possible.
3069 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
3070 rc = mdt_dom_read_on_open(info, info->mti_mdt,
3076 static long mdt_reint_opcode(struct ptlrpc_request *req,
3077 const struct req_format **fmt)
3079 struct mdt_device *mdt;
3080 struct mdt_rec_reint *rec;
3083 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
3085 opc = rec->rr_opcode;
3086 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
3087 if (opc < REINT_MAX && fmt[opc] != NULL)
3088 req_capsule_extend(&req->rq_pill, fmt[opc]);
3090 mdt = mdt_exp2dev(req->rq_export);
3091 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
3092 " rc = %d\n", req->rq_export->exp_obd->obd_name,
3093 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
3094 opc = err_serious(-EFAULT);
3097 opc = err_serious(-EFAULT);
3102 static int mdt_reint(struct tgt_session_info *tsi)
3106 static const struct req_format *reint_fmts[REINT_MAX] = {
3107 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
3108 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
3109 [REINT_LINK] = &RQF_MDS_REINT_LINK,
3110 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
3111 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
3112 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
3113 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
3114 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
3115 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
3116 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
3121 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
3123 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3125 * No lock possible here from client to pass it to reint code
3128 rc = mdt_reint_internal(info, NULL, opc);
3129 mdt_thread_info_fini(info);
3134 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
3138 /* this should sync the whole device */
3139 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
3141 struct dt_device *dt = mdt->mdt_bottom;
3145 rc = dt->dd_ops->dt_sync(env, dt);
3149 /* this should sync this object */
3150 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
3151 struct mdt_object *mo)
3157 if (!mdt_object_exists(mo)) {
3158 CWARN("%s: non existing object "DFID": rc = %d\n",
3159 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3164 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3165 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3166 dt_obj_version_t version;
3168 version = dt_version_get(env, mdt_obj2dt(mo));
3169 if (version > tgt->lut_obd->obd_last_committed)
3170 rc = mo_object_sync(env, mdt_object_child(mo));
3172 rc = mo_object_sync(env, mdt_object_child(mo));
3178 static int mdt_sync(struct tgt_session_info *tsi)
3180 struct ptlrpc_request *req = tgt_ses_req(tsi);
3181 struct req_capsule *pill = tsi->tsi_pill;
3182 struct mdt_body *body;
3183 ktime_t kstart = ktime_get();
3188 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3189 RETURN(err_serious(-ENOMEM));
3191 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3192 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3194 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3196 if (unlikely(info->mti_object == NULL))
3199 /* sync an object */
3200 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3203 const struct lu_fid *fid;
3204 struct lu_attr *la = &info->mti_attr.ma_attr;
3206 info->mti_attr.ma_need = MA_INODE;
3207 info->mti_attr.ma_valid = 0;
3208 rc = mdt_attr_get_complex(info, info->mti_object,
3211 body = req_capsule_server_get(pill,
3213 fid = mdt_object_fid(info->mti_object);
3214 mdt_pack_attr2body(info, body, la, fid);
3217 mdt_thread_info_fini(info);
3220 mdt_counter_incr(req, LPROC_MDT_SYNC,
3221 ktime_us_delta(ktime_get(), kstart));
3226 static int mdt_data_sync(struct tgt_session_info *tsi)
3228 struct mdt_thread_info *info;
3229 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3230 struct ost_body *body = tsi->tsi_ost_body;
3231 struct ost_body *repbody;
3232 struct mdt_object *mo = NULL;
3238 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3240 /* if no fid is specified then do nothing,
3241 * device sync is done via MDS_SYNC */
3242 if (fid_is_zero(&tsi->tsi_fid))
3245 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3247 RETURN(PTR_ERR(mo));
3249 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3253 repbody->oa.o_oi = body->oa.o_oi;
3254 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3256 info = tsi2mdt_info(tsi);
3257 ma = &info->mti_attr;
3258 ma->ma_need = MA_INODE;
3260 rc = mdt_attr_get_complex(info, mo, ma);
3262 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3265 mdt_thread_info_fini(info);
3270 mdt_object_put(tsi->tsi_env, mo);
3275 * Handle quota control requests to consult current usage/limit, but also
3276 * to configure quota enforcement
3278 static int mdt_quotactl(struct tgt_session_info *tsi)
3280 struct obd_export *exp = tsi->tsi_exp;
3281 struct req_capsule *pill = tsi->tsi_pill;
3282 struct obd_quotactl *oqctl, *repoqc;
3284 struct mdt_device *mdt = mdt_exp2dev(exp);
3285 struct lu_device *qmt = mdt->mdt_qmt_dev;
3286 struct lu_nodemap *nodemap;
3289 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3291 RETURN(err_serious(-EPROTO));
3293 rc = req_capsule_server_pack(pill);
3295 RETURN(err_serious(rc));
3297 nodemap = nodemap_get_from_exp(exp);
3298 if (IS_ERR(nodemap))
3299 RETURN(PTR_ERR(nodemap));
3301 switch (oqctl->qc_cmd) {
3302 /* master quotactl */
3305 case LUSTRE_Q_SETDEFAULT:
3306 case LUSTRE_Q_SETQUOTAPOOL:
3307 case LUSTRE_Q_SETINFOPOOL:
3308 case LUSTRE_Q_SETDEFAULT_POOL:
3309 case LUSTRE_Q_DELETEQID:
3310 case LUSTRE_Q_RESETQID:
3311 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3313 GOTO(out_nodemap, rc = -EPERM);
3317 case LUSTRE_Q_GETDEFAULT:
3318 case LUSTRE_Q_GETQUOTAPOOL:
3319 case LUSTRE_Q_GETINFOPOOL:
3320 case LUSTRE_Q_GETDEFAULT_POOL:
3322 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3323 /* slave quotactl */
3330 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3331 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3332 GOTO(out_nodemap, rc);
3336 switch (oqctl->qc_type) {
3338 id = nodemap_map_id(nodemap, NODEMAP_UID,
3339 NODEMAP_CLIENT_TO_FS, id);
3342 id = nodemap_map_id(nodemap, NODEMAP_GID,
3343 NODEMAP_CLIENT_TO_FS, id);
3346 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3347 NODEMAP_CLIENT_TO_FS, id);
3350 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3352 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3354 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3356 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3357 barrier_exit(tsi->tsi_tgt->lut_bottom);
3359 if (oqctl->qc_id != id)
3360 swap(oqctl->qc_id, id);
3362 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3363 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3364 RETURN(-EINPROGRESS);
3367 switch (oqctl->qc_cmd) {
3373 case LUSTRE_Q_SETDEFAULT:
3374 case LUSTRE_Q_GETDEFAULT:
3375 case LUSTRE_Q_SETQUOTAPOOL:
3376 case LUSTRE_Q_GETQUOTAPOOL:
3377 case LUSTRE_Q_SETINFOPOOL:
3378 case LUSTRE_Q_GETINFOPOOL:
3379 case LUSTRE_Q_SETDEFAULT_POOL:
3380 case LUSTRE_Q_GETDEFAULT_POOL:
3381 case LUSTRE_Q_DELETEQID:
3382 case LUSTRE_Q_RESETQID:
3383 /* forward quotactl request to QMT */
3384 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3389 /* slave quotactl */
3390 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3395 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3396 GOTO(out_nodemap, rc = -EFAULT);
3399 if (oqctl->qc_id != id)
3400 swap(oqctl->qc_id, id);
3402 QCTL_COPY_NO_PNAME(repoqc, oqctl);
3406 nodemap_putref(nodemap);
3411 /** clone llog ctxt from child (mdd)
3412 * This allows remote llog (replicator) access.
3413 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3414 * context was originally set up, or we can handle them directly.
3415 * I choose the latter, but that means I need any llog
3416 * contexts set up by child to be accessable by the mdt. So we clone the
3417 * context into our context list here.
3419 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3422 struct md_device *next = mdt->mdt_child;
3423 struct llog_ctxt *ctxt;
3426 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3429 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3430 if (rc || ctxt == NULL) {
3434 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3436 CERROR("Can't set mdt ctxt %d\n", rc);
3441 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3442 struct mdt_device *mdt, int idx)
3444 struct llog_ctxt *ctxt;
3446 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3449 /* Put once for the get we just did, and once for the clone */
3450 llog_ctxt_put(ctxt);
3451 llog_ctxt_put(ctxt);
3456 * sec context handlers
3458 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3460 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3466 * quota request handlers
3468 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3470 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3471 struct lu_device *qmt = mdt->mdt_qmt_dev;
3476 RETURN(err_serious(-EOPNOTSUPP));
3478 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3482 struct mdt_object *mdt_object_new(const struct lu_env *env,
3483 struct mdt_device *d,
3484 const struct lu_fid *f)
3486 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3487 struct lu_object *o;
3488 struct mdt_object *m;
3491 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3492 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3493 if (unlikely(IS_ERR(o)))
3494 m = (struct mdt_object *)o;
3500 struct mdt_object *mdt_object_find(const struct lu_env *env,
3501 struct mdt_device *d,
3502 const struct lu_fid *f)
3504 struct lu_object *o;
3505 struct mdt_object *m;
3508 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3509 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3510 if (unlikely(IS_ERR(o)))
3511 m = (struct mdt_object *)o;
3519 * Asyncronous commit for mdt device.
3521 * Pass asynchonous commit call down the MDS stack.
3523 * \param env environment
3524 * \param mdt the mdt device
3526 static void mdt_device_commit_async(const struct lu_env *env,
3527 struct mdt_device *mdt)
3529 struct dt_device *dt = mdt->mdt_bottom;
3533 rc = dt->dd_ops->dt_commit_async(env, dt);
3534 if (unlikely(rc != 0))
3535 CWARN("%s: async commit start failed: rc = %d\n",
3536 mdt_obd_name(mdt), rc);
3537 atomic_inc(&mdt->mdt_async_commit_count);
3542 * Mark the lock as "synchonous".
3544 * Mark the lock to deffer transaction commit to the unlock time.
3546 * \param lock the lock to mark as "synchonous"
3548 * \see mdt_is_lock_sync
3549 * \see mdt_save_lock
3551 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3553 lock->l_ast_data = (void*)1;
3557 * Check whehter the lock "synchonous" or not.
3559 * \param lock the lock to check
3560 * \retval 1 the lock is "synchonous"
3561 * \retval 0 the lock isn't "synchronous"
3563 * \see mdt_set_lock_sync
3564 * \see mdt_save_lock
3566 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3568 return lock->l_ast_data != NULL;
3572 * Blocking AST for mdt locks.
3574 * Starts transaction commit if in case of COS lock conflict or
3575 * deffers such a commit to the mdt_save_lock.
3577 * \param lock the lock which blocks a request or cancelling lock
3578 * \param desc unused
3579 * \param data unused
3580 * \param flag indicates whether this cancelling or blocking callback
3582 * \see ldlm_blocking_ast_nocheck
3584 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3585 void *data, int flag)
3587 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3588 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3589 struct ldlm_cb_set_arg *arg = data;
3590 bool commit_async = false;
3594 if (flag == LDLM_CB_CANCELING)
3597 lock_res_and_lock(lock);
3598 if (lock->l_blocking_ast != mdt_blocking_ast) {
3599 unlock_res_and_lock(lock);
3603 /* A blocking ast may be sent from ldlm_lock_decref_internal
3604 * when the last reference to a local lock was released and
3605 * during blocking event from ldlm_work_bl_ast_lock().
3606 * The 'data' parameter is l_ast_data in the first case and
3607 * callback arguments in the second one. Distinguish them by that.
3609 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3610 goto skip_cos_checks;
3612 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3613 if (mdt_cos_is_enabled(mdt)) {
3614 if (!arg->bl_desc->bl_same_client)
3615 mdt_set_lock_sync(lock);
3616 } else if (mdt_slc_is_enabled(mdt) &&
3617 arg->bl_desc->bl_cos_incompat) {
3618 mdt_set_lock_sync(lock);
3620 * we may do extra commit here, but there is a small
3621 * window to miss a commit: lock was unlocked (saved),
3622 * then a conflict lock queued and we come here, but
3623 * REP-ACK not received, so lock was not converted to
3625 * Fortunately this window is quite small, so the
3626 * extra commit should be rare (not to say distributed
3627 * operation is rare too).
3629 commit_async = true;
3631 } else if (lock->l_req_mode == LCK_COS) {
3632 commit_async = true;
3636 rc = ldlm_blocking_ast_nocheck(lock);
3641 rc = lu_env_init(&env, LCT_LOCAL);
3642 if (unlikely(rc != 0))
3643 CWARN("%s: lu_env initialization failed, cannot "
3644 "start asynchronous commit: rc = %d\n",
3647 mdt_device_commit_async(&env, mdt);
3654 * Blocking AST for cross-MDT lock
3656 * Discard lock from uncommitted_slc_locks and cancel it.
3658 * \param lock the lock which blocks a request or cancelling lock
3659 * \param desc unused
3660 * \param data unused
3661 * \param flag indicates whether this cancelling or blocking callback
3662 * \retval 0 on success
3663 * \retval negative number on error
3665 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3666 void *data, int flag)
3672 case LDLM_CB_BLOCKING: {
3673 struct lustre_handle lockh;
3675 ldlm_lock2handle(lock, &lockh);
3676 rc = ldlm_cli_cancel(&lockh,
3677 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3679 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3684 case LDLM_CB_CANCELING: {
3685 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3686 struct mdt_device *mdt =
3687 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3689 LDLM_DEBUG(lock, "Revoke remote lock");
3691 /* discard slc lock here so that it can be cleaned anytime,
3692 * especially for cleanup_resource() */
3693 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3695 /* once we cache lock, l_ast_data is set to mdt_object */
3696 if (lock->l_ast_data != NULL) {
3697 struct mdt_object *mo = lock->l_ast_data;
3700 rc = lu_env_init(&env, LCT_MD_THREAD);
3701 if (unlikely(rc != 0)) {
3702 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3704 PFID(mdt_object_fid(mo)), rc);
3708 if (lock->l_policy_data.l_inodebits.bits &
3709 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3710 rc = mo_invalidate(&env, mdt_object_child(mo));
3711 mo->mot_cache_attr = 0;
3713 mdt_object_put(&env, mo);
3725 int mdt_check_resent_lock(struct mdt_thread_info *info,
3726 struct mdt_object *mo,
3727 struct mdt_lock_handle *lhc)
3729 /* the lock might already be gotten in ldlm_handle_enqueue() */
3730 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3731 struct ptlrpc_request *req = mdt_info_req(info);
3732 struct ldlm_lock *lock;
3734 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3735 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3737 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3738 * a resend case, however, it could be already destroyed
3739 * due to client eviction or a raced cancel RPC. */
3740 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3741 lhc->mlh_reg_lh.cookie);
3745 if (!fid_res_name_eq(mdt_object_fid(mo),
3746 &lock->l_resource->lr_name)) {
3747 CWARN("%s: Although resent, but still not "
3748 "get child lock:"DFID"\n",
3749 info->mti_exp->exp_obd->obd_name,
3750 PFID(mdt_object_fid(mo)));
3751 LDLM_LOCK_PUT(lock);
3754 LDLM_LOCK_PUT(lock);
3760 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3762 mdt_object_get(NULL, lock->l_ast_data);
3765 static int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3766 struct mdt_object *obj,
3767 struct lustre_handle *lh,
3768 enum ldlm_mode mode,
3769 union ldlm_policy_data *policy,
3770 struct ldlm_res_id *res_id,
3773 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3776 LASSERT(mdt_object_remote(obj));
3778 memset(einfo, 0, sizeof(*einfo));
3779 einfo->ei_type = LDLM_IBITS;
3780 einfo->ei_mode = mode;
3781 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3782 einfo->ei_cb_cp = ldlm_completion_ast;
3783 einfo->ei_enq_slave = 0;
3784 einfo->ei_res_id = res_id;
3785 einfo->ei_req_slot = 1;
3788 * if we cache lock, couple lock with mdt_object, so that object
3789 * can be easily found in lock ASTs.
3791 einfo->ei_cbdata = obj;
3792 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3795 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), lh, einfo,
3802 /* other components like LFSCK can use lockless access
3803 * and populate cache, so we better invalidate it
3805 if (policy->l_inodebits.bits &
3806 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR))
3807 mo_invalidate(mti->mti_env, mdt_object_child(obj));
3813 * Helper function to take PDO and hash lock.
3815 * if \a pdo_lock is false, don't take PDO lock, this is case in rename.
3817 int mdt_object_pdo_lock(struct mdt_thread_info *info, struct mdt_object *obj,
3818 struct mdt_lock_handle *lh, const struct lu_name *name,
3819 enum ldlm_mode mode, bool pdo_lock, bool cos_incompat)
3821 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3822 union ldlm_policy_data *policy = &info->mti_policy;
3823 struct ldlm_res_id *res_id = &info->mti_res_id;
3825 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it is never going to
3826 * be sent to client and we do not want it slowed down due to possible
3829 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
3830 __u64 *cookie = NULL;
3835 /* check for exists after object is locked */
3836 if (!mdt_object_exists(obj))
3837 /* Non-existent object shouldn't have PDO lock */
3840 /* Non-dir object shouldn't have PDO lock */
3841 if (!S_ISDIR(lu_object_attr(&obj->mot_obj)))
3845 LASSERT(mode == LCK_PW || mode == LCK_EX);
3846 dlmflags |= LDLM_FL_COS_INCOMPAT;
3847 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3848 dlmflags |= LDLM_FL_COS_ENABLED;
3851 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
3852 policy->l_inodebits.try_bits = 0;
3853 policy->l_inodebits.li_gid = 0;
3854 fid_build_reg_res_name(mdt_object_fid(obj), res_id);
3856 cookie = &info->mti_exp->exp_handle.h_cookie;
3858 mdt_lock_pdo_init(lh, mode, name);
3859 mdt_lock_pdo_mode(info, obj, lh);
3860 if (lh->mlh_pdo_mode != LCK_NL) {
3862 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3863 lh->mlh_pdo_mode, policy, res_id,
3866 mdt_object_unlock(info, obj, lh, 1);
3870 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3873 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3874 policy, res_id, dlmflags, cookie);
3876 mdt_object_unlock(info, obj, lh, 1);
3877 else if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK) &&
3878 lh->mlh_pdo_hash != 0 &&
3879 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3880 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3885 int mdt_object_lock_internal(struct mdt_thread_info *info,
3886 struct mdt_object *obj, const struct lu_fid *fid,
3887 struct mdt_lock_handle *lh, __u64 *ibits,
3888 __u64 trybits, bool cache, bool cos_incompat)
3890 union ldlm_policy_data *policy = &info->mti_policy;
3891 struct ldlm_res_id *res_id = &info->mti_res_id;
3892 struct lustre_handle *handle;
3895 policy->l_inodebits.bits = *ibits;
3896 policy->l_inodebits.try_bits = trybits;
3897 policy->l_inodebits.li_gid = lh->mlh_gid;
3898 fid_build_reg_res_name(fid, res_id);
3900 if (obj && mdt_object_remote(obj)) {
3901 handle = &lh->mlh_rreg_lh;
3902 LASSERT(!lustre_handle_is_used(handle));
3903 LASSERT(lh->mlh_rreg_mode != LCK_MINMODE);
3904 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3905 rc = mdt_remote_object_lock_try(info, obj, handle,
3906 lh->mlh_rreg_mode, policy,
3909 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3911 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if
3912 * it is going to be sent to client. If it is -
3913 * mdt_intent_policy() path will fix it up and turn FL_LOCAL
3916 __u64 dlmflags = LDLM_FL_ATOMIC_CB | LDLM_FL_LOCAL_ONLY;
3917 __u64 *cookie = NULL;
3919 handle = &lh->mlh_reg_lh;
3920 LASSERT(!lustre_handle_is_used(handle));
3921 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3922 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3925 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3926 lh->mlh_reg_mode == LCK_EX);
3927 dlmflags |= LDLM_FL_COS_INCOMPAT;
3928 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3929 dlmflags |= LDLM_FL_COS_ENABLED;
3932 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3933 if (lh->mlh_type == MDT_REG_LOCK &&
3934 lh->mlh_reg_mode == LCK_EX && *ibits == MDS_INODELOCK_OPEN)
3935 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3939 cookie = &info->mti_exp->exp_handle.h_cookie;
3941 rc = mdt_fid_lock(info->mti_env, ns, handle, lh->mlh_reg_mode,
3942 policy, res_id, dlmflags, cookie);
3944 mdt_object_unlock(info, obj, lh, 1);
3948 struct ldlm_lock *lock;
3950 /* Return successfully acquired bits to a caller */
3951 lock = ldlm_handle2lock(handle);
3953 *ibits = lock->l_policy_data.l_inodebits.bits;
3954 LDLM_LOCK_PUT(lock);
3961 * MDT object locking functions:
3962 * mdt_object_lock(): lock object, this is used in most places, and normally
3963 * lock ibits doesn't contain LOOKUP, unless the caller knows it's not
3965 * mdt_object_check_lock(): lock object with LOOKUP and other ibits, it needs
3966 * to check whether parent is on remote MDT, if so, take LOOKUP on parent
3967 * MDT separately, and then lock other ibits on child object.
3968 * mdt_parent_lock(): take parent UPDATE lock with specific mode, if parent is
3969 * local, take PDO lock by name hash, otherwise take regular lock.
3970 * mdt_object_stripes_lock(): lock object which should be local, and if it's a
3971 * striped directory, lock its stripes, this is called in operations which
3972 * modify both object and stripes.
3973 * mdt_object_lock_try(): lock object with trybits, the trybits contains
3974 * optional inode lock bits that can be granted. This is called by
3975 * getattr/open to fetch more inode lock bits to client, and is also called
3976 * by dir migration to lock link parent in non-block mode to avoid
3983 * this is used to lock object in most places, and normally lock ibits doesn't
3984 * contain LOOKUP, unless the caller knows it's not remote object.
3986 * \param info struct mdt_thread_info
3988 * \param lh lock handle
3989 * \param ibits MDS inode lock bits
3990 * \param mode lock mode
3991 * \param cos_incompat DNE COS incompatible
3993 * \retval 0 on success, -ev on error.
3995 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *obj,
3996 struct mdt_lock_handle *lh, __u64 ibits,
3997 enum ldlm_mode mode, bool cos_incompat)
4002 mdt_lock_reg_init(lh, mode);
4003 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh,
4004 &ibits, 0, false, cos_incompat);
4009 * lock object with LOOKUP and other ibits
4011 * it will check whether parent and child are on different MDTs, if so, take
4012 * LOOKUP lock on parent MDT, and lock other ibits on child MDT, otherwise lock
4013 * all ibits on child MDT. Note, parent and child shouldn't be both on remote
4014 * MDTs, in which case specific lock function should be used, and it's in
4015 * rename and migrate only.
4017 * \param info struct mdt_thread_info
4018 * \param parent parent object
4019 * \param child child object
4020 * \param lh lock handle
4021 * \param ibits MDS inode lock bits
4022 * \param mode lock mode
4023 * \param cos_incompat DNE COS incompatible
4025 * \retval 0 on success, -ev on error.
4027 int mdt_object_check_lock(struct mdt_thread_info *info,
4028 struct mdt_object *parent, struct mdt_object *child,
4029 struct mdt_lock_handle *lh, __u64 ibits,
4030 enum ldlm_mode mode, bool cos_incompat)
4035 /* if LOOKUP ibit is not set, use mdt_object_lock() */
4036 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
4037 /* if only LOOKUP ibit is needed, use mdt_object_lookup_lock() */
4038 LASSERT(ibits != MDS_INODELOCK_LOOKUP);
4040 /* @parent and @child shouldn't both be on remote MDTs */
4041 LASSERT(!(mdt_object_remote(parent) && mdt_object_remote(child)));
4043 mdt_lock_reg_init(lh, mode);
4044 if (mdt_object_remote(parent) ^ mdt_object_remote(child)) {
4045 __u64 lookup_ibits = MDS_INODELOCK_LOOKUP;
4047 rc = mdt_object_lock_internal(info, parent,
4048 mdt_object_fid(child), lh,
4049 &lookup_ibits, 0, false,
4054 ibits &= ~MDS_INODELOCK_LOOKUP;
4057 rc = mdt_object_lock_internal(info, child, mdt_object_fid(child), lh,
4058 &ibits, 0, false, cos_incompat);
4059 if (rc && !(ibits & MDS_INODELOCK_LOOKUP))
4060 mdt_object_unlock(info, NULL, lh, 1);
4066 * take parent UPDATE lock
4068 * if parent is local, take PDO lock by name hash, otherwise take regular lock.
4070 * \param info struct mdt_thread_info
4071 * \param obj parent object
4072 * \param lh lock handle
4073 * \param lname child name
4074 * \param mode lock mode
4075 * \param cos_incompat DNE COS incompatible
4077 * \retval 0 on success, -ev on error.
4079 int mdt_parent_lock(struct mdt_thread_info *info, struct mdt_object *obj,
4080 struct mdt_lock_handle *lh, const struct lu_name *lname,
4081 enum ldlm_mode mode, bool cos_incompat)
4086 LASSERT(obj && lname);
4087 if (mdt_object_remote(obj)) {
4088 __u64 ibits = MDS_INODELOCK_UPDATE;
4090 mdt_lock_reg_init(lh, mode);
4091 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj),
4092 lh, &ibits, 0, false,
4095 rc = mdt_object_pdo_lock(info, obj, lh, lname, mode, true,
4102 * lock object with trybits
4104 * the trybits contains optional inode lock bits that can be granted. This is
4105 * called by getattr/open to fetch more inode lock bits to client, and is also
4106 * called by dir migration to lock link parent in non-block mode to avoid
4109 * \param info struct mdt_thread_info
4111 * \param lh lock handle
4112 * \param ibits MDS inode lock bits
4113 * \param trybits optional inode lock bits
4114 * \param mode lock mode
4115 * \param cos_incompat DNE COS incompatible
4117 * \retval 0 on success, -ev on error.
4119 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *obj,
4120 struct mdt_lock_handle *lh, __u64 *ibits,
4121 __u64 trybits, enum ldlm_mode mode, bool cos_incompat)
4123 bool trylock_only = *ibits == 0;
4127 LASSERT(!(*ibits & trybits));
4128 mdt_lock_reg_init(lh, mode);
4129 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh, ibits,
4130 trybits, false, cos_incompat);
4131 if (rc && trylock_only) { /* clear error for try ibits lock only */
4132 LASSERT(*ibits == 0);
4139 * Helper function to take \a obj LOOKUP lock.
4141 * Both \a pobj and \a obj may be located on remote MDTs.
4143 int mdt_object_lookup_lock(struct mdt_thread_info *info,
4144 struct mdt_object *pobj, struct mdt_object *obj,
4145 struct mdt_lock_handle *lh, enum ldlm_mode mode,
4148 __u64 ibits = MDS_INODELOCK_LOOKUP;
4152 /* if @parent is NULL, it's on local MDT, and @child is remote,
4153 * this is case in getattr/unlink/open by name.
4155 LASSERT(ergo(!pobj, mdt_object_remote(obj)));
4156 mdt_lock_reg_init(lh, mode);
4157 rc = mdt_object_lock_internal(info, pobj, mdt_object_fid(obj), lh,
4158 &ibits, 0, false, cos_incompat);
4163 * Save a lock within request object.
4165 * Keep the lock referenced until whether client ACK or transaction
4166 * commit happens or release the lock immediately depending on input
4167 * parameters. If COS is ON, a write lock is converted to COS lock
4170 * \param info thead info object
4171 * \param h lock handle
4172 * \param mode lock mode
4173 * \param decref force immediate lock releasing
4175 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
4176 enum ldlm_mode mode, int decref)
4178 struct tgt_session_info *tsi = info->mti_env->le_ses ?
4179 tgt_ses_info(info->mti_env) : NULL;
4183 if (lustre_handle_is_used(h)) {
4184 bool has_trans = tsi && tsi->tsi_has_trans;
4186 if (decref || !has_trans || !(mode & (LCK_PW | LCK_EX))) {
4187 mdt_fid_unlock(h, mode);
4189 struct mdt_device *mdt = info->mti_mdt;
4190 struct ldlm_lock *lock = ldlm_handle2lock(h);
4191 struct ptlrpc_request *req = mdt_info_req(info);
4192 bool cos = mdt_cos_is_enabled(mdt);
4193 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
4195 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
4198 /* there is no request if mdt_object_unlock() is called
4199 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
4200 if (likely(req != NULL)) {
4201 LDLM_DEBUG(lock, "save lock request %p reply "
4202 "state %p transno %lld\n", req,
4203 req->rq_reply_state, req->rq_transno);
4205 ldlm_lock_mode_downgrade(lock, LCK_COS);
4208 if (req->rq_export->exp_disconnected)
4209 mdt_fid_unlock(h, mode);
4211 ptlrpc_save_lock(req, h, mode, cos,
4214 mdt_fid_unlock(h, mode);
4217 if (mdt_is_lock_sync(lock)) {
4218 CDEBUG(D_HA, "sync_lock, do async commit\n");
4219 mdt_device_commit_async(info->mti_env, mdt);
4221 LDLM_LOCK_PUT(lock);
4230 * Save cross-MDT lock in uncommitted_slc_locks
4232 * Keep the lock referenced until transaction commit happens or release the lock
4233 * immediately depending on input parameters.
4235 * \param info thead info object
4236 * \param h lock handle
4237 * \param mode lock mode
4238 * \param decref force immediate lock releasing
4240 static void mdt_save_remote_lock(struct mdt_thread_info *info,
4241 struct mdt_object *o, struct lustre_handle *h,
4242 enum ldlm_mode mode, int decref)
4246 if (lustre_handle_is_used(h)) {
4247 struct ldlm_lock *lock = ldlm_handle2lock(h);
4248 struct ptlrpc_request *req = mdt_info_req(info);
4251 (lock->l_policy_data.l_inodebits.bits &
4252 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
4253 mo_invalidate(info->mti_env, mdt_object_child(o));
4255 if (decref || !req || !(mode & (LCK_PW | LCK_EX)) ||
4256 !tgt_ses_info(info->mti_env)->tsi_has_trans) {
4257 ldlm_lock_decref_and_cancel(h, mode);
4258 LDLM_LOCK_PUT(lock);
4260 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
4262 ldlm_lock_decref(h, mode);
4271 * Unlock mdt object.
4273 * Immeditely release the regular lock and the PDO lock or save the
4274 * lock in request and keep them referenced until client ACK or
4275 * transaction commit.
4277 * \param info thread info object
4278 * \param o mdt object
4279 * \param lh mdt lock handle referencing regular and PDO locks
4280 * \param decref force immediate lock releasing
4282 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
4284 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
4285 struct mdt_lock_handle *lh, int decref)
4289 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
4290 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
4291 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4297 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4298 const struct lu_fid *f,
4299 struct mdt_lock_handle *lh,
4300 __u64 ibits, enum ldlm_mode mode)
4302 struct mdt_object *o;
4304 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4308 rc = mdt_object_lock(info, o, lh, ibits, mode, false);
4310 mdt_object_put(info->mti_env, o);
4317 void mdt_object_unlock_put(struct mdt_thread_info * info,
4318 struct mdt_object *o,
4319 struct mdt_lock_handle *lh,
4322 mdt_object_unlock(info, o, lh, decref);
4323 mdt_object_put(info->mti_env, o);
4327 * Generic code handling requests that have struct mdt_body passed in:
4329 * - extract mdt_body from request and save it in @info, if present;
4331 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4334 * - if HAS_BODY flag is set for this request type check whether object
4335 * actually exists on storage (lu_object_exists()).
4338 static int mdt_body_unpack(struct mdt_thread_info *info,
4339 enum tgt_handler_flags flags)
4341 const struct mdt_body *body;
4342 struct mdt_object *obj;
4343 const struct lu_env *env;
4344 struct req_capsule *pill;
4348 env = info->mti_env;
4349 pill = info->mti_pill;
4351 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4355 if (!(body->mbo_valid & OBD_MD_FLID))
4358 if (!fid_is_sane(&body->mbo_fid1)) {
4359 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4363 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4365 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4366 mdt_object_put(env, obj);
4369 info->mti_object = obj;
4378 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4379 enum tgt_handler_flags flags)
4381 struct req_capsule *pill = info->mti_pill;
4386 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4387 rc = mdt_body_unpack(info, flags);
4391 if (rc == 0 && (flags & HAS_REPLY)) {
4393 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4394 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4395 req_capsule_ptlreq(pill) ?
4396 DEF_REP_MD_SIZE : MAX_MD_SIZE_OLD);
4398 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4399 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4402 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4403 * by default. If the target object has more ACL entries, then
4404 * enlarge the buffer when necessary. */
4405 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4406 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4407 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4409 mdt_preset_secctx_size(info);
4410 mdt_preset_encctx_size(info);
4412 rc = req_capsule_server_pack(pill);
4414 CWARN("%s: cannot pack response: rc = %d\n",
4415 mdt_obd_name(info->mti_mdt), rc);
4420 void mdt_thread_info_reset(struct mdt_thread_info *info)
4422 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4423 info->mti_body = NULL;
4424 info->mti_dlm_req = NULL;
4425 info->mti_cross_ref = 0;
4426 info->mti_opdata = 0;
4427 info->mti_big_lmm_used = 0;
4428 info->mti_big_acl_used = 0;
4429 info->mti_som_strict = 0;
4431 info->mti_spec.no_create = 0;
4432 info->mti_spec.sp_rm_entry = 0;
4433 info->mti_spec.sp_permitted = 0;
4435 info->mti_spec.u.sp_ea.eadata = NULL;
4436 info->mti_spec.u.sp_ea.eadatalen = 0;
4438 if (info->mti_batch_env && info->mti_object != NULL) {
4439 mdt_object_put(info->mti_env, info->mti_object);
4440 info->mti_object = NULL;
4445 * Initialize fields of struct mdt_thread_info. Other fields are left in
4446 * uninitialized state, because it's too expensive to zero out whole
4447 * mdt_thread_info (> 1K) on each request arrival.
4449 void mdt_thread_info_init(struct ptlrpc_request *req,
4450 struct mdt_thread_info *info)
4452 info->mti_pill = &req->rq_pill;
4454 /* mdt device: it can be NULL while CONNECT */
4455 if (req->rq_export) {
4456 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4457 info->mti_exp = req->rq_export;
4459 info->mti_mdt = NULL;
4460 info->mti_env = req->rq_svc_thread->t_env;
4461 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4462 info->mti_big_buf = LU_BUF_NULL;
4463 info->mti_batch_env = 0;
4464 info->mti_object = NULL;
4466 mdt_thread_info_reset(info);
4469 void mdt_thread_info_fini(struct mdt_thread_info *info)
4473 if (info->mti_object != NULL) {
4474 mdt_object_put(info->mti_env, info->mti_object);
4475 info->mti_object = NULL;
4478 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4479 mdt_lock_handle_assert(&info->mti_lh[i]);
4480 info->mti_env = NULL;
4481 info->mti_pill = NULL;
4482 info->mti_exp = NULL;
4483 info->mti_mdt = NULL;
4485 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4486 lu_buf_free(&info->mti_big_buf);
4489 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4491 struct mdt_thread_info *mti;
4493 mti = mdt_th_info(tsi->tsi_env);
4494 LASSERT(mti != NULL);
4496 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4497 if (tsi->tsi_corpus != NULL) {
4498 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4499 lu_object_get(tsi->tsi_corpus);
4501 mti->mti_body = tsi->tsi_mdt_body;
4502 mti->mti_dlm_req = tsi->tsi_dlm_req;
4507 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4509 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4511 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4512 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4514 return tgt_connect(tsi);
4517 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4518 struct mdt_thread_info *info,
4519 struct ldlm_lock **lockp, __u64 flags)
4521 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4524 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4525 struct mdt_thread_info *info,
4526 struct ldlm_lock **lockp, __u64 flags)
4528 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4532 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4533 struct ldlm_lock **lockp,
4534 struct mdt_lock_handle *lh,
4535 __u64 flags, int result)
4537 struct ptlrpc_request *req = mdt_info_req(info);
4538 struct ldlm_lock *lock = *lockp;
4539 struct ldlm_lock *new_lock;
4541 /* If possible resent found a lock, @lh is set to its handle */
4542 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4544 if (new_lock == NULL) {
4545 if (flags & LDLM_FL_INTENT_ONLY) {
4547 } else if (flags & LDLM_FL_RESENT) {
4548 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4549 * resend case, however, it could be already destroyed
4550 * due to client eviction or a raced cancel RPC.
4552 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4553 lh->mlh_reg_lh.cookie);
4556 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4557 mdt_obd_name(info->mti_mdt),
4558 lh->mlh_reg_lh.cookie, flags,
4559 PFID(&info->mti_tmp_fid1),
4560 PFID(&info->mti_tmp_fid2), result);
4563 lh->mlh_reg_lh.cookie = 0;
4568 * If we've already given this lock to a client once, then we should
4569 * have no readers or writers. Otherwise, we should have one reader
4570 * _or_ writer ref (which will be zeroed below) before returning the
4573 if (new_lock->l_export == req->rq_export) {
4574 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4576 LASSERT(new_lock->l_export == NULL);
4577 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4582 if (new_lock->l_export == req->rq_export) {
4584 * Already gave this to the client, which means that we
4585 * reconstructed a reply.
4587 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4590 LDLM_LOCK_RELEASE(new_lock);
4591 lh->mlh_reg_lh.cookie = 0;
4592 RETURN(ELDLM_LOCK_REPLACED);
4596 * Fixup the lock to be given to the client.
4598 lock_res_and_lock(new_lock);
4599 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4600 * possible blocking AST. */
4601 while (new_lock->l_readers > 0) {
4602 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4603 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4604 new_lock->l_readers--;
4606 while (new_lock->l_writers > 0) {
4607 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4608 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4609 new_lock->l_writers--;
4612 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4613 new_lock->l_blocking_ast = lock->l_blocking_ast;
4614 new_lock->l_completion_ast = lock->l_completion_ast;
4615 if (ldlm_has_dom(new_lock))
4616 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4617 new_lock->l_remote_handle = lock->l_remote_handle;
4618 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4620 unlock_res_and_lock(new_lock);
4622 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4623 &new_lock->l_remote_handle,
4624 &new_lock->l_exp_hash);
4626 LDLM_LOCK_RELEASE(new_lock);
4627 lh->mlh_reg_lh.cookie = 0;
4629 RETURN(ELDLM_LOCK_REPLACED);
4632 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4633 struct ldlm_lock *new_lock,
4634 struct mdt_lock_handle *lh, __u64 flags)
4636 struct ptlrpc_request *req = mdt_info_req(info);
4637 struct ldlm_request *dlmreq;
4639 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4642 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4644 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4645 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4647 if (flags & LDLM_FL_RESENT) {
4648 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4649 lh->mlh_reg_mode = new_lock->l_granted_mode;
4651 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4652 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4653 lh->mlh_reg_lh.cookie);
4658 * If the xid matches, then we know this is a resent request, and allow
4659 * it. (It's probably an OPEN, for which we don't send a lock.
4661 if (req_can_reconstruct(req, NULL) != 0)
4665 * This remote handle isn't enqueued, so we never received or processed
4666 * this request. Clear MSG_RESENT, because it can be handled like any
4667 * normal request now.
4669 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4671 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4672 dlmreq->lock_handle[0].cookie);
4675 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4676 struct mdt_thread_info *info,
4677 struct ldlm_lock **lockp,
4680 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4681 struct ldlm_reply *ldlm_rep = NULL;
4686 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4687 * (for the resend case) or a new lock. Below we will use it to
4688 * replace the original lock.
4690 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4691 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4692 rc = mdt_object_lock(info, info->mti_object, lhc,
4693 MDS_INODELOCK_XATTR, (*lockp)->l_req_mode,
4699 rc = mdt_getxattr(info);
4701 if (mdt_info_req(info)->rq_repmsg != NULL)
4702 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4704 if (ldlm_rep == NULL ||
4705 CFS_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4706 mdt_object_unlock(info, info->mti_object, lhc, 1);
4710 RETURN(err_serious(-EFAULT));
4713 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4715 /* This is left for interop instead of adding a new interop flag.
4717 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4718 if (ldlm_rep->lock_policy_res2) {
4719 mdt_object_unlock(info, info->mti_object, lhc, 1);
4720 RETURN(ELDLM_LOCK_ABORTED);
4724 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4728 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4729 struct mdt_thread_info *info,
4730 struct ldlm_lock **lockp,
4733 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4735 struct ldlm_reply *ldlm_rep;
4736 struct mdt_body *reqbody;
4737 struct mdt_body *repbody;
4741 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4744 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4747 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4748 repbody->mbo_eadatasize = 0;
4749 repbody->mbo_aclsize = 0;
4753 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4756 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4760 CERROR("%s: unsupported intent %#x\n",
4761 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4762 GOTO(out_shrink, rc = -EINVAL);
4765 rc = mdt_init_ucred(info, reqbody);
4767 GOTO(out_shrink, rc);
4769 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4770 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4772 /* Get lock from request for possible resent case. */
4773 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4775 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4776 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4778 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG) &&
4779 ldlm_rep->lock_policy_res2 != -ENOKEY)
4780 ldlm_rep->lock_policy_res2 = 0;
4781 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4782 ldlm_rep->lock_policy_res2) {
4783 lhc->mlh_reg_lh.cookie = 0ull;
4784 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4787 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4790 mdt_exit_ucred(info);
4792 mdt_client_compatibility(info);
4793 rc2 = mdt_fix_reply(info);
4799 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4800 struct mdt_thread_info *info,
4801 struct ldlm_lock **lockp,
4804 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4805 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4806 struct layout_intent *intent;
4807 struct ldlm_reply *ldlm_rep;
4808 struct lu_fid *fid = &info->mti_tmp_fid2;
4809 struct mdt_object *obj = NULL;
4810 int layout_size = 0;
4811 struct lu_buf *buf = &layout.mlc_buf;
4816 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4818 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4822 CDEBUG(D_INFO, DFID "got layout change request from client: "
4823 "opc:%u flags:%#x extent "DEXT"\n",
4824 PFID(fid), intent->li_opc, intent->li_flags,
4825 PEXT(&intent->li_extent));
4827 switch (intent->li_opc) {
4828 case LAYOUT_INTENT_TRUNC:
4829 case LAYOUT_INTENT_WRITE:
4830 layout.mlc_opc = MD_LAYOUT_WRITE;
4831 layout.mlc_intent = intent;
4833 case LAYOUT_INTENT_ACCESS:
4835 case LAYOUT_INTENT_READ:
4836 case LAYOUT_INTENT_GLIMPSE:
4837 case LAYOUT_INTENT_RELEASE:
4838 case LAYOUT_INTENT_RESTORE:
4839 CERROR("%s: Unsupported layout intent opc %d\n",
4840 mdt_obd_name(info->mti_mdt), intent->li_opc);
4843 CERROR("%s: Unknown layout intent opc %d\n",
4844 mdt_obd_name(info->mti_mdt), intent->li_opc);
4848 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4850 RETURN(PTR_ERR(obj));
4852 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4853 /* if layout is going to be changed don't use the current EA
4854 * size but the maximum one. That buffer will be shrinked
4855 * to the actual size in req_capsule_shrink() before reply.
4857 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4858 layout_size = info->mti_mdt->mdt_max_mdsize;
4860 layout_size = mdt_attr_get_eabuf_size(info, obj);
4861 if (layout_size < 0)
4862 GOTO(out, rc = layout_size);
4864 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4865 info->mti_mdt->mdt_max_mdsize = layout_size;
4867 CDEBUG(D_INFO, "%s: layout_size %d\n",
4868 mdt_obd_name(info->mti_mdt), layout_size);
4872 * set reply buffer size, so that ldlm_handle_enqueue0()->
4873 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4875 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4877 rc = req_capsule_server_pack(info->mti_pill);
4881 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4883 GOTO(out, rc = -EPROTO);
4885 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4887 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4888 if (layout.mlc_opc == MD_LAYOUT_NOP)
4891 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4895 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4896 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4902 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4903 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4905 buf->lb_len = req_capsule_get_size(info->mti_pill,
4906 &RMF_EADATA, RCL_CLIENT);
4908 * If it's a replay of layout write intent RPC, the client has
4909 * saved the extended lovea when it get reply then.
4911 if (buf->lb_len > 0)
4912 mdt_fix_lov_magic(info, buf->lb_buf);
4915 /* Get lock from request for possible resent case. */
4916 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4917 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4920 * Instantiate some layout components, if @buf contains lovea, then it's
4921 * a replay of the layout intent write RPC.
4923 rc = mdt_layout_change(info, obj, lhc, &layout);
4924 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4926 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4927 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4928 if (rc == ELDLM_LOCK_REPLACED &&
4929 (*lockp)->l_granted_mode == LCK_EX)
4930 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4935 mdt_object_put(info->mti_env, obj);
4939 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4940 struct mdt_thread_info *info,
4941 struct ldlm_lock **lockp,
4944 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4945 struct ldlm_reply *rep = NULL;
4948 struct ptlrpc_request *req = mdt_info_req(info);
4950 static const struct req_format *intent_fmts[REINT_MAX] = {
4951 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4952 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4957 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4961 /* Get lock from request for possible resent case. */
4962 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4964 rc = mdt_reint_internal(info, lhc, opc);
4966 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4967 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4969 /* Check whether the reply has been packed successfully. */
4970 if (mdt_info_req(info)->rq_repmsg != NULL)
4971 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4976 RETURN(err_serious(-EFAULT));
4979 /* MDC expects this in any case */
4981 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4983 /* the open lock or the lock for cross-ref object should be
4984 * returned to the client */
4985 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4986 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4987 rep->lock_policy_res2 = 0;
4988 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4992 rep->lock_policy_res2 = clear_serious(rc);
4994 if (rep->lock_policy_res2 == -ENOENT &&
4995 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4996 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4997 rep->lock_policy_res2 = 0;
4999 lhc->mlh_reg_lh.cookie = 0ull;
5000 if (rc == -ENOTCONN || rc == -ENODEV ||
5001 rc == -EOVERFLOW) { /**< if VBR failure then return error */
5003 * If it is the disconnect error (ENODEV & ENOCONN), the error
5004 * will be returned by rq_status, and client at ptlrpc layer
5005 * will detect this, then disconnect, reconnect the import
5006 * immediately, instead of impacting the following the rpc.
5011 * For other cases, the error will be returned by intent, and client
5012 * will retrieve the result from intent.
5014 RETURN(ELDLM_LOCK_ABORTED);
5017 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
5018 struct mdt_thread_info *info,
5019 struct ldlm_lock **lockp,
5020 u64 flags /* LDLM_FL_* */)
5022 struct req_capsule *pill = info->mti_pill;
5023 struct ptlrpc_request *req = mdt_info_req(info);
5024 const struct req_format *it_format;
5025 int (*it_handler)(enum ldlm_intent_flags,
5026 struct mdt_thread_info *,
5027 struct ldlm_lock **,
5029 enum tgt_handler_flags it_handler_flags = 0;
5030 struct ldlm_reply *rep;
5031 bool check_mdt_object = false;
5037 case IT_OPEN|IT_CREAT:
5039 * OCREAT is not a IS_MUTABLE request since the file may
5040 * already exist. We do the extra check of
5041 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
5042 * really need to create the object.
5044 it_format = &RQF_LDLM_INTENT;
5045 it_handler = &mdt_intent_open;
5048 check_mdt_object = true;
5051 it_format = &RQF_LDLM_INTENT_GETATTR;
5052 it_handler = &mdt_intent_getattr;
5053 it_handler_flags = HAS_REPLY;
5056 check_mdt_object = true;
5057 it_format = &RQF_LDLM_INTENT_GETXATTR;
5058 it_handler = &mdt_intent_getxattr;
5059 it_handler_flags = HAS_BODY;
5062 it_format = &RQF_LDLM_INTENT_LAYOUT;
5063 it_handler = &mdt_intent_layout;
5066 it_format = &RQF_LDLM_INTENT;
5067 it_handler = &mdt_intent_glimpse;
5070 it_format = &RQF_LDLM_INTENT;
5071 it_handler = &mdt_intent_brw;
5073 case IT_QUOTA_DQACQ:
5074 case IT_QUOTA_CONN: {
5075 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
5078 RETURN(-EOPNOTSUPP);
5080 if (mdt_rdonly(req->rq_export))
5083 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
5084 /* pass the request to quota master */
5085 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
5086 mdt_info_req(info), lockp,
5091 CERROR("%s: unknown intent code %#x\n",
5092 mdt_obd_name(info->mti_mdt), it_opc);
5096 if (!info->mti_batch_env)
5097 req_capsule_extend(pill, it_format);
5099 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
5103 if (unlikely(info->mti_object == NULL && check_mdt_object))
5106 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
5109 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
5111 /* execute policy */
5112 rc = (*it_handler)(it_opc, info, lockp, flags);
5114 /* Check whether the reply has been packed successfully. */
5115 if (info->mti_batch_env || req->rq_repmsg != NULL) {
5116 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
5117 rep->lock_policy_res2 =
5118 ptlrpc_status_hton(rep->lock_policy_res2);
5124 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
5125 enum ldlm_intent_flags it_opc)
5127 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
5129 /* update stats when IT code is known */
5130 if (srv_stats != NULL)
5131 lprocfs_counter_incr(srv_stats,
5132 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
5133 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
5136 static int mdt_intent_policy(const struct lu_env *env,
5137 struct ldlm_namespace *ns,
5138 struct ldlm_lock **lockp,
5140 enum ldlm_mode mode,
5141 __u64 flags, void *data)
5143 struct tgt_session_info *tsi;
5144 struct mdt_thread_info *info;
5145 struct ptlrpc_request *req = req_cookie;
5146 struct ldlm_intent *it;
5147 struct req_capsule *pill;
5148 const struct ldlm_lock_desc *ldesc;
5153 LASSERT(req != NULL);
5155 tsi = tgt_ses_info(env);
5157 info = mdt_th_info(env);
5158 LASSERT(info != NULL);
5160 /* Check whether it is a sub request processing in a batch request */
5161 if (info->mti_batch_env) {
5162 pill = info->mti_pill;
5163 LASSERT(pill == &info->mti_sub_pill);
5165 info = tsi2mdt_info(tsi);
5166 pill = info->mti_pill;
5169 LASSERT(pill->rc_req == req);
5170 ldesc = &info->mti_dlm_req->lock_desc;
5172 if (info->mti_batch_env ||
5173 req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
5175 * For batch processing environment, the request format has
5178 if (!info->mti_batch_env)
5179 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
5181 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
5183 mdt_ptlrpc_stats_update(req, it->opc);
5184 rc = mdt_intent_opc(it->opc, info, lockp, flags);
5188 /* Lock without inodebits makes no sense and will oops
5189 * later in ldlm. Let's check it now to see if we have
5190 * ibits corrupted somewhere in mdt_intent_opc().
5191 * The case for client miss to set ibits has been
5192 * processed by others. */
5193 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
5194 ldesc->l_policy_data.l_inodebits.bits != 0));
5196 rc = err_serious(-EFAULT);
5198 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
5199 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
5200 struct ldlm_reply *rep;
5202 /* No intent was provided but INTENT flag is set along with
5203 * DOM bit, this is considered as GLIMPSE request.
5204 * This logic is common for MDT and OST glimpse
5206 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
5207 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
5208 /* Check whether the reply has been packed successfully. */
5209 if (req->rq_repmsg != NULL) {
5210 rep = req_capsule_server_get(info->mti_pill,
5212 rep->lock_policy_res2 =
5213 ptlrpc_status_hton(rep->lock_policy_res2);
5216 /* No intent was provided */
5217 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
5218 rc = req_capsule_server_pack(pill);
5220 rc = err_serious(rc);
5223 if (!info->mti_batch_env)
5224 mdt_thread_info_fini(info);
5228 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
5230 struct seq_server_site *ss = mdt_seq_site(mdt);
5232 if (ss->ss_node_id == 0)
5235 if (ss->ss_client_seq != NULL) {
5236 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5237 ss->ss_client_seq->lcs_exp = NULL;
5240 if (ss->ss_server_fld != NULL) {
5241 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
5242 ss->ss_server_fld->lsf_control_exp = NULL;
5246 static void mdt_seq_fini_cli(struct mdt_device *mdt)
5248 struct seq_server_site *ss = mdt_seq_site(mdt);
5253 if (ss->ss_server_seq != NULL)
5254 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
5257 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
5259 mdt_seq_fini_cli(mdt);
5260 mdt_deregister_seq_exp(mdt);
5262 return seq_site_fini(env, mdt_seq_site(mdt));
5266 * It will retrieve its FLDB entries from MDT0, and it only happens
5267 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
5268 * and it needs to refresh FLDB from the MDT0.
5270 static int mdt_register_lwp_callback(void *data)
5273 struct mdt_device *mdt = data;
5274 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
5278 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
5280 rc = lu_env_init(&env, LCT_MD_THREAD);
5282 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
5286 /* Allocate new sequence now to avoid creating local transaction
5287 * in the normal transaction process */
5288 rc = seq_server_check_and_alloc_super(&env,
5289 mdt_seq_site(mdt)->ss_server_seq);
5294 rc = fld_update_from_controller(&env, fld);
5296 CERROR("%s: cannot update controller: rc = %d\n",
5297 mdt_obd_name(mdt), rc);
5306 static int mdt_register_seq_exp(struct mdt_device *mdt)
5308 struct seq_server_site *ss = mdt_seq_site(mdt);
5309 char *lwp_name = NULL;
5312 if (ss->ss_node_id == 0)
5315 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5316 if (lwp_name == NULL)
5317 GOTO(out_free, rc = -ENOMEM);
5319 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5323 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5328 rc = lustre_register_lwp_item(lwp_name,
5329 &ss->ss_server_fld->lsf_control_exp,
5330 mdt_register_lwp_callback, mdt);
5332 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5333 ss->ss_client_seq->lcs_exp = NULL;
5337 if (lwp_name != NULL)
5338 OBD_FREE(lwp_name, MAX_OBD_NAME);
5344 * Init client sequence manager which is used by local MDS to talk to sequence
5345 * controller on remote node.
5347 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5349 struct seq_server_site *ss = mdt_seq_site(mdt);
5353 /* check if this is adding the first MDC and controller is not yet
5355 OBD_ALLOC_PTR(ss->ss_client_seq);
5356 if (ss->ss_client_seq == NULL)
5359 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5360 if (prefix == NULL) {
5361 OBD_FREE_PTR(ss->ss_client_seq);
5362 ss->ss_client_seq = NULL;
5366 /* Note: seq_client_fini will be called in seq_site_fini */
5367 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5368 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5369 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5371 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5373 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5376 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5378 struct seq_server_site *ss;
5382 ss = mdt_seq_site(mdt);
5383 /* init sequence controller server(MDT0) */
5384 if (ss->ss_node_id == 0) {
5385 OBD_ALLOC_PTR(ss->ss_control_seq);
5386 if (ss->ss_control_seq == NULL)
5389 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5390 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5393 GOTO(out_seq_fini, rc);
5396 /* Init normal sequence server */
5397 OBD_ALLOC_PTR(ss->ss_server_seq);
5398 if (ss->ss_server_seq == NULL)
5399 GOTO(out_seq_fini, rc = -ENOMEM);
5401 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5402 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5404 GOTO(out_seq_fini, rc);
5406 /* init seq client for seq server to talk to seq controller(MDT0) */
5407 rc = mdt_seq_init_cli(env, mdt);
5409 GOTO(out_seq_fini, rc);
5411 if (ss->ss_node_id != 0)
5412 /* register controller export through lwp */
5413 rc = mdt_register_seq_exp(mdt);
5418 mdt_seq_fini(env, mdt);
5426 static int mdt_fld_fini(const struct lu_env *env,
5427 struct mdt_device *m)
5429 struct seq_server_site *ss = mdt_seq_site(m);
5432 if (ss && ss->ss_server_fld) {
5433 fld_server_fini(env, ss->ss_server_fld);
5434 OBD_FREE_PTR(ss->ss_server_fld);
5435 ss->ss_server_fld = NULL;
5441 static int mdt_fld_init(const struct lu_env *env,
5443 struct mdt_device *m)
5445 struct seq_server_site *ss;
5449 ss = mdt_seq_site(m);
5451 OBD_ALLOC_PTR(ss->ss_server_fld);
5452 if (ss->ss_server_fld == NULL)
5453 RETURN(rc = -ENOMEM);
5455 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5458 OBD_FREE_PTR(ss->ss_server_fld);
5459 ss->ss_server_fld = NULL;
5466 static void mdt_stack_pre_fini(const struct lu_env *env,
5467 struct mdt_device *m, struct lu_device *top)
5469 struct lustre_cfg_bufs *bufs;
5470 struct lustre_cfg *lcfg;
5471 struct mdt_thread_info *info;
5476 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5477 LASSERT(info != NULL);
5479 bufs = &info->mti_u.bufs;
5481 LASSERT(m->mdt_child_exp);
5482 LASSERT(m->mdt_child_exp->exp_obd);
5484 /* process cleanup, pass mdt obd name to get obd umount flags */
5485 /* XXX: this is needed because all layers are referenced by
5486 * objects (some of them are pinned by osd, for example *
5487 * the proper solution should be a model where object used
5488 * by osd only doesn't have mdt/mdd slices -bzzz */
5489 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5490 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5491 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5494 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5496 top->ld_ops->ldo_process_config(env, top, lcfg);
5497 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5501 static void mdt_stack_fini(const struct lu_env *env,
5502 struct mdt_device *m, struct lu_device *top)
5504 struct obd_device *obd = mdt2obd_dev(m);
5505 struct lustre_cfg_bufs *bufs;
5506 struct lustre_cfg *lcfg;
5507 struct mdt_thread_info *info;
5511 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5512 LASSERT(info != NULL);
5514 lu_dev_del_linkage(top->ld_site, top);
5516 lu_site_purge(env, top->ld_site, -1);
5518 bufs = &info->mti_u.bufs;
5519 /* process cleanup, pass mdt obd name to get obd umount flags */
5520 /* another purpose is to let all layers to release their objects */
5521 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5526 lustre_cfg_bufs_set_string(bufs, 1, flags);
5527 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5530 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5533 top->ld_ops->ldo_process_config(env, top, lcfg);
5534 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5536 lu_site_purge(env, top->ld_site, -1);
5538 m->mdt_child = NULL;
5539 m->mdt_bottom = NULL;
5541 obd_disconnect(m->mdt_child_exp);
5542 m->mdt_child_exp = NULL;
5544 obd_disconnect(m->mdt_bottom_exp);
5545 m->mdt_child_exp = NULL;
5548 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5549 const char *next, struct obd_export **exp)
5551 struct obd_connect_data *data = NULL;
5552 struct obd_device *obd;
5556 OBD_ALLOC_PTR(data);
5558 GOTO(out, rc = -ENOMEM);
5560 obd = class_name2obd(next);
5562 CERROR("%s: can't locate next device: %s\n",
5563 mdt_obd_name(m), next);
5564 GOTO(out, rc = -ENOTCONN);
5567 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5568 data->ocd_version = LUSTRE_VERSION_CODE;
5570 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5572 CERROR("%s: cannot connect to next dev %s (%d)\n",
5573 mdt_obd_name(m), next, rc);
5583 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5584 struct lustre_cfg *cfg)
5586 char *dev = lustre_cfg_string(cfg, 0);
5587 int rc, name_size, uuid_size;
5588 char *name, *uuid, *p;
5589 struct lustre_cfg_bufs *bufs;
5590 struct lustre_cfg *lcfg;
5591 struct obd_device *obd;
5592 struct lustre_profile *lprof;
5593 struct lu_site *site;
5596 /* in 1.8 we had the only device in the stack - MDS.
5597 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5598 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5599 * to generate names and setup MDT, MDD. MDT will be using
5600 * generated name to connect to MDD. for MDD the next device
5601 * will be LOD with name taken from so called "profile" which
5602 * is generated by mount_option line
5604 * 1.8 MGS generates config. commands like this:
5605 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5606 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5607 * 2.0 MGS generates config. commands like this:
5608 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5609 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5610 * 3:lustre-MDT0000-mdtlov 4:f
5612 * we generate MDD name from MDT one, just replacing T with D
5614 * after all the preparations, the logical equivalent will be
5615 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5616 * 3:lustre-MDT0000-mdtlov 4:f
5617 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5618 * 3:lustre-MDD0000 4:f
5620 * notice we build the stack from down to top: MDD first, then MDT */
5622 name_size = MAX_OBD_NAME;
5623 uuid_size = MAX_OBD_NAME;
5625 OBD_ALLOC(name, name_size);
5626 OBD_ALLOC(uuid, uuid_size);
5627 if (name == NULL || uuid == NULL)
5628 GOTO(cleanup_mem, rc = -ENOMEM);
5630 OBD_ALLOC_PTR(bufs);
5632 GOTO(cleanup_mem, rc = -ENOMEM);
5635 p = strstr(name, "-MDT");
5637 GOTO(free_bufs, rc = -ENOMEM);
5640 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5642 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5643 if (lprof == NULL || lprof->lp_dt == NULL) {
5644 CERROR("can't find the profile: %s\n",
5645 lustre_cfg_string(cfg, 0));
5646 GOTO(free_bufs, rc = -EINVAL);
5649 lustre_cfg_bufs_reset(bufs, name);
5650 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5651 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5652 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5654 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5656 GOTO(put_profile, rc = -ENOMEM);
5657 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5659 rc = class_attach(lcfg);
5661 GOTO(lcfg_cleanup, rc);
5663 obd = class_name2obd(name);
5665 CERROR("Can not find obd %s (%s in config)\n",
5666 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5667 GOTO(lcfg_cleanup, rc = -EINVAL);
5670 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5672 lustre_cfg_bufs_reset(bufs, name);
5673 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5674 lustre_cfg_bufs_set_string(bufs, 2, dev);
5675 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5677 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5679 GOTO(class_detach, rc = -ENOMEM);
5680 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5682 rc = class_setup(obd, lcfg);
5684 GOTO(class_detach, rc);
5686 /* connect to MDD we just setup */
5687 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5689 GOTO(class_detach, rc);
5691 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5693 LASSERT(mdt_lu_site(mdt) == NULL);
5694 mdt->mdt_lu_dev.ld_site = site;
5695 site->ls_top_dev = &mdt->mdt_lu_dev;
5696 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5698 /* now connect to bottom OSD */
5699 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5700 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5702 GOTO(class_detach, rc);
5704 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5706 rc = lu_env_refill((struct lu_env *)env);
5708 CERROR("Failure to refill session: '%d'\n", rc);
5710 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5715 class_detach(obd, lcfg);
5717 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5719 class_put_profile(lprof);
5724 OBD_FREE(name, name_size);
5726 OBD_FREE(uuid, uuid_size);
5730 /* setup quota master target on MDT0 */
5731 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5732 struct lustre_cfg *cfg)
5734 struct obd_device *obd;
5735 char *dev = lustre_cfg_string(cfg, 0);
5736 char *qmtname, *uuid, *p;
5737 struct lustre_cfg_bufs *bufs;
5738 struct lustre_cfg *lcfg;
5739 struct lustre_profile *lprof;
5740 struct obd_connect_data *data;
5744 LASSERT(mdt->mdt_qmt_exp == NULL);
5745 LASSERT(mdt->mdt_qmt_dev == NULL);
5747 /* quota master is on MDT0 only for now */
5748 if (mdt->mdt_seq_site.ss_node_id != 0)
5751 /* MGS generates config commands which look as follows:
5752 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5753 * 3:lustre-MDT0000-mdtlov 4:f
5755 * We generate the QMT name from the MDT one, just replacing MD with QM
5756 * after all the preparations, the logical equivalent will be:
5757 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5758 * 3:lustre-MDT0000-osd 4:f */
5759 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5760 OBD_ALLOC(uuid, UUID_MAX);
5761 OBD_ALLOC_PTR(bufs);
5762 OBD_ALLOC_PTR(data);
5763 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5764 GOTO(cleanup_mem, rc = -ENOMEM);
5766 strcpy(qmtname, dev);
5767 p = strstr(qmtname, "-MDT");
5769 GOTO(cleanup_mem, rc = -ENOMEM);
5770 /* replace MD with QM */
5774 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5776 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5777 if (lprof == NULL || lprof->lp_dt == NULL) {
5778 CERROR("can't find profile for %s\n",
5779 lustre_cfg_string(cfg, 0));
5780 GOTO(cleanup_mem, rc = -EINVAL);
5783 lustre_cfg_bufs_reset(bufs, qmtname);
5784 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5785 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5786 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5788 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5790 GOTO(put_profile, rc = -ENOMEM);
5791 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5793 rc = class_attach(lcfg);
5795 GOTO(lcfg_cleanup, rc);
5797 obd = class_name2obd(qmtname);
5799 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5800 lustre_cfg_string(cfg, 0));
5801 GOTO(lcfg_cleanup, rc = -EINVAL);
5804 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5806 lustre_cfg_bufs_reset(bufs, qmtname);
5807 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5808 lustre_cfg_bufs_set_string(bufs, 2, dev);
5810 /* for quota, the next device should be the OSD device */
5811 lustre_cfg_bufs_set_string(bufs, 3,
5812 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5814 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5816 GOTO(class_detach, rc = -ENOMEM);
5817 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5819 rc = class_setup(obd, lcfg);
5821 GOTO(class_detach, rc);
5823 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5825 /* configure local quota objects */
5826 if (CFS_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5829 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5833 GOTO(class_cleanup, rc);
5835 /* connect to quota master target */
5836 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5837 data->ocd_version = LUSTRE_VERSION_CODE;
5838 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5841 CERROR("cannot connect to quota master device %s (%d)\n",
5843 GOTO(class_cleanup, rc);
5849 class_manual_cleanup(obd);
5850 mdt->mdt_qmt_dev = NULL;
5851 GOTO(lcfg_cleanup, rc);
5855 class_detach(obd, lcfg);
5857 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5859 class_put_profile(lprof);
5864 OBD_FREE(qmtname, MAX_OBD_NAME);
5866 OBD_FREE(uuid, UUID_MAX);
5872 /* Shutdown quota master target associated with mdt */
5873 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5877 if (mdt->mdt_qmt_exp == NULL)
5879 LASSERT(mdt->mdt_qmt_dev != NULL);
5881 /* the qmt automatically shuts down when the mdt disconnects */
5882 obd_disconnect(mdt->mdt_qmt_exp);
5883 mdt->mdt_qmt_exp = NULL;
5884 mdt->mdt_qmt_dev = NULL;
5888 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5889 * for now. This will be removed along with converting rest of MDT code
5890 * to use tgt_session_info */
5891 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5893 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5896 if (unlikely(info->mti_object == NULL))
5899 rc = mdt_getxattr(info);
5901 mdt_thread_info_fini(info);
5905 static int mdt_llog_open(struct tgt_session_info *tsi)
5909 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
5910 RETURN(err_serious(-EACCES));
5912 RETURN(tgt_llog_open(tsi));
5915 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5916 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5917 #define OST_BRW_READ OST_READ
5918 #define OST_BRW_WRITE OST_WRITE
5920 static struct tgt_handler mdt_tgt_handlers[] = {
5921 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5922 0, MDS_CONNECT, mdt_tgt_connect,
5923 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5924 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5925 0, MDS_DISCONNECT, tgt_disconnect,
5926 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5927 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5928 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5929 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5930 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5931 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5932 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5933 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5935 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5936 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5937 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5938 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5939 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5940 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5941 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5942 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5944 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5945 mdt_hsm_ct_register),
5946 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5947 mdt_hsm_ct_unregister),
5948 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5950 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5952 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5953 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5955 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5958 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5959 TGT_MDT_HDL(IS_MUTABLE, MDS_BATCH, mdt_batch),
5962 static struct tgt_handler mdt_io_ops[] = {
5963 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5965 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5967 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5968 OST_PUNCH, mdt_punch_hdl,
5970 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5971 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
5973 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5974 TGT_RPC_HANDLER(OST_FIRST_OPC,
5975 0, OST_SET_INFO, mdt_io_set_info,
5976 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
5979 static struct tgt_handler mdt_sec_ctx_ops[] = {
5980 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5981 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5982 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5985 static struct tgt_handler mdt_quota_ops[] = {
5986 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5989 static struct tgt_handler mdt_llog_handlers[] = {
5990 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5991 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5992 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5993 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5996 static struct tgt_opc_slice mdt_common_slice[] = {
5998 .tos_opc_start = MDS_FIRST_OPC,
5999 .tos_opc_end = MDS_LAST_OPC,
6000 .tos_hs = mdt_tgt_handlers
6003 .tos_opc_start = OBD_FIRST_OPC,
6004 .tos_opc_end = OBD_LAST_OPC,
6005 .tos_hs = tgt_obd_handlers
6008 .tos_opc_start = LDLM_FIRST_OPC,
6009 .tos_opc_end = LDLM_LAST_OPC,
6010 .tos_hs = tgt_dlm_handlers
6013 .tos_opc_start = SEC_FIRST_OPC,
6014 .tos_opc_end = SEC_LAST_OPC,
6015 .tos_hs = mdt_sec_ctx_ops
6018 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
6019 .tos_opc_end = OUT_UPDATE_LAST_OPC,
6020 .tos_hs = tgt_out_handlers
6023 .tos_opc_start = FLD_FIRST_OPC,
6024 .tos_opc_end = FLD_LAST_OPC,
6025 .tos_hs = fld_handlers
6028 .tos_opc_start = SEQ_FIRST_OPC,
6029 .tos_opc_end = SEQ_LAST_OPC,
6030 .tos_hs = seq_handlers
6033 .tos_opc_start = QUOTA_DQACQ,
6034 .tos_opc_end = QUOTA_LAST_OPC,
6035 .tos_hs = mdt_quota_ops
6038 .tos_opc_start = LLOG_FIRST_OPC,
6039 .tos_opc_end = LLOG_LAST_OPC,
6040 .tos_hs = mdt_llog_handlers
6043 .tos_opc_start = LFSCK_FIRST_OPC,
6044 .tos_opc_end = LFSCK_LAST_OPC,
6045 .tos_hs = tgt_lfsck_handlers
6048 .tos_opc_start = OST_FIRST_OPC,
6049 .tos_opc_end = OST_LAST_OPC,
6050 .tos_hs = mdt_io_ops
6057 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
6059 struct md_device *next = m->mdt_child;
6060 struct lu_device *d = &m->mdt_lu_dev;
6061 struct obd_device *obd = mdt2obd_dev(m);
6062 struct lfsck_stop stop;
6065 stop.ls_status = LS_PAUSED;
6067 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
6069 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
6071 mdt_restriper_stop(m);
6072 ping_evictor_stop();
6074 /* Remove the HSM /proc entry so the coordinator cannot be
6075 * restarted by a user while it's shutting down.
6077 mdt_hsm_cdt_stop(m);
6079 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
6080 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
6082 if (m->mdt_namespace != NULL)
6083 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
6084 d->ld_obd->obd_force);
6086 obd_exports_barrier(obd);
6087 obd_zombie_barrier();
6089 mdt_quota_fini(env, m);
6091 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
6093 /* Calling the cleanup functions in the same order as in the mdt_init0
6096 mdt_tunables_fini(m);
6098 target_recovery_fini(obd);
6099 upcall_cache_cleanup(m->mdt_identity_cache);
6100 m->mdt_identity_cache = NULL;
6102 tgt_fini(env, &m->mdt_lut);
6104 mdt_hsm_cdt_fini(m);
6106 if (m->mdt_los != NULL) {
6107 local_oid_storage_fini(env, m->mdt_los);
6111 if (m->mdt_namespace != NULL) {
6112 ldlm_namespace_free_post(m->mdt_namespace);
6113 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
6116 if (m->mdt_md_root != NULL) {
6117 mdt_object_put(env, m->mdt_md_root);
6118 m->mdt_md_root = NULL;
6121 mdt_seq_fini(env, m);
6123 mdt_fld_fini(env, m);
6128 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6130 LASSERT(atomic_read(&d->ld_ref) == 0);
6132 server_put_mount(mdt_obd_name(m), true);
6137 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
6139 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
6140 struct lu_device_type *ldt, struct lustre_cfg *cfg)
6142 const struct dt_device_param *dt_conf;
6143 struct mdt_thread_info *info;
6144 struct obd_device *obd;
6145 const char *dev = lustre_cfg_string(cfg, 0);
6146 const char *num = lustre_cfg_string(cfg, 2);
6147 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
6148 struct lustre_mount_info *lmi = NULL;
6149 struct lustre_sb_info *lsi;
6151 struct seq_server_site *ss_site;
6152 const char *identity_upcall = "NONE";
6153 struct md_device *next;
6160 lu_device_init(&m->mdt_lu_dev, ldt);
6162 * Environment (env) might be missing mdt_thread_key values at that
6163 * point, if device is allocated when mdt_thread_key is in QUIESCENT
6166 * Usually device allocation path doesn't use module key values, but
6167 * mdt has to do a lot of work here, so allocate key value.
6169 rc = lu_env_refill((struct lu_env *)env);
6173 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
6174 LASSERT(info != NULL);
6176 obd = class_name2obd(dev);
6177 LASSERT(obd != NULL);
6179 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
6180 m->mdt_opts.mo_evict_tgt_nids = 1;
6181 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
6183 lmi = server_get_mount(dev);
6185 CERROR("Cannot get mount info for %s!\n", dev);
6188 lsi = s2lsi(lmi->lmi_sb);
6189 LASSERT(lsi->lsi_lmd);
6190 /* CMD is supported only in IAM mode */
6192 rc = kstrtol(num, 10, &node_id);
6197 if (test_bit(LMD_FLG_SKIP_LFSCK, lsi->lsi_lmd->lmd_flags))
6198 m->mdt_skip_lfsck = 1;
6201 /* Just try to get a DoM lock by default. Otherwise, having a group
6202 * lock granted, it may get blocked for a long time. */
6203 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
6204 /* DoM files are read at open and data is packed in the reply */
6205 m->mdt_opts.mo_dom_read_open = 1;
6207 m->mdt_squash.rsi_uid = 0;
6208 m->mdt_squash.rsi_gid = 0;
6209 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
6210 spin_lock_init(&m->mdt_squash.rsi_lock);
6211 spin_lock_init(&m->mdt_lock);
6212 m->mdt_enable_chprojid_gid = 0;
6213 m->mdt_enable_dir_migration = 1;
6214 m->mdt_enable_dir_restripe = 0;
6215 m->mdt_enable_dir_auto_split = 0;
6216 m->mdt_enable_parallel_rename_dir = 1;
6217 m->mdt_enable_parallel_rename_file = 1;
6218 m->mdt_enable_remote_dir = 1;
6219 m->mdt_enable_remote_dir_gid = 0;
6220 m->mdt_enable_remote_rename = 1;
6221 m->mdt_enable_striped_dir = 1;
6222 m->mdt_enable_dmv_implicit_inherit = 1;
6223 m->mdt_dir_restripe_nsonly = 1;
6224 m->mdt_max_mod_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
6226 atomic_set(&m->mdt_mds_mds_conns, 0);
6227 atomic_set(&m->mdt_async_commit_count, 0);
6228 atomic_set(&m->mdt_dmv_old_client_count, 0);
6230 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
6231 m->mdt_lu_dev.ld_obd = obd;
6232 /* Set this lu_device to obd for error handling purposes. */
6233 obd->obd_lu_dev = &m->mdt_lu_dev;
6235 /* init the stack */
6236 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
6238 CERROR("%s: Can't init device stack, rc %d\n",
6239 mdt_obd_name(m), rc);
6244 ss_site = mdt_seq_site(m);
6245 s->ld_seq_site = ss_site;
6248 /* set server index */
6249 ss_site->ss_node_id = node_id;
6251 /* failover is the default
6252 * FIXME: we do not failout mds0/mgs, which may cause some problems.
6253 * assumed whose ss_node_id == 0 XXX
6255 obd->obd_replayable = 1;
6256 /* No connection accepted until configurations will finish */
6257 obd->obd_no_conn = 1;
6259 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
6260 char *str = lustre_cfg_string(cfg, 4);
6261 if (strchr(str, 'n')) {
6262 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
6263 obd->obd_replayable = 0;
6267 rc = mdt_fld_init(env, mdt_obd_name(m), m);
6269 GOTO(err_fini_stack, rc);
6271 rc = mdt_seq_init(env, m);
6273 GOTO(err_fini_fld, rc);
6275 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
6276 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
6277 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
6278 LDLM_NAMESPACE_SERVER,
6279 LDLM_NAMESPACE_GREEDY,
6281 if (IS_ERR(m->mdt_namespace)) {
6282 rc = PTR_ERR(m->mdt_namespace);
6283 CERROR("%s: unable to create server namespace: rc = %d\n",
6285 m->mdt_namespace = NULL;
6286 GOTO(err_fini_seq, rc);
6289 m->mdt_namespace->ns_lvbp = m;
6290 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
6292 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
6293 /* set obd_namespace for compatibility with old code */
6294 obd->obd_namespace = m->mdt_namespace;
6296 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
6297 OBD_FAIL_MDS_ALL_REQUEST_NET,
6298 OBD_FAIL_MDS_ALL_REPLY_NET);
6300 GOTO(err_free_ns, rc);
6302 /* Amount of available space excluded from granting and reserved
6303 * for metadata. It is a percentage of the total MDT size. */
6304 tgd->tgd_reserved_pcnt = 10;
6306 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
6307 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
6309 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6311 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
6312 GOTO(err_tgt, rc = -ENOENT);
6314 fid.f_seq = FID_SEQ_LOCAL_NAME;
6317 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6321 rc = mdt_hsm_cdt_init(m);
6323 CERROR("%s: error initializing coordinator, rc %d\n",
6324 mdt_obd_name(m), rc);
6325 GOTO(err_los_fini, rc);
6328 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6330 next = m->mdt_child;
6331 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6333 mntopts = dt_conf->ddp_mntopts;
6335 if (mntopts & MNTOPT_USERXATTR)
6336 m->mdt_opts.mo_user_xattr = 1;
6338 m->mdt_opts.mo_user_xattr = 0;
6340 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6342 if (mntopts & MNTOPT_ACL)
6343 m->mdt_opts.mo_acl = 1;
6345 m->mdt_opts.mo_acl = 0;
6347 m->mdt_opts.mo_enable_strict_som = 1;
6349 /* XXX: to support suppgid for ACL, we enable identity_upcall
6350 * by default, otherwise, maybe got unexpected -EACCESS. */
6351 if (m->mdt_opts.mo_acl)
6352 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6354 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6356 &mdt_identity_upcall_cache_ops);
6357 if (IS_ERR(m->mdt_identity_cache)) {
6358 rc = PTR_ERR(m->mdt_identity_cache);
6359 m->mdt_identity_cache = NULL;
6360 GOTO(err_free_hsm, rc);
6363 rc = mdt_tunables_init(m, dev);
6365 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
6366 GOTO(err_recovery, rc);
6369 rc = mdt_quota_init(env, m, cfg);
6371 GOTO(err_procfs, rc);
6373 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
6374 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
6375 "mdt_ldlm_client", m->mdt_ldlm_client);
6377 ping_evictor_start();
6379 /* recovery will be started upon mdt_prepare()
6380 * when the whole stack is complete and ready
6381 * to serve the requests */
6383 /* Reduce the initial timeout on an MDS because it doesn't need such
6384 * a long timeout as an OST does. Adaptive timeouts will adjust this
6385 * value appropriately. */
6386 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
6387 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
6389 if (test_bit(LMD_FLG_LOCAL_RECOV, lsi->lsi_lmd->lmd_flags))
6390 m->mdt_lut.lut_local_recovery = 1;
6392 rc = mdt_restriper_start(m);
6394 GOTO(err_ping_evictor, rc);
6399 ping_evictor_stop();
6401 mdt_tunables_fini(m);
6403 upcall_cache_cleanup(m->mdt_identity_cache);
6404 m->mdt_identity_cache = NULL;
6406 mdt_hsm_cdt_fini(m);
6408 local_oid_storage_fini(env, m->mdt_los);
6411 /* keep recoverable clients */
6413 target_recovery_fini(obd);
6414 obd_exports_barrier(obd);
6415 obd_zombie_barrier();
6416 tgt_fini(env, &m->mdt_lut);
6418 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
6419 obd->obd_namespace = m->mdt_namespace = NULL;
6421 mdt_seq_fini(env, m);
6423 mdt_fld_fini(env, m);
6425 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6428 server_put_mount(dev, true);
6432 /* For interoperability, the left element is old parameter, the right one
6433 * is the new version of the parameter, if some parameter is deprecated,
6434 * the new version should be set as NULL. */
6435 static struct cfg_interop_param mdt_interop_param[] = {
6436 { "mdt.group_upcall", NULL },
6437 { "mdt.quota_type", NULL },
6438 { "mdd.quota_type", NULL },
6439 { "mdt.som", NULL },
6440 { "mdt.rootsquash", "mdt.root_squash" },
6441 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
6445 /* used by MGS to process specific configurations */
6446 static int mdt_process_config(const struct lu_env *env,
6447 struct lu_device *d, struct lustre_cfg *cfg)
6449 struct mdt_device *m = mdt_dev(d);
6450 struct md_device *md_next = m->mdt_child;
6451 struct lu_device *next = md2lu_dev(md_next);
6455 switch (cfg->lcfg_command) {
6457 struct obd_device *obd = d->ld_obd;
6458 /* For interoperability */
6459 struct cfg_interop_param *ptr = NULL;
6460 struct lustre_cfg *old_cfg = NULL;
6464 param = lustre_cfg_string(cfg, 1);
6465 if (param == NULL) {
6466 CERROR("param is empty\n");
6471 ptr = class_find_old_param(param, mdt_interop_param);
6473 if (ptr->new_param == NULL) {
6475 CWARN("For interoperability, skip this %s."
6476 " It is obsolete.\n", ptr->old_param);
6480 CWARN("Found old param %s, changed it to %s.\n",
6481 ptr->old_param, ptr->new_param);
6484 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
6491 count = class_modify_config(cfg, PARAM_MDT,
6492 &obd->obd_kset.kobj);
6494 struct coordinator *cdt = &m->mdt_coordinator;
6496 /* is it an HSM var ? */
6497 count = class_modify_config(cfg, PARAM_HSM,
6498 &cdt->cdt_hsm_kobj);
6500 /* we don't understand; pass it on */
6501 rc = next->ld_ops->ldo_process_config(env, next,
6504 rc = count > 0 ? 0 : count;
6506 rc = count > 0 ? 0 : count;
6510 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
6511 cfg->lcfg_buflens));
6515 /* others are passed further */
6516 rc = next->ld_ops->ldo_process_config(env, next, cfg);
6522 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
6523 const struct lu_object_header *hdr,
6524 struct lu_device *d)
6526 struct mdt_object *mo;
6530 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6532 struct lu_object *o;
6533 struct lu_object_header *h;
6536 h = &mo->mot_header;
6537 lu_object_header_init(h);
6538 lu_object_init(o, h, d);
6539 lu_object_add_top(h, o);
6540 o->lo_ops = &mdt_obj_ops;
6541 spin_lock_init(&mo->mot_write_lock);
6542 mutex_init(&mo->mot_som_mutex);
6543 mutex_init(&mo->mot_lov_mutex);
6544 init_rwsem(&mo->mot_dom_sem);
6545 init_rwsem(&mo->mot_open_sem);
6546 atomic_set(&mo->mot_open_count, 0);
6547 mo->mot_restripe_offset = 0;
6548 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6549 mo->mot_lsom_size = 0;
6550 mo->mot_lsom_blocks = 0;
6551 mo->mot_lsom_inited = false;
6557 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6558 const struct lu_object_conf *unused)
6560 struct mdt_device *d = mdt_dev(o->lo_dev);
6561 struct lu_device *under;
6562 struct lu_object *below;
6566 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6567 PFID(lu_object_fid(o)));
6569 under = &d->mdt_child->md_lu_dev;
6570 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6571 if (below != NULL) {
6572 lu_object_add(o, below);
6579 static void mdt_object_free_rcu(struct rcu_head *head)
6581 struct mdt_object *mo = container_of(head, struct mdt_object,
6582 mot_header.loh_rcu);
6584 kmem_cache_free(mdt_object_kmem, mo);
6587 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6589 struct mdt_object *mo = mdt_obj(o);
6590 struct lu_object_header *h;
6594 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6595 PFID(lu_object_fid(o)));
6597 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6598 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6601 lu_object_header_fini(h);
6602 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6603 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6608 static int mdt_object_print(const struct lu_env *env, void *cookie,
6609 lu_printer_t p, const struct lu_object *o)
6611 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6613 return (*p)(env, cookie,
6614 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6615 mdto, mdto->mot_lov_created ? "lov_created" : "",
6616 mdto->mot_cache_attr ? "cache_attr" : "",
6617 mdto->mot_write_count);
6620 static int mdt_prepare(const struct lu_env *env,
6621 struct lu_device *pdev,
6622 struct lu_device *cdev)
6624 struct mdt_device *mdt = mdt_dev(cdev);
6625 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6626 struct obd_device *obd = cdev->ld_obd;
6633 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6637 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6641 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6645 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6646 /* The LFSCK instance is registered just now, so it must be there when
6647 * register the namespace to such instance. */
6648 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6650 if (mdt->mdt_seq_site.ss_node_id == 0) {
6651 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6652 &mdt->mdt_md_root_fid);
6657 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6659 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6660 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6661 LASSERT(obd->obd_no_conn);
6662 spin_lock(&obd->obd_dev_lock);
6663 obd->obd_no_conn = 0;
6664 spin_unlock(&obd->obd_dev_lock);
6666 if (obd->obd_recovering == 0)
6667 mdt_postrecov(env, mdt);
6672 const struct lu_device_operations mdt_lu_ops = {
6673 .ldo_object_alloc = mdt_object_alloc,
6674 .ldo_process_config = mdt_process_config,
6675 .ldo_prepare = mdt_prepare,
6678 static const struct lu_object_operations mdt_obj_ops = {
6679 .loo_object_init = mdt_object_init,
6680 .loo_object_free = mdt_object_free,
6681 .loo_object_print = mdt_object_print
6684 static int mdt_obd_set_info_async(const struct lu_env *env,
6685 struct obd_export *exp,
6686 __u32 keylen, void *key,
6687 __u32 vallen, void *val,
6688 struct ptlrpc_request_set *set)
6694 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6695 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6702 static inline void mdt_enable_slc(struct mdt_device *mdt)
6704 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6705 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6708 static inline void mdt_disable_slc(struct mdt_device *mdt)
6710 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6711 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6715 * Match client and server connection feature flags.
6717 * Compute the compatibility flags for a connection request based on
6718 * features mutually supported by client and server.
6720 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6721 * must not be updated here, otherwise a partially initialized value may
6722 * be exposed. After the connection request is successfully processed,
6723 * the top-level MDT connect request handler atomically updates the export
6724 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6725 * reply. \see mdt_connect().
6727 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6728 * full struct obd_connect_data. So care must be taken when accessing fields
6729 * that are not present in struct obd_connect_data_v1. See LU-16.
6731 * \param exp the obd_export associated with this client/target pair
6732 * \param mdt the target device for the connection
6733 * \param data stores data for this connect request
6736 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6737 * \retval -EBADE client and server feature requirements are incompatible
6739 static int mdt_connect_internal(const struct lu_env *env,
6740 struct obd_export *exp,
6741 struct mdt_device *mdt,
6742 struct obd_connect_data *data, bool reconnect)
6744 const char *obd_name = mdt_obd_name(mdt);
6745 LASSERT(data != NULL);
6747 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6749 if (mdt->mdt_bottom->dd_rdonly &&
6750 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6751 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6754 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6755 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6757 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6759 if (!mdt->mdt_opts.mo_acl)
6760 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6762 if (!mdt->mdt_opts.mo_user_xattr)
6763 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6765 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6766 data->ocd_brw_size = min(data->ocd_brw_size,
6768 if (data->ocd_brw_size == 0) {
6769 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6770 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6771 "ocd_brw_size unexpectedly zero, network data "
6772 "corruption? Refusing to connect this client\n",
6773 obd_name, exp->exp_client_uuid.uuid,
6774 exp, data->ocd_connect_flags, data->ocd_version,
6775 data->ocd_grant, data->ocd_index);
6780 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6781 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6783 /* client is reporting its page size, for future use */
6784 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6785 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6786 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6787 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6788 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6789 /* ocd_grant_tax_kb is in 1K byte blocks */
6790 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6791 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6794 /* Save connect_data we have so far because tgt_grant_connect()
6795 * uses it to calculate grant, and we want to save the client
6796 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6797 exp->exp_connect_data = *data;
6798 if (OCD_HAS_FLAG(data, GRANT))
6799 tgt_grant_connect(env, exp, data, !reconnect);
6801 if (OCD_HAS_FLAG(data, MAXBYTES))
6802 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6804 /* NB: Disregard the rule against updating
6805 * exp_connect_data.ocd_connect_flags in this case, since
6806 * tgt_client_new() needs to know if this is a lightweight
6807 * connection, and it is safe to expose this flag before
6808 * connection processing completes. */
6809 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6810 spin_lock(&exp->exp_lock);
6811 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6812 spin_unlock(&exp->exp_lock);
6815 data->ocd_version = LUSTRE_VERSION_CODE;
6817 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6818 CWARN("%s: MDS requires FID support, but client not\n",
6823 if (OCD_HAS_FLAG(data, PINGLESS)) {
6824 if (ptlrpc_pinger_suppress_pings()) {
6825 spin_lock(&exp->exp_obd->obd_dev_lock);
6826 list_del_init(&exp->exp_obd_chain_timed);
6827 spin_unlock(&exp->exp_obd->obd_dev_lock);
6829 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6833 data->ocd_max_easize = mdt->mdt_max_ea_size;
6835 /* NB: Disregard the rule against updating
6836 * exp_connect_data.ocd_connect_flags in this case, since
6837 * tgt_client_new() needs to know if this is client supports
6838 * multiple modify RPCs, and it is safe to expose this flag before
6839 * connection processing completes. */
6840 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6841 if (mdt_max_mod_rpcs_changed(mdt))
6842 /* The new mdt.*.max_mod_rpcs_in_flight parameter
6843 * has not changed since initialization, but the
6844 * deprecated module parameter was changed,
6845 * so use that instead.
6847 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6849 data->ocd_maxmodrpcs = mdt->mdt_max_mod_rpcs_in_flight;
6850 spin_lock(&exp->exp_lock);
6851 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6852 spin_unlock(&exp->exp_lock);
6855 if (OCD_HAS_FLAG(data, CKSUM)) {
6856 __u32 cksum_types = data->ocd_cksum_types;
6858 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6860 if (unlikely(data->ocd_cksum_types == 0)) {
6861 CERROR("%s: Connect with checksum support but no "
6862 "ocd_cksum_types is set\n",
6863 exp->exp_obd->obd_name);
6867 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6868 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6869 cksum_types, data->ocd_cksum_types);
6871 /* This client does not support OBD_CONNECT_CKSUM
6872 * fall back to CRC32 */
6873 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6874 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6875 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6878 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6879 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6880 atomic_inc(&mdt->mdt_mds_mds_conns);
6881 mdt_enable_slc(mdt);
6884 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6885 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6887 if (!OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
6888 !OCD_HAS_FLAG2(data, DMV_IMP_INHERIT)) {
6889 atomic_inc(&mdt->mdt_dmv_old_client_count);
6890 mdt->mdt_enable_dmv_implicit_inherit = 0;
6896 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6897 struct mdt_thread_info *info,
6898 struct mdt_file_data *mfd)
6900 struct lu_context ses;
6904 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6909 lu_context_enter(&ses);
6911 mdt_ucred(info)->uc_valid = UCRED_OLD;
6912 /* do not let rbac interfere with dirty flag internal system event */
6913 mdt_ucred(info)->uc_rbac_file_perms = 1;
6914 mdt_ucred(info)->uc_rbac_dne_ops = 1;
6915 mdt_ucred(info)->uc_rbac_quota_ops = 1;
6916 mdt_ucred(info)->uc_rbac_byfid_ops = 1;
6917 mdt_ucred(info)->uc_rbac_chlg_ops = 1;
6918 mdt_ucred(info)->uc_rbac_fscrypt_admin = 1;
6919 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6921 lu_context_exit(&ses);
6922 lu_context_fini(&ses);
6928 static int mdt_export_cleanup(struct obd_export *exp)
6930 LIST_HEAD(closing_list);
6931 struct mdt_export_data *med = &exp->exp_mdt_data;
6932 struct obd_device *obd = exp->exp_obd;
6933 struct mdt_device *mdt;
6934 struct mdt_thread_info *info;
6936 struct mdt_file_data *mfd, *n;
6940 spin_lock(&med->med_open_lock);
6941 while (!list_empty(&med->med_open_head)) {
6942 struct list_head *tmp = med->med_open_head.next;
6943 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6945 /* Remove mfd handle so it can't be found again.
6946 * We are consuming the mfd_list reference here. */
6947 class_handle_unhash(&mfd->mfd_open_handle);
6948 list_move_tail(&mfd->mfd_list, &closing_list);
6950 spin_unlock(&med->med_open_lock);
6951 mdt = mdt_dev(obd->obd_lu_dev);
6952 LASSERT(mdt != NULL);
6954 rc = lu_env_init(&env, LCT_MD_THREAD);
6958 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6959 LASSERT(info != NULL);
6960 memset(info, 0, sizeof *info);
6961 info->mti_env = &env;
6962 info->mti_mdt = mdt;
6963 info->mti_exp = exp;
6965 if (!list_empty(&closing_list)) {
6966 struct md_attr *ma = &info->mti_attr;
6968 /* Close any open files (which may also cause orphan
6970 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6971 list_del_init(&mfd->mfd_list);
6972 ma->ma_need = ma->ma_valid = 0;
6974 /* This file is being closed due to an eviction, it
6975 * could have been modified and now dirty regarding to
6976 * HSM archive, check this!
6977 * The logic here is to mark a file dirty if there's a
6978 * chance it was dirtied before the client was evicted,
6979 * so that we don't have to wait for a release attempt
6980 * before finding out the file was actually dirty and
6981 * fail the release. Aggressively marking it dirty here
6982 * will cause the policy engine to attempt to
6983 * re-archive it; when rearchiving, we can compare the
6984 * current version to the HSM data_version and make the
6985 * archive request into a noop if it's not actually
6988 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6989 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6991 /* Don't unlink orphan on failover umount, LU-184 */
6992 if (exp->exp_flags & OBD_OPT_FAILOVER ||
6993 exp->exp_obd->obd_stopping) {
6994 ma->ma_valid = MA_FLAGS;
6995 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6997 ma->ma_valid |= MA_FORCE_LOG;
6998 mdt_mfd_close(info, mfd);
7001 info->mti_mdt = NULL;
7002 /* cleanup client slot early */
7003 /* Do not erase record for recoverable client. */
7004 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
7005 tgt_client_del(&env, exp);
7011 static int mdt_obd_disconnect(struct obd_export *exp)
7013 struct obd_connect_data *data = &exp->exp_connect_data;
7014 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
7020 class_export_get(exp);
7022 if (!(exp->exp_flags & OBD_OPT_FORCE))
7023 tgt_grant_sanity_check(exp->exp_obd, __func__);
7025 if (OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
7026 atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
7027 mdt_disable_slc(mdt);
7029 if (!OCD_HAS_FLAG(data, MDS_MDS) && !OCD_HAS_FLAG(data, LIGHTWEIGHT) &&
7030 !OCD_HAS_FLAG2(data, DMV_IMP_INHERIT) &&
7031 atomic_dec_and_test(&mdt->mdt_dmv_old_client_count))
7032 mdt->mdt_enable_dmv_implicit_inherit = 1;
7034 rc = server_disconnect_export(exp);
7036 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
7038 tgt_grant_discard(exp);
7040 rc = mdt_export_cleanup(exp);
7041 nodemap_del_member(exp);
7042 class_export_put(exp);
7046 /* mds_connect copy */
7047 static int mdt_obd_connect(const struct lu_env *env,
7048 struct obd_export **exp, struct obd_device *obd,
7049 struct obd_uuid *cluuid,
7050 struct obd_connect_data *data,
7053 struct obd_export *lexp;
7054 struct lustre_handle conn = { 0 };
7055 struct mdt_device *mdt;
7057 struct lnet_nid *client_nid = localdata;
7060 LASSERT(env != NULL);
7061 LASSERT(data != NULL);
7063 if (!exp || !obd || !cluuid)
7066 mdt = mdt_dev(obd->obd_lu_dev);
7069 * first, check whether the stack is ready to handle requests
7070 * XXX: probably not very appropriate method is used now
7071 * at some point we should find a better one
7073 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
7074 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
7075 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
7076 rc = obd_get_info(env, mdt->mdt_child_exp,
7077 sizeof(KEY_OSP_CONNECTED),
7078 KEY_OSP_CONNECTED, NULL, NULL);
7081 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
7084 rc = class_connect(&conn, obd, cluuid);
7088 lexp = class_conn2export(&conn);
7089 LASSERT(lexp != NULL);
7091 if (nid_is_nid4(client_nid)) {
7092 rc = nodemap_add_member(lnet_nid_to_nid4(client_nid), lexp);
7093 if (rc != 0 && rc != -EEXIST)
7097 rc = mdt_connect_internal(env, lexp, mdt, data, false);
7099 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
7102 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
7103 rc = tgt_client_new(env, lexp);
7105 mdt_export_stats_init(obd, lexp, localdata);
7109 class_disconnect(lexp);
7110 nodemap_del_member(lexp);
7114 /* Because we do not want this export to be evicted by pinger,
7115 * let's not add this export to the timed chain list. */
7116 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
7117 spin_lock(&lexp->exp_obd->obd_dev_lock);
7118 list_del_init(&lexp->exp_obd_chain_timed);
7119 spin_unlock(&lexp->exp_obd->obd_dev_lock);
7126 static int mdt_obd_reconnect(const struct lu_env *env,
7127 struct obd_export *exp, struct obd_device *obd,
7128 struct obd_uuid *cluuid,
7129 struct obd_connect_data *data,
7132 struct lnet_nid *client_nid = localdata;
7136 if (exp == NULL || obd == NULL || cluuid == NULL)
7139 if (nid_is_nid4(client_nid)) {
7140 rc = nodemap_add_member(lnet_nid_to_nid4(client_nid), exp);
7141 if (rc != 0 && rc != -EEXIST)
7145 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
7148 mdt_export_stats_init(obd, exp, localdata);
7150 nodemap_del_member(exp);
7155 /* FIXME: Can we avoid using these two interfaces? */
7156 static int mdt_init_export(struct obd_export *exp)
7158 struct mdt_export_data *med = &exp->exp_mdt_data;
7162 INIT_LIST_HEAD(&med->med_open_head);
7163 spin_lock_init(&med->med_open_lock);
7164 spin_lock(&exp->exp_lock);
7165 exp->exp_connecting = 1;
7166 spin_unlock(&exp->exp_lock);
7168 OBD_ALLOC(exp->exp_used_slots,
7169 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7170 if (exp->exp_used_slots == NULL)
7173 /* self-export doesn't need client data and ldlm initialization */
7174 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
7175 &exp->exp_client_uuid)))
7178 rc = tgt_client_alloc(exp);
7182 rc = ldlm_init_export(exp);
7189 tgt_client_free(exp);
7191 OBD_FREE(exp->exp_used_slots,
7192 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7193 exp->exp_used_slots = NULL;
7195 CERROR("%s: Failed to initialize export: rc = %d\n",
7196 exp->exp_obd->obd_name, rc);
7200 static int mdt_destroy_export(struct obd_export *exp)
7204 target_destroy_export(exp);
7205 if (exp->exp_used_slots)
7206 OBD_FREE(exp->exp_used_slots,
7207 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
7209 /* destroy can be called from failed obd_setup, so
7210 * checking uuid is safer than obd_self_export */
7211 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
7212 &exp->exp_client_uuid)))
7215 ldlm_destroy_export(exp);
7216 tgt_client_free(exp);
7218 LASSERT(list_empty(&exp->exp_outstanding_replies));
7219 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
7222 * discard grants once we're sure no more
7223 * interaction with the client is possible
7225 tgt_grant_discard(exp);
7226 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
7227 obd2obt(exp->exp_obd)->obt_lut->lut_tgd.tgd_tot_granted_clients--;
7229 if (!(exp->exp_flags & OBD_OPT_FORCE))
7230 tgt_grant_sanity_check(exp->exp_obd, __func__);
7235 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
7236 struct linkea_data *ldata)
7240 LASSERT(ldata->ld_buf->lb_buf != NULL);
7242 if (!mdt_object_exists(mdt_obj))
7245 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7246 ldata->ld_buf, XATTR_NAME_LINK);
7247 if (rc == -ERANGE) {
7248 /* Buf was too small, figure out what we need. */
7249 lu_buf_free(ldata->ld_buf);
7250 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7251 ldata->ld_buf, XATTR_NAME_LINK);
7254 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
7255 if (ldata->ld_buf->lb_buf == NULL)
7257 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7258 ldata->ld_buf, XATTR_NAME_LINK);
7263 return linkea_init_with_rec(ldata);
7267 * Given an MDT object, try to look up the full path to the object.
7268 * Part of the MDT layer implementation of lfs fid2path.
7270 * \param[in] info Per-thread common data shared by MDT level handlers.
7271 * \param[in] obj Object to do path lookup of
7272 * \param[in,out] fp User-provided struct to store path information
7273 * \param[in] root_fid Root FID of current path should reach
7275 * \retval 0 Lookup successful, path information stored in fp
7276 * \retval -EAGAIN Lookup failed, usually because object is being moved
7277 * \retval negative errno if there was a problem
7279 static int mdt_path_current(struct mdt_thread_info *info,
7280 struct mdt_object *obj,
7281 struct getinfo_fid2path *fp,
7282 struct lu_fid *root_fid)
7284 struct mdt_device *mdt = info->mti_mdt;
7285 struct lu_name *tmpname = &info->mti_name;
7286 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
7287 struct lu_buf *buf = &info->mti_big_buf;
7288 struct linkea_data ldata = { NULL };
7290 struct mdt_object *mdt_obj;
7291 struct link_ea_header *leh;
7292 struct link_ea_entry *lee;
7293 bool worthchecking = true;
7294 bool needsfid = false;
7295 bool supported = false;
7303 /* temp buffer for path element, the buffer will be finally freed
7304 * in mdt_thread_info_fini */
7305 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
7306 if (buf->lb_buf == NULL)
7310 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
7313 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
7315 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
7316 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
7317 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
7318 GOTO(out, rc = -ENOENT);
7320 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
7322 mdt_object_get(info->mti_env, mdt_obj);
7324 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
7325 if (IS_ERR(mdt_obj))
7326 GOTO(out, rc = PTR_ERR(mdt_obj));
7329 if (!mdt_object_exists(mdt_obj)) {
7330 mdt_object_put(info->mti_env, mdt_obj);
7331 GOTO(out, rc = -ENOENT);
7334 if (mdt_object_remote(mdt_obj)) {
7335 mdt_object_put(info->mti_env, mdt_obj);
7336 GOTO(remote_out, rc = -EREMOTE);
7339 if (worthchecking) {
7340 /* we need to know if the FID being
7341 * looked up is encrypted
7343 struct lu_attr la = { 0 };
7344 struct dt_object *dt = mdt_obj2dt(mdt_obj);
7346 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
7347 dt_attr_get(info->mti_env, dt, &la);
7348 if (la.la_valid & LA_FLAGS &&
7349 la.la_flags & LUSTRE_ENCRYPT_FL) {
7350 if (!supported && mdt_info_req(info) &&
7351 !exp_connect_encrypt_fid2path(
7352 mdt_info_req(info)->rq_export)) {
7353 /* client does not support fid2path
7354 * for encrypted files
7356 mdt_object_put(info->mti_env, mdt_obj);
7357 GOTO(out, rc = -ENODATA);
7365 worthchecking = false;
7372 rc = mdt_links_read(info, mdt_obj, &ldata);
7374 mdt_object_put(info->mti_env, mdt_obj);
7379 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
7380 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
7381 /* If set, use link #linkno for path lookup, otherwise use
7382 link #0. Only do this for the final path element. */
7383 if (first && fp->gf_linkno < leh->leh_reccount) {
7385 for (count = 0; count < fp->gf_linkno; count++) {
7386 lee = (struct link_ea_entry *)
7387 ((char *)lee + reclen);
7388 linkea_entry_unpack(lee, &reclen, tmpname,
7391 if (fp->gf_linkno < leh->leh_reccount - 1)
7392 /* indicate to user there are more links */
7396 /* Check if it is slave stripes */
7397 rc = mdt_is_dir_stripe(info, mdt_obj);
7398 mdt_object_put(info->mti_env, mdt_obj);
7402 fp->gf_fid = *tmpfid;
7406 /* Pack the name in the end of the buffer */
7407 ptr -= tmpname->ln_namelen;
7408 if (ptr - 1 <= fp->gf_u.gf_path)
7409 GOTO(out, rc = -ENAMETOOLONG);
7410 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
7412 /* Pack FID before file name, so that client can build
7413 * encoded/digested form.
7415 char fidstr[FID_LEN + 1];
7417 snprintf(fidstr, sizeof(fidstr), DFID,
7419 ptr -= strlen(fidstr);
7420 if (ptr - 1 <= fp->gf_u.gf_path)
7421 GOTO(out, rc = -ENAMETOOLONG);
7422 strncpy(ptr, fidstr, strlen(fidstr));
7426 /* keep the last resolved fid to the client, so the
7427 * client will build the left path on another MDT for
7429 fp->gf_fid = *tmpfid;
7434 /* non-zero will be treated as an error */
7439 ptr++; /* skip leading / unless this is an encrypted file */
7440 memmove(fp->gf_u.gf_path, ptr,
7441 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
7448 * Given an MDT object, use mdt_path_current to get the path.
7449 * Essentially a wrapper to retry mdt_path_current a set number of times
7450 * if -EAGAIN is returned (usually because an object is being moved).
7452 * Part of the MDT layer implementation of lfs fid2path.
7454 * \param[in] info Per-thread common data shared by mdt level handlers.
7455 * \param[in] obj Object to do path lookup of
7456 * \param[in,out] fp User-provided struct for arguments and to store path
7459 * \retval 0 Lookup successful, path information stored in fp
7460 * \retval negative errno if there was a problem
7462 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
7463 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
7465 struct mdt_device *mdt = info->mti_mdt;
7470 if (fp->gf_pathlen < 3)
7473 if (root_fid == NULL)
7474 root_fid = &mdt->mdt_md_root_fid;
7476 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
7477 fp->gf_u.gf_path[0] = '\0';
7481 /* Retry multiple times in case file is being moved */
7482 while (tries-- && rc == -EAGAIN)
7483 rc = mdt_path_current(info, obj, fp, root_fid);
7489 * Get the full path of the provided FID, as of changelog record recno.
7491 * This checks sanity and looks up object for user provided FID
7492 * before calling the actual path lookup code.
7494 * Part of the MDT layer implementation of lfs fid2path.
7496 * \param[in] info Per-thread common data shared by mdt level handlers.
7497 * \param[in,out] fp User-provided struct for arguments and to store path
7500 * \retval 0 Lookup successful, path information and recno stored in fp
7501 * \retval -ENOENT, object does not exist
7502 * \retval negative errno if there was a problem
7504 static int mdt_fid2path(struct mdt_thread_info *info,
7505 struct lu_fid *root_fid,
7506 struct getinfo_fid2path *fp)
7508 struct mdt_device *mdt = info->mti_mdt;
7509 struct mdt_object *obj;
7513 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
7514 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
7516 if (!fid_is_sane(&fp->gf_fid))
7519 if (!fid_is_namespace_visible(&fp->gf_fid)) {
7520 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
7521 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
7522 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
7526 /* return error if client-provided root fid
7527 * is not the one stored in the export
7529 if (root_fid && !fid_is_zero(&info->mti_exp->exp_root_fid) &&
7530 !lu_fid_eq(root_fid, &info->mti_exp->exp_root_fid)) {
7532 "%s: root fid from client "DFID" but "DFID" stored in export\n",
7533 mdt_obd_name(mdt), PFID(root_fid),
7534 PFID(&info->mti_exp->exp_root_fid));
7538 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
7541 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
7542 PFID(&fp->gf_fid), rc);
7546 if (mdt_object_remote(obj))
7548 else if (!mdt_object_exists(obj))
7554 mdt_object_put(info->mti_env, obj);
7555 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
7556 PFID(&fp->gf_fid), rc);
7560 rc = mdt_path(info, obj, fp, root_fid);
7562 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
7563 PFID(&fp->gf_fid), fp->gf_u.gf_path,
7564 fp->gf_recno, fp->gf_linkno);
7566 mdt_object_put(info->mti_env, obj);
7571 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
7572 void *val, int vallen)
7574 struct getinfo_fid2path *fpout, *fpin;
7575 struct lu_fid *root_fid = NULL;
7578 fpin = key + round_up(sizeof(KEY_FID2PATH), 8);
7581 if (req_capsule_req_need_swab(info->mti_pill))
7582 lustre_swab_fid2path(fpin);
7584 memcpy(fpout, fpin, sizeof(*fpin));
7585 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
7588 if (keylen >= round_up(sizeof(KEY_FID2PATH), 8) + sizeof(*fpin) +
7589 sizeof(struct lu_fid)) {
7590 /* client sent its root FID, which is normally fileset FID */
7591 root_fid = fpin->gf_u.gf_root_fid;
7592 if (req_capsule_req_need_swab(info->mti_pill))
7593 lustre_swab_lu_fid(root_fid);
7595 if (root_fid != NULL && !fid_is_sane(root_fid))
7599 rc = mdt_fid2path(info, root_fid, fpout);
7603 int mdt_get_info(struct tgt_session_info *tsi)
7613 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7615 CDEBUG(D_IOCTL, "No GETINFO key\n");
7616 RETURN(err_serious(-EFAULT));
7618 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7621 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7622 if (vallen == NULL) {
7623 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7624 tgt_name(tsi->tsi_tgt));
7625 RETURN(err_serious(-EFAULT));
7628 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7630 rc = req_capsule_server_pack(tsi->tsi_pill);
7632 RETURN(err_serious(rc));
7634 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7635 if (valout == NULL) {
7636 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7637 tgt_name(tsi->tsi_tgt));
7638 RETURN(err_serious(-EFAULT));
7641 if (KEY_IS(KEY_FID2PATH)) {
7642 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7644 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7645 mdt_thread_info_fini(info);
7652 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7654 struct obd_ioctl_data *data = karg;
7657 struct mdt_object *obj;
7658 struct mdt_lock_handle *lh;
7662 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7663 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7666 fid = (struct lu_fid *)data->ioc_inlbuf1;
7668 if (!fid_is_sane(fid))
7671 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7673 lh = &mti->mti_lh[MDT_LH_PARENT];
7674 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE, LCK_CR);
7676 RETURN(PTR_ERR(obj));
7678 if (mdt_object_remote(obj)) {
7681 * before calling version get the correct MDS should be
7682 * fid, this is error to find remote object here
7684 CERROR("nonlocal object "DFID"\n", PFID(fid));
7685 } else if (!mdt_object_exists(obj)) {
7686 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7689 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7690 *(__u64 *)data->ioc_inlbuf2 = version;
7693 mdt_object_unlock_put(mti, obj, lh, 1);
7697 /* ioctls on obd dev */
7698 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7699 void *karg, void __user *uarg)
7701 struct obd_device *obd = exp->exp_obd;
7702 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7703 struct dt_device *dt = mdt->mdt_bottom;
7704 struct obd_ioctl_data *data;
7709 CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
7710 obd->obd_name, cmd, len, karg, uarg);
7712 rc = lu_env_init(&env, LCT_MD_THREAD);
7716 /* handle commands that don't use @karg first */
7719 rc = mdt_device_sync(&env, mdt);
7721 case OBD_IOC_SET_READONLY:
7722 rc = dt_sync(&env, dt);
7724 rc = dt_ro(&env, dt);
7728 if (unlikely(karg == NULL)) {
7729 OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", rc = -EINVAL);
7735 case OBD_IOC_ABORT_RECOVERY: {
7736 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
7737 LCONSOLE_WARN("%s: Aborting MDT recovery\n",
7739 obd->obd_abort_mdt_recovery = 1;
7740 wake_up(&obd->obd_next_transno_waitq);
7741 } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7742 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7743 LCONSOLE_WARN("%s: Aborting client recovery\n",
7745 obd->obd_abort_recovery = 1;
7746 target_stop_recovery_thread(obd);
7751 case OBD_IOC_CHANGELOG_REG:
7752 case OBD_IOC_CHANGELOG_DEREG:
7753 case OBD_IOC_CHANGELOG_CLEAR:
7754 case OBD_IOC_LLOG_PRINT:
7755 case OBD_IOC_LLOG_CANCEL:
7756 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env, mdt->mdt_child,
7759 case OBD_IOC_START_LFSCK: {
7760 struct md_device *next = mdt->mdt_child;
7761 struct lfsck_start_param lsp;
7763 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7764 lsp.lsp_index_valid = 0;
7765 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7768 case OBD_IOC_STOP_LFSCK: {
7769 struct md_device *next = mdt->mdt_child;
7770 struct lfsck_stop stop;
7772 stop.ls_status = LS_STOPPED;
7773 /* Old lfsck utils may pass NULL @stop. */
7774 if (data->ioc_inlbuf1 == NULL)
7778 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7780 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7783 case OBD_IOC_QUERY_LFSCK: {
7784 struct md_device *next = mdt->mdt_child;
7786 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7790 case OBD_IOC_GET_OBJ_VERSION: {
7791 struct mdt_thread_info *mti;
7793 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7794 memset(mti, 0, sizeof(*mti));
7795 mti->mti_env = &env;
7799 rc = mdt_ioc_version_get(mti, karg);
7802 case OBD_IOC_CATLOGLIST: {
7803 struct mdt_thread_info *mti;
7805 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7806 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7807 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7808 &mti->mti_tmp_fid1);
7812 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
7820 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7822 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7826 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7827 struct lfsck_start_param lsp;
7829 lsp.lsp_start = NULL;
7830 lsp.lsp_index_valid = 0;
7831 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7832 OBD_IOC_START_LFSCK,
7834 if (rc != 0 && rc != -EALREADY)
7835 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7836 mdt_obd_name(mdt), rc);
7839 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7843 static int mdt_obd_postrecov(struct obd_device *obd)
7848 rc = lu_env_init(&env, LCT_MD_THREAD);
7851 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7856 static const struct obd_ops mdt_obd_device_ops = {
7857 .o_owner = THIS_MODULE,
7858 .o_set_info_async = mdt_obd_set_info_async,
7859 .o_connect = mdt_obd_connect,
7860 .o_reconnect = mdt_obd_reconnect,
7861 .o_disconnect = mdt_obd_disconnect,
7862 .o_init_export = mdt_init_export,
7863 .o_destroy_export = mdt_destroy_export,
7864 .o_iocontrol = mdt_iocontrol,
7865 .o_postrecov = mdt_obd_postrecov,
7866 /* Data-on-MDT IO methods */
7867 .o_preprw = mdt_obd_preprw,
7868 .o_commitrw = mdt_obd_commitrw,
7871 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7872 struct lu_device *d)
7874 struct mdt_device *m = mdt_dev(d);
7881 static struct lu_device *mdt_device_free(const struct lu_env *env,
7882 struct lu_device *d)
7884 struct mdt_device *m = mdt_dev(d);
7887 lu_device_fini(&m->mdt_lu_dev);
7893 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7894 struct lu_device_type *t,
7895 struct lustre_cfg *cfg)
7897 struct lu_device *l;
7898 struct mdt_device *m;
7905 rc = mdt_init0(env, m, t, cfg);
7907 mdt_device_free(env, l);
7912 l = ERR_PTR(-ENOMEM);
7916 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7917 LU_KEY_INIT(mdt, struct mdt_thread_info);
7919 static void mdt_key_fini(const struct lu_context *ctx,
7920 struct lu_context_key *key, void* data)
7922 struct mdt_thread_info *info = data;
7924 if (info->mti_big_lmm) {
7925 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7926 info->mti_big_lmm = NULL;
7927 info->mti_big_lmmsize = 0;
7930 if (info->mti_big_acl) {
7931 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7932 info->mti_big_acl = NULL;
7933 info->mti_big_aclsize = 0;
7939 /* context key: mdt_thread_key */
7940 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7942 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7944 return lu_ucred(info->mti_env);
7947 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7949 return lu_ucred_check(info->mti_env);
7953 * Enable/disable COS (Commit On Sharing).
7955 * Set/Clear the COS flag in mdt options.
7957 * \param mdt mdt device
7958 * \param val 0 disables COS, other values enable COS
7960 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7965 mdt->mdt_opts.mo_cos = val;
7966 rc = lu_env_init(&env, LCT_LOCAL);
7967 if (unlikely(rc != 0)) {
7968 CWARN("%s: lu_env initialization failed, cannot "
7969 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7972 mdt_device_sync(&env, mdt);
7977 * Check COS (Commit On Sharing) status.
7979 * Return COS flag status.
7981 * \param mdt mdt device
7983 int mdt_cos_is_enabled(struct mdt_device *mdt)
7985 return mdt->mdt_opts.mo_cos != 0;
7988 static const struct lu_device_type_operations mdt_device_type_ops = {
7989 .ldto_device_alloc = mdt_device_alloc,
7990 .ldto_device_free = mdt_device_free,
7991 .ldto_device_fini = mdt_device_fini
7994 static struct lu_device_type mdt_device_type = {
7995 .ldt_tags = LU_DEVICE_MD,
7996 .ldt_name = LUSTRE_MDT_NAME,
7997 .ldt_ops = &mdt_device_type_ops,
7998 .ldt_ctx_tags = LCT_MD_THREAD
8001 static int __init mdt_init(void)
8005 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
8006 FID_NOBRACE_LEN + 1);
8007 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
8009 rc = lu_kmem_init(mdt_caches);
8013 rc = mds_mod_init();
8017 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
8018 LUSTRE_MDT_NAME, &mdt_device_type);
8023 lu_kmem_fini(mdt_caches);
8030 static void __exit mdt_exit(void)
8032 class_unregister_type(LUSTRE_MDT_NAME);
8034 lu_kmem_fini(mdt_caches);
8037 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8038 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
8039 MODULE_VERSION(LUSTRE_VERSION_STRING);
8040 MODULE_LICENSE("GPL");
8042 module_init(mdt_init);
8043 module_exit(mdt_exit);