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 mdt_device *mdt = info->mti_mdt;
805 struct obd_export *exp = info->mti_exp;
806 struct md_attr *ma = &info->mti_attr;
807 struct lu_nodemap *nodemap = NULL;
809 LASSERT(ma->ma_valid & MA_INODE);
811 if (attr->la_valid & LA_ATIME) {
812 b->mbo_atime = attr->la_atime;
813 b->mbo_valid |= OBD_MD_FLATIME;
815 if (attr->la_valid & LA_MTIME) {
816 b->mbo_mtime = attr->la_mtime;
817 b->mbo_valid |= OBD_MD_FLMTIME;
819 if (attr->la_valid & LA_CTIME) {
820 b->mbo_ctime = attr->la_ctime;
821 b->mbo_valid |= OBD_MD_FLCTIME;
823 if (attr->la_valid & LA_BTIME) {
824 b->mbo_btime = attr->la_btime;
825 b->mbo_valid |= OBD_MD_FLBTIME;
827 if (attr->la_valid & LA_FLAGS) {
828 b->mbo_flags = attr->la_flags;
829 b->mbo_valid |= OBD_MD_FLFLAGS;
831 if (attr->la_valid & LA_NLINK) {
832 b->mbo_nlink = attr->la_nlink;
833 b->mbo_valid |= OBD_MD_FLNLINK;
835 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
836 nodemap = nodemap_get_from_exp(exp);
840 if (attr->la_valid & LA_UID) {
841 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
842 NODEMAP_FS_TO_CLIENT,
844 b->mbo_valid |= OBD_MD_FLUID;
846 if (attr->la_valid & LA_GID) {
847 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
848 NODEMAP_FS_TO_CLIENT,
850 b->mbo_valid |= OBD_MD_FLGID;
853 if (attr->la_valid & LA_PROJID) {
854 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
855 NODEMAP_FS_TO_CLIENT,
857 b->mbo_valid |= OBD_MD_FLPROJID;
860 b->mbo_mode = attr->la_mode;
861 if (attr->la_valid & LA_MODE)
862 b->mbo_valid |= OBD_MD_FLMODE;
863 if (attr->la_valid & LA_TYPE)
864 b->mbo_valid |= OBD_MD_FLTYPE;
868 b->mbo_valid |= OBD_MD_FLID;
869 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
870 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
873 if (!(attr->la_valid & LA_TYPE))
876 b->mbo_rdev = attr->la_rdev;
877 b->mbo_size = attr->la_size;
878 b->mbo_blocks = attr->la_blocks;
880 if (!S_ISREG(attr->la_mode)) {
881 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
882 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
883 /* means no objects are allocated on osts. */
884 LASSERT(!(ma->ma_valid & MA_LOV));
885 /* just ignore blocks occupied by extend attributes on MDS */
887 /* if no object is allocated on osts, the size on mds is valid.
889 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
890 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
891 if (mdt_hsm_is_released(ma->ma_lmm)) {
892 /* A released file stores its size on MDS. */
893 /* But return 1 block for released file, unless tools
894 * like tar will consider it fully sparse. (LU-3864)
896 if (unlikely(b->mbo_size == 0))
900 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
901 } else if (info->mti_som_strict &&
902 mdt->mdt_opts.mo_enable_strict_som) {
903 /* use SOM for size*/
904 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
905 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
906 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
907 b->mbo_size = ma->ma_som.ms_size;
908 b->mbo_blocks = ma->ma_som.ms_blocks;
912 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
913 b->mbo_valid & OBD_MD_FLLAZYSIZE))
914 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
915 PFID(fid), (unsigned long long)b->mbo_size);
918 if (!IS_ERR_OR_NULL(nodemap))
919 nodemap_putref(nodemap);
922 static inline int mdt_body_has_lov(const struct lu_attr *la,
923 const struct mdt_body *body)
925 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
926 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
929 void mdt_client_compatibility(struct mdt_thread_info *info)
931 struct mdt_body *body;
932 struct ptlrpc_request *req = mdt_info_req(info);
933 struct obd_export *exp = req->rq_export;
934 struct md_attr *ma = &info->mti_attr;
935 struct lu_attr *la = &ma->ma_attr;
938 if (exp_connect_layout(exp))
939 /* the client can deal with 16-bit lmm_stripe_count */
942 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
944 if (!mdt_body_has_lov(la, body))
947 /* now we have a reply with a lov for a client not compatible with the
948 * layout lock so we have to clean the layout generation number */
949 if (S_ISREG(la->la_mode))
950 ma->ma_lmm->lmm_layout_gen = 0;
954 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
955 struct mdt_object *o)
957 const struct lu_env *env = info->mti_env;
960 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
969 /* Is it a directory? Let's check for the LMV as well */
970 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
971 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
975 rc2 = mo_xattr_get(env, mdt_object_child(o),
977 XATTR_NAME_DEFAULT_LMV);
979 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
987 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
990 const struct lu_env *env = info->mti_env;
994 LASSERT(info->mti_big_lmm_used == 0);
995 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
999 /* big_lmm may need to be grown */
1000 if (info->mti_big_lmmsize < rc) {
1001 int size = size_roundup_power2(rc);
1003 if (info->mti_big_lmmsize > 0) {
1004 /* free old buffer */
1005 LASSERT(info->mti_big_lmm);
1006 OBD_FREE_LARGE(info->mti_big_lmm,
1007 info->mti_big_lmmsize);
1008 info->mti_big_lmm = NULL;
1009 info->mti_big_lmmsize = 0;
1012 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1013 if (info->mti_big_lmm == NULL)
1015 info->mti_big_lmmsize = size;
1017 LASSERT(info->mti_big_lmmsize >= rc);
1019 info->mti_buf.lb_buf = info->mti_big_lmm;
1020 info->mti_buf.lb_len = info->mti_big_lmmsize;
1021 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1026 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1027 struct md_attr *ma, const char *name)
1029 struct md_object *next = mdt_object_child(o);
1030 struct lu_buf *buf = &info->mti_buf;
1033 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1034 buf->lb_buf = ma->ma_lmm;
1035 buf->lb_len = ma->ma_lmm_size;
1036 LASSERT(!(ma->ma_valid & MA_LOV));
1037 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1038 buf->lb_buf = ma->ma_lmv;
1039 buf->lb_len = ma->ma_lmv_size;
1040 LASSERT(!(ma->ma_valid & MA_LMV));
1041 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1042 buf->lb_buf = ma->ma_default_lmv;
1043 buf->lb_len = ma->ma_default_lmv_size;
1044 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1049 LASSERT(buf->lb_buf);
1051 rc = mo_xattr_get(info->mti_env, next, buf, name);
1055 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1056 if (info->mti_big_lmm_used)
1057 ma->ma_lmm = info->mti_big_lmm;
1059 /* NOT return LOV EA with hole to old client. */
1060 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1061 LOV_PATTERN_F_HOLE) &&
1062 !(exp_connect_flags(info->mti_exp) &
1063 OBD_CONNECT_LFSCK)) {
1066 ma->ma_lmm_size = rc;
1067 ma->ma_valid |= MA_LOV;
1069 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1070 if (info->mti_big_lmm_used)
1071 ma->ma_lmv = info->mti_big_lmm;
1073 ma->ma_lmv_size = rc;
1074 ma->ma_valid |= MA_LMV;
1075 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1076 ma->ma_default_lmv_size = rc;
1077 ma->ma_valid |= MA_LMV_DEF;
1080 /* Update mdt_max_mdsize so all clients will be aware that */
1081 if (info->mti_mdt->mdt_max_mdsize < rc)
1082 info->mti_mdt->mdt_max_mdsize = rc;
1085 } else if (rc == -ENODATA) {
1088 } else if (rc == -ERANGE) {
1089 /* Default LMV has fixed size, so it must be able to fit
1090 * in the original buffer */
1091 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1093 rc = mdt_big_xattr_get(info, o, name);
1095 info->mti_big_lmm_used = 1;
1103 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1104 struct md_attr *ma, const char *name)
1108 if (!info->mti_big_lmm) {
1109 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1110 if (!info->mti_big_lmm)
1112 info->mti_big_lmmsize = PAGE_SIZE;
1115 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1116 ma->ma_lmm = info->mti_big_lmm;
1117 ma->ma_lmm_size = info->mti_big_lmmsize;
1118 ma->ma_valid &= ~MA_LOV;
1119 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1120 ma->ma_lmv = info->mti_big_lmm;
1121 ma->ma_lmv_size = info->mti_big_lmmsize;
1122 ma->ma_valid &= ~MA_LMV;
1127 LASSERT(!info->mti_big_lmm_used);
1128 rc = __mdt_stripe_get(info, o, ma, name);
1129 /* since big_lmm is always used here, clear 'used' flag to avoid
1130 * assertion in mdt_big_xattr_get().
1132 info->mti_big_lmm_used = 0;
1137 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1138 struct lu_fid *pfid)
1140 struct lu_buf *buf = &info->mti_buf;
1141 struct link_ea_header *leh;
1142 struct link_ea_entry *lee;
1146 buf->lb_buf = info->mti_big_lmm;
1147 buf->lb_len = info->mti_big_lmmsize;
1148 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1149 buf, XATTR_NAME_LINK);
1150 /* ignore errors, MA_PFID won't be set and it is
1151 * up to the caller to treat this as an error */
1152 if (rc == -ERANGE || buf->lb_len == 0) {
1153 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1154 buf->lb_buf = info->mti_big_lmm;
1155 buf->lb_len = info->mti_big_lmmsize;
1160 if (rc < sizeof(*leh)) {
1161 CERROR("short LinkEA on "DFID": rc = %d\n",
1162 PFID(mdt_object_fid(o)), rc);
1166 leh = (struct link_ea_header *) buf->lb_buf;
1167 lee = (struct link_ea_entry *)(leh + 1);
1168 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1169 leh->leh_magic = LINK_EA_MAGIC;
1170 leh->leh_reccount = __swab32(leh->leh_reccount);
1171 leh->leh_len = __swab64(leh->leh_len);
1173 if (leh->leh_magic != LINK_EA_MAGIC)
1175 if (leh->leh_reccount == 0)
1178 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1179 fid_be_to_cpu(pfid, pfid);
1184 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1185 struct lu_fid *pfid, struct lu_name *lname)
1187 struct lu_buf *buf = &info->mti_buf;
1188 struct link_ea_header *leh;
1189 struct link_ea_entry *lee;
1193 buf->lb_buf = info->mti_xattr_buf;
1194 buf->lb_len = sizeof(info->mti_xattr_buf);
1195 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1197 if (rc == -ERANGE) {
1198 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1199 buf->lb_buf = info->mti_big_lmm;
1200 buf->lb_len = info->mti_big_lmmsize;
1205 if (rc < sizeof(*leh)) {
1206 CERROR("short LinkEA on "DFID": rc = %d\n",
1207 PFID(mdt_object_fid(o)), rc);
1211 leh = (struct link_ea_header *)buf->lb_buf;
1212 lee = (struct link_ea_entry *)(leh + 1);
1213 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1214 leh->leh_magic = LINK_EA_MAGIC;
1215 leh->leh_reccount = __swab32(leh->leh_reccount);
1216 leh->leh_len = __swab64(leh->leh_len);
1218 if (leh->leh_magic != LINK_EA_MAGIC)
1221 if (leh->leh_reccount == 0)
1224 linkea_entry_unpack(lee, &reclen, lname, pfid);
1229 int mdt_attr_get_complex(struct mdt_thread_info *info,
1230 struct mdt_object *o, struct md_attr *ma)
1232 const struct lu_env *env = info->mti_env;
1233 struct md_object *next = mdt_object_child(o);
1234 struct lu_buf *buf = &info->mti_buf;
1235 int need = ma->ma_need;
1242 if (mdt_object_exists(o) == 0)
1243 GOTO(out, rc = -ENOENT);
1244 mode = lu_object_attr(&next->mo_lu);
1246 if (need & MA_INODE) {
1247 ma->ma_need = MA_INODE;
1248 rc = mo_attr_get(env, next, ma);
1253 (void) mdt_get_som(info, o, ma);
1254 ma->ma_valid |= MA_INODE;
1257 if (need & MA_PFID) {
1258 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1260 ma->ma_valid |= MA_PFID;
1261 /* ignore this error, parent fid is not mandatory */
1265 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1266 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1271 if (need & MA_LMV && S_ISDIR(mode)) {
1272 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1277 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1278 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1284 * In the handle of MA_INODE, we may already get the SOM attr.
1286 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1287 rc = mdt_get_som(info, o, ma);
1292 if (need & MA_HSM && S_ISREG(mode)) {
1293 buf->lb_buf = info->mti_xattr_buf;
1294 buf->lb_len = sizeof(info->mti_xattr_buf);
1295 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1296 sizeof(info->mti_xattr_buf));
1297 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1298 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1300 ma->ma_valid |= MA_HSM;
1301 else if (rc2 < 0 && rc2 != -ENODATA)
1302 GOTO(out, rc = rc2);
1305 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1306 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1307 buf->lb_buf = ma->ma_acl;
1308 buf->lb_len = ma->ma_acl_size;
1309 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1311 ma->ma_acl_size = rc2;
1312 ma->ma_valid |= MA_ACL_DEF;
1313 } else if (rc2 == -ENODATA) {
1315 ma->ma_acl_size = 0;
1317 GOTO(out, rc = rc2);
1322 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1323 rc, ma->ma_valid, ma->ma_lmm);
1327 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
1329 struct req_capsule *pill = info->mti_pill;
1332 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
1334 /* pre-set size in server part with max size */
1335 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
1337 info->mti_mdt->mdt_max_mdsize);
1341 static int mdt_getattr_internal(struct mdt_thread_info *info,
1342 struct mdt_object *o, int ma_need)
1344 struct mdt_device *mdt = info->mti_mdt;
1345 struct md_object *next = mdt_object_child(o);
1346 const struct mdt_body *reqbody = info->mti_body;
1347 struct ptlrpc_request *req = mdt_info_req(info);
1348 struct md_attr *ma = &info->mti_attr;
1349 struct lu_attr *la = &ma->ma_attr;
1350 struct req_capsule *pill = info->mti_pill;
1351 const struct lu_env *env = info->mti_env;
1352 struct mdt_body *repbody;
1353 struct lu_buf *buffer = &info->mti_buf;
1354 struct obd_export *exp = info->mti_exp;
1355 ktime_t kstart = ktime_get();
1360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1361 RETURN(err_serious(-ENOMEM));
1363 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1367 if (mdt_object_remote(o)) {
1368 /* This object is located on remote node.*/
1369 /* Return -ENOTSUPP for old client */
1370 if (!mdt_is_dne_client(req->rq_export))
1371 GOTO(out, rc = -ENOTSUPP);
1373 repbody->mbo_fid1 = *mdt_object_fid(o);
1374 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1378 if (reqbody->mbo_eadatasize > 0) {
1379 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1380 if (buffer->lb_buf == NULL)
1381 GOTO(out, rc = -EPROTO);
1382 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1385 buffer->lb_buf = NULL;
1387 ma_need &= ~(MA_LOV | MA_LMV);
1388 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1389 mdt_obd_name(info->mti_mdt),
1390 req->rq_export->exp_client_uuid.uuid);
1393 /* from 2.12.58 intent_getattr pack default LMV in reply */
1394 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1395 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1396 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1397 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1399 ma->ma_lmv = buffer->lb_buf;
1400 ma->ma_lmv_size = buffer->lb_len;
1401 ma->ma_default_lmv = req_capsule_server_get(pill,
1402 &RMF_DEFAULT_MDT_MD);
1403 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1404 &RMF_DEFAULT_MDT_MD,
1406 ma->ma_need = MA_INODE;
1407 if (ma->ma_lmv_size > 0)
1408 ma->ma_need |= MA_LMV;
1409 if (ma->ma_default_lmv_size > 0)
1410 ma->ma_need |= MA_LMV_DEF;
1411 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1412 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1413 /* If it is dir and client require MEA, then we got MEA */
1414 /* Assumption: MDT_MD size is enough for lmv size. */
1415 ma->ma_lmv = buffer->lb_buf;
1416 ma->ma_lmv_size = buffer->lb_len;
1417 ma->ma_need = MA_INODE;
1418 if (ma->ma_lmv_size > 0) {
1419 if (reqbody->mbo_valid & OBD_MD_MEA) {
1420 ma->ma_need |= MA_LMV;
1421 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1422 ma->ma_need |= MA_LMV_DEF;
1423 ma->ma_default_lmv = buffer->lb_buf;
1425 ma->ma_default_lmv_size = buffer->lb_len;
1426 ma->ma_lmv_size = 0;
1430 ma->ma_lmm = buffer->lb_buf;
1431 ma->ma_lmm_size = buffer->lb_len;
1432 ma->ma_need = MA_INODE | MA_HSM;
1433 if (ma->ma_lmm_size > 0) {
1434 ma->ma_need |= MA_LOV;
1435 /* Older clients may crash if they getattr overstriped
1438 if (!exp_connect_overstriping(exp) &&
1439 mdt_lmm_is_overstriping(ma->ma_lmm))
1440 RETURN(-EOPNOTSUPP);
1444 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1445 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1446 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1447 /* get default stripe info for this dir. */
1448 ma->ma_need |= MA_LOV_DEF;
1450 ma->ma_need |= ma_need;
1452 rc = mdt_attr_get_complex(info, o, ma);
1454 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1455 "%s: getattr error for "DFID": rc = %d\n",
1456 mdt_obd_name(info->mti_mdt),
1457 PFID(mdt_object_fid(o)), rc);
1461 /* if file is released, check if a restore is running */
1462 if (ma->ma_valid & MA_HSM) {
1463 repbody->mbo_valid |= OBD_MD_TSTATE;
1464 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1465 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1466 repbody->mbo_t_state = MS_RESTORE;
1469 if (unlikely(!(ma->ma_valid & MA_INODE)))
1472 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1474 if (mdt_body_has_lov(la, reqbody)) {
1475 u32 stripe_count = 1;
1476 bool fixed_layout = false;
1478 if (ma->ma_valid & MA_LOV) {
1479 LASSERT(ma->ma_lmm_size);
1480 repbody->mbo_eadatasize = ma->ma_lmm_size;
1481 if (S_ISDIR(la->la_mode))
1482 repbody->mbo_valid |= OBD_MD_FLDIREA;
1484 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1485 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1487 if (ma->ma_valid & MA_LMV) {
1488 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1489 u32 magic = le32_to_cpu(lmv->lmv_magic);
1491 /* Return -ENOTSUPP for old client */
1492 if (!mdt_is_striped_client(req->rq_export))
1495 LASSERT(S_ISDIR(la->la_mode));
1496 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1497 repbody->mbo_eadatasize = ma->ma_lmv_size;
1498 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1500 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1501 fixed_layout = lmv_is_fixed(lmv);
1502 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1503 mdt_restripe_migrate_add(info, o);
1504 else if (magic == LMV_MAGIC_V1 &&
1505 lmv_is_restriping(lmv))
1506 mdt_restripe_update_add(info, o);
1508 if (ma->ma_valid & MA_LMV_DEF) {
1509 /* Return -ENOTSUPP for old client */
1510 if (!mdt_is_striped_client(req->rq_export))
1512 LASSERT(S_ISDIR(la->la_mode));
1514 * when ll_dir_getstripe() gets default LMV, it
1515 * checks mbo_eadatasize.
1517 if (!(ma->ma_valid & MA_LMV))
1518 repbody->mbo_eadatasize =
1519 ma->ma_default_lmv_size;
1520 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1521 OBD_MD_DEFAULT_MEA);
1524 "dirent count %llu stripe count %u MDT count %d\n",
1525 ma->ma_attr.la_dirent_count, stripe_count,
1526 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1527 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1528 ma->ma_attr.la_dirent_count >
1529 mdt->mdt_restriper.mdr_dir_split_count &&
1530 !fid_is_root(mdt_object_fid(o)) &&
1531 mdt->mdt_enable_dir_auto_split &&
1532 !o->mot_restriping &&
1533 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1535 mdt_auto_split_add(info, o);
1536 } else if (S_ISLNK(la->la_mode) &&
1537 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1538 buffer->lb_buf = ma->ma_lmm;
1539 /* eadatasize from client includes NULL-terminator, so
1540 * there is no need to read it */
1541 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1542 rc = mo_readlink(env, next, buffer);
1543 if (unlikely(rc <= 0)) {
1544 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1545 mdt_obd_name(info->mti_mdt),
1546 PFID(mdt_object_fid(o)), rc);
1549 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1551 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1553 repbody->mbo_valid |= OBD_MD_LINKNAME;
1554 /* we need to report back size with NULL-terminator
1555 * because client expects that */
1556 repbody->mbo_eadatasize = rc + 1;
1557 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1558 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1559 "on "DFID ", expected %d\n",
1560 mdt_obd_name(info->mti_mdt),
1561 rc, PFID(mdt_object_fid(o)),
1562 reqbody->mbo_eadatasize - 1);
1563 /* NULL terminate */
1564 ((char *)ma->ma_lmm)[rc] = 0;
1566 /* If the total CDEBUG() size is larger than a page, it
1567 * will print a warning to the console, avoid this by
1568 * printing just the last part of the symlink. */
1569 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1570 print_limit < rc ? "..." : "", print_limit,
1571 (char *)ma->ma_lmm + rc - print_limit, rc);
1576 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1577 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1578 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1579 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1580 repbody->mbo_max_mdsize);
1583 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1584 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1585 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1586 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1587 if (IS_ERR(nodemap))
1588 RETURN(PTR_ERR(nodemap));
1590 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1591 nodemap_putref(nodemap);
1597 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1598 ktime_us_delta(ktime_get(), kstart));
1603 static int mdt_getattr(struct tgt_session_info *tsi)
1605 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1606 struct mdt_object *obj = info->mti_object;
1607 struct req_capsule *pill = info->mti_pill;
1608 struct mdt_body *reqbody;
1609 struct mdt_body *repbody;
1613 if (unlikely(info->mti_object == NULL))
1616 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1618 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1620 /* Special case for Data-on-MDT files to get data version */
1621 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1622 rc = mdt_data_version_get(tsi);
1626 /* Unlike intent case where we need to pre-fill out buffers early on
1627 * in intent policy for ldlm reasons, here we can have a much better
1628 * guess at EA size by just reading it from disk.
1629 * Exceptions are readdir and (missing) directory striping */
1631 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1632 /* No easy way to know how long is the symlink, but it cannot
1633 * be more than PATH_MAX, so we allocate +1 */
1635 /* A special case for fs ROOT: getattr there might fetch
1636 * default EA for entire fs, not just for this dir!
1638 } else if (lu_fid_eq(mdt_object_fid(obj),
1639 &info->mti_mdt->mdt_md_root_fid) &&
1640 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1641 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1643 /* Should the default strping be bigger, mdt_fix_reply
1644 * will reallocate */
1645 rc = DEF_REP_MD_SIZE;
1647 /* Read the actual EA size from disk */
1648 rc = mdt_attr_get_eabuf_size(info, obj);
1652 GOTO(out, rc = err_serious(rc));
1654 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1656 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1657 * by default. If the target object has more ACL entries, then
1658 * enlarge the buffer when necessary. */
1659 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1660 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1661 mdt_preset_encctx_size(info);
1663 rc = req_capsule_server_pack(pill);
1664 if (unlikely(rc != 0))
1665 GOTO(out, rc = err_serious(rc));
1667 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1668 LASSERT(repbody != NULL);
1669 repbody->mbo_eadatasize = 0;
1670 repbody->mbo_aclsize = 0;
1672 rc = mdt_check_ucred(info);
1674 GOTO(out_shrink, rc);
1676 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1678 rc = mdt_getattr_internal(info, obj, 0);
1680 GOTO(out_shrink, rc);
1682 rc = mdt_pack_encctx_in_reply(info, obj);
1685 mdt_client_compatibility(info);
1686 rc2 = mdt_fix_reply(info);
1690 mdt_thread_info_fini(info);
1695 * Handler of layout intent RPC requiring the layout modification
1697 * \param[in] info thread environment
1698 * \param[in] obj object
1699 * \param[out] lhc object ldlm lock handle
1700 * \param[in] layout layout change descriptor
1702 * \retval 0 on success
1703 * \retval < 0 error code
1705 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1706 struct mdt_lock_handle *lhc,
1707 struct md_layout_change *layout)
1713 if (!mdt_object_exists(obj))
1716 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1719 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1724 rc = mdt_check_resent_lock(info, obj, lhc);
1730 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1732 /* take layout lock to prepare layout change */
1733 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1734 lockpart |= MDS_INODELOCK_UPDATE;
1736 mdt_lock_handle_init(lhc);
1737 mdt_lock_reg_init(lhc, LCK_EX);
1738 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1743 mutex_lock(&obj->mot_som_mutex);
1744 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1745 mutex_unlock(&obj->mot_som_mutex);
1748 mdt_object_unlock(info, obj, lhc, 1);
1754 * Exchange MOF_LOV_CREATED flags between two objects after a
1755 * layout swap. No assumption is made on whether o1 or o2 have
1756 * created objects or not.
1758 * \param[in,out] o1 First swap layout object
1759 * \param[in,out] o2 Second swap layout object
1761 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1763 unsigned int o1_lov_created = o1->mot_lov_created;
1765 mutex_lock(&o1->mot_lov_mutex);
1766 mutex_lock(&o2->mot_lov_mutex);
1768 o1->mot_lov_created = o2->mot_lov_created;
1769 o2->mot_lov_created = o1_lov_created;
1771 mutex_unlock(&o2->mot_lov_mutex);
1772 mutex_unlock(&o1->mot_lov_mutex);
1775 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1777 struct mdt_thread_info *info;
1778 struct ptlrpc_request *req = tgt_ses_req(tsi);
1779 struct obd_export *exp = req->rq_export;
1780 struct mdt_object *o1, *o2, *o;
1781 struct mdt_lock_handle *lh1, *lh2;
1782 struct mdc_swap_layouts *msl;
1786 /* client does not support layout lock, so layout swaping
1788 * FIXME: there is a problem for old clients which don't support
1789 * layout lock yet. If those clients have already opened the file
1790 * they won't be notified at all so that old layout may still be
1791 * used to do IO. This can be fixed after file release is landed by
1792 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1793 if (!exp_connect_layout(exp))
1794 RETURN(-EOPNOTSUPP);
1796 info = tsi2mdt_info(tsi);
1797 if (unlikely(info->mti_object == NULL))
1800 if (info->mti_dlm_req != NULL)
1801 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1803 o1 = info->mti_object;
1804 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1805 &info->mti_body->mbo_fid2);
1807 GOTO(out, rc = PTR_ERR(o));
1809 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1810 GOTO(put, rc = -ENOENT);
1812 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1813 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1819 /* permission check. Make sure the calling process having permission
1820 * to write both files. */
1821 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1826 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1831 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1833 GOTO(put, rc = -EPROTO);
1835 lh1 = &info->mti_lh[MDT_LH_NEW];
1836 mdt_lock_reg_init(lh1, LCK_EX);
1837 lh2 = &info->mti_lh[MDT_LH_OLD];
1838 mdt_lock_reg_init(lh2, LCK_EX);
1840 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1841 MDS_INODELOCK_XATTR);
1845 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1846 MDS_INODELOCK_XATTR);
1850 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1851 mdt_object_child(o2), msl->msl_flags);
1855 mdt_swap_lov_flag(o1, o2);
1858 mdt_object_unlock(info, o2, lh2, rc);
1860 mdt_object_unlock(info, o1, lh1, rc);
1862 mdt_object_put(info->mti_env, o);
1864 mdt_thread_info_fini(info);
1868 static int mdt_raw_lookup(struct mdt_thread_info *info,
1869 struct mdt_object *parent,
1870 const struct lu_name *lname)
1872 struct lu_fid *fid = &info->mti_tmp_fid1;
1873 struct mdt_body *repbody;
1874 bool is_dotdot = false;
1875 bool is_old_parent_stripe = false;
1876 bool is_new_parent_checked = false;
1881 LASSERT(!info->mti_cross_ref);
1882 /* Always allow to lookup ".." */
1883 if (lname->ln_namelen == 2 &&
1884 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1885 info->mti_spec.sp_permitted = 1;
1887 if (mdt_is_dir_stripe(info, parent) == 1)
1888 is_old_parent_stripe = true;
1891 mdt_object_get(info->mti_env, parent);
1893 /* Only got the fid of this obj by name */
1895 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1897 mdt_object_put(info->mti_env, parent);
1901 /* getattr_name("..") should return master object FID for striped dir */
1902 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1903 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1905 RETURN(PTR_ERR(parent));
1907 /* old client getattr_name("..") with stripe FID */
1908 if (unlikely(is_old_parent_stripe)) {
1909 is_old_parent_stripe = false;
1913 /* ".." may be a stripe */
1914 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1915 is_new_parent_checked = true;
1919 mdt_object_put(info->mti_env, parent);
1922 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1923 repbody->mbo_fid1 = *fid;
1924 repbody->mbo_valid = OBD_MD_FLID;
1930 * Find name matching hash
1932 * We search \a child LinkEA for a name whose hash matches \a lname
1933 * (it contains an encoded hash).
1935 * \param info mdt thread info
1936 * \param lname encoded hash to find
1937 * \param parent parent object
1938 * \param child object to search with LinkEA
1940 * \retval 1 match found
1941 * \retval 0 no match found
1942 * \retval -ev negative errno upon error
1944 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
1945 struct mdt_object *parent, struct mdt_object *child)
1947 /* Here, lname is an encoded hash of on-disk name, and
1948 * client is doing access without encryption key.
1949 * So we need to get LinkEA, check parent fid is correct and
1950 * compare name hash with the one in the request.
1952 struct lu_buf *buf = &info->mti_big_buf;
1953 struct lu_name name;
1955 struct linkea_data ldata = { NULL };
1956 struct link_ea_header *leh;
1957 struct link_ea_entry *lee;
1958 struct lu_buf link = { 0 };
1960 int reclen, count, rc;
1963 if (lname->ln_namelen < LL_CRYPTO_BLOCK_SIZE)
1966 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1971 rc = mdt_links_read(info, child, &ldata);
1975 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
1978 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
1981 lee = (struct link_ea_entry *)(leh + 1);
1982 for (count = 0; count < leh->leh_reccount; count++) {
1983 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1984 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
1985 lu_buf_check_and_alloc(&link, name.ln_namelen);
1987 GOTO(out_match, rc = -ENOMEM);
1988 rc = critical_decode(name.ln_name, name.ln_namelen,
1991 if (memcmp(LLCRYPT_EXTRACT_DIGEST(link.lb_buf, rc),
1992 hash, LL_CRYPTO_BLOCK_SIZE) == 0) {
1997 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1999 if (count == leh->leh_reccount)
2012 * UPDATE lock should be taken against parent, and be released before exit;
2013 * child_bits lock should be taken against child, and be returned back:
2014 * (1)normal request should release the child lock;
2015 * (2)intent request will grant the lock to client.
2017 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2018 struct mdt_lock_handle *lhc,
2020 struct ldlm_reply *ldlm_rep)
2022 struct ptlrpc_request *req = mdt_info_req(info);
2023 struct mdt_body *reqbody = NULL;
2024 struct mdt_object *parent = info->mti_object;
2025 struct mdt_object *child = NULL;
2026 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2027 struct lu_name *lname = NULL;
2028 struct mdt_lock_handle *lhp = NULL;
2029 struct ldlm_lock *lock;
2030 struct req_capsule *pill = info->mti_pill;
2038 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2039 LASSERT(ergo(is_resent,
2040 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2045 if (info->mti_cross_ref) {
2046 /* Only getattr on the child. Parent is on another node. */
2047 mdt_set_disposition(info, ldlm_rep,
2048 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2050 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
2052 PFID(mdt_object_fid(child)), ldlm_rep);
2054 rc = mdt_check_resent_lock(info, child, lhc);
2057 } else if (rc > 0) {
2058 mdt_lock_handle_init(lhc);
2059 mdt_lock_reg_init(lhc, LCK_PR);
2062 * Object's name entry is on another MDS, it will
2063 * request PERM lock only because LOOKUP lock is owned
2064 * by the MDS where name entry resides.
2066 * TODO: it should try layout lock too. - Jinshan
2068 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2069 MDS_INODELOCK_LAYOUT);
2070 child_bits |= MDS_INODELOCK_PERM;
2072 rc = mdt_object_lock(info, child, lhc, child_bits);
2077 /* Finally, we can get attr for child. */
2078 if (!mdt_object_exists(child)) {
2079 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2081 "remote object doesn't exist.");
2082 mdt_object_unlock(info, child, lhc, 1);
2086 rc = mdt_getattr_internal(info, child, 0);
2087 if (unlikely(rc != 0)) {
2088 mdt_object_unlock(info, child, lhc, 1);
2092 rc = mdt_pack_secctx_in_reply(info, child);
2094 mdt_object_unlock(info, child, lhc, 1);
2098 rc = mdt_pack_encctx_in_reply(info, child);
2100 mdt_object_unlock(info, child, lhc, 1);
2104 lname = &info->mti_name;
2105 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2107 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2108 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2109 if (unlikely(reqbody == NULL))
2110 RETURN(err_serious(-EPROTO));
2112 *child_fid = reqbody->mbo_fid2;
2113 if (unlikely(!fid_is_sane(child_fid)))
2114 RETURN(err_serious(-EINVAL));
2116 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2117 mdt_object_get(info->mti_env, parent);
2120 child = mdt_object_find(info->mti_env, info->mti_mdt,
2123 RETURN(PTR_ERR(child));
2126 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2128 PFID(mdt_object_fid(parent)),
2129 PFID(&reqbody->mbo_fid2), ldlm_rep);
2130 } else if (lu_name_is_valid(lname)) {
2131 if (mdt_object_remote(parent)) {
2132 CERROR("%s: parent "DFID" is on remote target\n",
2133 mdt_obd_name(info->mti_mdt),
2134 PFID(mdt_object_fid(parent)));
2138 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
2139 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
2140 PNAME(lname), ldlm_rep);
2142 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2143 if (unlikely(reqbody == NULL))
2144 RETURN(err_serious(-EPROTO));
2146 *child_fid = reqbody->mbo_fid2;
2147 if (unlikely(!fid_is_sane(child_fid)))
2148 RETURN(err_serious(-EINVAL));
2150 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2151 mdt_object_get(info->mti_env, parent);
2154 child = mdt_object_find(info->mti_env, info->mti_mdt,
2157 RETURN(PTR_ERR(child));
2160 if (mdt_object_remote(child)) {
2161 CERROR("%s: child "DFID" is on remote target\n",
2162 mdt_obd_name(info->mti_mdt),
2163 PFID(mdt_object_fid(child)));
2164 GOTO(out_child, rc = -EPROTO);
2167 /* don't fetch LOOKUP lock if it's remote object */
2168 rc = mdt_is_remote_object(info, parent, child);
2170 GOTO(out_child, rc);
2172 child_bits &= ~MDS_INODELOCK_LOOKUP;
2174 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2176 PFID(mdt_object_fid(parent)),
2177 PFID(&reqbody->mbo_fid2), ldlm_rep);
2180 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2182 if (unlikely(!mdt_object_exists(parent)) &&
2183 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2184 lu_name_is_valid(lname)) {
2185 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2187 "Parent doesn't exist!");
2188 GOTO(out_child, rc = -ESTALE);
2191 if (!child && is_resent) {
2192 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2194 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2195 * a resend case, however, it could be already destroyed
2196 * due to client eviction or a raced cancel RPC.
2198 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2199 lhc->mlh_reg_lh.cookie);
2202 fid_extract_from_res_name(child_fid,
2203 &lock->l_resource->lr_name);
2204 LDLM_LOCK_PUT(lock);
2205 child = mdt_object_find(info->mti_env, info->mti_mdt,
2208 RETURN(PTR_ERR(child));
2209 } else if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2210 lu_name_is_valid(lname)) {
2211 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2212 rc = mdt_raw_lookup(info, parent, lname);
2217 /* step 1: lock parent only if parent is a directory */
2218 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2219 lhp = &info->mti_lh[MDT_LH_PARENT];
2220 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2221 rc = mdt_object_lock(info, parent, lhp,
2222 MDS_INODELOCK_UPDATE);
2223 if (unlikely(rc != 0))
2227 /* step 2: lookup child's fid by name */
2228 fid_zero(child_fid);
2229 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2230 child_fid, &info->mti_spec);
2232 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2235 GOTO(unlock_parent, rc);
2237 child = mdt_object_find(info->mti_env, info->mti_mdt,
2239 if (unlikely(IS_ERR(child)))
2240 GOTO(unlock_parent, rc = PTR_ERR(child));
2243 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2245 /* step 3: lock child regardless if it is local or remote. */
2248 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2249 /* Here, lname is an encoded hash of on-disk name, and
2250 * client is doing access without encryption key.
2251 * So we need to compare name hash with the one in the request.
2253 if (!find_name_matching_hash(info, lname, parent,
2255 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2256 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2257 GOTO(out_child, rc = -ENOENT);
2261 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2262 if (!mdt_object_exists(child)) {
2263 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2265 "Object doesn't exist!");
2266 GOTO(out_child, rc = -ENOENT);
2269 rc = mdt_check_resent_lock(info, child, lhc);
2271 GOTO(out_child, rc);
2272 } else if (rc > 0) {
2273 mdt_lock_handle_init(lhc);
2274 mdt_lock_reg_init(lhc, LCK_PR);
2276 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2277 !mdt_object_remote(child)) {
2278 struct md_attr *ma = &info->mti_attr;
2281 ma->ma_need = MA_INODE;
2282 rc = mdt_attr_get_complex(info, child, ma);
2283 if (unlikely(rc != 0))
2284 GOTO(out_child, rc);
2286 /* If the file has not been changed for some time, we
2287 * return not only a LOOKUP lock, but also an UPDATE
2288 * lock and this might save us RPC on later STAT. For
2289 * directories, it also let negative dentry cache start
2290 * working for this dir. */
2291 if (ma->ma_valid & MA_INODE &&
2292 ma->ma_attr.la_valid & LA_CTIME &&
2293 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2294 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2295 child_bits |= MDS_INODELOCK_UPDATE;
2298 /* layout lock must be granted in a best-effort way
2299 * for IT operations */
2300 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2301 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2302 !mdt_object_remote(child) && ldlm_rep != NULL) {
2303 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2304 exp_connect_layout(info->mti_exp)) {
2305 /* try to grant layout lock for regular file. */
2306 try_bits = MDS_INODELOCK_LAYOUT;
2308 /* Acquire DOM lock in advance for data-on-mdt file */
2309 if (child != parent)
2310 try_bits |= MDS_INODELOCK_DOM;
2313 if (try_bits != 0) {
2314 /* try layout lock, it may fail to be granted due to
2315 * contention at LOOKUP or UPDATE */
2316 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2318 if (child_bits & MDS_INODELOCK_LAYOUT)
2321 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2322 * client will enqueue the lock to the remote MDT */
2323 if (mdt_object_remote(child))
2324 child_bits &= ~MDS_INODELOCK_UPDATE;
2325 rc = mdt_object_lock(info, child, lhc, child_bits);
2327 if (unlikely(rc != 0))
2328 GOTO(out_child, rc);
2331 /* finally, we can get attr for child. */
2332 rc = mdt_getattr_internal(info, child, ma_need);
2333 if (unlikely(rc != 0)) {
2335 mdt_object_unlock(info, child, lhc, 1);
2336 GOTO(out_child, rc);
2339 rc = mdt_pack_secctx_in_reply(info, child);
2342 mdt_object_unlock(info, child, lhc, 1);
2343 GOTO(out_child, rc);
2346 rc = mdt_pack_encctx_in_reply(info, child);
2349 mdt_object_unlock(info, child, lhc, 1);
2350 GOTO(out_child, rc);
2353 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2355 /* Debugging code. */
2356 LDLM_DEBUG(lock, "Returning lock to client");
2357 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2358 &lock->l_resource->lr_name),
2359 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2360 PLDLMRES(lock->l_resource),
2361 PFID(mdt_object_fid(child)));
2363 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2364 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2365 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2367 req->rq_arrival_time.tv_sec +
2369 /* Put the lock to the waiting list and force the cancel */
2370 ldlm_set_ast_sent(lock);
2373 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2374 !mdt_object_remote(child) && child != parent) {
2375 mdt_object_put(info->mti_env, child);
2376 rc = mdt_pack_size2body(info, child_fid,
2378 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2379 /* DOM lock was taken in advance but this is
2380 * not DoM file. Drop the lock.
2382 lock_res_and_lock(lock);
2383 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2384 unlock_res_and_lock(lock);
2386 LDLM_LOCK_PUT(lock);
2387 GOTO(unlock_parent, rc = 0);
2389 LDLM_LOCK_PUT(lock);
2395 mdt_object_put(info->mti_env, child);
2398 mdt_object_unlock(info, parent, lhp, 1);
2399 if (rc == -ENOENT) {
2400 /* return -ENOKEY instead of -ENOENT to encryption-unaware
2401 * client if trying to access an encrypted file
2403 int rc2 = mdt_check_enc(info, parent);
2411 /* normal handler: should release the child lock */
2412 static int mdt_getattr_name(struct tgt_session_info *tsi)
2414 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2415 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2416 struct mdt_body *reqbody;
2417 struct mdt_body *repbody;
2422 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2423 LASSERT(reqbody != NULL);
2424 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2425 LASSERT(repbody != NULL);
2427 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2428 repbody->mbo_eadatasize = 0;
2429 repbody->mbo_aclsize = 0;
2431 rc = mdt_init_ucred(info, reqbody);
2433 GOTO(out_shrink, rc);
2435 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2436 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2437 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2438 lhc->mlh_reg_lh.cookie = 0;
2440 mdt_exit_ucred(info);
2443 mdt_client_compatibility(info);
2444 rc2 = mdt_fix_reply(info);
2447 mdt_thread_info_fini(info);
2451 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2452 const struct lu_fid *pfid,
2453 const struct lu_name *name,
2454 struct mdt_object *obj, s64 ctime)
2456 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2457 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2458 struct mdt_device *mdt = info->mti_mdt;
2459 struct md_attr *ma = &info->mti_attr;
2460 struct mdt_lock_handle *parent_lh;
2461 struct mdt_lock_handle *child_lh;
2462 struct mdt_object *pobj;
2463 bool cos_incompat = false;
2467 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2469 GOTO(out, rc = PTR_ERR(pobj));
2471 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2472 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2473 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2475 GOTO(put_parent, rc);
2477 if (mdt_object_remote(pobj))
2478 cos_incompat = true;
2480 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2481 name, child_fid, &info->mti_spec);
2483 GOTO(unlock_parent, rc);
2485 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2486 GOTO(unlock_parent, rc = -EREMCHG);
2488 child_lh = &info->mti_lh[MDT_LH_CHILD];
2489 mdt_lock_reg_init(child_lh, LCK_EX);
2490 rc = mdt_reint_striped_lock(info, obj, child_lh,
2491 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2492 einfo, cos_incompat);
2494 GOTO(unlock_parent, rc);
2496 if (atomic_read(&obj->mot_open_count)) {
2497 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2498 PFID(mdt_object_fid(obj)));
2499 GOTO(unlock_child, rc = -EBUSY);
2503 ma->ma_valid = MA_INODE;
2504 ma->ma_attr.la_valid = LA_CTIME;
2505 ma->ma_attr.la_ctime = ctime;
2507 mutex_lock(&obj->mot_lov_mutex);
2509 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2510 mdt_object_child(obj), name, ma, 0);
2512 mutex_unlock(&obj->mot_lov_mutex);
2515 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2517 mdt_object_unlock(info, pobj, parent_lh, 1);
2519 mdt_object_put(info->mti_env, pobj);
2524 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2525 struct mdt_object *obj)
2527 struct lu_ucred *uc = lu_ucred(info->mti_env);
2528 struct md_attr *ma = &info->mti_attr;
2529 struct lu_attr *la = &ma->ma_attr;
2533 ma->ma_need = MA_INODE;
2534 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2538 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2541 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2543 if (uc->uc_fsuid == la->la_uid) {
2544 if ((la->la_mode & S_IWUSR) == 0)
2546 } else if (uc->uc_fsgid == la->la_gid) {
2547 if ((la->la_mode & S_IWGRP) == 0)
2549 } else if ((la->la_mode & S_IWOTH) == 0) {
2557 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2560 struct mdt_device *mdt = info->mti_mdt;
2561 struct mdt_object *obj = NULL;
2562 struct linkea_data ldata = { NULL };
2563 struct lu_buf *buf = &info->mti_big_buf;
2564 struct lu_name *name = &info->mti_name;
2565 struct lu_fid *pfid = &info->mti_tmp_fid1;
2566 struct link_ea_header *leh;
2567 struct link_ea_entry *lee;
2568 int reclen, count, rc = 0;
2571 if (!fid_is_sane(fid))
2572 GOTO(out, rc = -EINVAL);
2574 if (!fid_is_namespace_visible(fid))
2575 GOTO(out, rc = -EINVAL);
2577 obj = mdt_object_find(info->mti_env, mdt, fid);
2579 GOTO(out, rc = PTR_ERR(obj));
2581 if (mdt_object_remote(obj))
2582 GOTO(out, rc = -EREMOTE);
2583 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2584 GOTO(out, rc = -ENOENT);
2586 rc = mdt_rmfid_check_permission(info, obj);
2591 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2593 GOTO(out, rc = -ENOMEM);
2596 rc = mdt_links_read(info, obj, &ldata);
2601 lee = (struct link_ea_entry *)(leh + 1);
2602 for (count = 0; count < leh->leh_reccount; count++) {
2603 /* remove every hardlink */
2604 linkea_entry_unpack(lee, &reclen, name, pfid);
2605 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2606 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2612 if (obj && !IS_ERR(obj))
2613 mdt_object_put(info->mti_env, obj);
2614 if (info->mti_big_buf.lb_buf)
2615 lu_buf_free(&info->mti_big_buf);
2620 static int mdt_rmfid(struct tgt_session_info *tsi)
2622 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2623 struct mdt_body *reqbody;
2624 struct lu_fid *fids, *rfids;
2630 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2631 if (reqbody == NULL)
2633 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2635 nr = bufsize / sizeof(struct lu_fid);
2636 if (nr * sizeof(struct lu_fid) != bufsize)
2638 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2639 RCL_SERVER, nr * sizeof(__u32));
2640 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2641 RCL_SERVER, nr * sizeof(struct lu_fid));
2642 rc = req_capsule_server_pack(tsi->tsi_pill);
2644 GOTO(out, rc = err_serious(rc));
2645 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2648 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2650 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2653 mdt_init_ucred(mti, reqbody);
2654 for (i = 0; i < nr; i++) {
2656 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2658 mdt_exit_ucred(mti);
2664 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2665 void *karg, void __user *uarg);
2667 int mdt_io_set_info(struct tgt_session_info *tsi)
2669 struct ptlrpc_request *req = tgt_ses_req(tsi);
2670 struct ost_body *body = NULL, *repbody;
2671 void *key, *val = NULL;
2672 int keylen, vallen, rc = 0;
2673 bool is_grant_shrink;
2677 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2679 DEBUG_REQ(D_HA, req, "no set_info key");
2680 RETURN(err_serious(-EFAULT));
2682 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2685 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2687 DEBUG_REQ(D_HA, req, "no set_info val");
2688 RETURN(err_serious(-EFAULT));
2690 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2693 is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
2694 if (is_grant_shrink)
2695 /* In this case the value is actually an RMF_OST_BODY, so we
2696 * transmutate the type of this PTLRPC */
2697 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
2699 rc = req_capsule_server_pack(tsi->tsi_pill);
2703 if (is_grant_shrink) {
2704 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
2706 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2709 /** handle grant shrink, similar to a read request */
2710 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
2713 CERROR("%s: Unsupported key %s\n",
2714 tgt_name(tsi->tsi_tgt), (char *)key);
2722 static int mdt_set_info(struct tgt_session_info *tsi)
2724 struct ptlrpc_request *req = tgt_ses_req(tsi);
2727 int keylen, vallen, rc = 0;
2731 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2733 DEBUG_REQ(D_HA, req, "no set_info key");
2734 RETURN(err_serious(-EFAULT));
2737 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2740 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2742 DEBUG_REQ(D_HA, req, "no set_info val");
2743 RETURN(err_serious(-EFAULT));
2746 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2749 /* Swab any part of val you need to here */
2750 if (KEY_IS(KEY_READ_ONLY)) {
2751 spin_lock(&req->rq_export->exp_lock);
2753 *exp_connect_flags_ptr(req->rq_export) |=
2756 *exp_connect_flags_ptr(req->rq_export) &=
2757 ~OBD_CONNECT_RDONLY;
2758 spin_unlock(&req->rq_export->exp_lock);
2759 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2760 struct changelog_setinfo *cs = val;
2762 if (vallen != sizeof(*cs)) {
2763 CERROR("%s: bad changelog_clear setinfo size %d\n",
2764 tgt_name(tsi->tsi_tgt), vallen);
2767 if (req_capsule_req_need_swab(&req->rq_pill)) {
2768 __swab64s(&cs->cs_recno);
2769 __swab32s(&cs->cs_id);
2772 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2774 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2776 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2778 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2785 static int mdt_readpage(struct tgt_session_info *tsi)
2787 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2788 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2789 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2790 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2791 struct mdt_body *repbody;
2797 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2798 RETURN(err_serious(-ENOMEM));
2800 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2801 if (repbody == NULL || reqbody == NULL)
2802 RETURN(err_serious(-EFAULT));
2805 * prepare @rdpg before calling lower layers and transfer itself. Here
2806 * reqbody->size contains offset of where to start to read and
2807 * reqbody->nlink contains number bytes to read.
2809 rdpg->rp_hash = reqbody->mbo_size;
2810 if (rdpg->rp_hash != reqbody->mbo_size) {
2811 CERROR("Invalid hash: %#llx != %#llx\n",
2812 rdpg->rp_hash, reqbody->mbo_size);
2816 rdpg->rp_attrs = reqbody->mbo_mode;
2817 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2818 rdpg->rp_attrs |= LUDA_64BITHASH;
2819 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2820 exp_max_brw_size(tsi->tsi_exp));
2821 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2823 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2824 if (rdpg->rp_pages == NULL)
2827 for (i = 0; i < rdpg->rp_npages; ++i) {
2828 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2829 if (rdpg->rp_pages[i] == NULL)
2830 GOTO(free_rdpg, rc = -ENOMEM);
2833 /* call lower layers to fill allocated pages with directory data */
2834 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2836 GOTO(free_rdpg, rc);
2838 /* send pages to client */
2839 rc = tgt_sendpage(tsi, rdpg, rc);
2844 for (i = 0; i < rdpg->rp_npages; i++)
2845 if (rdpg->rp_pages[i] != NULL)
2846 __free_page(rdpg->rp_pages[i]);
2847 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2849 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2855 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2857 struct lu_ucred *uc = mdt_ucred_check(info);
2858 struct lu_attr *attr = &info->mti_attr.ma_attr;
2863 if (op != REINT_SETATTR) {
2864 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2865 attr->la_uid = uc->uc_fsuid;
2866 /* for S_ISGID, inherit gid from his parent, such work will be
2867 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2868 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2869 attr->la_gid = uc->uc_fsgid;
2875 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2877 return op == REINT_OPEN &&
2878 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2881 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2883 struct req_capsule *pill = info->mti_pill;
2885 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2887 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2889 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2891 /* pre-set size in server part with max size */
2892 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2894 OBD_MAX_DEFAULT_EA_SIZE);
2896 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2901 static int mdt_reint_internal(struct mdt_thread_info *info,
2902 struct mdt_lock_handle *lhc,
2905 struct req_capsule *pill = info->mti_pill;
2906 struct mdt_body *repbody;
2911 rc = mdt_reint_unpack(info, op);
2913 CERROR("Can't unpack reint, rc %d\n", rc);
2914 RETURN(err_serious(rc));
2918 /* check if the file system is set to readonly. O_RDONLY open
2919 * is still allowed even the file system is set to readonly mode */
2920 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2921 RETURN(err_serious(-EROFS));
2923 /* for replay (no_create) lmm is not needed, client has it already */
2924 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2925 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2928 /* llog cookies are always 0, the field is kept for compatibility */
2929 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2930 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2932 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2933 * by default. If the target object has more ACL entries, then
2934 * enlarge the buffer when necessary. */
2935 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2936 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2937 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2939 mdt_preset_secctx_size(info);
2940 mdt_preset_encctx_size(info);
2942 rc = req_capsule_server_pack(pill);
2944 CERROR("Can't pack response, rc %d\n", rc);
2945 RETURN(err_serious(rc));
2948 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2949 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2951 repbody->mbo_eadatasize = 0;
2952 repbody->mbo_aclsize = 0;
2955 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2957 /* for replay no cookkie / lmm need, because client have this already */
2958 if (info->mti_spec.no_create)
2959 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2960 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2962 rc = mdt_init_ucred_reint(info);
2964 GOTO(out_shrink, rc);
2966 rc = mdt_fix_attr_ucred(info, op);
2968 GOTO(out_ucred, rc = err_serious(rc));
2970 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2972 GOTO(out_ucred, rc);
2973 } else if (rc == 1) {
2974 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2975 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2976 GOTO(out_ucred, rc);
2978 rc = mdt_reint_rec(info, lhc);
2981 mdt_exit_ucred(info);
2983 mdt_client_compatibility(info);
2985 rc2 = mdt_fix_reply(info);
2990 * Data-on-MDT optimization - read data along with OPEN and return it
2991 * in reply when possible.
2993 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2994 rc = mdt_dom_read_on_open(info, info->mti_mdt,
3000 static long mdt_reint_opcode(struct ptlrpc_request *req,
3001 const struct req_format **fmt)
3003 struct mdt_device *mdt;
3004 struct mdt_rec_reint *rec;
3007 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
3009 opc = rec->rr_opcode;
3010 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
3011 if (opc < REINT_MAX && fmt[opc] != NULL)
3012 req_capsule_extend(&req->rq_pill, fmt[opc]);
3014 mdt = mdt_exp2dev(req->rq_export);
3015 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
3016 " rc = %d\n", req->rq_export->exp_obd->obd_name,
3017 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
3018 opc = err_serious(-EFAULT);
3021 opc = err_serious(-EFAULT);
3026 static int mdt_reint(struct tgt_session_info *tsi)
3030 static const struct req_format *reint_fmts[REINT_MAX] = {
3031 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
3032 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
3033 [REINT_LINK] = &RQF_MDS_REINT_LINK,
3034 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
3035 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
3036 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
3037 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
3038 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
3039 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
3040 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
3045 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
3047 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3049 * No lock possible here from client to pass it to reint code
3052 rc = mdt_reint_internal(info, NULL, opc);
3053 mdt_thread_info_fini(info);
3058 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
3062 /* this should sync the whole device */
3063 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
3065 struct dt_device *dt = mdt->mdt_bottom;
3069 rc = dt->dd_ops->dt_sync(env, dt);
3073 /* this should sync this object */
3074 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
3075 struct mdt_object *mo)
3081 if (!mdt_object_exists(mo)) {
3082 CWARN("%s: non existing object "DFID": rc = %d\n",
3083 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3088 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3089 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3090 dt_obj_version_t version;
3092 version = dt_version_get(env, mdt_obj2dt(mo));
3093 if (version > tgt->lut_obd->obd_last_committed)
3094 rc = mo_object_sync(env, mdt_object_child(mo));
3096 rc = mo_object_sync(env, mdt_object_child(mo));
3102 static int mdt_sync(struct tgt_session_info *tsi)
3104 struct ptlrpc_request *req = tgt_ses_req(tsi);
3105 struct req_capsule *pill = tsi->tsi_pill;
3106 struct mdt_body *body;
3107 ktime_t kstart = ktime_get();
3112 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3113 RETURN(err_serious(-ENOMEM));
3115 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3116 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3118 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3120 if (unlikely(info->mti_object == NULL))
3123 /* sync an object */
3124 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3127 const struct lu_fid *fid;
3128 struct lu_attr *la = &info->mti_attr.ma_attr;
3130 info->mti_attr.ma_need = MA_INODE;
3131 info->mti_attr.ma_valid = 0;
3132 rc = mdt_attr_get_complex(info, info->mti_object,
3135 body = req_capsule_server_get(pill,
3137 fid = mdt_object_fid(info->mti_object);
3138 mdt_pack_attr2body(info, body, la, fid);
3141 mdt_thread_info_fini(info);
3144 mdt_counter_incr(req, LPROC_MDT_SYNC,
3145 ktime_us_delta(ktime_get(), kstart));
3150 static int mdt_data_sync(struct tgt_session_info *tsi)
3152 struct mdt_thread_info *info;
3153 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3154 struct ost_body *body = tsi->tsi_ost_body;
3155 struct ost_body *repbody;
3156 struct mdt_object *mo = NULL;
3162 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3164 /* if no fid is specified then do nothing,
3165 * device sync is done via MDS_SYNC */
3166 if (fid_is_zero(&tsi->tsi_fid))
3169 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3171 RETURN(PTR_ERR(mo));
3173 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3177 repbody->oa.o_oi = body->oa.o_oi;
3178 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3180 info = tsi2mdt_info(tsi);
3181 ma = &info->mti_attr;
3182 ma->ma_need = MA_INODE;
3184 rc = mdt_attr_get_complex(info, mo, ma);
3186 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3189 mdt_thread_info_fini(info);
3194 mdt_object_put(tsi->tsi_env, mo);
3199 * Handle quota control requests to consult current usage/limit, but also
3200 * to configure quota enforcement
3202 static int mdt_quotactl(struct tgt_session_info *tsi)
3204 struct obd_export *exp = tsi->tsi_exp;
3205 struct req_capsule *pill = tsi->tsi_pill;
3206 struct obd_quotactl *oqctl, *repoqc;
3208 struct mdt_device *mdt = mdt_exp2dev(exp);
3209 struct lu_device *qmt = mdt->mdt_qmt_dev;
3210 struct lu_nodemap *nodemap;
3213 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3215 RETURN(err_serious(-EPROTO));
3217 rc = req_capsule_server_pack(pill);
3219 RETURN(err_serious(rc));
3221 nodemap = nodemap_get_from_exp(exp);
3222 if (IS_ERR(nodemap))
3223 RETURN(PTR_ERR(nodemap));
3225 switch (oqctl->qc_cmd) {
3226 /* master quotactl */
3229 case LUSTRE_Q_SETDEFAULT:
3230 case LUSTRE_Q_SETQUOTAPOOL:
3231 case LUSTRE_Q_SETINFOPOOL:
3232 case LUSTRE_Q_SETDEFAULT_POOL:
3233 case LUSTRE_Q_DELETEQID:
3234 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3236 GOTO(out_nodemap, rc = -EPERM);
3240 case LUSTRE_Q_GETDEFAULT:
3241 case LUSTRE_Q_GETQUOTAPOOL:
3242 case LUSTRE_Q_GETINFOPOOL:
3243 case LUSTRE_Q_GETDEFAULT_POOL:
3245 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3246 /* slave quotactl */
3253 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3254 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3255 GOTO(out_nodemap, rc);
3259 switch (oqctl->qc_type) {
3261 id = nodemap_map_id(nodemap, NODEMAP_UID,
3262 NODEMAP_CLIENT_TO_FS, id);
3265 id = nodemap_map_id(nodemap, NODEMAP_GID,
3266 NODEMAP_CLIENT_TO_FS, id);
3269 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3270 NODEMAP_CLIENT_TO_FS, id);
3273 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3275 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3277 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3279 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3280 barrier_exit(tsi->tsi_tgt->lut_bottom);
3282 if (oqctl->qc_id != id)
3283 swap(oqctl->qc_id, id);
3285 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3286 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3287 RETURN(-EINPROGRESS);
3290 switch (oqctl->qc_cmd) {
3296 case LUSTRE_Q_SETDEFAULT:
3297 case LUSTRE_Q_GETDEFAULT:
3298 case LUSTRE_Q_SETQUOTAPOOL:
3299 case LUSTRE_Q_GETQUOTAPOOL:
3300 case LUSTRE_Q_SETINFOPOOL:
3301 case LUSTRE_Q_GETINFOPOOL:
3302 case LUSTRE_Q_SETDEFAULT_POOL:
3303 case LUSTRE_Q_GETDEFAULT_POOL:
3304 case LUSTRE_Q_DELETEQID:
3305 /* forward quotactl request to QMT */
3306 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3311 /* slave quotactl */
3312 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3317 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3318 GOTO(out_nodemap, rc = -EFAULT);
3321 if (oqctl->qc_id != id)
3322 swap(oqctl->qc_id, id);
3324 QCTL_COPY(repoqc, oqctl);
3328 nodemap_putref(nodemap);
3333 /** clone llog ctxt from child (mdd)
3334 * This allows remote llog (replicator) access.
3335 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3336 * context was originally set up, or we can handle them directly.
3337 * I choose the latter, but that means I need any llog
3338 * contexts set up by child to be accessable by the mdt. So we clone the
3339 * context into our context list here.
3341 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3344 struct md_device *next = mdt->mdt_child;
3345 struct llog_ctxt *ctxt;
3348 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3351 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3352 if (rc || ctxt == NULL) {
3356 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3358 CERROR("Can't set mdt ctxt %d\n", rc);
3363 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3364 struct mdt_device *mdt, int idx)
3366 struct llog_ctxt *ctxt;
3368 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3371 /* Put once for the get we just did, and once for the clone */
3372 llog_ctxt_put(ctxt);
3373 llog_ctxt_put(ctxt);
3378 * sec context handlers
3380 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3382 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3388 * quota request handlers
3390 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3392 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3393 struct lu_device *qmt = mdt->mdt_qmt_dev;
3398 RETURN(err_serious(-EOPNOTSUPP));
3400 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3404 struct mdt_object *mdt_object_new(const struct lu_env *env,
3405 struct mdt_device *d,
3406 const struct lu_fid *f)
3408 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3409 struct lu_object *o;
3410 struct mdt_object *m;
3413 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3414 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3415 if (unlikely(IS_ERR(o)))
3416 m = (struct mdt_object *)o;
3422 struct mdt_object *mdt_object_find(const struct lu_env *env,
3423 struct mdt_device *d,
3424 const struct lu_fid *f)
3426 struct lu_object *o;
3427 struct mdt_object *m;
3430 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3431 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3432 if (unlikely(IS_ERR(o)))
3433 m = (struct mdt_object *)o;
3441 * Asyncronous commit for mdt device.
3443 * Pass asynchonous commit call down the MDS stack.
3445 * \param env environment
3446 * \param mdt the mdt device
3448 static void mdt_device_commit_async(const struct lu_env *env,
3449 struct mdt_device *mdt)
3451 struct dt_device *dt = mdt->mdt_bottom;
3455 rc = dt->dd_ops->dt_commit_async(env, dt);
3456 if (unlikely(rc != 0))
3457 CWARN("%s: async commit start failed: rc = %d\n",
3458 mdt_obd_name(mdt), rc);
3459 atomic_inc(&mdt->mdt_async_commit_count);
3464 * Mark the lock as "synchonous".
3466 * Mark the lock to deffer transaction commit to the unlock time.
3468 * \param lock the lock to mark as "synchonous"
3470 * \see mdt_is_lock_sync
3471 * \see mdt_save_lock
3473 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3475 lock->l_ast_data = (void*)1;
3479 * Check whehter the lock "synchonous" or not.
3481 * \param lock the lock to check
3482 * \retval 1 the lock is "synchonous"
3483 * \retval 0 the lock isn't "synchronous"
3485 * \see mdt_set_lock_sync
3486 * \see mdt_save_lock
3488 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3490 return lock->l_ast_data != NULL;
3494 * Blocking AST for mdt locks.
3496 * Starts transaction commit if in case of COS lock conflict or
3497 * deffers such a commit to the mdt_save_lock.
3499 * \param lock the lock which blocks a request or cancelling lock
3500 * \param desc unused
3501 * \param data unused
3502 * \param flag indicates whether this cancelling or blocking callback
3504 * \see ldlm_blocking_ast_nocheck
3506 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3507 void *data, int flag)
3509 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3510 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3511 struct ldlm_cb_set_arg *arg = data;
3512 bool commit_async = false;
3516 if (flag == LDLM_CB_CANCELING)
3519 lock_res_and_lock(lock);
3520 if (lock->l_blocking_ast != mdt_blocking_ast) {
3521 unlock_res_and_lock(lock);
3525 /* A blocking ast may be sent from ldlm_lock_decref_internal
3526 * when the last reference to a local lock was released and
3527 * during blocking event from ldlm_work_bl_ast_lock().
3528 * The 'data' parameter is l_ast_data in the first case and
3529 * callback arguments in the second one. Distinguish them by that.
3531 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3532 goto skip_cos_checks;
3534 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3535 if (mdt_cos_is_enabled(mdt)) {
3536 if (!arg->bl_desc->bl_same_client)
3537 mdt_set_lock_sync(lock);
3538 } else if (mdt_slc_is_enabled(mdt) &&
3539 arg->bl_desc->bl_cos_incompat) {
3540 mdt_set_lock_sync(lock);
3542 * we may do extra commit here, but there is a small
3543 * window to miss a commit: lock was unlocked (saved),
3544 * then a conflict lock queued and we come here, but
3545 * REP-ACK not received, so lock was not converted to
3547 * Fortunately this window is quite small, so the
3548 * extra commit should be rare (not to say distributed
3549 * operation is rare too).
3551 commit_async = true;
3553 } else if (lock->l_req_mode == LCK_COS) {
3554 commit_async = true;
3558 rc = ldlm_blocking_ast_nocheck(lock);
3563 rc = lu_env_init(&env, LCT_LOCAL);
3564 if (unlikely(rc != 0))
3565 CWARN("%s: lu_env initialization failed, cannot "
3566 "start asynchronous commit: rc = %d\n",
3569 mdt_device_commit_async(&env, mdt);
3576 * Blocking AST for cross-MDT lock
3578 * Discard lock from uncommitted_slc_locks and cancel it.
3580 * \param lock the lock which blocks a request or cancelling lock
3581 * \param desc unused
3582 * \param data unused
3583 * \param flag indicates whether this cancelling or blocking callback
3584 * \retval 0 on success
3585 * \retval negative number on error
3587 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3588 void *data, int flag)
3594 case LDLM_CB_BLOCKING: {
3595 struct lustre_handle lockh;
3597 ldlm_lock2handle(lock, &lockh);
3598 rc = ldlm_cli_cancel(&lockh,
3599 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3601 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3606 case LDLM_CB_CANCELING: {
3607 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3608 struct mdt_device *mdt =
3609 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3611 LDLM_DEBUG(lock, "Revoke remote lock");
3613 /* discard slc lock here so that it can be cleaned anytime,
3614 * especially for cleanup_resource() */
3615 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3617 /* once we cache lock, l_ast_data is set to mdt_object */
3618 if (lock->l_ast_data != NULL) {
3619 struct mdt_object *mo = lock->l_ast_data;
3622 rc = lu_env_init(&env, LCT_MD_THREAD);
3623 if (unlikely(rc != 0)) {
3624 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3626 PFID(mdt_object_fid(mo)), rc);
3630 if (lock->l_policy_data.l_inodebits.bits &
3631 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3632 rc = mo_invalidate(&env, mdt_object_child(mo));
3633 mo->mot_cache_attr = 0;
3635 mdt_object_put(&env, mo);
3647 int mdt_check_resent_lock(struct mdt_thread_info *info,
3648 struct mdt_object *mo,
3649 struct mdt_lock_handle *lhc)
3651 /* the lock might already be gotten in ldlm_handle_enqueue() */
3652 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3653 struct ptlrpc_request *req = mdt_info_req(info);
3654 struct ldlm_lock *lock;
3656 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3657 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3659 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3660 * a resend case, however, it could be already destroyed
3661 * due to client eviction or a raced cancel RPC. */
3662 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3663 lhc->mlh_reg_lh.cookie);
3667 if (!fid_res_name_eq(mdt_object_fid(mo),
3668 &lock->l_resource->lr_name)) {
3669 CWARN("%s: Although resent, but still not "
3670 "get child lock:"DFID"\n",
3671 info->mti_exp->exp_obd->obd_name,
3672 PFID(mdt_object_fid(mo)));
3673 LDLM_LOCK_PUT(lock);
3676 LDLM_LOCK_PUT(lock);
3682 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3684 mdt_object_get(NULL, lock->l_ast_data);
3687 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3688 struct mdt_object *o, const struct lu_fid *fid,
3689 struct lustre_handle *lh, enum ldlm_mode mode,
3690 __u64 *ibits, __u64 trybits, bool cache)
3692 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3693 union ldlm_policy_data *policy = &mti->mti_policy;
3694 struct ldlm_res_id *res_id = &mti->mti_res_id;
3698 LASSERT(mdt_object_remote(o));
3700 fid_build_reg_res_name(fid, res_id);
3702 memset(einfo, 0, sizeof(*einfo));
3703 einfo->ei_type = LDLM_IBITS;
3704 einfo->ei_mode = mode;
3705 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3706 einfo->ei_cb_cp = ldlm_completion_ast;
3707 einfo->ei_enq_slave = 0;
3708 einfo->ei_res_id = res_id;
3709 einfo->ei_req_slot = 1;
3713 * if we cache lock, couple lock with mdt_object, so that object
3714 * can be easily found in lock ASTs.
3716 einfo->ei_cbdata = o;
3717 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3720 memset(policy, 0, sizeof(*policy));
3721 policy->l_inodebits.bits = *ibits;
3722 policy->l_inodebits.try_bits = trybits;
3724 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3727 /* Return successfully acquired bits to a caller */
3729 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3732 *ibits = lock->l_policy_data.l_inodebits.bits;
3733 LDLM_LOCK_PUT(lock);
3738 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3739 const struct lu_fid *fid, struct lustre_handle *lh,
3740 enum ldlm_mode mode, __u64 ibits, bool cache)
3742 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3746 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3747 struct mdt_lock_handle *lh, __u64 *ibits,
3748 __u64 trybits, bool cos_incompat)
3750 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3751 union ldlm_policy_data *policy = &info->mti_policy;
3752 struct ldlm_res_id *res_id = &info->mti_res_id;
3753 __u64 dlmflags = 0, *cookie = NULL;
3757 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3758 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3759 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3760 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3763 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3764 lh->mlh_reg_mode == LCK_EX);
3765 dlmflags |= LDLM_FL_COS_INCOMPAT;
3766 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3767 dlmflags |= LDLM_FL_COS_ENABLED;
3770 /* Only enqueue LOOKUP lock for remote object */
3771 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3773 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3774 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3775 *ibits == MDS_INODELOCK_OPEN)
3776 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3778 if (lh->mlh_type == MDT_PDO_LOCK) {
3779 /* check for exists after object is locked */
3780 if (mdt_object_exists(o) == 0) {
3781 /* Non-existent object shouldn't have PDO lock */
3784 /* Non-dir object shouldn't have PDO lock */
3785 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3790 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3791 dlmflags |= LDLM_FL_ATOMIC_CB;
3794 cookie = &info->mti_exp->exp_handle.h_cookie;
3797 * Take PDO lock on whole directory and build correct @res_id for lock
3798 * on part of directory.
3800 if (lh->mlh_pdo_hash != 0) {
3801 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3802 mdt_lock_pdo_mode(info, o, lh);
3803 if (lh->mlh_pdo_mode != LCK_NL) {
3805 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3806 * is never going to be sent to client and we do not
3807 * want it slowed down due to possible cancels.
3809 policy->l_inodebits.bits =
3810 *ibits & MDS_INODELOCK_UPDATE;
3811 policy->l_inodebits.try_bits =
3812 trybits & MDS_INODELOCK_UPDATE;
3813 /* at least one of them should be set */
3814 LASSERT(policy->l_inodebits.bits |
3815 policy->l_inodebits.try_bits);
3816 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3817 lh->mlh_pdo_mode, policy, res_id,
3819 if (unlikely(rc != 0))
3820 GOTO(out_unlock, rc);
3824 * Finish res_id initializing by name hash marking part of
3825 * directory which is taking modification.
3827 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3830 policy->l_inodebits.bits = *ibits;
3831 policy->l_inodebits.try_bits = trybits;
3832 policy->l_inodebits.li_gid = lh->mlh_gid;
3835 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3836 * going to be sent to client. If it is - mdt_intent_policy() path will
3837 * fix it up and turn FL_LOCAL flag off.
3839 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3840 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3844 mdt_object_unlock(info, o, lh, 1);
3845 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3846 lh->mlh_pdo_hash != 0 &&
3847 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3848 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3850 /* Return successfully acquired bits to a caller */
3852 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3855 *ibits = lock->l_policy_data.l_inodebits.bits;
3856 LDLM_LOCK_PUT(lock);
3862 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3863 struct mdt_lock_handle *lh, __u64 *ibits,
3864 __u64 trybits, bool cos_incompat)
3866 struct mdt_lock_handle *local_lh = NULL;
3870 if (!mdt_object_remote(o)) {
3871 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3876 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3877 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3878 MDS_INODELOCK_XATTR);
3880 /* Only enqueue LOOKUP lock for remote object */
3881 if (*ibits & MDS_INODELOCK_LOOKUP) {
3882 __u64 local = MDS_INODELOCK_LOOKUP;
3884 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3892 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3893 /* Sigh, PDO needs to enqueue 2 locks right now, but
3894 * enqueue RPC can only request 1 lock, to avoid extra
3895 * RPC, so it will instead enqueue EX lock for remote
3896 * object anyway XXX*/
3897 if (lh->mlh_type == MDT_PDO_LOCK &&
3898 lh->mlh_pdo_hash != 0) {
3900 "%s: "DFID" convert PDO lock to EX lock.\n",
3901 mdt_obd_name(info->mti_mdt),
3902 PFID(mdt_object_fid(o)));
3903 lh->mlh_pdo_hash = 0;
3904 lh->mlh_rreg_mode = LCK_EX;
3905 lh->mlh_type = MDT_REG_LOCK;
3908 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3911 ibits, trybits, false);
3912 if (rc != ELDLM_OK) {
3913 if (local_lh != NULL)
3914 mdt_object_unlock(info, o, local_lh, rc);
3919 /* other components like LFSCK can use lockless access
3920 * and populate cache, so we better invalidate it */
3921 mo_invalidate(info->mti_env, mdt_object_child(o));
3926 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3927 struct mdt_lock_handle *lh, __u64 ibits)
3929 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3932 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3933 struct mdt_lock_handle *lh, __u64 ibits,
3936 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3937 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3941 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3942 struct mdt_lock_handle *lh, __u64 *ibits,
3943 __u64 trybits, bool cos_incompat)
3945 bool trylock_only = *ibits == 0;
3948 LASSERT(!(*ibits & trybits));
3949 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3951 if (rc && trylock_only) { /* clear error for try ibits lock only */
3952 LASSERT(*ibits == 0);
3959 * Save a lock within request object.
3961 * Keep the lock referenced until whether client ACK or transaction
3962 * commit happens or release the lock immediately depending on input
3963 * parameters. If COS is ON, a write lock is converted to COS lock
3966 * \param info thead info object
3967 * \param h lock handle
3968 * \param mode lock mode
3969 * \param decref force immediate lock releasing
3971 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3972 enum ldlm_mode mode, int decref)
3974 struct tgt_session_info *tsi = info->mti_env->le_ses ?
3975 tgt_ses_info(info->mti_env) : NULL;
3979 if (lustre_handle_is_used(h)) {
3980 bool has_trans = tsi && tsi->tsi_has_trans;
3982 if (decref || !has_trans || !(mode & (LCK_PW | LCK_EX))) {
3983 mdt_fid_unlock(h, mode);
3985 struct mdt_device *mdt = info->mti_mdt;
3986 struct ldlm_lock *lock = ldlm_handle2lock(h);
3987 struct ptlrpc_request *req = mdt_info_req(info);
3988 bool cos = mdt_cos_is_enabled(mdt);
3989 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3991 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3994 /* there is no request if mdt_object_unlock() is called
3995 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3996 if (likely(req != NULL)) {
3997 LDLM_DEBUG(lock, "save lock request %p reply "
3998 "state %p transno %lld\n", req,
3999 req->rq_reply_state, req->rq_transno);
4001 ldlm_lock_mode_downgrade(lock, LCK_COS);
4004 if (req->rq_export->exp_disconnected)
4005 mdt_fid_unlock(h, mode);
4007 ptlrpc_save_lock(req, h, mode, cos,
4010 mdt_fid_unlock(h, mode);
4013 if (mdt_is_lock_sync(lock)) {
4014 CDEBUG(D_HA, "sync_lock, do async commit\n");
4015 mdt_device_commit_async(info->mti_env, mdt);
4017 LDLM_LOCK_PUT(lock);
4026 * Save cross-MDT lock in uncommitted_slc_locks
4028 * Keep the lock referenced until transaction commit happens or release the lock
4029 * immediately depending on input parameters.
4031 * \param info thead info object
4032 * \param h lock handle
4033 * \param mode lock mode
4034 * \param decref force immediate lock releasing
4036 static void mdt_save_remote_lock(struct mdt_thread_info *info,
4037 struct mdt_object *o, struct lustre_handle *h,
4038 enum ldlm_mode mode, int decref)
4042 if (lustre_handle_is_used(h)) {
4043 struct ldlm_lock *lock = ldlm_handle2lock(h);
4044 struct ptlrpc_request *req = mdt_info_req(info);
4047 (lock->l_policy_data.l_inodebits.bits &
4048 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
4049 mo_invalidate(info->mti_env, mdt_object_child(o));
4051 if (decref || !req || !(mode & (LCK_PW | LCK_EX)) ||
4052 !tgt_ses_info(info->mti_env)->tsi_has_trans) {
4053 ldlm_lock_decref_and_cancel(h, mode);
4054 LDLM_LOCK_PUT(lock);
4056 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
4058 ldlm_lock_decref(h, mode);
4067 * Unlock mdt object.
4069 * Immeditely release the regular lock and the PDO lock or save the
4070 * lock in request and keep them referenced until client ACK or
4071 * transaction commit.
4073 * \param info thread info object
4074 * \param o mdt object
4075 * \param lh mdt lock handle referencing regular and PDO locks
4076 * \param decref force immediate lock releasing
4078 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
4080 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
4081 struct mdt_lock_handle *lh, int decref)
4085 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
4086 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
4087 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4093 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4094 const struct lu_fid *f,
4095 struct mdt_lock_handle *lh,
4098 struct mdt_object *o;
4100 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4104 rc = mdt_object_lock(info, o, lh, ibits);
4106 mdt_object_put(info->mti_env, o);
4113 void mdt_object_unlock_put(struct mdt_thread_info * info,
4114 struct mdt_object * o,
4115 struct mdt_lock_handle *lh,
4118 mdt_object_unlock(info, o, lh, decref);
4119 mdt_object_put(info->mti_env, o);
4123 * Generic code handling requests that have struct mdt_body passed in:
4125 * - extract mdt_body from request and save it in @info, if present;
4127 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4130 * - if HAS_BODY flag is set for this request type check whether object
4131 * actually exists on storage (lu_object_exists()).
4134 static int mdt_body_unpack(struct mdt_thread_info *info,
4135 enum tgt_handler_flags flags)
4137 const struct mdt_body *body;
4138 struct mdt_object *obj;
4139 const struct lu_env *env;
4140 struct req_capsule *pill;
4144 env = info->mti_env;
4145 pill = info->mti_pill;
4147 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4151 if (!(body->mbo_valid & OBD_MD_FLID))
4154 if (!fid_is_sane(&body->mbo_fid1)) {
4155 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4159 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4161 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4162 mdt_object_put(env, obj);
4165 info->mti_object = obj;
4174 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4175 enum tgt_handler_flags flags)
4177 struct req_capsule *pill = info->mti_pill;
4182 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4183 rc = mdt_body_unpack(info, flags);
4187 if (rc == 0 && (flags & HAS_REPLY)) {
4189 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4190 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4192 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4193 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4196 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4197 * by default. If the target object has more ACL entries, then
4198 * enlarge the buffer when necessary. */
4199 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4200 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4201 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4203 mdt_preset_secctx_size(info);
4204 mdt_preset_encctx_size(info);
4206 rc = req_capsule_server_pack(pill);
4208 CWARN("%s: cannot pack response: rc = %d\n",
4209 mdt_obd_name(info->mti_mdt), rc);
4214 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
4216 lh->mlh_type = MDT_NUL_LOCK;
4217 lh->mlh_reg_lh.cookie = 0ull;
4218 lh->mlh_reg_mode = LCK_MINMODE;
4219 lh->mlh_pdo_lh.cookie = 0ull;
4220 lh->mlh_pdo_mode = LCK_MINMODE;
4221 lh->mlh_rreg_lh.cookie = 0ull;
4222 lh->mlh_rreg_mode = LCK_MINMODE;
4225 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
4227 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
4228 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
4232 * Initialize fields of struct mdt_thread_info. Other fields are left in
4233 * uninitialized state, because it's too expensive to zero out whole
4234 * mdt_thread_info (> 1K) on each request arrival.
4236 void mdt_thread_info_init(struct ptlrpc_request *req,
4237 struct mdt_thread_info *info)
4241 info->mti_pill = &req->rq_pill;
4244 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4245 mdt_lock_handle_init(&info->mti_lh[i]);
4247 /* mdt device: it can be NULL while CONNECT */
4248 if (req->rq_export) {
4249 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4250 info->mti_exp = req->rq_export;
4252 info->mti_mdt = NULL;
4253 info->mti_env = req->rq_svc_thread->t_env;
4254 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4256 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4257 info->mti_big_buf = LU_BUF_NULL;
4258 info->mti_body = NULL;
4259 info->mti_object = NULL;
4260 info->mti_dlm_req = NULL;
4261 info->mti_cross_ref = 0;
4262 info->mti_opdata = 0;
4263 info->mti_big_lmm_used = 0;
4264 info->mti_big_acl_used = 0;
4265 info->mti_som_strict = 0;
4267 info->mti_spec.no_create = 0;
4268 info->mti_spec.sp_rm_entry = 0;
4269 info->mti_spec.sp_permitted = 0;
4271 info->mti_spec.u.sp_ea.eadata = NULL;
4272 info->mti_spec.u.sp_ea.eadatalen = 0;
4275 void mdt_thread_info_fini(struct mdt_thread_info *info)
4279 if (info->mti_object != NULL) {
4280 mdt_object_put(info->mti_env, info->mti_object);
4281 info->mti_object = NULL;
4284 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4285 mdt_lock_handle_fini(&info->mti_lh[i]);
4286 info->mti_env = NULL;
4287 info->mti_pill = NULL;
4288 info->mti_exp = NULL;
4289 info->mti_mdt = NULL;
4291 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4292 lu_buf_free(&info->mti_big_buf);
4295 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4297 struct mdt_thread_info *mti;
4299 mti = mdt_th_info(tsi->tsi_env);
4300 LASSERT(mti != NULL);
4302 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4303 if (tsi->tsi_corpus != NULL) {
4304 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4305 lu_object_get(tsi->tsi_corpus);
4307 mti->mti_body = tsi->tsi_mdt_body;
4308 mti->mti_dlm_req = tsi->tsi_dlm_req;
4313 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4315 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4317 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4318 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4320 return tgt_connect(tsi);
4323 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4324 struct mdt_thread_info *info,
4325 struct ldlm_lock **lockp, __u64 flags)
4327 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4330 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4331 struct mdt_thread_info *info,
4332 struct ldlm_lock **lockp, __u64 flags)
4334 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4338 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4339 struct ldlm_lock **lockp,
4340 struct mdt_lock_handle *lh,
4341 __u64 flags, int result)
4343 struct ptlrpc_request *req = mdt_info_req(info);
4344 struct ldlm_lock *lock = *lockp;
4345 struct ldlm_lock *new_lock;
4347 /* If possible resent found a lock, @lh is set to its handle */
4348 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4350 if (new_lock == NULL) {
4351 if (flags & LDLM_FL_INTENT_ONLY) {
4353 } else if (flags & LDLM_FL_RESENT) {
4354 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4355 * resend case, however, it could be already destroyed
4356 * due to client eviction or a raced cancel RPC.
4358 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4359 lh->mlh_reg_lh.cookie);
4362 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4363 mdt_obd_name(info->mti_mdt),
4364 lh->mlh_reg_lh.cookie, flags,
4365 PFID(&info->mti_tmp_fid1),
4366 PFID(&info->mti_tmp_fid2), result);
4369 lh->mlh_reg_lh.cookie = 0;
4374 * If we've already given this lock to a client once, then we should
4375 * have no readers or writers. Otherwise, we should have one reader
4376 * _or_ writer ref (which will be zeroed below) before returning the
4379 if (new_lock->l_export == req->rq_export) {
4380 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4382 LASSERT(new_lock->l_export == NULL);
4383 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4388 if (new_lock->l_export == req->rq_export) {
4390 * Already gave this to the client, which means that we
4391 * reconstructed a reply.
4393 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4396 LDLM_LOCK_RELEASE(new_lock);
4397 lh->mlh_reg_lh.cookie = 0;
4398 RETURN(ELDLM_LOCK_REPLACED);
4402 * Fixup the lock to be given to the client.
4404 lock_res_and_lock(new_lock);
4405 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4406 * possible blocking AST. */
4407 while (new_lock->l_readers > 0) {
4408 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4409 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4410 new_lock->l_readers--;
4412 while (new_lock->l_writers > 0) {
4413 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4414 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4415 new_lock->l_writers--;
4418 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4419 new_lock->l_blocking_ast = lock->l_blocking_ast;
4420 new_lock->l_completion_ast = lock->l_completion_ast;
4421 if (ldlm_has_dom(new_lock))
4422 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4423 new_lock->l_remote_handle = lock->l_remote_handle;
4424 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4426 unlock_res_and_lock(new_lock);
4428 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4429 &new_lock->l_remote_handle,
4430 &new_lock->l_exp_hash);
4432 LDLM_LOCK_RELEASE(new_lock);
4433 lh->mlh_reg_lh.cookie = 0;
4435 RETURN(ELDLM_LOCK_REPLACED);
4438 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4439 struct ldlm_lock *new_lock,
4440 struct mdt_lock_handle *lh, __u64 flags)
4442 struct ptlrpc_request *req = mdt_info_req(info);
4443 struct ldlm_request *dlmreq;
4445 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4448 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4450 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4451 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4453 if (flags & LDLM_FL_RESENT) {
4454 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4455 lh->mlh_reg_mode = new_lock->l_granted_mode;
4457 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4458 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4459 lh->mlh_reg_lh.cookie);
4464 * If the xid matches, then we know this is a resent request, and allow
4465 * it. (It's probably an OPEN, for which we don't send a lock.
4467 if (req_can_reconstruct(req, NULL) != 0)
4471 * This remote handle isn't enqueued, so we never received or processed
4472 * this request. Clear MSG_RESENT, because it can be handled like any
4473 * normal request now.
4475 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4477 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4478 dlmreq->lock_handle[0].cookie);
4481 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4482 struct mdt_thread_info *info,
4483 struct ldlm_lock **lockp,
4486 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4487 struct ldlm_reply *ldlm_rep = NULL;
4492 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4493 * (for the resend case) or a new lock. Below we will use it to
4494 * replace the original lock.
4496 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4497 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4498 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4499 rc = mdt_object_lock(info, info->mti_object, lhc,
4500 MDS_INODELOCK_XATTR);
4505 rc = mdt_getxattr(info);
4507 if (mdt_info_req(info)->rq_repmsg != NULL)
4508 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4510 if (ldlm_rep == NULL ||
4511 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4512 mdt_object_unlock(info, info->mti_object, lhc, 1);
4516 RETURN(err_serious(-EFAULT));
4519 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4521 /* This is left for interop instead of adding a new interop flag.
4523 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4524 if (ldlm_rep->lock_policy_res2) {
4525 mdt_object_unlock(info, info->mti_object, lhc, 1);
4526 RETURN(ELDLM_LOCK_ABORTED);
4530 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4534 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4535 struct mdt_thread_info *info,
4536 struct ldlm_lock **lockp,
4539 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4541 struct ldlm_reply *ldlm_rep;
4542 struct mdt_body *reqbody;
4543 struct mdt_body *repbody;
4547 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4550 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4553 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4554 repbody->mbo_eadatasize = 0;
4555 repbody->mbo_aclsize = 0;
4559 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4562 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4566 CERROR("%s: unsupported intent %#x\n",
4567 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4568 GOTO(out_shrink, rc = -EINVAL);
4571 rc = mdt_init_ucred(info, reqbody);
4573 GOTO(out_shrink, rc);
4575 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4576 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4578 /* Get lock from request for possible resent case. */
4579 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4581 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4582 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4584 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG) &&
4585 ldlm_rep->lock_policy_res2 != -ENOKEY)
4586 ldlm_rep->lock_policy_res2 = 0;
4587 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4588 ldlm_rep->lock_policy_res2) {
4589 lhc->mlh_reg_lh.cookie = 0ull;
4590 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4593 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4596 mdt_exit_ucred(info);
4598 mdt_client_compatibility(info);
4599 rc2 = mdt_fix_reply(info);
4605 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4606 struct mdt_thread_info *info,
4607 struct ldlm_lock **lockp,
4610 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4611 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4612 struct layout_intent *intent;
4613 struct ldlm_reply *ldlm_rep;
4614 struct lu_fid *fid = &info->mti_tmp_fid2;
4615 struct mdt_object *obj = NULL;
4616 int layout_size = 0;
4617 struct lu_buf *buf = &layout.mlc_buf;
4622 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4624 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4628 CDEBUG(D_INFO, DFID "got layout change request from client: "
4629 "opc:%u flags:%#x extent "DEXT"\n",
4630 PFID(fid), intent->li_opc, intent->li_flags,
4631 PEXT(&intent->li_extent));
4633 switch (intent->li_opc) {
4634 case LAYOUT_INTENT_TRUNC:
4635 case LAYOUT_INTENT_WRITE:
4636 layout.mlc_opc = MD_LAYOUT_WRITE;
4637 layout.mlc_intent = intent;
4639 case LAYOUT_INTENT_ACCESS:
4641 case LAYOUT_INTENT_READ:
4642 case LAYOUT_INTENT_GLIMPSE:
4643 case LAYOUT_INTENT_RELEASE:
4644 case LAYOUT_INTENT_RESTORE:
4645 CERROR("%s: Unsupported layout intent opc %d\n",
4646 mdt_obd_name(info->mti_mdt), intent->li_opc);
4649 CERROR("%s: Unknown layout intent opc %d\n",
4650 mdt_obd_name(info->mti_mdt), intent->li_opc);
4654 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4656 RETURN(PTR_ERR(obj));
4658 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4659 /* if layout is going to be changed don't use the current EA
4660 * size but the maximum one. That buffer will be shrinked
4661 * to the actual size in req_capsule_shrink() before reply.
4663 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4664 layout_size = info->mti_mdt->mdt_max_mdsize;
4666 layout_size = mdt_attr_get_eabuf_size(info, obj);
4667 if (layout_size < 0)
4668 GOTO(out, rc = layout_size);
4670 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4671 info->mti_mdt->mdt_max_mdsize = layout_size;
4673 CDEBUG(D_INFO, "%s: layout_size %d\n",
4674 mdt_obd_name(info->mti_mdt), layout_size);
4678 * set reply buffer size, so that ldlm_handle_enqueue0()->
4679 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4681 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4683 rc = req_capsule_server_pack(info->mti_pill);
4687 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4689 GOTO(out, rc = -EPROTO);
4691 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4693 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4694 if (layout.mlc_opc == MD_LAYOUT_NOP)
4697 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4701 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4702 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4708 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4709 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4711 buf->lb_len = req_capsule_get_size(info->mti_pill,
4712 &RMF_EADATA, RCL_CLIENT);
4714 * If it's a replay of layout write intent RPC, the client has
4715 * saved the extended lovea when it get reply then.
4717 if (buf->lb_len > 0)
4718 mdt_fix_lov_magic(info, buf->lb_buf);
4721 /* Get lock from request for possible resent case. */
4722 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4723 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4726 * Instantiate some layout components, if @buf contains lovea, then it's
4727 * a replay of the layout intent write RPC.
4729 rc = mdt_layout_change(info, obj, lhc, &layout);
4730 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4732 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4733 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4734 if (rc == ELDLM_LOCK_REPLACED &&
4735 (*lockp)->l_granted_mode == LCK_EX)
4736 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4741 mdt_object_put(info->mti_env, obj);
4745 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4746 struct mdt_thread_info *info,
4747 struct ldlm_lock **lockp,
4750 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4751 struct ldlm_reply *rep = NULL;
4754 struct ptlrpc_request *req = mdt_info_req(info);
4756 static const struct req_format *intent_fmts[REINT_MAX] = {
4757 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4758 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4763 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4767 /* Get lock from request for possible resent case. */
4768 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4770 rc = mdt_reint_internal(info, lhc, opc);
4772 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4773 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4775 /* Check whether the reply has been packed successfully. */
4776 if (mdt_info_req(info)->rq_repmsg != NULL)
4777 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4782 RETURN(err_serious(-EFAULT));
4785 /* MDC expects this in any case */
4787 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4789 /* the open lock or the lock for cross-ref object should be
4790 * returned to the client */
4791 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4792 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4793 rep->lock_policy_res2 = 0;
4794 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4798 rep->lock_policy_res2 = clear_serious(rc);
4800 if (rep->lock_policy_res2 == -ENOENT &&
4801 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4802 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4803 rep->lock_policy_res2 = 0;
4805 lhc->mlh_reg_lh.cookie = 0ull;
4806 if (rc == -ENOTCONN || rc == -ENODEV ||
4807 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4809 * If it is the disconnect error (ENODEV & ENOCONN), the error
4810 * will be returned by rq_status, and client at ptlrpc layer
4811 * will detect this, then disconnect, reconnect the import
4812 * immediately, instead of impacting the following the rpc.
4817 * For other cases, the error will be returned by intent, and client
4818 * will retrieve the result from intent.
4820 RETURN(ELDLM_LOCK_ABORTED);
4823 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4824 struct mdt_thread_info *info,
4825 struct ldlm_lock **lockp,
4826 u64 flags /* LDLM_FL_* */)
4828 struct req_capsule *pill = info->mti_pill;
4829 struct ptlrpc_request *req = mdt_info_req(info);
4830 const struct req_format *it_format;
4831 int (*it_handler)(enum ldlm_intent_flags,
4832 struct mdt_thread_info *,
4833 struct ldlm_lock **,
4835 enum tgt_handler_flags it_handler_flags = 0;
4836 struct ldlm_reply *rep;
4837 bool check_mdt_object = false;
4843 case IT_OPEN|IT_CREAT:
4845 * OCREAT is not a IS_MUTABLE request since the file may
4846 * already exist. We do the extra check of
4847 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4848 * really need to create the object.
4850 it_format = &RQF_LDLM_INTENT;
4851 it_handler = &mdt_intent_open;
4854 check_mdt_object = true;
4857 it_format = &RQF_LDLM_INTENT_GETATTR;
4858 it_handler = &mdt_intent_getattr;
4859 it_handler_flags = HAS_REPLY;
4862 check_mdt_object = true;
4863 it_format = &RQF_LDLM_INTENT_GETXATTR;
4864 it_handler = &mdt_intent_getxattr;
4865 it_handler_flags = HAS_BODY;
4868 it_format = &RQF_LDLM_INTENT_LAYOUT;
4869 it_handler = &mdt_intent_layout;
4872 it_format = &RQF_LDLM_INTENT;
4873 it_handler = &mdt_intent_glimpse;
4876 it_format = &RQF_LDLM_INTENT;
4877 it_handler = &mdt_intent_brw;
4879 case IT_QUOTA_DQACQ:
4880 case IT_QUOTA_CONN: {
4881 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4884 RETURN(-EOPNOTSUPP);
4886 if (mdt_rdonly(req->rq_export))
4889 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4890 /* pass the request to quota master */
4891 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4892 mdt_info_req(info), lockp,
4897 CERROR("%s: unknown intent code %#x\n",
4898 mdt_obd_name(info->mti_mdt), it_opc);
4902 req_capsule_extend(pill, it_format);
4904 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4908 if (unlikely(info->mti_object == NULL && check_mdt_object))
4911 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4914 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4916 /* execute policy */
4917 rc = (*it_handler)(it_opc, info, lockp, flags);
4919 /* Check whether the reply has been packed successfully. */
4920 if (req->rq_repmsg != NULL) {
4921 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4922 rep->lock_policy_res2 =
4923 ptlrpc_status_hton(rep->lock_policy_res2);
4929 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4930 enum ldlm_intent_flags it_opc)
4932 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4934 /* update stats when IT code is known */
4935 if (srv_stats != NULL)
4936 lprocfs_counter_incr(srv_stats,
4937 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4938 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4941 static int mdt_intent_policy(const struct lu_env *env,
4942 struct ldlm_namespace *ns,
4943 struct ldlm_lock **lockp,
4945 enum ldlm_mode mode,
4946 __u64 flags, void *data)
4948 struct tgt_session_info *tsi;
4949 struct mdt_thread_info *info;
4950 struct ptlrpc_request *req = req_cookie;
4951 struct ldlm_intent *it;
4952 struct req_capsule *pill;
4953 const struct ldlm_lock_desc *ldesc;
4958 LASSERT(req != NULL);
4960 tsi = tgt_ses_info(env);
4962 info = tsi2mdt_info(tsi);
4963 LASSERT(info != NULL);
4964 pill = info->mti_pill;
4965 LASSERT(pill->rc_req == req);
4966 ldesc = &info->mti_dlm_req->lock_desc;
4968 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4969 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4970 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4972 mdt_ptlrpc_stats_update(req, it->opc);
4973 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4977 /* Lock without inodebits makes no sense and will oops
4978 * later in ldlm. Let's check it now to see if we have
4979 * ibits corrupted somewhere in mdt_intent_opc().
4980 * The case for client miss to set ibits has been
4981 * processed by others. */
4982 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4983 ldesc->l_policy_data.l_inodebits.bits != 0));
4985 rc = err_serious(-EFAULT);
4987 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4988 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4989 struct ldlm_reply *rep;
4991 /* No intent was provided but INTENT flag is set along with
4992 * DOM bit, this is considered as GLIMPSE request.
4993 * This logic is common for MDT and OST glimpse
4995 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4996 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4997 /* Check whether the reply has been packed successfully. */
4998 if (req->rq_repmsg != NULL) {
4999 rep = req_capsule_server_get(info->mti_pill,
5001 rep->lock_policy_res2 =
5002 ptlrpc_status_hton(rep->lock_policy_res2);
5005 /* No intent was provided */
5006 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
5007 rc = req_capsule_server_pack(pill);
5009 rc = err_serious(rc);
5011 mdt_thread_info_fini(info);
5015 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
5017 struct seq_server_site *ss = mdt_seq_site(mdt);
5019 if (ss->ss_node_id == 0)
5022 if (ss->ss_client_seq != NULL) {
5023 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5024 ss->ss_client_seq->lcs_exp = NULL;
5027 if (ss->ss_server_fld != NULL) {
5028 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
5029 ss->ss_server_fld->lsf_control_exp = NULL;
5033 static void mdt_seq_fini_cli(struct mdt_device *mdt)
5035 struct seq_server_site *ss = mdt_seq_site(mdt);
5040 if (ss->ss_server_seq != NULL)
5041 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
5044 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
5046 mdt_seq_fini_cli(mdt);
5047 mdt_deregister_seq_exp(mdt);
5049 return seq_site_fini(env, mdt_seq_site(mdt));
5053 * It will retrieve its FLDB entries from MDT0, and it only happens
5054 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
5055 * and it needs to refresh FLDB from the MDT0.
5057 static int mdt_register_lwp_callback(void *data)
5060 struct mdt_device *mdt = data;
5061 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
5065 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
5067 rc = lu_env_init(&env, LCT_MD_THREAD);
5069 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
5073 /* Allocate new sequence now to avoid creating local transaction
5074 * in the normal transaction process */
5075 rc = seq_server_check_and_alloc_super(&env,
5076 mdt_seq_site(mdt)->ss_server_seq);
5081 rc = fld_update_from_controller(&env, fld);
5083 CERROR("%s: cannot update controller: rc = %d\n",
5084 mdt_obd_name(mdt), rc);
5093 static int mdt_register_seq_exp(struct mdt_device *mdt)
5095 struct seq_server_site *ss = mdt_seq_site(mdt);
5096 char *lwp_name = NULL;
5099 if (ss->ss_node_id == 0)
5102 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5103 if (lwp_name == NULL)
5104 GOTO(out_free, rc = -ENOMEM);
5106 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5110 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5115 rc = lustre_register_lwp_item(lwp_name,
5116 &ss->ss_server_fld->lsf_control_exp,
5117 mdt_register_lwp_callback, mdt);
5119 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5120 ss->ss_client_seq->lcs_exp = NULL;
5124 if (lwp_name != NULL)
5125 OBD_FREE(lwp_name, MAX_OBD_NAME);
5131 * Init client sequence manager which is used by local MDS to talk to sequence
5132 * controller on remote node.
5134 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5136 struct seq_server_site *ss = mdt_seq_site(mdt);
5140 /* check if this is adding the first MDC and controller is not yet
5142 OBD_ALLOC_PTR(ss->ss_client_seq);
5143 if (ss->ss_client_seq == NULL)
5146 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5147 if (prefix == NULL) {
5148 OBD_FREE_PTR(ss->ss_client_seq);
5149 ss->ss_client_seq = NULL;
5153 /* Note: seq_client_fini will be called in seq_site_fini */
5154 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5155 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5156 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5158 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5160 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5163 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5165 struct seq_server_site *ss;
5169 ss = mdt_seq_site(mdt);
5170 /* init sequence controller server(MDT0) */
5171 if (ss->ss_node_id == 0) {
5172 OBD_ALLOC_PTR(ss->ss_control_seq);
5173 if (ss->ss_control_seq == NULL)
5176 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5177 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5180 GOTO(out_seq_fini, rc);
5183 /* Init normal sequence server */
5184 OBD_ALLOC_PTR(ss->ss_server_seq);
5185 if (ss->ss_server_seq == NULL)
5186 GOTO(out_seq_fini, rc = -ENOMEM);
5188 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5189 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5191 GOTO(out_seq_fini, rc);
5193 /* init seq client for seq server to talk to seq controller(MDT0) */
5194 rc = mdt_seq_init_cli(env, mdt);
5196 GOTO(out_seq_fini, rc);
5198 if (ss->ss_node_id != 0)
5199 /* register controller export through lwp */
5200 rc = mdt_register_seq_exp(mdt);
5205 mdt_seq_fini(env, mdt);
5213 static int mdt_fld_fini(const struct lu_env *env,
5214 struct mdt_device *m)
5216 struct seq_server_site *ss = mdt_seq_site(m);
5219 if (ss && ss->ss_server_fld) {
5220 fld_server_fini(env, ss->ss_server_fld);
5221 OBD_FREE_PTR(ss->ss_server_fld);
5222 ss->ss_server_fld = NULL;
5228 static int mdt_fld_init(const struct lu_env *env,
5230 struct mdt_device *m)
5232 struct seq_server_site *ss;
5236 ss = mdt_seq_site(m);
5238 OBD_ALLOC_PTR(ss->ss_server_fld);
5239 if (ss->ss_server_fld == NULL)
5240 RETURN(rc = -ENOMEM);
5242 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5245 OBD_FREE_PTR(ss->ss_server_fld);
5246 ss->ss_server_fld = NULL;
5253 static void mdt_stack_pre_fini(const struct lu_env *env,
5254 struct mdt_device *m, struct lu_device *top)
5256 struct lustre_cfg_bufs *bufs;
5257 struct lustre_cfg *lcfg;
5258 struct mdt_thread_info *info;
5263 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5264 LASSERT(info != NULL);
5266 bufs = &info->mti_u.bufs;
5268 LASSERT(m->mdt_child_exp);
5269 LASSERT(m->mdt_child_exp->exp_obd);
5271 /* process cleanup, pass mdt obd name to get obd umount flags */
5272 /* XXX: this is needed because all layers are referenced by
5273 * objects (some of them are pinned by osd, for example *
5274 * the proper solution should be a model where object used
5275 * by osd only doesn't have mdt/mdd slices -bzzz */
5276 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5277 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5278 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5281 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5283 top->ld_ops->ldo_process_config(env, top, lcfg);
5284 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5288 static void mdt_stack_fini(const struct lu_env *env,
5289 struct mdt_device *m, struct lu_device *top)
5291 struct obd_device *obd = mdt2obd_dev(m);
5292 struct lustre_cfg_bufs *bufs;
5293 struct lustre_cfg *lcfg;
5294 struct mdt_thread_info *info;
5298 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5299 LASSERT(info != NULL);
5301 lu_dev_del_linkage(top->ld_site, top);
5303 lu_site_purge(env, top->ld_site, -1);
5305 bufs = &info->mti_u.bufs;
5306 /* process cleanup, pass mdt obd name to get obd umount flags */
5307 /* another purpose is to let all layers to release their objects */
5308 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5313 lustre_cfg_bufs_set_string(bufs, 1, flags);
5314 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5317 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5320 top->ld_ops->ldo_process_config(env, top, lcfg);
5321 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5323 lu_site_purge(env, top->ld_site, -1);
5325 m->mdt_child = NULL;
5326 m->mdt_bottom = NULL;
5328 obd_disconnect(m->mdt_child_exp);
5329 m->mdt_child_exp = NULL;
5331 obd_disconnect(m->mdt_bottom_exp);
5332 m->mdt_child_exp = NULL;
5335 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5336 const char *next, struct obd_export **exp)
5338 struct obd_connect_data *data = NULL;
5339 struct obd_device *obd;
5343 OBD_ALLOC_PTR(data);
5345 GOTO(out, rc = -ENOMEM);
5347 obd = class_name2obd(next);
5349 CERROR("%s: can't locate next device: %s\n",
5350 mdt_obd_name(m), next);
5351 GOTO(out, rc = -ENOTCONN);
5354 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5355 data->ocd_version = LUSTRE_VERSION_CODE;
5357 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5359 CERROR("%s: cannot connect to next dev %s (%d)\n",
5360 mdt_obd_name(m), next, rc);
5370 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5371 struct lustre_cfg *cfg)
5373 char *dev = lustre_cfg_string(cfg, 0);
5374 int rc, name_size, uuid_size;
5375 char *name, *uuid, *p;
5376 struct lustre_cfg_bufs *bufs;
5377 struct lustre_cfg *lcfg;
5378 struct obd_device *obd;
5379 struct lustre_profile *lprof;
5380 struct lu_site *site;
5383 /* in 1.8 we had the only device in the stack - MDS.
5384 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5385 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5386 * to generate names and setup MDT, MDD. MDT will be using
5387 * generated name to connect to MDD. for MDD the next device
5388 * will be LOD with name taken from so called "profile" which
5389 * is generated by mount_option line
5391 * 1.8 MGS generates config. commands like this:
5392 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5393 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5394 * 2.0 MGS generates config. commands like this:
5395 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5396 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5397 * 3:lustre-MDT0000-mdtlov 4:f
5399 * we generate MDD name from MDT one, just replacing T with D
5401 * after all the preparations, the logical equivalent will be
5402 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5403 * 3:lustre-MDT0000-mdtlov 4:f
5404 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5405 * 3:lustre-MDD0000 4:f
5407 * notice we build the stack from down to top: MDD first, then MDT */
5409 name_size = MAX_OBD_NAME;
5410 uuid_size = MAX_OBD_NAME;
5412 OBD_ALLOC(name, name_size);
5413 OBD_ALLOC(uuid, uuid_size);
5414 if (name == NULL || uuid == NULL)
5415 GOTO(cleanup_mem, rc = -ENOMEM);
5417 OBD_ALLOC_PTR(bufs);
5419 GOTO(cleanup_mem, rc = -ENOMEM);
5422 p = strstr(name, "-MDT");
5424 GOTO(free_bufs, rc = -ENOMEM);
5427 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5429 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5430 if (lprof == NULL || lprof->lp_dt == NULL) {
5431 CERROR("can't find the profile: %s\n",
5432 lustre_cfg_string(cfg, 0));
5433 GOTO(free_bufs, rc = -EINVAL);
5436 lustre_cfg_bufs_reset(bufs, name);
5437 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5438 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5439 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5441 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5443 GOTO(put_profile, rc = -ENOMEM);
5444 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5446 rc = class_attach(lcfg);
5448 GOTO(lcfg_cleanup, rc);
5450 obd = class_name2obd(name);
5452 CERROR("Can not find obd %s (%s in config)\n",
5453 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5454 GOTO(lcfg_cleanup, rc = -EINVAL);
5457 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5459 lustre_cfg_bufs_reset(bufs, name);
5460 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5461 lustre_cfg_bufs_set_string(bufs, 2, dev);
5462 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5464 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5466 GOTO(class_detach, rc = -ENOMEM);
5467 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5469 rc = class_setup(obd, lcfg);
5471 GOTO(class_detach, rc);
5473 /* connect to MDD we just setup */
5474 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5476 GOTO(class_detach, rc);
5478 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5480 LASSERT(mdt_lu_site(mdt) == NULL);
5481 mdt->mdt_lu_dev.ld_site = site;
5482 site->ls_top_dev = &mdt->mdt_lu_dev;
5483 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5485 /* now connect to bottom OSD */
5486 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5487 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5489 GOTO(class_detach, rc);
5491 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5493 rc = lu_env_refill((struct lu_env *)env);
5495 CERROR("Failure to refill session: '%d'\n", rc);
5497 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5502 class_detach(obd, lcfg);
5504 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5506 class_put_profile(lprof);
5511 OBD_FREE(name, name_size);
5513 OBD_FREE(uuid, uuid_size);
5517 /* setup quota master target on MDT0 */
5518 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5519 struct lustre_cfg *cfg)
5521 struct obd_device *obd;
5522 char *dev = lustre_cfg_string(cfg, 0);
5523 char *qmtname, *uuid, *p;
5524 struct lustre_cfg_bufs *bufs;
5525 struct lustre_cfg *lcfg;
5526 struct lustre_profile *lprof;
5527 struct obd_connect_data *data;
5531 LASSERT(mdt->mdt_qmt_exp == NULL);
5532 LASSERT(mdt->mdt_qmt_dev == NULL);
5534 /* quota master is on MDT0 only for now */
5535 if (mdt->mdt_seq_site.ss_node_id != 0)
5538 /* MGS generates config commands which look as follows:
5539 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5540 * 3:lustre-MDT0000-mdtlov 4:f
5542 * We generate the QMT name from the MDT one, just replacing MD with QM
5543 * after all the preparations, the logical equivalent will be:
5544 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5545 * 3:lustre-MDT0000-osd 4:f */
5546 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5547 OBD_ALLOC(uuid, UUID_MAX);
5548 OBD_ALLOC_PTR(bufs);
5549 OBD_ALLOC_PTR(data);
5550 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5551 GOTO(cleanup_mem, rc = -ENOMEM);
5553 strcpy(qmtname, dev);
5554 p = strstr(qmtname, "-MDT");
5556 GOTO(cleanup_mem, rc = -ENOMEM);
5557 /* replace MD with QM */
5561 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5563 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5564 if (lprof == NULL || lprof->lp_dt == NULL) {
5565 CERROR("can't find profile for %s\n",
5566 lustre_cfg_string(cfg, 0));
5567 GOTO(cleanup_mem, rc = -EINVAL);
5570 lustre_cfg_bufs_reset(bufs, qmtname);
5571 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5572 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5573 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5575 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5577 GOTO(put_profile, rc = -ENOMEM);
5578 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5580 rc = class_attach(lcfg);
5582 GOTO(lcfg_cleanup, rc);
5584 obd = class_name2obd(qmtname);
5586 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5587 lustre_cfg_string(cfg, 0));
5588 GOTO(lcfg_cleanup, rc = -EINVAL);
5591 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5593 lustre_cfg_bufs_reset(bufs, qmtname);
5594 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5595 lustre_cfg_bufs_set_string(bufs, 2, dev);
5597 /* for quota, the next device should be the OSD device */
5598 lustre_cfg_bufs_set_string(bufs, 3,
5599 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5601 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5603 GOTO(class_detach, rc = -ENOMEM);
5604 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5606 rc = class_setup(obd, lcfg);
5608 GOTO(class_detach, rc);
5610 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5612 /* configure local quota objects */
5613 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5616 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5620 GOTO(class_cleanup, rc);
5622 /* connect to quota master target */
5623 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5624 data->ocd_version = LUSTRE_VERSION_CODE;
5625 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5628 CERROR("cannot connect to quota master device %s (%d)\n",
5630 GOTO(class_cleanup, rc);
5636 class_manual_cleanup(obd);
5637 mdt->mdt_qmt_dev = NULL;
5638 GOTO(lcfg_cleanup, rc);
5642 class_detach(obd, lcfg);
5644 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5646 class_put_profile(lprof);
5651 OBD_FREE(qmtname, MAX_OBD_NAME);
5653 OBD_FREE(uuid, UUID_MAX);
5659 /* Shutdown quota master target associated with mdt */
5660 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5664 if (mdt->mdt_qmt_exp == NULL)
5666 LASSERT(mdt->mdt_qmt_dev != NULL);
5668 /* the qmt automatically shuts down when the mdt disconnects */
5669 obd_disconnect(mdt->mdt_qmt_exp);
5670 mdt->mdt_qmt_exp = NULL;
5671 mdt->mdt_qmt_dev = NULL;
5675 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5676 * for now. This will be removed along with converting rest of MDT code
5677 * to use tgt_session_info */
5678 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5680 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5683 if (unlikely(info->mti_object == NULL))
5686 rc = mdt_getxattr(info);
5688 mdt_thread_info_fini(info);
5692 static int mdt_llog_open(struct tgt_session_info *tsi)
5696 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5697 RETURN(err_serious(-EACCES));
5699 RETURN(tgt_llog_open(tsi));
5702 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5703 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5704 #define OST_BRW_READ OST_READ
5705 #define OST_BRW_WRITE OST_WRITE
5707 static struct tgt_handler mdt_tgt_handlers[] = {
5708 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5709 0, MDS_CONNECT, mdt_tgt_connect,
5710 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5711 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5712 0, MDS_DISCONNECT, tgt_disconnect,
5713 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5714 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5715 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5716 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5717 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5718 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5719 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5720 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5722 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5723 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5724 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5725 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5726 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5727 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5728 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5729 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5731 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5732 mdt_hsm_ct_register),
5733 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5734 mdt_hsm_ct_unregister),
5735 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5737 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5739 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5740 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5742 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5745 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5748 static struct tgt_handler mdt_io_ops[] = {
5749 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5751 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5753 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5754 OST_PUNCH, mdt_punch_hdl,
5756 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5757 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
5759 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5760 TGT_RPC_HANDLER(OST_FIRST_OPC,
5761 0, OST_SET_INFO, mdt_io_set_info,
5762 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
5765 static struct tgt_handler mdt_sec_ctx_ops[] = {
5766 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5767 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5768 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5771 static struct tgt_handler mdt_quota_ops[] = {
5772 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5775 static struct tgt_handler mdt_llog_handlers[] = {
5776 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5777 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5778 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5779 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5782 static struct tgt_opc_slice mdt_common_slice[] = {
5784 .tos_opc_start = MDS_FIRST_OPC,
5785 .tos_opc_end = MDS_LAST_OPC,
5786 .tos_hs = mdt_tgt_handlers
5789 .tos_opc_start = OBD_FIRST_OPC,
5790 .tos_opc_end = OBD_LAST_OPC,
5791 .tos_hs = tgt_obd_handlers
5794 .tos_opc_start = LDLM_FIRST_OPC,
5795 .tos_opc_end = LDLM_LAST_OPC,
5796 .tos_hs = tgt_dlm_handlers
5799 .tos_opc_start = SEC_FIRST_OPC,
5800 .tos_opc_end = SEC_LAST_OPC,
5801 .tos_hs = mdt_sec_ctx_ops
5804 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5805 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5806 .tos_hs = tgt_out_handlers
5809 .tos_opc_start = FLD_FIRST_OPC,
5810 .tos_opc_end = FLD_LAST_OPC,
5811 .tos_hs = fld_handlers
5814 .tos_opc_start = SEQ_FIRST_OPC,
5815 .tos_opc_end = SEQ_LAST_OPC,
5816 .tos_hs = seq_handlers
5819 .tos_opc_start = QUOTA_DQACQ,
5820 .tos_opc_end = QUOTA_LAST_OPC,
5821 .tos_hs = mdt_quota_ops
5824 .tos_opc_start = LLOG_FIRST_OPC,
5825 .tos_opc_end = LLOG_LAST_OPC,
5826 .tos_hs = mdt_llog_handlers
5829 .tos_opc_start = LFSCK_FIRST_OPC,
5830 .tos_opc_end = LFSCK_LAST_OPC,
5831 .tos_hs = tgt_lfsck_handlers
5834 .tos_opc_start = OST_FIRST_OPC,
5835 .tos_opc_end = OST_LAST_OPC,
5836 .tos_hs = mdt_io_ops
5843 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5845 struct md_device *next = m->mdt_child;
5846 struct lu_device *d = &m->mdt_lu_dev;
5847 struct obd_device *obd = mdt2obd_dev(m);
5848 struct lfsck_stop stop;
5851 stop.ls_status = LS_PAUSED;
5853 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5855 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5857 mdt_restriper_stop(m);
5858 ping_evictor_stop();
5860 /* Remove the HSM /proc entry so the coordinator cannot be
5861 * restarted by a user while it's shutting down.
5863 mdt_hsm_cdt_stop(m);
5865 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5866 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5868 if (m->mdt_namespace != NULL)
5869 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5870 d->ld_obd->obd_force);
5872 obd_exports_barrier(obd);
5873 obd_zombie_barrier();
5875 mdt_quota_fini(env, m);
5877 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5879 /* Calling the cleanup functions in the same order as in the mdt_init0
5882 mdt_tunables_fini(m);
5884 target_recovery_fini(obd);
5885 upcall_cache_cleanup(m->mdt_identity_cache);
5886 m->mdt_identity_cache = NULL;
5888 tgt_fini(env, &m->mdt_lut);
5890 mdt_hsm_cdt_fini(m);
5892 if (m->mdt_los != NULL) {
5893 local_oid_storage_fini(env, m->mdt_los);
5897 if (m->mdt_namespace != NULL) {
5898 ldlm_namespace_free_post(m->mdt_namespace);
5899 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5902 if (m->mdt_md_root != NULL) {
5903 mdt_object_put(env, m->mdt_md_root);
5904 m->mdt_md_root = NULL;
5907 mdt_seq_fini(env, m);
5909 mdt_fld_fini(env, m);
5914 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5916 LASSERT(atomic_read(&d->ld_ref) == 0);
5918 server_put_mount(mdt_obd_name(m), true);
5923 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5925 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5926 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5928 const struct dt_device_param *dt_conf;
5929 struct mdt_thread_info *info;
5930 struct obd_device *obd;
5931 const char *dev = lustre_cfg_string(cfg, 0);
5932 const char *num = lustre_cfg_string(cfg, 2);
5933 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5934 struct lustre_mount_info *lmi = NULL;
5935 struct lustre_sb_info *lsi;
5937 struct seq_server_site *ss_site;
5938 const char *identity_upcall = "NONE";
5939 struct md_device *next;
5946 lu_device_init(&m->mdt_lu_dev, ldt);
5948 * Environment (env) might be missing mdt_thread_key values at that
5949 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5952 * Usually device allocation path doesn't use module key values, but
5953 * mdt has to do a lot of work here, so allocate key value.
5955 rc = lu_env_refill((struct lu_env *)env);
5959 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5960 LASSERT(info != NULL);
5962 obd = class_name2obd(dev);
5963 LASSERT(obd != NULL);
5965 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5966 m->mdt_opts.mo_evict_tgt_nids = 1;
5967 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5969 lmi = server_get_mount(dev);
5971 CERROR("Cannot get mount info for %s!\n", dev);
5974 lsi = s2lsi(lmi->lmi_sb);
5975 LASSERT(lsi->lsi_lmd);
5976 /* CMD is supported only in IAM mode */
5978 rc = kstrtol(num, 10, &node_id);
5983 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5984 m->mdt_skip_lfsck = 1;
5987 /* Just try to get a DoM lock by default. Otherwise, having a group
5988 * lock granted, it may get blocked for a long time. */
5989 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5990 /* DoM files are read at open and data is packed in the reply */
5991 m->mdt_opts.mo_dom_read_open = 1;
5993 m->mdt_squash.rsi_uid = 0;
5994 m->mdt_squash.rsi_gid = 0;
5995 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5996 spin_lock_init(&m->mdt_squash.rsi_lock);
5997 spin_lock_init(&m->mdt_lock);
5998 m->mdt_enable_chprojid_gid = 0;
5999 m->mdt_enable_dir_migration = 1;
6000 m->mdt_enable_dir_restripe = 0;
6001 m->mdt_enable_dir_auto_split = 0;
6002 m->mdt_enable_parallel_rename_dir = 1;
6003 m->mdt_enable_parallel_rename_file = 1;
6004 m->mdt_enable_remote_dir = 1;
6005 m->mdt_enable_remote_dir_gid = 0;
6006 m->mdt_enable_remote_rename = 1;
6007 m->mdt_enable_remote_subdir_mount = 1;
6008 m->mdt_enable_striped_dir = 1;
6009 m->mdt_dir_restripe_nsonly = 1;
6011 atomic_set(&m->mdt_mds_mds_conns, 0);
6012 atomic_set(&m->mdt_async_commit_count, 0);
6014 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
6015 m->mdt_lu_dev.ld_obd = obd;
6016 /* Set this lu_device to obd for error handling purposes. */
6017 obd->obd_lu_dev = &m->mdt_lu_dev;
6019 /* init the stack */
6020 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
6022 CERROR("%s: Can't init device stack, rc %d\n",
6023 mdt_obd_name(m), rc);
6028 ss_site = mdt_seq_site(m);
6029 s->ld_seq_site = ss_site;
6032 /* set server index */
6033 ss_site->ss_node_id = node_id;
6035 /* failover is the default
6036 * FIXME: we do not failout mds0/mgs, which may cause some problems.
6037 * assumed whose ss_node_id == 0 XXX
6039 obd->obd_replayable = 1;
6040 /* No connection accepted until configurations will finish */
6041 obd->obd_no_conn = 1;
6043 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
6044 char *str = lustre_cfg_string(cfg, 4);
6045 if (strchr(str, 'n')) {
6046 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
6047 obd->obd_replayable = 0;
6051 rc = mdt_fld_init(env, mdt_obd_name(m), m);
6053 GOTO(err_fini_stack, rc);
6055 rc = mdt_seq_init(env, m);
6057 GOTO(err_fini_fld, rc);
6059 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
6060 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
6061 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
6062 LDLM_NAMESPACE_SERVER,
6063 LDLM_NAMESPACE_GREEDY,
6065 if (IS_ERR(m->mdt_namespace)) {
6066 rc = PTR_ERR(m->mdt_namespace);
6067 CERROR("%s: unable to create server namespace: rc = %d\n",
6069 m->mdt_namespace = NULL;
6070 GOTO(err_fini_seq, rc);
6073 m->mdt_namespace->ns_lvbp = m;
6074 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
6076 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
6077 /* set obd_namespace for compatibility with old code */
6078 obd->obd_namespace = m->mdt_namespace;
6080 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
6081 OBD_FAIL_MDS_ALL_REQUEST_NET,
6082 OBD_FAIL_MDS_ALL_REPLY_NET);
6084 GOTO(err_free_ns, rc);
6086 /* Amount of available space excluded from granting and reserved
6087 * for metadata. It is a percentage of the total MDT size. */
6088 tgd->tgd_reserved_pcnt = 10;
6090 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
6091 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
6093 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6095 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
6096 GOTO(err_tgt, rc = -ENOENT);
6098 fid.f_seq = FID_SEQ_LOCAL_NAME;
6101 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6105 rc = mdt_hsm_cdt_init(m);
6107 CERROR("%s: error initializing coordinator, rc %d\n",
6108 mdt_obd_name(m), rc);
6109 GOTO(err_los_fini, rc);
6112 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6114 next = m->mdt_child;
6115 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6117 mntopts = dt_conf->ddp_mntopts;
6119 if (mntopts & MNTOPT_USERXATTR)
6120 m->mdt_opts.mo_user_xattr = 1;
6122 m->mdt_opts.mo_user_xattr = 0;
6124 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6126 if (mntopts & MNTOPT_ACL)
6127 m->mdt_opts.mo_acl = 1;
6129 m->mdt_opts.mo_acl = 0;
6131 m->mdt_opts.mo_enable_strict_som = 1;
6133 /* XXX: to support suppgid for ACL, we enable identity_upcall
6134 * by default, otherwise, maybe got unexpected -EACCESS. */
6135 if (m->mdt_opts.mo_acl)
6136 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6138 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6140 &mdt_identity_upcall_cache_ops);
6141 if (IS_ERR(m->mdt_identity_cache)) {
6142 rc = PTR_ERR(m->mdt_identity_cache);
6143 m->mdt_identity_cache = NULL;
6144 GOTO(err_free_hsm, rc);
6147 rc = mdt_tunables_init(m, dev);
6149 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
6150 GOTO(err_recovery, rc);
6153 rc = mdt_quota_init(env, m, cfg);
6155 GOTO(err_procfs, rc);
6157 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
6158 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
6159 "mdt_ldlm_client", m->mdt_ldlm_client);
6161 ping_evictor_start();
6163 /* recovery will be started upon mdt_prepare()
6164 * when the whole stack is complete and ready
6165 * to serve the requests */
6167 /* Reduce the initial timeout on an MDS because it doesn't need such
6168 * a long timeout as an OST does. Adaptive timeouts will adjust this
6169 * value appropriately. */
6170 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
6171 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
6173 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
6174 m->mdt_lut.lut_local_recovery = 1;
6176 rc = mdt_restriper_start(m);
6178 GOTO(err_ping_evictor, rc);
6183 ping_evictor_stop();
6185 mdt_tunables_fini(m);
6187 upcall_cache_cleanup(m->mdt_identity_cache);
6188 m->mdt_identity_cache = NULL;
6190 mdt_hsm_cdt_fini(m);
6192 local_oid_storage_fini(env, m->mdt_los);
6195 /* keep recoverable clients */
6197 target_recovery_fini(obd);
6198 obd_exports_barrier(obd);
6199 obd_zombie_barrier();
6200 tgt_fini(env, &m->mdt_lut);
6202 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
6203 obd->obd_namespace = m->mdt_namespace = NULL;
6205 mdt_seq_fini(env, m);
6207 mdt_fld_fini(env, m);
6209 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6212 server_put_mount(dev, true);
6216 /* For interoperability, the left element is old parameter, the right one
6217 * is the new version of the parameter, if some parameter is deprecated,
6218 * the new version should be set as NULL. */
6219 static struct cfg_interop_param mdt_interop_param[] = {
6220 { "mdt.group_upcall", NULL },
6221 { "mdt.quota_type", NULL },
6222 { "mdd.quota_type", NULL },
6223 { "mdt.som", NULL },
6224 { "mdt.rootsquash", "mdt.root_squash" },
6225 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
6229 /* used by MGS to process specific configurations */
6230 static int mdt_process_config(const struct lu_env *env,
6231 struct lu_device *d, struct lustre_cfg *cfg)
6233 struct mdt_device *m = mdt_dev(d);
6234 struct md_device *md_next = m->mdt_child;
6235 struct lu_device *next = md2lu_dev(md_next);
6239 switch (cfg->lcfg_command) {
6241 struct obd_device *obd = d->ld_obd;
6242 /* For interoperability */
6243 struct cfg_interop_param *ptr = NULL;
6244 struct lustre_cfg *old_cfg = NULL;
6248 param = lustre_cfg_string(cfg, 1);
6249 if (param == NULL) {
6250 CERROR("param is empty\n");
6255 ptr = class_find_old_param(param, mdt_interop_param);
6257 if (ptr->new_param == NULL) {
6259 CWARN("For interoperability, skip this %s."
6260 " It is obsolete.\n", ptr->old_param);
6264 CWARN("Found old param %s, changed it to %s.\n",
6265 ptr->old_param, ptr->new_param);
6268 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
6275 count = class_modify_config(cfg, PARAM_MDT,
6276 &obd->obd_kset.kobj);
6278 struct coordinator *cdt = &m->mdt_coordinator;
6280 /* is it an HSM var ? */
6281 count = class_modify_config(cfg, PARAM_HSM,
6282 &cdt->cdt_hsm_kobj);
6284 /* we don't understand; pass it on */
6285 rc = next->ld_ops->ldo_process_config(env, next,
6288 rc = count > 0 ? 0 : count;
6290 rc = count > 0 ? 0 : count;
6294 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
6295 cfg->lcfg_buflens));
6299 /* others are passed further */
6300 rc = next->ld_ops->ldo_process_config(env, next, cfg);
6306 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
6307 const struct lu_object_header *hdr,
6308 struct lu_device *d)
6310 struct mdt_object *mo;
6314 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6316 struct lu_object *o;
6317 struct lu_object_header *h;
6320 h = &mo->mot_header;
6321 lu_object_header_init(h);
6322 lu_object_init(o, h, d);
6323 lu_object_add_top(h, o);
6324 o->lo_ops = &mdt_obj_ops;
6325 spin_lock_init(&mo->mot_write_lock);
6326 mutex_init(&mo->mot_som_mutex);
6327 mutex_init(&mo->mot_lov_mutex);
6328 init_rwsem(&mo->mot_dom_sem);
6329 init_rwsem(&mo->mot_open_sem);
6330 atomic_set(&mo->mot_open_count, 0);
6331 mo->mot_restripe_offset = 0;
6332 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6333 mo->mot_lsom_size = 0;
6334 mo->mot_lsom_blocks = 0;
6335 mo->mot_lsom_inited = false;
6341 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6342 const struct lu_object_conf *unused)
6344 struct mdt_device *d = mdt_dev(o->lo_dev);
6345 struct lu_device *under;
6346 struct lu_object *below;
6350 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6351 PFID(lu_object_fid(o)));
6353 under = &d->mdt_child->md_lu_dev;
6354 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6355 if (below != NULL) {
6356 lu_object_add(o, below);
6363 static void mdt_object_free_rcu(struct rcu_head *head)
6365 struct mdt_object *mo = container_of(head, struct mdt_object,
6366 mot_header.loh_rcu);
6368 kmem_cache_free(mdt_object_kmem, mo);
6371 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6373 struct mdt_object *mo = mdt_obj(o);
6374 struct lu_object_header *h;
6378 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6379 PFID(lu_object_fid(o)));
6381 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6382 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6385 lu_object_header_fini(h);
6386 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6387 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6392 static int mdt_object_print(const struct lu_env *env, void *cookie,
6393 lu_printer_t p, const struct lu_object *o)
6395 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6397 return (*p)(env, cookie,
6398 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6399 mdto, mdto->mot_lov_created ? "lov_created" : "",
6400 mdto->mot_cache_attr ? "cache_attr" : "",
6401 mdto->mot_write_count);
6404 static int mdt_prepare(const struct lu_env *env,
6405 struct lu_device *pdev,
6406 struct lu_device *cdev)
6408 struct mdt_device *mdt = mdt_dev(cdev);
6409 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6410 struct obd_device *obd = cdev->ld_obd;
6417 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6421 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6425 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6429 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6430 /* The LFSCK instance is registered just now, so it must be there when
6431 * register the namespace to such instance. */
6432 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6434 if (mdt->mdt_seq_site.ss_node_id == 0) {
6435 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6436 &mdt->mdt_md_root_fid);
6441 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6443 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6444 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6445 LASSERT(obd->obd_no_conn);
6446 spin_lock(&obd->obd_dev_lock);
6447 obd->obd_no_conn = 0;
6448 spin_unlock(&obd->obd_dev_lock);
6450 if (obd->obd_recovering == 0)
6451 mdt_postrecov(env, mdt);
6456 const struct lu_device_operations mdt_lu_ops = {
6457 .ldo_object_alloc = mdt_object_alloc,
6458 .ldo_process_config = mdt_process_config,
6459 .ldo_prepare = mdt_prepare,
6462 static const struct lu_object_operations mdt_obj_ops = {
6463 .loo_object_init = mdt_object_init,
6464 .loo_object_free = mdt_object_free,
6465 .loo_object_print = mdt_object_print
6468 static int mdt_obd_set_info_async(const struct lu_env *env,
6469 struct obd_export *exp,
6470 __u32 keylen, void *key,
6471 __u32 vallen, void *val,
6472 struct ptlrpc_request_set *set)
6478 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6479 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6486 static inline void mdt_enable_slc(struct mdt_device *mdt)
6488 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6489 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6492 static inline void mdt_disable_slc(struct mdt_device *mdt)
6494 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6495 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6499 * Match client and server connection feature flags.
6501 * Compute the compatibility flags for a connection request based on
6502 * features mutually supported by client and server.
6504 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6505 * must not be updated here, otherwise a partially initialized value may
6506 * be exposed. After the connection request is successfully processed,
6507 * the top-level MDT connect request handler atomically updates the export
6508 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6509 * reply. \see mdt_connect().
6511 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6512 * full struct obd_connect_data. So care must be taken when accessing fields
6513 * that are not present in struct obd_connect_data_v1. See LU-16.
6515 * \param exp the obd_export associated with this client/target pair
6516 * \param mdt the target device for the connection
6517 * \param data stores data for this connect request
6520 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6521 * \retval -EBADE client and server feature requirements are incompatible
6523 static int mdt_connect_internal(const struct lu_env *env,
6524 struct obd_export *exp,
6525 struct mdt_device *mdt,
6526 struct obd_connect_data *data, bool reconnect)
6528 const char *obd_name = mdt_obd_name(mdt);
6529 LASSERT(data != NULL);
6531 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6533 if (mdt->mdt_bottom->dd_rdonly &&
6534 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6535 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6538 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6539 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6541 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6543 if (!mdt->mdt_opts.mo_acl)
6544 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6546 if (!mdt->mdt_opts.mo_user_xattr)
6547 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6549 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6550 data->ocd_brw_size = min(data->ocd_brw_size,
6552 if (data->ocd_brw_size == 0) {
6553 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6554 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6555 "ocd_brw_size unexpectedly zero, network data "
6556 "corruption? Refusing to connect this client\n",
6557 obd_name, exp->exp_client_uuid.uuid,
6558 exp, data->ocd_connect_flags, data->ocd_version,
6559 data->ocd_grant, data->ocd_index);
6564 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6565 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6567 /* client is reporting its page size, for future use */
6568 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6569 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6570 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6571 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6572 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6573 /* ocd_grant_tax_kb is in 1K byte blocks */
6574 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6575 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6578 /* Save connect_data we have so far because tgt_grant_connect()
6579 * uses it to calculate grant, and we want to save the client
6580 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6581 exp->exp_connect_data = *data;
6582 if (OCD_HAS_FLAG(data, GRANT))
6583 tgt_grant_connect(env, exp, data, !reconnect);
6585 if (OCD_HAS_FLAG(data, MAXBYTES))
6586 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6588 /* NB: Disregard the rule against updating
6589 * exp_connect_data.ocd_connect_flags in this case, since
6590 * tgt_client_new() needs to know if this is a lightweight
6591 * connection, and it is safe to expose this flag before
6592 * connection processing completes. */
6593 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6594 spin_lock(&exp->exp_lock);
6595 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6596 spin_unlock(&exp->exp_lock);
6599 data->ocd_version = LUSTRE_VERSION_CODE;
6601 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6602 CWARN("%s: MDS requires FID support, but client not\n",
6607 if (OCD_HAS_FLAG(data, PINGLESS)) {
6608 if (ptlrpc_pinger_suppress_pings()) {
6609 spin_lock(&exp->exp_obd->obd_dev_lock);
6610 list_del_init(&exp->exp_obd_chain_timed);
6611 spin_unlock(&exp->exp_obd->obd_dev_lock);
6613 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6617 data->ocd_max_easize = mdt->mdt_max_ea_size;
6619 /* NB: Disregard the rule against updating
6620 * exp_connect_data.ocd_connect_flags in this case, since
6621 * tgt_client_new() needs to know if this is client supports
6622 * multiple modify RPCs, and it is safe to expose this flag before
6623 * connection processing completes. */
6624 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6625 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6626 spin_lock(&exp->exp_lock);
6627 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6628 spin_unlock(&exp->exp_lock);
6631 if (OCD_HAS_FLAG(data, CKSUM)) {
6632 __u32 cksum_types = data->ocd_cksum_types;
6634 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6636 if (unlikely(data->ocd_cksum_types == 0)) {
6637 CERROR("%s: Connect with checksum support but no "
6638 "ocd_cksum_types is set\n",
6639 exp->exp_obd->obd_name);
6643 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6644 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6645 cksum_types, data->ocd_cksum_types);
6647 /* This client does not support OBD_CONNECT_CKSUM
6648 * fall back to CRC32 */
6649 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6650 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6651 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6654 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6655 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6656 atomic_inc(&mdt->mdt_mds_mds_conns);
6657 mdt_enable_slc(mdt);
6660 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6661 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6666 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6667 struct mdt_thread_info *info,
6668 struct mdt_file_data *mfd)
6670 struct lu_context ses;
6674 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6679 lu_context_enter(&ses);
6681 mdt_ucred(info)->uc_valid = UCRED_OLD;
6682 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6684 lu_context_exit(&ses);
6685 lu_context_fini(&ses);
6691 static int mdt_export_cleanup(struct obd_export *exp)
6693 LIST_HEAD(closing_list);
6694 struct mdt_export_data *med = &exp->exp_mdt_data;
6695 struct obd_device *obd = exp->exp_obd;
6696 struct mdt_device *mdt;
6697 struct mdt_thread_info *info;
6699 struct mdt_file_data *mfd, *n;
6703 spin_lock(&med->med_open_lock);
6704 while (!list_empty(&med->med_open_head)) {
6705 struct list_head *tmp = med->med_open_head.next;
6706 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6708 /* Remove mfd handle so it can't be found again.
6709 * We are consuming the mfd_list reference here. */
6710 class_handle_unhash(&mfd->mfd_open_handle);
6711 list_move_tail(&mfd->mfd_list, &closing_list);
6713 spin_unlock(&med->med_open_lock);
6714 mdt = mdt_dev(obd->obd_lu_dev);
6715 LASSERT(mdt != NULL);
6717 rc = lu_env_init(&env, LCT_MD_THREAD);
6721 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6722 LASSERT(info != NULL);
6723 memset(info, 0, sizeof *info);
6724 info->mti_env = &env;
6725 info->mti_mdt = mdt;
6726 info->mti_exp = exp;
6728 if (!list_empty(&closing_list)) {
6729 struct md_attr *ma = &info->mti_attr;
6731 /* Close any open files (which may also cause orphan
6733 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6734 list_del_init(&mfd->mfd_list);
6735 ma->ma_need = ma->ma_valid = 0;
6737 /* This file is being closed due to an eviction, it
6738 * could have been modified and now dirty regarding to
6739 * HSM archive, check this!
6740 * The logic here is to mark a file dirty if there's a
6741 * chance it was dirtied before the client was evicted,
6742 * so that we don't have to wait for a release attempt
6743 * before finding out the file was actually dirty and
6744 * fail the release. Aggressively marking it dirty here
6745 * will cause the policy engine to attempt to
6746 * re-archive it; when rearchiving, we can compare the
6747 * current version to the HSM data_version and make the
6748 * archive request into a noop if it's not actually
6751 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6752 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6754 /* Don't unlink orphan on failover umount, LU-184 */
6755 if (exp->exp_flags & OBD_OPT_FAILOVER ||
6756 exp->exp_obd->obd_stopping) {
6757 ma->ma_valid = MA_FLAGS;
6758 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6760 ma->ma_valid |= MA_FORCE_LOG;
6761 mdt_mfd_close(info, mfd);
6764 info->mti_mdt = NULL;
6765 /* cleanup client slot early */
6766 /* Do not erase record for recoverable client. */
6767 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6768 tgt_client_del(&env, exp);
6774 static int mdt_obd_disconnect(struct obd_export *exp)
6781 class_export_get(exp);
6783 if (!(exp->exp_flags & OBD_OPT_FORCE))
6784 tgt_grant_sanity_check(exp->exp_obd, __func__);
6786 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6787 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6788 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6790 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6791 mdt_disable_slc(mdt);
6794 rc = server_disconnect_export(exp);
6796 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6798 tgt_grant_discard(exp);
6800 rc = mdt_export_cleanup(exp);
6801 nodemap_del_member(exp);
6802 class_export_put(exp);
6806 /* mds_connect copy */
6807 static int mdt_obd_connect(const struct lu_env *env,
6808 struct obd_export **exp, struct obd_device *obd,
6809 struct obd_uuid *cluuid,
6810 struct obd_connect_data *data,
6813 struct obd_export *lexp;
6814 struct lustre_handle conn = { 0 };
6815 struct mdt_device *mdt;
6817 lnet_nid_t *client_nid = localdata;
6820 LASSERT(env != NULL);
6821 LASSERT(data != NULL);
6823 if (!exp || !obd || !cluuid)
6826 mdt = mdt_dev(obd->obd_lu_dev);
6829 * first, check whether the stack is ready to handle requests
6830 * XXX: probably not very appropriate method is used now
6831 * at some point we should find a better one
6833 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6834 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6835 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6836 rc = obd_get_info(env, mdt->mdt_child_exp,
6837 sizeof(KEY_OSP_CONNECTED),
6838 KEY_OSP_CONNECTED, NULL, NULL);
6841 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6844 rc = class_connect(&conn, obd, cluuid);
6848 lexp = class_conn2export(&conn);
6849 LASSERT(lexp != NULL);
6851 rc = nodemap_add_member(*client_nid, lexp);
6852 if (rc != 0 && rc != -EEXIST)
6855 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6857 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6860 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6861 rc = tgt_client_new(env, lexp);
6863 mdt_export_stats_init(obd, lexp, localdata);
6867 class_disconnect(lexp);
6868 nodemap_del_member(lexp);
6872 /* Because we do not want this export to be evicted by pinger,
6873 * let's not add this export to the timed chain list. */
6874 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6875 spin_lock(&lexp->exp_obd->obd_dev_lock);
6876 list_del_init(&lexp->exp_obd_chain_timed);
6877 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6884 static int mdt_obd_reconnect(const struct lu_env *env,
6885 struct obd_export *exp, struct obd_device *obd,
6886 struct obd_uuid *cluuid,
6887 struct obd_connect_data *data,
6890 lnet_nid_t *client_nid = localdata;
6894 if (exp == NULL || obd == NULL || cluuid == NULL)
6897 rc = nodemap_add_member(*client_nid, exp);
6898 if (rc != 0 && rc != -EEXIST)
6901 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6904 mdt_export_stats_init(obd, exp, localdata);
6906 nodemap_del_member(exp);
6911 /* FIXME: Can we avoid using these two interfaces? */
6912 static int mdt_init_export(struct obd_export *exp)
6914 struct mdt_export_data *med = &exp->exp_mdt_data;
6918 INIT_LIST_HEAD(&med->med_open_head);
6919 spin_lock_init(&med->med_open_lock);
6920 spin_lock(&exp->exp_lock);
6921 exp->exp_connecting = 1;
6922 spin_unlock(&exp->exp_lock);
6924 OBD_ALLOC(exp->exp_used_slots,
6925 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6926 if (exp->exp_used_slots == NULL)
6929 /* self-export doesn't need client data and ldlm initialization */
6930 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6931 &exp->exp_client_uuid)))
6934 rc = tgt_client_alloc(exp);
6938 rc = ldlm_init_export(exp);
6945 tgt_client_free(exp);
6947 OBD_FREE(exp->exp_used_slots,
6948 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6949 exp->exp_used_slots = NULL;
6951 CERROR("%s: Failed to initialize export: rc = %d\n",
6952 exp->exp_obd->obd_name, rc);
6956 static int mdt_destroy_export(struct obd_export *exp)
6960 target_destroy_export(exp);
6961 if (exp->exp_used_slots)
6962 OBD_FREE(exp->exp_used_slots,
6963 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6965 /* destroy can be called from failed obd_setup, so
6966 * checking uuid is safer than obd_self_export */
6967 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6968 &exp->exp_client_uuid)))
6971 ldlm_destroy_export(exp);
6972 tgt_client_free(exp);
6974 LASSERT(list_empty(&exp->exp_outstanding_replies));
6975 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6978 * discard grants once we're sure no more
6979 * interaction with the client is possible
6981 tgt_grant_discard(exp);
6982 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6983 obd2obt(exp->exp_obd)->obt_lut->lut_tgd.tgd_tot_granted_clients--;
6985 if (!(exp->exp_flags & OBD_OPT_FORCE))
6986 tgt_grant_sanity_check(exp->exp_obd, __func__);
6991 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6992 struct linkea_data *ldata)
6996 LASSERT(ldata->ld_buf->lb_buf != NULL);
6998 if (!mdt_object_exists(mdt_obj))
7001 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7002 ldata->ld_buf, XATTR_NAME_LINK);
7003 if (rc == -ERANGE) {
7004 /* Buf was too small, figure out what we need. */
7005 lu_buf_free(ldata->ld_buf);
7006 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7007 ldata->ld_buf, XATTR_NAME_LINK);
7010 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
7011 if (ldata->ld_buf->lb_buf == NULL)
7013 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
7014 ldata->ld_buf, XATTR_NAME_LINK);
7019 return linkea_init_with_rec(ldata);
7023 * Given an MDT object, try to look up the full path to the object.
7024 * Part of the MDT layer implementation of lfs fid2path.
7026 * \param[in] info Per-thread common data shared by MDT level handlers.
7027 * \param[in] obj Object to do path lookup of
7028 * \param[in,out] fp User-provided struct to store path information
7029 * \param[in] root_fid Root FID of current path should reach
7031 * \retval 0 Lookup successful, path information stored in fp
7032 * \retval -EAGAIN Lookup failed, usually because object is being moved
7033 * \retval negative errno if there was a problem
7035 static int mdt_path_current(struct mdt_thread_info *info,
7036 struct mdt_object *obj,
7037 struct getinfo_fid2path *fp,
7038 struct lu_fid *root_fid)
7040 struct mdt_device *mdt = info->mti_mdt;
7041 struct lu_name *tmpname = &info->mti_name;
7042 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
7043 struct lu_buf *buf = &info->mti_big_buf;
7044 struct linkea_data ldata = { NULL };
7046 struct mdt_object *mdt_obj;
7047 struct link_ea_header *leh;
7048 struct link_ea_entry *lee;
7055 /* temp buffer for path element, the buffer will be finally freed
7056 * in mdt_thread_info_fini */
7057 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
7058 if (buf->lb_buf == NULL)
7062 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
7065 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
7067 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
7068 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
7069 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
7070 GOTO(out, rc = -ENOENT);
7072 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
7074 mdt_object_get(info->mti_env, mdt_obj);
7076 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
7077 if (IS_ERR(mdt_obj))
7078 GOTO(out, rc = PTR_ERR(mdt_obj));
7081 if (!mdt_object_exists(mdt_obj)) {
7082 mdt_object_put(info->mti_env, mdt_obj);
7083 GOTO(out, rc = -ENOENT);
7086 if (mdt_object_remote(mdt_obj)) {
7087 mdt_object_put(info->mti_env, mdt_obj);
7088 GOTO(remote_out, rc = -EREMOTE);
7091 rc = mdt_links_read(info, mdt_obj, &ldata);
7093 mdt_object_put(info->mti_env, mdt_obj);
7098 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
7099 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
7100 /* If set, use link #linkno for path lookup, otherwise use
7101 link #0. Only do this for the final path element. */
7102 if (first && fp->gf_linkno < leh->leh_reccount) {
7104 for (count = 0; count < fp->gf_linkno; count++) {
7105 lee = (struct link_ea_entry *)
7106 ((char *)lee + reclen);
7107 linkea_entry_unpack(lee, &reclen, tmpname,
7110 if (fp->gf_linkno < leh->leh_reccount - 1)
7111 /* indicate to user there are more links */
7115 /* Check if it is slave stripes */
7116 rc = mdt_is_dir_stripe(info, mdt_obj);
7117 mdt_object_put(info->mti_env, mdt_obj);
7121 fp->gf_fid = *tmpfid;
7125 /* Pack the name in the end of the buffer */
7126 ptr -= tmpname->ln_namelen;
7127 if (ptr - 1 <= fp->gf_u.gf_path)
7128 GOTO(out, rc = -EOVERFLOW);
7129 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
7132 /* keep the last resolved fid to the client, so the
7133 * client will build the left path on another MDT for
7135 fp->gf_fid = *tmpfid;
7140 /* non-zero will be treated as an error */
7144 ptr++; /* skip leading / */
7145 memmove(fp->gf_u.gf_path, ptr,
7146 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
7153 * Given an MDT object, use mdt_path_current to get the path.
7154 * Essentially a wrapper to retry mdt_path_current a set number of times
7155 * if -EAGAIN is returned (usually because an object is being moved).
7157 * Part of the MDT layer implementation of lfs fid2path.
7159 * \param[in] info Per-thread common data shared by mdt level handlers.
7160 * \param[in] obj Object to do path lookup of
7161 * \param[in,out] fp User-provided struct for arguments and to store path
7164 * \retval 0 Lookup successful, path information stored in fp
7165 * \retval negative errno if there was a problem
7167 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
7168 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
7170 struct mdt_device *mdt = info->mti_mdt;
7175 if (fp->gf_pathlen < 3)
7178 if (root_fid == NULL)
7179 root_fid = &mdt->mdt_md_root_fid;
7181 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
7182 fp->gf_u.gf_path[0] = '\0';
7186 /* Retry multiple times in case file is being moved */
7187 while (tries-- && rc == -EAGAIN)
7188 rc = mdt_path_current(info, obj, fp, root_fid);
7194 * Get the full path of the provided FID, as of changelog record recno.
7196 * This checks sanity and looks up object for user provided FID
7197 * before calling the actual path lookup code.
7199 * Part of the MDT layer implementation of lfs fid2path.
7201 * \param[in] info Per-thread common data shared by mdt level handlers.
7202 * \param[in,out] fp User-provided struct for arguments and to store path
7205 * \retval 0 Lookup successful, path information and recno stored in fp
7206 * \retval -ENOENT, object does not exist
7207 * \retval negative errno if there was a problem
7209 static int mdt_fid2path(struct mdt_thread_info *info,
7210 struct lu_fid *root_fid,
7211 struct getinfo_fid2path *fp)
7213 struct mdt_device *mdt = info->mti_mdt;
7214 struct mdt_object *obj;
7218 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
7219 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
7221 if (!fid_is_sane(&fp->gf_fid))
7224 if (!fid_is_namespace_visible(&fp->gf_fid)) {
7225 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
7226 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
7227 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
7231 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
7234 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
7235 PFID(&fp->gf_fid), rc);
7239 if (mdt_object_remote(obj)) {
7241 } else if (!mdt_object_exists(obj)) {
7244 struct lu_attr la = { 0 };
7245 struct dt_object *dt = mdt_obj2dt(obj);
7247 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
7248 dt_attr_get(info->mti_env, mdt_obj2dt(obj), &la);
7249 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL)
7250 /* path resolution cannot be carried out on server
7251 * side for encrypted files
7259 mdt_object_put(info->mti_env, obj);
7260 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
7261 PFID(&fp->gf_fid), rc);
7265 rc = mdt_path(info, obj, fp, root_fid);
7267 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
7268 PFID(&fp->gf_fid), fp->gf_u.gf_path,
7269 fp->gf_recno, fp->gf_linkno);
7271 mdt_object_put(info->mti_env, obj);
7276 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
7277 void *val, int vallen)
7279 struct getinfo_fid2path *fpout, *fpin;
7280 struct lu_fid *root_fid = NULL;
7283 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
7286 if (req_capsule_req_need_swab(info->mti_pill))
7287 lustre_swab_fid2path(fpin);
7289 memcpy(fpout, fpin, sizeof(*fpin));
7290 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
7293 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
7294 sizeof(struct lu_fid)) {
7295 /* client sent its root FID, which is normally fileset FID */
7296 root_fid = fpin->gf_u.gf_root_fid;
7297 if (req_capsule_req_need_swab(info->mti_pill))
7298 lustre_swab_lu_fid(root_fid);
7300 if (root_fid != NULL && !fid_is_sane(root_fid))
7304 rc = mdt_fid2path(info, root_fid, fpout);
7308 int mdt_get_info(struct tgt_session_info *tsi)
7318 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7320 CDEBUG(D_IOCTL, "No GETINFO key\n");
7321 RETURN(err_serious(-EFAULT));
7323 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7326 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7327 if (vallen == NULL) {
7328 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7329 tgt_name(tsi->tsi_tgt));
7330 RETURN(err_serious(-EFAULT));
7333 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7335 rc = req_capsule_server_pack(tsi->tsi_pill);
7337 RETURN(err_serious(rc));
7339 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7340 if (valout == NULL) {
7341 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7342 tgt_name(tsi->tsi_tgt));
7343 RETURN(err_serious(-EFAULT));
7346 if (KEY_IS(KEY_FID2PATH)) {
7347 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7349 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7350 mdt_thread_info_fini(info);
7357 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7359 struct obd_ioctl_data *data = karg;
7362 struct mdt_object *obj;
7363 struct mdt_lock_handle *lh;
7367 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7368 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7371 fid = (struct lu_fid *)data->ioc_inlbuf1;
7373 if (!fid_is_sane(fid))
7376 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7378 lh = &mti->mti_lh[MDT_LH_PARENT];
7379 mdt_lock_reg_init(lh, LCK_CR);
7381 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7383 RETURN(PTR_ERR(obj));
7385 if (mdt_object_remote(obj)) {
7388 * before calling version get the correct MDS should be
7389 * fid, this is error to find remote object here
7391 CERROR("nonlocal object "DFID"\n", PFID(fid));
7392 } else if (!mdt_object_exists(obj)) {
7393 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7396 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7397 *(__u64 *)data->ioc_inlbuf2 = version;
7400 mdt_object_unlock_put(mti, obj, lh, 1);
7404 /* ioctls on obd dev */
7405 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7406 void *karg, void __user *uarg)
7409 struct obd_device *obd = exp->exp_obd;
7410 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7411 struct dt_device *dt = mdt->mdt_bottom;
7415 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7416 rc = lu_env_init(&env, LCT_MD_THREAD);
7422 rc = mdt_device_sync(&env, mdt);
7424 case OBD_IOC_SET_READONLY:
7425 rc = dt_sync(&env, dt);
7427 rc = dt_ro(&env, dt);
7429 case OBD_IOC_ABORT_RECOVERY: {
7430 struct obd_ioctl_data *data = karg;
7432 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
7433 CERROR("%s: Aborting MDT recovery\n",
7435 obd->obd_abort_mdt_recovery = 1;
7436 wake_up(&obd->obd_next_transno_waitq);
7437 } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7438 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7439 CERROR("%s: Aborting client recovery\n",
7441 obd->obd_abort_recovery = 1;
7442 target_stop_recovery_thread(obd);
7447 case OBD_IOC_CHANGELOG_REG:
7448 case OBD_IOC_CHANGELOG_DEREG:
7449 case OBD_IOC_CHANGELOG_CLEAR:
7450 case OBD_IOC_LLOG_PRINT:
7451 case OBD_IOC_LLOG_CANCEL:
7452 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7456 case OBD_IOC_START_LFSCK: {
7457 struct md_device *next = mdt->mdt_child;
7458 struct obd_ioctl_data *data = karg;
7459 struct lfsck_start_param lsp;
7461 if (unlikely(data == NULL)) {
7466 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7467 lsp.lsp_index_valid = 0;
7468 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7471 case OBD_IOC_STOP_LFSCK: {
7472 struct md_device *next = mdt->mdt_child;
7473 struct obd_ioctl_data *data = karg;
7474 struct lfsck_stop stop;
7476 stop.ls_status = LS_STOPPED;
7477 /* Old lfsck utils may pass NULL @stop. */
7478 if (data->ioc_inlbuf1 == NULL)
7482 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7484 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7487 case OBD_IOC_QUERY_LFSCK: {
7488 struct md_device *next = mdt->mdt_child;
7489 struct obd_ioctl_data *data = karg;
7491 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7495 case OBD_IOC_GET_OBJ_VERSION: {
7496 struct mdt_thread_info *mti;
7498 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7499 memset(mti, 0, sizeof(*mti));
7500 mti->mti_env = &env;
7504 rc = mdt_ioc_version_get(mti, karg);
7507 case OBD_IOC_CATLOGLIST: {
7508 struct mdt_thread_info *mti;
7510 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7511 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7512 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7513 &mti->mti_tmp_fid1);
7518 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7519 mdt_obd_name(mdt), cmd, rc);
7526 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7528 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7532 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7533 struct lfsck_start_param lsp;
7535 lsp.lsp_start = NULL;
7536 lsp.lsp_index_valid = 0;
7537 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7538 OBD_IOC_START_LFSCK,
7540 if (rc != 0 && rc != -EALREADY)
7541 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7542 mdt_obd_name(mdt), rc);
7545 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7549 static int mdt_obd_postrecov(struct obd_device *obd)
7554 rc = lu_env_init(&env, LCT_MD_THREAD);
7557 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7562 static const struct obd_ops mdt_obd_device_ops = {
7563 .o_owner = THIS_MODULE,
7564 .o_set_info_async = mdt_obd_set_info_async,
7565 .o_connect = mdt_obd_connect,
7566 .o_reconnect = mdt_obd_reconnect,
7567 .o_disconnect = mdt_obd_disconnect,
7568 .o_init_export = mdt_init_export,
7569 .o_destroy_export = mdt_destroy_export,
7570 .o_iocontrol = mdt_iocontrol,
7571 .o_postrecov = mdt_obd_postrecov,
7572 /* Data-on-MDT IO methods */
7573 .o_preprw = mdt_obd_preprw,
7574 .o_commitrw = mdt_obd_commitrw,
7577 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7578 struct lu_device *d)
7580 struct mdt_device *m = mdt_dev(d);
7587 static struct lu_device *mdt_device_free(const struct lu_env *env,
7588 struct lu_device *d)
7590 struct mdt_device *m = mdt_dev(d);
7593 lu_device_fini(&m->mdt_lu_dev);
7599 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7600 struct lu_device_type *t,
7601 struct lustre_cfg *cfg)
7603 struct lu_device *l;
7604 struct mdt_device *m;
7611 rc = mdt_init0(env, m, t, cfg);
7613 mdt_device_free(env, l);
7618 l = ERR_PTR(-ENOMEM);
7622 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7623 LU_KEY_INIT(mdt, struct mdt_thread_info);
7625 static void mdt_key_fini(const struct lu_context *ctx,
7626 struct lu_context_key *key, void* data)
7628 struct mdt_thread_info *info = data;
7630 if (info->mti_big_lmm) {
7631 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7632 info->mti_big_lmm = NULL;
7633 info->mti_big_lmmsize = 0;
7636 if (info->mti_big_acl) {
7637 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7638 info->mti_big_acl = NULL;
7639 info->mti_big_aclsize = 0;
7645 /* context key: mdt_thread_key */
7646 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7648 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7650 return lu_ucred(info->mti_env);
7653 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7655 return lu_ucred_check(info->mti_env);
7659 * Enable/disable COS (Commit On Sharing).
7661 * Set/Clear the COS flag in mdt options.
7663 * \param mdt mdt device
7664 * \param val 0 disables COS, other values enable COS
7666 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7671 mdt->mdt_opts.mo_cos = val;
7672 rc = lu_env_init(&env, LCT_LOCAL);
7673 if (unlikely(rc != 0)) {
7674 CWARN("%s: lu_env initialization failed, cannot "
7675 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7678 mdt_device_sync(&env, mdt);
7683 * Check COS (Commit On Sharing) status.
7685 * Return COS flag status.
7687 * \param mdt mdt device
7689 int mdt_cos_is_enabled(struct mdt_device *mdt)
7691 return mdt->mdt_opts.mo_cos != 0;
7694 static const struct lu_device_type_operations mdt_device_type_ops = {
7695 .ldto_device_alloc = mdt_device_alloc,
7696 .ldto_device_free = mdt_device_free,
7697 .ldto_device_fini = mdt_device_fini
7700 static struct lu_device_type mdt_device_type = {
7701 .ldt_tags = LU_DEVICE_MD,
7702 .ldt_name = LUSTRE_MDT_NAME,
7703 .ldt_ops = &mdt_device_type_ops,
7704 .ldt_ctx_tags = LCT_MD_THREAD
7707 static int __init mdt_init(void)
7711 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7712 FID_NOBRACE_LEN + 1);
7713 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7715 rc = lu_kmem_init(mdt_caches);
7719 rc = mds_mod_init();
7723 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7724 LUSTRE_MDT_NAME, &mdt_device_type);
7729 lu_kmem_fini(mdt_caches);
7736 static void __exit mdt_exit(void)
7738 class_unregister_type(LUSTRE_MDT_NAME);
7740 lu_kmem_fini(mdt_caches);
7743 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7744 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7745 MODULE_VERSION(LUSTRE_VERSION_STRING);
7746 MODULE_LICENSE("GPL");
7748 module_init(mdt_init);
7749 module_exit(mdt_exit);