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;
500 svcpt = req->rq_rqbd->rqbd_svcpt;
502 /* This will trigger a watchdog timeout */
503 at_est = obd_at_get(mdt->mdt_lu_dev.ld_obd, &svcpt->scp_at_estimate);
504 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
505 (MDT_SERVICE_WATCHDOG_FACTOR * at_est) + 1);
507 rc = mdt_check_ucred(info);
509 GOTO(out, rc = err_serious(rc));
511 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
512 GOTO(out, rc = err_serious(-ENOMEM));
514 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
516 GOTO(out, rc = -EPROTO);
518 if (mdt_is_sum_statfs_client(req->rq_export) &&
519 lustre_packed_msg_size(req->rq_reqmsg) ==
520 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
521 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
522 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
523 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
526 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
527 msf = &mdt->mdt_sum_osfs;
529 msf = &mdt->mdt_osfs;
531 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
532 /** statfs data is too old, get up-to-date one */
533 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
534 rc = next->md_ops->mdo_statfs(info->mti_env,
537 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
541 spin_lock(&mdt->mdt_lock);
542 msf->msf_osfs = *osfs;
543 msf->msf_age = ktime_get_seconds();
544 spin_unlock(&mdt->mdt_lock);
546 /** use cached statfs data */
547 spin_lock(&mdt->mdt_lock);
548 *osfs = msf->msf_osfs;
549 spin_unlock(&mdt->mdt_lock);
552 /* tgd_blockbit is recordsize bits set during mkfs.
553 * This once set does not change. However, 'zfs set'
554 * can be used to change the MDT blocksize. Instead
555 * of using cached value of 'tgd_blockbit' always
556 * calculate the blocksize bits which may have
559 current_blockbits = fls64(osfs->os_bsize) - 1;
561 /* at least try to account for cached pages. its still racy and
562 * might be under-reporting if clients haven't announced their
563 * caches with brw recently */
564 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
565 " pending %llu free %llu avail %llu\n",
566 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
567 tgd->tgd_tot_pending,
568 osfs->os_bfree << current_blockbits,
569 osfs->os_bavail << current_blockbits);
571 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
572 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
573 osfs->os_bsize - 1) >> current_blockbits));
575 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
576 if (mdt->mdt_lut.lut_no_create)
577 osfs->os_state |= OS_STATFS_NOCREATE;
578 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
579 "%llu objects: %llu free; state %x\n",
580 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
581 osfs->os_files, osfs->os_ffree, osfs->os_state);
583 if (!exp_grant_param_supp(tsi->tsi_exp) &&
584 current_blockbits > COMPAT_BSIZE_SHIFT) {
585 /* clients which don't support OBD_CONNECT_GRANT_PARAM
586 * should not see a block size > page size, otherwise
587 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
588 * block size which is the biggest block size known to work
589 * with all client's page size. */
590 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
591 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
592 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
593 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
596 mdt_counter_incr(req, LPROC_MDT_STATFS,
597 ktime_us_delta(ktime_get(), kstart));
599 mdt_thread_info_fini(info);
603 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
605 struct lov_comp_md_v1 *comp_v1;
606 struct lov_mds_md *v1;
608 __u32 dom_stripesize = 0;
610 bool has_ost_stripes = false;
617 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
620 comp_v1 = (struct lov_comp_md_v1 *)lmm;
621 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
622 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
624 /* Fast check for DoM entry with no mirroring, should be the first */
625 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
626 !(lov_pattern(le32_to_cpu(v1->lmm_pattern)) & LOV_PATTERN_MDT))
629 /* check all entries otherwise */
630 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
631 struct lov_comp_md_entry_v1 *lcme;
633 lcme = &comp_v1->lcm_entries[i];
634 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
637 off = le32_to_cpu(lcme->lcme_offset);
638 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
640 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) &
642 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
644 has_ost_stripes = true;
646 if (dom_stripesize && has_ost_stripes)
647 RETURN(dom_stripesize);
649 /* DoM-only case exits here */
650 if (is_dom_only && dom_stripesize)
652 RETURN(dom_stripesize);
656 * Pack size attributes into the reply.
658 int mdt_pack_size2body(struct mdt_thread_info *info,
659 const struct lu_fid *fid, struct lustre_handle *lh)
662 struct md_attr *ma = &info->mti_attr;
664 bool dom_lock = false;
668 LASSERT(ma->ma_attr.la_valid & LA_MODE);
670 if (!S_ISREG(ma->ma_attr.la_mode) ||
671 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
674 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
675 /* no DoM stripe, no size in reply */
679 if (lustre_handle_is_used(lh)) {
680 struct ldlm_lock *lock;
682 lock = ldlm_handle2lock(lh);
684 dom_lock = ldlm_has_dom(lock);
689 /* no DoM lock, no size in reply */
693 /* Either DoM lock exists or LMM has only DoM stripe then
694 * return size on body. */
695 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
697 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
701 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
703 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
705 * \param info thread info object
706 * \param repbody reply to pack ACLs into
707 * \param o mdt object of file to examine
708 * \param nodemap nodemap of client to reply to
710 * \retval -errno error getting or parsing ACL from disk
712 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
713 struct mdt_object *o, struct lu_nodemap *nodemap)
715 const struct lu_env *env = info->mti_env;
716 struct md_object *next = mdt_object_child(o);
717 struct lu_buf *buf = &info->mti_buf;
718 struct mdt_device *mdt = info->mti_mdt;
719 struct req_capsule *pill = info->mti_pill;
724 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
725 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
726 if (buf->lb_len == 0)
729 LASSERT(!info->mti_big_acl_used);
731 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
733 if (rc == -ENODATA) {
734 repbody->mbo_aclsize = 0;
735 repbody->mbo_valid |= OBD_MD_FLACL;
737 } else if (rc == -EOPNOTSUPP) {
739 } else if (rc == -ERANGE) {
740 if (exp_connect_large_acl(info->mti_exp) &&
741 !info->mti_big_acl_used) {
742 if (info->mti_big_acl == NULL) {
743 info->mti_big_aclsize =
745 mdt->mdt_max_ea_size,
747 OBD_ALLOC_LARGE(info->mti_big_acl,
748 info->mti_big_aclsize);
749 if (info->mti_big_acl == NULL) {
750 info->mti_big_aclsize = 0;
751 CERROR("%s: unable to grow "
754 PFID(mdt_object_fid(o)));
759 CDEBUG(D_INODE, "%s: grow the "DFID
760 " ACL buffer to size %d\n",
762 PFID(mdt_object_fid(o)),
763 info->mti_big_aclsize);
765 buf->lb_buf = info->mti_big_acl;
766 buf->lb_len = info->mti_big_aclsize;
767 info->mti_big_acl_used = 1;
770 /* FS has ACL bigger that our limits */
771 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
772 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
773 info->mti_big_aclsize);
776 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
777 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
780 rc = nodemap_map_acl(nodemap, buf->lb_buf,
781 rc, NODEMAP_FS_TO_CLIENT);
782 /* if all ACLs mapped out, rc is still >= 0 */
784 CERROR("%s: nodemap_map_acl unable to parse "DFID
785 " ACL: rc = %d\n", mdt_obd_name(mdt),
786 PFID(mdt_object_fid(o)), rc);
787 repbody->mbo_aclsize = 0;
788 repbody->mbo_valid &= ~OBD_MD_FLACL;
790 repbody->mbo_aclsize = rc;
791 repbody->mbo_valid |= OBD_MD_FLACL;
800 /* XXX Look into layout in MDT layer. */
801 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
803 struct lov_comp_md_v1 *comp_v1;
804 struct lov_mds_md *v1;
807 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
808 comp_v1 = (struct lov_comp_md_v1 *)lmm;
810 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
811 v1 = (struct lov_mds_md *)((char *)comp_v1 +
812 comp_v1->lcm_entries[i].lcme_offset);
813 /* We don't support partial release for now */
814 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
819 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
824 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
825 const struct lu_attr *attr, const struct lu_fid *fid)
827 struct mdt_device *mdt = info->mti_mdt;
828 struct obd_export *exp = info->mti_exp;
829 struct md_attr *ma = &info->mti_attr;
830 struct lu_nodemap *nodemap = NULL;
832 LASSERT(ma->ma_valid & MA_INODE);
834 if (attr->la_valid & LA_ATIME) {
835 b->mbo_atime = attr->la_atime;
836 b->mbo_valid |= OBD_MD_FLATIME;
838 if (attr->la_valid & LA_MTIME) {
839 b->mbo_mtime = attr->la_mtime;
840 b->mbo_valid |= OBD_MD_FLMTIME;
842 if (attr->la_valid & LA_CTIME) {
843 b->mbo_ctime = attr->la_ctime;
844 b->mbo_valid |= OBD_MD_FLCTIME;
846 if (attr->la_valid & LA_BTIME) {
847 b->mbo_btime = attr->la_btime;
848 b->mbo_valid |= OBD_MD_FLBTIME;
850 if (attr->la_valid & LA_FLAGS) {
851 b->mbo_flags = attr->la_flags;
852 b->mbo_valid |= OBD_MD_FLFLAGS;
854 if (attr->la_valid & LA_NLINK) {
855 b->mbo_nlink = attr->la_nlink;
856 b->mbo_valid |= OBD_MD_FLNLINK;
858 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
859 nodemap = nodemap_get_from_exp(exp);
863 if (attr->la_valid & LA_UID) {
864 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
865 NODEMAP_FS_TO_CLIENT,
867 b->mbo_valid |= OBD_MD_FLUID;
869 if (attr->la_valid & LA_GID) {
870 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
871 NODEMAP_FS_TO_CLIENT,
873 b->mbo_valid |= OBD_MD_FLGID;
876 if (attr->la_valid & LA_PROJID) {
877 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
878 NODEMAP_FS_TO_CLIENT,
880 b->mbo_valid |= OBD_MD_FLPROJID;
883 b->mbo_mode = attr->la_mode;
884 if (attr->la_valid & LA_MODE)
885 b->mbo_valid |= OBD_MD_FLMODE;
886 if (attr->la_valid & LA_TYPE)
887 b->mbo_valid |= OBD_MD_FLTYPE;
891 b->mbo_valid |= OBD_MD_FLID;
892 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
893 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
896 if (!(attr->la_valid & LA_TYPE))
899 b->mbo_rdev = attr->la_rdev;
900 b->mbo_size = attr->la_size;
901 b->mbo_blocks = attr->la_blocks;
903 if (!S_ISREG(attr->la_mode)) {
904 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
905 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
906 /* means no objects are allocated on osts. */
907 LASSERT(!(ma->ma_valid & MA_LOV));
908 /* just ignore blocks occupied by extend attributes on MDS */
910 /* if no object is allocated on osts, the size on mds is valid.
912 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
913 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
914 if (mdt_hsm_is_released(ma->ma_lmm)) {
915 /* A released file stores its size on MDS. */
916 /* But return 1 block for released file, unless tools
917 * like tar will consider it fully sparse. (LU-3864)
919 if (unlikely(b->mbo_size == 0))
923 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
924 } else if (info->mti_som_strict &&
925 mdt->mdt_opts.mo_enable_strict_som) {
926 /* use SOM for size*/
927 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
928 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
929 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
930 b->mbo_size = ma->ma_som.ms_size;
931 b->mbo_blocks = ma->ma_som.ms_blocks;
935 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
936 b->mbo_valid & OBD_MD_FLLAZYSIZE))
937 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
938 PFID(fid), (unsigned long long)b->mbo_size);
941 if (!IS_ERR_OR_NULL(nodemap))
942 nodemap_putref(nodemap);
945 static inline int mdt_body_has_lov(const struct lu_attr *la,
946 const struct mdt_body *body)
948 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
949 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
952 void mdt_client_compatibility(struct mdt_thread_info *info)
954 struct mdt_body *body;
955 struct ptlrpc_request *req = mdt_info_req(info);
956 struct obd_export *exp = req->rq_export;
957 struct md_attr *ma = &info->mti_attr;
958 struct lu_attr *la = &ma->ma_attr;
961 if (exp_connect_layout(exp))
962 /* the client can deal with 16-bit lmm_stripe_count */
965 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
967 if (!mdt_body_has_lov(la, body))
970 /* now we have a reply with a lov for a client not compatible with the
971 * layout lock so we have to clean the layout generation number */
972 if (S_ISREG(la->la_mode))
973 ma->ma_lmm->lmm_layout_gen = 0;
977 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
978 struct mdt_object *o)
980 const struct lu_env *env = info->mti_env;
983 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
992 /* Is it a directory? Let's check for the LMV as well */
993 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
994 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
998 rc2 = mo_xattr_get(env, mdt_object_child(o),
1000 XATTR_NAME_DEFAULT_LMV);
1002 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
1010 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
1013 const struct lu_env *env = info->mti_env;
1017 LASSERT(info->mti_big_lmm_used == 0);
1018 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
1022 /* big_lmm may need to be grown */
1023 if (info->mti_big_lmmsize < rc) {
1024 int size = size_roundup_power2(rc);
1026 if (info->mti_big_lmmsize > 0) {
1027 /* free old buffer */
1028 LASSERT(info->mti_big_lmm);
1029 OBD_FREE_LARGE(info->mti_big_lmm,
1030 info->mti_big_lmmsize);
1031 info->mti_big_lmm = NULL;
1032 info->mti_big_lmmsize = 0;
1035 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1036 if (info->mti_big_lmm == NULL)
1038 info->mti_big_lmmsize = size;
1040 LASSERT(info->mti_big_lmmsize >= rc);
1042 info->mti_buf.lb_buf = info->mti_big_lmm;
1043 info->mti_buf.lb_len = info->mti_big_lmmsize;
1044 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1049 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1050 struct md_attr *ma, const char *name)
1052 struct md_object *next = mdt_object_child(o);
1053 struct lu_buf *buf = &info->mti_buf;
1056 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1057 buf->lb_buf = ma->ma_lmm;
1058 buf->lb_len = ma->ma_lmm_size;
1059 LASSERT(!(ma->ma_valid & MA_LOV));
1060 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1061 buf->lb_buf = ma->ma_lmv;
1062 buf->lb_len = ma->ma_lmv_size;
1063 LASSERT(!(ma->ma_valid & MA_LMV));
1064 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1065 buf->lb_buf = ma->ma_default_lmv;
1066 buf->lb_len = ma->ma_default_lmv_size;
1067 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1072 LASSERT(buf->lb_buf);
1074 if (!mdt_object_exists(o))
1077 if (mdt_object_remote(o) && S_ISDIR(lu_object_attr(&o->mot_obj)))
1078 /* force reload layout for remote dir in case layout changed */
1079 mo_invalidate(info->mti_env, mdt_object_child(o));
1081 rc = mo_xattr_get(info->mti_env, next, buf, name);
1085 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1086 if (info->mti_big_lmm_used)
1087 ma->ma_lmm = info->mti_big_lmm;
1089 /* NOT return LOV EA with hole to old client. */
1090 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1091 LOV_PATTERN_F_HOLE) &&
1092 !(exp_connect_flags(info->mti_exp) &
1093 OBD_CONNECT_LFSCK)) {
1096 ma->ma_lmm_size = rc;
1097 ma->ma_valid |= MA_LOV;
1099 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1100 if (info->mti_big_lmm_used)
1101 ma->ma_lmv = info->mti_big_lmm;
1103 ma->ma_lmv_size = rc;
1104 ma->ma_valid |= MA_LMV;
1105 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1106 ma->ma_default_lmv_size = rc;
1107 ma->ma_valid |= MA_LMV_DEF;
1110 /* Update mdt_max_mdsize so all clients will be aware that */
1111 if (info->mti_mdt->mdt_max_mdsize < rc)
1112 info->mti_mdt->mdt_max_mdsize = rc;
1115 } else if (rc == -ENODATA) {
1118 } else if (rc == -ERANGE) {
1119 /* Default LMV has fixed size, so it must be able to fit
1120 * in the original buffer */
1121 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1123 rc = mdt_big_xattr_get(info, o, name);
1125 info->mti_big_lmm_used = 1;
1133 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1134 struct md_attr *ma, const char *name)
1138 if (!info->mti_big_lmm) {
1139 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1140 if (!info->mti_big_lmm)
1142 info->mti_big_lmmsize = PAGE_SIZE;
1145 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1146 ma->ma_lmm = info->mti_big_lmm;
1147 ma->ma_lmm_size = info->mti_big_lmmsize;
1148 ma->ma_valid &= ~MA_LOV;
1149 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1150 ma->ma_lmv = info->mti_big_lmm;
1151 ma->ma_lmv_size = info->mti_big_lmmsize;
1152 ma->ma_valid &= ~MA_LMV;
1157 LASSERT(!info->mti_big_lmm_used);
1158 rc = __mdt_stripe_get(info, o, ma, name);
1159 /* since big_lmm is always used here, clear 'used' flag to avoid
1160 * assertion in mdt_big_xattr_get().
1162 info->mti_big_lmm_used = 0;
1167 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1168 struct lu_fid *pfid)
1170 struct lu_buf *buf = &info->mti_buf;
1171 struct link_ea_header *leh;
1172 struct link_ea_entry *lee;
1176 buf->lb_buf = info->mti_big_lmm;
1177 buf->lb_len = info->mti_big_lmmsize;
1178 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1179 buf, XATTR_NAME_LINK);
1180 /* ignore errors, MA_PFID won't be set and it is
1181 * up to the caller to treat this as an error */
1182 if (rc == -ERANGE || buf->lb_len == 0) {
1183 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1184 buf->lb_buf = info->mti_big_lmm;
1185 buf->lb_len = info->mti_big_lmmsize;
1190 if (rc < sizeof(*leh)) {
1191 CERROR("short LinkEA on "DFID": rc = %d\n",
1192 PFID(mdt_object_fid(o)), rc);
1196 leh = (struct link_ea_header *) buf->lb_buf;
1197 lee = (struct link_ea_entry *)(leh + 1);
1198 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1199 leh->leh_magic = LINK_EA_MAGIC;
1200 leh->leh_reccount = __swab32(leh->leh_reccount);
1201 leh->leh_len = __swab64(leh->leh_len);
1203 if (leh->leh_magic != LINK_EA_MAGIC)
1205 if (leh->leh_reccount == 0)
1208 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1209 fid_be_to_cpu(pfid, pfid);
1214 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1215 struct lu_fid *pfid, struct lu_name *lname)
1217 struct lu_buf *buf = &info->mti_buf;
1218 struct link_ea_header *leh;
1219 struct link_ea_entry *lee;
1223 buf->lb_buf = info->mti_xattr_buf;
1224 buf->lb_len = sizeof(info->mti_xattr_buf);
1225 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1227 if (rc == -ERANGE) {
1228 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1229 buf->lb_buf = info->mti_big_lmm;
1230 buf->lb_len = info->mti_big_lmmsize;
1235 if (rc < sizeof(*leh)) {
1236 CERROR("short LinkEA on "DFID": rc = %d\n",
1237 PFID(mdt_object_fid(o)), rc);
1241 leh = (struct link_ea_header *)buf->lb_buf;
1242 lee = (struct link_ea_entry *)(leh + 1);
1243 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1244 leh->leh_magic = LINK_EA_MAGIC;
1245 leh->leh_reccount = __swab32(leh->leh_reccount);
1246 leh->leh_len = __swab64(leh->leh_len);
1248 if (leh->leh_magic != LINK_EA_MAGIC)
1251 if (leh->leh_reccount == 0)
1254 linkea_entry_unpack(lee, &reclen, lname, pfid);
1259 int mdt_attr_get_complex(struct mdt_thread_info *info,
1260 struct mdt_object *o, struct md_attr *ma)
1262 const struct lu_env *env = info->mti_env;
1263 struct md_object *next = mdt_object_child(o);
1264 struct lu_buf *buf = &info->mti_buf;
1265 int need = ma->ma_need;
1272 if (mdt_object_exists(o) == 0)
1273 GOTO(out, rc = -ENOENT);
1274 mode = lu_object_attr(&next->mo_lu);
1276 if (need & MA_INODE) {
1277 ma->ma_need = MA_INODE;
1278 if (need & MA_DIRENT_CNT)
1279 ma->ma_attr.la_valid |= LA_DIRENT_CNT;
1281 ma->ma_attr.la_valid &= ~LA_DIRENT_CNT;
1282 rc = mo_attr_get(env, next, ma);
1287 (void) mdt_get_som(info, o, ma);
1288 ma->ma_valid |= MA_INODE;
1291 if (need & MA_PFID) {
1292 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1294 ma->ma_valid |= MA_PFID;
1295 /* ignore this error, parent fid is not mandatory */
1299 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1300 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1305 if (need & MA_LMV && S_ISDIR(mode)) {
1306 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1311 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1312 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1318 * In the handle of MA_INODE, we may already get the SOM attr.
1320 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1321 rc = mdt_get_som(info, o, ma);
1326 if (need & MA_HSM && S_ISREG(mode)) {
1327 buf->lb_buf = info->mti_xattr_buf;
1328 buf->lb_len = sizeof(info->mti_xattr_buf);
1329 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1330 sizeof(info->mti_xattr_buf));
1331 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1332 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1334 ma->ma_valid |= MA_HSM;
1335 else if (rc2 < 0 && rc2 != -ENODATA)
1336 GOTO(out, rc = rc2);
1339 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1340 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1341 buf->lb_buf = ma->ma_acl;
1342 buf->lb_len = ma->ma_acl_size;
1343 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1345 ma->ma_acl_size = rc2;
1346 ma->ma_valid |= MA_ACL_DEF;
1347 } else if (rc2 == -ENODATA) {
1349 ma->ma_acl_size = 0;
1351 GOTO(out, rc = rc2);
1356 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1357 rc, ma->ma_valid, ma->ma_lmm);
1361 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
1363 struct req_capsule *pill = info->mti_pill;
1366 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
1368 /* pre-set size in server part with max size */
1369 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
1371 info->mti_mdt->mdt_max_mdsize);
1375 static int mdt_getattr_internal(struct mdt_thread_info *info,
1376 struct mdt_object *o, int ma_need)
1378 struct mdt_device *mdt = info->mti_mdt;
1379 struct md_object *next = mdt_object_child(o);
1380 const struct mdt_body *reqbody = info->mti_body;
1381 struct ptlrpc_request *req = mdt_info_req(info);
1382 struct md_attr *ma = &info->mti_attr;
1383 struct lu_attr *la = &ma->ma_attr;
1384 struct req_capsule *pill = info->mti_pill;
1385 const struct lu_env *env = info->mti_env;
1386 struct mdt_body *repbody;
1387 struct lu_buf *buffer = &info->mti_buf;
1388 struct obd_export *exp = info->mti_exp;
1389 ktime_t kstart = ktime_get();
1394 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1395 RETURN(err_serious(-ENOMEM));
1397 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1401 if (mdt_object_remote(o)) {
1402 /* This object is located on remote node.*/
1403 /* Return -ENOTSUPP for old client */
1404 if (!mdt_is_dne_client(req->rq_export))
1405 GOTO(out, rc = -ENOTSUPP);
1407 repbody->mbo_fid1 = *mdt_object_fid(o);
1408 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1412 if (reqbody->mbo_eadatasize > 0) {
1413 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1414 if (buffer->lb_buf == NULL)
1415 GOTO(out, rc = -EPROTO);
1416 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1419 buffer->lb_buf = NULL;
1421 ma_need &= ~(MA_LOV | MA_LMV);
1422 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1423 mdt_obd_name(info->mti_mdt),
1424 req->rq_export->exp_client_uuid.uuid);
1427 /* from 2.12.58 intent_getattr pack default LMV in reply */
1428 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1429 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1430 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1431 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1433 ma->ma_lmv = buffer->lb_buf;
1434 ma->ma_lmv_size = buffer->lb_len;
1435 ma->ma_default_lmv = req_capsule_server_get(pill,
1436 &RMF_DEFAULT_MDT_MD);
1437 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1438 &RMF_DEFAULT_MDT_MD,
1440 ma->ma_need = MA_INODE;
1441 if (ma->ma_lmv_size > 0)
1442 ma->ma_need |= MA_LMV;
1443 if (ma->ma_default_lmv_size > 0)
1444 ma->ma_need |= MA_LMV_DEF;
1445 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1446 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1447 /* If it is dir and client require MEA, then we got MEA */
1448 /* Assumption: MDT_MD size is enough for lmv size. */
1449 ma->ma_lmv = buffer->lb_buf;
1450 ma->ma_lmv_size = buffer->lb_len;
1451 ma->ma_need = MA_INODE;
1452 if (ma->ma_lmv_size > 0) {
1453 if (reqbody->mbo_valid & OBD_MD_MEA) {
1454 ma->ma_need |= MA_LMV;
1455 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1456 ma->ma_need |= MA_LMV_DEF;
1457 ma->ma_default_lmv = buffer->lb_buf;
1459 ma->ma_default_lmv_size = buffer->lb_len;
1460 ma->ma_lmv_size = 0;
1464 ma->ma_lmm = buffer->lb_buf;
1465 ma->ma_lmm_size = buffer->lb_len;
1466 ma->ma_need = MA_INODE | MA_HSM;
1467 if (ma->ma_lmm_size > 0) {
1468 ma->ma_need |= MA_LOV;
1469 /* Older clients may crash if they getattr overstriped
1472 if (!exp_connect_overstriping(exp) &&
1473 mdt_lmm_is_overstriping(ma->ma_lmm))
1474 RETURN(-EOPNOTSUPP);
1478 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1479 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1480 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1481 /* get default stripe info for this dir. */
1482 ma->ma_need |= MA_LOV_DEF;
1484 ma->ma_need |= ma_need;
1486 rc = mdt_attr_get_complex(info, o, ma);
1488 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1489 "%s: getattr error for "DFID": rc = %d\n",
1490 mdt_obd_name(info->mti_mdt),
1491 PFID(mdt_object_fid(o)), rc);
1495 /* return immutable attr on fscrypt metadata files
1496 * if fscrypt admin is not permitted
1498 if (o->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD &&
1499 !mdt_ucred(info)->uc_rbac_fscrypt_admin)
1500 la->la_flags |= LUSTRE_IMMUTABLE_FL;
1502 /* if file is released, check if a restore is running */
1503 if (ma->ma_valid & MA_HSM) {
1504 repbody->mbo_valid |= OBD_MD_TSTATE;
1505 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1506 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1507 repbody->mbo_t_state = MS_RESTORE;
1510 if (unlikely(!(ma->ma_valid & MA_INODE)))
1513 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1515 if (mdt_body_has_lov(la, reqbody)) {
1516 u32 stripe_count = 1;
1517 bool fixed_layout = false;
1519 if (ma->ma_valid & MA_LOV) {
1520 LASSERT(ma->ma_lmm_size);
1521 repbody->mbo_eadatasize = ma->ma_lmm_size;
1522 if (S_ISDIR(la->la_mode))
1523 repbody->mbo_valid |= OBD_MD_FLDIREA;
1525 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1526 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1528 if (ma->ma_valid & MA_LMV) {
1529 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1530 u32 magic = le32_to_cpu(lmv->lmv_magic);
1532 /* Return -ENOTSUPP for old client */
1533 if (!mdt_is_striped_client(req->rq_export))
1536 LASSERT(S_ISDIR(la->la_mode));
1537 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1538 repbody->mbo_eadatasize = ma->ma_lmv_size;
1539 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1541 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1542 fixed_layout = lmv_is_fixed(lmv);
1543 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1544 mdt_restripe_migrate_add(info, o);
1545 else if (magic == LMV_MAGIC_V1 &&
1546 lmv_is_restriping(lmv))
1547 mdt_restripe_update_add(info, o);
1549 if (ma->ma_valid & MA_LMV_DEF) {
1550 /* Return -ENOTSUPP for old client */
1551 if (!mdt_is_striped_client(req->rq_export))
1553 LASSERT(S_ISDIR(la->la_mode));
1555 * when ll_dir_getstripe() gets default LMV, it
1556 * checks mbo_eadatasize.
1558 if (!(ma->ma_valid & MA_LMV))
1559 repbody->mbo_eadatasize =
1560 ma->ma_default_lmv_size;
1561 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1562 OBD_MD_DEFAULT_MEA);
1565 "dirent count %llu stripe count %u MDT count %d\n",
1566 ma->ma_attr.la_dirent_count, stripe_count,
1567 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1568 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1569 ma->ma_attr.la_dirent_count >
1570 mdt->mdt_restriper.mdr_dir_split_count &&
1571 !fid_is_root(mdt_object_fid(o)) &&
1572 mdt->mdt_enable_dir_auto_split &&
1573 !o->mot_restriping &&
1574 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1576 mdt_auto_split_add(info, o);
1577 } else if (S_ISLNK(la->la_mode) &&
1578 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1579 buffer->lb_buf = ma->ma_lmm;
1580 /* eadatasize from client includes NULL-terminator, so
1581 * there is no need to read it */
1582 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1583 rc = mo_readlink(env, next, buffer);
1584 if (unlikely(rc <= 0)) {
1585 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1586 mdt_obd_name(info->mti_mdt),
1587 PFID(mdt_object_fid(o)), rc);
1590 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1592 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1594 repbody->mbo_valid |= OBD_MD_LINKNAME;
1595 /* we need to report back size with NULL-terminator
1596 * because client expects that */
1597 repbody->mbo_eadatasize = rc + 1;
1598 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1599 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1600 "on "DFID ", expected %d\n",
1601 mdt_obd_name(info->mti_mdt),
1602 rc, PFID(mdt_object_fid(o)),
1603 reqbody->mbo_eadatasize - 1);
1604 /* NULL terminate */
1605 ((char *)ma->ma_lmm)[rc] = 0;
1607 /* If the total CDEBUG() size is larger than a page, it
1608 * will print a warning to the console, avoid this by
1609 * printing just the last part of the symlink. */
1610 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1611 print_limit < rc ? "..." : "", print_limit,
1612 (char *)ma->ma_lmm + rc - print_limit, rc);
1617 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1618 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1619 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1620 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1621 repbody->mbo_max_mdsize);
1624 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1625 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1626 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1627 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1628 if (IS_ERR(nodemap))
1629 RETURN(PTR_ERR(nodemap));
1631 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1632 nodemap_putref(nodemap);
1638 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1639 ktime_us_delta(ktime_get(), kstart));
1644 static int mdt_getattr(struct tgt_session_info *tsi)
1646 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1647 struct mdt_object *obj = info->mti_object;
1648 struct req_capsule *pill = info->mti_pill;
1649 struct mdt_body *reqbody;
1650 struct mdt_body *repbody;
1654 if (unlikely(info->mti_object == NULL))
1657 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1659 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1661 /* Special case for Data-on-MDT files to get data version */
1662 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1663 rc = mdt_data_version_get(tsi);
1667 /* Unlike intent case where we need to pre-fill out buffers early on
1668 * in intent policy for ldlm reasons, here we can have a much better
1669 * guess at EA size by just reading it from disk.
1670 * Exceptions are readdir and (missing) directory striping */
1672 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1673 /* No easy way to know how long is the symlink, but it cannot
1674 * be more than PATH_MAX, so we allocate +1 */
1676 /* A special case for fs ROOT: getattr there might fetch
1677 * default EA for entire fs, not just for this dir!
1679 } else if (lu_fid_eq(mdt_object_fid(obj),
1680 &info->mti_mdt->mdt_md_root_fid) &&
1681 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1682 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1684 /* Should the default strping be bigger, mdt_fix_reply
1685 * will reallocate */
1686 rc = DEF_REP_MD_SIZE;
1688 /* Read the actual EA size from disk */
1689 rc = mdt_attr_get_eabuf_size(info, obj);
1693 GOTO(out, rc = err_serious(rc));
1695 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1697 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1698 * by default. If the target object has more ACL entries, then
1699 * enlarge the buffer when necessary. */
1700 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1701 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1702 mdt_preset_encctx_size(info);
1704 rc = req_capsule_server_pack(pill);
1705 if (unlikely(rc != 0))
1706 GOTO(out, rc = err_serious(rc));
1708 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1709 LASSERT(repbody != NULL);
1710 repbody->mbo_eadatasize = 0;
1711 repbody->mbo_aclsize = 0;
1713 rc = mdt_check_ucred(info);
1715 GOTO(out_shrink, rc);
1717 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1719 rc = mdt_init_ucred(info, reqbody);
1721 GOTO(out_shrink, rc);
1723 rc = mdt_getattr_internal(info, obj, 0);
1725 GOTO(out_ucred, rc);
1727 rc = mdt_pack_encctx_in_reply(info, obj);
1730 mdt_exit_ucred(info);
1732 mdt_client_compatibility(info);
1733 rc2 = mdt_fix_reply(info);
1737 mdt_thread_info_fini(info);
1742 * Handler of layout intent RPC requiring the layout modification
1744 * \param[in] info thread environment
1745 * \param[in] obj object
1746 * \param[out] lhc object ldlm lock handle
1747 * \param[in] layout layout change descriptor
1749 * \retval 0 on success
1750 * \retval < 0 error code
1752 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1753 struct mdt_lock_handle *lhc,
1754 struct md_layout_change *layout)
1760 if (!mdt_object_exists(obj))
1763 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1766 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1771 rc = mdt_check_resent_lock(info, obj, lhc);
1777 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1779 /* take layout lock to prepare layout change */
1780 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1781 lockpart |= MDS_INODELOCK_UPDATE;
1783 rc = mdt_object_lock(info, obj, lhc, lockpart, LCK_EX);
1788 mutex_lock(&obj->mot_som_mutex);
1789 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1790 mutex_unlock(&obj->mot_som_mutex);
1793 mdt_object_unlock(info, obj, lhc, 1);
1799 * Exchange MOF_LOV_CREATED flags between two objects after a
1800 * layout swap. No assumption is made on whether o1 or o2 have
1801 * created objects or not.
1803 * \param[in,out] o1 First swap layout object
1804 * \param[in,out] o2 Second swap layout object
1806 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1808 unsigned int o1_lov_created = o1->mot_lov_created;
1810 mutex_lock(&o1->mot_lov_mutex);
1811 mutex_lock(&o2->mot_lov_mutex);
1813 o1->mot_lov_created = o2->mot_lov_created;
1814 o2->mot_lov_created = o1_lov_created;
1816 mutex_unlock(&o2->mot_lov_mutex);
1817 mutex_unlock(&o1->mot_lov_mutex);
1820 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1822 struct mdt_thread_info *info;
1823 struct ptlrpc_request *req = tgt_ses_req(tsi);
1824 struct obd_export *exp = req->rq_export;
1825 struct mdt_object *o1, *o2, *o;
1826 struct mdt_lock_handle *lh1, *lh2;
1827 struct mdc_swap_layouts *msl;
1831 /* client does not support layout lock, so layout swaping
1833 * FIXME: there is a problem for old clients which don't support
1834 * layout lock yet. If those clients have already opened the file
1835 * they won't be notified at all so that old layout may still be
1836 * used to do IO. This can be fixed after file release is landed by
1837 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1838 if (!exp_connect_layout(exp))
1839 RETURN(-EOPNOTSUPP);
1841 info = tsi2mdt_info(tsi);
1842 if (unlikely(info->mti_object == NULL))
1845 if (info->mti_dlm_req != NULL)
1846 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1848 o1 = info->mti_object;
1849 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1850 &info->mti_body->mbo_fid2);
1852 GOTO(out, rc = PTR_ERR(o));
1854 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1855 GOTO(put, rc = -ENOENT);
1857 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1858 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1864 /* permission check. Make sure the calling process having permission
1865 * to write both files. */
1866 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1871 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1876 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1878 GOTO(put, rc = -EPROTO);
1880 lh1 = &info->mti_lh[MDT_LH_NEW];
1881 lh2 = &info->mti_lh[MDT_LH_OLD];
1882 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1883 MDS_INODELOCK_XATTR, LCK_EX);
1887 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1888 MDS_INODELOCK_XATTR, LCK_EX);
1892 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1893 mdt_object_child(o2), msl->msl_flags);
1897 mdt_swap_lov_flag(o1, o2);
1900 mdt_object_unlock(info, o2, lh2, rc);
1902 mdt_object_unlock(info, o1, lh1, rc);
1904 mdt_object_put(info->mti_env, o);
1906 mdt_thread_info_fini(info);
1910 static int mdt_raw_lookup(struct mdt_thread_info *info,
1911 struct mdt_object *parent,
1912 const struct lu_name *lname)
1914 struct lu_fid *fid = &info->mti_tmp_fid1;
1915 struct mdt_body *repbody;
1916 bool is_dotdot = false;
1917 bool is_old_parent_stripe = false;
1918 bool is_new_parent_checked = false;
1923 LASSERT(!info->mti_cross_ref);
1924 /* Always allow to lookup ".." */
1925 if (lname->ln_namelen == 2 &&
1926 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1927 info->mti_spec.sp_permitted = 1;
1929 if (mdt_is_dir_stripe(info, parent) == 1)
1930 is_old_parent_stripe = true;
1933 mdt_object_get(info->mti_env, parent);
1935 /* Only got the fid of this obj by name */
1937 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1939 mdt_object_put(info->mti_env, parent);
1943 /* getattr_name("..") should return master object FID for striped dir */
1944 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1945 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1947 RETURN(PTR_ERR(parent));
1949 /* old client getattr_name("..") with stripe FID */
1950 if (unlikely(is_old_parent_stripe)) {
1951 is_old_parent_stripe = false;
1955 /* ".." may be a stripe */
1956 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1957 is_new_parent_checked = true;
1961 mdt_object_put(info->mti_env, parent);
1964 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1965 repbody->mbo_fid1 = *fid;
1966 repbody->mbo_valid = OBD_MD_FLID;
1972 * Find name matching hash
1974 * We search \a child LinkEA for a name whose hash matches \a lname
1975 * (it contains an encoded hash).
1977 * \param info mdt thread info
1978 * \param lname encoded hash to find
1979 * \param parent parent object
1980 * \param child object to search with LinkEA
1982 * \retval 1 match found
1983 * \retval 0 no match found
1984 * \retval -ev negative errno upon error
1986 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
1987 struct mdt_object *parent, struct mdt_object *child)
1989 /* Here, lname is an encoded hash of on-disk name, and
1990 * client is doing access without encryption key.
1991 * So we need to get LinkEA, check parent fid is correct and
1992 * compare name hash with the one in the request.
1994 struct lu_buf *buf = &info->mti_big_buf;
1995 struct lu_name name;
1997 struct linkea_data ldata = { NULL };
1998 struct link_ea_header *leh;
1999 struct link_ea_entry *lee;
2000 struct lu_buf link = { 0 };
2002 int reclen, count, rc;
2005 if (lname->ln_namelen < LL_CRYPTO_BLOCK_SIZE)
2008 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2013 rc = mdt_links_read(info, child, &ldata);
2017 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
2020 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
2023 lee = (struct link_ea_entry *)(leh + 1);
2024 for (count = 0; count < leh->leh_reccount; count++) {
2025 linkea_entry_unpack(lee, &reclen, &name, &pfid);
2026 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
2027 lu_buf_check_and_alloc(&link, name.ln_namelen);
2029 GOTO(out_match, rc = -ENOMEM);
2030 rc = critical_decode(name.ln_name, name.ln_namelen,
2033 if (memcmp(LLCRYPT_EXTRACT_DIGEST(link.lb_buf, rc),
2034 hash, LL_CRYPTO_BLOCK_SIZE) == 0) {
2039 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2041 if (count == leh->leh_reccount)
2054 * UPDATE lock should be taken against parent, and be released before exit;
2055 * child_bits lock should be taken against child, and be returned back:
2056 * (1)normal request should release the child lock;
2057 * (2)intent request will grant the lock to client.
2059 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2060 struct mdt_lock_handle *lhc,
2062 struct ldlm_reply *ldlm_rep)
2064 struct ptlrpc_request *req = mdt_info_req(info);
2065 struct mdt_body *reqbody = NULL;
2066 struct mdt_object *parent = info->mti_object;
2067 struct mdt_object *child = NULL;
2068 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2069 struct lu_name *lname = NULL;
2070 struct mdt_lock_handle *lhp = NULL;
2071 struct ldlm_lock *lock;
2072 struct req_capsule *pill = info->mti_pill;
2073 bool fscrypt_md = false;
2081 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2082 LASSERT(ergo(is_resent,
2083 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2088 if (info->mti_mdt->mdt_enable_dir_auto_split)
2089 ma_need |= MA_DIRENT_CNT;
2091 if (info->mti_cross_ref) {
2092 /* Only getattr on the child. Parent is on another node. */
2093 mdt_set_disposition(info, ldlm_rep,
2094 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2096 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
2098 PFID(mdt_object_fid(child)), ldlm_rep);
2100 rc = mdt_check_resent_lock(info, child, lhc);
2103 } else if (rc > 0) {
2105 * Object's name entry is on another MDS, it will
2106 * request PERM lock only because LOOKUP lock is owned
2107 * by the MDS where name entry resides.
2109 * TODO: it should try layout lock too. - Jinshan
2111 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2112 MDS_INODELOCK_LAYOUT);
2113 child_bits |= MDS_INODELOCK_PERM;
2114 rc = mdt_object_lock(info, child, lhc, child_bits,
2120 /* Finally, we can get attr for child. */
2121 if (!mdt_object_exists(child)) {
2122 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2124 "remote object doesn't exist.");
2125 mdt_object_unlock(info, child, lhc, 1);
2129 rc = mdt_getattr_internal(info, child, ma_need);
2130 if (unlikely(rc != 0)) {
2131 mdt_object_unlock(info, child, lhc, 1);
2135 rc = mdt_pack_secctx_in_reply(info, child);
2137 mdt_object_unlock(info, child, lhc, 1);
2141 rc = mdt_pack_encctx_in_reply(info, child);
2143 mdt_object_unlock(info, child, lhc, 1);
2147 lname = &info->mti_name;
2148 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2150 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2151 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2152 if (unlikely(reqbody == NULL))
2153 RETURN(err_serious(-EPROTO));
2155 *child_fid = reqbody->mbo_fid2;
2156 if (unlikely(!fid_is_sane(child_fid)))
2157 RETURN(err_serious(-EINVAL));
2159 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2160 mdt_object_get(info->mti_env, parent);
2163 child = mdt_object_find(info->mti_env, info->mti_mdt,
2166 RETURN(PTR_ERR(child));
2169 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2171 PFID(mdt_object_fid(parent)),
2172 PFID(&reqbody->mbo_fid2), ldlm_rep);
2173 } else if (lu_name_is_valid(lname)) {
2174 if (mdt_object_remote(parent)) {
2175 CERROR("%s: parent "DFID" is on remote target\n",
2176 mdt_obd_name(info->mti_mdt),
2177 PFID(mdt_object_fid(parent)));
2181 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
2182 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
2183 PNAME(lname), ldlm_rep);
2185 if (parent->mot_obj.lo_header->loh_attr & LOHA_FSCRYPT_MD ||
2186 (fid_is_root(mdt_object_fid(parent)) &&
2187 lname->ln_namelen == strlen(dot_fscrypt_name) &&
2188 strncmp(lname->ln_name, dot_fscrypt_name,
2189 lname->ln_namelen) == 0))
2192 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2193 if (unlikely(reqbody == NULL))
2194 RETURN(err_serious(-EPROTO));
2196 *child_fid = reqbody->mbo_fid2;
2197 if (unlikely(!fid_is_sane(child_fid)))
2198 RETURN(err_serious(-EINVAL));
2200 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2201 mdt_object_get(info->mti_env, parent);
2204 child = mdt_object_find(info->mti_env, info->mti_mdt,
2207 RETURN(PTR_ERR(child));
2210 if (mdt_object_remote(child)) {
2211 CERROR("%s: child "DFID" is on remote target\n",
2212 mdt_obd_name(info->mti_mdt),
2213 PFID(mdt_object_fid(child)));
2214 GOTO(out_child, rc = -EPROTO);
2217 /* don't fetch LOOKUP lock if it's remote object */
2218 rc = mdt_is_remote_object(info, parent, child);
2220 GOTO(out_child, rc);
2222 child_bits &= ~MDS_INODELOCK_LOOKUP;
2224 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2226 PFID(mdt_object_fid(parent)),
2227 PFID(&reqbody->mbo_fid2), ldlm_rep);
2230 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2232 if (unlikely(!mdt_object_exists(parent)) &&
2233 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2234 lu_name_is_valid(lname)) {
2235 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2237 "Parent doesn't exist!");
2238 GOTO(out_child, rc = -ESTALE);
2241 if (!child && is_resent) {
2242 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2244 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2245 * a resend case, however, it could be already destroyed
2246 * due to client eviction or a raced cancel RPC.
2248 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2249 lhc->mlh_reg_lh.cookie);
2252 fid_extract_from_res_name(child_fid,
2253 &lock->l_resource->lr_name);
2254 LDLM_LOCK_PUT(lock);
2255 child = mdt_object_find(info->mti_env, info->mti_mdt,
2258 RETURN(PTR_ERR(child));
2259 } else if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2260 lu_name_is_valid(lname)) {
2261 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2262 rc = mdt_raw_lookup(info, parent, lname);
2267 /* step 1: lock parent only if parent is a directory */
2268 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2269 lhp = &info->mti_lh[MDT_LH_PARENT];
2270 rc = mdt_parent_lock(info, parent, lhp, lname, LCK_PR);
2271 if (unlikely(rc != 0))
2275 /* step 2: lookup child's fid by name */
2276 fid_zero(child_fid);
2277 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2278 child_fid, &info->mti_spec);
2280 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2283 GOTO(unlock_parent, rc);
2285 child = mdt_object_find(info->mti_env, info->mti_mdt,
2287 if (unlikely(IS_ERR(child)))
2288 GOTO(unlock_parent, rc = PTR_ERR(child));
2291 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2293 /* step 3: lock child regardless if it is local or remote. */
2296 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2297 /* Here, lname is an encoded hash of on-disk name, and
2298 * client is doing access without encryption key.
2299 * So we need to compare name hash with the one in the request.
2301 if (!find_name_matching_hash(info, lname, parent,
2303 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2304 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2305 GOTO(out_child, rc = -ENOENT);
2309 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2310 if (!mdt_object_exists(child)) {
2311 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2313 "Object doesn't exist!");
2314 GOTO(out_child, rc = -ENOENT);
2317 rc = mdt_check_resent_lock(info, child, lhc);
2319 GOTO(out_child, rc);
2320 } else if (rc > 0) {
2321 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2322 !mdt_object_remote(child)) {
2323 struct md_attr *ma = &info->mti_attr;
2326 ma->ma_need = MA_INODE;
2327 rc = mdt_attr_get_complex(info, child, ma);
2328 if (unlikely(rc != 0))
2329 GOTO(out_child, rc);
2331 /* If the file has not been changed for some time, we
2332 * return not only a LOOKUP lock, but also an UPDATE
2333 * lock and this might save us RPC on later STAT. For
2334 * directories, it also let negative dentry cache start
2335 * working for this dir. */
2336 if (ma->ma_valid & MA_INODE &&
2337 ma->ma_attr.la_valid & LA_CTIME &&
2338 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2339 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2340 child_bits |= MDS_INODELOCK_UPDATE;
2343 /* layout lock must be granted in a best-effort way
2344 * for IT operations */
2345 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2346 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2347 !mdt_object_remote(child) && ldlm_rep != NULL) {
2348 if (!CFS_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2349 exp_connect_layout(info->mti_exp)) {
2350 /* try to grant layout lock for regular file. */
2351 try_bits = MDS_INODELOCK_LAYOUT;
2353 /* Acquire DOM lock in advance for data-on-mdt file */
2354 if (child != parent)
2355 try_bits |= MDS_INODELOCK_DOM;
2359 * To avoid possible deadlock between batched statahead RPC
2360 * and rename()/migrate() operation, it should use trylock to
2361 * obtain the DLM PR ibits lock for file attributes in a
2362 * batched statahead RPC. A failed trylock means that other
2363 * users maybe modify the directory simultaneously as in current
2364 * Lustre design the server only grants read lock to a client.
2366 * When a trylock failed, the MDT reports the conflict with
2367 * error code -EBUSY, and stops statahead immediately.
2369 if (info->mti_batch_env) {
2371 * This is a sub stat-ahead request in a batched RPC.
2372 * However, the @child is a remote object, we just
2373 * return -EREMOTE here to forbid stat-ahead on it.
2375 if (mdt_object_remote(child))
2376 GOTO(out_child, rc = -EREMOTE);
2378 try_bits |= child_bits;
2382 if (try_bits != 0) {
2383 /* try layout lock, it may fail to be granted due to
2384 * contention at LOOKUP or UPDATE */
2385 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2387 if (child_bits & MDS_INODELOCK_LAYOUT)
2390 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2391 * client will enqueue the lock to the remote MDT */
2392 if (mdt_object_remote(child))
2393 rc = mdt_object_lookup_lock(info, NULL, child,
2396 rc = mdt_object_lock(info, child, lhc,
2397 child_bits, LCK_PR);
2399 if (unlikely(rc != 0))
2400 GOTO(out_child, rc);
2401 if (info->mti_batch_env && child_bits == 0) {
2403 mdt_object_unlock(info, child, lhc, 1);
2404 GOTO(out_child, rc = -EBUSY);
2409 child->mot_obj.lo_header->loh_attr |= LOHA_FSCRYPT_MD;
2411 /* finally, we can get attr for child. */
2412 rc = mdt_getattr_internal(info, child, ma_need);
2413 if (unlikely(rc != 0)) {
2415 mdt_object_unlock(info, child, lhc, 1);
2416 GOTO(out_child, rc);
2419 rc = mdt_pack_secctx_in_reply(info, child);
2422 mdt_object_unlock(info, child, lhc, 1);
2423 GOTO(out_child, rc);
2426 rc = mdt_pack_encctx_in_reply(info, child);
2429 mdt_object_unlock(info, child, lhc, 1);
2430 GOTO(out_child, rc);
2433 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2435 /* Debugging code. */
2436 LDLM_DEBUG(lock, "Returning lock to client");
2437 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2438 &lock->l_resource->lr_name),
2439 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2440 PLDLMRES(lock->l_resource),
2441 PFID(mdt_object_fid(child)));
2443 if (unlikely(CFS_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2444 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2445 CFS_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2447 req->rq_arrival_time.tv_sec +
2449 /* Put the lock to the waiting list and force the cancel */
2450 ldlm_set_ast_sent(lock);
2453 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2454 !mdt_object_remote(child) && child != parent) {
2455 mdt_object_put(info->mti_env, child);
2456 rc = mdt_pack_size2body(info, child_fid,
2458 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2459 /* DOM lock was taken in advance but this is
2460 * not DoM file. Drop the lock.
2462 lock_res_and_lock(lock);
2463 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2464 unlock_res_and_lock(lock);
2466 LDLM_LOCK_PUT(lock);
2467 GOTO(unlock_parent, rc = 0);
2469 LDLM_LOCK_PUT(lock);
2475 mdt_object_put(info->mti_env, child);
2478 mdt_object_unlock(info, parent, lhp, 1);
2479 if (rc == -ENOENT) {
2480 /* return -ENOKEY instead of -ENOENT to encryption-unaware
2481 * client if trying to access an encrypted file
2483 int rc2 = mdt_check_enc(info, parent);
2491 /* normal handler: should release the child lock */
2492 static int mdt_getattr_name(struct tgt_session_info *tsi)
2494 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2495 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2496 struct mdt_body *reqbody;
2497 struct mdt_body *repbody;
2502 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2503 LASSERT(reqbody != NULL);
2504 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2505 LASSERT(repbody != NULL);
2507 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2508 repbody->mbo_eadatasize = 0;
2509 repbody->mbo_aclsize = 0;
2511 rc = mdt_init_ucred(info, reqbody);
2513 GOTO(out_shrink, rc);
2515 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2516 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2517 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2518 lhc->mlh_reg_lh.cookie = 0;
2520 mdt_exit_ucred(info);
2523 mdt_client_compatibility(info);
2524 rc2 = mdt_fix_reply(info);
2527 mdt_thread_info_fini(info);
2531 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2532 const struct lu_fid *pfid,
2533 const struct lu_name *name,
2534 struct mdt_object *obj, s64 ctime)
2536 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2537 struct ldlm_enqueue_info *einfo = &info->mti_einfo;
2538 struct mdt_device *mdt = info->mti_mdt;
2539 struct md_attr *ma = &info->mti_attr;
2540 struct mdt_lock_handle *parent_lh;
2541 struct mdt_lock_handle *child_lh;
2542 struct mdt_object *pobj;
2546 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2548 GOTO(out, rc = PTR_ERR(pobj));
2550 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2551 rc = mdt_parent_lock(info, pobj, parent_lh, name, LCK_PW);
2553 GOTO(put_parent, rc);
2555 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2556 name, child_fid, &info->mti_spec);
2558 GOTO(unlock_parent, rc);
2560 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2561 GOTO(unlock_parent, rc = -EREMCHG);
2563 child_lh = &info->mti_lh[MDT_LH_CHILD];
2564 rc = mdt_object_stripes_lock(info, pobj, obj, child_lh, einfo,
2565 MDS_INODELOCK_LOOKUP |
2566 MDS_INODELOCK_UPDATE, LCK_EX);
2568 GOTO(unlock_parent, rc);
2570 if (atomic_read(&obj->mot_open_count)) {
2571 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2572 PFID(mdt_object_fid(obj)));
2573 GOTO(unlock_child, rc = -EBUSY);
2577 ma->ma_valid = MA_INODE;
2578 ma->ma_attr.la_valid = LA_CTIME;
2579 ma->ma_attr.la_ctime = ctime;
2581 mutex_lock(&obj->mot_lov_mutex);
2583 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2584 mdt_object_child(obj), name, ma, 0);
2586 mutex_unlock(&obj->mot_lov_mutex);
2589 mdt_object_stripes_unlock(info, obj, child_lh, einfo, 1);
2591 mdt_object_unlock(info, pobj, parent_lh, 1);
2593 mdt_object_put(info->mti_env, pobj);
2598 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2599 struct mdt_object *obj)
2601 struct lu_ucred *uc = lu_ucred(info->mti_env);
2602 struct md_attr *ma = &info->mti_attr;
2603 struct lu_attr *la = &ma->ma_attr;
2607 ma->ma_need = MA_INODE;
2608 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2612 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2615 /* we want rbac roles to have precedence over any other
2616 * permission or capability checks
2618 if (!uc->uc_rbac_byfid_ops)
2620 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2622 if (uc->uc_fsuid == la->la_uid) {
2623 if ((la->la_mode & S_IWUSR) == 0)
2625 } else if (uc->uc_fsgid == la->la_gid) {
2626 if ((la->la_mode & S_IWGRP) == 0)
2628 } else if ((la->la_mode & S_IWOTH) == 0) {
2636 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2639 struct mdt_device *mdt = info->mti_mdt;
2640 struct mdt_object *obj = NULL;
2641 struct linkea_data ldata = { NULL };
2642 struct lu_buf *buf = &info->mti_big_buf;
2643 struct lu_name *name = &info->mti_name;
2644 struct lu_fid *pfid = &info->mti_tmp_fid1;
2645 struct link_ea_header *leh;
2646 struct link_ea_entry *lee;
2647 int reclen, count, rc = 0;
2650 if (!fid_is_sane(fid))
2651 GOTO(out, rc = -EINVAL);
2653 if (!fid_is_namespace_visible(fid))
2654 GOTO(out, rc = -EINVAL);
2656 obj = mdt_object_find(info->mti_env, mdt, fid);
2658 GOTO(out, rc = PTR_ERR(obj));
2660 if (mdt_object_remote(obj))
2661 GOTO(out, rc = -EREMOTE);
2662 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2663 GOTO(out, rc = -ENOENT);
2665 rc = mdt_rmfid_check_permission(info, obj);
2670 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2672 GOTO(out, rc = -ENOMEM);
2675 rc = mdt_links_read(info, obj, &ldata);
2680 lee = (struct link_ea_entry *)(leh + 1);
2681 for (count = 0; count < leh->leh_reccount; count++) {
2682 /* remove every hardlink */
2683 linkea_entry_unpack(lee, &reclen, name, pfid);
2684 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2685 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2691 if (obj && !IS_ERR(obj))
2692 mdt_object_put(info->mti_env, obj);
2693 if (info->mti_big_buf.lb_buf)
2694 lu_buf_free(&info->mti_big_buf);
2699 static int mdt_rmfid(struct tgt_session_info *tsi)
2701 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2702 struct mdt_body *reqbody;
2703 struct lu_fid *fids, *rfids;
2709 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2710 if (reqbody == NULL)
2712 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2714 nr = bufsize / sizeof(struct lu_fid);
2715 if (nr * sizeof(struct lu_fid) != bufsize)
2717 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2718 RCL_SERVER, nr * sizeof(__u32));
2719 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2720 RCL_SERVER, nr * sizeof(struct lu_fid));
2721 rc = req_capsule_server_pack(tsi->tsi_pill);
2723 GOTO(out, rc = err_serious(rc));
2724 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2727 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2729 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2732 mdt_init_ucred(mti, reqbody);
2733 for (i = 0; i < nr; i++) {
2735 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2737 mdt_exit_ucred(mti);
2743 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2744 void *karg, void __user *uarg);
2746 int mdt_io_set_info(struct tgt_session_info *tsi)
2748 struct ptlrpc_request *req = tgt_ses_req(tsi);
2749 struct ost_body *body = NULL, *repbody;
2750 void *key, *val = NULL;
2751 int keylen, vallen, rc = 0;
2752 bool is_grant_shrink;
2756 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2758 DEBUG_REQ(D_HA, req, "no set_info key");
2759 RETURN(err_serious(-EFAULT));
2761 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2764 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2766 DEBUG_REQ(D_HA, req, "no set_info val");
2767 RETURN(err_serious(-EFAULT));
2769 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2772 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
2773 if (is_grant_shrink)
2774 /* In this case the value is actually an RMF_OST_BODY, so we
2775 * transmutate the type of this PTLRPC */
2776 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
2778 rc = req_capsule_server_pack(tsi->tsi_pill);
2782 if (is_grant_shrink) {
2783 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
2785 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2788 /** handle grant shrink, similar to a read request */
2789 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
2792 CERROR("%s: Unsupported key %s\n",
2793 tgt_name(tsi->tsi_tgt), (char *)key);
2801 static int mdt_set_info(struct tgt_session_info *tsi)
2803 struct ptlrpc_request *req = tgt_ses_req(tsi);
2806 int keylen, vallen, rc = 0;
2810 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2812 DEBUG_REQ(D_HA, req, "no set_info key");
2813 RETURN(err_serious(-EFAULT));
2816 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2819 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2821 DEBUG_REQ(D_HA, req, "no set_info val");
2822 RETURN(err_serious(-EFAULT));
2825 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2828 /* Swab any part of val you need to here */
2829 if (KEY_IS(KEY_READ_ONLY)) {
2830 spin_lock(&req->rq_export->exp_lock);
2832 *exp_connect_flags_ptr(req->rq_export) |=
2835 *exp_connect_flags_ptr(req->rq_export) &=
2836 ~OBD_CONNECT_RDONLY;
2837 spin_unlock(&req->rq_export->exp_lock);
2838 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2839 struct changelog_setinfo *cs = val;
2841 if (vallen != sizeof(*cs)) {
2842 CERROR("%s: bad changelog_clear setinfo size %d\n",
2843 tgt_name(tsi->tsi_tgt), vallen);
2846 if (req_capsule_req_need_swab(&req->rq_pill)) {
2847 __swab64s(&cs->cs_recno);
2848 __swab32s(&cs->cs_id);
2851 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
2853 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2855 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2857 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2864 static int mdt_readpage(struct tgt_session_info *tsi)
2866 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2867 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2868 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2869 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2870 struct mdt_body *repbody;
2876 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2877 RETURN(err_serious(-ENOMEM));
2879 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2880 if (repbody == NULL || reqbody == NULL)
2881 RETURN(err_serious(-EFAULT));
2884 * prepare @rdpg before calling lower layers and transfer itself. Here
2885 * reqbody->size contains offset of where to start to read and
2886 * reqbody->nlink contains number bytes to read.
2888 rdpg->rp_hash = reqbody->mbo_size;
2889 if (rdpg->rp_hash != reqbody->mbo_size) {
2890 CERROR("Invalid hash: %#llx != %#llx\n",
2891 rdpg->rp_hash, reqbody->mbo_size);
2895 rdpg->rp_attrs = reqbody->mbo_mode;
2896 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2897 rdpg->rp_attrs |= LUDA_64BITHASH;
2898 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2899 exp_max_brw_size(tsi->tsi_exp));
2900 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2902 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2903 if (rdpg->rp_pages == NULL)
2906 for (i = 0; i < rdpg->rp_npages; ++i) {
2907 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2908 if (rdpg->rp_pages[i] == NULL)
2909 GOTO(free_rdpg, rc = -ENOMEM);
2912 /* call lower layers to fill allocated pages with directory data */
2913 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2915 GOTO(free_rdpg, rc);
2917 /* send pages to client */
2918 rc = tgt_sendpage(tsi, rdpg, rc);
2923 for (i = 0; i < rdpg->rp_npages; i++)
2924 if (rdpg->rp_pages[i] != NULL)
2925 __free_page(rdpg->rp_pages[i]);
2926 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2928 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2934 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2936 struct lu_ucred *uc = mdt_ucred_check(info);
2937 struct lu_attr *attr = &info->mti_attr.ma_attr;
2942 if (op != REINT_SETATTR) {
2943 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2944 attr->la_uid = uc->uc_fsuid;
2945 /* for S_ISGID, inherit gid from his parent, such work will be
2946 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2947 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2948 attr->la_gid = uc->uc_fsgid;
2954 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2956 return op == REINT_OPEN &&
2957 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2960 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2962 struct req_capsule *pill = info->mti_pill;
2964 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2966 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2968 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2970 /* pre-set size in server part with max size */
2971 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2973 req_capsule_ptlreq(pill) ?
2974 OBD_MAX_DEFAULT_EA_SIZE :
2977 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2982 static int mdt_reint_internal(struct mdt_thread_info *info,
2983 struct mdt_lock_handle *lhc,
2986 struct req_capsule *pill = info->mti_pill;
2987 struct mdt_body *repbody;
2992 rc = mdt_reint_unpack(info, op);
2994 CERROR("Can't unpack reint, rc %d\n", rc);
2995 RETURN(err_serious(rc));
2999 /* check if the file system is set to readonly. O_RDONLY open
3000 * is still allowed even the file system is set to readonly mode */
3001 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
3002 RETURN(err_serious(-EROFS));
3004 /* for replay (no_create) lmm is not needed, client has it already */
3005 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3006 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3009 /* llog cookies are always 0, the field is kept for compatibility */
3010 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3011 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
3013 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3014 * by default. If the target object has more ACL entries, then
3015 * enlarge the buffer when necessary. */
3016 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3017 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3018 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3020 mdt_preset_secctx_size(info);
3021 mdt_preset_encctx_size(info);
3023 rc = req_capsule_server_pack(pill);
3025 CERROR("Can't pack response, rc %d\n", rc);
3026 RETURN(err_serious(rc));
3029 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
3030 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
3032 repbody->mbo_eadatasize = 0;
3033 repbody->mbo_aclsize = 0;
3036 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
3038 /* for replay no cookkie / lmm need, because client have this already */
3039 if (info->mti_spec.no_create)
3040 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3041 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
3043 rc = mdt_init_ucred_reint(info);
3045 GOTO(out_shrink, rc);
3047 rc = mdt_fix_attr_ucred(info, op);
3049 GOTO(out_ucred, rc = err_serious(rc));
3051 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
3053 GOTO(out_ucred, rc);
3054 } else if (rc == 1) {
3055 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
3056 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
3057 GOTO(out_ucred, rc);
3059 rc = mdt_reint_rec(info, lhc);
3062 mdt_exit_ucred(info);
3064 mdt_client_compatibility(info);
3066 rc2 = mdt_fix_reply(info);
3071 * Data-on-MDT optimization - read data along with OPEN and return it
3072 * in reply when possible.
3074 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
3075 rc = mdt_dom_read_on_open(info, info->mti_mdt,
3081 static long mdt_reint_opcode(struct ptlrpc_request *req,
3082 const struct req_format **fmt)
3084 struct mdt_device *mdt;
3085 struct mdt_rec_reint *rec;
3088 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
3090 opc = rec->rr_opcode;
3091 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
3092 if (opc < REINT_MAX && fmt[opc] != NULL)
3093 req_capsule_extend(&req->rq_pill, fmt[opc]);
3095 mdt = mdt_exp2dev(req->rq_export);
3096 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
3097 " rc = %d\n", req->rq_export->exp_obd->obd_name,
3098 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
3099 opc = err_serious(-EFAULT);
3102 opc = err_serious(-EFAULT);
3107 static int mdt_reint(struct tgt_session_info *tsi)
3111 static const struct req_format *reint_fmts[REINT_MAX] = {
3112 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
3113 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
3114 [REINT_LINK] = &RQF_MDS_REINT_LINK,
3115 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
3116 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
3117 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
3118 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
3119 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
3120 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
3121 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
3126 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
3128 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3130 * No lock possible here from client to pass it to reint code
3133 rc = mdt_reint_internal(info, NULL, opc);
3134 mdt_thread_info_fini(info);
3139 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
3143 /* this should sync the whole device */
3144 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
3146 struct dt_device *dt = mdt->mdt_bottom;
3150 rc = dt->dd_ops->dt_sync(env, dt);
3154 /* this should sync this object */
3155 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
3156 struct mdt_object *mo)
3162 if (!mdt_object_exists(mo)) {
3163 CWARN("%s: non existing object "DFID": rc = %d\n",
3164 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3169 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3170 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3171 dt_obj_version_t version;
3173 version = dt_version_get(env, mdt_obj2dt(mo));
3174 if (version > tgt->lut_obd->obd_last_committed)
3175 rc = mo_object_sync(env, mdt_object_child(mo));
3177 rc = mo_object_sync(env, mdt_object_child(mo));
3183 static int mdt_sync(struct tgt_session_info *tsi)
3185 struct ptlrpc_request *req = tgt_ses_req(tsi);
3186 struct req_capsule *pill = tsi->tsi_pill;
3187 struct mdt_body *body;
3188 ktime_t kstart = ktime_get();
3193 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3194 RETURN(err_serious(-ENOMEM));
3196 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3197 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3199 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3201 if (unlikely(info->mti_object == NULL))
3204 /* sync an object */
3205 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3208 const struct lu_fid *fid;
3209 struct lu_attr *la = &info->mti_attr.ma_attr;
3211 info->mti_attr.ma_need = MA_INODE;
3212 info->mti_attr.ma_valid = 0;
3213 rc = mdt_attr_get_complex(info, info->mti_object,
3216 body = req_capsule_server_get(pill,
3218 fid = mdt_object_fid(info->mti_object);
3219 mdt_pack_attr2body(info, body, la, fid);
3222 mdt_thread_info_fini(info);
3225 mdt_counter_incr(req, LPROC_MDT_SYNC,
3226 ktime_us_delta(ktime_get(), kstart));
3231 static int mdt_data_sync(struct tgt_session_info *tsi)
3233 struct mdt_thread_info *info;
3234 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3235 struct ost_body *body = tsi->tsi_ost_body;
3236 struct ost_body *repbody;
3237 struct mdt_object *mo = NULL;
3243 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3245 /* if no fid is specified then do nothing,
3246 * device sync is done via MDS_SYNC */
3247 if (fid_is_zero(&tsi->tsi_fid))
3250 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3252 RETURN(PTR_ERR(mo));
3254 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3258 repbody->oa.o_oi = body->oa.o_oi;
3259 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3261 info = tsi2mdt_info(tsi);
3262 ma = &info->mti_attr;
3263 ma->ma_need = MA_INODE;
3265 rc = mdt_attr_get_complex(info, mo, ma);
3267 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3270 mdt_thread_info_fini(info);
3275 mdt_object_put(tsi->tsi_env, mo);
3280 * Handle quota control requests to consult current usage/limit, but also
3281 * to configure quota enforcement
3283 static int mdt_quotactl(struct tgt_session_info *tsi)
3285 struct obd_export *exp = tsi->tsi_exp;
3286 struct req_capsule *pill = tsi->tsi_pill;
3287 struct obd_quotactl *oqctl, *repoqc;
3289 struct mdt_device *mdt = mdt_exp2dev(exp);
3290 struct lu_device *qmt = mdt->mdt_qmt_dev;
3291 struct lu_nodemap *nodemap;
3294 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3296 RETURN(err_serious(-EPROTO));
3298 rc = req_capsule_server_pack(pill);
3300 RETURN(err_serious(rc));
3302 nodemap = nodemap_get_from_exp(exp);
3303 if (IS_ERR(nodemap))
3304 RETURN(PTR_ERR(nodemap));
3306 switch (oqctl->qc_cmd) {
3307 /* master quotactl */
3310 case LUSTRE_Q_SETDEFAULT:
3311 case LUSTRE_Q_SETQUOTAPOOL:
3312 case LUSTRE_Q_SETINFOPOOL:
3313 case LUSTRE_Q_SETDEFAULT_POOL:
3314 case LUSTRE_Q_DELETEQID:
3315 case LUSTRE_Q_RESETQID:
3316 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3318 GOTO(out_nodemap, rc = -EPERM);
3322 case LUSTRE_Q_GETDEFAULT:
3323 case LUSTRE_Q_GETQUOTAPOOL:
3324 case LUSTRE_Q_GETINFOPOOL:
3325 case LUSTRE_Q_GETDEFAULT_POOL:
3327 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3328 /* slave quotactl */
3335 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3336 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3337 GOTO(out_nodemap, rc);
3341 switch (oqctl->qc_type) {
3343 id = nodemap_map_id(nodemap, NODEMAP_UID,
3344 NODEMAP_CLIENT_TO_FS, id);
3347 id = nodemap_map_id(nodemap, NODEMAP_GID,
3348 NODEMAP_CLIENT_TO_FS, id);
3351 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3352 NODEMAP_CLIENT_TO_FS, id);
3355 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3357 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3359 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3361 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3362 barrier_exit(tsi->tsi_tgt->lut_bottom);
3364 if (oqctl->qc_id != id)
3365 swap(oqctl->qc_id, id);
3367 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3368 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3369 RETURN(-EINPROGRESS);
3372 switch (oqctl->qc_cmd) {
3378 case LUSTRE_Q_SETDEFAULT:
3379 case LUSTRE_Q_GETDEFAULT:
3380 case LUSTRE_Q_SETQUOTAPOOL:
3381 case LUSTRE_Q_GETQUOTAPOOL:
3382 case LUSTRE_Q_SETINFOPOOL:
3383 case LUSTRE_Q_GETINFOPOOL:
3384 case LUSTRE_Q_SETDEFAULT_POOL:
3385 case LUSTRE_Q_GETDEFAULT_POOL:
3386 case LUSTRE_Q_DELETEQID:
3387 case LUSTRE_Q_RESETQID:
3388 /* forward quotactl request to QMT */
3389 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3394 /* slave quotactl */
3395 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3400 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3401 GOTO(out_nodemap, rc = -EFAULT);
3404 if (oqctl->qc_id != id)
3405 swap(oqctl->qc_id, id);
3407 QCTL_COPY_NO_PNAME(repoqc, oqctl);
3411 nodemap_putref(nodemap);
3416 /** clone llog ctxt from child (mdd)
3417 * This allows remote llog (replicator) access.
3418 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3419 * context was originally set up, or we can handle them directly.
3420 * I choose the latter, but that means I need any llog
3421 * contexts set up by child to be accessable by the mdt. So we clone the
3422 * context into our context list here.
3424 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3427 struct md_device *next = mdt->mdt_child;
3428 struct llog_ctxt *ctxt;
3431 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3434 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3435 if (rc || ctxt == NULL) {
3439 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3441 CERROR("Can't set mdt ctxt %d\n", rc);
3446 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3447 struct mdt_device *mdt, int idx)
3449 struct llog_ctxt *ctxt;
3451 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3454 /* Put once for the get we just did, and once for the clone */
3455 llog_ctxt_put(ctxt);
3456 llog_ctxt_put(ctxt);
3461 * sec context handlers
3463 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3465 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3471 * quota request handlers
3473 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3475 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3476 struct lu_device *qmt = mdt->mdt_qmt_dev;
3481 RETURN(err_serious(-EOPNOTSUPP));
3483 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3487 struct mdt_object *mdt_object_new(const struct lu_env *env,
3488 struct mdt_device *d,
3489 const struct lu_fid *f)
3491 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3492 struct lu_object *o;
3493 struct mdt_object *m;
3496 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3497 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3498 if (unlikely(IS_ERR(o)))
3499 m = (struct mdt_object *)o;
3505 struct mdt_object *mdt_object_find(const struct lu_env *env,
3506 struct mdt_device *d,
3507 const struct lu_fid *f)
3509 struct lu_object *o;
3510 struct mdt_object *m;
3513 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3514 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3515 if (unlikely(IS_ERR(o)))
3516 m = (struct mdt_object *)o;
3524 * Asyncronous commit for mdt device.
3526 * Pass asynchonous commit call down the MDS stack.
3528 * \param env environment
3529 * \param mdt the mdt device
3531 static void mdt_device_commit_async(const struct lu_env *env,
3532 struct mdt_device *mdt)
3534 struct dt_device *dt = mdt->mdt_bottom;
3538 rc = dt->dd_ops->dt_commit_async(env, dt);
3539 if (unlikely(rc != 0))
3540 CWARN("%s: async commit start failed: rc = %d\n",
3541 mdt_obd_name(mdt), rc);
3542 atomic_inc(&mdt->mdt_async_commit_count);
3547 * Mark the lock as "synchonous".
3549 * Mark the lock to deffer transaction commit to the unlock time.
3551 * \param lock the lock to mark as "synchonous"
3553 * \see mdt_is_lock_sync
3554 * \see mdt_save_lock
3556 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3558 lock->l_ast_data = (void*)1;
3562 * Check whehter the lock "synchonous" or not.
3564 * \param lock the lock to check
3565 * \retval 1 the lock is "synchonous"
3566 * \retval 0 the lock isn't "synchronous"
3568 * \see mdt_set_lock_sync
3569 * \see mdt_save_lock
3571 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3573 return lock->l_ast_data != NULL;
3577 * Blocking AST for mdt locks.
3579 * Starts transaction commit if in case of COS lock conflict or
3580 * deffers such a commit to the mdt_save_lock.
3582 * \param lock the lock which blocks a request or cancelling lock
3583 * \param desc unused
3584 * \param data unused
3585 * \param flag indicates whether this cancelling or blocking callback
3587 * \see ldlm_blocking_ast_nocheck
3589 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3590 void *data, int flag)
3592 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3593 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3594 struct ldlm_cb_set_arg *arg = data;
3595 bool commit_async = false;
3599 if (flag == LDLM_CB_CANCELING)
3602 lock_res_and_lock(lock);
3603 if (lock->l_blocking_ast != mdt_blocking_ast) {
3604 unlock_res_and_lock(lock);
3608 /* A blocking ast may be sent from ldlm_lock_decref_internal
3609 * when the last reference to a local lock was released and
3610 * during blocking event from ldlm_work_bl_ast_lock().
3611 * The 'data' parameter is l_ast_data in the first case and
3612 * callback arguments in the second one. Distinguish them by that.
3614 if (data && data != lock->l_ast_data && arg->bl_desc) {
3615 if (lock->l_req_mode & (LCK_COS | LCK_TXN))
3616 commit_async = true;
3617 else if ((lock->l_req_mode & (LCK_PW | LCK_EX)) &&
3618 ((mdt_cos_is_enabled(mdt) &&
3619 !arg->bl_desc->bl_same_client) ||
3620 (mdt_slc_is_enabled(mdt) &&
3621 arg->bl_desc->bl_txn_dependent)))
3622 mdt_set_lock_sync(lock);
3625 rc = ldlm_blocking_ast_nocheck(lock);
3630 rc = lu_env_init(&env, LCT_LOCAL);
3631 if (unlikely(rc != 0))
3632 CWARN("%s: lu_env initialization failed, cannot "
3633 "start asynchronous commit: rc = %d\n",
3636 mdt_device_commit_async(&env, mdt);
3643 * Blocking AST for cross-MDT lock
3645 * Discard lock from uncommitted_slc_locks and cancel it.
3647 * \param lock the lock which blocks a request or cancelling lock
3648 * \param desc unused
3649 * \param data unused
3650 * \param flag indicates whether this cancelling or blocking callback
3651 * \retval 0 on success
3652 * \retval negative number on error
3654 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3655 void *data, int flag)
3661 case LDLM_CB_BLOCKING: {
3662 struct lustre_handle lockh;
3664 ldlm_lock2handle(lock, &lockh);
3665 rc = ldlm_cli_cancel(&lockh,
3666 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3668 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3673 case LDLM_CB_CANCELING: {
3674 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3675 struct mdt_device *mdt =
3676 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3678 LDLM_DEBUG(lock, "Revoke remote lock");
3680 /* discard slc lock here so that it can be cleaned anytime,
3681 * especially for cleanup_resource() */
3682 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3684 /* once we cache lock, l_ast_data is set to mdt_object */
3685 if (lock->l_ast_data != NULL) {
3686 struct mdt_object *mo = lock->l_ast_data;
3689 rc = lu_env_init(&env, LCT_MD_THREAD);
3690 if (unlikely(rc != 0)) {
3691 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3693 PFID(mdt_object_fid(mo)), rc);
3697 if (lock->l_policy_data.l_inodebits.bits &
3698 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3699 rc = mo_invalidate(&env, mdt_object_child(mo));
3700 mo->mot_cache_attr = 0;
3702 mdt_object_put(&env, mo);
3714 int mdt_check_resent_lock(struct mdt_thread_info *info,
3715 struct mdt_object *mo,
3716 struct mdt_lock_handle *lhc)
3718 /* the lock might already be gotten in ldlm_handle_enqueue() */
3719 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3720 struct ptlrpc_request *req = mdt_info_req(info);
3721 struct ldlm_lock *lock;
3723 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3724 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3726 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3727 * a resend case, however, it could be already destroyed
3728 * due to client eviction or a raced cancel RPC. */
3729 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3730 lhc->mlh_reg_lh.cookie);
3734 if (!fid_res_name_eq(mdt_object_fid(mo),
3735 &lock->l_resource->lr_name)) {
3736 CWARN("%s: Although resent, but still not "
3737 "get child lock:"DFID"\n",
3738 info->mti_exp->exp_obd->obd_name,
3739 PFID(mdt_object_fid(mo)));
3740 LDLM_LOCK_PUT(lock);
3743 LDLM_LOCK_PUT(lock);
3749 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3751 mdt_object_get(NULL, lock->l_ast_data);
3754 static int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3755 struct mdt_object *obj,
3756 struct lustre_handle *lh,
3757 enum ldlm_mode mode,
3758 union ldlm_policy_data *policy,
3759 struct ldlm_res_id *res_id,
3762 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3765 LASSERT(mdt_object_remote(obj));
3767 memset(einfo, 0, sizeof(*einfo));
3768 einfo->ei_type = LDLM_IBITS;
3769 einfo->ei_mode = mode;
3770 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3771 einfo->ei_cb_cp = ldlm_completion_ast;
3772 einfo->ei_enq_slave = 0;
3773 einfo->ei_res_id = res_id;
3774 einfo->ei_req_slot = 1;
3777 * if we cache lock, couple lock with mdt_object, so that object
3778 * can be easily found in lock ASTs.
3780 einfo->ei_cbdata = obj;
3781 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3784 rc = mo_object_lock(mti->mti_env, mdt_object_child(obj), lh, einfo,
3791 /* other components like LFSCK can use lockless access
3792 * and populate cache, so we better invalidate it
3794 if (policy->l_inodebits.bits &
3795 (MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR))
3796 mo_invalidate(mti->mti_env, mdt_object_child(obj));
3802 * Helper function to take PDO and hash lock.
3804 * if \a pdo_lock is false, don't take PDO lock, this is case in rename.
3806 int mdt_object_pdo_lock(struct mdt_thread_info *info, struct mdt_object *obj,
3807 struct mdt_lock_handle *lh, const struct lu_name *name,
3808 enum ldlm_mode mode, bool pdo_lock)
3810 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3811 union ldlm_policy_data *policy = &info->mti_policy;
3812 struct ldlm_res_id *res_id = &info->mti_res_id;
3814 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it is never going to
3815 * be sent to client and we do not want it slowed down due to possible
3818 __u64 dlmflags = LDLM_FL_ATOMIC_CB;
3819 __u64 *cookie = NULL;
3824 /* check for exists after object is locked */
3825 if (!mdt_object_exists(obj))
3826 /* Non-existent object shouldn't have PDO lock */
3829 /* Non-dir object shouldn't have PDO lock */
3830 if (!S_ISDIR(lu_object_attr(&obj->mot_obj)))
3833 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
3834 policy->l_inodebits.try_bits = 0;
3835 policy->l_inodebits.li_gid = 0;
3836 policy->l_inodebits.li_initiator_id = mdt_node_id(info->mti_mdt);
3837 fid_build_reg_res_name(mdt_object_fid(obj), res_id);
3839 cookie = &info->mti_exp->exp_handle.h_cookie;
3841 mdt_lock_pdo_init(lh, mode, name);
3842 mdt_lock_pdo_mode(info, obj, lh);
3843 if (lh->mlh_pdo_mode != LCK_NL) {
3845 if (mdt_object_remote(obj)) {
3846 rc = mdt_remote_object_lock_try(info, obj,
3847 &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
3848 policy, res_id, false);
3849 lh->mlh_pdo_remote = 1;
3851 rc = mdt_fid_lock(info->mti_env, ns,
3852 &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
3853 policy, res_id, dlmflags, cookie);
3856 mdt_object_unlock(info, obj, lh, 1);
3860 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3863 if (mdt_object_remote(obj))
3864 rc = mdt_remote_object_lock_try(info, obj, &lh->mlh_rreg_lh,
3865 lh->mlh_rreg_mode, policy, res_id, false);
3867 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh,
3868 lh->mlh_reg_mode, policy, res_id, dlmflags, cookie);
3870 mdt_object_unlock(info, obj, lh, 1);
3871 else if (CFS_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK) &&
3872 lh->mlh_pdo_hash != 0 &&
3873 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3874 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3879 int mdt_object_lock_internal(struct mdt_thread_info *info,
3880 struct mdt_object *obj, const struct lu_fid *fid,
3881 struct mdt_lock_handle *lh, __u64 *ibits,
3882 __u64 trybits, bool cache)
3884 union ldlm_policy_data *policy = &info->mti_policy;
3885 struct ldlm_res_id *res_id = &info->mti_res_id;
3886 struct lustre_handle *handle;
3889 policy->l_inodebits.bits = *ibits;
3890 policy->l_inodebits.try_bits = trybits;
3891 policy->l_inodebits.li_gid = lh->mlh_gid;
3892 policy->l_inodebits.li_initiator_id = mdt_node_id(info->mti_mdt);
3893 fid_build_reg_res_name(fid, res_id);
3895 if (obj && mdt_object_remote(obj)) {
3896 handle = &lh->mlh_rreg_lh;
3897 LASSERT(!lustre_handle_is_used(handle));
3898 LASSERT(lh->mlh_rreg_mode != LCK_MINMODE);
3899 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3900 rc = mdt_remote_object_lock_try(info, obj, handle,
3901 lh->mlh_rreg_mode, policy,
3904 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3906 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if
3907 * it is going to be sent to client. If it is -
3908 * mdt_intent_policy() path will fix it up and turn FL_LOCAL
3911 __u64 dlmflags = LDLM_FL_ATOMIC_CB | LDLM_FL_LOCAL_ONLY;
3912 __u64 *cookie = NULL;
3914 handle = &lh->mlh_reg_lh;
3915 LASSERT(!lustre_handle_is_used(handle));
3916 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3917 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3919 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3920 if (lh->mlh_type == MDT_REG_LOCK &&
3921 lh->mlh_reg_mode == LCK_EX && *ibits == MDS_INODELOCK_OPEN)
3922 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3926 cookie = &info->mti_exp->exp_handle.h_cookie;
3928 rc = mdt_fid_lock(info->mti_env, ns, handle, lh->mlh_reg_mode,
3929 policy, res_id, dlmflags, cookie);
3931 mdt_object_unlock(info, obj, lh, 1);
3935 struct ldlm_lock *lock;
3937 /* Return successfully acquired bits to a caller */
3938 lock = ldlm_handle2lock(handle);
3940 *ibits = lock->l_policy_data.l_inodebits.bits;
3941 LDLM_LOCK_PUT(lock);
3948 * MDT object locking functions:
3949 * mdt_object_lock(): lock object, this is used in most places, and normally
3950 * lock ibits doesn't contain LOOKUP, unless the caller knows it's not
3952 * mdt_object_check_lock(): lock object with LOOKUP and other ibits, it needs
3953 * to check whether parent is on remote MDT, if so, take LOOKUP on parent
3954 * MDT separately, and then lock other ibits on child object.
3955 * mdt_parent_lock(): take parent UPDATE lock with specific mode, if parent is
3956 * local, take PDO lock by name hash, otherwise take regular lock.
3957 * mdt_object_stripes_lock(): lock object which should be local, and if it's a
3958 * striped directory, lock its stripes, this is called in operations which
3959 * modify both object and stripes.
3960 * mdt_object_lock_try(): lock object with trybits, the trybits contains
3961 * optional inode lock bits that can be granted. This is called by
3962 * getattr/open to fetch more inode lock bits to client, and is also called
3963 * by dir migration to lock link parent in non-block mode to avoid
3970 * this is used to lock object in most places, and normally lock ibits doesn't
3971 * contain LOOKUP, unless the caller knows it's not remote object.
3973 * \param info struct mdt_thread_info
3975 * \param lh lock handle
3976 * \param ibits MDS inode lock bits
3977 * \param mode lock mode
3979 * \retval 0 on success, -ev on error.
3981 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *obj,
3982 struct mdt_lock_handle *lh, __u64 ibits,
3983 enum ldlm_mode mode)
3988 mdt_lock_reg_init(lh, mode);
3989 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh,
3995 * lock object with LOOKUP and other ibits
3997 * it will check whether parent and child are on different MDTs, if so, take
3998 * LOOKUP lock on parent MDT, and lock other ibits on child MDT, otherwise lock
3999 * all ibits on child MDT. Note, parent and child shouldn't be both on remote
4000 * MDTs, in which case specific lock function should be used, and it's in
4001 * rename and migrate only.
4003 * \param info struct mdt_thread_info
4004 * \param parent parent object
4005 * \param child child object
4006 * \param lh lock handle
4007 * \param ibits MDS inode lock bits
4008 * \param mode lock mode
4010 * \retval 0 on success, -ev on error.
4012 int mdt_object_check_lock(struct mdt_thread_info *info,
4013 struct mdt_object *parent, struct mdt_object *child,
4014 struct mdt_lock_handle *lh, __u64 ibits,
4015 enum ldlm_mode mode)
4020 /* if LOOKUP ibit is not set, use mdt_object_lock() */
4021 LASSERT(ibits & MDS_INODELOCK_LOOKUP);
4022 /* if only LOOKUP ibit is needed, use mdt_object_lookup_lock() */
4023 LASSERT(ibits != MDS_INODELOCK_LOOKUP);
4025 /* @parent and @child shouldn't both be on remote MDTs */
4026 LASSERT(!(mdt_object_remote(parent) && mdt_object_remote(child)));
4028 mdt_lock_reg_init(lh, mode);
4029 if (mdt_object_remote(parent) ^ mdt_object_remote(child)) {
4030 __u64 lookup_ibits = MDS_INODELOCK_LOOKUP;
4032 rc = mdt_object_lock_internal(info, parent,
4033 mdt_object_fid(child), lh,
4034 &lookup_ibits, 0, false);
4038 ibits &= ~MDS_INODELOCK_LOOKUP;
4041 rc = mdt_object_lock_internal(info, child, mdt_object_fid(child), lh,
4043 if (rc && !(ibits & MDS_INODELOCK_LOOKUP))
4044 mdt_object_unlock(info, NULL, lh, 1);
4050 * take parent UPDATE lock
4052 * if parent is local or mode is LCK_PW, take PDO lock, otherwise take regular
4055 * \param info struct mdt_thread_info
4056 * \param obj parent object
4057 * \param lh lock handle
4058 * \param lname child name
4059 * \param mode lock mode
4061 * \retval 0 on success, -ev on error.
4063 int mdt_parent_lock(struct mdt_thread_info *info, struct mdt_object *obj,
4064 struct mdt_lock_handle *lh, const struct lu_name *lname,
4065 enum ldlm_mode mode)
4070 LASSERT(obj && lname);
4071 LASSERT(mode == LCK_PW || mode == LCK_PR);
4072 if (mdt_object_remote(obj) && mode == LCK_PR) {
4073 __u64 ibits = MDS_INODELOCK_UPDATE;
4075 mdt_lock_reg_init(lh, mode);
4076 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj),
4077 lh, &ibits, 0, false);
4079 rc = mdt_object_pdo_lock(info, obj, lh, lname, mode, true);
4085 * lock object with trybits
4087 * the trybits contains optional inode lock bits that can be granted. This is
4088 * called by getattr/open to fetch more inode lock bits to client, and is also
4089 * called by dir migration to lock link parent in non-block mode to avoid
4092 * \param info struct mdt_thread_info
4094 * \param lh lock handle
4095 * \param ibits MDS inode lock bits
4096 * \param trybits optional inode lock bits
4097 * \param mode lock mode
4099 * \retval 0 on success, -ev on error.
4101 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *obj,
4102 struct mdt_lock_handle *lh, __u64 *ibits,
4103 __u64 trybits, enum ldlm_mode mode)
4105 bool trylock_only = *ibits == 0;
4109 LASSERT(!(*ibits & trybits));
4110 mdt_lock_reg_init(lh, mode);
4111 rc = mdt_object_lock_internal(info, obj, mdt_object_fid(obj), lh, ibits,
4113 if (rc && trylock_only) { /* clear error for try ibits lock only */
4114 LASSERT(*ibits == 0);
4121 * Helper function to take \a obj LOOKUP lock.
4123 * Both \a pobj and \a obj may be located on remote MDTs.
4125 int mdt_object_lookup_lock(struct mdt_thread_info *info,
4126 struct mdt_object *pobj, struct mdt_object *obj,
4127 struct mdt_lock_handle *lh, enum ldlm_mode mode)
4129 __u64 ibits = MDS_INODELOCK_LOOKUP;
4133 /* if @parent is NULL, it's on local MDT, and @child is remote,
4134 * this is case in getattr/unlink/open by name.
4136 LASSERT(ergo(!pobj, mdt_object_remote(obj)));
4137 mdt_lock_reg_init(lh, mode);
4138 rc = mdt_object_lock_internal(info, pobj, mdt_object_fid(obj), lh,
4144 * Save a lock within request object.
4146 * Keep the lock referenced until whether client ACK or transaction
4147 * commit happens or release the lock immediately depending on input
4148 * parameters. If COS is ON, a write lock is converted to COS lock
4151 * \param info thead info object
4152 * \param h lock handle
4153 * \param mode lock mode
4154 * \param decref force immediate lock releasing
4156 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
4157 enum ldlm_mode mode, int decref)
4159 struct tgt_session_info *tsi = info->mti_env->le_ses ?
4160 tgt_ses_info(info->mti_env) : NULL;
4164 if (lustre_handle_is_used(h)) {
4165 bool has_trans = tsi && tsi->tsi_has_trans;
4167 if (decref || !has_trans || !(mode & (LCK_PW | LCK_EX))) {
4168 mdt_fid_unlock(h, mode);
4170 struct mdt_device *mdt = info->mti_mdt;
4171 struct ldlm_lock *lock = ldlm_handle2lock(h);
4172 struct ptlrpc_request *req = mdt_info_req(info);
4173 bool no_ack = false;
4175 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
4178 /* there is no request if mdt_object_unlock() is called
4179 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
4180 if (likely(req != NULL)) {
4181 LDLM_DEBUG(lock, "save lock request %p reply "
4182 "state %p transno %lld", req,
4183 req->rq_reply_state, req->rq_transno);
4184 if (mdt_cos_is_enabled(mdt)) {
4187 ldlm_lock_mode_downgrade(lock, mode);
4188 } else if (mdt_slc_is_enabled(mdt)) {
4190 if (mode != LCK_TXN) {
4192 ldlm_lock_mode_downgrade(lock,
4196 if (req->rq_export->exp_disconnected)
4197 mdt_fid_unlock(h, mode);
4199 ptlrpc_save_lock(req, h, mode, no_ack);
4201 mdt_fid_unlock(h, mode);
4204 if (mdt_is_lock_sync(lock)) {
4205 CDEBUG(D_HA, "sync_lock, do async commit\n");
4206 mdt_device_commit_async(info->mti_env, mdt);
4208 LDLM_LOCK_PUT(lock);
4217 * Save cross-MDT lock in uncommitted_slc_locks
4219 * Keep the lock referenced until transaction commit happens or release the lock
4220 * immediately depending on input parameters.
4222 * \param info thead info object
4223 * \param h lock handle
4224 * \param mode lock mode
4225 * \param decref force immediate lock releasing
4227 static void mdt_save_remote_lock(struct mdt_thread_info *info,
4228 struct mdt_object *o, struct lustre_handle *h,
4229 enum ldlm_mode mode, int decref)
4233 if (lustre_handle_is_used(h)) {
4234 struct ldlm_lock *lock = ldlm_handle2lock(h);
4235 struct ptlrpc_request *req = mdt_info_req(info);
4238 (lock->l_policy_data.l_inodebits.bits &
4239 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
4240 mo_invalidate(info->mti_env, mdt_object_child(o));
4242 if (decref || !req || !(mode & (LCK_PW | LCK_EX)) ||
4243 !tgt_ses_info(info->mti_env)->tsi_has_trans) {
4244 ldlm_lock_decref_and_cancel(h, mode);
4245 LDLM_LOCK_PUT(lock);
4247 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
4249 ldlm_lock_decref(h, mode);
4258 * Unlock mdt object.
4260 * Immeditely release the regular lock and the PDO lock or save the
4261 * lock in request and keep them referenced until client ACK or
4262 * transaction commit.
4264 * \param info thread info object
4265 * \param o mdt object
4266 * \param lh mdt lock handle referencing regular and PDO locks
4267 * \param decref force immediate lock releasing
4269 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
4271 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
4272 struct mdt_lock_handle *lh, int decref)
4276 if (lh->mlh_pdo_remote)
4277 mdt_save_remote_lock(info, o, &lh->mlh_pdo_lh,
4278 lh->mlh_pdo_mode, decref);
4280 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
4281 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
4282 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4288 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4289 const struct lu_fid *f,
4290 struct mdt_lock_handle *lh,
4291 __u64 ibits, enum ldlm_mode mode)
4293 struct mdt_object *o;
4295 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4299 rc = mdt_object_lock(info, o, lh, ibits, mode);
4301 mdt_object_put(info->mti_env, o);
4308 void mdt_object_unlock_put(struct mdt_thread_info * info,
4309 struct mdt_object *o,
4310 struct mdt_lock_handle *lh,
4313 mdt_object_unlock(info, o, lh, decref);
4314 mdt_object_put(info->mti_env, o);
4318 * Generic code handling requests that have struct mdt_body passed in:
4320 * - extract mdt_body from request and save it in @info, if present;
4322 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4325 * - if HAS_BODY flag is set for this request type check whether object
4326 * actually exists on storage (lu_object_exists()).
4329 static int mdt_body_unpack(struct mdt_thread_info *info,
4330 enum tgt_handler_flags flags)
4332 const struct mdt_body *body;
4333 struct mdt_object *obj;
4334 const struct lu_env *env;
4335 struct req_capsule *pill;
4339 env = info->mti_env;
4340 pill = info->mti_pill;
4342 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4346 if (!(body->mbo_valid & OBD_MD_FLID))
4349 if (!fid_is_sane(&body->mbo_fid1)) {
4350 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4354 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4356 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4357 mdt_object_put(env, obj);
4360 info->mti_object = obj;
4369 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4370 enum tgt_handler_flags flags)
4372 struct req_capsule *pill = info->mti_pill;
4377 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4378 rc = mdt_body_unpack(info, flags);
4382 if (rc == 0 && (flags & HAS_REPLY)) {
4384 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4385 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4386 req_capsule_ptlreq(pill) ?
4387 DEF_REP_MD_SIZE : MAX_MD_SIZE_OLD);
4389 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4390 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4393 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4394 * by default. If the target object has more ACL entries, then
4395 * enlarge the buffer when necessary. */
4396 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4397 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4398 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4400 mdt_preset_secctx_size(info);
4401 mdt_preset_encctx_size(info);
4403 rc = req_capsule_server_pack(pill);
4405 CWARN("%s: cannot pack response: rc = %d\n",
4406 mdt_obd_name(info->mti_mdt), rc);
4411 void mdt_thread_info_reset(struct mdt_thread_info *info)
4413 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4414 info->mti_body = NULL;
4415 info->mti_dlm_req = NULL;
4416 info->mti_cross_ref = 0;
4417 info->mti_opdata = 0;
4418 info->mti_big_lmm_used = 0;
4419 info->mti_big_acl_used = 0;
4420 info->mti_som_strict = 0;
4422 info->mti_spec.no_create = 0;
4423 info->mti_spec.sp_rm_entry = 0;
4424 info->mti_spec.sp_permitted = 0;
4426 info->mti_spec.u.sp_ea.eadata = NULL;
4427 info->mti_spec.u.sp_ea.eadatalen = 0;
4429 if (info->mti_batch_env && info->mti_object != NULL) {
4430 mdt_object_put(info->mti_env, info->mti_object);
4431 info->mti_object = NULL;
4436 * Initialize fields of struct mdt_thread_info. Other fields are left in
4437 * uninitialized state, because it's too expensive to zero out whole
4438 * mdt_thread_info (> 1K) on each request arrival.
4440 void mdt_thread_info_init(struct ptlrpc_request *req,
4441 struct mdt_thread_info *info)
4443 info->mti_pill = &req->rq_pill;
4445 /* mdt device: it can be NULL while CONNECT */
4446 if (req->rq_export) {
4447 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4448 info->mti_exp = req->rq_export;
4450 info->mti_mdt = NULL;
4451 info->mti_env = req->rq_svc_thread->t_env;
4452 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4453 info->mti_big_buf = LU_BUF_NULL;
4454 info->mti_batch_env = 0;
4455 info->mti_object = NULL;
4457 mdt_thread_info_reset(info);
4460 void mdt_thread_info_fini(struct mdt_thread_info *info)
4464 if (info->mti_object != NULL) {
4465 mdt_object_put(info->mti_env, info->mti_object);
4466 info->mti_object = NULL;
4469 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4470 mdt_lock_handle_assert(&info->mti_lh[i]);
4471 info->mti_env = NULL;
4472 info->mti_pill = NULL;
4473 info->mti_exp = NULL;
4474 info->mti_mdt = NULL;
4476 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4477 lu_buf_free(&info->mti_big_buf);
4480 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4482 struct mdt_thread_info *mti;
4484 mti = mdt_th_info(tsi->tsi_env);
4485 LASSERT(mti != NULL);
4487 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4488 if (tsi->tsi_corpus != NULL) {
4489 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4490 lu_object_get(tsi->tsi_corpus);
4492 mti->mti_body = tsi->tsi_mdt_body;
4493 mti->mti_dlm_req = tsi->tsi_dlm_req;
4498 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4500 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4502 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4503 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4505 return tgt_connect(tsi);
4508 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4509 struct mdt_thread_info *info,
4510 struct ldlm_lock **lockp, __u64 flags)
4512 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4515 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4516 struct mdt_thread_info *info,
4517 struct ldlm_lock **lockp, __u64 flags)
4519 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4523 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4524 struct ldlm_lock **lockp,
4525 struct mdt_lock_handle *lh,
4526 __u64 flags, int result)
4528 struct ptlrpc_request *req = mdt_info_req(info);
4529 struct ldlm_lock *lock = *lockp;
4530 struct ldlm_lock *new_lock;
4532 /* If possible resent found a lock, @lh is set to its handle */
4533 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4535 if (new_lock == NULL) {
4536 if (flags & LDLM_FL_INTENT_ONLY) {
4538 } else if (flags & LDLM_FL_RESENT) {
4539 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4540 * resend case, however, it could be already destroyed
4541 * due to client eviction or a raced cancel RPC.
4543 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4544 lh->mlh_reg_lh.cookie);
4547 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4548 mdt_obd_name(info->mti_mdt),
4549 lh->mlh_reg_lh.cookie, flags,
4550 PFID(&info->mti_tmp_fid1),
4551 PFID(&info->mti_tmp_fid2), result);
4554 lh->mlh_reg_lh.cookie = 0;
4559 * If we've already given this lock to a client once, then we should
4560 * have no readers or writers. Otherwise, we should have one reader
4561 * _or_ writer ref (which will be zeroed below) before returning the
4564 if (new_lock->l_export == req->rq_export) {
4565 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4567 LASSERT(new_lock->l_export == NULL);
4568 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4573 if (new_lock->l_export == req->rq_export) {
4575 * Already gave this to the client, which means that we
4576 * reconstructed a reply.
4578 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4581 LDLM_LOCK_RELEASE(new_lock);
4582 lh->mlh_reg_lh.cookie = 0;
4583 RETURN(ELDLM_LOCK_REPLACED);
4587 * Fixup the lock to be given to the client.
4589 lock_res_and_lock(new_lock);
4590 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4591 * possible blocking AST. */
4592 while (new_lock->l_readers > 0) {
4593 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4594 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4595 new_lock->l_readers--;
4597 while (new_lock->l_writers > 0) {
4598 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4599 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4600 new_lock->l_writers--;
4603 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4604 new_lock->l_blocking_ast = lock->l_blocking_ast;
4605 new_lock->l_completion_ast = lock->l_completion_ast;
4606 if (ldlm_has_dom(new_lock))
4607 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4608 new_lock->l_remote_handle = lock->l_remote_handle;
4609 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4611 unlock_res_and_lock(new_lock);
4613 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4614 &new_lock->l_remote_handle,
4615 &new_lock->l_exp_hash);
4617 LDLM_LOCK_RELEASE(new_lock);
4618 lh->mlh_reg_lh.cookie = 0;
4620 RETURN(ELDLM_LOCK_REPLACED);
4623 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4624 struct ldlm_lock *new_lock,
4625 struct mdt_lock_handle *lh, __u64 flags)
4627 struct ptlrpc_request *req = mdt_info_req(info);
4628 struct ldlm_request *dlmreq;
4630 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4633 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4635 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4636 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4638 if (flags & LDLM_FL_RESENT) {
4639 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4640 lh->mlh_reg_mode = new_lock->l_granted_mode;
4642 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4643 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4644 lh->mlh_reg_lh.cookie);
4649 * If the xid matches, then we know this is a resent request, and allow
4650 * it. (It's probably an OPEN, for which we don't send a lock.
4652 if (req_can_reconstruct(req, NULL) != 0)
4656 * This remote handle isn't enqueued, so we never received or processed
4657 * this request. Clear MSG_RESENT, because it can be handled like any
4658 * normal request now.
4660 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4662 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4663 dlmreq->lock_handle[0].cookie);
4666 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4667 struct mdt_thread_info *info,
4668 struct ldlm_lock **lockp,
4671 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4672 struct ldlm_reply *ldlm_rep = NULL;
4677 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4678 * (for the resend case) or a new lock. Below we will use it to
4679 * replace the original lock.
4681 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4682 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4683 rc = mdt_object_lock(info, info->mti_object, lhc,
4684 MDS_INODELOCK_XATTR, (*lockp)->l_req_mode);
4689 rc = mdt_getxattr(info);
4691 if (mdt_info_req(info)->rq_repmsg != NULL)
4692 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4694 if (ldlm_rep == NULL ||
4695 CFS_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4696 mdt_object_unlock(info, info->mti_object, lhc, 1);
4700 RETURN(err_serious(-EFAULT));
4703 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4705 /* This is left for interop instead of adding a new interop flag.
4707 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4708 if (ldlm_rep->lock_policy_res2) {
4709 mdt_object_unlock(info, info->mti_object, lhc, 1);
4710 RETURN(ELDLM_LOCK_ABORTED);
4714 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4718 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4719 struct mdt_thread_info *info,
4720 struct ldlm_lock **lockp,
4723 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4725 struct ldlm_reply *ldlm_rep;
4726 struct mdt_body *reqbody;
4727 struct mdt_body *repbody;
4731 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4734 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4737 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4738 repbody->mbo_eadatasize = 0;
4739 repbody->mbo_aclsize = 0;
4743 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4746 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4750 CERROR("%s: unsupported intent %#x\n",
4751 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4752 GOTO(out_shrink, rc = -EINVAL);
4755 rc = mdt_init_ucred(info, reqbody);
4757 GOTO(out_shrink, rc);
4759 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4760 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4762 /* Get lock from request for possible resent case. */
4763 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4765 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4766 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4768 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG) &&
4769 ldlm_rep->lock_policy_res2 != -ENOKEY)
4770 ldlm_rep->lock_policy_res2 = 0;
4771 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4772 ldlm_rep->lock_policy_res2) {
4773 lhc->mlh_reg_lh.cookie = 0ull;
4774 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4777 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4780 mdt_exit_ucred(info);
4782 mdt_client_compatibility(info);
4783 rc2 = mdt_fix_reply(info);
4789 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4790 struct mdt_thread_info *info,
4791 struct ldlm_lock **lockp,
4794 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4795 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4796 struct layout_intent *intent;
4797 struct ldlm_reply *ldlm_rep;
4798 struct lu_fid *fid = &info->mti_tmp_fid2;
4799 struct mdt_object *obj = NULL;
4800 int layout_size = 0;
4801 struct lu_buf *buf = &layout.mlc_buf;
4806 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4808 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4812 CDEBUG(D_INFO, DFID "got layout change request from client: "
4813 "opc:%u flags:%#x extent "DEXT"\n",
4814 PFID(fid), intent->lai_opc, intent->lai_flags,
4815 PEXT(&intent->lai_extent));
4817 switch (intent->lai_opc) {
4818 case LAYOUT_INTENT_TRUNC:
4819 case LAYOUT_INTENT_WRITE:
4820 case LAYOUT_INTENT_PCCRO_SET:
4821 case LAYOUT_INTENT_PCCRO_CLEAR:
4822 layout.mlc_opc = MD_LAYOUT_WRITE;
4823 layout.mlc_intent = intent;
4825 case LAYOUT_INTENT_ACCESS:
4827 case LAYOUT_INTENT_READ:
4828 case LAYOUT_INTENT_GLIMPSE:
4829 case LAYOUT_INTENT_RELEASE:
4830 case LAYOUT_INTENT_RESTORE:
4831 CERROR("%s: Unsupported layout intent opc %d\n",
4832 mdt_obd_name(info->mti_mdt), intent->lai_opc);
4835 CERROR("%s: Unknown layout intent opc %d\n",
4836 mdt_obd_name(info->mti_mdt), intent->lai_opc);
4840 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4842 RETURN(PTR_ERR(obj));
4844 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4845 /* if layout is going to be changed don't use the current EA
4846 * size but the maximum one. That buffer will be shrinked
4847 * to the actual size in req_capsule_shrink() before reply.
4849 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4850 layout_size = info->mti_mdt->mdt_max_mdsize;
4852 layout_size = mdt_attr_get_eabuf_size(info, obj);
4853 if (layout_size < 0)
4854 GOTO(out, rc = layout_size);
4856 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4857 info->mti_mdt->mdt_max_mdsize = layout_size;
4859 CDEBUG(D_INFO, "%s: layout_size %d\n",
4860 mdt_obd_name(info->mti_mdt), layout_size);
4864 * set reply buffer size, so that ldlm_handle_enqueue0()->
4865 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4867 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4869 rc = req_capsule_server_pack(info->mti_pill);
4873 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4875 GOTO(out, rc = -EPROTO);
4877 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4879 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4880 if (layout.mlc_opc == MD_LAYOUT_NOP)
4883 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4887 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4888 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4894 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4895 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4897 buf->lb_len = req_capsule_get_size(info->mti_pill,
4898 &RMF_EADATA, RCL_CLIENT);
4900 * If it's a replay of layout write intent RPC, the client has
4901 * saved the extended lovea when it get reply then.
4903 if (buf->lb_len > 0)
4904 mdt_fix_lov_magic(info, buf->lb_buf);
4907 /* Get lock from request for possible resent case. */
4908 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4909 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4912 * Instantiate some layout components, if @buf contains lovea, then it's
4913 * a replay of the layout intent write RPC.
4915 rc = mdt_layout_change(info, obj, lhc, &layout);
4916 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4918 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4919 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4920 if (rc == ELDLM_LOCK_REPLACED &&
4921 (*lockp)->l_granted_mode == LCK_EX)
4922 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4927 mdt_object_put(info->mti_env, obj);
4931 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4932 struct mdt_thread_info *info,
4933 struct ldlm_lock **lockp,
4936 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4937 struct ldlm_reply *rep = NULL;
4940 struct ptlrpc_request *req = mdt_info_req(info);
4942 static const struct req_format *intent_fmts[REINT_MAX] = {
4943 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4944 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4949 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4953 /* Get lock from request for possible resent case. */
4954 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4956 rc = mdt_reint_internal(info, lhc, opc);
4958 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4959 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4961 /* Check whether the reply has been packed successfully. */
4962 if (mdt_info_req(info)->rq_repmsg != NULL)
4963 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4968 RETURN(err_serious(-EFAULT));
4971 /* MDC expects this in any case */
4973 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4975 /* the open lock or the lock for cross-ref object should be
4976 * returned to the client */
4977 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4978 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4979 rep->lock_policy_res2 = 0;
4980 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4984 rep->lock_policy_res2 = clear_serious(rc);
4986 if (rep->lock_policy_res2 == -ENOENT &&
4987 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4988 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4989 rep->lock_policy_res2 = 0;
4991 lhc->mlh_reg_lh.cookie = 0ull;
4992 if (rc == -ENOTCONN || rc == -ENODEV ||
4993 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4995 * If it is the disconnect error (ENODEV & ENOCONN), the error
4996 * will be returned by rq_status, and client at ptlrpc layer
4997 * will detect this, then disconnect, reconnect the import
4998 * immediately, instead of impacting the following the rpc.
5003 * For other cases, the error will be returned by intent, and client
5004 * will retrieve the result from intent.
5006 RETURN(ELDLM_LOCK_ABORTED);
5009 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
5010 struct mdt_thread_info *info,
5011 struct ldlm_lock **lockp,
5012 u64 flags /* LDLM_FL_* */)
5014 struct req_capsule *pill = info->mti_pill;
5015 struct ptlrpc_request *req = mdt_info_req(info);
5016 const struct req_format *it_format;
5017 int (*it_handler)(enum ldlm_intent_flags,
5018 struct mdt_thread_info *,
5019 struct ldlm_lock **,
5021 enum tgt_handler_flags it_handler_flags = 0;
5022 struct ldlm_reply *rep;
5023 bool check_mdt_object = false;
5029 case IT_OPEN|IT_CREAT:
5031 * OCREAT is not a IS_MUTABLE request since the file may
5032 * already exist. We do the extra check of
5033 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
5034 * really need to create the object.
5036 it_format = &RQF_LDLM_INTENT;
5037 it_handler = &mdt_intent_open;
5040 check_mdt_object = true;
5043 it_format = &RQF_LDLM_INTENT_GETATTR;
5044 it_handler = &mdt_intent_getattr;
5045 it_handler_flags = HAS_REPLY;
5048 check_mdt_object = true;
5049 it_format = &RQF_LDLM_INTENT_GETXATTR;
5050 it_handler = &mdt_intent_getxattr;
5051 it_handler_flags = HAS_BODY;
5054 it_format = &RQF_LDLM_INTENT_LAYOUT;
5055 it_handler = &mdt_intent_layout;
5058 it_format = &RQF_LDLM_INTENT;
5059 it_handler = &mdt_intent_glimpse;
5062 it_format = &RQF_LDLM_INTENT;
5063 it_handler = &mdt_intent_brw;
5065 case IT_QUOTA_DQACQ:
5066 case IT_QUOTA_CONN: {
5067 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
5070 RETURN(-EOPNOTSUPP);
5072 if (mdt_rdonly(req->rq_export))
5075 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
5076 /* pass the request to quota master */
5077 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
5078 mdt_info_req(info), lockp,
5083 CERROR("%s: unknown intent code %#x\n",
5084 mdt_obd_name(info->mti_mdt), it_opc);
5088 if (!info->mti_batch_env)
5089 req_capsule_extend(pill, it_format);
5091 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
5095 if (unlikely(info->mti_object == NULL && check_mdt_object))
5098 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
5101 CFS_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
5103 /* execute policy */
5104 rc = (*it_handler)(it_opc, info, lockp, flags);
5106 /* Check whether the reply has been packed successfully. */
5107 if (info->mti_batch_env || req->rq_repmsg != NULL) {
5108 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
5109 rep->lock_policy_res2 =
5110 ptlrpc_status_hton(rep->lock_policy_res2);
5116 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
5117 enum ldlm_intent_flags it_opc)
5119 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
5121 /* update stats when IT code is known */
5122 if (srv_stats != NULL)
5123 lprocfs_counter_incr(srv_stats,
5124 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
5125 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
5128 static int mdt_intent_policy(const struct lu_env *env,
5129 struct ldlm_namespace *ns,
5130 struct ldlm_lock **lockp,
5132 enum ldlm_mode mode,
5133 __u64 flags, void *data)
5135 struct tgt_session_info *tsi;
5136 struct mdt_thread_info *info;
5137 struct ptlrpc_request *req = req_cookie;
5138 struct ldlm_intent *it;
5139 struct req_capsule *pill;
5140 const struct ldlm_lock_desc *ldesc;
5145 LASSERT(req != NULL);
5147 tsi = tgt_ses_info(env);
5149 info = mdt_th_info(env);
5150 LASSERT(info != NULL);
5152 /* Check whether it is a sub request processing in a batch request */
5153 if (info->mti_batch_env) {
5154 pill = info->mti_pill;
5155 LASSERT(pill == &info->mti_sub_pill);
5157 info = tsi2mdt_info(tsi);
5158 pill = info->mti_pill;
5161 LASSERT(pill->rc_req == req);
5162 ldesc = &info->mti_dlm_req->lock_desc;
5164 if (info->mti_batch_env ||
5165 req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
5167 * For batch processing environment, the request format has
5170 if (!info->mti_batch_env)
5171 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
5173 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
5175 mdt_ptlrpc_stats_update(req, it->opc);
5176 rc = mdt_intent_opc(it->opc, info, lockp, flags);
5180 /* Lock without inodebits makes no sense and will oops
5181 * later in ldlm. Let's check it now to see if we have
5182 * ibits corrupted somewhere in mdt_intent_opc().
5183 * The case for client miss to set ibits has been
5184 * processed by others. */
5185 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
5186 ldesc->l_policy_data.l_inodebits.bits != 0));
5188 rc = err_serious(-EFAULT);
5190 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
5191 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
5192 struct ldlm_reply *rep;
5194 /* No intent was provided but INTENT flag is set along with
5195 * DOM bit, this is considered as GLIMPSE request.
5196 * This logic is common for MDT and OST glimpse
5198 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
5199 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
5200 /* Check whether the reply has been packed successfully. */
5201 if (req->rq_repmsg != NULL) {
5202 rep = req_capsule_server_get(info->mti_pill,
5204 rep->lock_policy_res2 =
5205 ptlrpc_status_hton(rep->lock_policy_res2);
5208 /* No intent was provided */
5209 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
5210 rc = req_capsule_server_pack(pill);
5212 rc = err_serious(rc);
5215 if (!info->mti_batch_env)
5216 mdt_thread_info_fini(info);
5220 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
5222 struct seq_server_site *ss = mdt_seq_site(mdt);
5224 if (ss->ss_node_id == 0)
5227 if (ss->ss_client_seq != NULL) {
5228 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5229 ss->ss_client_seq->lcs_exp = NULL;
5232 if (ss->ss_server_fld != NULL) {
5233 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
5234 ss->ss_server_fld->lsf_control_exp = NULL;
5238 static void mdt_seq_fini_cli(struct mdt_device *mdt)
5240 struct seq_server_site *ss = mdt_seq_site(mdt);
5245 if (ss->ss_server_seq != NULL)
5246 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
5249 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
5251 mdt_seq_fini_cli(mdt);
5252 mdt_deregister_seq_exp(mdt);
5254 return seq_site_fini(env, mdt_seq_site(mdt));
5258 * It will retrieve its FLDB entries from MDT0, and it only happens
5259 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
5260 * and it needs to refresh FLDB from the MDT0.
5262 static int mdt_register_lwp_callback(void *data)
5265 struct mdt_device *mdt = data;
5266 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
5270 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
5272 rc = lu_env_init(&env, LCT_MD_THREAD);
5274 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
5278 /* Allocate new sequence now to avoid creating local transaction
5279 * in the normal transaction process */
5280 rc = seq_server_check_and_alloc_super(&env,
5281 mdt_seq_site(mdt)->ss_server_seq);
5286 rc = fld_update_from_controller(&env, fld);
5288 CERROR("%s: cannot update controller: rc = %d\n",
5289 mdt_obd_name(mdt), rc);
5298 static int mdt_register_seq_exp(struct mdt_device *mdt)
5300 struct seq_server_site *ss = mdt_seq_site(mdt);
5301 char *lwp_name = NULL;
5304 if (ss->ss_node_id == 0)
5307 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5308 if (lwp_name == NULL)
5309 GOTO(out_free, rc = -ENOMEM);
5311 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5315 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5320 rc = lustre_register_lwp_item(lwp_name,
5321 &ss->ss_server_fld->lsf_control_exp,
5322 mdt_register_lwp_callback, mdt);
5324 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5325 ss->ss_client_seq->lcs_exp = NULL;
5329 if (lwp_name != NULL)
5330 OBD_FREE(lwp_name, MAX_OBD_NAME);
5336 * Init client sequence manager which is used by local MDS to talk to sequence
5337 * controller on remote node.
5339 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5341 struct seq_server_site *ss = mdt_seq_site(mdt);
5345 /* check if this is adding the first MDC and controller is not yet
5347 OBD_ALLOC_PTR(ss->ss_client_seq);
5348 if (ss->ss_client_seq == NULL)
5351 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5352 if (prefix == NULL) {
5353 OBD_FREE_PTR(ss->ss_client_seq);
5354 ss->ss_client_seq = NULL;
5358 /* Note: seq_client_fini will be called in seq_site_fini */
5359 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5360 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5361 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5363 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5365 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5368 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5370 struct seq_server_site *ss;
5374 ss = mdt_seq_site(mdt);
5375 /* init sequence controller server(MDT0) */
5376 if (ss->ss_node_id == 0) {
5377 OBD_ALLOC_PTR(ss->ss_control_seq);
5378 if (ss->ss_control_seq == NULL)
5381 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5382 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5385 GOTO(out_seq_fini, rc);
5388 /* Init normal sequence server */
5389 OBD_ALLOC_PTR(ss->ss_server_seq);
5390 if (ss->ss_server_seq == NULL)
5391 GOTO(out_seq_fini, rc = -ENOMEM);
5393 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5394 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5396 GOTO(out_seq_fini, rc);
5398 /* init seq client for seq server to talk to seq controller(MDT0) */
5399 rc = mdt_seq_init_cli(env, mdt);
5401 GOTO(out_seq_fini, rc);
5403 if (ss->ss_node_id != 0)
5404 /* register controller export through lwp */
5405 rc = mdt_register_seq_exp(mdt);
5410 mdt_seq_fini(env, mdt);
5418 static int mdt_fld_fini(const struct lu_env *env,
5419 struct mdt_device *m)
5421 struct seq_server_site *ss = mdt_seq_site(m);
5424 if (ss && ss->ss_server_fld) {
5425 fld_server_fini(env, ss->ss_server_fld);
5426 OBD_FREE_PTR(ss->ss_server_fld);
5427 ss->ss_server_fld = NULL;
5433 static int mdt_fld_init(const struct lu_env *env,
5435 struct mdt_device *m)
5437 struct seq_server_site *ss;
5441 ss = mdt_seq_site(m);
5443 OBD_ALLOC_PTR(ss->ss_server_fld);
5444 if (ss->ss_server_fld == NULL)
5445 RETURN(rc = -ENOMEM);
5447 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5450 OBD_FREE_PTR(ss->ss_server_fld);
5451 ss->ss_server_fld = NULL;
5458 static void mdt_stack_pre_fini(const struct lu_env *env,
5459 struct mdt_device *m, struct lu_device *top)
5461 struct lustre_cfg_bufs *bufs;
5462 struct lustre_cfg *lcfg;
5463 struct mdt_thread_info *info;
5468 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5469 LASSERT(info != NULL);
5471 bufs = &info->mti_u.bufs;
5473 LASSERT(m->mdt_child_exp);
5474 LASSERT(m->mdt_child_exp->exp_obd);
5476 /* process cleanup, pass mdt obd name to get obd umount flags */
5477 /* XXX: this is needed because all layers are referenced by
5478 * objects (some of them are pinned by osd, for example *
5479 * the proper solution should be a model where object used
5480 * by osd only doesn't have mdt/mdd slices -bzzz */
5481 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5482 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5483 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5486 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5488 top->ld_ops->ldo_process_config(env, top, lcfg);
5489 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5493 static void mdt_stack_fini(const struct lu_env *env,
5494 struct mdt_device *m, struct lu_device *top)
5496 struct obd_device *obd = mdt2obd_dev(m);
5497 struct lustre_cfg_bufs *bufs;
5498 struct lustre_cfg *lcfg;
5499 struct mdt_thread_info *info;
5503 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5504 LASSERT(info != NULL);
5506 lu_dev_del_linkage(top->ld_site, top);
5508 lu_site_purge(env, top->ld_site, -1);
5510 bufs = &info->mti_u.bufs;
5511 /* process cleanup, pass mdt obd name to get obd umount flags */
5512 /* another purpose is to let all layers to release their objects */
5513 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5518 lustre_cfg_bufs_set_string(bufs, 1, flags);
5519 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5522 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5525 top->ld_ops->ldo_process_config(env, top, lcfg);
5526 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5528 lu_site_purge(env, top->ld_site, -1);
5530 m->mdt_child = NULL;
5531 m->mdt_bottom = NULL;
5533 obd_disconnect(m->mdt_child_exp);
5534 m->mdt_child_exp = NULL;
5536 obd_disconnect(m->mdt_bottom_exp);
5537 m->mdt_child_exp = NULL;
5540 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5541 const char *next, struct obd_export **exp)
5543 struct obd_connect_data *data = NULL;
5544 struct obd_device *obd;
5548 OBD_ALLOC_PTR(data);
5550 GOTO(out, rc = -ENOMEM);
5552 obd = class_name2obd(next);
5554 CERROR("%s: can't locate next device: %s\n",
5555 mdt_obd_name(m), next);
5556 GOTO(out, rc = -ENOTCONN);
5559 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5560 data->ocd_version = LUSTRE_VERSION_CODE;
5562 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5564 CERROR("%s: cannot connect to next dev %s (%d)\n",
5565 mdt_obd_name(m), next, rc);
5575 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5576 struct lustre_cfg *cfg)
5578 char *dev = lustre_cfg_string(cfg, 0);
5579 int rc, name_size, uuid_size;
5580 char *name, *uuid, *p;
5581 struct lustre_cfg_bufs *bufs;
5582 struct lustre_cfg *lcfg;
5583 struct obd_device *obd;
5584 struct lustre_profile *lprof;
5585 struct lu_site *site;
5588 /* in 1.8 we had the only device in the stack - MDS.
5589 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5590 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5591 * to generate names and setup MDT, MDD. MDT will be using
5592 * generated name to connect to MDD. for MDD the next device
5593 * will be LOD with name taken from so called "profile" which
5594 * is generated by mount_option line
5596 * 1.8 MGS generates config. commands like this:
5597 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5598 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5599 * 2.0 MGS generates config. commands like this:
5600 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5601 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5602 * 3:lustre-MDT0000-mdtlov 4:f
5604 * we generate MDD name from MDT one, just replacing T with D
5606 * after all the preparations, the logical equivalent will be
5607 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5608 * 3:lustre-MDT0000-mdtlov 4:f
5609 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5610 * 3:lustre-MDD0000 4:f
5612 * notice we build the stack from down to top: MDD first, then MDT */
5614 name_size = MAX_OBD_NAME;
5615 uuid_size = MAX_OBD_NAME;
5617 OBD_ALLOC(name, name_size);
5618 OBD_ALLOC(uuid, uuid_size);
5619 if (name == NULL || uuid == NULL)
5620 GOTO(cleanup_mem, rc = -ENOMEM);
5622 OBD_ALLOC_PTR(bufs);
5624 GOTO(cleanup_mem, rc = -ENOMEM);
5627 p = strstr(name, "-MDT");
5629 GOTO(free_bufs, rc = -ENOMEM);
5632 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5634 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5635 if (lprof == NULL || lprof->lp_dt == NULL) {
5636 CERROR("can't find the profile: %s\n",
5637 lustre_cfg_string(cfg, 0));
5638 GOTO(free_bufs, rc = -EINVAL);
5641 lustre_cfg_bufs_reset(bufs, name);
5642 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5643 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5644 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5646 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5648 GOTO(put_profile, rc = -ENOMEM);
5649 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5651 rc = class_attach(lcfg);
5653 GOTO(lcfg_cleanup, rc);
5655 obd = class_name2obd(name);
5657 CERROR("Can not find obd %s (%s in config)\n",
5658 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5659 GOTO(lcfg_cleanup, rc = -EINVAL);
5662 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5664 lustre_cfg_bufs_reset(bufs, name);
5665 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5666 lustre_cfg_bufs_set_string(bufs, 2, dev);
5667 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5669 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5671 GOTO(class_detach, rc = -ENOMEM);
5672 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5674 rc = class_setup(obd, lcfg);
5676 GOTO(class_detach, rc);
5678 /* connect to MDD we just setup */
5679 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5681 GOTO(class_detach, rc);
5683 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5685 LASSERT(mdt_lu_site(mdt) == NULL);
5686 mdt->mdt_lu_dev.ld_site = site;
5687 site->ls_top_dev = &mdt->mdt_lu_dev;
5688 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5690 /* now connect to bottom OSD */
5691 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5692 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5694 GOTO(class_detach, rc);
5696 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5698 rc = lu_env_refill((struct lu_env *)env);
5700 CERROR("Failure to refill session: '%d'\n", rc);
5702 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5707 class_detach(obd, lcfg);
5709 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5711 class_put_profile(lprof);
5716 OBD_FREE(name, name_size);
5718 OBD_FREE(uuid, uuid_size);
5722 /* setup quota master target on MDT0 */
5723 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5724 struct lustre_cfg *cfg)
5726 struct obd_device *obd;
5727 char *dev = lustre_cfg_string(cfg, 0);
5728 char *qmtname, *uuid, *p;
5729 struct lustre_cfg_bufs *bufs;
5730 struct lustre_cfg *lcfg;
5731 struct lustre_profile *lprof;
5732 struct obd_connect_data *data;
5736 LASSERT(mdt->mdt_qmt_exp == NULL);
5737 LASSERT(mdt->mdt_qmt_dev == NULL);
5739 /* quota master is on MDT0 only for now */
5740 if (mdt->mdt_seq_site.ss_node_id != 0)
5743 /* MGS generates config commands which look as follows:
5744 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5745 * 3:lustre-MDT0000-mdtlov 4:f
5747 * We generate the QMT name from the MDT one, just replacing MD with QM
5748 * after all the preparations, the logical equivalent will be:
5749 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5750 * 3:lustre-MDT0000-osd 4:f */
5751 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5752 OBD_ALLOC(uuid, UUID_MAX);
5753 OBD_ALLOC_PTR(bufs);
5754 OBD_ALLOC_PTR(data);
5755 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5756 GOTO(cleanup_mem, rc = -ENOMEM);
5758 strcpy(qmtname, dev);
5759 p = strstr(qmtname, "-MDT");
5761 GOTO(cleanup_mem, rc = -ENOMEM);
5762 /* replace MD with QM */
5766 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5768 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5769 if (lprof == NULL || lprof->lp_dt == NULL) {
5770 CERROR("can't find profile for %s\n",
5771 lustre_cfg_string(cfg, 0));
5772 GOTO(cleanup_mem, rc = -EINVAL);
5775 lustre_cfg_bufs_reset(bufs, qmtname);
5776 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5777 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5778 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5780 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5782 GOTO(put_profile, rc = -ENOMEM);
5783 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5785 rc = class_attach(lcfg);
5787 GOTO(lcfg_cleanup, rc);
5789 obd = class_name2obd(qmtname);
5791 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5792 lustre_cfg_string(cfg, 0));
5793 GOTO(lcfg_cleanup, rc = -EINVAL);
5796 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5798 lustre_cfg_bufs_reset(bufs, qmtname);
5799 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5800 lustre_cfg_bufs_set_string(bufs, 2, dev);
5802 /* for quota, the next device should be the OSD device */
5803 lustre_cfg_bufs_set_string(bufs, 3,
5804 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5806 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5808 GOTO(class_detach, rc = -ENOMEM);
5809 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5811 rc = class_setup(obd, lcfg);
5813 GOTO(class_detach, rc);
5815 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5817 /* configure local quota objects */
5818 if (CFS_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5821 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5825 GOTO(class_cleanup, rc);
5827 /* connect to quota master target */
5828 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5829 data->ocd_version = LUSTRE_VERSION_CODE;
5830 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5833 CERROR("cannot connect to quota master device %s (%d)\n",
5835 GOTO(class_cleanup, rc);
5841 class_manual_cleanup(obd);
5842 mdt->mdt_qmt_dev = NULL;
5843 GOTO(lcfg_cleanup, rc);
5847 class_detach(obd, lcfg);
5849 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5851 class_put_profile(lprof);
5856 OBD_FREE(qmtname, MAX_OBD_NAME);
5858 OBD_FREE(uuid, UUID_MAX);
5864 /* Shutdown quota master target associated with mdt */
5865 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5869 if (mdt->mdt_qmt_exp == NULL)
5871 LASSERT(mdt->mdt_qmt_dev != NULL);
5873 /* the qmt automatically shuts down when the mdt disconnects */
5874 obd_disconnect(mdt->mdt_qmt_exp);
5875 mdt->mdt_qmt_exp = NULL;
5876 mdt->mdt_qmt_dev = NULL;
5880 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5881 * for now. This will be removed along with converting rest of MDT code
5882 * to use tgt_session_info */
5883 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5885 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5888 if (unlikely(info->mti_object == NULL))
5891 rc = mdt_getxattr(info);
5893 mdt_thread_info_fini(info);
5897 static int mdt_llog_open(struct tgt_session_info *tsi)
5901 if (!mdt_changelog_allow(tsi2mdt_info(tsi)))
5902 RETURN(err_serious(-EACCES));
5904 RETURN(tgt_llog_open(tsi));
5907 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5908 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5909 #define OST_BRW_READ OST_READ
5910 #define OST_BRW_WRITE OST_WRITE
5912 static struct tgt_handler mdt_tgt_handlers[] = {
5913 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5914 0, MDS_CONNECT, mdt_tgt_connect,
5915 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5916 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5917 0, MDS_DISCONNECT, tgt_disconnect,
5918 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5919 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5920 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5921 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5922 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5923 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5924 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5925 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5927 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5928 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5929 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5930 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5931 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5932 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5933 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5934 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5936 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5937 mdt_hsm_ct_register),
5938 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5939 mdt_hsm_ct_unregister),
5940 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5942 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5944 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5945 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5947 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5950 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5951 TGT_MDT_HDL(IS_MUTABLE, MDS_BATCH, mdt_batch),
5954 static struct tgt_handler mdt_io_ops[] = {
5955 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5957 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5959 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5960 OST_PUNCH, mdt_punch_hdl,
5962 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5963 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
5965 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5966 TGT_RPC_HANDLER(OST_FIRST_OPC,
5967 0, OST_SET_INFO, mdt_io_set_info,
5968 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
5971 static struct tgt_handler mdt_sec_ctx_ops[] = {
5972 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5973 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5974 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5977 static struct tgt_handler mdt_quota_ops[] = {
5978 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5981 static struct tgt_handler mdt_llog_handlers[] = {
5982 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5983 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5984 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5985 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5988 static struct tgt_opc_slice mdt_common_slice[] = {
5990 .tos_opc_start = MDS_FIRST_OPC,
5991 .tos_opc_end = MDS_LAST_OPC,
5992 .tos_hs = mdt_tgt_handlers
5995 .tos_opc_start = OBD_FIRST_OPC,
5996 .tos_opc_end = OBD_LAST_OPC,
5997 .tos_hs = tgt_obd_handlers
6000 .tos_opc_start = LDLM_FIRST_OPC,
6001 .tos_opc_end = LDLM_LAST_OPC,
6002 .tos_hs = tgt_dlm_handlers
6005 .tos_opc_start = SEC_FIRST_OPC,
6006 .tos_opc_end = SEC_LAST_OPC,
6007 .tos_hs = mdt_sec_ctx_ops
6010 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
6011 .tos_opc_end = OUT_UPDATE_LAST_OPC,
6012 .tos_hs = tgt_out_handlers
6015 .tos_opc_start = FLD_FIRST_OPC,
6016 .tos_opc_end = FLD_LAST_OPC,
6017 .tos_hs = fld_handlers
6020 .tos_opc_start = SEQ_FIRST_OPC,
6021 .tos_opc_end = SEQ_LAST_OPC,
6022 .tos_hs = seq_handlers
6025 .tos_opc_start = QUOTA_DQACQ,
6026 .tos_opc_end = QUOTA_LAST_OPC,
6027 .tos_hs = mdt_quota_ops
6030 .tos_opc_start = LLOG_FIRST_OPC,
6031 .tos_opc_end = LLOG_LAST_OPC,
6032 .tos_hs = mdt_llog_handlers
6035 .tos_opc_start = LFSCK_FIRST_OPC,
6036 .tos_opc_end = LFSCK_LAST_OPC,
6037 .tos_hs = tgt_lfsck_handlers
6040 .tos_opc_start = OST_FIRST_OPC,
6041 .tos_opc_end = OST_LAST_OPC,
6042 .tos_hs = mdt_io_ops
6049 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
6051 struct md_device *next = m->mdt_child;
6052 struct lu_device *d = &m->mdt_lu_dev;
6053 struct obd_device *obd = mdt2obd_dev(m);
6054 struct lfsck_stop stop;
6057 stop.ls_status = LS_PAUSED;
6059 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
6061 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
6063 mdt_restriper_stop(m);
6064 ping_evictor_stop();
6066 /* Remove the HSM /proc entry so the coordinator cannot be
6067 * restarted by a user while it's shutting down.
6069 mdt_hsm_cdt_stop(m);
6071 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
6072 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
6074 if (m->mdt_namespace != NULL)
6075 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
6076 d->ld_obd->obd_force);
6078 obd_exports_barrier(obd);
6079 obd_zombie_barrier();
6081 mdt_quota_fini(env, m);
6083 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
6085 /* Calling the cleanup functions in the same order as in the mdt_init0
6088 mdt_tunables_fini(m);
6090 target_recovery_fini(obd);
6091 upcall_cache_cleanup(m->mdt_identity_cache);
6092 m->mdt_identity_cache = NULL;
6094 tgt_fini(env, &m->mdt_lut);
6096 mdt_hsm_cdt_fini(m);
6098 if (m->mdt_los != NULL) {
6099 local_oid_storage_fini(env, m->mdt_los);
6103 if (m->mdt_namespace != NULL) {
6104 ldlm_namespace_free_post(m->mdt_namespace);
6105 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
6108 if (m->mdt_md_root != NULL) {
6109 mdt_object_put(env, m->mdt_md_root);
6110 m->mdt_md_root = NULL;
6113 mdt_seq_fini(env, m);
6115 mdt_fld_fini(env, m);
6120 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6122 LASSERT(atomic_read(&d->ld_ref) == 0);
6124 server_put_mount(mdt_obd_name(m), true);
6129 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
6131 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
6132 struct lu_device_type *ldt, struct lustre_cfg *cfg)
6134 const struct dt_device_param *dt_conf;
6135 struct mdt_thread_info *info;
6136 struct obd_device *obd;
6137 const char *dev = lustre_cfg_string(cfg, 0);
6138 const char *num = lustre_cfg_string(cfg, 2);
6139 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
6140 struct lustre_mount_info *lmi = NULL;
6141 struct lustre_sb_info *lsi;
6143 struct seq_server_site *ss_site;
6144 const char *identity_upcall = "NONE";
6145 struct md_device *next;
6152 lu_device_init(&m->mdt_lu_dev, ldt);
6154 * Environment (env) might be missing mdt_thread_key values at that
6155 * point, if device is allocated when mdt_thread_key is in QUIESCENT
6158 * Usually device allocation path doesn't use module key values, but
6159 * mdt has to do a lot of work here, so allocate key value.
6161 rc = lu_env_refill((struct lu_env *)env);
6165 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
6166 LASSERT(info != NULL);
6168 obd = class_name2obd(dev);
6169 LASSERT(obd != NULL);
6171 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
6172 m->mdt_opts.mo_evict_tgt_nids = 1;
6173 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
6175 lmi = server_get_mount(dev);
6177 CERROR("Cannot get mount info for %s!\n", dev);
6180 lsi = s2lsi(lmi->lmi_sb);
6181 LASSERT(lsi->lsi_lmd);
6182 /* CMD is supported only in IAM mode */
6184 rc = kstrtol(num, 10, &node_id);
6189 if (test_bit(LMD_FLG_SKIP_LFSCK, lsi->lsi_lmd->lmd_flags))
6190 m->mdt_skip_lfsck = 1;
6191 if (test_bit(LMD_FLG_NO_CREATE, lsi->lsi_lmd->lmd_flags))
6192 m->mdt_lut.lut_no_create = 1;
6195 /* Just try to get a DoM lock by default. Otherwise, having a group
6196 * lock granted, it may get blocked for a long time. */
6197 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
6198 /* DoM files are read at open and data is packed in the reply */
6199 m->mdt_opts.mo_dom_read_open = 1;
6201 m->mdt_squash.rsi_uid = 0;
6202 m->mdt_squash.rsi_gid = 0;
6203 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
6204 spin_lock_init(&m->mdt_squash.rsi_lock);
6205 spin_lock_init(&m->mdt_lock);
6206 m->mdt_enable_chprojid_gid = 0;
6207 m->mdt_enable_dir_migration = 1;
6208 m->mdt_enable_dir_restripe = 0;
6209 m->mdt_enable_dir_auto_split = 0;
6210 m->mdt_enable_parallel_rename_dir = 1;
6211 m->mdt_enable_parallel_rename_file = 1;
6212 m->mdt_enable_remote_dir = 1;
6213 m->mdt_enable_remote_dir_gid = 0;
6214 m->mdt_enable_remote_rename = 1;
6215 m->mdt_enable_striped_dir = 1;
6216 m->mdt_enable_dmv_implicit_inherit = 1;
6217 m->mdt_dir_restripe_nsonly = 1;
6218 m->mdt_max_mod_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
6220 atomic_set(&m->mdt_mds_mds_conns, 0);
6221 atomic_set(&m->mdt_async_commit_count, 0);
6222 atomic_set(&m->mdt_dmv_old_client_count, 0);
6224 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
6225 m->mdt_lu_dev.ld_obd = obd;
6226 /* Set this lu_device to obd for error handling purposes. */
6227 obd->obd_lu_dev = &m->mdt_lu_dev;
6229 strncpy(m->mdt_job_xattr, XATTR_NAME_JOB_DEFAULT, XATTR_JOB_MAX_LEN);
6231 /* init the stack */
6232 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
6234 CERROR("%s: Can't init device stack, rc %d\n",
6235 mdt_obd_name(m), rc);
6240 ss_site = mdt_seq_site(m);
6241 s->ld_seq_site = ss_site;
6244 /* set server index */
6245 ss_site->ss_node_id = node_id;
6247 /* failover is the default
6248 * FIXME: we do not failout mds0/mgs, which may cause some problems.
6249 * assumed whose ss_node_id == 0 XXX
6251 obd->obd_replayable = 1;
6252 /* No connection accepted until configurations will finish */
6253 obd->obd_no_conn = 1;
6255 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
6256 char *str = lustre_cfg_string(cfg, 4);
6257 if (strchr(str, 'n')) {
6258 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
6259 obd->obd_replayable = 0;
6263 rc = mdt_fld_init(env, mdt_obd_name(m), m);
6265 GOTO(err_fini_stack, rc);
6267 rc = mdt_seq_init(env, m);
6269 GOTO(err_fini_fld, rc);
6271 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
6272 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
6273 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
6274 LDLM_NAMESPACE_SERVER,
6275 LDLM_NAMESPACE_GREEDY,
6277 if (IS_ERR(m->mdt_namespace)) {
6278 rc = PTR_ERR(m->mdt_namespace);
6279 CERROR("%s: unable to create server namespace: rc = %d\n",
6281 m->mdt_namespace = NULL;
6282 GOTO(err_fini_seq, rc);
6285 m->mdt_namespace->ns_lvbp = m;
6286 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
6288 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
6289 /* set obd_namespace for compatibility with old code */
6290 obd->obd_namespace = m->mdt_namespace;
6292 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
6293 OBD_FAIL_MDS_ALL_REQUEST_NET,
6294 OBD_FAIL_MDS_ALL_REPLY_NET);
6296 GOTO(err_free_ns, rc);
6298 /* Amount of available space excluded from granting and reserved
6299 * for metadata. It is a percentage of the total MDT size. */
6300 tgd->tgd_reserved_pcnt = 10;
6302 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
6303 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
6305 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6307 if (CFS_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
6308 GOTO(err_tgt, rc = -ENOENT);
6310 fid.f_seq = FID_SEQ_LOCAL_NAME;
6313 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6317 rc = mdt_hsm_cdt_init(m);
6319 CERROR("%s: error initializing coordinator, rc %d\n",
6320 mdt_obd_name(m), rc);
6321 GOTO(err_los_fini, rc);
6324 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6326 next = m->mdt_child;
6327 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6329 mntopts = dt_conf->ddp_mntopts;
6331 if (mntopts & MNTOPT_USERXATTR)
6332 m->mdt_opts.mo_user_xattr = 1;
6334 m->mdt_opts.mo_user_xattr = 0;
6336 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6338 if (mntopts & MNTOPT_ACL)
6339 m->mdt_opts.mo_acl = 1;
6341 m->mdt_opts.mo_acl = 0;
6343 m->mdt_opts.mo_enable_strict_som = 1;
6345 /* XXX: to support suppgid for ACL, we enable identity_upcall
6346 * by default, otherwise, maybe got unexpected -EACCESS. */
6347 if (m->mdt_opts.mo_acl)
6348 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6350 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6352 UC_IDCACHE_HASH_SIZE,
6353 1200, /* entry expire: 20 mn */
6354 30, /* acquire expire: 30 s */
6355 true, /* acquire can replay */
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]") !=
8010 rc = libcfs_setup();
8014 rc = lu_kmem_init(mdt_caches);
8018 rc = mds_mod_init();
8022 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
8023 LUSTRE_MDT_NAME, &mdt_device_type);
8028 lu_kmem_fini(mdt_caches);
8035 static void __exit mdt_exit(void)
8037 class_unregister_type(LUSTRE_MDT_NAME);
8039 lu_kmem_fini(mdt_caches);
8042 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
8043 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
8044 MODULE_VERSION(LUSTRE_VERSION_STRING);
8045 MODULE_LICENSE("GPL");
8047 module_init(mdt_init);
8048 module_exit(mdt_exit);