4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_handler.c
33 * Lustre Metadata Target (mdt) request handler
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
47 #include <linux/pagemap.h>
49 #include <dt_object.h>
50 #include <lustre_acl.h>
51 #include <lustre_export.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lfsck.h>
54 #include <lustre_log.h>
55 #include <lustre_nodemap.h>
56 #include <lustre_mds.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <lustre_quota.h>
59 #include <lustre_swab.h>
60 #include <lustre_lmv.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
66 #include <lustre_crypto.h>
68 #include "mdt_internal.h"
70 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);
275 * Check whether \a o is directory stripe object.
277 * \param[in] info thread environment
278 * \param[in] o MDT object
280 * \retval 1 is directory stripe.
281 * \retval 0 isn't directory stripe.
282 * \retval < 1 error code
284 static int mdt_is_dir_stripe(struct mdt_thread_info *info,
285 struct mdt_object *o)
287 struct md_attr *ma = &info->mti_attr;
288 struct lmv_mds_md_v1 *lmv;
291 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
295 if (!(ma->ma_valid & MA_LMV))
298 lmv = &ma->ma_lmv->lmv_md_v1;
300 if (!lmv_is_sane2(lmv))
303 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE)
309 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
312 struct mdt_device *mdt = info->mti_mdt;
313 struct lu_name *lname = &info->mti_name;
314 const char *start = fileset;
315 char *filename = info->mti_filename;
316 struct mdt_object *parent;
320 LASSERT(!info->mti_cross_ref);
323 * We may want to allow this to mount a completely separate
324 * fileset from the MDT in the future, but keeping it to
325 * ROOT/ only for now avoid potential security issues.
327 *fid = mdt->mdt_md_root_fid;
329 while (rc == 0 && start != NULL && *start != '\0') {
330 const char *s1 = start;
336 while (*s2 != '/' && *s2 != '\0')
344 lname->ln_namelen = s2 - s1;
345 if (lname->ln_namelen > NAME_MAX) {
350 /* reject .. as a path component */
351 if (lname->ln_namelen == 2 &&
352 strncmp(s1, "..", 2) == 0) {
357 strncpy(filename, s1, lname->ln_namelen);
358 filename[lname->ln_namelen] = '\0';
359 lname->ln_name = filename;
361 parent = mdt_object_find(info->mti_env, mdt, fid);
362 if (IS_ERR(parent)) {
363 rc = PTR_ERR(parent);
366 /* Only got the fid of this obj by name */
368 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
369 fid, &info->mti_spec);
370 mdt_object_put(info->mti_env, parent);
373 parent = mdt_object_find(info->mti_env, mdt, fid);
375 rc = PTR_ERR(parent);
377 mode = lu_object_attr(&parent->mot_obj);
378 if (!S_ISDIR(mode)) {
380 } else if (mdt_is_remote_object(info, parent, parent)) {
381 if (!mdt->mdt_enable_remote_subdir_mount) {
383 LCONSOLE_WARN("%s: subdir mount '%s' refused because 'enable_remote_subdir_mount=0': rc = %d\n",
387 LCONSOLE_INFO("%s: subdir mount '%s' is remote and may be slow\n",
392 mdt_object_put(info->mti_env, parent);
399 static int mdt_get_root(struct tgt_session_info *tsi)
401 struct mdt_thread_info *info = tsi2mdt_info(tsi);
402 struct mdt_device *mdt = info->mti_mdt;
403 struct mdt_body *repbody;
404 char *fileset = NULL, *buffer = NULL;
406 struct obd_export *exp = info->mti_exp;
407 char *nodemap_fileset;
411 rc = mdt_check_ucred(info);
413 GOTO(out, rc = err_serious(rc));
415 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
416 GOTO(out, rc = err_serious(-ENOMEM));
418 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
420 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
422 GOTO(out, rc = err_serious(-EFAULT));
425 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
426 if (nodemap_fileset && nodemap_fileset[0]) {
427 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
429 /* consider fileset from client as a sub-fileset
430 * of the nodemap one */
431 OBD_ALLOC(buffer, PATH_MAX + 1);
433 GOTO(out, rc = err_serious(-ENOMEM));
434 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
435 nodemap_fileset, fileset) >= PATH_MAX + 1)
436 GOTO(out, rc = err_serious(-EINVAL));
439 /* enforce fileset as specified in the nodemap */
440 fileset = nodemap_fileset;
445 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
446 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
448 GOTO(out, rc = err_serious(rc));
450 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
452 repbody->mbo_valid |= OBD_MD_FLID;
456 mdt_thread_info_fini(info);
458 OBD_FREE(buffer, PATH_MAX+1);
462 static int mdt_statfs(struct tgt_session_info *tsi)
464 struct ptlrpc_request *req = tgt_ses_req(tsi);
465 struct mdt_thread_info *info = tsi2mdt_info(tsi);
466 struct mdt_device *mdt = info->mti_mdt;
467 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
468 struct md_device *next = mdt->mdt_child;
469 struct ptlrpc_service_part *svcpt;
470 struct obd_statfs *osfs;
471 struct mdt_body *reqbody = NULL;
472 struct mdt_statfs_cache *msf;
473 ktime_t kstart = ktime_get();
474 int current_blockbits;
479 svcpt = req->rq_rqbd->rqbd_svcpt;
481 /* This will trigger a watchdog timeout */
482 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
483 (MDT_SERVICE_WATCHDOG_FACTOR *
484 at_get(&svcpt->scp_at_estimate)) + 1);
486 rc = mdt_check_ucred(info);
488 GOTO(out, rc = err_serious(rc));
490 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
491 GOTO(out, rc = err_serious(-ENOMEM));
493 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
495 GOTO(out, rc = -EPROTO);
497 if (mdt_is_sum_statfs_client(req->rq_export) &&
498 lustre_packed_msg_size(req->rq_reqmsg) ==
499 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
500 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
501 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
502 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
505 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
506 msf = &mdt->mdt_sum_osfs;
508 msf = &mdt->mdt_osfs;
510 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
511 /** statfs data is too old, get up-to-date one */
512 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
513 rc = next->md_ops->mdo_statfs(info->mti_env,
516 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
520 spin_lock(&mdt->mdt_lock);
521 msf->msf_osfs = *osfs;
522 msf->msf_age = ktime_get_seconds();
523 spin_unlock(&mdt->mdt_lock);
525 /** use cached statfs data */
526 spin_lock(&mdt->mdt_lock);
527 *osfs = msf->msf_osfs;
528 spin_unlock(&mdt->mdt_lock);
531 /* tgd_blockbit is recordsize bits set during mkfs.
532 * This once set does not change. However, 'zfs set'
533 * can be used to change the MDT blocksize. Instead
534 * of using cached value of 'tgd_blockbit' always
535 * calculate the blocksize bits which may have
538 current_blockbits = fls64(osfs->os_bsize) - 1;
540 /* at least try to account for cached pages. its still racy and
541 * might be under-reporting if clients haven't announced their
542 * caches with brw recently */
543 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
544 " pending %llu free %llu avail %llu\n",
545 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
546 tgd->tgd_tot_pending,
547 osfs->os_bfree << current_blockbits,
548 osfs->os_bavail << current_blockbits);
550 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
551 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
552 osfs->os_bsize - 1) >> current_blockbits));
554 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
555 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
556 "%llu objects: %llu free; state %x\n",
557 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
558 osfs->os_files, osfs->os_ffree, osfs->os_state);
560 if (!exp_grant_param_supp(tsi->tsi_exp) &&
561 current_blockbits > COMPAT_BSIZE_SHIFT) {
562 /* clients which don't support OBD_CONNECT_GRANT_PARAM
563 * should not see a block size > page size, otherwise
564 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
565 * block size which is the biggest block size known to work
566 * with all client's page size. */
567 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
568 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
569 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
570 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
573 mdt_counter_incr(req, LPROC_MDT_STATFS,
574 ktime_us_delta(ktime_get(), kstart));
576 mdt_thread_info_fini(info);
580 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
582 struct lov_comp_md_v1 *comp_v1;
583 struct lov_mds_md *v1;
585 __u32 dom_stripesize = 0;
587 bool has_ost_stripes = false;
594 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
597 comp_v1 = (struct lov_comp_md_v1 *)lmm;
598 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
599 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
601 /* Fast check for DoM entry with no mirroring, should be the first */
602 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
603 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
606 /* check all entries otherwise */
607 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
608 struct lov_comp_md_entry_v1 *lcme;
610 lcme = &comp_v1->lcm_entries[i];
611 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
614 off = le32_to_cpu(lcme->lcme_offset);
615 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
617 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
619 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
621 has_ost_stripes = true;
623 if (dom_stripesize && has_ost_stripes)
624 RETURN(dom_stripesize);
626 /* DoM-only case exits here */
627 if (is_dom_only && dom_stripesize)
629 RETURN(dom_stripesize);
633 * Pack size attributes into the reply.
635 int mdt_pack_size2body(struct mdt_thread_info *info,
636 const struct lu_fid *fid, struct lustre_handle *lh)
639 struct md_attr *ma = &info->mti_attr;
641 bool dom_lock = false;
645 LASSERT(ma->ma_attr.la_valid & LA_MODE);
647 if (!S_ISREG(ma->ma_attr.la_mode) ||
648 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
651 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
652 /* no DoM stripe, no size in reply */
656 if (lustre_handle_is_used(lh)) {
657 struct ldlm_lock *lock;
659 lock = ldlm_handle2lock(lh);
661 dom_lock = ldlm_has_dom(lock);
666 /* no DoM lock, no size in reply */
670 /* Either DoM lock exists or LMM has only DoM stripe then
671 * return size on body. */
672 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
674 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
678 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
680 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
682 * \param info thread info object
683 * \param repbody reply to pack ACLs into
684 * \param o mdt object of file to examine
685 * \param nodemap nodemap of client to reply to
687 * \retval -errno error getting or parsing ACL from disk
689 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
690 struct mdt_object *o, struct lu_nodemap *nodemap)
692 const struct lu_env *env = info->mti_env;
693 struct md_object *next = mdt_object_child(o);
694 struct lu_buf *buf = &info->mti_buf;
695 struct mdt_device *mdt = info->mti_mdt;
696 struct req_capsule *pill = info->mti_pill;
701 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
702 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
703 if (buf->lb_len == 0)
706 LASSERT(!info->mti_big_acl_used);
708 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
710 if (rc == -ENODATA) {
711 repbody->mbo_aclsize = 0;
712 repbody->mbo_valid |= OBD_MD_FLACL;
714 } else if (rc == -EOPNOTSUPP) {
716 } else if (rc == -ERANGE) {
717 if (exp_connect_large_acl(info->mti_exp) &&
718 !info->mti_big_acl_used) {
719 if (info->mti_big_acl == NULL) {
720 info->mti_big_aclsize =
722 mdt->mdt_max_ea_size,
724 OBD_ALLOC_LARGE(info->mti_big_acl,
725 info->mti_big_aclsize);
726 if (info->mti_big_acl == NULL) {
727 info->mti_big_aclsize = 0;
728 CERROR("%s: unable to grow "
731 PFID(mdt_object_fid(o)));
736 CDEBUG(D_INODE, "%s: grow the "DFID
737 " ACL buffer to size %d\n",
739 PFID(mdt_object_fid(o)),
740 info->mti_big_aclsize);
742 buf->lb_buf = info->mti_big_acl;
743 buf->lb_len = info->mti_big_aclsize;
744 info->mti_big_acl_used = 1;
747 /* FS has ACL bigger that our limits */
748 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
749 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
750 info->mti_big_aclsize);
753 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
754 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
757 rc = nodemap_map_acl(nodemap, buf->lb_buf,
758 rc, NODEMAP_FS_TO_CLIENT);
759 /* if all ACLs mapped out, rc is still >= 0 */
761 CERROR("%s: nodemap_map_acl unable to parse "DFID
762 " ACL: rc = %d\n", mdt_obd_name(mdt),
763 PFID(mdt_object_fid(o)), rc);
764 repbody->mbo_aclsize = 0;
765 repbody->mbo_valid &= ~OBD_MD_FLACL;
767 repbody->mbo_aclsize = rc;
768 repbody->mbo_valid |= OBD_MD_FLACL;
777 /* XXX Look into layout in MDT layer. */
778 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
780 struct lov_comp_md_v1 *comp_v1;
781 struct lov_mds_md *v1;
784 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
785 comp_v1 = (struct lov_comp_md_v1 *)lmm;
787 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
788 v1 = (struct lov_mds_md *)((char *)comp_v1 +
789 comp_v1->lcm_entries[i].lcme_offset);
790 /* We don't support partial release for now */
791 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
796 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
801 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
802 const struct lu_attr *attr, const struct lu_fid *fid)
804 struct md_attr *ma = &info->mti_attr;
805 struct obd_export *exp = info->mti_exp;
806 struct lu_nodemap *nodemap = NULL;
808 LASSERT(ma->ma_valid & MA_INODE);
810 if (attr->la_valid & LA_ATIME) {
811 b->mbo_atime = attr->la_atime;
812 b->mbo_valid |= OBD_MD_FLATIME;
814 if (attr->la_valid & LA_MTIME) {
815 b->mbo_mtime = attr->la_mtime;
816 b->mbo_valid |= OBD_MD_FLMTIME;
818 if (attr->la_valid & LA_CTIME) {
819 b->mbo_ctime = attr->la_ctime;
820 b->mbo_valid |= OBD_MD_FLCTIME;
822 if (attr->la_valid & LA_BTIME) {
823 b->mbo_btime = attr->la_btime;
824 b->mbo_valid |= OBD_MD_FLBTIME;
826 if (attr->la_valid & LA_FLAGS) {
827 b->mbo_flags = attr->la_flags;
828 b->mbo_valid |= OBD_MD_FLFLAGS;
830 if (attr->la_valid & LA_NLINK) {
831 b->mbo_nlink = attr->la_nlink;
832 b->mbo_valid |= OBD_MD_FLNLINK;
834 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
835 nodemap = nodemap_get_from_exp(exp);
839 if (attr->la_valid & LA_UID) {
840 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
841 NODEMAP_FS_TO_CLIENT,
843 b->mbo_valid |= OBD_MD_FLUID;
845 if (attr->la_valid & LA_GID) {
846 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
847 NODEMAP_FS_TO_CLIENT,
849 b->mbo_valid |= OBD_MD_FLGID;
852 if (attr->la_valid & LA_PROJID) {
853 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
854 NODEMAP_FS_TO_CLIENT,
856 b->mbo_valid |= OBD_MD_FLPROJID;
859 b->mbo_mode = attr->la_mode;
860 if (attr->la_valid & LA_MODE)
861 b->mbo_valid |= OBD_MD_FLMODE;
862 if (attr->la_valid & LA_TYPE)
863 b->mbo_valid |= OBD_MD_FLTYPE;
867 b->mbo_valid |= OBD_MD_FLID;
868 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
869 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
872 if (!(attr->la_valid & LA_TYPE))
875 b->mbo_rdev = attr->la_rdev;
876 b->mbo_size = attr->la_size;
877 b->mbo_blocks = attr->la_blocks;
879 if (!S_ISREG(attr->la_mode)) {
880 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
881 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
882 /* means no objects are allocated on osts. */
883 LASSERT(!(ma->ma_valid & MA_LOV));
884 /* just ignore blocks occupied by extend attributes on MDS */
886 /* if no object is allocated on osts, the size on mds is valid.
888 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
889 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
890 if (mdt_hsm_is_released(ma->ma_lmm)) {
891 /* A released file stores its size on MDS. */
892 /* But return 1 block for released file, unless tools
893 * like tar will consider it fully sparse. (LU-3864)
895 if (unlikely(b->mbo_size == 0))
899 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
900 } else if (info->mti_som_valid) { /* som is valid */
901 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
902 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
903 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
904 b->mbo_size = ma->ma_som.ms_size;
905 b->mbo_blocks = ma->ma_som.ms_blocks;
909 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
910 b->mbo_valid & OBD_MD_FLLAZYSIZE))
911 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
912 PFID(fid), (unsigned long long)b->mbo_size);
915 if (!IS_ERR_OR_NULL(nodemap))
916 nodemap_putref(nodemap);
919 static inline int mdt_body_has_lov(const struct lu_attr *la,
920 const struct mdt_body *body)
922 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
923 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
926 void mdt_client_compatibility(struct mdt_thread_info *info)
928 struct mdt_body *body;
929 struct ptlrpc_request *req = mdt_info_req(info);
930 struct obd_export *exp = req->rq_export;
931 struct md_attr *ma = &info->mti_attr;
932 struct lu_attr *la = &ma->ma_attr;
935 if (exp_connect_layout(exp))
936 /* the client can deal with 16-bit lmm_stripe_count */
939 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
941 if (!mdt_body_has_lov(la, body))
944 /* now we have a reply with a lov for a client not compatible with the
945 * layout lock so we have to clean the layout generation number */
946 if (S_ISREG(la->la_mode))
947 ma->ma_lmm->lmm_layout_gen = 0;
951 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
952 struct mdt_object *o)
954 const struct lu_env *env = info->mti_env;
957 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
966 /* Is it a directory? Let's check for the LMV as well */
967 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
968 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
972 rc2 = mo_xattr_get(env, mdt_object_child(o),
974 XATTR_NAME_DEFAULT_LMV);
976 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
984 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
987 const struct lu_env *env = info->mti_env;
991 LASSERT(info->mti_big_lmm_used == 0);
992 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
996 /* big_lmm may need to be grown */
997 if (info->mti_big_lmmsize < rc) {
998 int size = size_roundup_power2(rc);
1000 if (info->mti_big_lmmsize > 0) {
1001 /* free old buffer */
1002 LASSERT(info->mti_big_lmm);
1003 OBD_FREE_LARGE(info->mti_big_lmm,
1004 info->mti_big_lmmsize);
1005 info->mti_big_lmm = NULL;
1006 info->mti_big_lmmsize = 0;
1009 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1010 if (info->mti_big_lmm == NULL)
1012 info->mti_big_lmmsize = size;
1014 LASSERT(info->mti_big_lmmsize >= rc);
1016 info->mti_buf.lb_buf = info->mti_big_lmm;
1017 info->mti_buf.lb_len = info->mti_big_lmmsize;
1018 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1023 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1024 struct md_attr *ma, const char *name)
1026 struct md_object *next = mdt_object_child(o);
1027 struct lu_buf *buf = &info->mti_buf;
1030 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1031 buf->lb_buf = ma->ma_lmm;
1032 buf->lb_len = ma->ma_lmm_size;
1033 LASSERT(!(ma->ma_valid & MA_LOV));
1034 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1035 buf->lb_buf = ma->ma_lmv;
1036 buf->lb_len = ma->ma_lmv_size;
1037 LASSERT(!(ma->ma_valid & MA_LMV));
1038 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1039 buf->lb_buf = ma->ma_default_lmv;
1040 buf->lb_len = ma->ma_default_lmv_size;
1041 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1046 LASSERT(buf->lb_buf);
1048 rc = mo_xattr_get(info->mti_env, next, buf, name);
1052 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1053 if (info->mti_big_lmm_used)
1054 ma->ma_lmm = info->mti_big_lmm;
1056 /* NOT return LOV EA with hole to old client. */
1057 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1058 LOV_PATTERN_F_HOLE) &&
1059 !(exp_connect_flags(info->mti_exp) &
1060 OBD_CONNECT_LFSCK)) {
1063 ma->ma_lmm_size = rc;
1064 ma->ma_valid |= MA_LOV;
1066 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1067 if (info->mti_big_lmm_used)
1068 ma->ma_lmv = info->mti_big_lmm;
1070 ma->ma_lmv_size = rc;
1071 ma->ma_valid |= MA_LMV;
1072 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1073 ma->ma_default_lmv_size = rc;
1074 ma->ma_valid |= MA_LMV_DEF;
1077 /* Update mdt_max_mdsize so all clients will be aware that */
1078 if (info->mti_mdt->mdt_max_mdsize < rc)
1079 info->mti_mdt->mdt_max_mdsize = rc;
1082 } else if (rc == -ENODATA) {
1085 } else if (rc == -ERANGE) {
1086 /* Default LMV has fixed size, so it must be able to fit
1087 * in the original buffer */
1088 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1090 rc = mdt_big_xattr_get(info, o, name);
1092 info->mti_big_lmm_used = 1;
1100 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1101 struct md_attr *ma, const char *name)
1105 if (!info->mti_big_lmm) {
1106 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1107 if (!info->mti_big_lmm)
1109 info->mti_big_lmmsize = PAGE_SIZE;
1112 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1113 ma->ma_lmm = info->mti_big_lmm;
1114 ma->ma_lmm_size = info->mti_big_lmmsize;
1115 ma->ma_valid &= ~MA_LOV;
1116 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1117 ma->ma_lmv = info->mti_big_lmm;
1118 ma->ma_lmv_size = info->mti_big_lmmsize;
1119 ma->ma_valid &= ~MA_LMV;
1124 LASSERT(!info->mti_big_lmm_used);
1125 rc = __mdt_stripe_get(info, o, ma, name);
1126 /* since big_lmm is always used here, clear 'used' flag to avoid
1127 * assertion in mdt_big_xattr_get().
1129 info->mti_big_lmm_used = 0;
1134 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1135 struct lu_fid *pfid)
1137 struct lu_buf *buf = &info->mti_buf;
1138 struct link_ea_header *leh;
1139 struct link_ea_entry *lee;
1143 buf->lb_buf = info->mti_big_lmm;
1144 buf->lb_len = info->mti_big_lmmsize;
1145 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1146 buf, XATTR_NAME_LINK);
1147 /* ignore errors, MA_PFID won't be set and it is
1148 * up to the caller to treat this as an error */
1149 if (rc == -ERANGE || buf->lb_len == 0) {
1150 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1151 buf->lb_buf = info->mti_big_lmm;
1152 buf->lb_len = info->mti_big_lmmsize;
1157 if (rc < sizeof(*leh)) {
1158 CERROR("short LinkEA on "DFID": rc = %d\n",
1159 PFID(mdt_object_fid(o)), rc);
1163 leh = (struct link_ea_header *) buf->lb_buf;
1164 lee = (struct link_ea_entry *)(leh + 1);
1165 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1166 leh->leh_magic = LINK_EA_MAGIC;
1167 leh->leh_reccount = __swab32(leh->leh_reccount);
1168 leh->leh_len = __swab64(leh->leh_len);
1170 if (leh->leh_magic != LINK_EA_MAGIC)
1172 if (leh->leh_reccount == 0)
1175 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1176 fid_be_to_cpu(pfid, pfid);
1181 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1182 struct lu_fid *pfid, struct lu_name *lname)
1184 struct lu_buf *buf = &info->mti_buf;
1185 struct link_ea_header *leh;
1186 struct link_ea_entry *lee;
1190 buf->lb_buf = info->mti_xattr_buf;
1191 buf->lb_len = sizeof(info->mti_xattr_buf);
1192 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1194 if (rc == -ERANGE) {
1195 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1196 buf->lb_buf = info->mti_big_lmm;
1197 buf->lb_len = info->mti_big_lmmsize;
1202 if (rc < sizeof(*leh)) {
1203 CERROR("short LinkEA on "DFID": rc = %d\n",
1204 PFID(mdt_object_fid(o)), rc);
1208 leh = (struct link_ea_header *)buf->lb_buf;
1209 lee = (struct link_ea_entry *)(leh + 1);
1210 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1211 leh->leh_magic = LINK_EA_MAGIC;
1212 leh->leh_reccount = __swab32(leh->leh_reccount);
1213 leh->leh_len = __swab64(leh->leh_len);
1215 if (leh->leh_magic != LINK_EA_MAGIC)
1218 if (leh->leh_reccount == 0)
1221 linkea_entry_unpack(lee, &reclen, lname, pfid);
1226 int mdt_attr_get_complex(struct mdt_thread_info *info,
1227 struct mdt_object *o, struct md_attr *ma)
1229 const struct lu_env *env = info->mti_env;
1230 struct md_object *next = mdt_object_child(o);
1231 struct lu_buf *buf = &info->mti_buf;
1232 int need = ma->ma_need;
1239 if (mdt_object_exists(o) == 0)
1240 GOTO(out, rc = -ENOENT);
1241 mode = lu_object_attr(&next->mo_lu);
1243 if (need & MA_INODE) {
1244 ma->ma_need = MA_INODE;
1245 rc = mo_attr_get(env, next, ma);
1250 (void) mdt_get_som(info, o, ma);
1251 ma->ma_valid |= MA_INODE;
1254 if (need & MA_PFID) {
1255 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1257 ma->ma_valid |= MA_PFID;
1258 /* ignore this error, parent fid is not mandatory */
1262 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1263 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1268 if (need & MA_LMV && S_ISDIR(mode)) {
1269 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1274 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1275 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1281 * In the handle of MA_INODE, we may already get the SOM attr.
1283 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1284 rc = mdt_get_som(info, o, ma);
1289 if (need & MA_HSM && S_ISREG(mode)) {
1290 buf->lb_buf = info->mti_xattr_buf;
1291 buf->lb_len = sizeof(info->mti_xattr_buf);
1292 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1293 sizeof(info->mti_xattr_buf));
1294 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1295 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1297 ma->ma_valid |= MA_HSM;
1298 else if (rc2 < 0 && rc2 != -ENODATA)
1299 GOTO(out, rc = rc2);
1302 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1303 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1304 buf->lb_buf = ma->ma_acl;
1305 buf->lb_len = ma->ma_acl_size;
1306 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1308 ma->ma_acl_size = rc2;
1309 ma->ma_valid |= MA_ACL_DEF;
1310 } else if (rc2 == -ENODATA) {
1312 ma->ma_acl_size = 0;
1314 GOTO(out, rc = rc2);
1319 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1320 rc, ma->ma_valid, ma->ma_lmm);
1324 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
1326 struct req_capsule *pill = info->mti_pill;
1329 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
1331 /* pre-set size in server part with max size */
1332 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
1334 info->mti_mdt->mdt_max_mdsize);
1338 static int mdt_getattr_internal(struct mdt_thread_info *info,
1339 struct mdt_object *o, int ma_need)
1341 struct mdt_device *mdt = info->mti_mdt;
1342 struct md_object *next = mdt_object_child(o);
1343 const struct mdt_body *reqbody = info->mti_body;
1344 struct ptlrpc_request *req = mdt_info_req(info);
1345 struct md_attr *ma = &info->mti_attr;
1346 struct lu_attr *la = &ma->ma_attr;
1347 struct req_capsule *pill = info->mti_pill;
1348 const struct lu_env *env = info->mti_env;
1349 struct mdt_body *repbody;
1350 struct lu_buf *buffer = &info->mti_buf;
1351 struct obd_export *exp = info->mti_exp;
1352 ktime_t kstart = ktime_get();
1357 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1358 RETURN(err_serious(-ENOMEM));
1360 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1364 if (mdt_object_remote(o)) {
1365 /* This object is located on remote node.*/
1366 /* Return -ENOTSUPP for old client */
1367 if (!mdt_is_dne_client(req->rq_export))
1368 GOTO(out, rc = -ENOTSUPP);
1370 repbody->mbo_fid1 = *mdt_object_fid(o);
1371 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1375 if (reqbody->mbo_eadatasize > 0) {
1376 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1377 if (buffer->lb_buf == NULL)
1378 GOTO(out, rc = -EPROTO);
1379 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1382 buffer->lb_buf = NULL;
1384 ma_need &= ~(MA_LOV | MA_LMV);
1385 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1386 mdt_obd_name(info->mti_mdt),
1387 req->rq_export->exp_client_uuid.uuid);
1390 /* from 2.12.58 intent_getattr pack default LMV in reply */
1391 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1392 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1393 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1394 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1396 ma->ma_lmv = buffer->lb_buf;
1397 ma->ma_lmv_size = buffer->lb_len;
1398 ma->ma_default_lmv = req_capsule_server_get(pill,
1399 &RMF_DEFAULT_MDT_MD);
1400 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1401 &RMF_DEFAULT_MDT_MD,
1403 ma->ma_need = MA_INODE;
1404 if (ma->ma_lmv_size > 0)
1405 ma->ma_need |= MA_LMV;
1406 if (ma->ma_default_lmv_size > 0)
1407 ma->ma_need |= MA_LMV_DEF;
1408 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1409 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1410 /* If it is dir and client require MEA, then we got MEA */
1411 /* Assumption: MDT_MD size is enough for lmv size. */
1412 ma->ma_lmv = buffer->lb_buf;
1413 ma->ma_lmv_size = buffer->lb_len;
1414 ma->ma_need = MA_INODE;
1415 if (ma->ma_lmv_size > 0) {
1416 if (reqbody->mbo_valid & OBD_MD_MEA) {
1417 ma->ma_need |= MA_LMV;
1418 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1419 ma->ma_need |= MA_LMV_DEF;
1420 ma->ma_default_lmv = buffer->lb_buf;
1422 ma->ma_default_lmv_size = buffer->lb_len;
1423 ma->ma_lmv_size = 0;
1427 ma->ma_lmm = buffer->lb_buf;
1428 ma->ma_lmm_size = buffer->lb_len;
1429 ma->ma_need = MA_INODE | MA_HSM;
1430 if (ma->ma_lmm_size > 0) {
1431 ma->ma_need |= MA_LOV;
1432 /* Older clients may crash if they getattr overstriped
1435 if (!exp_connect_overstriping(exp) &&
1436 mdt_lmm_is_overstriping(ma->ma_lmm))
1437 RETURN(-EOPNOTSUPP);
1441 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1442 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1443 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1444 /* get default stripe info for this dir. */
1445 ma->ma_need |= MA_LOV_DEF;
1447 ma->ma_need |= ma_need;
1449 rc = mdt_attr_get_complex(info, o, ma);
1451 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1452 "%s: getattr error for "DFID": rc = %d\n",
1453 mdt_obd_name(info->mti_mdt),
1454 PFID(mdt_object_fid(o)), rc);
1458 /* if file is released, check if a restore is running */
1459 if (ma->ma_valid & MA_HSM) {
1460 repbody->mbo_valid |= OBD_MD_TSTATE;
1461 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1462 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1463 repbody->mbo_t_state = MS_RESTORE;
1466 if (unlikely(!(ma->ma_valid & MA_INODE)))
1469 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1471 if (mdt_body_has_lov(la, reqbody)) {
1472 u32 stripe_count = 1;
1473 bool fixed_layout = false;
1475 if (ma->ma_valid & MA_LOV) {
1476 LASSERT(ma->ma_lmm_size);
1477 repbody->mbo_eadatasize = ma->ma_lmm_size;
1478 if (S_ISDIR(la->la_mode))
1479 repbody->mbo_valid |= OBD_MD_FLDIREA;
1481 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1482 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1484 if (ma->ma_valid & MA_LMV) {
1485 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1486 u32 magic = le32_to_cpu(lmv->lmv_magic);
1488 /* Return -ENOTSUPP for old client */
1489 if (!mdt_is_striped_client(req->rq_export))
1492 LASSERT(S_ISDIR(la->la_mode));
1493 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1494 repbody->mbo_eadatasize = ma->ma_lmv_size;
1495 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1497 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1498 fixed_layout = lmv_is_fixed(lmv);
1499 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1500 mdt_restripe_migrate_add(info, o);
1501 else if (magic == LMV_MAGIC_V1 &&
1502 lmv_is_restriping(lmv))
1503 mdt_restripe_update_add(info, o);
1505 if (ma->ma_valid & MA_LMV_DEF) {
1506 /* Return -ENOTSUPP for old client */
1507 if (!mdt_is_striped_client(req->rq_export))
1509 LASSERT(S_ISDIR(la->la_mode));
1511 * when ll_dir_getstripe() gets default LMV, it
1512 * checks mbo_eadatasize.
1514 if (!(ma->ma_valid & MA_LMV))
1515 repbody->mbo_eadatasize =
1516 ma->ma_default_lmv_size;
1517 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1518 OBD_MD_DEFAULT_MEA);
1521 "dirent count %llu stripe count %u MDT count %d\n",
1522 ma->ma_attr.la_dirent_count, stripe_count,
1523 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1524 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1525 ma->ma_attr.la_dirent_count >
1526 mdt->mdt_restriper.mdr_dir_split_count &&
1527 !fid_is_root(mdt_object_fid(o)) &&
1528 mdt->mdt_enable_dir_auto_split &&
1529 !o->mot_restriping &&
1530 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1532 mdt_auto_split_add(info, o);
1533 } else if (S_ISLNK(la->la_mode) &&
1534 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1535 buffer->lb_buf = ma->ma_lmm;
1536 /* eadatasize from client includes NULL-terminator, so
1537 * there is no need to read it */
1538 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1539 rc = mo_readlink(env, next, buffer);
1540 if (unlikely(rc <= 0)) {
1541 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1542 mdt_obd_name(info->mti_mdt),
1543 PFID(mdt_object_fid(o)), rc);
1546 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1548 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1550 repbody->mbo_valid |= OBD_MD_LINKNAME;
1551 /* we need to report back size with NULL-terminator
1552 * because client expects that */
1553 repbody->mbo_eadatasize = rc + 1;
1554 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1555 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1556 "on "DFID ", expected %d\n",
1557 mdt_obd_name(info->mti_mdt),
1558 rc, PFID(mdt_object_fid(o)),
1559 reqbody->mbo_eadatasize - 1);
1560 /* NULL terminate */
1561 ((char *)ma->ma_lmm)[rc] = 0;
1563 /* If the total CDEBUG() size is larger than a page, it
1564 * will print a warning to the console, avoid this by
1565 * printing just the last part of the symlink. */
1566 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1567 print_limit < rc ? "..." : "", print_limit,
1568 (char *)ma->ma_lmm + rc - print_limit, rc);
1573 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1574 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1575 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1576 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1577 repbody->mbo_max_mdsize);
1580 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1581 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1582 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1583 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1584 if (IS_ERR(nodemap))
1585 RETURN(PTR_ERR(nodemap));
1587 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1588 nodemap_putref(nodemap);
1594 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1595 ktime_us_delta(ktime_get(), kstart));
1600 static int mdt_getattr(struct tgt_session_info *tsi)
1602 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1603 struct mdt_object *obj = info->mti_object;
1604 struct req_capsule *pill = info->mti_pill;
1605 struct mdt_body *reqbody;
1606 struct mdt_body *repbody;
1610 if (unlikely(info->mti_object == NULL))
1613 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1615 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1617 /* Special case for Data-on-MDT files to get data version */
1618 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1619 rc = mdt_data_version_get(tsi);
1623 /* Unlike intent case where we need to pre-fill out buffers early on
1624 * in intent policy for ldlm reasons, here we can have a much better
1625 * guess at EA size by just reading it from disk.
1626 * Exceptions are readdir and (missing) directory striping */
1628 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1629 /* No easy way to know how long is the symlink, but it cannot
1630 * be more than PATH_MAX, so we allocate +1 */
1632 /* A special case for fs ROOT: getattr there might fetch
1633 * default EA for entire fs, not just for this dir!
1635 } else if (lu_fid_eq(mdt_object_fid(obj),
1636 &info->mti_mdt->mdt_md_root_fid) &&
1637 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1638 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1640 /* Should the default strping be bigger, mdt_fix_reply
1641 * will reallocate */
1642 rc = DEF_REP_MD_SIZE;
1644 /* Read the actual EA size from disk */
1645 rc = mdt_attr_get_eabuf_size(info, obj);
1649 GOTO(out, rc = err_serious(rc));
1651 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1653 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1654 * by default. If the target object has more ACL entries, then
1655 * enlarge the buffer when necessary. */
1656 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1657 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1658 mdt_preset_encctx_size(info);
1660 rc = req_capsule_server_pack(pill);
1661 if (unlikely(rc != 0))
1662 GOTO(out, rc = err_serious(rc));
1664 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1665 LASSERT(repbody != NULL);
1666 repbody->mbo_eadatasize = 0;
1667 repbody->mbo_aclsize = 0;
1669 rc = mdt_check_ucred(info);
1671 GOTO(out_shrink, rc);
1673 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1675 rc = mdt_getattr_internal(info, obj, 0);
1677 GOTO(out_shrink, rc);
1679 rc = mdt_pack_encctx_in_reply(info, obj);
1682 mdt_client_compatibility(info);
1683 rc2 = mdt_fix_reply(info);
1687 mdt_thread_info_fini(info);
1692 * Handler of layout intent RPC requiring the layout modification
1694 * \param[in] info thread environment
1695 * \param[in] obj object
1696 * \param[out] lhc object ldlm lock handle
1697 * \param[in] layout layout change descriptor
1699 * \retval 0 on success
1700 * \retval < 0 error code
1702 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1703 struct mdt_lock_handle *lhc,
1704 struct md_layout_change *layout)
1710 if (!mdt_object_exists(obj))
1713 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1716 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1721 rc = mdt_check_resent_lock(info, obj, lhc);
1727 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1729 /* take layout lock to prepare layout change */
1730 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1731 lockpart |= MDS_INODELOCK_UPDATE;
1733 mdt_lock_handle_init(lhc);
1734 mdt_lock_reg_init(lhc, LCK_EX);
1735 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1740 mutex_lock(&obj->mot_som_mutex);
1741 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1742 mutex_unlock(&obj->mot_som_mutex);
1745 mdt_object_unlock(info, obj, lhc, 1);
1751 * Exchange MOF_LOV_CREATED flags between two objects after a
1752 * layout swap. No assumption is made on whether o1 or o2 have
1753 * created objects or not.
1755 * \param[in,out] o1 First swap layout object
1756 * \param[in,out] o2 Second swap layout object
1758 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1760 unsigned int o1_lov_created = o1->mot_lov_created;
1762 mutex_lock(&o1->mot_lov_mutex);
1763 mutex_lock(&o2->mot_lov_mutex);
1765 o1->mot_lov_created = o2->mot_lov_created;
1766 o2->mot_lov_created = o1_lov_created;
1768 mutex_unlock(&o2->mot_lov_mutex);
1769 mutex_unlock(&o1->mot_lov_mutex);
1772 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1774 struct mdt_thread_info *info;
1775 struct ptlrpc_request *req = tgt_ses_req(tsi);
1776 struct obd_export *exp = req->rq_export;
1777 struct mdt_object *o1, *o2, *o;
1778 struct mdt_lock_handle *lh1, *lh2;
1779 struct mdc_swap_layouts *msl;
1783 /* client does not support layout lock, so layout swaping
1785 * FIXME: there is a problem for old clients which don't support
1786 * layout lock yet. If those clients have already opened the file
1787 * they won't be notified at all so that old layout may still be
1788 * used to do IO. This can be fixed after file release is landed by
1789 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1790 if (!exp_connect_layout(exp))
1791 RETURN(-EOPNOTSUPP);
1793 info = tsi2mdt_info(tsi);
1794 if (unlikely(info->mti_object == NULL))
1797 if (info->mti_dlm_req != NULL)
1798 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1800 o1 = info->mti_object;
1801 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1802 &info->mti_body->mbo_fid2);
1804 GOTO(out, rc = PTR_ERR(o));
1806 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1807 GOTO(put, rc = -ENOENT);
1809 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1810 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1816 /* permission check. Make sure the calling process having permission
1817 * to write both files. */
1818 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1823 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1828 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1830 GOTO(put, rc = -EPROTO);
1832 lh1 = &info->mti_lh[MDT_LH_NEW];
1833 mdt_lock_reg_init(lh1, LCK_EX);
1834 lh2 = &info->mti_lh[MDT_LH_OLD];
1835 mdt_lock_reg_init(lh2, LCK_EX);
1837 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1838 MDS_INODELOCK_XATTR);
1842 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1843 MDS_INODELOCK_XATTR);
1847 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1848 mdt_object_child(o2), msl->msl_flags);
1852 mdt_swap_lov_flag(o1, o2);
1855 mdt_object_unlock(info, o2, lh2, rc);
1857 mdt_object_unlock(info, o1, lh1, rc);
1859 mdt_object_put(info->mti_env, o);
1861 mdt_thread_info_fini(info);
1865 static int mdt_raw_lookup(struct mdt_thread_info *info,
1866 struct mdt_object *parent,
1867 const struct lu_name *lname)
1869 struct lu_fid *fid = &info->mti_tmp_fid1;
1870 struct mdt_body *repbody;
1871 bool is_dotdot = false;
1872 bool is_old_parent_stripe = false;
1873 bool is_new_parent_checked = false;
1878 LASSERT(!info->mti_cross_ref);
1879 /* Always allow to lookup ".." */
1880 if (lname->ln_namelen == 2 &&
1881 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1882 info->mti_spec.sp_permitted = 1;
1884 if (mdt_is_dir_stripe(info, parent) == 1)
1885 is_old_parent_stripe = true;
1888 mdt_object_get(info->mti_env, parent);
1890 /* Only got the fid of this obj by name */
1892 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1894 mdt_object_put(info->mti_env, parent);
1898 /* getattr_name("..") should return master object FID for striped dir */
1899 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1900 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1902 RETURN(PTR_ERR(parent));
1904 /* old client getattr_name("..") with stripe FID */
1905 if (unlikely(is_old_parent_stripe)) {
1906 is_old_parent_stripe = false;
1910 /* ".." may be a stripe */
1911 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1912 is_new_parent_checked = true;
1916 mdt_object_put(info->mti_env, parent);
1919 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1920 repbody->mbo_fid1 = *fid;
1921 repbody->mbo_valid = OBD_MD_FLID;
1927 * Find name matching hash
1929 * We search \a child LinkEA for a name whose hash matches \a lname
1930 * (it contains an encoded hash).
1932 * \param info mdt thread info
1933 * \param lname encoded hash to find
1934 * \param parent parent object
1935 * \param child object to search with LinkEA
1937 * \retval 1 match found
1938 * \retval 0 no match found
1939 * \retval -ev negative errno upon error
1941 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
1942 struct mdt_object *parent, struct mdt_object *child)
1944 /* Here, lname is an encoded hash of on-disk name, and
1945 * client is doing access without encryption key.
1946 * So we need to get LinkEA, check parent fid is correct and
1947 * compare name hash with the one in the request.
1949 struct lu_buf *buf = &info->mti_big_buf;
1950 struct lu_name name;
1952 struct linkea_data ldata = { NULL };
1953 struct link_ea_header *leh;
1954 struct link_ea_entry *lee;
1955 struct lu_buf link = { 0 };
1957 int reclen, count, rc;
1961 if (lname->ln_namelen < LLCRYPT_FNAME_DIGEST_SIZE)
1964 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1969 rc = mdt_links_read(info, child, &ldata);
1973 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
1976 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
1979 lee = (struct link_ea_entry *)(leh + 1);
1980 for (count = 0; count < leh->leh_reccount; count++) {
1981 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1982 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
1983 lu_buf_check_and_alloc(&link, name.ln_namelen);
1985 GOTO(out_match, rc = -ENOMEM);
1986 rc = critical_decode(name.ln_name, name.ln_namelen,
1989 if (memcmp(LLCRYPT_FNAME_DIGEST(link.lb_buf, rc),
1990 hash, LLCRYPT_FNAME_DIGEST_SIZE) == 0) {
1995 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1997 if (count == leh->leh_reccount)
2010 * UPDATE lock should be taken against parent, and be released before exit;
2011 * child_bits lock should be taken against child, and be returned back:
2012 * (1)normal request should release the child lock;
2013 * (2)intent request will grant the lock to client.
2015 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2016 struct mdt_lock_handle *lhc,
2018 struct ldlm_reply *ldlm_rep)
2020 struct ptlrpc_request *req = mdt_info_req(info);
2021 struct mdt_body *reqbody = NULL;
2022 struct mdt_object *parent = info->mti_object;
2023 struct mdt_object *child = NULL;
2024 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2025 struct lu_name *lname = NULL;
2026 struct mdt_lock_handle *lhp = NULL;
2027 struct ldlm_lock *lock;
2028 struct req_capsule *pill = info->mti_pill;
2036 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2037 LASSERT(ergo(is_resent,
2038 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2043 if (info->mti_cross_ref) {
2044 /* Only getattr on the child. Parent is on another node. */
2045 mdt_set_disposition(info, ldlm_rep,
2046 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2048 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
2050 PFID(mdt_object_fid(child)), ldlm_rep);
2052 rc = mdt_check_resent_lock(info, child, lhc);
2055 } else if (rc > 0) {
2056 mdt_lock_handle_init(lhc);
2057 mdt_lock_reg_init(lhc, LCK_PR);
2060 * Object's name entry is on another MDS, it will
2061 * request PERM lock only because LOOKUP lock is owned
2062 * by the MDS where name entry resides.
2064 * TODO: it should try layout lock too. - Jinshan
2066 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2067 MDS_INODELOCK_LAYOUT);
2068 child_bits |= MDS_INODELOCK_PERM;
2070 rc = mdt_object_lock(info, child, lhc, child_bits);
2075 /* Finally, we can get attr for child. */
2076 if (!mdt_object_exists(child)) {
2077 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2079 "remote object doesn't exist.");
2080 mdt_object_unlock(info, child, lhc, 1);
2084 rc = mdt_getattr_internal(info, child, 0);
2085 if (unlikely(rc != 0)) {
2086 mdt_object_unlock(info, child, lhc, 1);
2090 rc = mdt_pack_secctx_in_reply(info, child);
2092 mdt_object_unlock(info, child, lhc, 1);
2096 rc = mdt_pack_encctx_in_reply(info, child);
2098 mdt_object_unlock(info, child, lhc, 1);
2102 lname = &info->mti_name;
2103 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2105 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2106 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2107 if (unlikely(reqbody == NULL))
2108 RETURN(err_serious(-EPROTO));
2110 *child_fid = reqbody->mbo_fid2;
2111 if (unlikely(!fid_is_sane(child_fid)))
2112 RETURN(err_serious(-EINVAL));
2114 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2115 mdt_object_get(info->mti_env, parent);
2118 child = mdt_object_find(info->mti_env, info->mti_mdt,
2121 RETURN(PTR_ERR(child));
2124 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2126 PFID(mdt_object_fid(parent)),
2127 PFID(&reqbody->mbo_fid2), ldlm_rep);
2128 } else if (lu_name_is_valid(lname)) {
2129 if (mdt_object_remote(parent)) {
2130 CERROR("%s: parent "DFID" is on remote target\n",
2131 mdt_obd_name(info->mti_mdt),
2132 PFID(mdt_object_fid(parent)));
2136 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
2137 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
2138 PNAME(lname), ldlm_rep);
2140 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2141 if (unlikely(reqbody == NULL))
2142 RETURN(err_serious(-EPROTO));
2144 *child_fid = reqbody->mbo_fid2;
2145 if (unlikely(!fid_is_sane(child_fid)))
2146 RETURN(err_serious(-EINVAL));
2148 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2149 mdt_object_get(info->mti_env, parent);
2152 child = mdt_object_find(info->mti_env, info->mti_mdt,
2155 RETURN(PTR_ERR(child));
2158 if (mdt_object_remote(child)) {
2159 CERROR("%s: child "DFID" is on remote target\n",
2160 mdt_obd_name(info->mti_mdt),
2161 PFID(mdt_object_fid(child)));
2162 GOTO(out_child, rc = -EPROTO);
2165 /* don't fetch LOOKUP lock if it's remote object */
2166 rc = mdt_is_remote_object(info, parent, child);
2168 GOTO(out_child, rc);
2170 child_bits &= ~MDS_INODELOCK_LOOKUP;
2172 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2174 PFID(mdt_object_fid(parent)),
2175 PFID(&reqbody->mbo_fid2), ldlm_rep);
2178 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2180 if (unlikely(!mdt_object_exists(parent)) &&
2181 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2182 lu_name_is_valid(lname)) {
2183 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2185 "Parent doesn't exist!");
2186 GOTO(out_child, rc = -ESTALE);
2189 if (!child && is_resent) {
2190 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2192 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2193 * a resend case, however, it could be already destroyed
2194 * due to client eviction or a raced cancel RPC.
2196 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2197 lhc->mlh_reg_lh.cookie);
2200 fid_extract_from_res_name(child_fid,
2201 &lock->l_resource->lr_name);
2202 LDLM_LOCK_PUT(lock);
2203 child = mdt_object_find(info->mti_env, info->mti_mdt,
2206 RETURN(PTR_ERR(child));
2207 } else if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2208 lu_name_is_valid(lname)) {
2209 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2210 rc = mdt_raw_lookup(info, parent, lname);
2215 /* step 1: lock parent only if parent is a directory */
2216 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2217 lhp = &info->mti_lh[MDT_LH_PARENT];
2218 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2219 rc = mdt_object_lock(info, parent, lhp,
2220 MDS_INODELOCK_UPDATE);
2221 if (unlikely(rc != 0))
2225 /* step 2: lookup child's fid by name */
2226 fid_zero(child_fid);
2227 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2228 child_fid, &info->mti_spec);
2230 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2233 GOTO(unlock_parent, rc);
2235 child = mdt_object_find(info->mti_env, info->mti_mdt,
2237 if (unlikely(IS_ERR(child)))
2238 GOTO(unlock_parent, rc = PTR_ERR(child));
2241 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2243 /* step 3: lock child regardless if it is local or remote. */
2246 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2247 /* Here, lname is an encoded hash of on-disk name, and
2248 * client is doing access without encryption key.
2249 * So we need to compare name hash with the one in the request.
2251 if (!find_name_matching_hash(info, lname, parent,
2253 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2254 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2255 GOTO(out_child, rc = -ENOENT);
2259 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2260 if (!mdt_object_exists(child)) {
2261 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2263 "Object doesn't exist!");
2264 GOTO(out_child, rc = -ENOENT);
2267 rc = mdt_check_resent_lock(info, child, lhc);
2269 GOTO(out_child, rc);
2270 } else if (rc > 0) {
2271 mdt_lock_handle_init(lhc);
2272 mdt_lock_reg_init(lhc, LCK_PR);
2274 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2275 !mdt_object_remote(child)) {
2276 struct md_attr *ma = &info->mti_attr;
2279 ma->ma_need = MA_INODE;
2280 rc = mdt_attr_get_complex(info, child, ma);
2281 if (unlikely(rc != 0))
2282 GOTO(out_child, rc);
2284 /* If the file has not been changed for some time, we
2285 * return not only a LOOKUP lock, but also an UPDATE
2286 * lock and this might save us RPC on later STAT. For
2287 * directories, it also let negative dentry cache start
2288 * working for this dir. */
2289 if (ma->ma_valid & MA_INODE &&
2290 ma->ma_attr.la_valid & LA_CTIME &&
2291 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2292 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2293 child_bits |= MDS_INODELOCK_UPDATE;
2296 /* layout lock must be granted in a best-effort way
2297 * for IT operations */
2298 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2299 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2300 !mdt_object_remote(child) && ldlm_rep != NULL) {
2301 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2302 exp_connect_layout(info->mti_exp)) {
2303 /* try to grant layout lock for regular file. */
2304 try_bits = MDS_INODELOCK_LAYOUT;
2306 /* Acquire DOM lock in advance for data-on-mdt file */
2307 if (child != parent)
2308 try_bits |= MDS_INODELOCK_DOM;
2311 if (try_bits != 0) {
2312 /* try layout lock, it may fail to be granted due to
2313 * contention at LOOKUP or UPDATE */
2314 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2316 if (child_bits & MDS_INODELOCK_LAYOUT)
2319 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2320 * client will enqueue the lock to the remote MDT */
2321 if (mdt_object_remote(child))
2322 child_bits &= ~MDS_INODELOCK_UPDATE;
2323 rc = mdt_object_lock(info, child, lhc, child_bits);
2325 if (unlikely(rc != 0))
2326 GOTO(out_child, rc);
2329 /* finally, we can get attr for child. */
2330 rc = mdt_getattr_internal(info, child, ma_need);
2331 if (unlikely(rc != 0)) {
2333 mdt_object_unlock(info, child, lhc, 1);
2334 GOTO(out_child, rc);
2337 rc = mdt_pack_secctx_in_reply(info, child);
2340 mdt_object_unlock(info, child, lhc, 1);
2341 GOTO(out_child, rc);
2344 rc = mdt_pack_encctx_in_reply(info, child);
2347 mdt_object_unlock(info, child, lhc, 1);
2348 GOTO(out_child, rc);
2351 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2353 /* Debugging code. */
2354 LDLM_DEBUG(lock, "Returning lock to client");
2355 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2356 &lock->l_resource->lr_name),
2357 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2358 PLDLMRES(lock->l_resource),
2359 PFID(mdt_object_fid(child)));
2361 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2362 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2363 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2365 req->rq_arrival_time.tv_sec +
2367 /* Put the lock to the waiting list and force the cancel */
2368 ldlm_set_ast_sent(lock);
2371 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2372 !mdt_object_remote(child) && child != parent) {
2373 mdt_object_put(info->mti_env, child);
2374 rc = mdt_pack_size2body(info, child_fid,
2376 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2377 /* DOM lock was taken in advance but this is
2378 * not DoM file. Drop the lock.
2380 lock_res_and_lock(lock);
2381 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2382 unlock_res_and_lock(lock);
2384 LDLM_LOCK_PUT(lock);
2385 GOTO(unlock_parent, rc = 0);
2387 LDLM_LOCK_PUT(lock);
2393 mdt_object_put(info->mti_env, child);
2396 mdt_object_unlock(info, parent, lhp, 1);
2400 /* normal handler: should release the child lock */
2401 static int mdt_getattr_name(struct tgt_session_info *tsi)
2403 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2404 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2405 struct mdt_body *reqbody;
2406 struct mdt_body *repbody;
2411 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2412 LASSERT(reqbody != NULL);
2413 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2414 LASSERT(repbody != NULL);
2416 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2417 repbody->mbo_eadatasize = 0;
2418 repbody->mbo_aclsize = 0;
2420 rc = mdt_init_ucred(info, reqbody);
2422 GOTO(out_shrink, rc);
2424 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2425 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2426 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2427 lhc->mlh_reg_lh.cookie = 0;
2429 mdt_exit_ucred(info);
2432 mdt_client_compatibility(info);
2433 rc2 = mdt_fix_reply(info);
2436 mdt_thread_info_fini(info);
2440 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2441 const struct lu_fid *pfid,
2442 const struct lu_name *name,
2443 struct mdt_object *obj, s64 ctime)
2445 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2446 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2447 struct mdt_device *mdt = info->mti_mdt;
2448 struct md_attr *ma = &info->mti_attr;
2449 struct mdt_lock_handle *parent_lh;
2450 struct mdt_lock_handle *child_lh;
2451 struct mdt_object *pobj;
2452 bool cos_incompat = false;
2456 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2458 GOTO(out, rc = PTR_ERR(pobj));
2460 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2461 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2462 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2464 GOTO(put_parent, rc);
2466 if (mdt_object_remote(pobj))
2467 cos_incompat = true;
2469 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2470 name, child_fid, &info->mti_spec);
2472 GOTO(unlock_parent, rc);
2474 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2475 GOTO(unlock_parent, rc = -EREMCHG);
2477 child_lh = &info->mti_lh[MDT_LH_CHILD];
2478 mdt_lock_reg_init(child_lh, LCK_EX);
2479 rc = mdt_reint_striped_lock(info, obj, child_lh,
2480 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2481 einfo, cos_incompat);
2483 GOTO(unlock_parent, rc);
2485 if (atomic_read(&obj->mot_open_count)) {
2486 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2487 PFID(mdt_object_fid(obj)));
2488 GOTO(unlock_child, rc = -EBUSY);
2492 ma->ma_valid = MA_INODE;
2493 ma->ma_attr.la_valid = LA_CTIME;
2494 ma->ma_attr.la_ctime = ctime;
2496 mutex_lock(&obj->mot_lov_mutex);
2498 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2499 mdt_object_child(obj), name, ma, 0);
2501 mutex_unlock(&obj->mot_lov_mutex);
2504 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2506 mdt_object_unlock(info, pobj, parent_lh, 1);
2508 mdt_object_put(info->mti_env, pobj);
2513 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2514 struct mdt_object *obj)
2516 struct lu_ucred *uc = lu_ucred(info->mti_env);
2517 struct md_attr *ma = &info->mti_attr;
2518 struct lu_attr *la = &ma->ma_attr;
2522 ma->ma_need = MA_INODE;
2523 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2527 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2530 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2532 if (uc->uc_fsuid == la->la_uid) {
2533 if ((la->la_mode & S_IWUSR) == 0)
2535 } else if (uc->uc_fsgid == la->la_gid) {
2536 if ((la->la_mode & S_IWGRP) == 0)
2538 } else if ((la->la_mode & S_IWOTH) == 0) {
2546 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2549 struct mdt_device *mdt = info->mti_mdt;
2550 struct mdt_object *obj = NULL;
2551 struct linkea_data ldata = { NULL };
2552 struct lu_buf *buf = &info->mti_big_buf;
2553 struct lu_name *name = &info->mti_name;
2554 struct lu_fid *pfid = &info->mti_tmp_fid1;
2555 struct link_ea_header *leh;
2556 struct link_ea_entry *lee;
2557 int reclen, count, rc = 0;
2560 if (!fid_is_sane(fid))
2561 GOTO(out, rc = -EINVAL);
2563 if (!fid_is_namespace_visible(fid))
2564 GOTO(out, rc = -EINVAL);
2566 obj = mdt_object_find(info->mti_env, mdt, fid);
2568 GOTO(out, rc = PTR_ERR(obj));
2570 if (mdt_object_remote(obj))
2571 GOTO(out, rc = -EREMOTE);
2572 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2573 GOTO(out, rc = -ENOENT);
2575 rc = mdt_rmfid_check_permission(info, obj);
2580 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2582 GOTO(out, rc = -ENOMEM);
2585 rc = mdt_links_read(info, obj, &ldata);
2590 lee = (struct link_ea_entry *)(leh + 1);
2591 for (count = 0; count < leh->leh_reccount; count++) {
2592 /* remove every hardlink */
2593 linkea_entry_unpack(lee, &reclen, name, pfid);
2594 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2595 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2601 if (obj && !IS_ERR(obj))
2602 mdt_object_put(info->mti_env, obj);
2603 if (info->mti_big_buf.lb_buf)
2604 lu_buf_free(&info->mti_big_buf);
2609 static int mdt_rmfid(struct tgt_session_info *tsi)
2611 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2612 struct mdt_body *reqbody;
2613 struct lu_fid *fids, *rfids;
2619 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2620 if (reqbody == NULL)
2622 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2624 nr = bufsize / sizeof(struct lu_fid);
2625 if (nr * sizeof(struct lu_fid) != bufsize)
2627 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2628 RCL_SERVER, nr * sizeof(__u32));
2629 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2630 RCL_SERVER, nr * sizeof(struct lu_fid));
2631 rc = req_capsule_server_pack(tsi->tsi_pill);
2633 GOTO(out, rc = err_serious(rc));
2634 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2637 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2639 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2642 mdt_init_ucred(mti, reqbody);
2643 for (i = 0; i < nr; i++) {
2645 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2647 mdt_exit_ucred(mti);
2653 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2654 void *karg, void __user *uarg);
2656 int mdt_io_set_info(struct tgt_session_info *tsi)
2658 struct ptlrpc_request *req = tgt_ses_req(tsi);
2659 struct ost_body *body = NULL, *repbody;
2660 void *key, *val = NULL;
2661 int keylen, vallen, rc = 0;
2662 bool is_grant_shrink;
2666 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2668 DEBUG_REQ(D_HA, req, "no set_info key");
2669 RETURN(err_serious(-EFAULT));
2671 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2674 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2676 DEBUG_REQ(D_HA, req, "no set_info val");
2677 RETURN(err_serious(-EFAULT));
2679 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2682 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
2683 if (is_grant_shrink)
2684 /* In this case the value is actually an RMF_OST_BODY, so we
2685 * transmutate the type of this PTLRPC */
2686 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
2688 rc = req_capsule_server_pack(tsi->tsi_pill);
2692 if (is_grant_shrink) {
2693 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
2695 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2698 /** handle grant shrink, similar to a read request */
2699 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
2702 CERROR("%s: Unsupported key %s\n",
2703 tgt_name(tsi->tsi_tgt), (char *)key);
2711 static int mdt_set_info(struct tgt_session_info *tsi)
2713 struct ptlrpc_request *req = tgt_ses_req(tsi);
2716 int keylen, vallen, rc = 0;
2720 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2722 DEBUG_REQ(D_HA, req, "no set_info key");
2723 RETURN(err_serious(-EFAULT));
2726 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2729 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2731 DEBUG_REQ(D_HA, req, "no set_info val");
2732 RETURN(err_serious(-EFAULT));
2735 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2738 /* Swab any part of val you need to here */
2739 if (KEY_IS(KEY_READ_ONLY)) {
2740 spin_lock(&req->rq_export->exp_lock);
2742 *exp_connect_flags_ptr(req->rq_export) |=
2745 *exp_connect_flags_ptr(req->rq_export) &=
2746 ~OBD_CONNECT_RDONLY;
2747 spin_unlock(&req->rq_export->exp_lock);
2748 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2749 struct changelog_setinfo *cs = val;
2751 if (vallen != sizeof(*cs)) {
2752 CERROR("%s: bad changelog_clear setinfo size %d\n",
2753 tgt_name(tsi->tsi_tgt), vallen);
2756 if (req_capsule_req_need_swab(&req->rq_pill)) {
2757 __swab64s(&cs->cs_recno);
2758 __swab32s(&cs->cs_id);
2761 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2763 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2765 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2767 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2774 static int mdt_readpage(struct tgt_session_info *tsi)
2776 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2777 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2778 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2779 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2780 struct mdt_body *repbody;
2786 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2787 RETURN(err_serious(-ENOMEM));
2789 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2790 if (repbody == NULL || reqbody == NULL)
2791 RETURN(err_serious(-EFAULT));
2794 * prepare @rdpg before calling lower layers and transfer itself. Here
2795 * reqbody->size contains offset of where to start to read and
2796 * reqbody->nlink contains number bytes to read.
2798 rdpg->rp_hash = reqbody->mbo_size;
2799 if (rdpg->rp_hash != reqbody->mbo_size) {
2800 CERROR("Invalid hash: %#llx != %#llx\n",
2801 rdpg->rp_hash, reqbody->mbo_size);
2805 rdpg->rp_attrs = reqbody->mbo_mode;
2806 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2807 rdpg->rp_attrs |= LUDA_64BITHASH;
2808 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2809 exp_max_brw_size(tsi->tsi_exp));
2810 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2812 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2813 if (rdpg->rp_pages == NULL)
2816 for (i = 0; i < rdpg->rp_npages; ++i) {
2817 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2818 if (rdpg->rp_pages[i] == NULL)
2819 GOTO(free_rdpg, rc = -ENOMEM);
2822 /* call lower layers to fill allocated pages with directory data */
2823 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2825 GOTO(free_rdpg, rc);
2827 /* send pages to client */
2828 rc = tgt_sendpage(tsi, rdpg, rc);
2833 for (i = 0; i < rdpg->rp_npages; i++)
2834 if (rdpg->rp_pages[i] != NULL)
2835 __free_page(rdpg->rp_pages[i]);
2836 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2838 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2844 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2846 struct lu_ucred *uc = mdt_ucred_check(info);
2847 struct lu_attr *attr = &info->mti_attr.ma_attr;
2852 if (op != REINT_SETATTR) {
2853 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2854 attr->la_uid = uc->uc_fsuid;
2855 /* for S_ISGID, inherit gid from his parent, such work will be
2856 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2857 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2858 attr->la_gid = uc->uc_fsgid;
2864 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2866 return op == REINT_OPEN &&
2867 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2870 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2872 struct req_capsule *pill = info->mti_pill;
2874 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2876 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2878 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2880 /* pre-set size in server part with max size */
2881 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2883 OBD_MAX_DEFAULT_EA_SIZE);
2885 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2890 static int mdt_reint_internal(struct mdt_thread_info *info,
2891 struct mdt_lock_handle *lhc,
2894 struct req_capsule *pill = info->mti_pill;
2895 struct mdt_body *repbody;
2900 rc = mdt_reint_unpack(info, op);
2902 CERROR("Can't unpack reint, rc %d\n", rc);
2903 RETURN(err_serious(rc));
2907 /* check if the file system is set to readonly. O_RDONLY open
2908 * is still allowed even the file system is set to readonly mode */
2909 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2910 RETURN(err_serious(-EROFS));
2912 /* for replay (no_create) lmm is not needed, client has it already */
2913 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2914 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2917 /* llog cookies are always 0, the field is kept for compatibility */
2918 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2919 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2921 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2922 * by default. If the target object has more ACL entries, then
2923 * enlarge the buffer when necessary. */
2924 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2925 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2926 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2928 mdt_preset_secctx_size(info);
2929 mdt_preset_encctx_size(info);
2931 rc = req_capsule_server_pack(pill);
2933 CERROR("Can't pack response, rc %d\n", rc);
2934 RETURN(err_serious(rc));
2937 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2938 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2940 repbody->mbo_eadatasize = 0;
2941 repbody->mbo_aclsize = 0;
2944 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2946 /* for replay no cookkie / lmm need, because client have this already */
2947 if (info->mti_spec.no_create)
2948 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2949 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2951 rc = mdt_init_ucred_reint(info);
2953 GOTO(out_shrink, rc);
2955 rc = mdt_fix_attr_ucred(info, op);
2957 GOTO(out_ucred, rc = err_serious(rc));
2959 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2961 GOTO(out_ucred, rc);
2962 } else if (rc == 1) {
2963 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2964 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2965 GOTO(out_ucred, rc);
2967 rc = mdt_reint_rec(info, lhc);
2970 mdt_exit_ucred(info);
2972 mdt_client_compatibility(info);
2974 rc2 = mdt_fix_reply(info);
2979 * Data-on-MDT optimization - read data along with OPEN and return it
2980 * in reply when possible.
2982 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2983 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2989 static long mdt_reint_opcode(struct ptlrpc_request *req,
2990 const struct req_format **fmt)
2992 struct mdt_device *mdt;
2993 struct mdt_rec_reint *rec;
2996 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2998 opc = rec->rr_opcode;
2999 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
3000 if (opc < REINT_MAX && fmt[opc] != NULL)
3001 req_capsule_extend(&req->rq_pill, fmt[opc]);
3003 mdt = mdt_exp2dev(req->rq_export);
3004 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
3005 " rc = %d\n", req->rq_export->exp_obd->obd_name,
3006 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
3007 opc = err_serious(-EFAULT);
3010 opc = err_serious(-EFAULT);
3015 static int mdt_reint(struct tgt_session_info *tsi)
3019 static const struct req_format *reint_fmts[REINT_MAX] = {
3020 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
3021 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
3022 [REINT_LINK] = &RQF_MDS_REINT_LINK,
3023 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
3024 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
3025 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
3026 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
3027 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
3028 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
3029 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
3034 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
3036 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3038 * No lock possible here from client to pass it to reint code
3041 rc = mdt_reint_internal(info, NULL, opc);
3042 mdt_thread_info_fini(info);
3047 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
3051 /* this should sync the whole device */
3052 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
3054 struct dt_device *dt = mdt->mdt_bottom;
3058 rc = dt->dd_ops->dt_sync(env, dt);
3062 /* this should sync this object */
3063 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
3064 struct mdt_object *mo)
3070 if (!mdt_object_exists(mo)) {
3071 CWARN("%s: non existing object "DFID": rc = %d\n",
3072 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3077 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3078 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3079 dt_obj_version_t version;
3081 version = dt_version_get(env, mdt_obj2dt(mo));
3082 if (version > tgt->lut_obd->obd_last_committed)
3083 rc = mo_object_sync(env, mdt_object_child(mo));
3085 rc = mo_object_sync(env, mdt_object_child(mo));
3091 static int mdt_sync(struct tgt_session_info *tsi)
3093 struct ptlrpc_request *req = tgt_ses_req(tsi);
3094 struct req_capsule *pill = tsi->tsi_pill;
3095 struct mdt_body *body;
3096 ktime_t kstart = ktime_get();
3101 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3102 RETURN(err_serious(-ENOMEM));
3104 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3105 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3107 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3109 if (unlikely(info->mti_object == NULL))
3112 /* sync an object */
3113 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3116 const struct lu_fid *fid;
3117 struct lu_attr *la = &info->mti_attr.ma_attr;
3119 info->mti_attr.ma_need = MA_INODE;
3120 info->mti_attr.ma_valid = 0;
3121 rc = mdt_attr_get_complex(info, info->mti_object,
3124 body = req_capsule_server_get(pill,
3126 fid = mdt_object_fid(info->mti_object);
3127 mdt_pack_attr2body(info, body, la, fid);
3130 mdt_thread_info_fini(info);
3133 mdt_counter_incr(req, LPROC_MDT_SYNC,
3134 ktime_us_delta(ktime_get(), kstart));
3139 static int mdt_data_sync(struct tgt_session_info *tsi)
3141 struct mdt_thread_info *info;
3142 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3143 struct ost_body *body = tsi->tsi_ost_body;
3144 struct ost_body *repbody;
3145 struct mdt_object *mo = NULL;
3151 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3153 /* if no fid is specified then do nothing,
3154 * device sync is done via MDS_SYNC */
3155 if (fid_is_zero(&tsi->tsi_fid))
3158 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3160 RETURN(PTR_ERR(mo));
3162 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3166 repbody->oa.o_oi = body->oa.o_oi;
3167 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3169 info = tsi2mdt_info(tsi);
3170 ma = &info->mti_attr;
3171 ma->ma_need = MA_INODE;
3173 rc = mdt_attr_get_complex(info, mo, ma);
3175 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3178 mdt_thread_info_fini(info);
3183 mdt_object_put(tsi->tsi_env, mo);
3188 * Handle quota control requests to consult current usage/limit, but also
3189 * to configure quota enforcement
3191 static int mdt_quotactl(struct tgt_session_info *tsi)
3193 struct obd_export *exp = tsi->tsi_exp;
3194 struct req_capsule *pill = tsi->tsi_pill;
3195 struct obd_quotactl *oqctl, *repoqc;
3197 struct mdt_device *mdt = mdt_exp2dev(exp);
3198 struct lu_device *qmt = mdt->mdt_qmt_dev;
3199 struct lu_nodemap *nodemap;
3202 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3204 RETURN(err_serious(-EPROTO));
3206 rc = req_capsule_server_pack(pill);
3208 RETURN(err_serious(rc));
3210 nodemap = nodemap_get_from_exp(exp);
3211 if (IS_ERR(nodemap))
3212 RETURN(PTR_ERR(nodemap));
3214 switch (oqctl->qc_cmd) {
3215 /* master quotactl */
3218 case LUSTRE_Q_SETDEFAULT:
3219 case LUSTRE_Q_SETQUOTAPOOL:
3220 case LUSTRE_Q_SETINFOPOOL:
3221 case LUSTRE_Q_SETDEFAULT_POOL:
3222 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3224 GOTO(out_nodemap, rc = -EPERM);
3228 case LUSTRE_Q_GETDEFAULT:
3229 case LUSTRE_Q_GETQUOTAPOOL:
3230 case LUSTRE_Q_GETINFOPOOL:
3231 case LUSTRE_Q_GETDEFAULT_POOL:
3233 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3234 /* slave quotactl */
3241 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3242 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3243 GOTO(out_nodemap, rc);
3247 switch (oqctl->qc_type) {
3249 id = nodemap_map_id(nodemap, NODEMAP_UID,
3250 NODEMAP_CLIENT_TO_FS, id);
3253 id = nodemap_map_id(nodemap, NODEMAP_GID,
3254 NODEMAP_CLIENT_TO_FS, id);
3257 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3258 NODEMAP_CLIENT_TO_FS, id);
3261 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3263 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3265 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3267 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3268 barrier_exit(tsi->tsi_tgt->lut_bottom);
3270 if (oqctl->qc_id != id)
3271 swap(oqctl->qc_id, id);
3273 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3274 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3275 RETURN(-EINPROGRESS);
3278 switch (oqctl->qc_cmd) {
3284 case LUSTRE_Q_SETDEFAULT:
3285 case LUSTRE_Q_GETDEFAULT:
3286 case LUSTRE_Q_SETQUOTAPOOL:
3287 case LUSTRE_Q_GETQUOTAPOOL:
3288 case LUSTRE_Q_SETINFOPOOL:
3289 case LUSTRE_Q_GETINFOPOOL:
3290 case LUSTRE_Q_SETDEFAULT_POOL:
3291 case LUSTRE_Q_GETDEFAULT_POOL:
3292 /* forward quotactl request to QMT */
3293 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3298 /* slave quotactl */
3299 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3304 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3305 GOTO(out_nodemap, rc = -EFAULT);
3308 if (oqctl->qc_id != id)
3309 swap(oqctl->qc_id, id);
3311 QCTL_COPY(repoqc, oqctl);
3315 nodemap_putref(nodemap);
3320 /** clone llog ctxt from child (mdd)
3321 * This allows remote llog (replicator) access.
3322 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3323 * context was originally set up, or we can handle them directly.
3324 * I choose the latter, but that means I need any llog
3325 * contexts set up by child to be accessable by the mdt. So we clone the
3326 * context into our context list here.
3328 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3331 struct md_device *next = mdt->mdt_child;
3332 struct llog_ctxt *ctxt;
3335 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3338 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3339 if (rc || ctxt == NULL) {
3343 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3345 CERROR("Can't set mdt ctxt %d\n", rc);
3350 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3351 struct mdt_device *mdt, int idx)
3353 struct llog_ctxt *ctxt;
3355 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3358 /* Put once for the get we just did, and once for the clone */
3359 llog_ctxt_put(ctxt);
3360 llog_ctxt_put(ctxt);
3365 * sec context handlers
3367 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3369 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3375 * quota request handlers
3377 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3379 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3380 struct lu_device *qmt = mdt->mdt_qmt_dev;
3385 RETURN(err_serious(-EOPNOTSUPP));
3387 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3391 struct mdt_object *mdt_object_new(const struct lu_env *env,
3392 struct mdt_device *d,
3393 const struct lu_fid *f)
3395 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3396 struct lu_object *o;
3397 struct mdt_object *m;
3400 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3401 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3402 if (unlikely(IS_ERR(o)))
3403 m = (struct mdt_object *)o;
3409 struct mdt_object *mdt_object_find(const struct lu_env *env,
3410 struct mdt_device *d,
3411 const struct lu_fid *f)
3413 struct lu_object *o;
3414 struct mdt_object *m;
3417 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3418 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3419 if (unlikely(IS_ERR(o)))
3420 m = (struct mdt_object *)o;
3428 * Asyncronous commit for mdt device.
3430 * Pass asynchonous commit call down the MDS stack.
3432 * \param env environment
3433 * \param mdt the mdt device
3435 static void mdt_device_commit_async(const struct lu_env *env,
3436 struct mdt_device *mdt)
3438 struct dt_device *dt = mdt->mdt_bottom;
3442 rc = dt->dd_ops->dt_commit_async(env, dt);
3443 if (unlikely(rc != 0))
3444 CWARN("%s: async commit start failed: rc = %d\n",
3445 mdt_obd_name(mdt), rc);
3446 atomic_inc(&mdt->mdt_async_commit_count);
3451 * Mark the lock as "synchonous".
3453 * Mark the lock to deffer transaction commit to the unlock time.
3455 * \param lock the lock to mark as "synchonous"
3457 * \see mdt_is_lock_sync
3458 * \see mdt_save_lock
3460 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3462 lock->l_ast_data = (void*)1;
3466 * Check whehter the lock "synchonous" or not.
3468 * \param lock the lock to check
3469 * \retval 1 the lock is "synchonous"
3470 * \retval 0 the lock isn't "synchronous"
3472 * \see mdt_set_lock_sync
3473 * \see mdt_save_lock
3475 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3477 return lock->l_ast_data != NULL;
3481 * Blocking AST for mdt locks.
3483 * Starts transaction commit if in case of COS lock conflict or
3484 * deffers such a commit to the mdt_save_lock.
3486 * \param lock the lock which blocks a request or cancelling lock
3487 * \param desc unused
3488 * \param data unused
3489 * \param flag indicates whether this cancelling or blocking callback
3491 * \see ldlm_blocking_ast_nocheck
3493 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3494 void *data, int flag)
3496 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3497 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3498 struct ldlm_cb_set_arg *arg = data;
3499 bool commit_async = false;
3503 if (flag == LDLM_CB_CANCELING)
3506 lock_res_and_lock(lock);
3507 if (lock->l_blocking_ast != mdt_blocking_ast) {
3508 unlock_res_and_lock(lock);
3512 /* A blocking ast may be sent from ldlm_lock_decref_internal
3513 * when the last reference to a local lock was released and
3514 * during blocking event from ldlm_work_bl_ast_lock().
3515 * The 'data' parameter is l_ast_data in the first case and
3516 * callback arguments in the second one. Distinguish them by that.
3518 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3519 goto skip_cos_checks;
3521 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3522 if (mdt_cos_is_enabled(mdt)) {
3523 if (!arg->bl_desc->bl_same_client)
3524 mdt_set_lock_sync(lock);
3525 } else if (mdt_slc_is_enabled(mdt) &&
3526 arg->bl_desc->bl_cos_incompat) {
3527 mdt_set_lock_sync(lock);
3529 * we may do extra commit here, but there is a small
3530 * window to miss a commit: lock was unlocked (saved),
3531 * then a conflict lock queued and we come here, but
3532 * REP-ACK not received, so lock was not converted to
3534 * Fortunately this window is quite small, so the
3535 * extra commit should be rare (not to say distributed
3536 * operation is rare too).
3538 commit_async = true;
3540 } else if (lock->l_req_mode == LCK_COS) {
3541 commit_async = true;
3545 rc = ldlm_blocking_ast_nocheck(lock);
3550 rc = lu_env_init(&env, LCT_LOCAL);
3551 if (unlikely(rc != 0))
3552 CWARN("%s: lu_env initialization failed, cannot "
3553 "start asynchronous commit: rc = %d\n",
3556 mdt_device_commit_async(&env, mdt);
3563 * Blocking AST for cross-MDT lock
3565 * Discard lock from uncommitted_slc_locks and cancel it.
3567 * \param lock the lock which blocks a request or cancelling lock
3568 * \param desc unused
3569 * \param data unused
3570 * \param flag indicates whether this cancelling or blocking callback
3571 * \retval 0 on success
3572 * \retval negative number on error
3574 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3575 void *data, int flag)
3581 case LDLM_CB_BLOCKING: {
3582 struct lustre_handle lockh;
3584 ldlm_lock2handle(lock, &lockh);
3585 rc = ldlm_cli_cancel(&lockh,
3586 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3588 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3593 case LDLM_CB_CANCELING: {
3594 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3595 struct mdt_device *mdt =
3596 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3598 LDLM_DEBUG(lock, "Revoke remote lock\n");
3600 /* discard slc lock here so that it can be cleaned anytime,
3601 * especially for cleanup_resource() */
3602 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3604 /* once we cache lock, l_ast_data is set to mdt_object */
3605 if (lock->l_ast_data != NULL) {
3606 struct mdt_object *mo = lock->l_ast_data;
3609 rc = lu_env_init(&env, LCT_MD_THREAD);
3610 if (unlikely(rc != 0)) {
3611 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3613 PFID(mdt_object_fid(mo)), rc);
3617 if (lock->l_policy_data.l_inodebits.bits &
3618 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3619 rc = mo_invalidate(&env, mdt_object_child(mo));
3620 mo->mot_cache_attr = 0;
3622 mdt_object_put(&env, mo);
3634 int mdt_check_resent_lock(struct mdt_thread_info *info,
3635 struct mdt_object *mo,
3636 struct mdt_lock_handle *lhc)
3638 /* the lock might already be gotten in ldlm_handle_enqueue() */
3639 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3640 struct ptlrpc_request *req = mdt_info_req(info);
3641 struct ldlm_lock *lock;
3643 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3644 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3646 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3647 * a resend case, however, it could be already destroyed
3648 * due to client eviction or a raced cancel RPC. */
3649 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3650 lhc->mlh_reg_lh.cookie);
3654 if (!fid_res_name_eq(mdt_object_fid(mo),
3655 &lock->l_resource->lr_name)) {
3656 CWARN("%s: Although resent, but still not "
3657 "get child lock:"DFID"\n",
3658 info->mti_exp->exp_obd->obd_name,
3659 PFID(mdt_object_fid(mo)));
3660 LDLM_LOCK_PUT(lock);
3663 LDLM_LOCK_PUT(lock);
3669 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3671 mdt_object_get(NULL, lock->l_ast_data);
3674 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3675 struct mdt_object *o, const struct lu_fid *fid,
3676 struct lustre_handle *lh, enum ldlm_mode mode,
3677 __u64 *ibits, __u64 trybits, bool cache)
3679 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3680 union ldlm_policy_data *policy = &mti->mti_policy;
3681 struct ldlm_res_id *res_id = &mti->mti_res_id;
3685 LASSERT(mdt_object_remote(o));
3687 fid_build_reg_res_name(fid, res_id);
3689 memset(einfo, 0, sizeof(*einfo));
3690 einfo->ei_type = LDLM_IBITS;
3691 einfo->ei_mode = mode;
3692 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3693 einfo->ei_cb_cp = ldlm_completion_ast;
3694 einfo->ei_enq_slave = 0;
3695 einfo->ei_res_id = res_id;
3696 einfo->ei_req_slot = 1;
3700 * if we cache lock, couple lock with mdt_object, so that object
3701 * can be easily found in lock ASTs.
3703 einfo->ei_cbdata = o;
3704 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3707 memset(policy, 0, sizeof(*policy));
3708 policy->l_inodebits.bits = *ibits;
3709 policy->l_inodebits.try_bits = trybits;
3711 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3714 /* Return successfully acquired bits to a caller */
3716 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3719 *ibits = lock->l_policy_data.l_inodebits.bits;
3720 LDLM_LOCK_PUT(lock);
3725 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3726 const struct lu_fid *fid, struct lustre_handle *lh,
3727 enum ldlm_mode mode, __u64 ibits, bool cache)
3729 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3733 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3734 struct mdt_lock_handle *lh, __u64 *ibits,
3735 __u64 trybits, bool cos_incompat)
3737 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3738 union ldlm_policy_data *policy = &info->mti_policy;
3739 struct ldlm_res_id *res_id = &info->mti_res_id;
3740 __u64 dlmflags = 0, *cookie = NULL;
3744 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3745 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3746 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3747 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3750 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3751 lh->mlh_reg_mode == LCK_EX);
3752 dlmflags |= LDLM_FL_COS_INCOMPAT;
3753 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3754 dlmflags |= LDLM_FL_COS_ENABLED;
3757 /* Only enqueue LOOKUP lock for remote object */
3758 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3760 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3761 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3762 *ibits == MDS_INODELOCK_OPEN)
3763 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3765 if (lh->mlh_type == MDT_PDO_LOCK) {
3766 /* check for exists after object is locked */
3767 if (mdt_object_exists(o) == 0) {
3768 /* Non-existent object shouldn't have PDO lock */
3771 /* Non-dir object shouldn't have PDO lock */
3772 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3777 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3778 dlmflags |= LDLM_FL_ATOMIC_CB;
3781 cookie = &info->mti_exp->exp_handle.h_cookie;
3784 * Take PDO lock on whole directory and build correct @res_id for lock
3785 * on part of directory.
3787 if (lh->mlh_pdo_hash != 0) {
3788 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3789 mdt_lock_pdo_mode(info, o, lh);
3790 if (lh->mlh_pdo_mode != LCK_NL) {
3792 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3793 * is never going to be sent to client and we do not
3794 * want it slowed down due to possible cancels.
3796 policy->l_inodebits.bits =
3797 *ibits & MDS_INODELOCK_UPDATE;
3798 policy->l_inodebits.try_bits =
3799 trybits & MDS_INODELOCK_UPDATE;
3800 /* at least one of them should be set */
3801 LASSERT(policy->l_inodebits.bits |
3802 policy->l_inodebits.try_bits);
3803 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3804 lh->mlh_pdo_mode, policy, res_id,
3806 if (unlikely(rc != 0))
3807 GOTO(out_unlock, rc);
3811 * Finish res_id initializing by name hash marking part of
3812 * directory which is taking modification.
3814 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3817 policy->l_inodebits.bits = *ibits;
3818 policy->l_inodebits.try_bits = trybits;
3819 policy->l_inodebits.li_gid = lh->mlh_gid;
3822 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3823 * going to be sent to client. If it is - mdt_intent_policy() path will
3824 * fix it up and turn FL_LOCAL flag off.
3826 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3827 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3831 mdt_object_unlock(info, o, lh, 1);
3832 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3833 lh->mlh_pdo_hash != 0 &&
3834 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3835 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3837 /* Return successfully acquired bits to a caller */
3839 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3842 *ibits = lock->l_policy_data.l_inodebits.bits;
3843 LDLM_LOCK_PUT(lock);
3849 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3850 struct mdt_lock_handle *lh, __u64 *ibits,
3851 __u64 trybits, bool cos_incompat)
3853 struct mdt_lock_handle *local_lh = NULL;
3857 if (!mdt_object_remote(o)) {
3858 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3863 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3864 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3865 MDS_INODELOCK_XATTR);
3867 /* Only enqueue LOOKUP lock for remote object */
3868 if (*ibits & MDS_INODELOCK_LOOKUP) {
3869 __u64 local = MDS_INODELOCK_LOOKUP;
3871 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3879 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3880 /* Sigh, PDO needs to enqueue 2 locks right now, but
3881 * enqueue RPC can only request 1 lock, to avoid extra
3882 * RPC, so it will instead enqueue EX lock for remote
3883 * object anyway XXX*/
3884 if (lh->mlh_type == MDT_PDO_LOCK &&
3885 lh->mlh_pdo_hash != 0) {
3887 "%s: "DFID" convert PDO lock to EX lock.\n",
3888 mdt_obd_name(info->mti_mdt),
3889 PFID(mdt_object_fid(o)));
3890 lh->mlh_pdo_hash = 0;
3891 lh->mlh_rreg_mode = LCK_EX;
3892 lh->mlh_type = MDT_REG_LOCK;
3895 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3898 ibits, trybits, false);
3899 if (rc != ELDLM_OK) {
3900 if (local_lh != NULL)
3901 mdt_object_unlock(info, o, local_lh, rc);
3906 /* other components like LFSCK can use lockless access
3907 * and populate cache, so we better invalidate it */
3908 mo_invalidate(info->mti_env, mdt_object_child(o));
3913 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3914 struct mdt_lock_handle *lh, __u64 ibits)
3916 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3919 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3920 struct mdt_lock_handle *lh, __u64 ibits,
3923 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3924 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3928 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3929 struct mdt_lock_handle *lh, __u64 *ibits,
3930 __u64 trybits, bool cos_incompat)
3932 bool trylock_only = *ibits == 0;
3935 LASSERT(!(*ibits & trybits));
3936 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3938 if (rc && trylock_only) { /* clear error for try ibits lock only */
3939 LASSERT(*ibits == 0);
3946 * Save a lock within request object.
3948 * Keep the lock referenced until whether client ACK or transaction
3949 * commit happens or release the lock immediately depending on input
3950 * parameters. If COS is ON, a write lock is converted to COS lock
3953 * \param info thead info object
3954 * \param h lock handle
3955 * \param mode lock mode
3956 * \param decref force immediate lock releasing
3958 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3959 enum ldlm_mode mode, int decref)
3963 if (lustre_handle_is_used(h)) {
3964 if (decref || !info->mti_has_trans ||
3965 !(mode & (LCK_PW | LCK_EX))) {
3966 mdt_fid_unlock(h, mode);
3968 struct mdt_device *mdt = info->mti_mdt;
3969 struct ldlm_lock *lock = ldlm_handle2lock(h);
3970 struct ptlrpc_request *req = mdt_info_req(info);
3971 bool cos = mdt_cos_is_enabled(mdt);
3972 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3974 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3977 /* there is no request if mdt_object_unlock() is called
3978 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3979 if (likely(req != NULL)) {
3980 LDLM_DEBUG(lock, "save lock request %p reply "
3981 "state %p transno %lld\n", req,
3982 req->rq_reply_state, req->rq_transno);
3984 ldlm_lock_mode_downgrade(lock, LCK_COS);
3987 if (req->rq_export->exp_disconnected)
3988 mdt_fid_unlock(h, mode);
3990 ptlrpc_save_lock(req, h, mode, cos,
3993 mdt_fid_unlock(h, mode);
3995 if (mdt_is_lock_sync(lock)) {
3996 CDEBUG(D_HA, "found sync-lock,"
3997 " async commit started\n");
3998 mdt_device_commit_async(info->mti_env,
4001 LDLM_LOCK_PUT(lock);
4010 * Save cross-MDT lock in uncommitted_slc_locks
4012 * Keep the lock referenced until transaction commit happens or release the lock
4013 * immediately depending on input parameters.
4015 * \param info thead info object
4016 * \param h lock handle
4017 * \param mode lock mode
4018 * \param decref force immediate lock releasing
4020 static void mdt_save_remote_lock(struct mdt_thread_info *info,
4021 struct mdt_object *o, struct lustre_handle *h,
4022 enum ldlm_mode mode, int decref)
4026 if (lustre_handle_is_used(h)) {
4027 struct ldlm_lock *lock = ldlm_handle2lock(h);
4028 struct ptlrpc_request *req = mdt_info_req(info);
4031 (lock->l_policy_data.l_inodebits.bits &
4032 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
4033 mo_invalidate(info->mti_env, mdt_object_child(o));
4035 if (decref || !info->mti_has_trans || !req ||
4036 !(mode & (LCK_PW | LCK_EX))) {
4037 ldlm_lock_decref_and_cancel(h, mode);
4038 LDLM_LOCK_PUT(lock);
4040 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
4042 ldlm_lock_decref(h, mode);
4051 * Unlock mdt object.
4053 * Immeditely release the regular lock and the PDO lock or save the
4054 * lock in request and keep them referenced until client ACK or
4055 * transaction commit.
4057 * \param info thread info object
4058 * \param o mdt object
4059 * \param lh mdt lock handle referencing regular and PDO locks
4060 * \param decref force immediate lock releasing
4062 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
4064 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
4065 struct mdt_lock_handle *lh, int decref)
4069 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
4070 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
4071 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4077 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4078 const struct lu_fid *f,
4079 struct mdt_lock_handle *lh,
4082 struct mdt_object *o;
4084 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4088 rc = mdt_object_lock(info, o, lh, ibits);
4090 mdt_object_put(info->mti_env, o);
4097 void mdt_object_unlock_put(struct mdt_thread_info * info,
4098 struct mdt_object * o,
4099 struct mdt_lock_handle *lh,
4102 mdt_object_unlock(info, o, lh, decref);
4103 mdt_object_put(info->mti_env, o);
4107 * Generic code handling requests that have struct mdt_body passed in:
4109 * - extract mdt_body from request and save it in @info, if present;
4111 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4114 * - if HAS_BODY flag is set for this request type check whether object
4115 * actually exists on storage (lu_object_exists()).
4118 static int mdt_body_unpack(struct mdt_thread_info *info,
4119 enum tgt_handler_flags flags)
4121 const struct mdt_body *body;
4122 struct mdt_object *obj;
4123 const struct lu_env *env;
4124 struct req_capsule *pill;
4128 env = info->mti_env;
4129 pill = info->mti_pill;
4131 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4135 if (!(body->mbo_valid & OBD_MD_FLID))
4138 if (!fid_is_sane(&body->mbo_fid1)) {
4139 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4143 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4145 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4146 mdt_object_put(env, obj);
4149 info->mti_object = obj;
4158 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4159 enum tgt_handler_flags flags)
4161 struct req_capsule *pill = info->mti_pill;
4166 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4167 rc = mdt_body_unpack(info, flags);
4171 if (rc == 0 && (flags & HAS_REPLY)) {
4173 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4174 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4176 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4177 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4180 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4181 * by default. If the target object has more ACL entries, then
4182 * enlarge the buffer when necessary. */
4183 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4184 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4185 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4187 mdt_preset_secctx_size(info);
4188 mdt_preset_encctx_size(info);
4190 rc = req_capsule_server_pack(pill);
4192 CWARN("%s: cannot pack response: rc = %d\n",
4193 mdt_obd_name(info->mti_mdt), rc);
4198 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
4200 lh->mlh_type = MDT_NUL_LOCK;
4201 lh->mlh_reg_lh.cookie = 0ull;
4202 lh->mlh_reg_mode = LCK_MINMODE;
4203 lh->mlh_pdo_lh.cookie = 0ull;
4204 lh->mlh_pdo_mode = LCK_MINMODE;
4205 lh->mlh_rreg_lh.cookie = 0ull;
4206 lh->mlh_rreg_mode = LCK_MINMODE;
4209 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
4211 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
4212 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
4216 * Initialize fields of struct mdt_thread_info. Other fields are left in
4217 * uninitialized state, because it's too expensive to zero out whole
4218 * mdt_thread_info (> 1K) on each request arrival.
4220 void mdt_thread_info_init(struct ptlrpc_request *req,
4221 struct mdt_thread_info *info)
4225 info->mti_pill = &req->rq_pill;
4228 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4229 mdt_lock_handle_init(&info->mti_lh[i]);
4231 /* mdt device: it can be NULL while CONNECT */
4232 if (req->rq_export) {
4233 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4234 info->mti_exp = req->rq_export;
4236 info->mti_mdt = NULL;
4237 info->mti_env = req->rq_svc_thread->t_env;
4238 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4240 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4241 info->mti_big_buf = LU_BUF_NULL;
4242 info->mti_body = NULL;
4243 info->mti_object = NULL;
4244 info->mti_dlm_req = NULL;
4245 info->mti_has_trans = 0;
4246 info->mti_cross_ref = 0;
4247 info->mti_opdata = 0;
4248 info->mti_big_lmm_used = 0;
4249 info->mti_big_acl_used = 0;
4250 info->mti_som_valid = 0;
4252 info->mti_spec.no_create = 0;
4253 info->mti_spec.sp_rm_entry = 0;
4254 info->mti_spec.sp_permitted = 0;
4256 info->mti_spec.u.sp_ea.eadata = NULL;
4257 info->mti_spec.u.sp_ea.eadatalen = 0;
4260 void mdt_thread_info_fini(struct mdt_thread_info *info)
4264 if (info->mti_object != NULL) {
4265 mdt_object_put(info->mti_env, info->mti_object);
4266 info->mti_object = NULL;
4269 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4270 mdt_lock_handle_fini(&info->mti_lh[i]);
4271 info->mti_env = NULL;
4272 info->mti_pill = NULL;
4273 info->mti_exp = NULL;
4274 info->mti_mdt = NULL;
4276 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4277 lu_buf_free(&info->mti_big_buf);
4280 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4282 struct mdt_thread_info *mti;
4284 mti = mdt_th_info(tsi->tsi_env);
4285 LASSERT(mti != NULL);
4287 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4288 if (tsi->tsi_corpus != NULL) {
4289 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4290 lu_object_get(tsi->tsi_corpus);
4292 mti->mti_body = tsi->tsi_mdt_body;
4293 mti->mti_dlm_req = tsi->tsi_dlm_req;
4298 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4300 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4302 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4303 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4305 return tgt_connect(tsi);
4308 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4309 struct mdt_thread_info *info,
4310 struct ldlm_lock **lockp, __u64 flags)
4312 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4315 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4316 struct mdt_thread_info *info,
4317 struct ldlm_lock **lockp, __u64 flags)
4319 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4323 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4324 struct ldlm_lock **lockp,
4325 struct mdt_lock_handle *lh,
4326 __u64 flags, int result)
4328 struct ptlrpc_request *req = mdt_info_req(info);
4329 struct ldlm_lock *lock = *lockp;
4330 struct ldlm_lock *new_lock;
4332 /* If possible resent found a lock, @lh is set to its handle */
4333 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4335 if (new_lock == NULL) {
4336 if (flags & LDLM_FL_INTENT_ONLY) {
4338 } else if (flags & LDLM_FL_RESENT) {
4339 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4340 * resend case, however, it could be already destroyed
4341 * due to client eviction or a raced cancel RPC.
4343 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4344 lh->mlh_reg_lh.cookie);
4347 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4348 mdt_obd_name(info->mti_mdt),
4349 lh->mlh_reg_lh.cookie, flags,
4350 PFID(&info->mti_tmp_fid1),
4351 PFID(&info->mti_tmp_fid2), result);
4354 lh->mlh_reg_lh.cookie = 0;
4359 * If we've already given this lock to a client once, then we should
4360 * have no readers or writers. Otherwise, we should have one reader
4361 * _or_ writer ref (which will be zeroed below) before returning the
4364 if (new_lock->l_export == req->rq_export) {
4365 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4367 LASSERT(new_lock->l_export == NULL);
4368 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4373 if (new_lock->l_export == req->rq_export) {
4375 * Already gave this to the client, which means that we
4376 * reconstructed a reply.
4378 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4381 LDLM_LOCK_RELEASE(new_lock);
4382 lh->mlh_reg_lh.cookie = 0;
4383 RETURN(ELDLM_LOCK_REPLACED);
4387 * Fixup the lock to be given to the client.
4389 lock_res_and_lock(new_lock);
4390 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4391 * possible blocking AST. */
4392 while (new_lock->l_readers > 0) {
4393 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4394 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4395 new_lock->l_readers--;
4397 while (new_lock->l_writers > 0) {
4398 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4399 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4400 new_lock->l_writers--;
4403 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4404 new_lock->l_blocking_ast = lock->l_blocking_ast;
4405 new_lock->l_completion_ast = lock->l_completion_ast;
4406 if (ldlm_has_dom(new_lock))
4407 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4408 new_lock->l_remote_handle = lock->l_remote_handle;
4409 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4411 unlock_res_and_lock(new_lock);
4413 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4414 &new_lock->l_remote_handle,
4415 &new_lock->l_exp_hash);
4417 LDLM_LOCK_RELEASE(new_lock);
4418 lh->mlh_reg_lh.cookie = 0;
4420 RETURN(ELDLM_LOCK_REPLACED);
4423 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4424 struct ldlm_lock *new_lock,
4425 struct mdt_lock_handle *lh, __u64 flags)
4427 struct ptlrpc_request *req = mdt_info_req(info);
4428 struct ldlm_request *dlmreq;
4430 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4433 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4435 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4436 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4438 if (flags & LDLM_FL_RESENT) {
4439 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4440 lh->mlh_reg_mode = new_lock->l_granted_mode;
4442 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4443 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4444 lh->mlh_reg_lh.cookie);
4449 * If the xid matches, then we know this is a resent request, and allow
4450 * it. (It's probably an OPEN, for which we don't send a lock.
4452 if (req_can_reconstruct(req, NULL) != 0)
4456 * This remote handle isn't enqueued, so we never received or processed
4457 * this request. Clear MSG_RESENT, because it can be handled like any
4458 * normal request now.
4460 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4462 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4463 dlmreq->lock_handle[0].cookie);
4466 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4467 struct mdt_thread_info *info,
4468 struct ldlm_lock **lockp,
4471 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4472 struct ldlm_reply *ldlm_rep = NULL;
4477 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4478 * (for the resend case) or a new lock. Below we will use it to
4479 * replace the original lock.
4481 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4482 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4483 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4484 rc = mdt_object_lock(info, info->mti_object, lhc,
4485 MDS_INODELOCK_XATTR);
4490 rc = mdt_getxattr(info);
4492 if (mdt_info_req(info)->rq_repmsg != NULL)
4493 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4495 if (ldlm_rep == NULL ||
4496 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4497 mdt_object_unlock(info, info->mti_object, lhc, 1);
4501 RETURN(err_serious(-EFAULT));
4504 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4506 /* This is left for interop instead of adding a new interop flag.
4508 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4509 if (ldlm_rep->lock_policy_res2) {
4510 mdt_object_unlock(info, info->mti_object, lhc, 1);
4511 RETURN(ELDLM_LOCK_ABORTED);
4515 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4519 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4520 struct mdt_thread_info *info,
4521 struct ldlm_lock **lockp,
4524 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4526 struct ldlm_reply *ldlm_rep;
4527 struct mdt_body *reqbody;
4528 struct mdt_body *repbody;
4532 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4535 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4538 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4539 repbody->mbo_eadatasize = 0;
4540 repbody->mbo_aclsize = 0;
4544 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4547 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4551 CERROR("%s: unsupported intent %#x\n",
4552 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4553 GOTO(out_shrink, rc = -EINVAL);
4556 rc = mdt_init_ucred(info, reqbody);
4558 GOTO(out_shrink, rc);
4560 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4561 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4563 /* Get lock from request for possible resent case. */
4564 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4566 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4567 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4569 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4570 ldlm_rep->lock_policy_res2 = 0;
4571 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4572 ldlm_rep->lock_policy_res2) {
4573 lhc->mlh_reg_lh.cookie = 0ull;
4574 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4577 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4580 mdt_exit_ucred(info);
4582 mdt_client_compatibility(info);
4583 rc2 = mdt_fix_reply(info);
4589 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4590 struct mdt_thread_info *info,
4591 struct ldlm_lock **lockp,
4594 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4595 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4596 struct layout_intent *intent;
4597 struct ldlm_reply *ldlm_rep;
4598 struct lu_fid *fid = &info->mti_tmp_fid2;
4599 struct mdt_object *obj = NULL;
4600 int layout_size = 0;
4601 struct lu_buf *buf = &layout.mlc_buf;
4606 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4608 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4612 CDEBUG(D_INFO, DFID "got layout change request from client: "
4613 "opc:%u flags:%#x extent "DEXT"\n",
4614 PFID(fid), intent->li_opc, intent->li_flags,
4615 PEXT(&intent->li_extent));
4617 switch (intent->li_opc) {
4618 case LAYOUT_INTENT_TRUNC:
4619 case LAYOUT_INTENT_WRITE:
4620 layout.mlc_opc = MD_LAYOUT_WRITE;
4621 layout.mlc_intent = intent;
4623 case LAYOUT_INTENT_ACCESS:
4625 case LAYOUT_INTENT_READ:
4626 case LAYOUT_INTENT_GLIMPSE:
4627 case LAYOUT_INTENT_RELEASE:
4628 case LAYOUT_INTENT_RESTORE:
4629 CERROR("%s: Unsupported layout intent opc %d\n",
4630 mdt_obd_name(info->mti_mdt), intent->li_opc);
4633 CERROR("%s: Unknown layout intent opc %d\n",
4634 mdt_obd_name(info->mti_mdt), intent->li_opc);
4638 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4640 RETURN(PTR_ERR(obj));
4642 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4643 /* if layout is going to be changed don't use the current EA
4644 * size but the maximum one. That buffer will be shrinked
4645 * to the actual size in req_capsule_shrink() before reply.
4647 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4648 layout_size = info->mti_mdt->mdt_max_mdsize;
4650 layout_size = mdt_attr_get_eabuf_size(info, obj);
4651 if (layout_size < 0)
4652 GOTO(out, rc = layout_size);
4654 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4655 info->mti_mdt->mdt_max_mdsize = layout_size;
4657 CDEBUG(D_INFO, "%s: layout_size %d\n",
4658 mdt_obd_name(info->mti_mdt), layout_size);
4662 * set reply buffer size, so that ldlm_handle_enqueue0()->
4663 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4665 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4667 rc = req_capsule_server_pack(info->mti_pill);
4671 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4673 GOTO(out, rc = -EPROTO);
4675 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4677 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4678 if (layout.mlc_opc == MD_LAYOUT_NOP)
4681 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4685 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4686 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4692 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4693 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4695 buf->lb_len = req_capsule_get_size(info->mti_pill,
4696 &RMF_EADATA, RCL_CLIENT);
4698 * If it's a replay of layout write intent RPC, the client has
4699 * saved the extended lovea when it get reply then.
4701 if (buf->lb_len > 0)
4702 mdt_fix_lov_magic(info, buf->lb_buf);
4705 /* Get lock from request for possible resent case. */
4706 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4707 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4710 * Instantiate some layout components, if @buf contains lovea, then it's
4711 * a replay of the layout intent write RPC.
4713 rc = mdt_layout_change(info, obj, lhc, &layout);
4714 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4716 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4717 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4718 if (rc == ELDLM_LOCK_REPLACED &&
4719 (*lockp)->l_granted_mode == LCK_EX)
4720 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4725 mdt_object_put(info->mti_env, obj);
4729 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4730 struct mdt_thread_info *info,
4731 struct ldlm_lock **lockp,
4734 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4735 struct ldlm_reply *rep = NULL;
4738 struct ptlrpc_request *req = mdt_info_req(info);
4740 static const struct req_format *intent_fmts[REINT_MAX] = {
4741 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4742 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4747 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4751 /* Get lock from request for possible resent case. */
4752 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4754 rc = mdt_reint_internal(info, lhc, opc);
4756 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4757 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4759 /* Check whether the reply has been packed successfully. */
4760 if (mdt_info_req(info)->rq_repmsg != NULL)
4761 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4766 RETURN(err_serious(-EFAULT));
4769 /* MDC expects this in any case */
4771 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4773 /* the open lock or the lock for cross-ref object should be
4774 * returned to the client */
4775 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4776 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4777 rep->lock_policy_res2 = 0;
4778 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4782 rep->lock_policy_res2 = clear_serious(rc);
4784 if (rep->lock_policy_res2 == -ENOENT &&
4785 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4786 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4787 rep->lock_policy_res2 = 0;
4789 lhc->mlh_reg_lh.cookie = 0ull;
4790 if (rc == -ENOTCONN || rc == -ENODEV ||
4791 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4793 * If it is the disconnect error (ENODEV & ENOCONN), the error
4794 * will be returned by rq_status, and client at ptlrpc layer
4795 * will detect this, then disconnect, reconnect the import
4796 * immediately, instead of impacting the following the rpc.
4801 * For other cases, the error will be returned by intent, and client
4802 * will retrieve the result from intent.
4804 RETURN(ELDLM_LOCK_ABORTED);
4807 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4808 struct mdt_thread_info *info,
4809 struct ldlm_lock **lockp,
4810 u64 flags /* LDLM_FL_* */)
4812 struct req_capsule *pill = info->mti_pill;
4813 struct ptlrpc_request *req = mdt_info_req(info);
4814 const struct req_format *it_format;
4815 int (*it_handler)(enum ldlm_intent_flags,
4816 struct mdt_thread_info *,
4817 struct ldlm_lock **,
4819 enum tgt_handler_flags it_handler_flags = 0;
4820 struct ldlm_reply *rep;
4821 bool check_mdt_object = false;
4827 case IT_OPEN|IT_CREAT:
4829 * OCREAT is not a IS_MUTABLE request since the file may
4830 * already exist. We do the extra check of
4831 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4832 * really need to create the object.
4834 it_format = &RQF_LDLM_INTENT;
4835 it_handler = &mdt_intent_open;
4838 check_mdt_object = true;
4841 it_format = &RQF_LDLM_INTENT_GETATTR;
4842 it_handler = &mdt_intent_getattr;
4843 it_handler_flags = HAS_REPLY;
4846 check_mdt_object = true;
4847 it_format = &RQF_LDLM_INTENT_GETXATTR;
4848 it_handler = &mdt_intent_getxattr;
4849 it_handler_flags = HAS_BODY;
4852 it_format = &RQF_LDLM_INTENT_LAYOUT;
4853 it_handler = &mdt_intent_layout;
4856 it_format = &RQF_LDLM_INTENT;
4857 it_handler = &mdt_intent_glimpse;
4860 it_format = &RQF_LDLM_INTENT;
4861 it_handler = &mdt_intent_brw;
4863 case IT_QUOTA_DQACQ:
4864 case IT_QUOTA_CONN: {
4865 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4868 RETURN(-EOPNOTSUPP);
4870 if (mdt_rdonly(req->rq_export))
4873 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4874 /* pass the request to quota master */
4875 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4876 mdt_info_req(info), lockp,
4881 CERROR("%s: unknown intent code %#x\n",
4882 mdt_obd_name(info->mti_mdt), it_opc);
4886 req_capsule_extend(pill, it_format);
4888 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4892 if (unlikely(info->mti_object == NULL && check_mdt_object))
4895 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4898 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4900 /* execute policy */
4901 rc = (*it_handler)(it_opc, info, lockp, flags);
4903 /* Check whether the reply has been packed successfully. */
4904 if (req->rq_repmsg != NULL) {
4905 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4906 rep->lock_policy_res2 =
4907 ptlrpc_status_hton(rep->lock_policy_res2);
4913 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4914 enum ldlm_intent_flags it_opc)
4916 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4918 /* update stats when IT code is known */
4919 if (srv_stats != NULL)
4920 lprocfs_counter_incr(srv_stats,
4921 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4922 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4925 static int mdt_intent_policy(const struct lu_env *env,
4926 struct ldlm_namespace *ns,
4927 struct ldlm_lock **lockp,
4929 enum ldlm_mode mode,
4930 __u64 flags, void *data)
4932 struct tgt_session_info *tsi;
4933 struct mdt_thread_info *info;
4934 struct ptlrpc_request *req = req_cookie;
4935 struct ldlm_intent *it;
4936 struct req_capsule *pill;
4937 const struct ldlm_lock_desc *ldesc;
4942 LASSERT(req != NULL);
4944 tsi = tgt_ses_info(env);
4946 info = tsi2mdt_info(tsi);
4947 LASSERT(info != NULL);
4948 pill = info->mti_pill;
4949 LASSERT(pill->rc_req == req);
4950 ldesc = &info->mti_dlm_req->lock_desc;
4952 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4953 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4954 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4956 mdt_ptlrpc_stats_update(req, it->opc);
4957 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4961 /* Lock without inodebits makes no sense and will oops
4962 * later in ldlm. Let's check it now to see if we have
4963 * ibits corrupted somewhere in mdt_intent_opc().
4964 * The case for client miss to set ibits has been
4965 * processed by others. */
4966 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4967 ldesc->l_policy_data.l_inodebits.bits != 0));
4969 rc = err_serious(-EFAULT);
4971 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4972 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4973 struct ldlm_reply *rep;
4975 /* No intent was provided but INTENT flag is set along with
4976 * DOM bit, this is considered as GLIMPSE request.
4977 * This logic is common for MDT and OST glimpse
4979 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4980 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4981 /* Check whether the reply has been packed successfully. */
4982 if (req->rq_repmsg != NULL) {
4983 rep = req_capsule_server_get(info->mti_pill,
4985 rep->lock_policy_res2 =
4986 ptlrpc_status_hton(rep->lock_policy_res2);
4989 /* No intent was provided */
4990 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4991 rc = req_capsule_server_pack(pill);
4993 rc = err_serious(rc);
4995 mdt_thread_info_fini(info);
4999 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
5001 struct seq_server_site *ss = mdt_seq_site(mdt);
5003 if (ss->ss_node_id == 0)
5006 if (ss->ss_client_seq != NULL) {
5007 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5008 ss->ss_client_seq->lcs_exp = NULL;
5011 if (ss->ss_server_fld != NULL) {
5012 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
5013 ss->ss_server_fld->lsf_control_exp = NULL;
5017 static void mdt_seq_fini_cli(struct mdt_device *mdt)
5019 struct seq_server_site *ss = mdt_seq_site(mdt);
5024 if (ss->ss_server_seq != NULL)
5025 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
5028 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
5030 mdt_seq_fini_cli(mdt);
5031 mdt_deregister_seq_exp(mdt);
5033 return seq_site_fini(env, mdt_seq_site(mdt));
5037 * It will retrieve its FLDB entries from MDT0, and it only happens
5038 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
5039 * and it needs to refresh FLDB from the MDT0.
5041 static int mdt_register_lwp_callback(void *data)
5044 struct mdt_device *mdt = data;
5045 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
5049 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
5051 rc = lu_env_init(&env, LCT_MD_THREAD);
5053 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
5057 /* Allocate new sequence now to avoid creating local transaction
5058 * in the normal transaction process */
5059 rc = seq_server_check_and_alloc_super(&env,
5060 mdt_seq_site(mdt)->ss_server_seq);
5065 rc = fld_update_from_controller(&env, fld);
5067 CERROR("%s: cannot update controller: rc = %d\n",
5068 mdt_obd_name(mdt), rc);
5077 static int mdt_register_seq_exp(struct mdt_device *mdt)
5079 struct seq_server_site *ss = mdt_seq_site(mdt);
5080 char *lwp_name = NULL;
5083 if (ss->ss_node_id == 0)
5086 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5087 if (lwp_name == NULL)
5088 GOTO(out_free, rc = -ENOMEM);
5090 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5094 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5099 rc = lustre_register_lwp_item(lwp_name,
5100 &ss->ss_server_fld->lsf_control_exp,
5101 mdt_register_lwp_callback, mdt);
5103 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5104 ss->ss_client_seq->lcs_exp = NULL;
5108 if (lwp_name != NULL)
5109 OBD_FREE(lwp_name, MAX_OBD_NAME);
5115 * Init client sequence manager which is used by local MDS to talk to sequence
5116 * controller on remote node.
5118 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5120 struct seq_server_site *ss = mdt_seq_site(mdt);
5124 /* check if this is adding the first MDC and controller is not yet
5126 OBD_ALLOC_PTR(ss->ss_client_seq);
5127 if (ss->ss_client_seq == NULL)
5130 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5131 if (prefix == NULL) {
5132 OBD_FREE_PTR(ss->ss_client_seq);
5133 ss->ss_client_seq = NULL;
5137 /* Note: seq_client_fini will be called in seq_site_fini */
5138 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5139 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5140 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5142 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5144 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5147 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5149 struct seq_server_site *ss;
5153 ss = mdt_seq_site(mdt);
5154 /* init sequence controller server(MDT0) */
5155 if (ss->ss_node_id == 0) {
5156 OBD_ALLOC_PTR(ss->ss_control_seq);
5157 if (ss->ss_control_seq == NULL)
5160 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5161 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5164 GOTO(out_seq_fini, rc);
5167 /* Init normal sequence server */
5168 OBD_ALLOC_PTR(ss->ss_server_seq);
5169 if (ss->ss_server_seq == NULL)
5170 GOTO(out_seq_fini, rc = -ENOMEM);
5172 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5173 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5175 GOTO(out_seq_fini, rc);
5177 /* init seq client for seq server to talk to seq controller(MDT0) */
5178 rc = mdt_seq_init_cli(env, mdt);
5180 GOTO(out_seq_fini, rc);
5182 if (ss->ss_node_id != 0)
5183 /* register controller export through lwp */
5184 rc = mdt_register_seq_exp(mdt);
5189 mdt_seq_fini(env, mdt);
5197 static int mdt_fld_fini(const struct lu_env *env,
5198 struct mdt_device *m)
5200 struct seq_server_site *ss = mdt_seq_site(m);
5203 if (ss && ss->ss_server_fld) {
5204 fld_server_fini(env, ss->ss_server_fld);
5205 OBD_FREE_PTR(ss->ss_server_fld);
5206 ss->ss_server_fld = NULL;
5212 static int mdt_fld_init(const struct lu_env *env,
5214 struct mdt_device *m)
5216 struct seq_server_site *ss;
5220 ss = mdt_seq_site(m);
5222 OBD_ALLOC_PTR(ss->ss_server_fld);
5223 if (ss->ss_server_fld == NULL)
5224 RETURN(rc = -ENOMEM);
5226 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5229 OBD_FREE_PTR(ss->ss_server_fld);
5230 ss->ss_server_fld = NULL;
5237 static void mdt_stack_pre_fini(const struct lu_env *env,
5238 struct mdt_device *m, struct lu_device *top)
5240 struct lustre_cfg_bufs *bufs;
5241 struct lustre_cfg *lcfg;
5242 struct mdt_thread_info *info;
5247 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5248 LASSERT(info != NULL);
5250 bufs = &info->mti_u.bufs;
5252 LASSERT(m->mdt_child_exp);
5253 LASSERT(m->mdt_child_exp->exp_obd);
5255 /* process cleanup, pass mdt obd name to get obd umount flags */
5256 /* XXX: this is needed because all layers are referenced by
5257 * objects (some of them are pinned by osd, for example *
5258 * the proper solution should be a model where object used
5259 * by osd only doesn't have mdt/mdd slices -bzzz */
5260 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5261 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5262 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5265 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5267 top->ld_ops->ldo_process_config(env, top, lcfg);
5268 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5272 static void mdt_stack_fini(const struct lu_env *env,
5273 struct mdt_device *m, struct lu_device *top)
5275 struct obd_device *obd = mdt2obd_dev(m);
5276 struct lustre_cfg_bufs *bufs;
5277 struct lustre_cfg *lcfg;
5278 struct mdt_thread_info *info;
5282 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5283 LASSERT(info != NULL);
5285 lu_dev_del_linkage(top->ld_site, top);
5287 lu_site_purge(env, top->ld_site, -1);
5289 bufs = &info->mti_u.bufs;
5290 /* process cleanup, pass mdt obd name to get obd umount flags */
5291 /* another purpose is to let all layers to release their objects */
5292 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5297 lustre_cfg_bufs_set_string(bufs, 1, flags);
5298 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5301 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5304 top->ld_ops->ldo_process_config(env, top, lcfg);
5305 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5307 lu_site_purge(env, top->ld_site, -1);
5309 m->mdt_child = NULL;
5310 m->mdt_bottom = NULL;
5312 obd_disconnect(m->mdt_child_exp);
5313 m->mdt_child_exp = NULL;
5315 obd_disconnect(m->mdt_bottom_exp);
5316 m->mdt_child_exp = NULL;
5319 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5320 const char *next, struct obd_export **exp)
5322 struct obd_connect_data *data = NULL;
5323 struct obd_device *obd;
5327 OBD_ALLOC_PTR(data);
5329 GOTO(out, rc = -ENOMEM);
5331 obd = class_name2obd(next);
5333 CERROR("%s: can't locate next device: %s\n",
5334 mdt_obd_name(m), next);
5335 GOTO(out, rc = -ENOTCONN);
5338 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5339 data->ocd_version = LUSTRE_VERSION_CODE;
5341 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5343 CERROR("%s: cannot connect to next dev %s (%d)\n",
5344 mdt_obd_name(m), next, rc);
5354 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5355 struct lustre_cfg *cfg)
5357 char *dev = lustre_cfg_string(cfg, 0);
5358 int rc, name_size, uuid_size;
5359 char *name, *uuid, *p;
5360 struct lustre_cfg_bufs *bufs;
5361 struct lustre_cfg *lcfg;
5362 struct obd_device *obd;
5363 struct lustre_profile *lprof;
5364 struct lu_site *site;
5367 /* in 1.8 we had the only device in the stack - MDS.
5368 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5369 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5370 * to generate names and setup MDT, MDD. MDT will be using
5371 * generated name to connect to MDD. for MDD the next device
5372 * will be LOD with name taken from so called "profile" which
5373 * is generated by mount_option line
5375 * 1.8 MGS generates config. commands like this:
5376 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5377 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5378 * 2.0 MGS generates config. commands like this:
5379 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5380 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5381 * 3:lustre-MDT0000-mdtlov 4:f
5383 * we generate MDD name from MDT one, just replacing T with D
5385 * after all the preparations, the logical equivalent will be
5386 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5387 * 3:lustre-MDT0000-mdtlov 4:f
5388 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5389 * 3:lustre-MDD0000 4:f
5391 * notice we build the stack from down to top: MDD first, then MDT */
5393 name_size = MAX_OBD_NAME;
5394 uuid_size = MAX_OBD_NAME;
5396 OBD_ALLOC(name, name_size);
5397 OBD_ALLOC(uuid, uuid_size);
5398 if (name == NULL || uuid == NULL)
5399 GOTO(cleanup_mem, rc = -ENOMEM);
5401 OBD_ALLOC_PTR(bufs);
5403 GOTO(cleanup_mem, rc = -ENOMEM);
5406 p = strstr(name, "-MDT");
5408 GOTO(free_bufs, rc = -ENOMEM);
5411 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5413 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5414 if (lprof == NULL || lprof->lp_dt == NULL) {
5415 CERROR("can't find the profile: %s\n",
5416 lustre_cfg_string(cfg, 0));
5417 GOTO(free_bufs, rc = -EINVAL);
5420 lustre_cfg_bufs_reset(bufs, name);
5421 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5422 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5423 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5425 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5427 GOTO(put_profile, rc = -ENOMEM);
5428 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5430 rc = class_attach(lcfg);
5432 GOTO(lcfg_cleanup, rc);
5434 obd = class_name2obd(name);
5436 CERROR("Can not find obd %s (%s in config)\n",
5437 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5438 GOTO(lcfg_cleanup, rc = -EINVAL);
5441 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5443 lustre_cfg_bufs_reset(bufs, name);
5444 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5445 lustre_cfg_bufs_set_string(bufs, 2, dev);
5446 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5448 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5450 GOTO(class_detach, rc = -ENOMEM);
5451 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5453 rc = class_setup(obd, lcfg);
5455 GOTO(class_detach, rc);
5457 /* connect to MDD we just setup */
5458 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5460 GOTO(class_detach, rc);
5462 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5464 LASSERT(mdt_lu_site(mdt) == NULL);
5465 mdt->mdt_lu_dev.ld_site = site;
5466 site->ls_top_dev = &mdt->mdt_lu_dev;
5467 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5469 /* now connect to bottom OSD */
5470 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5471 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5473 GOTO(class_detach, rc);
5475 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5477 rc = lu_env_refill((struct lu_env *)env);
5479 CERROR("Failure to refill session: '%d'\n", rc);
5481 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5486 class_detach(obd, lcfg);
5488 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5490 class_put_profile(lprof);
5495 OBD_FREE(name, name_size);
5497 OBD_FREE(uuid, uuid_size);
5501 /* setup quota master target on MDT0 */
5502 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5503 struct lustre_cfg *cfg)
5505 struct obd_device *obd;
5506 char *dev = lustre_cfg_string(cfg, 0);
5507 char *qmtname, *uuid, *p;
5508 struct lustre_cfg_bufs *bufs;
5509 struct lustre_cfg *lcfg;
5510 struct lustre_profile *lprof;
5511 struct obd_connect_data *data;
5515 LASSERT(mdt->mdt_qmt_exp == NULL);
5516 LASSERT(mdt->mdt_qmt_dev == NULL);
5518 /* quota master is on MDT0 only for now */
5519 if (mdt->mdt_seq_site.ss_node_id != 0)
5522 /* MGS generates config commands which look as follows:
5523 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5524 * 3:lustre-MDT0000-mdtlov 4:f
5526 * We generate the QMT name from the MDT one, just replacing MD with QM
5527 * after all the preparations, the logical equivalent will be:
5528 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5529 * 3:lustre-MDT0000-osd 4:f */
5530 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5531 OBD_ALLOC(uuid, UUID_MAX);
5532 OBD_ALLOC_PTR(bufs);
5533 OBD_ALLOC_PTR(data);
5534 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5535 GOTO(cleanup_mem, rc = -ENOMEM);
5537 strcpy(qmtname, dev);
5538 p = strstr(qmtname, "-MDT");
5540 GOTO(cleanup_mem, rc = -ENOMEM);
5541 /* replace MD with QM */
5545 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5547 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5548 if (lprof == NULL || lprof->lp_dt == NULL) {
5549 CERROR("can't find profile for %s\n",
5550 lustre_cfg_string(cfg, 0));
5551 GOTO(cleanup_mem, rc = -EINVAL);
5554 lustre_cfg_bufs_reset(bufs, qmtname);
5555 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5556 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5557 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5559 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5561 GOTO(put_profile, rc = -ENOMEM);
5562 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5564 rc = class_attach(lcfg);
5566 GOTO(lcfg_cleanup, rc);
5568 obd = class_name2obd(qmtname);
5570 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5571 lustre_cfg_string(cfg, 0));
5572 GOTO(lcfg_cleanup, rc = -EINVAL);
5575 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5577 lustre_cfg_bufs_reset(bufs, qmtname);
5578 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5579 lustre_cfg_bufs_set_string(bufs, 2, dev);
5581 /* for quota, the next device should be the OSD device */
5582 lustre_cfg_bufs_set_string(bufs, 3,
5583 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5585 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5587 GOTO(class_detach, rc = -ENOMEM);
5588 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5590 rc = class_setup(obd, lcfg);
5592 GOTO(class_detach, rc);
5594 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5596 /* configure local quota objects */
5597 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5600 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5604 GOTO(class_cleanup, rc);
5606 /* connect to quota master target */
5607 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5608 data->ocd_version = LUSTRE_VERSION_CODE;
5609 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5612 CERROR("cannot connect to quota master device %s (%d)\n",
5614 GOTO(class_cleanup, rc);
5620 class_manual_cleanup(obd);
5621 mdt->mdt_qmt_dev = NULL;
5622 GOTO(lcfg_cleanup, rc);
5626 class_detach(obd, lcfg);
5628 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5630 class_put_profile(lprof);
5635 OBD_FREE(qmtname, MAX_OBD_NAME);
5637 OBD_FREE(uuid, UUID_MAX);
5643 /* Shutdown quota master target associated with mdt */
5644 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5648 if (mdt->mdt_qmt_exp == NULL)
5650 LASSERT(mdt->mdt_qmt_dev != NULL);
5652 /* the qmt automatically shuts down when the mdt disconnects */
5653 obd_disconnect(mdt->mdt_qmt_exp);
5654 mdt->mdt_qmt_exp = NULL;
5655 mdt->mdt_qmt_dev = NULL;
5659 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5660 * for now. This will be removed along with converting rest of MDT code
5661 * to use tgt_session_info */
5662 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5664 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5667 if (unlikely(info->mti_object == NULL))
5670 rc = mdt_getxattr(info);
5672 mdt_thread_info_fini(info);
5676 static int mdt_llog_open(struct tgt_session_info *tsi)
5680 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5681 RETURN(err_serious(-EACCES));
5683 RETURN(tgt_llog_open(tsi));
5686 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5687 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5688 #define OST_BRW_READ OST_READ
5689 #define OST_BRW_WRITE OST_WRITE
5691 static struct tgt_handler mdt_tgt_handlers[] = {
5692 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5693 0, MDS_CONNECT, mdt_tgt_connect,
5694 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5695 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5696 0, MDS_DISCONNECT, tgt_disconnect,
5697 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5698 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5699 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5700 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5701 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5702 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5703 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5704 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5706 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5707 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5708 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5709 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5710 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5711 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5712 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5713 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5715 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5716 mdt_hsm_ct_register),
5717 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5718 mdt_hsm_ct_unregister),
5719 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5721 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5723 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5724 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5726 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5729 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5732 static struct tgt_handler mdt_io_ops[] = {
5733 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5735 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5737 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5738 OST_PUNCH, mdt_punch_hdl,
5740 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5741 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
5743 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5744 TGT_RPC_HANDLER(OST_FIRST_OPC,
5745 0, OST_SET_INFO, mdt_io_set_info,
5746 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
5749 static struct tgt_handler mdt_sec_ctx_ops[] = {
5750 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5751 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5752 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5755 static struct tgt_handler mdt_quota_ops[] = {
5756 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5759 static struct tgt_handler mdt_llog_handlers[] = {
5760 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5761 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5762 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5763 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5766 static struct tgt_opc_slice mdt_common_slice[] = {
5768 .tos_opc_start = MDS_FIRST_OPC,
5769 .tos_opc_end = MDS_LAST_OPC,
5770 .tos_hs = mdt_tgt_handlers
5773 .tos_opc_start = OBD_FIRST_OPC,
5774 .tos_opc_end = OBD_LAST_OPC,
5775 .tos_hs = tgt_obd_handlers
5778 .tos_opc_start = LDLM_FIRST_OPC,
5779 .tos_opc_end = LDLM_LAST_OPC,
5780 .tos_hs = tgt_dlm_handlers
5783 .tos_opc_start = SEC_FIRST_OPC,
5784 .tos_opc_end = SEC_LAST_OPC,
5785 .tos_hs = mdt_sec_ctx_ops
5788 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5789 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5790 .tos_hs = tgt_out_handlers
5793 .tos_opc_start = FLD_FIRST_OPC,
5794 .tos_opc_end = FLD_LAST_OPC,
5795 .tos_hs = fld_handlers
5798 .tos_opc_start = SEQ_FIRST_OPC,
5799 .tos_opc_end = SEQ_LAST_OPC,
5800 .tos_hs = seq_handlers
5803 .tos_opc_start = QUOTA_DQACQ,
5804 .tos_opc_end = QUOTA_LAST_OPC,
5805 .tos_hs = mdt_quota_ops
5808 .tos_opc_start = LLOG_FIRST_OPC,
5809 .tos_opc_end = LLOG_LAST_OPC,
5810 .tos_hs = mdt_llog_handlers
5813 .tos_opc_start = LFSCK_FIRST_OPC,
5814 .tos_opc_end = LFSCK_LAST_OPC,
5815 .tos_hs = tgt_lfsck_handlers
5818 .tos_opc_start = OST_FIRST_OPC,
5819 .tos_opc_end = OST_LAST_OPC,
5820 .tos_hs = mdt_io_ops
5827 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5829 struct md_device *next = m->mdt_child;
5830 struct lu_device *d = &m->mdt_lu_dev;
5831 struct obd_device *obd = mdt2obd_dev(m);
5832 struct lfsck_stop stop;
5835 stop.ls_status = LS_PAUSED;
5837 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5839 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5841 mdt_restriper_stop(m);
5842 ping_evictor_stop();
5844 /* Remove the HSM /proc entry so the coordinator cannot be
5845 * restarted by a user while it's shutting down.
5847 mdt_hsm_cdt_stop(m);
5849 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5850 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5852 if (m->mdt_namespace != NULL)
5853 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5854 d->ld_obd->obd_force);
5856 obd_exports_barrier(obd);
5857 obd_zombie_barrier();
5859 mdt_quota_fini(env, m);
5861 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5863 /* Calling the cleanup functions in the same order as in the mdt_init0
5866 mdt_tunables_fini(m);
5868 target_recovery_fini(obd);
5869 upcall_cache_cleanup(m->mdt_identity_cache);
5870 m->mdt_identity_cache = NULL;
5872 mdt_fs_cleanup(env, m);
5874 tgt_fini(env, &m->mdt_lut);
5876 mdt_hsm_cdt_fini(m);
5878 if (m->mdt_los != NULL) {
5879 local_oid_storage_fini(env, m->mdt_los);
5883 if (m->mdt_namespace != NULL) {
5884 ldlm_namespace_free_post(m->mdt_namespace);
5885 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5888 if (m->mdt_md_root != NULL) {
5889 mdt_object_put(env, m->mdt_md_root);
5890 m->mdt_md_root = NULL;
5893 mdt_seq_fini(env, m);
5895 mdt_fld_fini(env, m);
5900 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5902 LASSERT(atomic_read(&d->ld_ref) == 0);
5904 server_put_mount(mdt_obd_name(m), true);
5909 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5911 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5912 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5914 const struct dt_device_param *dt_conf;
5915 struct mdt_thread_info *info;
5916 struct obd_device *obd;
5917 const char *dev = lustre_cfg_string(cfg, 0);
5918 const char *num = lustre_cfg_string(cfg, 2);
5919 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5920 struct lustre_mount_info *lmi = NULL;
5921 struct lustre_sb_info *lsi;
5923 struct seq_server_site *ss_site;
5924 const char *identity_upcall = "NONE";
5925 struct md_device *next;
5932 lu_device_init(&m->mdt_lu_dev, ldt);
5934 * Environment (env) might be missing mdt_thread_key values at that
5935 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5938 * Usually device allocation path doesn't use module key values, but
5939 * mdt has to do a lot of work here, so allocate key value.
5941 rc = lu_env_refill((struct lu_env *)env);
5945 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5946 LASSERT(info != NULL);
5948 obd = class_name2obd(dev);
5949 LASSERT(obd != NULL);
5951 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5952 m->mdt_opts.mo_evict_tgt_nids = 1;
5953 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5955 lmi = server_get_mount(dev);
5957 CERROR("Cannot get mount info for %s!\n", dev);
5960 lsi = s2lsi(lmi->lmi_sb);
5961 LASSERT(lsi->lsi_lmd);
5962 /* CMD is supported only in IAM mode */
5964 rc = kstrtol(num, 10, &node_id);
5968 obd->u.obt.obt_magic = OBT_MAGIC;
5969 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5970 m->mdt_skip_lfsck = 1;
5973 /* Just try to get a DoM lock by default. Otherwise, having a group
5974 * lock granted, it may get blocked for a long time. */
5975 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5976 /* DoM files are read at open and data is packed in the reply */
5977 m->mdt_opts.mo_dom_read_open = 1;
5979 m->mdt_squash.rsi_uid = 0;
5980 m->mdt_squash.rsi_gid = 0;
5981 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5982 spin_lock_init(&m->mdt_squash.rsi_lock);
5983 spin_lock_init(&m->mdt_lock);
5984 m->mdt_enable_remote_dir = 1;
5985 m->mdt_enable_striped_dir = 1;
5986 m->mdt_enable_dir_migration = 1;
5987 m->mdt_enable_dir_restripe = 0;
5988 m->mdt_enable_dir_auto_split = 0;
5989 m->mdt_enable_remote_dir_gid = 0;
5990 m->mdt_enable_chprojid_gid = 0;
5991 m->mdt_enable_remote_rename = 1;
5992 m->mdt_dir_restripe_nsonly = 1;
5993 m->mdt_enable_remote_subdir_mount = 1;
5995 atomic_set(&m->mdt_mds_mds_conns, 0);
5996 atomic_set(&m->mdt_async_commit_count, 0);
5998 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5999 m->mdt_lu_dev.ld_obd = obd;
6000 /* Set this lu_device to obd for error handling purposes. */
6001 obd->obd_lu_dev = &m->mdt_lu_dev;
6003 /* init the stack */
6004 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
6006 CERROR("%s: Can't init device stack, rc %d\n",
6007 mdt_obd_name(m), rc);
6012 ss_site = mdt_seq_site(m);
6013 s->ld_seq_site = ss_site;
6016 /* set server index */
6017 ss_site->ss_node_id = node_id;
6019 /* failover is the default
6020 * FIXME: we do not failout mds0/mgs, which may cause some problems.
6021 * assumed whose ss_node_id == 0 XXX
6023 obd->obd_replayable = 1;
6024 /* No connection accepted until configurations will finish */
6025 obd->obd_no_conn = 1;
6027 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
6028 char *str = lustre_cfg_string(cfg, 4);
6029 if (strchr(str, 'n')) {
6030 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
6031 obd->obd_replayable = 0;
6035 rc = mdt_fld_init(env, mdt_obd_name(m), m);
6037 GOTO(err_fini_stack, rc);
6039 rc = mdt_seq_init(env, m);
6041 GOTO(err_fini_fld, rc);
6043 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
6044 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
6045 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
6046 LDLM_NAMESPACE_SERVER,
6047 LDLM_NAMESPACE_GREEDY,
6049 if (IS_ERR(m->mdt_namespace)) {
6050 rc = PTR_ERR(m->mdt_namespace);
6051 CERROR("%s: unable to create server namespace: rc = %d\n",
6053 m->mdt_namespace = NULL;
6054 GOTO(err_fini_seq, rc);
6057 m->mdt_namespace->ns_lvbp = m;
6058 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
6060 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
6061 /* set obd_namespace for compatibility with old code */
6062 obd->obd_namespace = m->mdt_namespace;
6064 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
6065 OBD_FAIL_MDS_ALL_REQUEST_NET,
6066 OBD_FAIL_MDS_ALL_REPLY_NET);
6068 GOTO(err_free_ns, rc);
6070 /* Amount of available space excluded from granting and reserved
6071 * for metadata. It is a percentage of the total MDT size. */
6072 tgd->tgd_reserved_pcnt = 10;
6074 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
6075 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
6077 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6079 rc = mdt_fs_setup(env, m, obd, lsi);
6083 fid.f_seq = FID_SEQ_LOCAL_NAME;
6086 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6088 GOTO(err_fs_cleanup, rc);
6090 rc = mdt_hsm_cdt_init(m);
6092 CERROR("%s: error initializing coordinator, rc %d\n",
6093 mdt_obd_name(m), rc);
6094 GOTO(err_los_fini, rc);
6097 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6099 next = m->mdt_child;
6100 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6102 mntopts = dt_conf->ddp_mntopts;
6104 if (mntopts & MNTOPT_USERXATTR)
6105 m->mdt_opts.mo_user_xattr = 1;
6107 m->mdt_opts.mo_user_xattr = 0;
6109 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6111 if (mntopts & MNTOPT_ACL)
6112 m->mdt_opts.mo_acl = 1;
6114 m->mdt_opts.mo_acl = 0;
6116 /* XXX: to support suppgid for ACL, we enable identity_upcall
6117 * by default, otherwise, maybe got unexpected -EACCESS. */
6118 if (m->mdt_opts.mo_acl)
6119 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6121 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6123 &mdt_identity_upcall_cache_ops);
6124 if (IS_ERR(m->mdt_identity_cache)) {
6125 rc = PTR_ERR(m->mdt_identity_cache);
6126 m->mdt_identity_cache = NULL;
6127 GOTO(err_free_hsm, rc);
6130 rc = mdt_tunables_init(m, dev);
6132 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
6133 GOTO(err_recovery, rc);
6136 rc = mdt_quota_init(env, m, cfg);
6138 GOTO(err_procfs, rc);
6140 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
6141 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
6142 "mdt_ldlm_client", m->mdt_ldlm_client);
6144 ping_evictor_start();
6146 /* recovery will be started upon mdt_prepare()
6147 * when the whole stack is complete and ready
6148 * to serve the requests */
6150 /* Reduce the initial timeout on an MDS because it doesn't need such
6151 * a long timeout as an OST does. Adaptive timeouts will adjust this
6152 * value appropriately. */
6153 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
6154 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
6156 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
6157 m->mdt_lut.lut_local_recovery = 1;
6159 rc = mdt_restriper_start(m);
6161 GOTO(err_ping_evictor, rc);
6166 ping_evictor_stop();
6168 mdt_tunables_fini(m);
6170 upcall_cache_cleanup(m->mdt_identity_cache);
6171 m->mdt_identity_cache = NULL;
6173 mdt_hsm_cdt_fini(m);
6175 local_oid_storage_fini(env, m->mdt_los);
6178 mdt_fs_cleanup(env, m);
6180 /* keep recoverable clients */
6182 target_recovery_fini(obd);
6183 obd_exports_barrier(obd);
6184 obd_zombie_barrier();
6185 tgt_fini(env, &m->mdt_lut);
6187 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
6188 obd->obd_namespace = m->mdt_namespace = NULL;
6190 mdt_seq_fini(env, m);
6192 mdt_fld_fini(env, m);
6194 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6197 server_put_mount(dev, true);
6201 /* For interoperability, the left element is old parameter, the right one
6202 * is the new version of the parameter, if some parameter is deprecated,
6203 * the new version should be set as NULL. */
6204 static struct cfg_interop_param mdt_interop_param[] = {
6205 { "mdt.group_upcall", NULL },
6206 { "mdt.quota_type", NULL },
6207 { "mdd.quota_type", NULL },
6208 { "mdt.som", NULL },
6209 { "mdt.rootsquash", "mdt.root_squash" },
6210 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
6214 /* used by MGS to process specific configurations */
6215 static int mdt_process_config(const struct lu_env *env,
6216 struct lu_device *d, struct lustre_cfg *cfg)
6218 struct mdt_device *m = mdt_dev(d);
6219 struct md_device *md_next = m->mdt_child;
6220 struct lu_device *next = md2lu_dev(md_next);
6224 switch (cfg->lcfg_command) {
6226 struct obd_device *obd = d->ld_obd;
6227 /* For interoperability */
6228 struct cfg_interop_param *ptr = NULL;
6229 struct lustre_cfg *old_cfg = NULL;
6233 param = lustre_cfg_string(cfg, 1);
6234 if (param == NULL) {
6235 CERROR("param is empty\n");
6240 ptr = class_find_old_param(param, mdt_interop_param);
6242 if (ptr->new_param == NULL) {
6244 CWARN("For interoperability, skip this %s."
6245 " It is obsolete.\n", ptr->old_param);
6249 CWARN("Found old param %s, changed it to %s.\n",
6250 ptr->old_param, ptr->new_param);
6253 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
6260 count = class_modify_config(cfg, PARAM_MDT,
6261 &obd->obd_kset.kobj);
6263 struct coordinator *cdt = &m->mdt_coordinator;
6265 /* is it an HSM var ? */
6266 count = class_modify_config(cfg, PARAM_HSM,
6267 &cdt->cdt_hsm_kobj);
6269 /* we don't understand; pass it on */
6270 rc = next->ld_ops->ldo_process_config(env, next,
6273 rc = count > 0 ? 0 : count;
6275 rc = count > 0 ? 0 : count;
6279 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
6280 cfg->lcfg_buflens));
6284 /* others are passed further */
6285 rc = next->ld_ops->ldo_process_config(env, next, cfg);
6291 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
6292 const struct lu_object_header *hdr,
6293 struct lu_device *d)
6295 struct mdt_object *mo;
6299 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6301 struct lu_object *o;
6302 struct lu_object_header *h;
6305 h = &mo->mot_header;
6306 lu_object_header_init(h);
6307 lu_object_init(o, h, d);
6308 lu_object_add_top(h, o);
6309 o->lo_ops = &mdt_obj_ops;
6310 spin_lock_init(&mo->mot_write_lock);
6311 mutex_init(&mo->mot_som_mutex);
6312 mutex_init(&mo->mot_lov_mutex);
6313 init_rwsem(&mo->mot_dom_sem);
6314 init_rwsem(&mo->mot_open_sem);
6315 atomic_set(&mo->mot_open_count, 0);
6316 mo->mot_restripe_offset = 0;
6317 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6318 mo->mot_lsom_size = 0;
6319 mo->mot_lsom_blocks = 0;
6320 mo->mot_lsom_inited = false;
6326 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6327 const struct lu_object_conf *unused)
6329 struct mdt_device *d = mdt_dev(o->lo_dev);
6330 struct lu_device *under;
6331 struct lu_object *below;
6335 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6336 PFID(lu_object_fid(o)));
6338 under = &d->mdt_child->md_lu_dev;
6339 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6340 if (below != NULL) {
6341 lu_object_add(o, below);
6348 static void mdt_object_free_rcu(struct rcu_head *head)
6350 struct mdt_object *mo = container_of(head, struct mdt_object,
6351 mot_header.loh_rcu);
6353 kmem_cache_free(mdt_object_kmem, mo);
6356 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6358 struct mdt_object *mo = mdt_obj(o);
6359 struct lu_object_header *h;
6363 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6364 PFID(lu_object_fid(o)));
6366 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6367 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6370 lu_object_header_fini(h);
6371 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6372 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6377 static int mdt_object_print(const struct lu_env *env, void *cookie,
6378 lu_printer_t p, const struct lu_object *o)
6380 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6382 return (*p)(env, cookie,
6383 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6384 mdto, mdto->mot_lov_created ? "lov_created" : "",
6385 mdto->mot_cache_attr ? "cache_attr" : "",
6386 mdto->mot_write_count);
6389 static int mdt_prepare(const struct lu_env *env,
6390 struct lu_device *pdev,
6391 struct lu_device *cdev)
6393 struct mdt_device *mdt = mdt_dev(cdev);
6394 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6395 struct obd_device *obd = cdev->ld_obd;
6402 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6406 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6410 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6414 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6415 /* The LFSCK instance is registered just now, so it must be there when
6416 * register the namespace to such instance. */
6417 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6419 if (mdt->mdt_seq_site.ss_node_id == 0) {
6420 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6421 &mdt->mdt_md_root_fid);
6426 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6428 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6429 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6430 LASSERT(obd->obd_no_conn);
6431 spin_lock(&obd->obd_dev_lock);
6432 obd->obd_no_conn = 0;
6433 spin_unlock(&obd->obd_dev_lock);
6435 if (obd->obd_recovering == 0)
6436 mdt_postrecov(env, mdt);
6441 const struct lu_device_operations mdt_lu_ops = {
6442 .ldo_object_alloc = mdt_object_alloc,
6443 .ldo_process_config = mdt_process_config,
6444 .ldo_prepare = mdt_prepare,
6447 static const struct lu_object_operations mdt_obj_ops = {
6448 .loo_object_init = mdt_object_init,
6449 .loo_object_free = mdt_object_free,
6450 .loo_object_print = mdt_object_print
6453 static int mdt_obd_set_info_async(const struct lu_env *env,
6454 struct obd_export *exp,
6455 __u32 keylen, void *key,
6456 __u32 vallen, void *val,
6457 struct ptlrpc_request_set *set)
6463 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6464 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6471 static inline void mdt_enable_slc(struct mdt_device *mdt)
6473 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6474 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6477 static inline void mdt_disable_slc(struct mdt_device *mdt)
6479 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6480 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6484 * Match client and server connection feature flags.
6486 * Compute the compatibility flags for a connection request based on
6487 * features mutually supported by client and server.
6489 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6490 * must not be updated here, otherwise a partially initialized value may
6491 * be exposed. After the connection request is successfully processed,
6492 * the top-level MDT connect request handler atomically updates the export
6493 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6494 * reply. \see mdt_connect().
6496 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6497 * full struct obd_connect_data. So care must be taken when accessing fields
6498 * that are not present in struct obd_connect_data_v1. See LU-16.
6500 * \param exp the obd_export associated with this client/target pair
6501 * \param mdt the target device for the connection
6502 * \param data stores data for this connect request
6505 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6506 * \retval -EBADE client and server feature requirements are incompatible
6508 static int mdt_connect_internal(const struct lu_env *env,
6509 struct obd_export *exp,
6510 struct mdt_device *mdt,
6511 struct obd_connect_data *data, bool reconnect)
6513 const char *obd_name = mdt_obd_name(mdt);
6514 LASSERT(data != NULL);
6516 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6518 if (mdt->mdt_bottom->dd_rdonly &&
6519 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6520 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6523 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6524 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6526 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6528 if (!mdt->mdt_opts.mo_acl)
6529 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6531 if (!mdt->mdt_opts.mo_user_xattr)
6532 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6534 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6535 data->ocd_brw_size = min(data->ocd_brw_size,
6537 if (data->ocd_brw_size == 0) {
6538 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6539 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6540 "ocd_brw_size unexpectedly zero, network data "
6541 "corruption? Refusing to connect this client\n",
6542 obd_name, exp->exp_client_uuid.uuid,
6543 exp, data->ocd_connect_flags, data->ocd_version,
6544 data->ocd_grant, data->ocd_index);
6549 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6550 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6552 /* client is reporting its page size, for future use */
6553 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6554 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6555 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6556 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6557 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6558 /* ocd_grant_tax_kb is in 1K byte blocks */
6559 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6560 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6563 /* Save connect_data we have so far because tgt_grant_connect()
6564 * uses it to calculate grant, and we want to save the client
6565 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6566 exp->exp_connect_data = *data;
6567 if (OCD_HAS_FLAG(data, GRANT))
6568 tgt_grant_connect(env, exp, data, !reconnect);
6570 if (OCD_HAS_FLAG(data, MAXBYTES))
6571 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6573 /* NB: Disregard the rule against updating
6574 * exp_connect_data.ocd_connect_flags in this case, since
6575 * tgt_client_new() needs to know if this is a lightweight
6576 * connection, and it is safe to expose this flag before
6577 * connection processing completes. */
6578 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6579 spin_lock(&exp->exp_lock);
6580 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6581 spin_unlock(&exp->exp_lock);
6584 data->ocd_version = LUSTRE_VERSION_CODE;
6586 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6587 CWARN("%s: MDS requires FID support, but client not\n",
6592 if (OCD_HAS_FLAG(data, PINGLESS)) {
6593 if (ptlrpc_pinger_suppress_pings()) {
6594 spin_lock(&exp->exp_obd->obd_dev_lock);
6595 list_del_init(&exp->exp_obd_chain_timed);
6596 spin_unlock(&exp->exp_obd->obd_dev_lock);
6598 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6602 data->ocd_max_easize = mdt->mdt_max_ea_size;
6604 /* NB: Disregard the rule against updating
6605 * exp_connect_data.ocd_connect_flags in this case, since
6606 * tgt_client_new() needs to know if this is client supports
6607 * multiple modify RPCs, and it is safe to expose this flag before
6608 * connection processing completes. */
6609 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6610 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6611 spin_lock(&exp->exp_lock);
6612 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6613 spin_unlock(&exp->exp_lock);
6616 if (OCD_HAS_FLAG(data, CKSUM)) {
6617 __u32 cksum_types = data->ocd_cksum_types;
6619 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6621 if (unlikely(data->ocd_cksum_types == 0)) {
6622 CERROR("%s: Connect with checksum support but no "
6623 "ocd_cksum_types is set\n",
6624 exp->exp_obd->obd_name);
6628 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6629 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6630 cksum_types, data->ocd_cksum_types);
6632 /* This client does not support OBD_CONNECT_CKSUM
6633 * fall back to CRC32 */
6634 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6635 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6636 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6639 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6640 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6641 atomic_inc(&mdt->mdt_mds_mds_conns);
6642 mdt_enable_slc(mdt);
6645 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6646 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6651 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6652 struct mdt_thread_info *info,
6653 struct mdt_file_data *mfd)
6655 struct lu_context ses;
6659 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6664 lu_context_enter(&ses);
6666 mdt_ucred(info)->uc_valid = UCRED_OLD;
6667 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6669 lu_context_exit(&ses);
6670 lu_context_fini(&ses);
6676 static int mdt_export_cleanup(struct obd_export *exp)
6678 LIST_HEAD(closing_list);
6679 struct mdt_export_data *med = &exp->exp_mdt_data;
6680 struct obd_device *obd = exp->exp_obd;
6681 struct mdt_device *mdt;
6682 struct mdt_thread_info *info;
6684 struct mdt_file_data *mfd, *n;
6688 spin_lock(&med->med_open_lock);
6689 while (!list_empty(&med->med_open_head)) {
6690 struct list_head *tmp = med->med_open_head.next;
6691 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6693 /* Remove mfd handle so it can't be found again.
6694 * We are consuming the mfd_list reference here. */
6695 class_handle_unhash(&mfd->mfd_open_handle);
6696 list_move_tail(&mfd->mfd_list, &closing_list);
6698 spin_unlock(&med->med_open_lock);
6699 mdt = mdt_dev(obd->obd_lu_dev);
6700 LASSERT(mdt != NULL);
6702 rc = lu_env_init(&env, LCT_MD_THREAD);
6706 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6707 LASSERT(info != NULL);
6708 memset(info, 0, sizeof *info);
6709 info->mti_env = &env;
6710 info->mti_mdt = mdt;
6711 info->mti_exp = exp;
6713 if (!list_empty(&closing_list)) {
6714 struct md_attr *ma = &info->mti_attr;
6716 /* Close any open files (which may also cause orphan
6718 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6719 list_del_init(&mfd->mfd_list);
6720 ma->ma_need = ma->ma_valid = 0;
6722 /* This file is being closed due to an eviction, it
6723 * could have been modified and now dirty regarding to
6724 * HSM archive, check this!
6725 * The logic here is to mark a file dirty if there's a
6726 * chance it was dirtied before the client was evicted,
6727 * so that we don't have to wait for a release attempt
6728 * before finding out the file was actually dirty and
6729 * fail the release. Aggressively marking it dirty here
6730 * will cause the policy engine to attempt to
6731 * re-archive it; when rearchiving, we can compare the
6732 * current version to the HSM data_version and make the
6733 * archive request into a noop if it's not actually
6736 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6737 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6739 /* Don't unlink orphan on failover umount, LU-184 */
6740 if (exp->exp_flags & OBD_OPT_FAILOVER ||
6741 exp->exp_obd->obd_stopping) {
6742 ma->ma_valid = MA_FLAGS;
6743 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6745 ma->ma_valid |= MA_FORCE_LOG;
6746 mdt_mfd_close(info, mfd);
6749 info->mti_mdt = NULL;
6750 /* cleanup client slot early */
6751 /* Do not erase record for recoverable client. */
6752 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6753 tgt_client_del(&env, exp);
6759 static int mdt_obd_disconnect(struct obd_export *exp)
6766 class_export_get(exp);
6768 if (!(exp->exp_flags & OBD_OPT_FORCE))
6769 tgt_grant_sanity_check(exp->exp_obd, __func__);
6771 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6772 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6773 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6775 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6776 mdt_disable_slc(mdt);
6779 rc = server_disconnect_export(exp);
6781 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6783 tgt_grant_discard(exp);
6785 rc = mdt_export_cleanup(exp);
6786 nodemap_del_member(exp);
6787 class_export_put(exp);
6791 /* mds_connect copy */
6792 static int mdt_obd_connect(const struct lu_env *env,
6793 struct obd_export **exp, struct obd_device *obd,
6794 struct obd_uuid *cluuid,
6795 struct obd_connect_data *data,
6798 struct obd_export *lexp;
6799 struct lustre_handle conn = { 0 };
6800 struct mdt_device *mdt;
6802 lnet_nid_t *client_nid = localdata;
6805 LASSERT(env != NULL);
6806 LASSERT(data != NULL);
6808 if (!exp || !obd || !cluuid)
6811 mdt = mdt_dev(obd->obd_lu_dev);
6814 * first, check whether the stack is ready to handle requests
6815 * XXX: probably not very appropriate method is used now
6816 * at some point we should find a better one
6818 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6819 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6820 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6821 rc = obd_get_info(env, mdt->mdt_child_exp,
6822 sizeof(KEY_OSP_CONNECTED),
6823 KEY_OSP_CONNECTED, NULL, NULL);
6826 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6829 rc = class_connect(&conn, obd, cluuid);
6833 lexp = class_conn2export(&conn);
6834 LASSERT(lexp != NULL);
6836 rc = nodemap_add_member(*client_nid, lexp);
6837 if (rc != 0 && rc != -EEXIST)
6840 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6842 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6845 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6846 rc = tgt_client_new(env, lexp);
6848 mdt_export_stats_init(obd, lexp, localdata);
6852 class_disconnect(lexp);
6853 nodemap_del_member(lexp);
6857 /* Because we do not want this export to be evicted by pinger,
6858 * let's not add this export to the timed chain list. */
6859 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6860 spin_lock(&lexp->exp_obd->obd_dev_lock);
6861 list_del_init(&lexp->exp_obd_chain_timed);
6862 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6869 static int mdt_obd_reconnect(const struct lu_env *env,
6870 struct obd_export *exp, struct obd_device *obd,
6871 struct obd_uuid *cluuid,
6872 struct obd_connect_data *data,
6875 lnet_nid_t *client_nid = localdata;
6879 if (exp == NULL || obd == NULL || cluuid == NULL)
6882 rc = nodemap_add_member(*client_nid, exp);
6883 if (rc != 0 && rc != -EEXIST)
6886 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6889 mdt_export_stats_init(obd, exp, localdata);
6891 nodemap_del_member(exp);
6896 /* FIXME: Can we avoid using these two interfaces? */
6897 static int mdt_init_export(struct obd_export *exp)
6899 struct mdt_export_data *med = &exp->exp_mdt_data;
6903 INIT_LIST_HEAD(&med->med_open_head);
6904 spin_lock_init(&med->med_open_lock);
6905 spin_lock(&exp->exp_lock);
6906 exp->exp_connecting = 1;
6907 spin_unlock(&exp->exp_lock);
6909 OBD_ALLOC(exp->exp_used_slots,
6910 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6911 if (exp->exp_used_slots == NULL)
6914 /* self-export doesn't need client data and ldlm initialization */
6915 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6916 &exp->exp_client_uuid)))
6919 rc = tgt_client_alloc(exp);
6923 rc = ldlm_init_export(exp);
6930 tgt_client_free(exp);
6932 OBD_FREE(exp->exp_used_slots,
6933 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6934 exp->exp_used_slots = NULL;
6936 CERROR("%s: Failed to initialize export: rc = %d\n",
6937 exp->exp_obd->obd_name, rc);
6941 static int mdt_destroy_export(struct obd_export *exp)
6945 target_destroy_export(exp);
6946 if (exp->exp_used_slots)
6947 OBD_FREE(exp->exp_used_slots,
6948 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6950 /* destroy can be called from failed obd_setup, so
6951 * checking uuid is safer than obd_self_export */
6952 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6953 &exp->exp_client_uuid)))
6956 ldlm_destroy_export(exp);
6957 tgt_client_free(exp);
6959 LASSERT(list_empty(&exp->exp_outstanding_replies));
6960 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6963 * discard grants once we're sure no more
6964 * interaction with the client is possible
6966 tgt_grant_discard(exp);
6967 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6968 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6970 if (!(exp->exp_flags & OBD_OPT_FORCE))
6971 tgt_grant_sanity_check(exp->exp_obd, __func__);
6976 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6977 struct linkea_data *ldata)
6981 LASSERT(ldata->ld_buf->lb_buf != NULL);
6983 if (!mdt_object_exists(mdt_obj))
6986 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6987 ldata->ld_buf, XATTR_NAME_LINK);
6988 if (rc == -ERANGE) {
6989 /* Buf was too small, figure out what we need. */
6990 lu_buf_free(ldata->ld_buf);
6991 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6992 ldata->ld_buf, XATTR_NAME_LINK);
6995 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6996 if (ldata->ld_buf->lb_buf == NULL)
6998 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6999 ldata->ld_buf, XATTR_NAME_LINK);
7004 return linkea_init_with_rec(ldata);
7008 * Given an MDT object, try to look up the full path to the object.
7009 * Part of the MDT layer implementation of lfs fid2path.
7011 * \param[in] info Per-thread common data shared by MDT level handlers.
7012 * \param[in] obj Object to do path lookup of
7013 * \param[in,out] fp User-provided struct to store path information
7014 * \param[in] root_fid Root FID of current path should reach
7016 * \retval 0 Lookup successful, path information stored in fp
7017 * \retval -EAGAIN Lookup failed, usually because object is being moved
7018 * \retval negative errno if there was a problem
7020 static int mdt_path_current(struct mdt_thread_info *info,
7021 struct mdt_object *obj,
7022 struct getinfo_fid2path *fp,
7023 struct lu_fid *root_fid)
7025 struct mdt_device *mdt = info->mti_mdt;
7026 struct lu_name *tmpname = &info->mti_name;
7027 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
7028 struct lu_buf *buf = &info->mti_big_buf;
7029 struct linkea_data ldata = { NULL };
7031 struct mdt_object *mdt_obj;
7032 struct link_ea_header *leh;
7033 struct link_ea_entry *lee;
7040 /* temp buffer for path element, the buffer will be finally freed
7041 * in mdt_thread_info_fini */
7042 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
7043 if (buf->lb_buf == NULL)
7047 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
7050 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
7052 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
7053 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
7054 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
7055 GOTO(out, rc = -ENOENT);
7057 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
7059 mdt_object_get(info->mti_env, mdt_obj);
7061 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
7062 if (IS_ERR(mdt_obj))
7063 GOTO(out, rc = PTR_ERR(mdt_obj));
7066 if (!mdt_object_exists(mdt_obj)) {
7067 mdt_object_put(info->mti_env, mdt_obj);
7068 GOTO(out, rc = -ENOENT);
7071 if (mdt_object_remote(mdt_obj)) {
7072 mdt_object_put(info->mti_env, mdt_obj);
7073 GOTO(remote_out, rc = -EREMOTE);
7076 rc = mdt_links_read(info, mdt_obj, &ldata);
7078 mdt_object_put(info->mti_env, mdt_obj);
7083 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
7084 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
7085 /* If set, use link #linkno for path lookup, otherwise use
7086 link #0. Only do this for the final path element. */
7087 if (first && fp->gf_linkno < leh->leh_reccount) {
7089 for (count = 0; count < fp->gf_linkno; count++) {
7090 lee = (struct link_ea_entry *)
7091 ((char *)lee + reclen);
7092 linkea_entry_unpack(lee, &reclen, tmpname,
7095 if (fp->gf_linkno < leh->leh_reccount - 1)
7096 /* indicate to user there are more links */
7100 /* Check if it is slave stripes */
7101 rc = mdt_is_dir_stripe(info, mdt_obj);
7102 mdt_object_put(info->mti_env, mdt_obj);
7106 fp->gf_fid = *tmpfid;
7110 /* Pack the name in the end of the buffer */
7111 ptr -= tmpname->ln_namelen;
7112 if (ptr - 1 <= fp->gf_u.gf_path)
7113 GOTO(out, rc = -EOVERFLOW);
7114 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
7117 /* keep the last resolved fid to the client, so the
7118 * client will build the left path on another MDT for
7120 fp->gf_fid = *tmpfid;
7125 /* non-zero will be treated as an error */
7129 ptr++; /* skip leading / */
7130 memmove(fp->gf_u.gf_path, ptr,
7131 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
7138 * Given an MDT object, use mdt_path_current to get the path.
7139 * Essentially a wrapper to retry mdt_path_current a set number of times
7140 * if -EAGAIN is returned (usually because an object is being moved).
7142 * Part of the MDT layer implementation of lfs fid2path.
7144 * \param[in] info Per-thread common data shared by mdt level handlers.
7145 * \param[in] obj Object to do path lookup of
7146 * \param[in,out] fp User-provided struct for arguments and to store path
7149 * \retval 0 Lookup successful, path information stored in fp
7150 * \retval negative errno if there was a problem
7152 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
7153 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
7155 struct mdt_device *mdt = info->mti_mdt;
7160 if (fp->gf_pathlen < 3)
7163 if (root_fid == NULL)
7164 root_fid = &mdt->mdt_md_root_fid;
7166 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
7167 fp->gf_u.gf_path[0] = '\0';
7171 /* Retry multiple times in case file is being moved */
7172 while (tries-- && rc == -EAGAIN)
7173 rc = mdt_path_current(info, obj, fp, root_fid);
7179 * Get the full path of the provided FID, as of changelog record recno.
7181 * This checks sanity and looks up object for user provided FID
7182 * before calling the actual path lookup code.
7184 * Part of the MDT layer implementation of lfs fid2path.
7186 * \param[in] info Per-thread common data shared by mdt level handlers.
7187 * \param[in,out] fp User-provided struct for arguments and to store path
7190 * \retval 0 Lookup successful, path information and recno stored in fp
7191 * \retval -ENOENT, object does not exist
7192 * \retval negative errno if there was a problem
7194 static int mdt_fid2path(struct mdt_thread_info *info,
7195 struct lu_fid *root_fid,
7196 struct getinfo_fid2path *fp)
7198 struct mdt_device *mdt = info->mti_mdt;
7199 struct mdt_object *obj;
7203 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
7204 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
7206 if (!fid_is_sane(&fp->gf_fid))
7209 if (!fid_is_namespace_visible(&fp->gf_fid)) {
7210 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
7211 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
7212 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
7216 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
7219 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
7220 PFID(&fp->gf_fid), rc);
7224 if (mdt_object_remote(obj)) {
7226 } else if (!mdt_object_exists(obj)) {
7229 struct lu_attr la = { 0 };
7230 struct dt_object *dt = mdt_obj2dt(obj);
7232 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
7233 dt_attr_get(info->mti_env, mdt_obj2dt(obj), &la);
7234 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL)
7235 /* path resolution cannot be carried out on server
7236 * side for encrypted files
7244 mdt_object_put(info->mti_env, obj);
7245 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
7246 PFID(&fp->gf_fid), rc);
7250 rc = mdt_path(info, obj, fp, root_fid);
7252 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
7253 PFID(&fp->gf_fid), fp->gf_u.gf_path,
7254 fp->gf_recno, fp->gf_linkno);
7256 mdt_object_put(info->mti_env, obj);
7261 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
7262 void *val, int vallen)
7264 struct getinfo_fid2path *fpout, *fpin;
7265 struct lu_fid *root_fid = NULL;
7268 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
7271 if (req_capsule_req_need_swab(info->mti_pill))
7272 lustre_swab_fid2path(fpin);
7274 memcpy(fpout, fpin, sizeof(*fpin));
7275 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
7278 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
7279 sizeof(struct lu_fid)) {
7280 /* client sent its root FID, which is normally fileset FID */
7281 root_fid = fpin->gf_u.gf_root_fid;
7282 if (req_capsule_req_need_swab(info->mti_pill))
7283 lustre_swab_lu_fid(root_fid);
7285 if (root_fid != NULL && !fid_is_sane(root_fid))
7289 rc = mdt_fid2path(info, root_fid, fpout);
7293 int mdt_get_info(struct tgt_session_info *tsi)
7303 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7305 CDEBUG(D_IOCTL, "No GETINFO key\n");
7306 RETURN(err_serious(-EFAULT));
7308 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7311 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7312 if (vallen == NULL) {
7313 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7314 tgt_name(tsi->tsi_tgt));
7315 RETURN(err_serious(-EFAULT));
7318 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7320 rc = req_capsule_server_pack(tsi->tsi_pill);
7322 RETURN(err_serious(rc));
7324 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7325 if (valout == NULL) {
7326 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7327 tgt_name(tsi->tsi_tgt));
7328 RETURN(err_serious(-EFAULT));
7331 if (KEY_IS(KEY_FID2PATH)) {
7332 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7334 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7335 mdt_thread_info_fini(info);
7342 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7344 struct obd_ioctl_data *data = karg;
7347 struct mdt_object *obj;
7348 struct mdt_lock_handle *lh;
7352 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7353 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7356 fid = (struct lu_fid *)data->ioc_inlbuf1;
7358 if (!fid_is_sane(fid))
7361 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7363 lh = &mti->mti_lh[MDT_LH_PARENT];
7364 mdt_lock_reg_init(lh, LCK_CR);
7366 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7368 RETURN(PTR_ERR(obj));
7370 if (mdt_object_remote(obj)) {
7373 * before calling version get the correct MDS should be
7374 * fid, this is error to find remote object here
7376 CERROR("nonlocal object "DFID"\n", PFID(fid));
7377 } else if (!mdt_object_exists(obj)) {
7378 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7381 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7382 *(__u64 *)data->ioc_inlbuf2 = version;
7385 mdt_object_unlock_put(mti, obj, lh, 1);
7389 /* ioctls on obd dev */
7390 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7391 void *karg, void __user *uarg)
7394 struct obd_device *obd = exp->exp_obd;
7395 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7396 struct dt_device *dt = mdt->mdt_bottom;
7400 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7401 rc = lu_env_init(&env, LCT_MD_THREAD);
7407 rc = mdt_device_sync(&env, mdt);
7409 case OBD_IOC_SET_READONLY:
7410 rc = dt_sync(&env, dt);
7412 rc = dt_ro(&env, dt);
7414 case OBD_IOC_ABORT_RECOVERY: {
7415 struct obd_ioctl_data *data = karg;
7417 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7418 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
7419 obd->obd_abort_recov_mdt = 1;
7420 wake_up(&obd->obd_next_transno_waitq);
7421 } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7422 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7423 obd->obd_abort_recovery = 1;
7424 target_stop_recovery_thread(obd);
7429 case OBD_IOC_CHANGELOG_REG:
7430 case OBD_IOC_CHANGELOG_DEREG:
7431 case OBD_IOC_CHANGELOG_CLEAR:
7432 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7436 case OBD_IOC_START_LFSCK: {
7437 struct md_device *next = mdt->mdt_child;
7438 struct obd_ioctl_data *data = karg;
7439 struct lfsck_start_param lsp;
7441 if (unlikely(data == NULL)) {
7446 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7447 lsp.lsp_index_valid = 0;
7448 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7451 case OBD_IOC_STOP_LFSCK: {
7452 struct md_device *next = mdt->mdt_child;
7453 struct obd_ioctl_data *data = karg;
7454 struct lfsck_stop stop;
7456 stop.ls_status = LS_STOPPED;
7457 /* Old lfsck utils may pass NULL @stop. */
7458 if (data->ioc_inlbuf1 == NULL)
7462 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7464 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7467 case OBD_IOC_QUERY_LFSCK: {
7468 struct md_device *next = mdt->mdt_child;
7469 struct obd_ioctl_data *data = karg;
7471 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7475 case OBD_IOC_GET_OBJ_VERSION: {
7476 struct mdt_thread_info *mti;
7477 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7478 memset(mti, 0, sizeof *mti);
7479 mti->mti_env = &env;
7483 rc = mdt_ioc_version_get(mti, karg);
7486 case OBD_IOC_CATLOGLIST: {
7487 struct mdt_thread_info *mti;
7489 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7490 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7491 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7492 &mti->mti_tmp_fid1);
7497 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7498 mdt_obd_name(mdt), cmd, rc);
7505 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7507 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7511 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7512 struct lfsck_start_param lsp;
7514 lsp.lsp_start = NULL;
7515 lsp.lsp_index_valid = 0;
7516 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7517 OBD_IOC_START_LFSCK,
7519 if (rc != 0 && rc != -EALREADY)
7520 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7521 mdt_obd_name(mdt), rc);
7524 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7528 static int mdt_obd_postrecov(struct obd_device *obd)
7533 rc = lu_env_init(&env, LCT_MD_THREAD);
7536 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7541 static const struct obd_ops mdt_obd_device_ops = {
7542 .o_owner = THIS_MODULE,
7543 .o_set_info_async = mdt_obd_set_info_async,
7544 .o_connect = mdt_obd_connect,
7545 .o_reconnect = mdt_obd_reconnect,
7546 .o_disconnect = mdt_obd_disconnect,
7547 .o_init_export = mdt_init_export,
7548 .o_destroy_export = mdt_destroy_export,
7549 .o_iocontrol = mdt_iocontrol,
7550 .o_postrecov = mdt_obd_postrecov,
7551 /* Data-on-MDT IO methods */
7552 .o_preprw = mdt_obd_preprw,
7553 .o_commitrw = mdt_obd_commitrw,
7556 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7557 struct lu_device *d)
7559 struct mdt_device *m = mdt_dev(d);
7566 static struct lu_device *mdt_device_free(const struct lu_env *env,
7567 struct lu_device *d)
7569 struct mdt_device *m = mdt_dev(d);
7572 lu_device_fini(&m->mdt_lu_dev);
7578 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7579 struct lu_device_type *t,
7580 struct lustre_cfg *cfg)
7582 struct lu_device *l;
7583 struct mdt_device *m;
7590 rc = mdt_init0(env, m, t, cfg);
7592 mdt_device_free(env, l);
7597 l = ERR_PTR(-ENOMEM);
7601 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7602 LU_KEY_INIT(mdt, struct mdt_thread_info);
7604 static void mdt_key_fini(const struct lu_context *ctx,
7605 struct lu_context_key *key, void* data)
7607 struct mdt_thread_info *info = data;
7609 if (info->mti_big_lmm) {
7610 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7611 info->mti_big_lmm = NULL;
7612 info->mti_big_lmmsize = 0;
7615 if (info->mti_big_acl) {
7616 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7617 info->mti_big_acl = NULL;
7618 info->mti_big_aclsize = 0;
7624 /* context key: mdt_thread_key */
7625 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7627 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7629 return lu_ucred(info->mti_env);
7632 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7634 return lu_ucred_check(info->mti_env);
7638 * Enable/disable COS (Commit On Sharing).
7640 * Set/Clear the COS flag in mdt options.
7642 * \param mdt mdt device
7643 * \param val 0 disables COS, other values enable COS
7645 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7650 mdt->mdt_opts.mo_cos = val;
7651 rc = lu_env_init(&env, LCT_LOCAL);
7652 if (unlikely(rc != 0)) {
7653 CWARN("%s: lu_env initialization failed, cannot "
7654 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7657 mdt_device_sync(&env, mdt);
7662 * Check COS (Commit On Sharing) status.
7664 * Return COS flag status.
7666 * \param mdt mdt device
7668 int mdt_cos_is_enabled(struct mdt_device *mdt)
7670 return mdt->mdt_opts.mo_cos != 0;
7673 static const struct lu_device_type_operations mdt_device_type_ops = {
7674 .ldto_device_alloc = mdt_device_alloc,
7675 .ldto_device_free = mdt_device_free,
7676 .ldto_device_fini = mdt_device_fini
7679 static struct lu_device_type mdt_device_type = {
7680 .ldt_tags = LU_DEVICE_MD,
7681 .ldt_name = LUSTRE_MDT_NAME,
7682 .ldt_ops = &mdt_device_type_ops,
7683 .ldt_ctx_tags = LCT_MD_THREAD
7686 static int __init mdt_init(void)
7690 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7691 FID_NOBRACE_LEN + 1);
7692 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7694 rc = lu_kmem_init(mdt_caches);
7698 rc = mds_mod_init();
7702 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7703 LUSTRE_MDT_NAME, &mdt_device_type);
7708 lu_kmem_fini(mdt_caches);
7715 static void __exit mdt_exit(void)
7717 class_unregister_type(LUSTRE_MDT_NAME);
7719 lu_kmem_fini(mdt_caches);
7722 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7723 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7724 MODULE_VERSION(LUSTRE_VERSION_STRING);
7725 MODULE_LICENSE("GPL");
7727 module_init(mdt_init);
7728 module_exit(mdt_exit);