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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
61 #include <lustre_lmv.h>
63 #include <obd_support.h>
64 #include <lustre_barrier.h>
65 #include <obd_cksum.h>
66 #include <llog_swab.h>
68 #include "mdt_internal.h"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
134 return rep->lock_policy_res1 & op_flag;
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, __u64 op_flag)
141 info->mti_opdata &= ~op_flag;
142 tgt_opdata_clear(info->mti_env, op_flag);
145 rep->lock_policy_res1 &= ~op_flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, __u64 op_flag)
152 info->mti_opdata |= op_flag;
153 tgt_opdata_set(info->mti_env, op_flag);
156 rep->lock_policy_res1 |= op_flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
169 mdt_lock_reg_init(lh, lock->l_req_mode);
170 if (lock->l_req_mode == LCK_GROUP)
171 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
174 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
175 const struct lu_name *lname)
177 lh->mlh_reg_mode = lock_mode;
178 lh->mlh_pdo_mode = LCK_MINMODE;
179 lh->mlh_rreg_mode = lock_mode;
180 lh->mlh_type = MDT_PDO_LOCK;
182 if (lu_name_is_valid(lname)) {
183 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
185 /* XXX Workaround for LU-2856
187 * Zero is a valid return value of full_name_hash, but
188 * several users of mlh_pdo_hash assume a non-zero
189 * hash value. We therefore map zero onto an
190 * arbitrary, but consistent value (1) to avoid
191 * problems further down the road. */
192 if (unlikely(lh->mlh_pdo_hash == 0))
193 lh->mlh_pdo_hash = 1;
195 lh->mlh_pdo_hash = 0;
199 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
200 struct mdt_lock_handle *lh)
206 * Any dir access needs couple of locks:
208 * 1) on part of dir we gonna take lookup/modify;
210 * 2) on whole dir to protect it from concurrent splitting and/or to
211 * flush client's cache for readdir().
213 * so, for a given mode and object this routine decides what lock mode
214 * to use for lock #2:
216 * 1) if caller's gonna lookup in dir then we need to protect dir from
217 * being splitted only - LCK_CR
219 * 2) if caller's gonna modify dir then we need to protect dir from
220 * being splitted and to flush cache - LCK_CW
222 * 3) if caller's gonna modify dir and that dir seems ready for
223 * splitting then we need to protect it from any type of access
224 * (lookup/modify/split) - LCK_EX --bzzz
227 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
228 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
231 * Ask underlaying level its opinion about preferable PDO lock mode
232 * having access type passed as regular lock mode:
234 * - MDL_MINMODE means that lower layer does not want to specify lock
237 * - MDL_NL means that no PDO lock should be taken. This is used in some
238 * cases. Say, for non-splittable directories no need to use PDO locks
241 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
242 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
244 if (mode != MDL_MINMODE) {
245 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
248 * Lower layer does not want to specify locking mode. We do it
249 * our selves. No special protection is needed, just flush
250 * client's cache on modification and allow concurrent
253 switch (lh->mlh_reg_mode) {
255 lh->mlh_pdo_mode = LCK_EX;
258 lh->mlh_pdo_mode = LCK_CR;
261 lh->mlh_pdo_mode = LCK_CW;
264 CERROR("Not expected lock type (0x%x)\n",
265 (int)lh->mlh_reg_mode);
270 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
274 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
277 struct mdt_device *mdt = info->mti_mdt;
278 struct lu_name *lname = &info->mti_name;
279 char *filename = info->mti_filename;
280 struct mdt_object *parent;
284 LASSERT(!info->mti_cross_ref);
287 * We may want to allow this to mount a completely separate
288 * fileset from the MDT in the future, but keeping it to
289 * ROOT/ only for now avoid potential security issues.
291 *fid = mdt->mdt_md_root_fid;
293 while (rc == 0 && fileset != NULL && *fileset != '\0') {
294 const char *s1 = fileset;
300 while (*s2 != '/' && *s2 != '\0')
308 lname->ln_namelen = s2 - s1;
309 if (lname->ln_namelen > NAME_MAX) {
314 /* reject .. as a path component */
315 if (lname->ln_namelen == 2 &&
316 strncmp(s1, "..", 2) == 0) {
321 strncpy(filename, s1, lname->ln_namelen);
322 filename[lname->ln_namelen] = '\0';
323 lname->ln_name = filename;
325 parent = mdt_object_find(info->mti_env, mdt, fid);
326 if (IS_ERR(parent)) {
327 rc = PTR_ERR(parent);
330 /* Only got the fid of this obj by name */
332 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
333 fid, &info->mti_spec);
334 mdt_object_put(info->mti_env, parent);
337 parent = mdt_object_find(info->mti_env, mdt, fid);
339 rc = PTR_ERR(parent);
341 mode = lu_object_attr(&parent->mot_obj);
342 mdt_object_put(info->mti_env, parent);
351 static int mdt_get_root(struct tgt_session_info *tsi)
353 struct mdt_thread_info *info = tsi2mdt_info(tsi);
354 struct mdt_device *mdt = info->mti_mdt;
355 struct mdt_body *repbody;
356 char *fileset = NULL, *buffer = NULL;
358 struct obd_export *exp = info->mti_exp;
359 char *nodemap_fileset;
363 rc = mdt_check_ucred(info);
365 GOTO(out, rc = err_serious(rc));
367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
368 GOTO(out, rc = err_serious(-ENOMEM));
370 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
371 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
372 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
374 GOTO(out, rc = err_serious(-EFAULT));
377 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
378 if (nodemap_fileset && nodemap_fileset[0]) {
379 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
381 /* consider fileset from client as a sub-fileset
382 * of the nodemap one */
383 OBD_ALLOC(buffer, PATH_MAX + 1);
385 GOTO(out, rc = err_serious(-ENOMEM));
386 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
387 nodemap_fileset, fileset) >= PATH_MAX + 1)
388 GOTO(out, rc = err_serious(-EINVAL));
391 /* enforce fileset as specified in the nodemap */
392 fileset = nodemap_fileset;
397 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
398 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
400 GOTO(out, rc = err_serious(rc));
402 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
404 repbody->mbo_valid |= OBD_MD_FLID;
408 mdt_thread_info_fini(info);
410 OBD_FREE(buffer, PATH_MAX+1);
414 static int mdt_statfs(struct tgt_session_info *tsi)
416 struct ptlrpc_request *req = tgt_ses_req(tsi);
417 struct mdt_thread_info *info = tsi2mdt_info(tsi);
418 struct mdt_device *mdt = info->mti_mdt;
419 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
420 struct md_device *next = mdt->mdt_child;
421 struct ptlrpc_service_part *svcpt;
422 struct obd_statfs *osfs;
423 struct mdt_body *reqbody = NULL;
424 struct mdt_statfs_cache *msf;
425 ktime_t kstart = ktime_get();
430 svcpt = req->rq_rqbd->rqbd_svcpt;
432 /* This will trigger a watchdog timeout */
433 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
434 (MDT_SERVICE_WATCHDOG_FACTOR *
435 at_get(&svcpt->scp_at_estimate)) + 1);
437 rc = mdt_check_ucred(info);
439 GOTO(out, rc = err_serious(rc));
441 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
442 GOTO(out, rc = err_serious(-ENOMEM));
444 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
446 GOTO(out, rc = -EPROTO);
448 if (mdt_is_sum_statfs_client(req->rq_export) &&
449 lustre_packed_msg_size(req->rq_reqmsg) ==
450 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
451 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
452 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
453 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
456 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
457 msf = &mdt->mdt_sum_osfs;
459 msf = &mdt->mdt_osfs;
461 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
462 /** statfs data is too old, get up-to-date one */
463 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
464 rc = next->md_ops->mdo_statfs(info->mti_env,
467 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
471 spin_lock(&mdt->mdt_lock);
472 msf->msf_osfs = *osfs;
473 msf->msf_age = ktime_get_seconds();
474 spin_unlock(&mdt->mdt_lock);
476 /** use cached statfs data */
477 spin_lock(&mdt->mdt_lock);
478 *osfs = msf->msf_osfs;
479 spin_unlock(&mdt->mdt_lock);
482 /* at least try to account for cached pages. its still racy and
483 * might be under-reporting if clients haven't announced their
484 * caches with brw recently */
485 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
486 " pending %llu free %llu avail %llu\n",
487 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
488 tgd->tgd_tot_pending,
489 osfs->os_bfree << tgd->tgd_blockbits,
490 osfs->os_bavail << tgd->tgd_blockbits);
492 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
493 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
494 osfs->os_bsize - 1) >> tgd->tgd_blockbits));
496 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
497 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
498 "%llu objects: %llu free; state %x\n",
499 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
500 osfs->os_files, osfs->os_ffree, osfs->os_state);
502 if (!exp_grant_param_supp(tsi->tsi_exp) &&
503 tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
504 /* clients which don't support OBD_CONNECT_GRANT_PARAM
505 * should not see a block size > page size, otherwise
506 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
507 * block size which is the biggest block size known to work
508 * with all client's page size. */
509 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
510 osfs->os_bfree <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
511 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
512 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
515 mdt_counter_incr(req, LPROC_MDT_STATFS,
516 ktime_us_delta(ktime_get(), kstart));
518 mdt_thread_info_fini(info);
522 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
524 struct lov_comp_md_v1 *comp_v1;
525 struct lov_mds_md *v1;
527 __u32 dom_stripesize = 0;
529 bool has_ost_stripes = false;
536 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
539 comp_v1 = (struct lov_comp_md_v1 *)lmm;
540 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
541 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
543 /* Fast check for DoM entry with no mirroring, should be the first */
544 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
545 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
548 /* check all entries otherwise */
549 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
550 struct lov_comp_md_entry_v1 *lcme;
552 lcme = &comp_v1->lcm_entries[i];
553 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
556 off = le32_to_cpu(lcme->lcme_offset);
557 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
559 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
561 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
563 has_ost_stripes = true;
565 if (dom_stripesize && has_ost_stripes)
566 RETURN(dom_stripesize);
568 /* DoM-only case exits here */
569 if (is_dom_only && dom_stripesize)
571 RETURN(dom_stripesize);
575 * Pack size attributes into the reply.
577 int mdt_pack_size2body(struct mdt_thread_info *info,
578 const struct lu_fid *fid, struct lustre_handle *lh)
581 struct md_attr *ma = &info->mti_attr;
583 bool dom_lock = false;
587 LASSERT(ma->ma_attr.la_valid & LA_MODE);
589 if (!S_ISREG(ma->ma_attr.la_mode) ||
590 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
593 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
594 /* no DoM stripe, no size in reply */
598 if (lustre_handle_is_used(lh)) {
599 struct ldlm_lock *lock;
601 lock = ldlm_handle2lock(lh);
603 dom_lock = ldlm_has_dom(lock);
608 /* no DoM lock, no size in reply */
612 /* Either DoM lock exists or LMM has only DoM stripe then
613 * return size on body. */
614 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
616 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
620 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
622 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
624 * \param info thread info object
625 * \param repbody reply to pack ACLs into
626 * \param o mdt object of file to examine
627 * \param nodemap nodemap of client to reply to
629 * \retval -errno error getting or parsing ACL from disk
631 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
632 struct mdt_object *o, struct lu_nodemap *nodemap)
634 const struct lu_env *env = info->mti_env;
635 struct md_object *next = mdt_object_child(o);
636 struct lu_buf *buf = &info->mti_buf;
637 struct mdt_device *mdt = info->mti_mdt;
638 struct req_capsule *pill = info->mti_pill;
643 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
644 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
645 if (buf->lb_len == 0)
649 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
651 if (rc == -ENODATA) {
652 repbody->mbo_aclsize = 0;
653 repbody->mbo_valid |= OBD_MD_FLACL;
655 } else if (rc == -EOPNOTSUPP) {
659 exp_connect_large_acl(info->mti_exp) &&
660 buf->lb_buf != info->mti_big_acl) {
661 if (info->mti_big_acl == NULL) {
662 info->mti_big_aclsize =
664 mdt->mdt_max_ea_size,
666 OBD_ALLOC_LARGE(info->mti_big_acl,
667 info->mti_big_aclsize);
668 if (info->mti_big_acl == NULL) {
669 info->mti_big_aclsize = 0;
670 CERROR("%s: unable to grow "
673 PFID(mdt_object_fid(o)));
678 CDEBUG(D_INODE, "%s: grow the "DFID
679 " ACL buffer to size %d\n",
681 PFID(mdt_object_fid(o)),
682 info->mti_big_aclsize);
684 buf->lb_buf = info->mti_big_acl;
685 buf->lb_len = info->mti_big_aclsize;
690 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
691 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
700 acl_buflen = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
701 if (acl_buflen >= rc)
704 /* If LOV/LMA EA is small, we can reuse part of their buffer */
705 client = ptlrpc_req_get_repsize(pill->rc_req);
706 server = lustre_packed_msg_size(pill->rc_req->rq_repmsg);
707 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
708 lmm_buflen = req_capsule_get_size(pill, &RMF_MDT_MD,
710 lmmsize = repbody->mbo_eadatasize;
713 if (client < server - acl_buflen - lmm_buflen + rc + lmmsize) {
714 CDEBUG(D_INODE, "%s: client prepared buffer size %d "
715 "is not big enough with the ACL size %d (%d)\n",
716 mdt_obd_name(mdt), client, rc,
717 server - acl_buflen - lmm_buflen + rc + lmmsize);
718 repbody->mbo_aclsize = 0;
719 repbody->mbo_valid &= ~OBD_MD_FLACL;
724 if (buf->lb_buf == info->mti_big_acl)
725 info->mti_big_acl_used = 1;
727 rc = nodemap_map_acl(nodemap, buf->lb_buf,
728 rc, NODEMAP_FS_TO_CLIENT);
729 /* if all ACLs mapped out, rc is still >= 0 */
731 CERROR("%s: nodemap_map_acl unable to parse "DFID
732 " ACL: rc = %d\n", mdt_obd_name(mdt),
733 PFID(mdt_object_fid(o)), rc);
734 repbody->mbo_aclsize = 0;
735 repbody->mbo_valid &= ~OBD_MD_FLACL;
737 repbody->mbo_aclsize = rc;
738 repbody->mbo_valid |= OBD_MD_FLACL;
747 /* XXX Look into layout in MDT layer. */
748 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
750 struct lov_comp_md_v1 *comp_v1;
751 struct lov_mds_md *v1;
754 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
755 comp_v1 = (struct lov_comp_md_v1 *)lmm;
757 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
758 v1 = (struct lov_mds_md *)((char *)comp_v1 +
759 comp_v1->lcm_entries[i].lcme_offset);
760 /* We don't support partial release for now */
761 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
766 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
771 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
772 const struct lu_attr *attr, const struct lu_fid *fid)
774 struct md_attr *ma = &info->mti_attr;
775 struct obd_export *exp = info->mti_exp;
776 struct lu_nodemap *nodemap = NULL;
778 LASSERT(ma->ma_valid & MA_INODE);
780 if (attr->la_valid & LA_ATIME) {
781 b->mbo_atime = attr->la_atime;
782 b->mbo_valid |= OBD_MD_FLATIME;
784 if (attr->la_valid & LA_MTIME) {
785 b->mbo_mtime = attr->la_mtime;
786 b->mbo_valid |= OBD_MD_FLMTIME;
788 if (attr->la_valid & LA_CTIME) {
789 b->mbo_ctime = attr->la_ctime;
790 b->mbo_valid |= OBD_MD_FLCTIME;
792 if (attr->la_valid & LA_BTIME) {
793 b->mbo_btime = attr->la_btime;
794 b->mbo_valid |= OBD_MD_FLBTIME;
796 if (attr->la_valid & LA_FLAGS) {
797 b->mbo_flags = attr->la_flags;
798 b->mbo_valid |= OBD_MD_FLFLAGS;
800 if (attr->la_valid & LA_NLINK) {
801 b->mbo_nlink = attr->la_nlink;
802 b->mbo_valid |= OBD_MD_FLNLINK;
804 if (attr->la_valid & (LA_UID|LA_GID)) {
805 nodemap = nodemap_get_from_exp(exp);
809 if (attr->la_valid & LA_UID) {
810 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
811 NODEMAP_FS_TO_CLIENT,
813 b->mbo_valid |= OBD_MD_FLUID;
815 if (attr->la_valid & LA_GID) {
816 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
817 NODEMAP_FS_TO_CLIENT,
819 b->mbo_valid |= OBD_MD_FLGID;
822 if (attr->la_valid & LA_PROJID) {
823 /* TODO, nodemap for project id */
824 b->mbo_projid = attr->la_projid;
825 b->mbo_valid |= OBD_MD_FLPROJID;
828 b->mbo_mode = attr->la_mode;
829 if (attr->la_valid & LA_MODE)
830 b->mbo_valid |= OBD_MD_FLMODE;
831 if (attr->la_valid & LA_TYPE)
832 b->mbo_valid |= OBD_MD_FLTYPE;
836 b->mbo_valid |= OBD_MD_FLID;
837 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
838 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
841 if (!(attr->la_valid & LA_TYPE))
844 b->mbo_rdev = attr->la_rdev;
845 b->mbo_size = attr->la_size;
846 b->mbo_blocks = attr->la_blocks;
848 if (!S_ISREG(attr->la_mode)) {
849 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
850 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
851 /* means no objects are allocated on osts. */
852 LASSERT(!(ma->ma_valid & MA_LOV));
853 /* just ignore blocks occupied by extend attributes on MDS */
855 /* if no object is allocated on osts, the size on mds is valid.
857 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
858 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
859 if (mdt_hsm_is_released(ma->ma_lmm)) {
860 /* A released file stores its size on MDS. */
861 /* But return 1 block for released file, unless tools
862 * like tar will consider it fully sparse. (LU-3864)
864 if (unlikely(b->mbo_size == 0))
868 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
869 } else if (info->mti_som_valid) { /* som is valid */
870 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
871 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
872 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
873 b->mbo_size = ma->ma_som.ms_size;
874 b->mbo_blocks = ma->ma_som.ms_blocks;
878 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
879 b->mbo_valid & OBD_MD_FLLAZYSIZE))
880 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
881 PFID(fid), (unsigned long long)b->mbo_size);
884 if (!IS_ERR_OR_NULL(nodemap))
885 nodemap_putref(nodemap);
888 static inline int mdt_body_has_lov(const struct lu_attr *la,
889 const struct mdt_body *body)
891 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
892 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
895 void mdt_client_compatibility(struct mdt_thread_info *info)
897 struct mdt_body *body;
898 struct ptlrpc_request *req = mdt_info_req(info);
899 struct obd_export *exp = req->rq_export;
900 struct md_attr *ma = &info->mti_attr;
901 struct lu_attr *la = &ma->ma_attr;
904 if (exp_connect_layout(exp))
905 /* the client can deal with 16-bit lmm_stripe_count */
908 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
910 if (!mdt_body_has_lov(la, body))
913 /* now we have a reply with a lov for a client not compatible with the
914 * layout lock so we have to clean the layout generation number */
915 if (S_ISREG(la->la_mode))
916 ma->ma_lmm->lmm_layout_gen = 0;
920 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
921 struct mdt_object *o)
923 const struct lu_env *env = info->mti_env;
926 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
935 /* Is it a directory? Let's check for the LMV as well */
936 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
937 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
941 rc2 = mo_xattr_get(env, mdt_object_child(o),
943 XATTR_NAME_DEFAULT_LMV);
945 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
953 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
956 const struct lu_env *env = info->mti_env;
960 LASSERT(info->mti_big_lmm_used == 0);
961 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
965 /* big_lmm may need to be grown */
966 if (info->mti_big_lmmsize < rc) {
967 int size = size_roundup_power2(rc);
969 if (info->mti_big_lmmsize > 0) {
970 /* free old buffer */
971 LASSERT(info->mti_big_lmm);
972 OBD_FREE_LARGE(info->mti_big_lmm,
973 info->mti_big_lmmsize);
974 info->mti_big_lmm = NULL;
975 info->mti_big_lmmsize = 0;
978 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
979 if (info->mti_big_lmm == NULL)
981 info->mti_big_lmmsize = size;
983 LASSERT(info->mti_big_lmmsize >= rc);
985 info->mti_buf.lb_buf = info->mti_big_lmm;
986 info->mti_buf.lb_len = info->mti_big_lmmsize;
987 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
992 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
993 struct md_attr *ma, const char *name)
995 struct md_object *next = mdt_object_child(o);
996 struct lu_buf *buf = &info->mti_buf;
999 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1000 buf->lb_buf = ma->ma_lmm;
1001 buf->lb_len = ma->ma_lmm_size;
1002 LASSERT(!(ma->ma_valid & MA_LOV));
1003 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1004 buf->lb_buf = ma->ma_lmv;
1005 buf->lb_len = ma->ma_lmv_size;
1006 LASSERT(!(ma->ma_valid & MA_LMV));
1007 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1008 buf->lb_buf = ma->ma_default_lmv;
1009 buf->lb_len = ma->ma_default_lmv_size;
1010 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1015 LASSERT(buf->lb_buf);
1017 rc = mo_xattr_get(info->mti_env, next, buf, name);
1021 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1022 if (info->mti_big_lmm_used)
1023 ma->ma_lmm = info->mti_big_lmm;
1025 /* NOT return LOV EA with hole to old client. */
1026 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1027 LOV_PATTERN_F_HOLE) &&
1028 !(exp_connect_flags(info->mti_exp) &
1029 OBD_CONNECT_LFSCK)) {
1032 ma->ma_lmm_size = rc;
1033 ma->ma_valid |= MA_LOV;
1035 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1036 if (info->mti_big_lmm_used)
1037 ma->ma_lmv = info->mti_big_lmm;
1039 ma->ma_lmv_size = rc;
1040 ma->ma_valid |= MA_LMV;
1041 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1042 ma->ma_default_lmv_size = rc;
1043 ma->ma_valid |= MA_LMV_DEF;
1046 /* Update mdt_max_mdsize so all clients will be aware that */
1047 if (info->mti_mdt->mdt_max_mdsize < rc)
1048 info->mti_mdt->mdt_max_mdsize = rc;
1051 } else if (rc == -ENODATA) {
1054 } else if (rc == -ERANGE) {
1055 /* Default LMV has fixed size, so it must be able to fit
1056 * in the original buffer */
1057 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1059 rc = mdt_big_xattr_get(info, o, name);
1061 info->mti_big_lmm_used = 1;
1069 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1070 struct md_attr *ma, const char *name)
1074 if (!info->mti_big_lmm) {
1075 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1076 if (!info->mti_big_lmm)
1078 info->mti_big_lmmsize = PAGE_SIZE;
1081 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1082 ma->ma_lmm = info->mti_big_lmm;
1083 ma->ma_lmm_size = info->mti_big_lmmsize;
1084 ma->ma_valid &= ~MA_LOV;
1085 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1086 ma->ma_lmv = info->mti_big_lmm;
1087 ma->ma_lmv_size = info->mti_big_lmmsize;
1088 ma->ma_valid &= ~MA_LMV;
1093 LASSERT(!info->mti_big_lmm_used);
1094 rc = __mdt_stripe_get(info, o, ma, name);
1095 /* since big_lmm is always used here, clear 'used' flag to avoid
1096 * assertion in mdt_big_xattr_get().
1098 info->mti_big_lmm_used = 0;
1103 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1104 struct lu_fid *pfid)
1106 struct lu_buf *buf = &info->mti_buf;
1107 struct link_ea_header *leh;
1108 struct link_ea_entry *lee;
1112 buf->lb_buf = info->mti_big_lmm;
1113 buf->lb_len = info->mti_big_lmmsize;
1114 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1115 buf, XATTR_NAME_LINK);
1116 /* ignore errors, MA_PFID won't be set and it is
1117 * up to the caller to treat this as an error */
1118 if (rc == -ERANGE || buf->lb_len == 0) {
1119 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1120 buf->lb_buf = info->mti_big_lmm;
1121 buf->lb_len = info->mti_big_lmmsize;
1126 if (rc < sizeof(*leh)) {
1127 CERROR("short LinkEA on "DFID": rc = %d\n",
1128 PFID(mdt_object_fid(o)), rc);
1132 leh = (struct link_ea_header *) buf->lb_buf;
1133 lee = (struct link_ea_entry *)(leh + 1);
1134 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1135 leh->leh_magic = LINK_EA_MAGIC;
1136 leh->leh_reccount = __swab32(leh->leh_reccount);
1137 leh->leh_len = __swab64(leh->leh_len);
1139 if (leh->leh_magic != LINK_EA_MAGIC)
1141 if (leh->leh_reccount == 0)
1144 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1145 fid_be_to_cpu(pfid, pfid);
1150 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1151 struct lu_fid *pfid, struct lu_name *lname)
1153 struct lu_buf *buf = &info->mti_buf;
1154 struct link_ea_header *leh;
1155 struct link_ea_entry *lee;
1159 buf->lb_buf = info->mti_xattr_buf;
1160 buf->lb_len = sizeof(info->mti_xattr_buf);
1161 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1163 if (rc == -ERANGE) {
1164 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1165 buf->lb_buf = info->mti_big_lmm;
1166 buf->lb_len = info->mti_big_lmmsize;
1171 if (rc < sizeof(*leh)) {
1172 CERROR("short LinkEA on "DFID": rc = %d\n",
1173 PFID(mdt_object_fid(o)), rc);
1177 leh = (struct link_ea_header *)buf->lb_buf;
1178 lee = (struct link_ea_entry *)(leh + 1);
1179 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1180 leh->leh_magic = LINK_EA_MAGIC;
1181 leh->leh_reccount = __swab32(leh->leh_reccount);
1182 leh->leh_len = __swab64(leh->leh_len);
1184 if (leh->leh_magic != LINK_EA_MAGIC)
1187 if (leh->leh_reccount == 0)
1190 linkea_entry_unpack(lee, &reclen, lname, pfid);
1195 int mdt_attr_get_complex(struct mdt_thread_info *info,
1196 struct mdt_object *o, struct md_attr *ma)
1198 const struct lu_env *env = info->mti_env;
1199 struct md_object *next = mdt_object_child(o);
1200 struct lu_buf *buf = &info->mti_buf;
1201 int need = ma->ma_need;
1208 if (mdt_object_exists(o) == 0)
1209 GOTO(out, rc = -ENOENT);
1210 mode = lu_object_attr(&next->mo_lu);
1212 if (need & MA_INODE) {
1213 ma->ma_need = MA_INODE;
1214 rc = mo_attr_get(env, next, ma);
1219 (void) mdt_get_som(info, o, ma);
1220 ma->ma_valid |= MA_INODE;
1223 if (need & MA_PFID) {
1224 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1226 ma->ma_valid |= MA_PFID;
1227 /* ignore this error, parent fid is not mandatory */
1231 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1232 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1237 if (need & MA_LMV && S_ISDIR(mode)) {
1238 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1243 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1244 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1250 * In the handle of MA_INODE, we may already get the SOM attr.
1252 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1253 rc = mdt_get_som(info, o, ma);
1258 if (need & MA_HSM && S_ISREG(mode)) {
1259 buf->lb_buf = info->mti_xattr_buf;
1260 buf->lb_len = sizeof(info->mti_xattr_buf);
1261 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1262 sizeof(info->mti_xattr_buf));
1263 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1264 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1266 ma->ma_valid |= MA_HSM;
1267 else if (rc2 < 0 && rc2 != -ENODATA)
1268 GOTO(out, rc = rc2);
1271 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1272 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1273 buf->lb_buf = ma->ma_acl;
1274 buf->lb_len = ma->ma_acl_size;
1275 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1277 ma->ma_acl_size = rc2;
1278 ma->ma_valid |= MA_ACL_DEF;
1279 } else if (rc2 == -ENODATA) {
1281 ma->ma_acl_size = 0;
1283 GOTO(out, rc = rc2);
1288 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1289 rc, ma->ma_valid, ma->ma_lmm);
1293 static int mdt_getattr_internal(struct mdt_thread_info *info,
1294 struct mdt_object *o, int ma_need)
1296 struct mdt_device *mdt = info->mti_mdt;
1297 struct md_object *next = mdt_object_child(o);
1298 const struct mdt_body *reqbody = info->mti_body;
1299 struct ptlrpc_request *req = mdt_info_req(info);
1300 struct md_attr *ma = &info->mti_attr;
1301 struct lu_attr *la = &ma->ma_attr;
1302 struct req_capsule *pill = info->mti_pill;
1303 const struct lu_env *env = info->mti_env;
1304 struct mdt_body *repbody;
1305 struct lu_buf *buffer = &info->mti_buf;
1306 struct obd_export *exp = info->mti_exp;
1307 ktime_t kstart = ktime_get();
1312 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1313 RETURN(err_serious(-ENOMEM));
1315 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1319 if (mdt_object_remote(o)) {
1320 /* This object is located on remote node.*/
1321 /* Return -ENOTSUPP for old client */
1322 if (!mdt_is_dne_client(req->rq_export))
1323 GOTO(out, rc = -ENOTSUPP);
1325 repbody->mbo_fid1 = *mdt_object_fid(o);
1326 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1330 if (reqbody->mbo_eadatasize > 0) {
1331 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1332 if (buffer->lb_buf == NULL)
1333 GOTO(out, rc = -EPROTO);
1334 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1337 buffer->lb_buf = NULL;
1339 ma_need &= ~(MA_LOV | MA_LMV);
1340 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1341 mdt_obd_name(info->mti_mdt),
1342 req->rq_export->exp_client_uuid.uuid);
1345 /* from 2.12.58 intent_getattr pack default LMV in reply */
1346 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1347 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1348 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1349 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1351 ma->ma_lmv = buffer->lb_buf;
1352 ma->ma_lmv_size = buffer->lb_len;
1353 ma->ma_default_lmv = req_capsule_server_get(pill,
1354 &RMF_DEFAULT_MDT_MD);
1355 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1356 &RMF_DEFAULT_MDT_MD,
1358 ma->ma_need = MA_INODE;
1359 if (ma->ma_lmv_size > 0)
1360 ma->ma_need |= MA_LMV;
1361 if (ma->ma_default_lmv_size > 0)
1362 ma->ma_need |= MA_LMV_DEF;
1363 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1364 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1365 /* If it is dir and client require MEA, then we got MEA */
1366 /* Assumption: MDT_MD size is enough for lmv size. */
1367 ma->ma_lmv = buffer->lb_buf;
1368 ma->ma_lmv_size = buffer->lb_len;
1369 ma->ma_need = MA_INODE;
1370 if (ma->ma_lmv_size > 0) {
1371 if (reqbody->mbo_valid & OBD_MD_MEA) {
1372 ma->ma_need |= MA_LMV;
1373 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1374 ma->ma_need |= MA_LMV_DEF;
1375 ma->ma_default_lmv = buffer->lb_buf;
1377 ma->ma_default_lmv_size = buffer->lb_len;
1378 ma->ma_lmv_size = 0;
1382 ma->ma_lmm = buffer->lb_buf;
1383 ma->ma_lmm_size = buffer->lb_len;
1384 ma->ma_need = MA_INODE | MA_HSM;
1385 if (ma->ma_lmm_size > 0) {
1386 ma->ma_need |= MA_LOV;
1387 /* Older clients may crash if they getattr overstriped
1390 if (!exp_connect_overstriping(exp) &&
1391 mdt_lmm_is_overstriping(ma->ma_lmm))
1392 RETURN(-EOPNOTSUPP);
1396 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1397 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1398 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1399 /* get default stripe info for this dir. */
1400 ma->ma_need |= MA_LOV_DEF;
1402 ma->ma_need |= ma_need;
1404 rc = mdt_attr_get_complex(info, o, ma);
1406 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1407 "%s: getattr error for "DFID": rc = %d\n",
1408 mdt_obd_name(info->mti_mdt),
1409 PFID(mdt_object_fid(o)), rc);
1413 /* if file is released, check if a restore is running */
1414 if (ma->ma_valid & MA_HSM) {
1415 repbody->mbo_valid |= OBD_MD_TSTATE;
1416 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1417 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1418 repbody->mbo_t_state = MS_RESTORE;
1421 if (unlikely(!(ma->ma_valid & MA_INODE)))
1424 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1426 if (mdt_body_has_lov(la, reqbody)) {
1427 u32 stripe_count = 1;
1429 if (ma->ma_valid & MA_LOV) {
1430 LASSERT(ma->ma_lmm_size);
1431 repbody->mbo_eadatasize = ma->ma_lmm_size;
1432 if (S_ISDIR(la->la_mode))
1433 repbody->mbo_valid |= OBD_MD_FLDIREA;
1435 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1436 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1438 if (ma->ma_valid & MA_LMV) {
1439 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1440 u32 magic = le32_to_cpu(lmv->lmv_magic);
1442 /* Return -ENOTSUPP for old client */
1443 if (!mdt_is_striped_client(req->rq_export))
1446 LASSERT(S_ISDIR(la->la_mode));
1447 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1448 repbody->mbo_eadatasize = ma->ma_lmv_size;
1449 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1451 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1452 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1453 mdt_restripe_migrate_add(info, o);
1454 else if (magic == LMV_MAGIC_V1 &&
1455 lmv_is_restriping(lmv))
1456 mdt_restripe_update_add(info, o);
1458 if (ma->ma_valid & MA_LMV_DEF) {
1459 /* Return -ENOTSUPP for old client */
1460 if (!mdt_is_striped_client(req->rq_export))
1462 LASSERT(S_ISDIR(la->la_mode));
1464 * when ll_dir_getstripe() gets default LMV, it
1465 * checks mbo_eadatasize.
1467 if (!(ma->ma_valid & MA_LMV))
1468 repbody->mbo_eadatasize =
1469 ma->ma_default_lmv_size;
1470 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1471 OBD_MD_DEFAULT_MEA);
1474 "dirent count %llu stripe count %u MDT count %d\n",
1475 ma->ma_attr.la_dirent_count, stripe_count,
1476 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1477 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1478 ma->ma_attr.la_dirent_count >
1479 mdt->mdt_restriper.mdr_dir_split_count &&
1480 !fid_is_root(mdt_object_fid(o)) &&
1481 mdt->mdt_enable_dir_auto_split &&
1482 !o->mot_restriping &&
1483 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1484 mdt_auto_split_add(info, o);
1485 } else if (S_ISLNK(la->la_mode) &&
1486 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1487 buffer->lb_buf = ma->ma_lmm;
1488 /* eadatasize from client includes NULL-terminator, so
1489 * there is no need to read it */
1490 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1491 rc = mo_readlink(env, next, buffer);
1492 if (unlikely(rc <= 0)) {
1493 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1494 mdt_obd_name(info->mti_mdt),
1495 PFID(mdt_object_fid(o)), rc);
1498 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1500 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1502 repbody->mbo_valid |= OBD_MD_LINKNAME;
1503 /* we need to report back size with NULL-terminator
1504 * because client expects that */
1505 repbody->mbo_eadatasize = rc + 1;
1506 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1507 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1508 "on "DFID ", expected %d\n",
1509 mdt_obd_name(info->mti_mdt),
1510 rc, PFID(mdt_object_fid(o)),
1511 reqbody->mbo_eadatasize - 1);
1512 /* NULL terminate */
1513 ((char *)ma->ma_lmm)[rc] = 0;
1515 /* If the total CDEBUG() size is larger than a page, it
1516 * will print a warning to the console, avoid this by
1517 * printing just the last part of the symlink. */
1518 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1519 print_limit < rc ? "..." : "", print_limit,
1520 (char *)ma->ma_lmm + rc - print_limit, rc);
1525 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1526 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1527 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1528 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1529 repbody->mbo_max_mdsize);
1532 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1533 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1534 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1535 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1536 if (IS_ERR(nodemap))
1537 RETURN(PTR_ERR(nodemap));
1539 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1540 nodemap_putref(nodemap);
1546 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1547 ktime_us_delta(ktime_get(), kstart));
1552 static int mdt_getattr(struct tgt_session_info *tsi)
1554 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1555 struct mdt_object *obj = info->mti_object;
1556 struct req_capsule *pill = info->mti_pill;
1557 struct mdt_body *reqbody;
1558 struct mdt_body *repbody;
1562 if (unlikely(info->mti_object == NULL))
1565 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1567 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1569 /* Special case for Data-on-MDT files to get data version */
1570 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1571 rc = mdt_data_version_get(tsi);
1575 /* Unlike intent case where we need to pre-fill out buffers early on
1576 * in intent policy for ldlm reasons, here we can have a much better
1577 * guess at EA size by just reading it from disk.
1578 * Exceptions are readdir and (missing) directory striping */
1580 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1581 /* No easy way to know how long is the symlink, but it cannot
1582 * be more than PATH_MAX, so we allocate +1 */
1584 /* A special case for fs ROOT: getattr there might fetch
1585 * default EA for entire fs, not just for this dir!
1587 } else if (lu_fid_eq(mdt_object_fid(obj),
1588 &info->mti_mdt->mdt_md_root_fid) &&
1589 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1590 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1592 /* Should the default strping be bigger, mdt_fix_reply
1593 * will reallocate */
1594 rc = DEF_REP_MD_SIZE;
1596 /* Read the actual EA size from disk */
1597 rc = mdt_attr_get_eabuf_size(info, obj);
1601 GOTO(out, rc = err_serious(rc));
1603 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1605 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1606 * by default. If the target object has more ACL entries, then
1607 * enlarge the buffer when necessary. */
1608 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1609 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1611 rc = req_capsule_server_pack(pill);
1612 if (unlikely(rc != 0))
1613 GOTO(out, rc = err_serious(rc));
1615 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1616 LASSERT(repbody != NULL);
1617 repbody->mbo_eadatasize = 0;
1618 repbody->mbo_aclsize = 0;
1620 rc = mdt_check_ucred(info);
1622 GOTO(out_shrink, rc);
1624 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1626 rc = mdt_getattr_internal(info, obj, 0);
1629 mdt_client_compatibility(info);
1630 rc2 = mdt_fix_reply(info);
1634 mdt_thread_info_fini(info);
1639 * Handler of layout intent RPC requiring the layout modification
1641 * \param[in] info thread environment
1642 * \param[in] obj object
1643 * \param[out] lhc object ldlm lock handle
1644 * \param[in] layout layout change descriptor
1646 * \retval 0 on success
1647 * \retval < 0 error code
1649 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1650 struct mdt_lock_handle *lhc,
1651 struct md_layout_change *layout)
1657 if (!mdt_object_exists(obj))
1660 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1663 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1668 rc = mdt_check_resent_lock(info, obj, lhc);
1674 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1676 /* take layout lock to prepare layout change */
1677 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1678 lockpart |= MDS_INODELOCK_UPDATE;
1680 mdt_lock_handle_init(lhc);
1681 mdt_lock_reg_init(lhc, LCK_EX);
1682 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1687 mutex_lock(&obj->mot_som_mutex);
1688 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1689 mutex_unlock(&obj->mot_som_mutex);
1692 mdt_object_unlock(info, obj, lhc, 1);
1698 * Exchange MOF_LOV_CREATED flags between two objects after a
1699 * layout swap. No assumption is made on whether o1 or o2 have
1700 * created objects or not.
1702 * \param[in,out] o1 First swap layout object
1703 * \param[in,out] o2 Second swap layout object
1705 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1707 unsigned int o1_lov_created = o1->mot_lov_created;
1709 mutex_lock(&o1->mot_lov_mutex);
1710 mutex_lock(&o2->mot_lov_mutex);
1712 o1->mot_lov_created = o2->mot_lov_created;
1713 o2->mot_lov_created = o1_lov_created;
1715 mutex_unlock(&o2->mot_lov_mutex);
1716 mutex_unlock(&o1->mot_lov_mutex);
1719 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1721 struct mdt_thread_info *info;
1722 struct ptlrpc_request *req = tgt_ses_req(tsi);
1723 struct obd_export *exp = req->rq_export;
1724 struct mdt_object *o1, *o2, *o;
1725 struct mdt_lock_handle *lh1, *lh2;
1726 struct mdc_swap_layouts *msl;
1730 /* client does not support layout lock, so layout swaping
1732 * FIXME: there is a problem for old clients which don't support
1733 * layout lock yet. If those clients have already opened the file
1734 * they won't be notified at all so that old layout may still be
1735 * used to do IO. This can be fixed after file release is landed by
1736 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1737 if (!exp_connect_layout(exp))
1738 RETURN(-EOPNOTSUPP);
1740 info = tsi2mdt_info(tsi);
1741 if (unlikely(info->mti_object == NULL))
1744 if (info->mti_dlm_req != NULL)
1745 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1747 o1 = info->mti_object;
1748 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1749 &info->mti_body->mbo_fid2);
1751 GOTO(out, rc = PTR_ERR(o));
1753 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1754 GOTO(put, rc = -ENOENT);
1756 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1757 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1763 /* permission check. Make sure the calling process having permission
1764 * to write both files. */
1765 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1770 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1775 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1777 GOTO(put, rc = -EPROTO);
1779 lh1 = &info->mti_lh[MDT_LH_NEW];
1780 mdt_lock_reg_init(lh1, LCK_EX);
1781 lh2 = &info->mti_lh[MDT_LH_OLD];
1782 mdt_lock_reg_init(lh2, LCK_EX);
1784 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1785 MDS_INODELOCK_XATTR);
1789 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1790 MDS_INODELOCK_XATTR);
1794 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1795 mdt_object_child(o2), msl->msl_flags);
1799 mdt_swap_lov_flag(o1, o2);
1802 mdt_object_unlock(info, o2, lh2, rc);
1804 mdt_object_unlock(info, o1, lh1, rc);
1806 mdt_object_put(info->mti_env, o);
1808 mdt_thread_info_fini(info);
1812 static int mdt_raw_lookup(struct mdt_thread_info *info,
1813 struct mdt_object *parent,
1814 const struct lu_name *lname,
1815 struct ldlm_reply *ldlm_rep)
1817 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1821 LASSERT(!info->mti_cross_ref);
1823 /* Only got the fid of this obj by name */
1824 fid_zero(child_fid);
1825 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1826 lname, child_fid, &info->mti_spec);
1828 struct mdt_body *repbody;
1830 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1831 repbody->mbo_fid1 = *child_fid;
1832 repbody->mbo_valid = OBD_MD_FLID;
1833 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1834 } else if (rc == -ENOENT) {
1835 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1842 * UPDATE lock should be taken against parent, and be released before exit;
1843 * child_bits lock should be taken against child, and be returned back:
1844 * (1)normal request should release the child lock;
1845 * (2)intent request will grant the lock to client.
1847 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1848 struct mdt_lock_handle *lhc,
1850 struct ldlm_reply *ldlm_rep)
1852 struct ptlrpc_request *req = mdt_info_req(info);
1853 struct mdt_body *reqbody = NULL;
1854 struct mdt_object *parent = info->mti_object;
1855 struct mdt_object *child = NULL;
1856 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1857 struct lu_name *lname = NULL;
1858 struct mdt_lock_handle *lhp = NULL;
1859 struct ldlm_lock *lock;
1860 struct req_capsule *pill = info->mti_pill;
1868 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1869 LASSERT(ergo(is_resent,
1870 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1875 if (info->mti_cross_ref) {
1876 /* Only getattr on the child. Parent is on another node. */
1877 mdt_set_disposition(info, ldlm_rep,
1878 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1880 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1882 PFID(mdt_object_fid(child)), ldlm_rep);
1884 rc = mdt_check_resent_lock(info, child, lhc);
1887 } else if (rc > 0) {
1888 mdt_lock_handle_init(lhc);
1889 mdt_lock_reg_init(lhc, LCK_PR);
1892 * Object's name entry is on another MDS, it will
1893 * request PERM lock only because LOOKUP lock is owned
1894 * by the MDS where name entry resides.
1896 * TODO: it should try layout lock too. - Jinshan
1898 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1899 MDS_INODELOCK_LAYOUT);
1900 child_bits |= MDS_INODELOCK_PERM;
1902 rc = mdt_object_lock(info, child, lhc, child_bits);
1907 /* Finally, we can get attr for child. */
1908 if (!mdt_object_exists(child)) {
1909 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1911 "remote object doesn't exist.");
1912 mdt_object_unlock(info, child, lhc, 1);
1916 rc = mdt_getattr_internal(info, child, 0);
1917 if (unlikely(rc != 0)) {
1918 mdt_object_unlock(info, child, lhc, 1);
1922 rc = mdt_pack_secctx_in_reply(info, child);
1924 mdt_object_unlock(info, child, lhc, 1);
1928 rc = mdt_pack_encctx_in_reply(info, child);
1930 mdt_object_unlock(info, child, lhc, 1);
1934 lname = &info->mti_name;
1935 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1937 if (lu_name_is_valid(lname)) {
1938 if (mdt_object_remote(parent)) {
1939 CERROR("%s: parent "DFID" is on remote target\n",
1940 mdt_obd_name(info->mti_mdt),
1941 PFID(mdt_object_fid(parent)));
1945 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1946 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1947 PNAME(lname), ldlm_rep);
1949 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1950 if (unlikely(reqbody == NULL))
1951 RETURN(err_serious(-EPROTO));
1953 *child_fid = reqbody->mbo_fid2;
1954 if (unlikely(!fid_is_sane(child_fid)))
1955 RETURN(err_serious(-EINVAL));
1957 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1958 mdt_object_get(info->mti_env, parent);
1961 child = mdt_object_find(info->mti_env, info->mti_mdt,
1964 RETURN(PTR_ERR(child));
1967 if (mdt_object_remote(child)) {
1968 CERROR("%s: child "DFID" is on remote target\n",
1969 mdt_obd_name(info->mti_mdt),
1970 PFID(mdt_object_fid(child)));
1971 GOTO(out_child, rc = -EPROTO);
1974 /* don't fetch LOOKUP lock if it's remote object */
1975 rc = mdt_is_remote_object(info, parent, child);
1977 GOTO(out_child, rc);
1979 child_bits &= ~MDS_INODELOCK_LOOKUP;
1981 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1983 PFID(mdt_object_fid(parent)),
1984 PFID(&reqbody->mbo_fid2), ldlm_rep);
1987 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1989 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1990 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1992 "Parent doesn't exist!");
1993 GOTO(out_child, rc = -ESTALE);
1996 if (lu_name_is_valid(lname)) {
1997 /* Always allow to lookup ".." */
1998 if (unlikely(lname->ln_namelen == 2 &&
1999 lname->ln_name[0] == '.' &&
2000 lname->ln_name[1] == '.'))
2001 info->mti_spec.sp_permitted = 1;
2003 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2004 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
2009 /* step 1: lock parent only if parent is a directory */
2010 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2011 lhp = &info->mti_lh[MDT_LH_PARENT];
2012 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2013 rc = mdt_object_lock(info, parent, lhp,
2014 MDS_INODELOCK_UPDATE);
2015 if (unlikely(rc != 0))
2019 /* step 2: lookup child's fid by name */
2020 fid_zero(child_fid);
2021 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2022 child_fid, &info->mti_spec);
2024 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2027 GOTO(unlock_parent, rc);
2029 child = mdt_object_find(info->mti_env, info->mti_mdt,
2031 if (unlikely(IS_ERR(child)))
2032 GOTO(unlock_parent, rc = PTR_ERR(child));
2035 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2037 /* step 3: lock child regardless if it is local or remote. */
2040 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2041 if (!mdt_object_exists(child)) {
2042 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2044 "Object doesn't exist!");
2045 GOTO(out_child, rc = -ENOENT);
2048 rc = mdt_check_resent_lock(info, child, lhc);
2050 GOTO(out_child, rc);
2051 } else if (rc > 0) {
2052 mdt_lock_handle_init(lhc);
2053 mdt_lock_reg_init(lhc, LCK_PR);
2055 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2056 !mdt_object_remote(child)) {
2057 struct md_attr *ma = &info->mti_attr;
2060 ma->ma_need = MA_INODE;
2061 rc = mdt_attr_get_complex(info, child, ma);
2062 if (unlikely(rc != 0))
2063 GOTO(out_child, rc);
2065 /* If the file has not been changed for some time, we
2066 * return not only a LOOKUP lock, but also an UPDATE
2067 * lock and this might save us RPC on later STAT. For
2068 * directories, it also let negative dentry cache start
2069 * working for this dir. */
2070 if (ma->ma_valid & MA_INODE &&
2071 ma->ma_attr.la_valid & LA_CTIME &&
2072 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2073 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2074 child_bits |= MDS_INODELOCK_UPDATE;
2077 /* layout lock must be granted in a best-effort way
2078 * for IT operations */
2079 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2080 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2081 !mdt_object_remote(child) && ldlm_rep != NULL) {
2082 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2083 exp_connect_layout(info->mti_exp)) {
2084 /* try to grant layout lock for regular file. */
2085 try_bits = MDS_INODELOCK_LAYOUT;
2087 /* Acquire DOM lock in advance for data-on-mdt file */
2088 if (child != parent)
2089 try_bits |= MDS_INODELOCK_DOM;
2092 if (try_bits != 0) {
2093 /* try layout lock, it may fail to be granted due to
2094 * contention at LOOKUP or UPDATE */
2095 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2097 if (child_bits & MDS_INODELOCK_LAYOUT)
2100 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2101 * client will enqueue the lock to the remote MDT */
2102 if (mdt_object_remote(child))
2103 child_bits &= ~MDS_INODELOCK_UPDATE;
2104 rc = mdt_object_lock(info, child, lhc, child_bits);
2106 if (unlikely(rc != 0))
2107 GOTO(out_child, rc);
2110 /* finally, we can get attr for child. */
2111 rc = mdt_getattr_internal(info, child, ma_need);
2112 if (unlikely(rc != 0)) {
2113 mdt_object_unlock(info, child, lhc, 1);
2114 GOTO(out_child, rc);
2117 rc = mdt_pack_secctx_in_reply(info, child);
2119 mdt_object_unlock(info, child, lhc, 1);
2120 GOTO(out_child, rc);
2123 rc = mdt_pack_encctx_in_reply(info, child);
2125 mdt_object_unlock(info, child, lhc, 1);
2126 GOTO(out_child, rc);
2129 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2131 /* Debugging code. */
2132 LDLM_DEBUG(lock, "Returning lock to client");
2133 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2134 &lock->l_resource->lr_name),
2135 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2136 PLDLMRES(lock->l_resource),
2137 PFID(mdt_object_fid(child)));
2139 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2140 !mdt_object_remote(child) && child != parent) {
2141 mdt_object_put(info->mti_env, child);
2142 rc = mdt_pack_size2body(info, child_fid,
2144 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2145 /* DOM lock was taken in advance but this is
2146 * not DoM file. Drop the lock.
2148 lock_res_and_lock(lock);
2149 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2150 unlock_res_and_lock(lock);
2152 LDLM_LOCK_PUT(lock);
2153 GOTO(unlock_parent, rc = 0);
2155 LDLM_LOCK_PUT(lock);
2161 mdt_object_put(info->mti_env, child);
2164 mdt_object_unlock(info, parent, lhp, 1);
2168 /* normal handler: should release the child lock */
2169 static int mdt_getattr_name(struct tgt_session_info *tsi)
2171 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2172 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2173 struct mdt_body *reqbody;
2174 struct mdt_body *repbody;
2179 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2180 LASSERT(reqbody != NULL);
2181 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2182 LASSERT(repbody != NULL);
2184 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2185 repbody->mbo_eadatasize = 0;
2186 repbody->mbo_aclsize = 0;
2188 rc = mdt_init_ucred(info, reqbody);
2190 GOTO(out_shrink, rc);
2192 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2193 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2194 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2195 lhc->mlh_reg_lh.cookie = 0;
2197 mdt_exit_ucred(info);
2200 mdt_client_compatibility(info);
2201 rc2 = mdt_fix_reply(info);
2204 mdt_thread_info_fini(info);
2208 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2209 const struct lu_fid *pfid,
2210 const struct lu_name *name,
2211 struct mdt_object *obj, s64 ctime)
2213 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2214 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2215 struct mdt_device *mdt = info->mti_mdt;
2216 struct md_attr *ma = &info->mti_attr;
2217 struct mdt_lock_handle *parent_lh;
2218 struct mdt_lock_handle *child_lh;
2219 struct mdt_object *pobj;
2220 bool cos_incompat = false;
2224 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2226 GOTO(out, rc = PTR_ERR(pobj));
2228 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2229 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2230 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2232 GOTO(put_parent, rc);
2234 if (mdt_object_remote(pobj))
2235 cos_incompat = true;
2237 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2238 name, child_fid, &info->mti_spec);
2240 GOTO(unlock_parent, rc);
2242 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2243 GOTO(unlock_parent, rc = -EREMCHG);
2245 child_lh = &info->mti_lh[MDT_LH_CHILD];
2246 mdt_lock_reg_init(child_lh, LCK_EX);
2247 rc = mdt_reint_striped_lock(info, obj, child_lh,
2248 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2249 einfo, cos_incompat);
2251 GOTO(unlock_parent, rc);
2253 if (atomic_read(&obj->mot_open_count)) {
2254 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2255 PFID(mdt_object_fid(obj)));
2256 GOTO(unlock_child, rc = -EBUSY);
2260 ma->ma_valid = MA_INODE;
2261 ma->ma_attr.la_valid = LA_CTIME;
2262 ma->ma_attr.la_ctime = ctime;
2264 mutex_lock(&obj->mot_lov_mutex);
2266 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2267 mdt_object_child(obj), name, ma, 0);
2269 mutex_unlock(&obj->mot_lov_mutex);
2272 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2274 mdt_object_unlock(info, pobj, parent_lh, 1);
2276 mdt_object_put(info->mti_env, pobj);
2281 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2282 struct mdt_object *obj)
2284 struct lu_ucred *uc = lu_ucred(info->mti_env);
2285 struct md_attr *ma = &info->mti_attr;
2286 struct lu_attr *la = &ma->ma_attr;
2290 ma->ma_need = MA_INODE;
2291 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2295 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2298 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
2300 if (uc->uc_fsuid == la->la_uid) {
2301 if ((la->la_mode & S_IWUSR) == 0)
2303 } else if (uc->uc_fsgid == la->la_gid) {
2304 if ((la->la_mode & S_IWGRP) == 0)
2306 } else if ((la->la_mode & S_IWOTH) == 0) {
2314 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2317 struct mdt_device *mdt = info->mti_mdt;
2318 struct mdt_object *obj = NULL;
2319 struct linkea_data ldata = { NULL };
2320 struct lu_buf *buf = &info->mti_big_buf;
2321 struct lu_name *name = &info->mti_name;
2322 struct lu_fid *pfid = &info->mti_tmp_fid1;
2323 struct link_ea_header *leh;
2324 struct link_ea_entry *lee;
2325 int reclen, count, rc = 0;
2328 if (!fid_is_sane(fid))
2329 GOTO(out, rc = -EINVAL);
2331 if (!fid_is_namespace_visible(fid))
2332 GOTO(out, rc = -EINVAL);
2334 obj = mdt_object_find(info->mti_env, mdt, fid);
2336 GOTO(out, rc = PTR_ERR(obj));
2338 if (mdt_object_remote(obj))
2339 GOTO(out, rc = -EREMOTE);
2340 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2341 GOTO(out, rc = -ENOENT);
2343 rc = mdt_rmfid_check_permission(info, obj);
2348 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2350 GOTO(out, rc = -ENOMEM);
2353 rc = mdt_links_read(info, obj, &ldata);
2358 lee = (struct link_ea_entry *)(leh + 1);
2359 for (count = 0; count < leh->leh_reccount; count++) {
2360 /* remove every hardlink */
2361 linkea_entry_unpack(lee, &reclen, name, pfid);
2362 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2363 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2369 if (obj && !IS_ERR(obj))
2370 mdt_object_put(info->mti_env, obj);
2371 if (info->mti_big_buf.lb_buf)
2372 lu_buf_free(&info->mti_big_buf);
2377 static int mdt_rmfid(struct tgt_session_info *tsi)
2379 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2380 struct mdt_body *reqbody;
2381 struct lu_fid *fids, *rfids;
2387 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2388 if (reqbody == NULL)
2390 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2392 nr = bufsize / sizeof(struct lu_fid);
2393 if (nr * sizeof(struct lu_fid) != bufsize)
2395 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2396 RCL_SERVER, nr * sizeof(__u32));
2397 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2398 RCL_SERVER, nr * sizeof(struct lu_fid));
2399 rc = req_capsule_server_pack(tsi->tsi_pill);
2401 GOTO(out, rc = err_serious(rc));
2402 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2405 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2407 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2410 mdt_init_ucred(mti, reqbody);
2411 for (i = 0; i < nr; i++) {
2413 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2415 mdt_exit_ucred(mti);
2421 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2422 void *karg, void __user *uarg);
2424 static int mdt_set_info(struct tgt_session_info *tsi)
2426 struct ptlrpc_request *req = tgt_ses_req(tsi);
2429 int keylen, vallen, rc = 0;
2433 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2435 DEBUG_REQ(D_HA, req, "no set_info key");
2436 RETURN(err_serious(-EFAULT));
2439 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2442 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2444 DEBUG_REQ(D_HA, req, "no set_info val");
2445 RETURN(err_serious(-EFAULT));
2448 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2451 /* Swab any part of val you need to here */
2452 if (KEY_IS(KEY_READ_ONLY)) {
2453 spin_lock(&req->rq_export->exp_lock);
2455 *exp_connect_flags_ptr(req->rq_export) |=
2458 *exp_connect_flags_ptr(req->rq_export) &=
2459 ~OBD_CONNECT_RDONLY;
2460 spin_unlock(&req->rq_export->exp_lock);
2461 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2462 struct changelog_setinfo *cs = val;
2464 if (vallen != sizeof(*cs)) {
2465 CERROR("%s: bad changelog_clear setinfo size %d\n",
2466 tgt_name(tsi->tsi_tgt), vallen);
2469 if (ptlrpc_req_need_swab(req)) {
2470 __swab64s(&cs->cs_recno);
2471 __swab32s(&cs->cs_id);
2474 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2476 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2478 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2480 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2487 static int mdt_readpage(struct tgt_session_info *tsi)
2489 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2490 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2491 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2492 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2493 struct mdt_body *repbody;
2499 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2500 RETURN(err_serious(-ENOMEM));
2502 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2503 if (repbody == NULL || reqbody == NULL)
2504 RETURN(err_serious(-EFAULT));
2507 * prepare @rdpg before calling lower layers and transfer itself. Here
2508 * reqbody->size contains offset of where to start to read and
2509 * reqbody->nlink contains number bytes to read.
2511 rdpg->rp_hash = reqbody->mbo_size;
2512 if (rdpg->rp_hash != reqbody->mbo_size) {
2513 CERROR("Invalid hash: %#llx != %#llx\n",
2514 rdpg->rp_hash, reqbody->mbo_size);
2518 rdpg->rp_attrs = reqbody->mbo_mode;
2519 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2520 rdpg->rp_attrs |= LUDA_64BITHASH;
2521 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2522 exp_max_brw_size(tsi->tsi_exp));
2523 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2525 OBD_ALLOC_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2526 if (rdpg->rp_pages == NULL)
2529 for (i = 0; i < rdpg->rp_npages; ++i) {
2530 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2531 if (rdpg->rp_pages[i] == NULL)
2532 GOTO(free_rdpg, rc = -ENOMEM);
2535 /* call lower layers to fill allocated pages with directory data */
2536 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2538 GOTO(free_rdpg, rc);
2540 /* send pages to client */
2541 rc = tgt_sendpage(tsi, rdpg, rc);
2546 for (i = 0; i < rdpg->rp_npages; i++)
2547 if (rdpg->rp_pages[i] != NULL)
2548 __free_page(rdpg->rp_pages[i]);
2549 OBD_FREE_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2551 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2557 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2559 struct lu_ucred *uc = mdt_ucred_check(info);
2560 struct lu_attr *attr = &info->mti_attr.ma_attr;
2565 if (op != REINT_SETATTR) {
2566 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2567 attr->la_uid = uc->uc_fsuid;
2568 /* for S_ISGID, inherit gid from his parent, such work will be
2569 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2570 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2571 attr->la_gid = uc->uc_fsgid;
2577 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2579 return op == REINT_OPEN &&
2580 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2583 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2585 struct req_capsule *pill = info->mti_pill;
2587 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2589 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2591 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2593 /* pre-set size in server part with max size */
2594 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2596 OBD_MAX_DEFAULT_EA_SIZE);
2598 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2603 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
2605 struct req_capsule *pill = info->mti_pill;
2607 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
2609 /* pre-set size in server part with max size */
2610 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
2612 info->mti_mdt->mdt_max_mdsize);
2615 static int mdt_reint_internal(struct mdt_thread_info *info,
2616 struct mdt_lock_handle *lhc,
2619 struct req_capsule *pill = info->mti_pill;
2620 struct mdt_body *repbody;
2625 rc = mdt_reint_unpack(info, op);
2627 CERROR("Can't unpack reint, rc %d\n", rc);
2628 RETURN(err_serious(rc));
2632 /* check if the file system is set to readonly. O_RDONLY open
2633 * is still allowed even the file system is set to readonly mode */
2634 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2635 RETURN(err_serious(-EROFS));
2637 /* for replay (no_create) lmm is not needed, client has it already */
2638 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2639 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2642 /* llog cookies are always 0, the field is kept for compatibility */
2643 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2644 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2646 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2647 * by default. If the target object has more ACL entries, then
2648 * enlarge the buffer when necessary. */
2649 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2650 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2651 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2653 mdt_preset_secctx_size(info);
2654 mdt_preset_encctx_size(info);
2656 rc = req_capsule_server_pack(pill);
2658 CERROR("Can't pack response, rc %d\n", rc);
2659 RETURN(err_serious(rc));
2662 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2663 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2665 repbody->mbo_eadatasize = 0;
2666 repbody->mbo_aclsize = 0;
2669 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2671 /* for replay no cookkie / lmm need, because client have this already */
2672 if (info->mti_spec.no_create)
2673 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2674 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2676 rc = mdt_init_ucred_reint(info);
2678 GOTO(out_shrink, rc);
2680 rc = mdt_fix_attr_ucred(info, op);
2682 GOTO(out_ucred, rc = err_serious(rc));
2684 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2686 GOTO(out_ucred, rc);
2687 } else if (rc == 1) {
2688 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2689 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2690 GOTO(out_ucred, rc);
2692 rc = mdt_reint_rec(info, lhc);
2695 mdt_exit_ucred(info);
2697 mdt_client_compatibility(info);
2699 rc2 = mdt_fix_reply(info);
2704 * Data-on-MDT optimization - read data along with OPEN and return it
2705 * in reply when possible.
2707 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2708 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2714 static long mdt_reint_opcode(struct ptlrpc_request *req,
2715 const struct req_format **fmt)
2717 struct mdt_device *mdt;
2718 struct mdt_rec_reint *rec;
2721 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2723 opc = rec->rr_opcode;
2724 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2725 if (opc < REINT_MAX && fmt[opc] != NULL)
2726 req_capsule_extend(&req->rq_pill, fmt[opc]);
2728 mdt = mdt_exp2dev(req->rq_export);
2729 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2730 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2731 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2732 opc = err_serious(-EFAULT);
2735 opc = err_serious(-EFAULT);
2740 static int mdt_reint(struct tgt_session_info *tsi)
2744 static const struct req_format *reint_fmts[REINT_MAX] = {
2745 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2746 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2747 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2748 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2749 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2750 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2751 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2752 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2753 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
2754 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2759 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2761 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2763 * No lock possible here from client to pass it to reint code
2766 rc = mdt_reint_internal(info, NULL, opc);
2767 mdt_thread_info_fini(info);
2772 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2776 /* this should sync the whole device */
2777 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2779 struct dt_device *dt = mdt->mdt_bottom;
2783 rc = dt->dd_ops->dt_sync(env, dt);
2787 /* this should sync this object */
2788 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2789 struct mdt_object *mo)
2795 if (!mdt_object_exists(mo)) {
2796 CWARN("%s: non existing object "DFID": rc = %d\n",
2797 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2802 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2803 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2804 dt_obj_version_t version;
2806 version = dt_version_get(env, mdt_obj2dt(mo));
2807 if (version > tgt->lut_obd->obd_last_committed)
2808 rc = mo_object_sync(env, mdt_object_child(mo));
2810 rc = mo_object_sync(env, mdt_object_child(mo));
2816 static int mdt_sync(struct tgt_session_info *tsi)
2818 struct ptlrpc_request *req = tgt_ses_req(tsi);
2819 struct req_capsule *pill = tsi->tsi_pill;
2820 struct mdt_body *body;
2821 ktime_t kstart = ktime_get();
2826 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2827 RETURN(err_serious(-ENOMEM));
2829 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2830 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2832 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2834 if (unlikely(info->mti_object == NULL))
2837 /* sync an object */
2838 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2841 const struct lu_fid *fid;
2842 struct lu_attr *la = &info->mti_attr.ma_attr;
2844 info->mti_attr.ma_need = MA_INODE;
2845 info->mti_attr.ma_valid = 0;
2846 rc = mdt_attr_get_complex(info, info->mti_object,
2849 body = req_capsule_server_get(pill,
2851 fid = mdt_object_fid(info->mti_object);
2852 mdt_pack_attr2body(info, body, la, fid);
2855 mdt_thread_info_fini(info);
2858 mdt_counter_incr(req, LPROC_MDT_SYNC,
2859 ktime_us_delta(ktime_get(), kstart));
2864 static int mdt_data_sync(struct tgt_session_info *tsi)
2866 struct mdt_thread_info *info;
2867 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2868 struct ost_body *body = tsi->tsi_ost_body;
2869 struct ost_body *repbody;
2870 struct mdt_object *mo = NULL;
2876 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2878 /* if no fid is specified then do nothing,
2879 * device sync is done via MDS_SYNC */
2880 if (fid_is_zero(&tsi->tsi_fid))
2883 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2885 RETURN(PTR_ERR(mo));
2887 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2891 repbody->oa.o_oi = body->oa.o_oi;
2892 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2894 info = tsi2mdt_info(tsi);
2895 ma = &info->mti_attr;
2896 ma->ma_need = MA_INODE;
2898 rc = mdt_attr_get_complex(info, mo, ma);
2900 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2903 mdt_thread_info_fini(info);
2908 mdt_object_put(tsi->tsi_env, mo);
2913 * Handle quota control requests to consult current usage/limit, but also
2914 * to configure quota enforcement
2916 static int mdt_quotactl(struct tgt_session_info *tsi)
2918 struct obd_export *exp = tsi->tsi_exp;
2919 struct req_capsule *pill = tsi->tsi_pill;
2920 struct obd_quotactl *oqctl, *repoqc;
2922 struct mdt_device *mdt = mdt_exp2dev(exp);
2923 struct lu_device *qmt = mdt->mdt_qmt_dev;
2924 struct lu_nodemap *nodemap;
2927 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2929 RETURN(err_serious(-EPROTO));
2931 rc = req_capsule_server_pack(pill);
2933 RETURN(err_serious(rc));
2935 nodemap = nodemap_get_from_exp(exp);
2936 if (IS_ERR(nodemap))
2937 RETURN(PTR_ERR(nodemap));
2939 switch (oqctl->qc_cmd) {
2940 /* master quotactl */
2943 case LUSTRE_Q_SETDEFAULT:
2944 case LUSTRE_Q_SETQUOTAPOOL:
2945 case LUSTRE_Q_SETINFOPOOL:
2946 if (!nodemap_can_setquota(nodemap))
2947 GOTO(out_nodemap, rc = -EPERM);
2951 case LUSTRE_Q_GETDEFAULT:
2952 case LUSTRE_Q_GETQUOTAPOOL:
2953 case LUSTRE_Q_GETINFOPOOL:
2955 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2956 /* slave quotactl */
2963 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2964 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2965 GOTO(out_nodemap, rc);
2969 switch (oqctl->qc_type) {
2971 id = nodemap_map_id(nodemap, NODEMAP_UID,
2972 NODEMAP_CLIENT_TO_FS, id);
2975 id = nodemap_map_id(nodemap, NODEMAP_GID,
2976 NODEMAP_CLIENT_TO_FS, id);
2979 /* todo: check/map project id */
2983 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2985 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2987 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2989 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2990 barrier_exit(tsi->tsi_tgt->lut_bottom);
2992 if (oqctl->qc_id != id)
2993 swap(oqctl->qc_id, id);
2995 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2996 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2997 RETURN(-EINPROGRESS);
3000 switch (oqctl->qc_cmd) {
3006 case LUSTRE_Q_SETDEFAULT:
3007 case LUSTRE_Q_GETDEFAULT:
3008 case LUSTRE_Q_SETQUOTAPOOL:
3009 case LUSTRE_Q_GETQUOTAPOOL:
3010 case LUSTRE_Q_SETINFOPOOL:
3011 case LUSTRE_Q_GETINFOPOOL:
3012 /* forward quotactl request to QMT */
3013 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3018 /* slave quotactl */
3019 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3024 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3025 GOTO(out_nodemap, rc = -EFAULT);
3028 if (oqctl->qc_id != id)
3029 swap(oqctl->qc_id, id);
3031 QCTL_COPY(repoqc, oqctl);
3035 nodemap_putref(nodemap);
3040 /** clone llog ctxt from child (mdd)
3041 * This allows remote llog (replicator) access.
3042 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3043 * context was originally set up, or we can handle them directly.
3044 * I choose the latter, but that means I need any llog
3045 * contexts set up by child to be accessable by the mdt. So we clone the
3046 * context into our context list here.
3048 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3051 struct md_device *next = mdt->mdt_child;
3052 struct llog_ctxt *ctxt;
3055 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3058 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3059 if (rc || ctxt == NULL) {
3063 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3065 CERROR("Can't set mdt ctxt %d\n", rc);
3070 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3071 struct mdt_device *mdt, int idx)
3073 struct llog_ctxt *ctxt;
3075 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3078 /* Put once for the get we just did, and once for the clone */
3079 llog_ctxt_put(ctxt);
3080 llog_ctxt_put(ctxt);
3085 * sec context handlers
3087 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3089 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3095 * quota request handlers
3097 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3099 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3100 struct lu_device *qmt = mdt->mdt_qmt_dev;
3105 RETURN(err_serious(-EOPNOTSUPP));
3107 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3111 struct mdt_object *mdt_object_new(const struct lu_env *env,
3112 struct mdt_device *d,
3113 const struct lu_fid *f)
3115 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3116 struct lu_object *o;
3117 struct mdt_object *m;
3120 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3121 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3122 if (unlikely(IS_ERR(o)))
3123 m = (struct mdt_object *)o;
3129 struct mdt_object *mdt_object_find(const struct lu_env *env,
3130 struct mdt_device *d,
3131 const struct lu_fid *f)
3133 struct lu_object *o;
3134 struct mdt_object *m;
3137 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3138 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3139 if (unlikely(IS_ERR(o)))
3140 m = (struct mdt_object *)o;
3148 * Asyncronous commit for mdt device.
3150 * Pass asynchonous commit call down the MDS stack.
3152 * \param env environment
3153 * \param mdt the mdt device
3155 static void mdt_device_commit_async(const struct lu_env *env,
3156 struct mdt_device *mdt)
3158 struct dt_device *dt = mdt->mdt_bottom;
3162 rc = dt->dd_ops->dt_commit_async(env, dt);
3163 if (unlikely(rc != 0))
3164 CWARN("%s: async commit start failed: rc = %d\n",
3165 mdt_obd_name(mdt), rc);
3166 atomic_inc(&mdt->mdt_async_commit_count);
3171 * Mark the lock as "synchonous".
3173 * Mark the lock to deffer transaction commit to the unlock time.
3175 * \param lock the lock to mark as "synchonous"
3177 * \see mdt_is_lock_sync
3178 * \see mdt_save_lock
3180 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3182 lock->l_ast_data = (void*)1;
3186 * Check whehter the lock "synchonous" or not.
3188 * \param lock the lock to check
3189 * \retval 1 the lock is "synchonous"
3190 * \retval 0 the lock isn't "synchronous"
3192 * \see mdt_set_lock_sync
3193 * \see mdt_save_lock
3195 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3197 return lock->l_ast_data != NULL;
3201 * Blocking AST for mdt locks.
3203 * Starts transaction commit if in case of COS lock conflict or
3204 * deffers such a commit to the mdt_save_lock.
3206 * \param lock the lock which blocks a request or cancelling lock
3207 * \param desc unused
3208 * \param data unused
3209 * \param flag indicates whether this cancelling or blocking callback
3211 * \see ldlm_blocking_ast_nocheck
3213 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3214 void *data, int flag)
3216 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3217 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3218 struct ldlm_cb_set_arg *arg = data;
3219 bool commit_async = false;
3223 if (flag == LDLM_CB_CANCELING)
3226 lock_res_and_lock(lock);
3227 if (lock->l_blocking_ast != mdt_blocking_ast) {
3228 unlock_res_and_lock(lock);
3232 /* A blocking ast may be sent from ldlm_lock_decref_internal
3233 * when the last reference to a local lock was released and
3234 * during blocking event from ldlm_work_bl_ast_lock().
3235 * The 'data' parameter is l_ast_data in the first case and
3236 * callback arguments in the second one. Distinguish them by that.
3238 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3239 goto skip_cos_checks;
3241 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3242 if (mdt_cos_is_enabled(mdt)) {
3243 if (!arg->bl_desc->bl_same_client)
3244 mdt_set_lock_sync(lock);
3245 } else if (mdt_slc_is_enabled(mdt) &&
3246 arg->bl_desc->bl_cos_incompat) {
3247 mdt_set_lock_sync(lock);
3249 * we may do extra commit here, but there is a small
3250 * window to miss a commit: lock was unlocked (saved),
3251 * then a conflict lock queued and we come here, but
3252 * REP-ACK not received, so lock was not converted to
3254 * Fortunately this window is quite small, so the
3255 * extra commit should be rare (not to say distributed
3256 * operation is rare too).
3258 commit_async = true;
3260 } else if (lock->l_req_mode == LCK_COS) {
3261 commit_async = true;
3265 rc = ldlm_blocking_ast_nocheck(lock);
3270 rc = lu_env_init(&env, LCT_LOCAL);
3271 if (unlikely(rc != 0))
3272 CWARN("%s: lu_env initialization failed, cannot "
3273 "start asynchronous commit: rc = %d\n",
3276 mdt_device_commit_async(&env, mdt);
3283 * Blocking AST for cross-MDT lock
3285 * Discard lock from uncommitted_slc_locks and cancel it.
3287 * \param lock the lock which blocks a request or cancelling lock
3288 * \param desc unused
3289 * \param data unused
3290 * \param flag indicates whether this cancelling or blocking callback
3291 * \retval 0 on success
3292 * \retval negative number on error
3294 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3295 void *data, int flag)
3301 case LDLM_CB_BLOCKING: {
3302 struct lustre_handle lockh;
3304 ldlm_lock2handle(lock, &lockh);
3305 rc = ldlm_cli_cancel(&lockh,
3306 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3308 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3313 case LDLM_CB_CANCELING: {
3314 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3315 struct mdt_device *mdt =
3316 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3318 LDLM_DEBUG(lock, "Revoke remote lock\n");
3320 /* discard slc lock here so that it can be cleaned anytime,
3321 * especially for cleanup_resource() */
3322 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3324 /* once we cache lock, l_ast_data is set to mdt_object */
3325 if (lock->l_ast_data != NULL) {
3326 struct mdt_object *mo = lock->l_ast_data;
3329 rc = lu_env_init(&env, LCT_MD_THREAD);
3330 if (unlikely(rc != 0)) {
3331 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3333 PFID(mdt_object_fid(mo)), rc);
3337 if (lock->l_policy_data.l_inodebits.bits &
3338 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3339 rc = mo_invalidate(&env, mdt_object_child(mo));
3340 mo->mot_cache_attr = 0;
3342 mdt_object_put(&env, mo);
3354 int mdt_check_resent_lock(struct mdt_thread_info *info,
3355 struct mdt_object *mo,
3356 struct mdt_lock_handle *lhc)
3358 /* the lock might already be gotten in ldlm_handle_enqueue() */
3359 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3360 struct ptlrpc_request *req = mdt_info_req(info);
3361 struct ldlm_lock *lock;
3363 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3364 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3366 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3367 * a resend case, however, it could be already destroyed
3368 * due to client eviction or a raced cancel RPC. */
3369 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3370 lhc->mlh_reg_lh.cookie);
3374 if (!fid_res_name_eq(mdt_object_fid(mo),
3375 &lock->l_resource->lr_name)) {
3376 CWARN("%s: Although resent, but still not "
3377 "get child lock:"DFID"\n",
3378 info->mti_exp->exp_obd->obd_name,
3379 PFID(mdt_object_fid(mo)));
3380 LDLM_LOCK_PUT(lock);
3383 LDLM_LOCK_PUT(lock);
3389 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3391 mdt_object_get(NULL, lock->l_ast_data);
3394 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3395 struct mdt_object *o, const struct lu_fid *fid,
3396 struct lustre_handle *lh, enum ldlm_mode mode,
3397 __u64 *ibits, __u64 trybits, bool cache)
3399 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3400 union ldlm_policy_data *policy = &mti->mti_policy;
3401 struct ldlm_res_id *res_id = &mti->mti_res_id;
3405 LASSERT(mdt_object_remote(o));
3407 fid_build_reg_res_name(fid, res_id);
3409 memset(einfo, 0, sizeof(*einfo));
3410 einfo->ei_type = LDLM_IBITS;
3411 einfo->ei_mode = mode;
3412 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3413 einfo->ei_cb_cp = ldlm_completion_ast;
3414 einfo->ei_enq_slave = 0;
3415 einfo->ei_res_id = res_id;
3419 * if we cache lock, couple lock with mdt_object, so that object
3420 * can be easily found in lock ASTs.
3422 einfo->ei_cbdata = o;
3423 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3426 memset(policy, 0, sizeof(*policy));
3427 policy->l_inodebits.bits = *ibits;
3428 policy->l_inodebits.try_bits = trybits;
3430 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3433 /* Return successfully acquired bits to a caller */
3435 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3438 *ibits = lock->l_policy_data.l_inodebits.bits;
3439 LDLM_LOCK_PUT(lock);
3444 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3445 const struct lu_fid *fid, struct lustre_handle *lh,
3446 enum ldlm_mode mode, __u64 ibits, bool cache)
3448 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3452 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3453 struct mdt_lock_handle *lh, __u64 *ibits,
3454 __u64 trybits, bool cos_incompat)
3456 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3457 union ldlm_policy_data *policy = &info->mti_policy;
3458 struct ldlm_res_id *res_id = &info->mti_res_id;
3459 __u64 dlmflags = 0, *cookie = NULL;
3463 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3464 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3465 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3466 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3469 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3470 lh->mlh_reg_mode == LCK_EX);
3471 dlmflags |= LDLM_FL_COS_INCOMPAT;
3472 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3473 dlmflags |= LDLM_FL_COS_ENABLED;
3476 /* Only enqueue LOOKUP lock for remote object */
3477 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3479 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3480 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3481 *ibits == MDS_INODELOCK_OPEN)
3482 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3484 if (lh->mlh_type == MDT_PDO_LOCK) {
3485 /* check for exists after object is locked */
3486 if (mdt_object_exists(o) == 0) {
3487 /* Non-existent object shouldn't have PDO lock */
3490 /* Non-dir object shouldn't have PDO lock */
3491 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3496 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3497 dlmflags |= LDLM_FL_ATOMIC_CB;
3500 cookie = &info->mti_exp->exp_handle.h_cookie;
3503 * Take PDO lock on whole directory and build correct @res_id for lock
3504 * on part of directory.
3506 if (lh->mlh_pdo_hash != 0) {
3507 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3508 mdt_lock_pdo_mode(info, o, lh);
3509 if (lh->mlh_pdo_mode != LCK_NL) {
3511 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3512 * is never going to be sent to client and we do not
3513 * want it slowed down due to possible cancels.
3515 policy->l_inodebits.bits =
3516 *ibits & MDS_INODELOCK_UPDATE;
3517 policy->l_inodebits.try_bits =
3518 trybits & MDS_INODELOCK_UPDATE;
3519 /* at least one of them should be set */
3520 LASSERT(policy->l_inodebits.bits |
3521 policy->l_inodebits.try_bits);
3522 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3523 lh->mlh_pdo_mode, policy, res_id,
3525 if (unlikely(rc != 0))
3526 GOTO(out_unlock, rc);
3530 * Finish res_id initializing by name hash marking part of
3531 * directory which is taking modification.
3533 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3536 policy->l_inodebits.bits = *ibits;
3537 policy->l_inodebits.try_bits = trybits;
3538 policy->l_inodebits.li_gid = lh->mlh_gid;
3541 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3542 * going to be sent to client. If it is - mdt_intent_policy() path will
3543 * fix it up and turn FL_LOCAL flag off.
3545 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3546 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3550 mdt_object_unlock(info, o, lh, 1);
3551 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3552 lh->mlh_pdo_hash != 0 &&
3553 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3554 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3556 /* Return successfully acquired bits to a caller */
3558 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3561 *ibits = lock->l_policy_data.l_inodebits.bits;
3562 LDLM_LOCK_PUT(lock);
3568 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3569 struct mdt_lock_handle *lh, __u64 *ibits,
3570 __u64 trybits, bool cos_incompat)
3572 struct mdt_lock_handle *local_lh = NULL;
3576 if (!mdt_object_remote(o)) {
3577 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3582 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3583 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3584 MDS_INODELOCK_XATTR);
3586 /* Only enqueue LOOKUP lock for remote object */
3587 if (*ibits & MDS_INODELOCK_LOOKUP) {
3588 __u64 local = MDS_INODELOCK_LOOKUP;
3590 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3598 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3599 /* Sigh, PDO needs to enqueue 2 locks right now, but
3600 * enqueue RPC can only request 1 lock, to avoid extra
3601 * RPC, so it will instead enqueue EX lock for remote
3602 * object anyway XXX*/
3603 if (lh->mlh_type == MDT_PDO_LOCK &&
3604 lh->mlh_pdo_hash != 0) {
3606 "%s: "DFID" convert PDO lock to EX lock.\n",
3607 mdt_obd_name(info->mti_mdt),
3608 PFID(mdt_object_fid(o)));
3609 lh->mlh_pdo_hash = 0;
3610 lh->mlh_rreg_mode = LCK_EX;
3611 lh->mlh_type = MDT_REG_LOCK;
3614 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3617 ibits, trybits, false);
3618 if (rc != ELDLM_OK) {
3619 if (local_lh != NULL)
3620 mdt_object_unlock(info, o, local_lh, rc);
3625 /* other components like LFSCK can use lockless access
3626 * and populate cache, so we better invalidate it */
3627 mo_invalidate(info->mti_env, mdt_object_child(o));
3632 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3633 struct mdt_lock_handle *lh, __u64 ibits)
3635 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3638 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3639 struct mdt_lock_handle *lh, __u64 ibits,
3642 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3643 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3647 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3648 struct mdt_lock_handle *lh, __u64 *ibits,
3649 __u64 trybits, bool cos_incompat)
3651 bool trylock_only = *ibits == 0;
3654 LASSERT(!(*ibits & trybits));
3655 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3657 if (rc && trylock_only) { /* clear error for try ibits lock only */
3658 LASSERT(*ibits == 0);
3665 * Save a lock within request object.
3667 * Keep the lock referenced until whether client ACK or transaction
3668 * commit happens or release the lock immediately depending on input
3669 * parameters. If COS is ON, a write lock is converted to COS lock
3672 * \param info thead info object
3673 * \param h lock handle
3674 * \param mode lock mode
3675 * \param decref force immediate lock releasing
3677 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3678 enum ldlm_mode mode, int decref)
3682 if (lustre_handle_is_used(h)) {
3683 if (decref || !info->mti_has_trans ||
3684 !(mode & (LCK_PW | LCK_EX))) {
3685 mdt_fid_unlock(h, mode);
3687 struct mdt_device *mdt = info->mti_mdt;
3688 struct ldlm_lock *lock = ldlm_handle2lock(h);
3689 struct ptlrpc_request *req = mdt_info_req(info);
3690 bool cos = mdt_cos_is_enabled(mdt);
3691 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3693 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3696 /* there is no request if mdt_object_unlock() is called
3697 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3698 if (likely(req != NULL)) {
3699 LDLM_DEBUG(lock, "save lock request %p reply "
3700 "state %p transno %lld\n", req,
3701 req->rq_reply_state, req->rq_transno);
3703 ldlm_lock_mode_downgrade(lock, LCK_COS);
3706 if (req->rq_export->exp_disconnected)
3707 mdt_fid_unlock(h, mode);
3709 ptlrpc_save_lock(req, h, mode, cos,
3712 mdt_fid_unlock(h, mode);
3714 if (mdt_is_lock_sync(lock)) {
3715 CDEBUG(D_HA, "found sync-lock,"
3716 " async commit started\n");
3717 mdt_device_commit_async(info->mti_env,
3720 LDLM_LOCK_PUT(lock);
3729 * Save cross-MDT lock in uncommitted_slc_locks
3731 * Keep the lock referenced until transaction commit happens or release the lock
3732 * immediately depending on input parameters.
3734 * \param info thead info object
3735 * \param h lock handle
3736 * \param mode lock mode
3737 * \param decref force immediate lock releasing
3739 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3740 struct mdt_object *o, struct lustre_handle *h,
3741 enum ldlm_mode mode, int decref)
3745 if (lustre_handle_is_used(h)) {
3746 struct ldlm_lock *lock = ldlm_handle2lock(h);
3747 struct ptlrpc_request *req = mdt_info_req(info);
3750 (lock->l_policy_data.l_inodebits.bits &
3751 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3752 mo_invalidate(info->mti_env, mdt_object_child(o));
3754 if (decref || !info->mti_has_trans || !req ||
3755 !(mode & (LCK_PW | LCK_EX))) {
3756 ldlm_lock_decref_and_cancel(h, mode);
3757 LDLM_LOCK_PUT(lock);
3759 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3761 ldlm_lock_decref(h, mode);
3770 * Unlock mdt object.
3772 * Immeditely release the regular lock and the PDO lock or save the
3773 * lock in request and keep them referenced until client ACK or
3774 * transaction commit.
3776 * \param info thread info object
3777 * \param o mdt object
3778 * \param lh mdt lock handle referencing regular and PDO locks
3779 * \param decref force immediate lock releasing
3781 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3783 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3784 struct mdt_lock_handle *lh, int decref)
3788 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3789 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3790 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3796 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3797 const struct lu_fid *f,
3798 struct mdt_lock_handle *lh,
3801 struct mdt_object *o;
3803 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3807 rc = mdt_object_lock(info, o, lh, ibits);
3809 mdt_object_put(info->mti_env, o);
3816 void mdt_object_unlock_put(struct mdt_thread_info * info,
3817 struct mdt_object * o,
3818 struct mdt_lock_handle *lh,
3821 mdt_object_unlock(info, o, lh, decref);
3822 mdt_object_put(info->mti_env, o);
3826 * Generic code handling requests that have struct mdt_body passed in:
3828 * - extract mdt_body from request and save it in @info, if present;
3830 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3833 * - if HAS_BODY flag is set for this request type check whether object
3834 * actually exists on storage (lu_object_exists()).
3837 static int mdt_body_unpack(struct mdt_thread_info *info,
3838 enum tgt_handler_flags flags)
3840 const struct mdt_body *body;
3841 struct mdt_object *obj;
3842 const struct lu_env *env;
3843 struct req_capsule *pill;
3847 env = info->mti_env;
3848 pill = info->mti_pill;
3850 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3854 if (!(body->mbo_valid & OBD_MD_FLID))
3857 if (!fid_is_sane(&body->mbo_fid1)) {
3858 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3862 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3864 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
3865 mdt_object_put(env, obj);
3868 info->mti_object = obj;
3877 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
3878 enum tgt_handler_flags flags)
3880 struct req_capsule *pill = info->mti_pill;
3885 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3886 rc = mdt_body_unpack(info, flags);
3890 if (rc == 0 && (flags & HAS_REPLY)) {
3892 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3893 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3895 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3896 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3899 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3900 * by default. If the target object has more ACL entries, then
3901 * enlarge the buffer when necessary. */
3902 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3903 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3904 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3906 mdt_preset_secctx_size(info);
3907 mdt_preset_encctx_size(info);
3909 rc = req_capsule_server_pack(pill);
3911 CWARN("%s: cannot pack response: rc = %d\n",
3912 mdt_obd_name(info->mti_mdt), rc);
3917 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3919 lh->mlh_type = MDT_NUL_LOCK;
3920 lh->mlh_reg_lh.cookie = 0ull;
3921 lh->mlh_reg_mode = LCK_MINMODE;
3922 lh->mlh_pdo_lh.cookie = 0ull;
3923 lh->mlh_pdo_mode = LCK_MINMODE;
3924 lh->mlh_rreg_lh.cookie = 0ull;
3925 lh->mlh_rreg_mode = LCK_MINMODE;
3928 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3930 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3931 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3935 * Initialize fields of struct mdt_thread_info. Other fields are left in
3936 * uninitialized state, because it's too expensive to zero out whole
3937 * mdt_thread_info (> 1K) on each request arrival.
3939 void mdt_thread_info_init(struct ptlrpc_request *req,
3940 struct mdt_thread_info *info)
3944 info->mti_pill = &req->rq_pill;
3947 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3948 mdt_lock_handle_init(&info->mti_lh[i]);
3950 /* mdt device: it can be NULL while CONNECT */
3951 if (req->rq_export) {
3952 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3953 info->mti_exp = req->rq_export;
3955 info->mti_mdt = NULL;
3956 info->mti_env = req->rq_svc_thread->t_env;
3957 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3959 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3960 info->mti_big_buf = LU_BUF_NULL;
3961 info->mti_body = NULL;
3962 info->mti_object = NULL;
3963 info->mti_dlm_req = NULL;
3964 info->mti_has_trans = 0;
3965 info->mti_cross_ref = 0;
3966 info->mti_opdata = 0;
3967 info->mti_big_lmm_used = 0;
3968 info->mti_big_acl_used = 0;
3969 info->mti_som_valid = 0;
3971 info->mti_spec.no_create = 0;
3972 info->mti_spec.sp_rm_entry = 0;
3973 info->mti_spec.sp_permitted = 0;
3975 info->mti_spec.u.sp_ea.eadata = NULL;
3976 info->mti_spec.u.sp_ea.eadatalen = 0;
3979 void mdt_thread_info_fini(struct mdt_thread_info *info)
3983 if (info->mti_object != NULL) {
3984 mdt_object_put(info->mti_env, info->mti_object);
3985 info->mti_object = NULL;
3988 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3989 mdt_lock_handle_fini(&info->mti_lh[i]);
3990 info->mti_env = NULL;
3991 info->mti_pill = NULL;
3992 info->mti_exp = NULL;
3993 info->mti_mdt = NULL;
3995 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3996 lu_buf_free(&info->mti_big_buf);
3999 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4001 struct mdt_thread_info *mti;
4003 mti = mdt_th_info(tsi->tsi_env);
4004 LASSERT(mti != NULL);
4006 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4007 if (tsi->tsi_corpus != NULL) {
4008 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4009 lu_object_get(tsi->tsi_corpus);
4011 mti->mti_body = tsi->tsi_mdt_body;
4012 mti->mti_dlm_req = tsi->tsi_dlm_req;
4017 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4019 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4021 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4022 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4024 return tgt_connect(tsi);
4027 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4028 struct mdt_thread_info *info,
4029 struct ldlm_lock **lockp, __u64 flags)
4031 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4034 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4035 struct mdt_thread_info *info,
4036 struct ldlm_lock **lockp, __u64 flags)
4038 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4042 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4043 struct ldlm_lock **lockp,
4044 struct mdt_lock_handle *lh,
4045 __u64 flags, int result)
4047 struct ptlrpc_request *req = mdt_info_req(info);
4048 struct ldlm_lock *lock = *lockp;
4049 struct ldlm_lock *new_lock;
4051 /* If possible resent found a lock, @lh is set to its handle */
4052 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4054 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
4055 lh->mlh_reg_lh.cookie = 0;
4059 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
4060 /* Lock is pinned by ldlm_handle_enqueue0() as it is
4061 * a resend case, however, it could be already destroyed
4062 * due to client eviction or a raced cancel RPC. */
4063 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4064 lh->mlh_reg_lh.cookie);
4065 lh->mlh_reg_lh.cookie = 0;
4069 LASSERTF(new_lock != NULL,
4070 "lockh %#llx flags %#llx : rc = %d\n",
4071 lh->mlh_reg_lh.cookie, flags, result);
4074 * If we've already given this lock to a client once, then we should
4075 * have no readers or writers. Otherwise, we should have one reader
4076 * _or_ writer ref (which will be zeroed below) before returning the
4079 if (new_lock->l_export == req->rq_export) {
4080 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4082 LASSERT(new_lock->l_export == NULL);
4083 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4088 if (new_lock->l_export == req->rq_export) {
4090 * Already gave this to the client, which means that we
4091 * reconstructed a reply.
4093 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4096 LDLM_LOCK_RELEASE(new_lock);
4097 lh->mlh_reg_lh.cookie = 0;
4098 RETURN(ELDLM_LOCK_REPLACED);
4102 * Fixup the lock to be given to the client.
4104 lock_res_and_lock(new_lock);
4105 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4106 * possible blocking AST. */
4107 while (new_lock->l_readers > 0) {
4108 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4109 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4110 new_lock->l_readers--;
4112 while (new_lock->l_writers > 0) {
4113 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4114 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4115 new_lock->l_writers--;
4118 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4119 new_lock->l_blocking_ast = lock->l_blocking_ast;
4120 new_lock->l_completion_ast = lock->l_completion_ast;
4121 if (ldlm_has_dom(new_lock))
4122 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4123 new_lock->l_remote_handle = lock->l_remote_handle;
4124 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4126 unlock_res_and_lock(new_lock);
4128 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4129 &new_lock->l_remote_handle,
4130 &new_lock->l_exp_hash);
4132 LDLM_LOCK_RELEASE(new_lock);
4133 lh->mlh_reg_lh.cookie = 0;
4135 RETURN(ELDLM_LOCK_REPLACED);
4138 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4139 struct ldlm_lock *new_lock,
4140 struct mdt_lock_handle *lh, __u64 flags)
4142 struct ptlrpc_request *req = mdt_info_req(info);
4143 struct ldlm_request *dlmreq;
4145 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4148 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4150 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4151 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4153 if (flags & LDLM_FL_RESENT) {
4154 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4155 lh->mlh_reg_mode = new_lock->l_granted_mode;
4157 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4158 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4159 lh->mlh_reg_lh.cookie);
4164 * If the xid matches, then we know this is a resent request, and allow
4165 * it. (It's probably an OPEN, for which we don't send a lock.
4167 if (req_can_reconstruct(req, NULL) != 0)
4171 * This remote handle isn't enqueued, so we never received or processed
4172 * this request. Clear MSG_RESENT, because it can be handled like any
4173 * normal request now.
4175 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4177 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4178 dlmreq->lock_handle[0].cookie);
4181 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4182 struct mdt_thread_info *info,
4183 struct ldlm_lock **lockp,
4186 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4187 struct ldlm_reply *ldlm_rep = NULL;
4192 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4193 * (for the resend case) or a new lock. Below we will use it to
4194 * replace the original lock.
4196 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4197 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4198 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4199 rc = mdt_object_lock(info, info->mti_object, lhc,
4200 MDS_INODELOCK_XATTR);
4205 rc = mdt_getxattr(info);
4207 if (mdt_info_req(info)->rq_repmsg != NULL)
4208 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4210 if (ldlm_rep == NULL ||
4211 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4212 mdt_object_unlock(info, info->mti_object, lhc, 1);
4216 RETURN(err_serious(-EFAULT));
4219 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4221 /* This is left for interop instead of adding a new interop flag.
4223 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4224 if (ldlm_rep->lock_policy_res2) {
4225 mdt_object_unlock(info, info->mti_object, lhc, 1);
4226 RETURN(ELDLM_LOCK_ABORTED);
4230 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4234 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4235 struct mdt_thread_info *info,
4236 struct ldlm_lock **lockp,
4239 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4241 struct ldlm_reply *ldlm_rep;
4242 struct mdt_body *reqbody;
4243 struct mdt_body *repbody;
4247 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4250 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4253 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4254 repbody->mbo_eadatasize = 0;
4255 repbody->mbo_aclsize = 0;
4259 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4262 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4266 CERROR("%s: unsupported intent %#x\n",
4267 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4268 GOTO(out_shrink, rc = -EINVAL);
4271 rc = mdt_init_ucred(info, reqbody);
4273 GOTO(out_shrink, rc);
4275 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4276 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4278 /* Get lock from request for possible resent case. */
4279 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4281 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4282 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4284 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4285 ldlm_rep->lock_policy_res2 = 0;
4286 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4287 ldlm_rep->lock_policy_res2) {
4288 lhc->mlh_reg_lh.cookie = 0ull;
4289 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4292 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4295 mdt_exit_ucred(info);
4297 mdt_client_compatibility(info);
4298 rc2 = mdt_fix_reply(info);
4304 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4305 struct mdt_thread_info *info,
4306 struct ldlm_lock **lockp,
4309 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4310 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4311 struct layout_intent *intent;
4312 struct ldlm_reply *ldlm_rep;
4313 struct lu_fid *fid = &info->mti_tmp_fid2;
4314 struct mdt_object *obj = NULL;
4315 int layout_size = 0;
4316 struct lu_buf *buf = &layout.mlc_buf;
4321 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4323 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4327 CDEBUG(D_INFO, DFID "got layout change request from client: "
4328 "opc:%u flags:%#x extent "DEXT"\n",
4329 PFID(fid), intent->li_opc, intent->li_flags,
4330 PEXT(&intent->li_extent));
4332 switch (intent->li_opc) {
4333 case LAYOUT_INTENT_TRUNC:
4334 case LAYOUT_INTENT_WRITE:
4335 layout.mlc_opc = MD_LAYOUT_WRITE;
4336 layout.mlc_intent = intent;
4338 case LAYOUT_INTENT_ACCESS:
4340 case LAYOUT_INTENT_READ:
4341 case LAYOUT_INTENT_GLIMPSE:
4342 case LAYOUT_INTENT_RELEASE:
4343 case LAYOUT_INTENT_RESTORE:
4344 CERROR("%s: Unsupported layout intent opc %d\n",
4345 mdt_obd_name(info->mti_mdt), intent->li_opc);
4348 CERROR("%s: Unknown layout intent opc %d\n",
4349 mdt_obd_name(info->mti_mdt), intent->li_opc);
4353 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4355 RETURN(PTR_ERR(obj));
4357 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4358 /* if layout is going to be changed don't use the current EA
4359 * size but the maximum one. That buffer will be shrinked
4360 * to the actual size in req_capsule_shrink() before reply.
4362 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4363 layout_size = info->mti_mdt->mdt_max_mdsize;
4365 layout_size = mdt_attr_get_eabuf_size(info, obj);
4366 if (layout_size < 0)
4367 GOTO(out, rc = layout_size);
4369 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4370 info->mti_mdt->mdt_max_mdsize = layout_size;
4372 CDEBUG(D_INFO, "%s: layout_size %d\n",
4373 mdt_obd_name(info->mti_mdt), layout_size);
4377 * set reply buffer size, so that ldlm_handle_enqueue0()->
4378 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4380 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4382 rc = req_capsule_server_pack(info->mti_pill);
4386 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4388 GOTO(out, rc = -EPROTO);
4390 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4392 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4393 if (layout.mlc_opc == MD_LAYOUT_NOP)
4396 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4400 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4401 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4407 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4408 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4410 buf->lb_len = req_capsule_get_size(info->mti_pill,
4411 &RMF_EADATA, RCL_CLIENT);
4413 * If it's a replay of layout write intent RPC, the client has
4414 * saved the extended lovea when it get reply then.
4416 if (buf->lb_len > 0)
4417 mdt_fix_lov_magic(info, buf->lb_buf);
4420 /* Get lock from request for possible resent case. */
4421 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4422 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4425 * Instantiate some layout components, if @buf contains lovea, then it's
4426 * a replay of the layout intent write RPC.
4428 rc = mdt_layout_change(info, obj, lhc, &layout);
4429 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4431 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4432 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4433 if (rc == ELDLM_LOCK_REPLACED &&
4434 (*lockp)->l_granted_mode == LCK_EX)
4435 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4440 mdt_object_put(info->mti_env, obj);
4444 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4445 struct mdt_thread_info *info,
4446 struct ldlm_lock **lockp,
4449 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4450 struct ldlm_reply *rep = NULL;
4453 struct ptlrpc_request *req = mdt_info_req(info);
4455 static const struct req_format *intent_fmts[REINT_MAX] = {
4456 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4457 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4462 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4466 /* Get lock from request for possible resent case. */
4467 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4469 rc = mdt_reint_internal(info, lhc, opc);
4471 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4472 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4474 /* Check whether the reply has been packed successfully. */
4475 if (mdt_info_req(info)->rq_repmsg != NULL)
4476 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4481 RETURN(err_serious(-EFAULT));
4484 /* MDC expects this in any case */
4486 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4488 /* the open lock or the lock for cross-ref object should be
4489 * returned to the client */
4490 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4491 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4492 rep->lock_policy_res2 = 0;
4493 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4497 rep->lock_policy_res2 = clear_serious(rc);
4499 if (rep->lock_policy_res2 == -ENOENT &&
4500 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4501 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4502 rep->lock_policy_res2 = 0;
4504 lhc->mlh_reg_lh.cookie = 0ull;
4505 if (rc == -ENOTCONN || rc == -ENODEV ||
4506 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4508 * If it is the disconnect error (ENODEV & ENOCONN), the error
4509 * will be returned by rq_status, and client at ptlrpc layer
4510 * will detect this, then disconnect, reconnect the import
4511 * immediately, instead of impacting the following the rpc.
4516 * For other cases, the error will be returned by intent, and client
4517 * will retrieve the result from intent.
4519 RETURN(ELDLM_LOCK_ABORTED);
4522 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4523 struct mdt_thread_info *info,
4524 struct ldlm_lock **lockp,
4525 u64 flags /* LDLM_FL_* */)
4527 struct req_capsule *pill = info->mti_pill;
4528 struct ptlrpc_request *req = mdt_info_req(info);
4529 const struct req_format *it_format;
4530 int (*it_handler)(enum ldlm_intent_flags,
4531 struct mdt_thread_info *,
4532 struct ldlm_lock **,
4534 enum tgt_handler_flags it_handler_flags = 0;
4535 struct ldlm_reply *rep;
4536 bool check_mdt_object = false;
4542 case IT_OPEN|IT_CREAT:
4544 * OCREAT is not a IS_MUTABLE request since the file may
4545 * already exist. We do the extra check of
4546 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4547 * really need to create the object.
4549 it_format = &RQF_LDLM_INTENT;
4550 it_handler = &mdt_intent_open;
4553 check_mdt_object = true;
4556 it_format = &RQF_LDLM_INTENT_GETATTR;
4557 it_handler = &mdt_intent_getattr;
4558 it_handler_flags = HAS_REPLY;
4561 check_mdt_object = true;
4562 it_format = &RQF_LDLM_INTENT_GETXATTR;
4563 it_handler = &mdt_intent_getxattr;
4564 it_handler_flags = HAS_BODY;
4567 it_format = &RQF_LDLM_INTENT_LAYOUT;
4568 it_handler = &mdt_intent_layout;
4571 it_format = &RQF_LDLM_INTENT;
4572 it_handler = &mdt_intent_glimpse;
4575 it_format = &RQF_LDLM_INTENT;
4576 it_handler = &mdt_intent_brw;
4578 case IT_QUOTA_DQACQ:
4579 case IT_QUOTA_CONN: {
4580 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4583 RETURN(-EOPNOTSUPP);
4585 if (mdt_rdonly(req->rq_export))
4588 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4589 /* pass the request to quota master */
4590 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4591 mdt_info_req(info), lockp,
4596 CERROR("%s: unknown intent code %#x\n",
4597 mdt_obd_name(info->mti_mdt), it_opc);
4601 req_capsule_extend(pill, it_format);
4603 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4607 if (unlikely(info->mti_object == NULL && check_mdt_object))
4610 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4613 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4615 /* execute policy */
4616 rc = (*it_handler)(it_opc, info, lockp, flags);
4618 /* Check whether the reply has been packed successfully. */
4619 if (req->rq_repmsg != NULL) {
4620 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4621 rep->lock_policy_res2 =
4622 ptlrpc_status_hton(rep->lock_policy_res2);
4628 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4629 enum ldlm_intent_flags it_opc)
4631 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4633 /* update stats when IT code is known */
4634 if (srv_stats != NULL)
4635 lprocfs_counter_incr(srv_stats,
4636 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4637 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4640 static int mdt_intent_policy(const struct lu_env *env,
4641 struct ldlm_namespace *ns,
4642 struct ldlm_lock **lockp,
4644 enum ldlm_mode mode,
4645 __u64 flags, void *data)
4647 struct tgt_session_info *tsi;
4648 struct mdt_thread_info *info;
4649 struct ptlrpc_request *req = req_cookie;
4650 struct ldlm_intent *it;
4651 struct req_capsule *pill;
4652 const struct ldlm_lock_desc *ldesc;
4657 LASSERT(req != NULL);
4659 tsi = tgt_ses_info(env);
4661 info = tsi2mdt_info(tsi);
4662 LASSERT(info != NULL);
4663 pill = info->mti_pill;
4664 LASSERT(pill->rc_req == req);
4665 ldesc = &info->mti_dlm_req->lock_desc;
4667 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4668 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4669 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4671 mdt_ptlrpc_stats_update(req, it->opc);
4672 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4676 /* Lock without inodebits makes no sense and will oops
4677 * later in ldlm. Let's check it now to see if we have
4678 * ibits corrupted somewhere in mdt_intent_opc().
4679 * The case for client miss to set ibits has been
4680 * processed by others. */
4681 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4682 ldesc->l_policy_data.l_inodebits.bits != 0));
4684 rc = err_serious(-EFAULT);
4686 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4687 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4688 struct ldlm_reply *rep;
4690 /* No intent was provided but INTENT flag is set along with
4691 * DOM bit, this is considered as GLIMPSE request.
4692 * This logic is common for MDT and OST glimpse
4694 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4695 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4696 /* Check whether the reply has been packed successfully. */
4697 if (req->rq_repmsg != NULL) {
4698 rep = req_capsule_server_get(info->mti_pill,
4700 rep->lock_policy_res2 =
4701 ptlrpc_status_hton(rep->lock_policy_res2);
4704 /* No intent was provided */
4705 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4706 rc = req_capsule_server_pack(pill);
4708 rc = err_serious(rc);
4710 mdt_thread_info_fini(info);
4714 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4716 struct seq_server_site *ss = mdt_seq_site(mdt);
4718 if (ss->ss_node_id == 0)
4721 if (ss->ss_client_seq != NULL) {
4722 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4723 ss->ss_client_seq->lcs_exp = NULL;
4726 if (ss->ss_server_fld != NULL) {
4727 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4728 ss->ss_server_fld->lsf_control_exp = NULL;
4732 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4734 struct seq_server_site *ss = mdt_seq_site(mdt);
4739 if (ss->ss_server_seq != NULL)
4740 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4743 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4745 mdt_seq_fini_cli(mdt);
4746 mdt_deregister_seq_exp(mdt);
4748 return seq_site_fini(env, mdt_seq_site(mdt));
4752 * It will retrieve its FLDB entries from MDT0, and it only happens
4753 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4754 * and it needs to refresh FLDB from the MDT0.
4756 static int mdt_register_lwp_callback(void *data)
4759 struct mdt_device *mdt = data;
4760 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4764 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4766 rc = lu_env_init(&env, LCT_MD_THREAD);
4768 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4772 /* Allocate new sequence now to avoid creating local transaction
4773 * in the normal transaction process */
4774 rc = seq_server_check_and_alloc_super(&env,
4775 mdt_seq_site(mdt)->ss_server_seq);
4780 rc = fld_update_from_controller(&env, fld);
4782 CERROR("%s: cannot update controller: rc = %d\n",
4783 mdt_obd_name(mdt), rc);
4792 static int mdt_register_seq_exp(struct mdt_device *mdt)
4794 struct seq_server_site *ss = mdt_seq_site(mdt);
4795 char *lwp_name = NULL;
4798 if (ss->ss_node_id == 0)
4801 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4802 if (lwp_name == NULL)
4803 GOTO(out_free, rc = -ENOMEM);
4805 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4809 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4814 rc = lustre_register_lwp_item(lwp_name,
4815 &ss->ss_server_fld->lsf_control_exp,
4816 mdt_register_lwp_callback, mdt);
4818 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4819 ss->ss_client_seq->lcs_exp = NULL;
4823 if (lwp_name != NULL)
4824 OBD_FREE(lwp_name, MAX_OBD_NAME);
4830 * Init client sequence manager which is used by local MDS to talk to sequence
4831 * controller on remote node.
4833 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4835 struct seq_server_site *ss = mdt_seq_site(mdt);
4839 /* check if this is adding the first MDC and controller is not yet
4841 OBD_ALLOC_PTR(ss->ss_client_seq);
4842 if (ss->ss_client_seq == NULL)
4845 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4846 if (prefix == NULL) {
4847 OBD_FREE_PTR(ss->ss_client_seq);
4848 ss->ss_client_seq = NULL;
4852 /* Note: seq_client_fini will be called in seq_site_fini */
4853 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4854 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4855 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4857 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4859 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
4862 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4864 struct seq_server_site *ss;
4868 ss = mdt_seq_site(mdt);
4869 /* init sequence controller server(MDT0) */
4870 if (ss->ss_node_id == 0) {
4871 OBD_ALLOC_PTR(ss->ss_control_seq);
4872 if (ss->ss_control_seq == NULL)
4875 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4876 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4879 GOTO(out_seq_fini, rc);
4882 /* Init normal sequence server */
4883 OBD_ALLOC_PTR(ss->ss_server_seq);
4884 if (ss->ss_server_seq == NULL)
4885 GOTO(out_seq_fini, rc = -ENOMEM);
4887 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4888 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4890 GOTO(out_seq_fini, rc);
4892 /* init seq client for seq server to talk to seq controller(MDT0) */
4893 rc = mdt_seq_init_cli(env, mdt);
4895 GOTO(out_seq_fini, rc);
4897 if (ss->ss_node_id != 0)
4898 /* register controller export through lwp */
4899 rc = mdt_register_seq_exp(mdt);
4904 mdt_seq_fini(env, mdt);
4912 static int mdt_fld_fini(const struct lu_env *env,
4913 struct mdt_device *m)
4915 struct seq_server_site *ss = mdt_seq_site(m);
4918 if (ss && ss->ss_server_fld) {
4919 fld_server_fini(env, ss->ss_server_fld);
4920 OBD_FREE_PTR(ss->ss_server_fld);
4921 ss->ss_server_fld = NULL;
4927 static int mdt_fld_init(const struct lu_env *env,
4929 struct mdt_device *m)
4931 struct seq_server_site *ss;
4935 ss = mdt_seq_site(m);
4937 OBD_ALLOC_PTR(ss->ss_server_fld);
4938 if (ss->ss_server_fld == NULL)
4939 RETURN(rc = -ENOMEM);
4941 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4944 OBD_FREE_PTR(ss->ss_server_fld);
4945 ss->ss_server_fld = NULL;
4952 static void mdt_stack_pre_fini(const struct lu_env *env,
4953 struct mdt_device *m, struct lu_device *top)
4955 struct lustre_cfg_bufs *bufs;
4956 struct lustre_cfg *lcfg;
4957 struct mdt_thread_info *info;
4962 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4963 LASSERT(info != NULL);
4965 bufs = &info->mti_u.bufs;
4967 LASSERT(m->mdt_child_exp);
4968 LASSERT(m->mdt_child_exp->exp_obd);
4970 /* process cleanup, pass mdt obd name to get obd umount flags */
4971 /* XXX: this is needed because all layers are referenced by
4972 * objects (some of them are pinned by osd, for example *
4973 * the proper solution should be a model where object used
4974 * by osd only doesn't have mdt/mdd slices -bzzz */
4975 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4976 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4977 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4980 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4982 top->ld_ops->ldo_process_config(env, top, lcfg);
4983 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4987 static void mdt_stack_fini(const struct lu_env *env,
4988 struct mdt_device *m, struct lu_device *top)
4990 struct obd_device *obd = mdt2obd_dev(m);
4991 struct lustre_cfg_bufs *bufs;
4992 struct lustre_cfg *lcfg;
4993 struct mdt_thread_info *info;
4997 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4998 LASSERT(info != NULL);
5000 lu_dev_del_linkage(top->ld_site, top);
5002 lu_site_purge(env, top->ld_site, -1);
5004 bufs = &info->mti_u.bufs;
5005 /* process cleanup, pass mdt obd name to get obd umount flags */
5006 /* another purpose is to let all layers to release their objects */
5007 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5012 lustre_cfg_bufs_set_string(bufs, 1, flags);
5013 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5016 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5019 top->ld_ops->ldo_process_config(env, top, lcfg);
5020 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5022 lu_site_purge(env, top->ld_site, -1);
5024 m->mdt_child = NULL;
5025 m->mdt_bottom = NULL;
5027 obd_disconnect(m->mdt_child_exp);
5028 m->mdt_child_exp = NULL;
5030 obd_disconnect(m->mdt_bottom_exp);
5031 m->mdt_child_exp = NULL;
5034 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5035 const char *next, struct obd_export **exp)
5037 struct obd_connect_data *data = NULL;
5038 struct obd_device *obd;
5042 OBD_ALLOC_PTR(data);
5044 GOTO(out, rc = -ENOMEM);
5046 obd = class_name2obd(next);
5048 CERROR("%s: can't locate next device: %s\n",
5049 mdt_obd_name(m), next);
5050 GOTO(out, rc = -ENOTCONN);
5053 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5054 data->ocd_version = LUSTRE_VERSION_CODE;
5056 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5058 CERROR("%s: cannot connect to next dev %s (%d)\n",
5059 mdt_obd_name(m), next, rc);
5069 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5070 struct lustre_cfg *cfg)
5072 char *dev = lustre_cfg_string(cfg, 0);
5073 int rc, name_size, uuid_size;
5074 char *name, *uuid, *p;
5075 struct lustre_cfg_bufs *bufs;
5076 struct lustre_cfg *lcfg;
5077 struct obd_device *obd;
5078 struct lustre_profile *lprof;
5079 struct lu_site *site;
5082 /* in 1.8 we had the only device in the stack - MDS.
5083 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5084 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5085 * to generate names and setup MDT, MDD. MDT will be using
5086 * generated name to connect to MDD. for MDD the next device
5087 * will be LOD with name taken from so called "profile" which
5088 * is generated by mount_option line
5090 * 1.8 MGS generates config. commands like this:
5091 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5092 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5093 * 2.0 MGS generates config. commands like this:
5094 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5095 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5096 * 3:lustre-MDT0000-mdtlov 4:f
5098 * we generate MDD name from MDT one, just replacing T with D
5100 * after all the preparations, the logical equivalent will be
5101 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5102 * 3:lustre-MDT0000-mdtlov 4:f
5103 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5104 * 3:lustre-MDD0000 4:f
5106 * notice we build the stack from down to top: MDD first, then MDT */
5108 name_size = MAX_OBD_NAME;
5109 uuid_size = MAX_OBD_NAME;
5111 OBD_ALLOC(name, name_size);
5112 OBD_ALLOC(uuid, uuid_size);
5113 if (name == NULL || uuid == NULL)
5114 GOTO(cleanup_mem, rc = -ENOMEM);
5116 OBD_ALLOC_PTR(bufs);
5118 GOTO(cleanup_mem, rc = -ENOMEM);
5121 p = strstr(name, "-MDT");
5123 GOTO(free_bufs, rc = -ENOMEM);
5126 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5128 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5129 if (lprof == NULL || lprof->lp_dt == NULL) {
5130 CERROR("can't find the profile: %s\n",
5131 lustre_cfg_string(cfg, 0));
5132 GOTO(free_bufs, rc = -EINVAL);
5135 lustre_cfg_bufs_reset(bufs, name);
5136 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5137 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5138 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5140 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5142 GOTO(put_profile, rc = -ENOMEM);
5143 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5145 rc = class_attach(lcfg);
5147 GOTO(lcfg_cleanup, rc);
5149 obd = class_name2obd(name);
5151 CERROR("Can not find obd %s (%s in config)\n",
5152 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5153 GOTO(lcfg_cleanup, rc = -EINVAL);
5156 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5158 lustre_cfg_bufs_reset(bufs, name);
5159 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5160 lustre_cfg_bufs_set_string(bufs, 2, dev);
5161 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5163 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5165 GOTO(class_detach, rc = -ENOMEM);
5166 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5168 rc = class_setup(obd, lcfg);
5170 GOTO(class_detach, rc);
5172 /* connect to MDD we just setup */
5173 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5175 GOTO(class_detach, rc);
5177 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5179 LASSERT(mdt_lu_site(mdt) == NULL);
5180 mdt->mdt_lu_dev.ld_site = site;
5181 site->ls_top_dev = &mdt->mdt_lu_dev;
5182 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5184 /* now connect to bottom OSD */
5185 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5186 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5188 GOTO(class_detach, rc);
5190 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5192 rc = lu_env_refill((struct lu_env *)env);
5194 CERROR("Failure to refill session: '%d'\n", rc);
5196 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5201 class_detach(obd, lcfg);
5203 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5205 class_put_profile(lprof);
5210 OBD_FREE(name, name_size);
5212 OBD_FREE(uuid, uuid_size);
5216 /* setup quota master target on MDT0 */
5217 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5218 struct lustre_cfg *cfg)
5220 struct obd_device *obd;
5221 char *dev = lustre_cfg_string(cfg, 0);
5222 char *qmtname, *uuid, *p;
5223 struct lustre_cfg_bufs *bufs;
5224 struct lustre_cfg *lcfg;
5225 struct lustre_profile *lprof;
5226 struct obd_connect_data *data;
5230 LASSERT(mdt->mdt_qmt_exp == NULL);
5231 LASSERT(mdt->mdt_qmt_dev == NULL);
5233 /* quota master is on MDT0 only for now */
5234 if (mdt->mdt_seq_site.ss_node_id != 0)
5237 /* MGS generates config commands which look as follows:
5238 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5239 * 3:lustre-MDT0000-mdtlov 4:f
5241 * We generate the QMT name from the MDT one, just replacing MD with QM
5242 * after all the preparations, the logical equivalent will be:
5243 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5244 * 3:lustre-MDT0000-osd 4:f */
5245 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5246 OBD_ALLOC(uuid, UUID_MAX);
5247 OBD_ALLOC_PTR(bufs);
5248 OBD_ALLOC_PTR(data);
5249 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5250 GOTO(cleanup_mem, rc = -ENOMEM);
5252 strcpy(qmtname, dev);
5253 p = strstr(qmtname, "-MDT");
5255 GOTO(cleanup_mem, rc = -ENOMEM);
5256 /* replace MD with QM */
5260 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5262 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5263 if (lprof == NULL || lprof->lp_dt == NULL) {
5264 CERROR("can't find profile for %s\n",
5265 lustre_cfg_string(cfg, 0));
5266 GOTO(cleanup_mem, rc = -EINVAL);
5269 lustre_cfg_bufs_reset(bufs, qmtname);
5270 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5271 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5272 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5274 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5276 GOTO(put_profile, rc = -ENOMEM);
5277 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5279 rc = class_attach(lcfg);
5281 GOTO(lcfg_cleanup, rc);
5283 obd = class_name2obd(qmtname);
5285 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5286 lustre_cfg_string(cfg, 0));
5287 GOTO(lcfg_cleanup, rc = -EINVAL);
5290 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5292 lustre_cfg_bufs_reset(bufs, qmtname);
5293 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5294 lustre_cfg_bufs_set_string(bufs, 2, dev);
5296 /* for quota, the next device should be the OSD device */
5297 lustre_cfg_bufs_set_string(bufs, 3,
5298 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5300 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5302 GOTO(class_detach, rc = -ENOMEM);
5303 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5305 rc = class_setup(obd, lcfg);
5307 GOTO(class_detach, rc);
5309 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5311 /* configure local quota objects */
5312 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5315 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5319 GOTO(class_cleanup, rc);
5321 /* connect to quota master target */
5322 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5323 data->ocd_version = LUSTRE_VERSION_CODE;
5324 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5327 CERROR("cannot connect to quota master device %s (%d)\n",
5329 GOTO(class_cleanup, rc);
5335 class_manual_cleanup(obd);
5336 mdt->mdt_qmt_dev = NULL;
5337 GOTO(lcfg_cleanup, rc);
5341 class_detach(obd, lcfg);
5343 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5345 class_put_profile(lprof);
5350 OBD_FREE(qmtname, MAX_OBD_NAME);
5352 OBD_FREE(uuid, UUID_MAX);
5358 /* Shutdown quota master target associated with mdt */
5359 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5363 if (mdt->mdt_qmt_exp == NULL)
5365 LASSERT(mdt->mdt_qmt_dev != NULL);
5367 /* the qmt automatically shuts down when the mdt disconnects */
5368 obd_disconnect(mdt->mdt_qmt_exp);
5369 mdt->mdt_qmt_exp = NULL;
5370 mdt->mdt_qmt_dev = NULL;
5374 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5375 * for now. This will be removed along with converting rest of MDT code
5376 * to use tgt_session_info */
5377 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5379 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5382 if (unlikely(info->mti_object == NULL))
5385 rc = mdt_getxattr(info);
5387 mdt_thread_info_fini(info);
5391 static int mdt_llog_open(struct tgt_session_info *tsi)
5395 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5396 RETURN(err_serious(-EACCES));
5398 RETURN(tgt_llog_open(tsi));
5401 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5402 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5403 #define OST_BRW_READ OST_READ
5404 #define OST_BRW_WRITE OST_WRITE
5406 static struct tgt_handler mdt_tgt_handlers[] = {
5407 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5408 0, MDS_CONNECT, mdt_tgt_connect,
5409 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5410 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5411 0, MDS_DISCONNECT, tgt_disconnect,
5412 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5413 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5414 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5415 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5416 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5417 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5418 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5419 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5421 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5422 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5423 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5424 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5425 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5426 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5427 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5428 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5430 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5431 mdt_hsm_ct_register),
5432 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5433 mdt_hsm_ct_unregister),
5434 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5436 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5438 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5439 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5441 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5444 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5447 static struct tgt_handler mdt_io_ops[] = {
5448 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5450 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5452 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5453 OST_PUNCH, mdt_punch_hdl,
5455 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5456 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5459 static struct tgt_handler mdt_sec_ctx_ops[] = {
5460 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5461 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5462 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5465 static struct tgt_handler mdt_quota_ops[] = {
5466 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5469 static struct tgt_handler mdt_llog_handlers[] = {
5470 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5471 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5472 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5473 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5476 static struct tgt_opc_slice mdt_common_slice[] = {
5478 .tos_opc_start = MDS_FIRST_OPC,
5479 .tos_opc_end = MDS_LAST_OPC,
5480 .tos_hs = mdt_tgt_handlers
5483 .tos_opc_start = OBD_FIRST_OPC,
5484 .tos_opc_end = OBD_LAST_OPC,
5485 .tos_hs = tgt_obd_handlers
5488 .tos_opc_start = LDLM_FIRST_OPC,
5489 .tos_opc_end = LDLM_LAST_OPC,
5490 .tos_hs = tgt_dlm_handlers
5493 .tos_opc_start = SEC_FIRST_OPC,
5494 .tos_opc_end = SEC_LAST_OPC,
5495 .tos_hs = mdt_sec_ctx_ops
5498 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5499 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5500 .tos_hs = tgt_out_handlers
5503 .tos_opc_start = FLD_FIRST_OPC,
5504 .tos_opc_end = FLD_LAST_OPC,
5505 .tos_hs = fld_handlers
5508 .tos_opc_start = SEQ_FIRST_OPC,
5509 .tos_opc_end = SEQ_LAST_OPC,
5510 .tos_hs = seq_handlers
5513 .tos_opc_start = QUOTA_DQACQ,
5514 .tos_opc_end = QUOTA_LAST_OPC,
5515 .tos_hs = mdt_quota_ops
5518 .tos_opc_start = LLOG_FIRST_OPC,
5519 .tos_opc_end = LLOG_LAST_OPC,
5520 .tos_hs = mdt_llog_handlers
5523 .tos_opc_start = LFSCK_FIRST_OPC,
5524 .tos_opc_end = LFSCK_LAST_OPC,
5525 .tos_hs = tgt_lfsck_handlers
5528 .tos_opc_start = OST_FIRST_OPC,
5529 .tos_opc_end = OST_LAST_OPC,
5530 .tos_hs = mdt_io_ops
5537 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5539 struct md_device *next = m->mdt_child;
5540 struct lu_device *d = &m->mdt_lu_dev;
5541 struct obd_device *obd = mdt2obd_dev(m);
5542 struct lfsck_stop stop;
5545 stop.ls_status = LS_PAUSED;
5547 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5549 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5551 mdt_restriper_stop(m);
5552 ping_evictor_stop();
5554 /* Remove the HSM /proc entry so the coordinator cannot be
5555 * restarted by a user while it's shutting down.
5557 mdt_hsm_cdt_stop(m);
5559 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5560 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5562 if (m->mdt_namespace != NULL)
5563 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5564 d->ld_obd->obd_force);
5566 obd_exports_barrier(obd);
5567 obd_zombie_barrier();
5569 mdt_quota_fini(env, m);
5571 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5573 /* Calling the cleanup functions in the same order as in the mdt_init0
5576 mdt_tunables_fini(m);
5578 target_recovery_fini(obd);
5579 upcall_cache_cleanup(m->mdt_identity_cache);
5580 m->mdt_identity_cache = NULL;
5582 mdt_fs_cleanup(env, m);
5584 tgt_fini(env, &m->mdt_lut);
5586 mdt_hsm_cdt_fini(m);
5588 if (m->mdt_los != NULL) {
5589 local_oid_storage_fini(env, m->mdt_los);
5593 if (m->mdt_namespace != NULL) {
5594 ldlm_namespace_free_post(m->mdt_namespace);
5595 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5598 if (m->mdt_md_root != NULL) {
5599 mdt_object_put(env, m->mdt_md_root);
5600 m->mdt_md_root = NULL;
5603 mdt_seq_fini(env, m);
5605 mdt_fld_fini(env, m);
5610 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5612 LASSERT(atomic_read(&d->ld_ref) == 0);
5614 server_put_mount(mdt_obd_name(m), true);
5619 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5621 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5622 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5624 const struct dt_device_param *dt_conf;
5625 struct mdt_thread_info *info;
5626 struct obd_device *obd;
5627 const char *dev = lustre_cfg_string(cfg, 0);
5628 const char *num = lustre_cfg_string(cfg, 2);
5629 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5630 struct lustre_mount_info *lmi = NULL;
5631 struct lustre_sb_info *lsi;
5633 struct seq_server_site *ss_site;
5634 const char *identity_upcall = "NONE";
5635 struct md_device *next;
5642 lu_device_init(&m->mdt_lu_dev, ldt);
5644 * Environment (env) might be missing mdt_thread_key values at that
5645 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5648 * Usually device allocation path doesn't use module key values, but
5649 * mdt has to do a lot of work here, so allocate key value.
5651 rc = lu_env_refill((struct lu_env *)env);
5655 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5656 LASSERT(info != NULL);
5658 obd = class_name2obd(dev);
5659 LASSERT(obd != NULL);
5661 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5662 m->mdt_opts.mo_evict_tgt_nids = 1;
5663 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5665 lmi = server_get_mount(dev);
5667 CERROR("Cannot get mount info for %s!\n", dev);
5670 lsi = s2lsi(lmi->lmi_sb);
5671 LASSERT(lsi->lsi_lmd);
5672 /* CMD is supported only in IAM mode */
5674 rc = kstrtol(num, 10, &node_id);
5678 obd->u.obt.obt_magic = OBT_MAGIC;
5679 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5680 m->mdt_skip_lfsck = 1;
5683 /* Just try to get a DoM lock by default. Otherwise, having a group
5684 * lock granted, it may get blocked for a long time. */
5685 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5686 /* DoM files are read at open and data is packed in the reply */
5687 m->mdt_opts.mo_dom_read_open = 1;
5689 m->mdt_squash.rsi_uid = 0;
5690 m->mdt_squash.rsi_gid = 0;
5691 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5692 spin_lock_init(&m->mdt_squash.rsi_lock);
5693 spin_lock_init(&m->mdt_lock);
5694 m->mdt_enable_remote_dir = 1;
5695 m->mdt_enable_striped_dir = 1;
5696 m->mdt_enable_dir_migration = 1;
5697 m->mdt_enable_dir_restripe = 0;
5698 m->mdt_enable_dir_auto_split = 0;
5699 m->mdt_enable_remote_dir_gid = 0;
5700 m->mdt_enable_chprojid_gid = 0;
5701 m->mdt_enable_remote_rename = 1;
5702 m->mdt_dir_restripe_nsonly = 1;
5704 atomic_set(&m->mdt_mds_mds_conns, 0);
5705 atomic_set(&m->mdt_async_commit_count, 0);
5707 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5708 m->mdt_lu_dev.ld_obd = obd;
5709 /* Set this lu_device to obd for error handling purposes. */
5710 obd->obd_lu_dev = &m->mdt_lu_dev;
5712 /* init the stack */
5713 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5715 CERROR("%s: Can't init device stack, rc %d\n",
5716 mdt_obd_name(m), rc);
5721 ss_site = mdt_seq_site(m);
5722 s->ld_seq_site = ss_site;
5725 /* set server index */
5726 ss_site->ss_node_id = node_id;
5728 /* failover is the default
5729 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5730 * assumed whose ss_node_id == 0 XXX
5732 obd->obd_replayable = 1;
5733 /* No connection accepted until configurations will finish */
5734 obd->obd_no_conn = 1;
5736 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5737 char *str = lustre_cfg_string(cfg, 4);
5738 if (strchr(str, 'n')) {
5739 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5740 obd->obd_replayable = 0;
5744 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5746 GOTO(err_fini_stack, rc);
5748 rc = mdt_seq_init(env, m);
5750 GOTO(err_fini_fld, rc);
5752 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5753 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5754 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5755 LDLM_NAMESPACE_SERVER,
5756 LDLM_NAMESPACE_GREEDY,
5758 if (m->mdt_namespace == NULL)
5759 GOTO(err_fini_seq, rc = -ENOMEM);
5761 m->mdt_namespace->ns_lvbp = m;
5762 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5764 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5765 /* set obd_namespace for compatibility with old code */
5766 obd->obd_namespace = m->mdt_namespace;
5768 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5769 OBD_FAIL_MDS_ALL_REQUEST_NET,
5770 OBD_FAIL_MDS_ALL_REPLY_NET);
5772 GOTO(err_free_ns, rc);
5774 /* Amount of available space excluded from granting and reserved
5775 * for metadata. It is a percentage of the total MDT size. */
5776 tgd->tgd_reserved_pcnt = 10;
5778 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5779 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5781 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5783 rc = mdt_fs_setup(env, m, obd, lsi);
5787 fid.f_seq = FID_SEQ_LOCAL_NAME;
5790 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
5792 GOTO(err_fs_cleanup, rc);
5794 rc = mdt_hsm_cdt_init(m);
5796 CERROR("%s: error initializing coordinator, rc %d\n",
5797 mdt_obd_name(m), rc);
5798 GOTO(err_los_fini, rc);
5801 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5803 next = m->mdt_child;
5804 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5806 mntopts = dt_conf->ddp_mntopts;
5808 if (mntopts & MNTOPT_USERXATTR)
5809 m->mdt_opts.mo_user_xattr = 1;
5811 m->mdt_opts.mo_user_xattr = 0;
5813 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5815 if (mntopts & MNTOPT_ACL)
5816 m->mdt_opts.mo_acl = 1;
5818 m->mdt_opts.mo_acl = 0;
5820 /* XXX: to support suppgid for ACL, we enable identity_upcall
5821 * by default, otherwise, maybe got unexpected -EACCESS. */
5822 if (m->mdt_opts.mo_acl)
5823 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5825 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5827 &mdt_identity_upcall_cache_ops);
5828 if (IS_ERR(m->mdt_identity_cache)) {
5829 rc = PTR_ERR(m->mdt_identity_cache);
5830 m->mdt_identity_cache = NULL;
5831 GOTO(err_free_hsm, rc);
5834 rc = mdt_tunables_init(m, dev);
5836 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5837 GOTO(err_recovery, rc);
5840 rc = mdt_quota_init(env, m, cfg);
5842 GOTO(err_procfs, rc);
5844 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5845 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5846 "mdt_ldlm_client", m->mdt_ldlm_client);
5848 ping_evictor_start();
5850 /* recovery will be started upon mdt_prepare()
5851 * when the whole stack is complete and ready
5852 * to serve the requests */
5854 /* Reduce the initial timeout on an MDS because it doesn't need such
5855 * a long timeout as an OST does. Adaptive timeouts will adjust this
5856 * value appropriately. */
5857 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5858 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5860 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
5861 m->mdt_lut.lut_local_recovery = 1;
5863 rc = mdt_restriper_start(m);
5865 GOTO(err_ping_evictor, rc);
5870 ping_evictor_stop();
5872 mdt_tunables_fini(m);
5874 upcall_cache_cleanup(m->mdt_identity_cache);
5875 m->mdt_identity_cache = NULL;
5877 mdt_hsm_cdt_fini(m);
5879 local_oid_storage_fini(env, m->mdt_los);
5882 mdt_fs_cleanup(env, m);
5884 /* keep recoverable clients */
5886 target_recovery_fini(obd);
5887 obd_exports_barrier(obd);
5888 obd_zombie_barrier();
5889 tgt_fini(env, &m->mdt_lut);
5891 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5892 obd->obd_namespace = m->mdt_namespace = NULL;
5894 mdt_seq_fini(env, m);
5896 mdt_fld_fini(env, m);
5898 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5901 server_put_mount(dev, true);
5905 /* For interoperability, the left element is old parameter, the right one
5906 * is the new version of the parameter, if some parameter is deprecated,
5907 * the new version should be set as NULL. */
5908 static struct cfg_interop_param mdt_interop_param[] = {
5909 { "mdt.group_upcall", NULL },
5910 { "mdt.quota_type", NULL },
5911 { "mdd.quota_type", NULL },
5912 { "mdt.som", NULL },
5913 { "mdt.rootsquash", "mdt.root_squash" },
5914 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5918 /* used by MGS to process specific configurations */
5919 static int mdt_process_config(const struct lu_env *env,
5920 struct lu_device *d, struct lustre_cfg *cfg)
5922 struct mdt_device *m = mdt_dev(d);
5923 struct md_device *md_next = m->mdt_child;
5924 struct lu_device *next = md2lu_dev(md_next);
5928 switch (cfg->lcfg_command) {
5930 struct obd_device *obd = d->ld_obd;
5931 /* For interoperability */
5932 struct cfg_interop_param *ptr = NULL;
5933 struct lustre_cfg *old_cfg = NULL;
5937 param = lustre_cfg_string(cfg, 1);
5938 if (param == NULL) {
5939 CERROR("param is empty\n");
5944 ptr = class_find_old_param(param, mdt_interop_param);
5946 if (ptr->new_param == NULL) {
5948 CWARN("For interoperability, skip this %s."
5949 " It is obsolete.\n", ptr->old_param);
5953 CWARN("Found old param %s, changed it to %s.\n",
5954 ptr->old_param, ptr->new_param);
5957 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5964 count = class_modify_config(cfg, PARAM_MDT,
5965 &obd->obd_kset.kobj);
5967 struct coordinator *cdt = &m->mdt_coordinator;
5969 /* is it an HSM var ? */
5970 count = class_modify_config(cfg, PARAM_HSM,
5971 &cdt->cdt_hsm_kobj);
5973 /* we don't understand; pass it on */
5974 rc = next->ld_ops->ldo_process_config(env, next,
5977 rc = count > 0 ? 0 : count;
5979 rc = count > 0 ? 0 : count;
5983 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5984 cfg->lcfg_buflens));
5988 /* others are passed further */
5989 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5995 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5996 const struct lu_object_header *hdr,
5997 struct lu_device *d)
5999 struct mdt_object *mo;
6003 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6005 struct lu_object *o;
6006 struct lu_object_header *h;
6009 h = &mo->mot_header;
6010 lu_object_header_init(h);
6011 lu_object_init(o, h, d);
6012 lu_object_add_top(h, o);
6013 o->lo_ops = &mdt_obj_ops;
6014 spin_lock_init(&mo->mot_write_lock);
6015 mutex_init(&mo->mot_som_mutex);
6016 mutex_init(&mo->mot_lov_mutex);
6017 init_rwsem(&mo->mot_dom_sem);
6018 init_rwsem(&mo->mot_open_sem);
6019 atomic_set(&mo->mot_open_count, 0);
6020 mo->mot_restripe_offset = 0;
6021 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6027 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6028 const struct lu_object_conf *unused)
6030 struct mdt_device *d = mdt_dev(o->lo_dev);
6031 struct lu_device *under;
6032 struct lu_object *below;
6036 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6037 PFID(lu_object_fid(o)));
6039 under = &d->mdt_child->md_lu_dev;
6040 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6041 if (below != NULL) {
6042 lu_object_add(o, below);
6049 static void mdt_object_free_rcu(struct rcu_head *head)
6051 struct mdt_object *mo = container_of(head, struct mdt_object,
6052 mot_header.loh_rcu);
6054 kmem_cache_free(mdt_object_kmem, mo);
6057 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6059 struct mdt_object *mo = mdt_obj(o);
6060 struct lu_object_header *h;
6064 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6065 PFID(lu_object_fid(o)));
6067 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6068 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6071 lu_object_header_fini(h);
6072 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6073 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6078 static int mdt_object_print(const struct lu_env *env, void *cookie,
6079 lu_printer_t p, const struct lu_object *o)
6081 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6083 return (*p)(env, cookie,
6084 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6085 mdto, mdto->mot_lov_created ? "lov_created" : "",
6086 mdto->mot_cache_attr ? "cache_attr" : "",
6087 mdto->mot_write_count);
6090 static int mdt_prepare(const struct lu_env *env,
6091 struct lu_device *pdev,
6092 struct lu_device *cdev)
6094 struct mdt_device *mdt = mdt_dev(cdev);
6095 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6096 struct obd_device *obd = cdev->ld_obd;
6103 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6107 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6111 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6115 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6116 /* The LFSCK instance is registered just now, so it must be there when
6117 * register the namespace to such instance. */
6118 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6120 if (mdt->mdt_seq_site.ss_node_id == 0) {
6121 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6122 &mdt->mdt_md_root_fid);
6127 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6129 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6130 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6131 LASSERT(obd->obd_no_conn);
6132 spin_lock(&obd->obd_dev_lock);
6133 obd->obd_no_conn = 0;
6134 spin_unlock(&obd->obd_dev_lock);
6136 if (obd->obd_recovering == 0)
6137 mdt_postrecov(env, mdt);
6142 const struct lu_device_operations mdt_lu_ops = {
6143 .ldo_object_alloc = mdt_object_alloc,
6144 .ldo_process_config = mdt_process_config,
6145 .ldo_prepare = mdt_prepare,
6148 static const struct lu_object_operations mdt_obj_ops = {
6149 .loo_object_init = mdt_object_init,
6150 .loo_object_free = mdt_object_free,
6151 .loo_object_print = mdt_object_print
6154 static int mdt_obd_set_info_async(const struct lu_env *env,
6155 struct obd_export *exp,
6156 __u32 keylen, void *key,
6157 __u32 vallen, void *val,
6158 struct ptlrpc_request_set *set)
6164 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6165 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6172 static inline void mdt_enable_slc(struct mdt_device *mdt)
6174 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6175 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6178 static inline void mdt_disable_slc(struct mdt_device *mdt)
6180 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6181 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6185 * Match client and server connection feature flags.
6187 * Compute the compatibility flags for a connection request based on
6188 * features mutually supported by client and server.
6190 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6191 * must not be updated here, otherwise a partially initialized value may
6192 * be exposed. After the connection request is successfully processed,
6193 * the top-level MDT connect request handler atomically updates the export
6194 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6195 * reply. \see mdt_connect().
6197 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6198 * full struct obd_connect_data. So care must be taken when accessing fields
6199 * that are not present in struct obd_connect_data_v1. See LU-16.
6201 * \param exp the obd_export associated with this client/target pair
6202 * \param mdt the target device for the connection
6203 * \param data stores data for this connect request
6206 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6207 * \retval -EBADE client and server feature requirements are incompatible
6209 static int mdt_connect_internal(const struct lu_env *env,
6210 struct obd_export *exp,
6211 struct mdt_device *mdt,
6212 struct obd_connect_data *data, bool reconnect)
6214 const char *obd_name = mdt_obd_name(mdt);
6215 LASSERT(data != NULL);
6217 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6219 if (mdt->mdt_bottom->dd_rdonly &&
6220 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6221 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6224 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6225 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6227 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6229 if (!mdt->mdt_opts.mo_acl)
6230 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6232 if (!mdt->mdt_opts.mo_user_xattr)
6233 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6235 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6236 data->ocd_brw_size = min(data->ocd_brw_size,
6238 if (data->ocd_brw_size == 0) {
6239 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6240 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6241 "ocd_brw_size unexpectedly zero, network data "
6242 "corruption? Refusing to connect this client\n",
6243 obd_name, exp->exp_client_uuid.uuid,
6244 exp, data->ocd_connect_flags, data->ocd_version,
6245 data->ocd_grant, data->ocd_index);
6250 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6251 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6253 /* client is reporting its page size, for future use */
6254 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6255 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6256 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6257 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6258 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6259 /* ocd_grant_tax_kb is in 1K byte blocks */
6260 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6261 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6264 /* Save connect_data we have so far because tgt_grant_connect()
6265 * uses it to calculate grant, and we want to save the client
6266 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6267 exp->exp_connect_data = *data;
6268 if (OCD_HAS_FLAG(data, GRANT))
6269 tgt_grant_connect(env, exp, data, !reconnect);
6271 if (OCD_HAS_FLAG(data, MAXBYTES))
6272 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6274 /* NB: Disregard the rule against updating
6275 * exp_connect_data.ocd_connect_flags in this case, since
6276 * tgt_client_new() needs to know if this is a lightweight
6277 * connection, and it is safe to expose this flag before
6278 * connection processing completes. */
6279 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6280 spin_lock(&exp->exp_lock);
6281 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6282 spin_unlock(&exp->exp_lock);
6285 data->ocd_version = LUSTRE_VERSION_CODE;
6287 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6288 CWARN("%s: MDS requires FID support, but client not\n",
6293 if (OCD_HAS_FLAG(data, PINGLESS)) {
6294 if (ptlrpc_pinger_suppress_pings()) {
6295 spin_lock(&exp->exp_obd->obd_dev_lock);
6296 list_del_init(&exp->exp_obd_chain_timed);
6297 spin_unlock(&exp->exp_obd->obd_dev_lock);
6299 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6303 data->ocd_max_easize = mdt->mdt_max_ea_size;
6305 /* NB: Disregard the rule against updating
6306 * exp_connect_data.ocd_connect_flags in this case, since
6307 * tgt_client_new() needs to know if this is client supports
6308 * multiple modify RPCs, and it is safe to expose this flag before
6309 * connection processing completes. */
6310 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6311 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6312 spin_lock(&exp->exp_lock);
6313 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6314 spin_unlock(&exp->exp_lock);
6317 if (OCD_HAS_FLAG(data, CKSUM)) {
6318 __u32 cksum_types = data->ocd_cksum_types;
6320 /* The client set in ocd_cksum_types the checksum types it
6321 * supports. We have to mask off the algorithms that we don't
6323 data->ocd_cksum_types &=
6324 obd_cksum_types_supported_server(obd_name);
6326 if (unlikely(data->ocd_cksum_types == 0)) {
6327 CERROR("%s: Connect with checksum support but no "
6328 "ocd_cksum_types is set\n",
6329 exp->exp_obd->obd_name);
6333 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6334 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6335 cksum_types, data->ocd_cksum_types);
6337 /* This client does not support OBD_CONNECT_CKSUM
6338 * fall back to CRC32 */
6339 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6340 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6341 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6344 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6345 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6346 atomic_inc(&mdt->mdt_mds_mds_conns);
6347 mdt_enable_slc(mdt);
6350 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6351 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6356 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6357 struct mdt_thread_info *info,
6358 struct mdt_file_data *mfd)
6360 struct lu_context ses;
6364 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6369 lu_context_enter(&ses);
6371 mdt_ucred(info)->uc_valid = UCRED_OLD;
6372 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6374 lu_context_exit(&ses);
6375 lu_context_fini(&ses);
6381 static int mdt_export_cleanup(struct obd_export *exp)
6383 LIST_HEAD(closing_list);
6384 struct mdt_export_data *med = &exp->exp_mdt_data;
6385 struct obd_device *obd = exp->exp_obd;
6386 struct mdt_device *mdt;
6387 struct mdt_thread_info *info;
6389 struct mdt_file_data *mfd, *n;
6393 spin_lock(&med->med_open_lock);
6394 while (!list_empty(&med->med_open_head)) {
6395 struct list_head *tmp = med->med_open_head.next;
6396 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6398 /* Remove mfd handle so it can't be found again.
6399 * We are consuming the mfd_list reference here. */
6400 class_handle_unhash(&mfd->mfd_open_handle);
6401 list_move_tail(&mfd->mfd_list, &closing_list);
6403 spin_unlock(&med->med_open_lock);
6404 mdt = mdt_dev(obd->obd_lu_dev);
6405 LASSERT(mdt != NULL);
6407 rc = lu_env_init(&env, LCT_MD_THREAD);
6411 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6412 LASSERT(info != NULL);
6413 memset(info, 0, sizeof *info);
6414 info->mti_env = &env;
6415 info->mti_mdt = mdt;
6416 info->mti_exp = exp;
6418 if (!list_empty(&closing_list)) {
6419 struct md_attr *ma = &info->mti_attr;
6421 /* Close any open files (which may also cause orphan
6423 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6424 list_del_init(&mfd->mfd_list);
6425 ma->ma_need = ma->ma_valid = 0;
6427 /* This file is being closed due to an eviction, it
6428 * could have been modified and now dirty regarding to
6429 * HSM archive, check this!
6430 * The logic here is to mark a file dirty if there's a
6431 * chance it was dirtied before the client was evicted,
6432 * so that we don't have to wait for a release attempt
6433 * before finding out the file was actually dirty and
6434 * fail the release. Aggressively marking it dirty here
6435 * will cause the policy engine to attempt to
6436 * re-archive it; when rearchiving, we can compare the
6437 * current version to the HSM data_version and make the
6438 * archive request into a noop if it's not actually
6441 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6442 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6444 /* Don't unlink orphan on failover umount, LU-184 */
6445 if (exp->exp_flags & OBD_OPT_FAILOVER) {
6446 ma->ma_valid = MA_FLAGS;
6447 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6449 ma->ma_valid |= MA_FORCE_LOG;
6450 mdt_mfd_close(info, mfd);
6453 info->mti_mdt = NULL;
6454 /* cleanup client slot early */
6455 /* Do not erase record for recoverable client. */
6456 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6457 tgt_client_del(&env, exp);
6463 static int mdt_obd_disconnect(struct obd_export *exp)
6470 class_export_get(exp);
6472 if (!(exp->exp_flags & OBD_OPT_FORCE))
6473 tgt_grant_sanity_check(exp->exp_obd, __func__);
6475 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6476 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6477 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6479 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6480 mdt_disable_slc(mdt);
6483 rc = server_disconnect_export(exp);
6485 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6487 tgt_grant_discard(exp);
6489 rc = mdt_export_cleanup(exp);
6490 nodemap_del_member(exp);
6491 class_export_put(exp);
6495 /* mds_connect copy */
6496 static int mdt_obd_connect(const struct lu_env *env,
6497 struct obd_export **exp, struct obd_device *obd,
6498 struct obd_uuid *cluuid,
6499 struct obd_connect_data *data,
6502 struct obd_export *lexp;
6503 struct lustre_handle conn = { 0 };
6504 struct mdt_device *mdt;
6506 lnet_nid_t *client_nid = localdata;
6509 LASSERT(env != NULL);
6510 LASSERT(data != NULL);
6512 if (!exp || !obd || !cluuid)
6515 mdt = mdt_dev(obd->obd_lu_dev);
6518 * first, check whether the stack is ready to handle requests
6519 * XXX: probably not very appropriate method is used now
6520 * at some point we should find a better one
6522 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6523 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6524 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6525 rc = obd_get_info(env, mdt->mdt_child_exp,
6526 sizeof(KEY_OSP_CONNECTED),
6527 KEY_OSP_CONNECTED, NULL, NULL);
6530 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6533 rc = class_connect(&conn, obd, cluuid);
6537 lexp = class_conn2export(&conn);
6538 LASSERT(lexp != NULL);
6540 rc = nodemap_add_member(*client_nid, lexp);
6541 if (rc != 0 && rc != -EEXIST)
6544 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6546 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6549 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6550 rc = tgt_client_new(env, lexp);
6552 mdt_export_stats_init(obd, lexp, localdata);
6556 class_disconnect(lexp);
6557 nodemap_del_member(lexp);
6561 /* Because we do not want this export to be evicted by pinger,
6562 * let's not add this export to the timed chain list. */
6563 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6564 spin_lock(&lexp->exp_obd->obd_dev_lock);
6565 list_del_init(&lexp->exp_obd_chain_timed);
6566 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6573 static int mdt_obd_reconnect(const struct lu_env *env,
6574 struct obd_export *exp, struct obd_device *obd,
6575 struct obd_uuid *cluuid,
6576 struct obd_connect_data *data,
6579 lnet_nid_t *client_nid = localdata;
6583 if (exp == NULL || obd == NULL || cluuid == NULL)
6586 rc = nodemap_add_member(*client_nid, exp);
6587 if (rc != 0 && rc != -EEXIST)
6590 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6593 mdt_export_stats_init(obd, exp, localdata);
6595 nodemap_del_member(exp);
6600 /* FIXME: Can we avoid using these two interfaces? */
6601 static int mdt_init_export(struct obd_export *exp)
6603 struct mdt_export_data *med = &exp->exp_mdt_data;
6607 INIT_LIST_HEAD(&med->med_open_head);
6608 spin_lock_init(&med->med_open_lock);
6609 spin_lock(&exp->exp_lock);
6610 exp->exp_connecting = 1;
6611 spin_unlock(&exp->exp_lock);
6613 OBD_ALLOC(exp->exp_used_slots,
6614 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6615 if (exp->exp_used_slots == NULL)
6618 /* self-export doesn't need client data and ldlm initialization */
6619 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6620 &exp->exp_client_uuid)))
6623 rc = tgt_client_alloc(exp);
6627 rc = ldlm_init_export(exp);
6634 tgt_client_free(exp);
6636 OBD_FREE(exp->exp_used_slots,
6637 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6638 exp->exp_used_slots = NULL;
6640 CERROR("%s: Failed to initialize export: rc = %d\n",
6641 exp->exp_obd->obd_name, rc);
6645 static int mdt_destroy_export(struct obd_export *exp)
6649 target_destroy_export(exp);
6650 if (exp->exp_used_slots)
6651 OBD_FREE(exp->exp_used_slots,
6652 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6654 /* destroy can be called from failed obd_setup, so
6655 * checking uuid is safer than obd_self_export */
6656 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6657 &exp->exp_client_uuid)))
6660 ldlm_destroy_export(exp);
6661 tgt_client_free(exp);
6663 LASSERT(list_empty(&exp->exp_outstanding_replies));
6664 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6667 * discard grants once we're sure no more
6668 * interaction with the client is possible
6670 tgt_grant_discard(exp);
6671 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6672 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6674 if (!(exp->exp_flags & OBD_OPT_FORCE))
6675 tgt_grant_sanity_check(exp->exp_obd, __func__);
6680 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6681 struct linkea_data *ldata)
6685 LASSERT(ldata->ld_buf->lb_buf != NULL);
6687 if (!mdt_object_exists(mdt_obj))
6690 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6691 ldata->ld_buf, XATTR_NAME_LINK);
6692 if (rc == -ERANGE) {
6693 /* Buf was too small, figure out what we need. */
6694 lu_buf_free(ldata->ld_buf);
6695 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6696 ldata->ld_buf, XATTR_NAME_LINK);
6699 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6700 if (ldata->ld_buf->lb_buf == NULL)
6702 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6703 ldata->ld_buf, XATTR_NAME_LINK);
6708 return linkea_init_with_rec(ldata);
6712 * Given an MDT object, try to look up the full path to the object.
6713 * Part of the MDT layer implementation of lfs fid2path.
6715 * \param[in] info Per-thread common data shared by MDT level handlers.
6716 * \param[in] obj Object to do path lookup of
6717 * \param[in,out] fp User-provided struct to store path information
6718 * \param[in] root_fid Root FID of current path should reach
6720 * \retval 0 Lookup successful, path information stored in fp
6721 * \retval -EAGAIN Lookup failed, usually because object is being moved
6722 * \retval negative errno if there was a problem
6724 static int mdt_path_current(struct mdt_thread_info *info,
6725 struct mdt_object *obj,
6726 struct getinfo_fid2path *fp,
6727 struct lu_fid *root_fid)
6729 struct mdt_device *mdt = info->mti_mdt;
6730 struct mdt_object *mdt_obj;
6731 struct link_ea_header *leh;
6732 struct link_ea_entry *lee;
6733 struct lu_name *tmpname = &info->mti_name;
6734 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6735 struct lu_buf *buf = &info->mti_big_buf;
6738 struct linkea_data ldata = { NULL };
6743 /* temp buffer for path element, the buffer will be finally freed
6744 * in mdt_thread_info_fini */
6745 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6746 if (buf->lb_buf == NULL)
6750 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6753 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6755 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6756 struct lu_buf lmv_buf;
6758 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6759 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6760 GOTO(out, rc = -ENOENT);
6762 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6764 mdt_object_get(info->mti_env, mdt_obj);
6766 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6767 if (IS_ERR(mdt_obj))
6768 GOTO(out, rc = PTR_ERR(mdt_obj));
6771 if (!mdt_object_exists(mdt_obj)) {
6772 mdt_object_put(info->mti_env, mdt_obj);
6773 GOTO(out, rc = -ENOENT);
6776 if (mdt_object_remote(mdt_obj)) {
6777 mdt_object_put(info->mti_env, mdt_obj);
6778 GOTO(remote_out, rc = -EREMOTE);
6781 rc = mdt_links_read(info, mdt_obj, &ldata);
6783 mdt_object_put(info->mti_env, mdt_obj);
6788 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6789 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6790 /* If set, use link #linkno for path lookup, otherwise use
6791 link #0. Only do this for the final path element. */
6792 if (first && fp->gf_linkno < leh->leh_reccount) {
6794 for (count = 0; count < fp->gf_linkno; count++) {
6795 lee = (struct link_ea_entry *)
6796 ((char *)lee + reclen);
6797 linkea_entry_unpack(lee, &reclen, tmpname,
6800 if (fp->gf_linkno < leh->leh_reccount - 1)
6801 /* indicate to user there are more links */
6805 lmv_buf.lb_buf = info->mti_xattr_buf;
6806 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
6807 /* Check if it is slave stripes */
6808 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6809 &lmv_buf, XATTR_NAME_LMV);
6810 mdt_object_put(info->mti_env, mdt_obj);
6812 union lmv_mds_md *lmm = lmv_buf.lb_buf;
6814 /* For slave stripes, get its master */
6815 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
6816 fp->gf_fid = *tmpfid;
6819 } else if (rc < 0 && rc != -ENODATA) {
6823 /* Pack the name in the end of the buffer */
6824 ptr -= tmpname->ln_namelen;
6825 if (ptr - 1 <= fp->gf_u.gf_path)
6826 GOTO(out, rc = -EOVERFLOW);
6827 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6830 /* keep the last resolved fid to the client, so the
6831 * client will build the left path on another MDT for
6833 fp->gf_fid = *tmpfid;
6838 /* non-zero will be treated as an error */
6842 ptr++; /* skip leading / */
6843 memmove(fp->gf_u.gf_path, ptr,
6844 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6851 * Given an MDT object, use mdt_path_current to get the path.
6852 * Essentially a wrapper to retry mdt_path_current a set number of times
6853 * if -EAGAIN is returned (usually because an object is being moved).
6855 * Part of the MDT layer implementation of lfs fid2path.
6857 * \param[in] info Per-thread common data shared by mdt level handlers.
6858 * \param[in] obj Object to do path lookup of
6859 * \param[in,out] fp User-provided struct for arguments and to store path
6862 * \retval 0 Lookup successful, path information stored in fp
6863 * \retval negative errno if there was a problem
6865 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6866 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6868 struct mdt_device *mdt = info->mti_mdt;
6873 if (fp->gf_pathlen < 3)
6876 if (root_fid == NULL)
6877 root_fid = &mdt->mdt_md_root_fid;
6879 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6880 fp->gf_u.gf_path[0] = '\0';
6884 /* Retry multiple times in case file is being moved */
6885 while (tries-- && rc == -EAGAIN)
6886 rc = mdt_path_current(info, obj, fp, root_fid);
6892 * Get the full path of the provided FID, as of changelog record recno.
6894 * This checks sanity and looks up object for user provided FID
6895 * before calling the actual path lookup code.
6897 * Part of the MDT layer implementation of lfs fid2path.
6899 * \param[in] info Per-thread common data shared by mdt level handlers.
6900 * \param[in,out] fp User-provided struct for arguments and to store path
6903 * \retval 0 Lookup successful, path information and recno stored in fp
6904 * \retval -ENOENT, object does not exist
6905 * \retval negative errno if there was a problem
6907 static int mdt_fid2path(struct mdt_thread_info *info,
6908 struct lu_fid *root_fid,
6909 struct getinfo_fid2path *fp)
6911 struct mdt_device *mdt = info->mti_mdt;
6912 struct mdt_object *obj;
6916 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6917 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6919 if (!fid_is_sane(&fp->gf_fid))
6922 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6923 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6924 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6925 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6929 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6932 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6933 PFID(&fp->gf_fid), rc);
6937 if (mdt_object_remote(obj))
6939 else if (!mdt_object_exists(obj))
6945 mdt_object_put(info->mti_env, obj);
6946 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6947 PFID(&fp->gf_fid), rc);
6951 rc = mdt_path(info, obj, fp, root_fid);
6953 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6954 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6955 fp->gf_recno, fp->gf_linkno);
6957 mdt_object_put(info->mti_env, obj);
6962 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6963 void *val, int vallen)
6965 struct getinfo_fid2path *fpout, *fpin;
6966 struct lu_fid *root_fid = NULL;
6969 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6972 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6973 lustre_swab_fid2path(fpin);
6975 memcpy(fpout, fpin, sizeof(*fpin));
6976 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6979 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6980 sizeof(struct lu_fid)) {
6981 /* client sent its root FID, which is normally fileset FID */
6982 root_fid = fpin->gf_u.gf_root_fid;
6983 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6984 lustre_swab_lu_fid(root_fid);
6986 if (root_fid != NULL && !fid_is_sane(root_fid))
6990 rc = mdt_fid2path(info, root_fid, fpout);
6994 int mdt_get_info(struct tgt_session_info *tsi)
7004 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7006 CDEBUG(D_IOCTL, "No GETINFO key\n");
7007 RETURN(err_serious(-EFAULT));
7009 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7012 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7013 if (vallen == NULL) {
7014 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7015 tgt_name(tsi->tsi_tgt));
7016 RETURN(err_serious(-EFAULT));
7019 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7021 rc = req_capsule_server_pack(tsi->tsi_pill);
7023 RETURN(err_serious(rc));
7025 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7026 if (valout == NULL) {
7027 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7028 tgt_name(tsi->tsi_tgt));
7029 RETURN(err_serious(-EFAULT));
7032 if (KEY_IS(KEY_FID2PATH)) {
7033 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7035 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7036 mdt_thread_info_fini(info);
7043 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7045 struct obd_ioctl_data *data = karg;
7048 struct mdt_object *obj;
7049 struct mdt_lock_handle *lh;
7053 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7054 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7057 fid = (struct lu_fid *)data->ioc_inlbuf1;
7059 if (!fid_is_sane(fid))
7062 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7064 lh = &mti->mti_lh[MDT_LH_PARENT];
7065 mdt_lock_reg_init(lh, LCK_CR);
7067 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7069 RETURN(PTR_ERR(obj));
7071 if (mdt_object_remote(obj)) {
7074 * before calling version get the correct MDS should be
7075 * fid, this is error to find remote object here
7077 CERROR("nonlocal object "DFID"\n", PFID(fid));
7078 } else if (!mdt_object_exists(obj)) {
7079 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7082 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7083 *(__u64 *)data->ioc_inlbuf2 = version;
7086 mdt_object_unlock_put(mti, obj, lh, 1);
7090 /* ioctls on obd dev */
7091 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7092 void *karg, void __user *uarg)
7095 struct obd_device *obd = exp->exp_obd;
7096 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7097 struct dt_device *dt = mdt->mdt_bottom;
7101 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7102 rc = lu_env_init(&env, LCT_MD_THREAD);
7108 rc = mdt_device_sync(&env, mdt);
7110 case OBD_IOC_SET_READONLY:
7111 rc = dt_sync(&env, dt);
7113 rc = dt_ro(&env, dt);
7115 case OBD_IOC_ABORT_RECOVERY: {
7116 struct obd_ioctl_data *data = karg;
7118 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7119 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT)
7120 obd->obd_abort_recov_mdt = 1;
7121 else /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7122 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7123 obd->obd_abort_recovery = 1;
7125 target_stop_recovery_thread(obd);
7129 case OBD_IOC_CHANGELOG_REG:
7130 case OBD_IOC_CHANGELOG_DEREG:
7131 case OBD_IOC_CHANGELOG_CLEAR:
7132 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7136 case OBD_IOC_START_LFSCK: {
7137 struct md_device *next = mdt->mdt_child;
7138 struct obd_ioctl_data *data = karg;
7139 struct lfsck_start_param lsp;
7141 if (unlikely(data == NULL)) {
7146 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7147 lsp.lsp_index_valid = 0;
7148 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7151 case OBD_IOC_STOP_LFSCK: {
7152 struct md_device *next = mdt->mdt_child;
7153 struct obd_ioctl_data *data = karg;
7154 struct lfsck_stop stop;
7156 stop.ls_status = LS_STOPPED;
7157 /* Old lfsck utils may pass NULL @stop. */
7158 if (data->ioc_inlbuf1 == NULL)
7162 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7164 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7167 case OBD_IOC_QUERY_LFSCK: {
7168 struct md_device *next = mdt->mdt_child;
7169 struct obd_ioctl_data *data = karg;
7171 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7175 case OBD_IOC_GET_OBJ_VERSION: {
7176 struct mdt_thread_info *mti;
7177 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7178 memset(mti, 0, sizeof *mti);
7179 mti->mti_env = &env;
7183 rc = mdt_ioc_version_get(mti, karg);
7186 case OBD_IOC_CATLOGLIST: {
7187 struct mdt_thread_info *mti;
7189 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7190 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7191 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7192 &mti->mti_tmp_fid1);
7197 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7198 mdt_obd_name(mdt), cmd, rc);
7205 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7207 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7211 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7212 struct lfsck_start_param lsp;
7214 lsp.lsp_start = NULL;
7215 lsp.lsp_index_valid = 0;
7216 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7217 OBD_IOC_START_LFSCK,
7219 if (rc != 0 && rc != -EALREADY)
7220 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7221 mdt_obd_name(mdt), rc);
7224 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7228 static int mdt_obd_postrecov(struct obd_device *obd)
7233 rc = lu_env_init(&env, LCT_MD_THREAD);
7236 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7241 static const struct obd_ops mdt_obd_device_ops = {
7242 .o_owner = THIS_MODULE,
7243 .o_set_info_async = mdt_obd_set_info_async,
7244 .o_connect = mdt_obd_connect,
7245 .o_reconnect = mdt_obd_reconnect,
7246 .o_disconnect = mdt_obd_disconnect,
7247 .o_init_export = mdt_init_export,
7248 .o_destroy_export = mdt_destroy_export,
7249 .o_iocontrol = mdt_iocontrol,
7250 .o_postrecov = mdt_obd_postrecov,
7251 /* Data-on-MDT IO methods */
7252 .o_preprw = mdt_obd_preprw,
7253 .o_commitrw = mdt_obd_commitrw,
7256 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7257 struct lu_device *d)
7259 struct mdt_device *m = mdt_dev(d);
7266 static struct lu_device *mdt_device_free(const struct lu_env *env,
7267 struct lu_device *d)
7269 struct mdt_device *m = mdt_dev(d);
7272 lu_device_fini(&m->mdt_lu_dev);
7278 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7279 struct lu_device_type *t,
7280 struct lustre_cfg *cfg)
7282 struct lu_device *l;
7283 struct mdt_device *m;
7290 rc = mdt_init0(env, m, t, cfg);
7292 mdt_device_free(env, l);
7297 l = ERR_PTR(-ENOMEM);
7301 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7302 LU_KEY_INIT(mdt, struct mdt_thread_info);
7304 static void mdt_key_fini(const struct lu_context *ctx,
7305 struct lu_context_key *key, void* data)
7307 struct mdt_thread_info *info = data;
7309 if (info->mti_big_lmm) {
7310 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7311 info->mti_big_lmm = NULL;
7312 info->mti_big_lmmsize = 0;
7315 if (info->mti_big_acl) {
7316 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7317 info->mti_big_acl = NULL;
7318 info->mti_big_aclsize = 0;
7324 /* context key: mdt_thread_key */
7325 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7327 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7329 return lu_ucred(info->mti_env);
7332 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7334 return lu_ucred_check(info->mti_env);
7338 * Enable/disable COS (Commit On Sharing).
7340 * Set/Clear the COS flag in mdt options.
7342 * \param mdt mdt device
7343 * \param val 0 disables COS, other values enable COS
7345 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7350 mdt->mdt_opts.mo_cos = val;
7351 rc = lu_env_init(&env, LCT_LOCAL);
7352 if (unlikely(rc != 0)) {
7353 CWARN("%s: lu_env initialization failed, cannot "
7354 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7357 mdt_device_sync(&env, mdt);
7362 * Check COS (Commit On Sharing) status.
7364 * Return COS flag status.
7366 * \param mdt mdt device
7368 int mdt_cos_is_enabled(struct mdt_device *mdt)
7370 return mdt->mdt_opts.mo_cos != 0;
7373 static struct lu_device_type_operations mdt_device_type_ops = {
7374 .ldto_device_alloc = mdt_device_alloc,
7375 .ldto_device_free = mdt_device_free,
7376 .ldto_device_fini = mdt_device_fini
7379 static struct lu_device_type mdt_device_type = {
7380 .ldt_tags = LU_DEVICE_MD,
7381 .ldt_name = LUSTRE_MDT_NAME,
7382 .ldt_ops = &mdt_device_type_ops,
7383 .ldt_ctx_tags = LCT_MD_THREAD
7386 static int __init mdt_init(void)
7390 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7391 FID_NOBRACE_LEN + 1);
7392 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7394 rc = lu_kmem_init(mdt_caches);
7398 rc = mds_mod_init();
7402 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
7403 LUSTRE_MDT_NAME, &mdt_device_type);
7408 lu_kmem_fini(mdt_caches);
7415 static void __exit mdt_exit(void)
7417 class_unregister_type(LUSTRE_MDT_NAME);
7419 lu_kmem_fini(mdt_caches);
7422 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7423 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7424 MODULE_VERSION(LUSTRE_VERSION_STRING);
7425 MODULE_LICENSE("GPL");
7427 module_init(mdt_init);
7428 module_exit(mdt_exit);