4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_handler.c
33 * Lustre Metadata Target (mdt) request handler
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
47 #include <linux/pagemap.h>
49 #include <dt_object.h>
50 #include <lustre_acl.h>
51 #include <lustre_export.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lfsck.h>
54 #include <lustre_log.h>
55 #include <lustre_nodemap.h>
56 #include <lustre_mds.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <lustre_quota.h>
59 #include <lustre_swab.h>
60 #include <lustre_lmv.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
66 #include <lustre_crypto.h>
68 #include "mdt_internal.h"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
134 return rep->lock_policy_res1 & op_flag;
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, __u64 op_flag)
141 info->mti_opdata &= ~op_flag;
142 tgt_opdata_clear(info->mti_env, op_flag);
145 rep->lock_policy_res1 &= ~op_flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, __u64 op_flag)
152 info->mti_opdata |= op_flag;
153 tgt_opdata_set(info->mti_env, op_flag);
156 rep->lock_policy_res1 |= op_flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
169 mdt_lock_reg_init(lh, lock->l_req_mode);
170 if (lock->l_req_mode == LCK_GROUP)
171 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
174 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
175 const struct lu_name *lname)
177 lh->mlh_reg_mode = lock_mode;
178 lh->mlh_pdo_mode = LCK_MINMODE;
179 lh->mlh_rreg_mode = lock_mode;
180 lh->mlh_type = MDT_PDO_LOCK;
182 if (lu_name_is_valid(lname)) {
183 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
185 /* XXX Workaround for LU-2856
187 * Zero is a valid return value of full_name_hash, but
188 * several users of mlh_pdo_hash assume a non-zero
189 * hash value. We therefore map zero onto an
190 * arbitrary, but consistent value (1) to avoid
191 * problems further down the road. */
192 if (unlikely(lh->mlh_pdo_hash == 0))
193 lh->mlh_pdo_hash = 1;
195 lh->mlh_pdo_hash = 0;
199 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
200 struct mdt_lock_handle *lh)
206 * Any dir access needs couple of locks:
208 * 1) on part of dir we gonna take lookup/modify;
210 * 2) on whole dir to protect it from concurrent splitting and/or to
211 * flush client's cache for readdir().
213 * so, for a given mode and object this routine decides what lock mode
214 * to use for lock #2:
216 * 1) if caller's gonna lookup in dir then we need to protect dir from
217 * being splitted only - LCK_CR
219 * 2) if caller's gonna modify dir then we need to protect dir from
220 * being splitted and to flush cache - LCK_CW
222 * 3) if caller's gonna modify dir and that dir seems ready for
223 * splitting then we need to protect it from any type of access
224 * (lookup/modify/split) - LCK_EX --bzzz
227 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
228 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
231 * Ask underlaying level its opinion about preferable PDO lock mode
232 * having access type passed as regular lock mode:
234 * - MDL_MINMODE means that lower layer does not want to specify lock
237 * - MDL_NL means that no PDO lock should be taken. This is used in some
238 * cases. Say, for non-splittable directories no need to use PDO locks
241 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
242 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
244 if (mode != MDL_MINMODE) {
245 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
248 * Lower layer does not want to specify locking mode. We do it
249 * our selves. No special protection is needed, just flush
250 * client's cache on modification and allow concurrent
253 switch (lh->mlh_reg_mode) {
255 lh->mlh_pdo_mode = LCK_EX;
258 lh->mlh_pdo_mode = LCK_CR;
261 lh->mlh_pdo_mode = LCK_CW;
264 CERROR("Not expected lock type (0x%x)\n",
265 (int)lh->mlh_reg_mode);
270 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
275 * Check whether \a o is directory stripe object.
277 * \param[in] info thread environment
278 * \param[in] o MDT object
280 * \retval 1 is directory stripe.
281 * \retval 0 isn't directory stripe.
282 * \retval < 1 error code
284 static int mdt_is_dir_stripe(struct mdt_thread_info *info,
285 struct mdt_object *o)
287 struct md_attr *ma = &info->mti_attr;
288 struct lmv_mds_md_v1 *lmv;
291 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
295 if (!(ma->ma_valid & MA_LMV))
298 lmv = &ma->ma_lmv->lmv_md_v1;
300 if (!lmv_is_sane2(lmv))
303 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE)
309 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
312 struct mdt_device *mdt = info->mti_mdt;
313 struct lu_name *lname = &info->mti_name;
314 const char *start = fileset;
315 char *filename = info->mti_filename;
316 struct mdt_object *parent;
320 LASSERT(!info->mti_cross_ref);
323 * We may want to allow this to mount a completely separate
324 * fileset from the MDT in the future, but keeping it to
325 * ROOT/ only for now avoid potential security issues.
327 *fid = mdt->mdt_md_root_fid;
329 while (rc == 0 && start != NULL && *start != '\0') {
330 const char *s1 = start;
336 while (*s2 != '/' && *s2 != '\0')
344 lname->ln_namelen = s2 - s1;
345 if (lname->ln_namelen > NAME_MAX) {
350 /* reject .. as a path component */
351 if (lname->ln_namelen == 2 &&
352 strncmp(s1, "..", 2) == 0) {
357 strncpy(filename, s1, lname->ln_namelen);
358 filename[lname->ln_namelen] = '\0';
359 lname->ln_name = filename;
361 parent = mdt_object_find(info->mti_env, mdt, fid);
362 if (IS_ERR(parent)) {
363 rc = PTR_ERR(parent);
366 /* Only got the fid of this obj by name */
368 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
369 fid, &info->mti_spec);
370 mdt_object_put(info->mti_env, parent);
373 parent = mdt_object_find(info->mti_env, mdt, fid);
375 rc = PTR_ERR(parent);
377 mode = lu_object_attr(&parent->mot_obj);
378 if (!S_ISDIR(mode)) {
380 } else if (mdt_is_remote_object(info, parent, parent)) {
381 if (!mdt->mdt_enable_remote_subdir_mount) {
383 LCONSOLE_WARN("%s: subdir mount '%s' refused because 'enable_remote_subdir_mount=0': rc = %d\n",
387 LCONSOLE_INFO("%s: subdir mount '%s' is remote and may be slow\n",
392 mdt_object_put(info->mti_env, parent);
399 static int mdt_get_root(struct tgt_session_info *tsi)
401 struct mdt_thread_info *info = tsi2mdt_info(tsi);
402 struct mdt_device *mdt = info->mti_mdt;
403 struct mdt_body *repbody;
404 char *fileset = NULL, *buffer = NULL;
406 struct obd_export *exp = info->mti_exp;
407 char *nodemap_fileset;
411 rc = mdt_check_ucred(info);
413 GOTO(out, rc = err_serious(rc));
415 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
416 GOTO(out, rc = err_serious(-ENOMEM));
418 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
419 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
420 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
422 GOTO(out, rc = err_serious(-EFAULT));
425 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
426 if (nodemap_fileset && nodemap_fileset[0]) {
427 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
429 /* consider fileset from client as a sub-fileset
430 * of the nodemap one */
431 OBD_ALLOC(buffer, PATH_MAX + 1);
433 GOTO(out, rc = err_serious(-ENOMEM));
434 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
435 nodemap_fileset, fileset) >= PATH_MAX + 1)
436 GOTO(out, rc = err_serious(-EINVAL));
439 /* enforce fileset as specified in the nodemap */
440 fileset = nodemap_fileset;
445 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
446 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
448 GOTO(out, rc = err_serious(rc));
450 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
452 repbody->mbo_valid |= OBD_MD_FLID;
456 mdt_thread_info_fini(info);
458 OBD_FREE(buffer, PATH_MAX+1);
462 static int mdt_statfs(struct tgt_session_info *tsi)
464 struct ptlrpc_request *req = tgt_ses_req(tsi);
465 struct mdt_thread_info *info = tsi2mdt_info(tsi);
466 struct mdt_device *mdt = info->mti_mdt;
467 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
468 struct md_device *next = mdt->mdt_child;
469 struct ptlrpc_service_part *svcpt;
470 struct obd_statfs *osfs;
471 struct mdt_body *reqbody = NULL;
472 struct mdt_statfs_cache *msf;
473 ktime_t kstart = ktime_get();
474 int current_blockbits;
479 svcpt = req->rq_rqbd->rqbd_svcpt;
481 /* This will trigger a watchdog timeout */
482 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
483 (MDT_SERVICE_WATCHDOG_FACTOR *
484 at_get(&svcpt->scp_at_estimate)) + 1);
486 rc = mdt_check_ucred(info);
488 GOTO(out, rc = err_serious(rc));
490 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
491 GOTO(out, rc = err_serious(-ENOMEM));
493 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
495 GOTO(out, rc = -EPROTO);
497 if (mdt_is_sum_statfs_client(req->rq_export) &&
498 lustre_packed_msg_size(req->rq_reqmsg) ==
499 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
500 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
501 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
502 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
505 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
506 msf = &mdt->mdt_sum_osfs;
508 msf = &mdt->mdt_osfs;
510 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
511 /** statfs data is too old, get up-to-date one */
512 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
513 rc = next->md_ops->mdo_statfs(info->mti_env,
516 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
520 spin_lock(&mdt->mdt_lock);
521 msf->msf_osfs = *osfs;
522 msf->msf_age = ktime_get_seconds();
523 spin_unlock(&mdt->mdt_lock);
525 /** use cached statfs data */
526 spin_lock(&mdt->mdt_lock);
527 *osfs = msf->msf_osfs;
528 spin_unlock(&mdt->mdt_lock);
531 /* tgd_blockbit is recordsize bits set during mkfs.
532 * This once set does not change. However, 'zfs set'
533 * can be used to change the MDT blocksize. Instead
534 * of using cached value of 'tgd_blockbit' always
535 * calculate the blocksize bits which may have
538 current_blockbits = fls64(osfs->os_bsize) - 1;
540 /* at least try to account for cached pages. its still racy and
541 * might be under-reporting if clients haven't announced their
542 * caches with brw recently */
543 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
544 " pending %llu free %llu avail %llu\n",
545 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
546 tgd->tgd_tot_pending,
547 osfs->os_bfree << current_blockbits,
548 osfs->os_bavail << current_blockbits);
550 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
551 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
552 osfs->os_bsize - 1) >> current_blockbits));
554 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
555 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
556 "%llu objects: %llu free; state %x\n",
557 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
558 osfs->os_files, osfs->os_ffree, osfs->os_state);
560 if (!exp_grant_param_supp(tsi->tsi_exp) &&
561 current_blockbits > COMPAT_BSIZE_SHIFT) {
562 /* clients which don't support OBD_CONNECT_GRANT_PARAM
563 * should not see a block size > page size, otherwise
564 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
565 * block size which is the biggest block size known to work
566 * with all client's page size. */
567 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
568 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
569 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
570 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
573 mdt_counter_incr(req, LPROC_MDT_STATFS,
574 ktime_us_delta(ktime_get(), kstart));
576 mdt_thread_info_fini(info);
580 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
582 struct lov_comp_md_v1 *comp_v1;
583 struct lov_mds_md *v1;
585 __u32 dom_stripesize = 0;
587 bool has_ost_stripes = false;
594 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
597 comp_v1 = (struct lov_comp_md_v1 *)lmm;
598 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
599 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
601 /* Fast check for DoM entry with no mirroring, should be the first */
602 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
603 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
606 /* check all entries otherwise */
607 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
608 struct lov_comp_md_entry_v1 *lcme;
610 lcme = &comp_v1->lcm_entries[i];
611 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
614 off = le32_to_cpu(lcme->lcme_offset);
615 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
617 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
619 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
621 has_ost_stripes = true;
623 if (dom_stripesize && has_ost_stripes)
624 RETURN(dom_stripesize);
626 /* DoM-only case exits here */
627 if (is_dom_only && dom_stripesize)
629 RETURN(dom_stripesize);
633 * Pack size attributes into the reply.
635 int mdt_pack_size2body(struct mdt_thread_info *info,
636 const struct lu_fid *fid, struct lustre_handle *lh)
639 struct md_attr *ma = &info->mti_attr;
641 bool dom_lock = false;
645 LASSERT(ma->ma_attr.la_valid & LA_MODE);
647 if (!S_ISREG(ma->ma_attr.la_mode) ||
648 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
651 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
652 /* no DoM stripe, no size in reply */
656 if (lustre_handle_is_used(lh)) {
657 struct ldlm_lock *lock;
659 lock = ldlm_handle2lock(lh);
661 dom_lock = ldlm_has_dom(lock);
666 /* no DoM lock, no size in reply */
670 /* Either DoM lock exists or LMM has only DoM stripe then
671 * return size on body. */
672 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
674 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
678 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
680 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
682 * \param info thread info object
683 * \param repbody reply to pack ACLs into
684 * \param o mdt object of file to examine
685 * \param nodemap nodemap of client to reply to
687 * \retval -errno error getting or parsing ACL from disk
689 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
690 struct mdt_object *o, struct lu_nodemap *nodemap)
692 const struct lu_env *env = info->mti_env;
693 struct md_object *next = mdt_object_child(o);
694 struct lu_buf *buf = &info->mti_buf;
695 struct mdt_device *mdt = info->mti_mdt;
696 struct req_capsule *pill = info->mti_pill;
701 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
702 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
703 if (buf->lb_len == 0)
706 LASSERT(!info->mti_big_acl_used);
708 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
710 if (rc == -ENODATA) {
711 repbody->mbo_aclsize = 0;
712 repbody->mbo_valid |= OBD_MD_FLACL;
714 } else if (rc == -EOPNOTSUPP) {
716 } else if (rc == -ERANGE) {
717 if (exp_connect_large_acl(info->mti_exp) &&
718 !info->mti_big_acl_used) {
719 if (info->mti_big_acl == NULL) {
720 info->mti_big_aclsize =
722 mdt->mdt_max_ea_size,
724 OBD_ALLOC_LARGE(info->mti_big_acl,
725 info->mti_big_aclsize);
726 if (info->mti_big_acl == NULL) {
727 info->mti_big_aclsize = 0;
728 CERROR("%s: unable to grow "
731 PFID(mdt_object_fid(o)));
736 CDEBUG(D_INODE, "%s: grow the "DFID
737 " ACL buffer to size %d\n",
739 PFID(mdt_object_fid(o)),
740 info->mti_big_aclsize);
742 buf->lb_buf = info->mti_big_acl;
743 buf->lb_len = info->mti_big_aclsize;
744 info->mti_big_acl_used = 1;
747 /* FS has ACL bigger that our limits */
748 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
749 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
750 info->mti_big_aclsize);
753 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
754 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
757 rc = nodemap_map_acl(nodemap, buf->lb_buf,
758 rc, NODEMAP_FS_TO_CLIENT);
759 /* if all ACLs mapped out, rc is still >= 0 */
761 CERROR("%s: nodemap_map_acl unable to parse "DFID
762 " ACL: rc = %d\n", mdt_obd_name(mdt),
763 PFID(mdt_object_fid(o)), rc);
764 repbody->mbo_aclsize = 0;
765 repbody->mbo_valid &= ~OBD_MD_FLACL;
767 repbody->mbo_aclsize = rc;
768 repbody->mbo_valid |= OBD_MD_FLACL;
777 /* XXX Look into layout in MDT layer. */
778 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
780 struct lov_comp_md_v1 *comp_v1;
781 struct lov_mds_md *v1;
784 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
785 comp_v1 = (struct lov_comp_md_v1 *)lmm;
787 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
788 v1 = (struct lov_mds_md *)((char *)comp_v1 +
789 comp_v1->lcm_entries[i].lcme_offset);
790 /* We don't support partial release for now */
791 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
796 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
801 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
802 const struct lu_attr *attr, const struct lu_fid *fid)
804 struct md_attr *ma = &info->mti_attr;
805 struct obd_export *exp = info->mti_exp;
806 struct lu_nodemap *nodemap = NULL;
808 LASSERT(ma->ma_valid & MA_INODE);
810 if (attr->la_valid & LA_ATIME) {
811 b->mbo_atime = attr->la_atime;
812 b->mbo_valid |= OBD_MD_FLATIME;
814 if (attr->la_valid & LA_MTIME) {
815 b->mbo_mtime = attr->la_mtime;
816 b->mbo_valid |= OBD_MD_FLMTIME;
818 if (attr->la_valid & LA_CTIME) {
819 b->mbo_ctime = attr->la_ctime;
820 b->mbo_valid |= OBD_MD_FLCTIME;
822 if (attr->la_valid & LA_BTIME) {
823 b->mbo_btime = attr->la_btime;
824 b->mbo_valid |= OBD_MD_FLBTIME;
826 if (attr->la_valid & LA_FLAGS) {
827 b->mbo_flags = attr->la_flags;
828 b->mbo_valid |= OBD_MD_FLFLAGS;
830 if (attr->la_valid & LA_NLINK) {
831 b->mbo_nlink = attr->la_nlink;
832 b->mbo_valid |= OBD_MD_FLNLINK;
834 if (attr->la_valid & (LA_UID|LA_GID|LA_PROJID)) {
835 nodemap = nodemap_get_from_exp(exp);
839 if (attr->la_valid & LA_UID) {
840 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
841 NODEMAP_FS_TO_CLIENT,
843 b->mbo_valid |= OBD_MD_FLUID;
845 if (attr->la_valid & LA_GID) {
846 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
847 NODEMAP_FS_TO_CLIENT,
849 b->mbo_valid |= OBD_MD_FLGID;
852 if (attr->la_valid & LA_PROJID) {
853 b->mbo_projid = nodemap_map_id(nodemap, NODEMAP_PROJID,
854 NODEMAP_FS_TO_CLIENT,
856 b->mbo_valid |= OBD_MD_FLPROJID;
859 b->mbo_mode = attr->la_mode;
860 if (attr->la_valid & LA_MODE)
861 b->mbo_valid |= OBD_MD_FLMODE;
862 if (attr->la_valid & LA_TYPE)
863 b->mbo_valid |= OBD_MD_FLTYPE;
867 b->mbo_valid |= OBD_MD_FLID;
868 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
869 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
872 if (!(attr->la_valid & LA_TYPE))
875 b->mbo_rdev = attr->la_rdev;
876 b->mbo_size = attr->la_size;
877 b->mbo_blocks = attr->la_blocks;
879 if (!S_ISREG(attr->la_mode)) {
880 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
881 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
882 /* means no objects are allocated on osts. */
883 LASSERT(!(ma->ma_valid & MA_LOV));
884 /* just ignore blocks occupied by extend attributes on MDS */
886 /* if no object is allocated on osts, the size on mds is valid.
888 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
889 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
890 if (mdt_hsm_is_released(ma->ma_lmm)) {
891 /* A released file stores its size on MDS. */
892 /* But return 1 block for released file, unless tools
893 * like tar will consider it fully sparse. (LU-3864)
895 if (unlikely(b->mbo_size == 0))
899 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
900 } else if (info->mti_som_valid) { /* som is valid */
901 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
902 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
903 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
904 b->mbo_size = ma->ma_som.ms_size;
905 b->mbo_blocks = ma->ma_som.ms_blocks;
909 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
910 b->mbo_valid & OBD_MD_FLLAZYSIZE))
911 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
912 PFID(fid), (unsigned long long)b->mbo_size);
915 if (!IS_ERR_OR_NULL(nodemap))
916 nodemap_putref(nodemap);
919 static inline int mdt_body_has_lov(const struct lu_attr *la,
920 const struct mdt_body *body)
922 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
923 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
926 void mdt_client_compatibility(struct mdt_thread_info *info)
928 struct mdt_body *body;
929 struct ptlrpc_request *req = mdt_info_req(info);
930 struct obd_export *exp = req->rq_export;
931 struct md_attr *ma = &info->mti_attr;
932 struct lu_attr *la = &ma->ma_attr;
935 if (exp_connect_layout(exp))
936 /* the client can deal with 16-bit lmm_stripe_count */
939 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
941 if (!mdt_body_has_lov(la, body))
944 /* now we have a reply with a lov for a client not compatible with the
945 * layout lock so we have to clean the layout generation number */
946 if (S_ISREG(la->la_mode))
947 ma->ma_lmm->lmm_layout_gen = 0;
951 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
952 struct mdt_object *o)
954 const struct lu_env *env = info->mti_env;
957 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
966 /* Is it a directory? Let's check for the LMV as well */
967 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
968 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
972 rc2 = mo_xattr_get(env, mdt_object_child(o),
974 XATTR_NAME_DEFAULT_LMV);
976 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
984 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
987 const struct lu_env *env = info->mti_env;
991 LASSERT(info->mti_big_lmm_used == 0);
992 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
996 /* big_lmm may need to be grown */
997 if (info->mti_big_lmmsize < rc) {
998 int size = size_roundup_power2(rc);
1000 if (info->mti_big_lmmsize > 0) {
1001 /* free old buffer */
1002 LASSERT(info->mti_big_lmm);
1003 OBD_FREE_LARGE(info->mti_big_lmm,
1004 info->mti_big_lmmsize);
1005 info->mti_big_lmm = NULL;
1006 info->mti_big_lmmsize = 0;
1009 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
1010 if (info->mti_big_lmm == NULL)
1012 info->mti_big_lmmsize = size;
1014 LASSERT(info->mti_big_lmmsize >= rc);
1016 info->mti_buf.lb_buf = info->mti_big_lmm;
1017 info->mti_buf.lb_len = info->mti_big_lmmsize;
1018 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
1023 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1024 struct md_attr *ma, const char *name)
1026 struct md_object *next = mdt_object_child(o);
1027 struct lu_buf *buf = &info->mti_buf;
1030 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1031 buf->lb_buf = ma->ma_lmm;
1032 buf->lb_len = ma->ma_lmm_size;
1033 LASSERT(!(ma->ma_valid & MA_LOV));
1034 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1035 buf->lb_buf = ma->ma_lmv;
1036 buf->lb_len = ma->ma_lmv_size;
1037 LASSERT(!(ma->ma_valid & MA_LMV));
1038 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1039 buf->lb_buf = ma->ma_default_lmv;
1040 buf->lb_len = ma->ma_default_lmv_size;
1041 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1046 LASSERT(buf->lb_buf);
1048 rc = mo_xattr_get(info->mti_env, next, buf, name);
1052 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1053 if (info->mti_big_lmm_used)
1054 ma->ma_lmm = info->mti_big_lmm;
1056 /* NOT return LOV EA with hole to old client. */
1057 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1058 LOV_PATTERN_F_HOLE) &&
1059 !(exp_connect_flags(info->mti_exp) &
1060 OBD_CONNECT_LFSCK)) {
1063 ma->ma_lmm_size = rc;
1064 ma->ma_valid |= MA_LOV;
1066 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1067 if (info->mti_big_lmm_used)
1068 ma->ma_lmv = info->mti_big_lmm;
1070 ma->ma_lmv_size = rc;
1071 ma->ma_valid |= MA_LMV;
1072 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1073 ma->ma_default_lmv_size = rc;
1074 ma->ma_valid |= MA_LMV_DEF;
1077 /* Update mdt_max_mdsize so all clients will be aware that */
1078 if (info->mti_mdt->mdt_max_mdsize < rc)
1079 info->mti_mdt->mdt_max_mdsize = rc;
1082 } else if (rc == -ENODATA) {
1085 } else if (rc == -ERANGE) {
1086 /* Default LMV has fixed size, so it must be able to fit
1087 * in the original buffer */
1088 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1090 rc = mdt_big_xattr_get(info, o, name);
1092 info->mti_big_lmm_used = 1;
1100 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1101 struct md_attr *ma, const char *name)
1105 if (!info->mti_big_lmm) {
1106 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1107 if (!info->mti_big_lmm)
1109 info->mti_big_lmmsize = PAGE_SIZE;
1112 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1113 ma->ma_lmm = info->mti_big_lmm;
1114 ma->ma_lmm_size = info->mti_big_lmmsize;
1115 ma->ma_valid &= ~MA_LOV;
1116 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1117 ma->ma_lmv = info->mti_big_lmm;
1118 ma->ma_lmv_size = info->mti_big_lmmsize;
1119 ma->ma_valid &= ~MA_LMV;
1124 LASSERT(!info->mti_big_lmm_used);
1125 rc = __mdt_stripe_get(info, o, ma, name);
1126 /* since big_lmm is always used here, clear 'used' flag to avoid
1127 * assertion in mdt_big_xattr_get().
1129 info->mti_big_lmm_used = 0;
1134 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1135 struct lu_fid *pfid)
1137 struct lu_buf *buf = &info->mti_buf;
1138 struct link_ea_header *leh;
1139 struct link_ea_entry *lee;
1143 buf->lb_buf = info->mti_big_lmm;
1144 buf->lb_len = info->mti_big_lmmsize;
1145 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1146 buf, XATTR_NAME_LINK);
1147 /* ignore errors, MA_PFID won't be set and it is
1148 * up to the caller to treat this as an error */
1149 if (rc == -ERANGE || buf->lb_len == 0) {
1150 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1151 buf->lb_buf = info->mti_big_lmm;
1152 buf->lb_len = info->mti_big_lmmsize;
1157 if (rc < sizeof(*leh)) {
1158 CERROR("short LinkEA on "DFID": rc = %d\n",
1159 PFID(mdt_object_fid(o)), rc);
1163 leh = (struct link_ea_header *) buf->lb_buf;
1164 lee = (struct link_ea_entry *)(leh + 1);
1165 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1166 leh->leh_magic = LINK_EA_MAGIC;
1167 leh->leh_reccount = __swab32(leh->leh_reccount);
1168 leh->leh_len = __swab64(leh->leh_len);
1170 if (leh->leh_magic != LINK_EA_MAGIC)
1172 if (leh->leh_reccount == 0)
1175 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1176 fid_be_to_cpu(pfid, pfid);
1181 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1182 struct lu_fid *pfid, struct lu_name *lname)
1184 struct lu_buf *buf = &info->mti_buf;
1185 struct link_ea_header *leh;
1186 struct link_ea_entry *lee;
1190 buf->lb_buf = info->mti_xattr_buf;
1191 buf->lb_len = sizeof(info->mti_xattr_buf);
1192 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1194 if (rc == -ERANGE) {
1195 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1196 buf->lb_buf = info->mti_big_lmm;
1197 buf->lb_len = info->mti_big_lmmsize;
1202 if (rc < sizeof(*leh)) {
1203 CERROR("short LinkEA on "DFID": rc = %d\n",
1204 PFID(mdt_object_fid(o)), rc);
1208 leh = (struct link_ea_header *)buf->lb_buf;
1209 lee = (struct link_ea_entry *)(leh + 1);
1210 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1211 leh->leh_magic = LINK_EA_MAGIC;
1212 leh->leh_reccount = __swab32(leh->leh_reccount);
1213 leh->leh_len = __swab64(leh->leh_len);
1215 if (leh->leh_magic != LINK_EA_MAGIC)
1218 if (leh->leh_reccount == 0)
1221 linkea_entry_unpack(lee, &reclen, lname, pfid);
1226 int mdt_attr_get_complex(struct mdt_thread_info *info,
1227 struct mdt_object *o, struct md_attr *ma)
1229 const struct lu_env *env = info->mti_env;
1230 struct md_object *next = mdt_object_child(o);
1231 struct lu_buf *buf = &info->mti_buf;
1232 int need = ma->ma_need;
1239 if (mdt_object_exists(o) == 0)
1240 GOTO(out, rc = -ENOENT);
1241 mode = lu_object_attr(&next->mo_lu);
1243 if (need & MA_INODE) {
1244 ma->ma_need = MA_INODE;
1245 rc = mo_attr_get(env, next, ma);
1250 (void) mdt_get_som(info, o, ma);
1251 ma->ma_valid |= MA_INODE;
1254 if (need & MA_PFID) {
1255 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1257 ma->ma_valid |= MA_PFID;
1258 /* ignore this error, parent fid is not mandatory */
1262 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1263 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1268 if (need & MA_LMV && S_ISDIR(mode)) {
1269 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1274 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1275 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1281 * In the handle of MA_INODE, we may already get the SOM attr.
1283 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1284 rc = mdt_get_som(info, o, ma);
1289 if (need & MA_HSM && S_ISREG(mode)) {
1290 buf->lb_buf = info->mti_xattr_buf;
1291 buf->lb_len = sizeof(info->mti_xattr_buf);
1292 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1293 sizeof(info->mti_xattr_buf));
1294 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1295 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1297 ma->ma_valid |= MA_HSM;
1298 else if (rc2 < 0 && rc2 != -ENODATA)
1299 GOTO(out, rc = rc2);
1302 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1303 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1304 buf->lb_buf = ma->ma_acl;
1305 buf->lb_len = ma->ma_acl_size;
1306 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1308 ma->ma_acl_size = rc2;
1309 ma->ma_valid |= MA_ACL_DEF;
1310 } else if (rc2 == -ENODATA) {
1312 ma->ma_acl_size = 0;
1314 GOTO(out, rc = rc2);
1319 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1320 rc, ma->ma_valid, ma->ma_lmm);
1324 static int mdt_getattr_internal(struct mdt_thread_info *info,
1325 struct mdt_object *o, int ma_need)
1327 struct mdt_device *mdt = info->mti_mdt;
1328 struct md_object *next = mdt_object_child(o);
1329 const struct mdt_body *reqbody = info->mti_body;
1330 struct ptlrpc_request *req = mdt_info_req(info);
1331 struct md_attr *ma = &info->mti_attr;
1332 struct lu_attr *la = &ma->ma_attr;
1333 struct req_capsule *pill = info->mti_pill;
1334 const struct lu_env *env = info->mti_env;
1335 struct mdt_body *repbody;
1336 struct lu_buf *buffer = &info->mti_buf;
1337 struct obd_export *exp = info->mti_exp;
1338 ktime_t kstart = ktime_get();
1343 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1344 RETURN(err_serious(-ENOMEM));
1346 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1350 if (mdt_object_remote(o)) {
1351 /* This object is located on remote node.*/
1352 /* Return -ENOTSUPP for old client */
1353 if (!mdt_is_dne_client(req->rq_export))
1354 GOTO(out, rc = -ENOTSUPP);
1356 repbody->mbo_fid1 = *mdt_object_fid(o);
1357 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1361 if (reqbody->mbo_eadatasize > 0) {
1362 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1363 if (buffer->lb_buf == NULL)
1364 GOTO(out, rc = -EPROTO);
1365 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1368 buffer->lb_buf = NULL;
1370 ma_need &= ~(MA_LOV | MA_LMV);
1371 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1372 mdt_obd_name(info->mti_mdt),
1373 req->rq_export->exp_client_uuid.uuid);
1376 /* from 2.12.58 intent_getattr pack default LMV in reply */
1377 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1378 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1379 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1380 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1382 ma->ma_lmv = buffer->lb_buf;
1383 ma->ma_lmv_size = buffer->lb_len;
1384 ma->ma_default_lmv = req_capsule_server_get(pill,
1385 &RMF_DEFAULT_MDT_MD);
1386 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1387 &RMF_DEFAULT_MDT_MD,
1389 ma->ma_need = MA_INODE;
1390 if (ma->ma_lmv_size > 0)
1391 ma->ma_need |= MA_LMV;
1392 if (ma->ma_default_lmv_size > 0)
1393 ma->ma_need |= MA_LMV_DEF;
1394 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1395 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1396 /* If it is dir and client require MEA, then we got MEA */
1397 /* Assumption: MDT_MD size is enough for lmv size. */
1398 ma->ma_lmv = buffer->lb_buf;
1399 ma->ma_lmv_size = buffer->lb_len;
1400 ma->ma_need = MA_INODE;
1401 if (ma->ma_lmv_size > 0) {
1402 if (reqbody->mbo_valid & OBD_MD_MEA) {
1403 ma->ma_need |= MA_LMV;
1404 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1405 ma->ma_need |= MA_LMV_DEF;
1406 ma->ma_default_lmv = buffer->lb_buf;
1408 ma->ma_default_lmv_size = buffer->lb_len;
1409 ma->ma_lmv_size = 0;
1413 ma->ma_lmm = buffer->lb_buf;
1414 ma->ma_lmm_size = buffer->lb_len;
1415 ma->ma_need = MA_INODE | MA_HSM;
1416 if (ma->ma_lmm_size > 0) {
1417 ma->ma_need |= MA_LOV;
1418 /* Older clients may crash if they getattr overstriped
1421 if (!exp_connect_overstriping(exp) &&
1422 mdt_lmm_is_overstriping(ma->ma_lmm))
1423 RETURN(-EOPNOTSUPP);
1427 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1428 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1429 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1430 /* get default stripe info for this dir. */
1431 ma->ma_need |= MA_LOV_DEF;
1433 ma->ma_need |= ma_need;
1435 rc = mdt_attr_get_complex(info, o, ma);
1437 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1438 "%s: getattr error for "DFID": rc = %d\n",
1439 mdt_obd_name(info->mti_mdt),
1440 PFID(mdt_object_fid(o)), rc);
1444 /* if file is released, check if a restore is running */
1445 if (ma->ma_valid & MA_HSM) {
1446 repbody->mbo_valid |= OBD_MD_TSTATE;
1447 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1448 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1449 repbody->mbo_t_state = MS_RESTORE;
1452 if (unlikely(!(ma->ma_valid & MA_INODE)))
1455 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1457 if (mdt_body_has_lov(la, reqbody)) {
1458 u32 stripe_count = 1;
1459 bool fixed_layout = false;
1461 if (ma->ma_valid & MA_LOV) {
1462 LASSERT(ma->ma_lmm_size);
1463 repbody->mbo_eadatasize = ma->ma_lmm_size;
1464 if (S_ISDIR(la->la_mode))
1465 repbody->mbo_valid |= OBD_MD_FLDIREA;
1467 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1468 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1470 if (ma->ma_valid & MA_LMV) {
1471 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1472 u32 magic = le32_to_cpu(lmv->lmv_magic);
1474 /* Return -ENOTSUPP for old client */
1475 if (!mdt_is_striped_client(req->rq_export))
1478 LASSERT(S_ISDIR(la->la_mode));
1479 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1480 repbody->mbo_eadatasize = ma->ma_lmv_size;
1481 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1483 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1484 fixed_layout = lmv_is_fixed(lmv);
1485 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1486 mdt_restripe_migrate_add(info, o);
1487 else if (magic == LMV_MAGIC_V1 &&
1488 lmv_is_restriping(lmv))
1489 mdt_restripe_update_add(info, o);
1491 if (ma->ma_valid & MA_LMV_DEF) {
1492 /* Return -ENOTSUPP for old client */
1493 if (!mdt_is_striped_client(req->rq_export))
1495 LASSERT(S_ISDIR(la->la_mode));
1497 * when ll_dir_getstripe() gets default LMV, it
1498 * checks mbo_eadatasize.
1500 if (!(ma->ma_valid & MA_LMV))
1501 repbody->mbo_eadatasize =
1502 ma->ma_default_lmv_size;
1503 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1504 OBD_MD_DEFAULT_MEA);
1507 "dirent count %llu stripe count %u MDT count %d\n",
1508 ma->ma_attr.la_dirent_count, stripe_count,
1509 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1510 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1511 ma->ma_attr.la_dirent_count >
1512 mdt->mdt_restriper.mdr_dir_split_count &&
1513 !fid_is_root(mdt_object_fid(o)) &&
1514 mdt->mdt_enable_dir_auto_split &&
1515 !o->mot_restriping &&
1516 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1 &&
1518 mdt_auto_split_add(info, o);
1519 } else if (S_ISLNK(la->la_mode) &&
1520 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1521 buffer->lb_buf = ma->ma_lmm;
1522 /* eadatasize from client includes NULL-terminator, so
1523 * there is no need to read it */
1524 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1525 rc = mo_readlink(env, next, buffer);
1526 if (unlikely(rc <= 0)) {
1527 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1528 mdt_obd_name(info->mti_mdt),
1529 PFID(mdt_object_fid(o)), rc);
1532 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1534 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1536 repbody->mbo_valid |= OBD_MD_LINKNAME;
1537 /* we need to report back size with NULL-terminator
1538 * because client expects that */
1539 repbody->mbo_eadatasize = rc + 1;
1540 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1541 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1542 "on "DFID ", expected %d\n",
1543 mdt_obd_name(info->mti_mdt),
1544 rc, PFID(mdt_object_fid(o)),
1545 reqbody->mbo_eadatasize - 1);
1546 /* NULL terminate */
1547 ((char *)ma->ma_lmm)[rc] = 0;
1549 /* If the total CDEBUG() size is larger than a page, it
1550 * will print a warning to the console, avoid this by
1551 * printing just the last part of the symlink. */
1552 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1553 print_limit < rc ? "..." : "", print_limit,
1554 (char *)ma->ma_lmm + rc - print_limit, rc);
1559 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1560 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1561 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1562 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1563 repbody->mbo_max_mdsize);
1566 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1567 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1568 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1569 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1570 if (IS_ERR(nodemap))
1571 RETURN(PTR_ERR(nodemap));
1573 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1574 nodemap_putref(nodemap);
1580 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1581 ktime_us_delta(ktime_get(), kstart));
1586 static int mdt_getattr(struct tgt_session_info *tsi)
1588 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1589 struct mdt_object *obj = info->mti_object;
1590 struct req_capsule *pill = info->mti_pill;
1591 struct mdt_body *reqbody;
1592 struct mdt_body *repbody;
1596 if (unlikely(info->mti_object == NULL))
1599 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1601 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1603 /* Special case for Data-on-MDT files to get data version */
1604 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1605 rc = mdt_data_version_get(tsi);
1609 /* Unlike intent case where we need to pre-fill out buffers early on
1610 * in intent policy for ldlm reasons, here we can have a much better
1611 * guess at EA size by just reading it from disk.
1612 * Exceptions are readdir and (missing) directory striping */
1614 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1615 /* No easy way to know how long is the symlink, but it cannot
1616 * be more than PATH_MAX, so we allocate +1 */
1618 /* A special case for fs ROOT: getattr there might fetch
1619 * default EA for entire fs, not just for this dir!
1621 } else if (lu_fid_eq(mdt_object_fid(obj),
1622 &info->mti_mdt->mdt_md_root_fid) &&
1623 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1624 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1626 /* Should the default strping be bigger, mdt_fix_reply
1627 * will reallocate */
1628 rc = DEF_REP_MD_SIZE;
1630 /* Read the actual EA size from disk */
1631 rc = mdt_attr_get_eabuf_size(info, obj);
1635 GOTO(out, rc = err_serious(rc));
1637 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1639 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1640 * by default. If the target object has more ACL entries, then
1641 * enlarge the buffer when necessary. */
1642 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1643 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1645 rc = req_capsule_server_pack(pill);
1646 if (unlikely(rc != 0))
1647 GOTO(out, rc = err_serious(rc));
1649 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1650 LASSERT(repbody != NULL);
1651 repbody->mbo_eadatasize = 0;
1652 repbody->mbo_aclsize = 0;
1654 rc = mdt_check_ucred(info);
1656 GOTO(out_shrink, rc);
1658 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1660 rc = mdt_getattr_internal(info, obj, 0);
1663 mdt_client_compatibility(info);
1664 rc2 = mdt_fix_reply(info);
1668 mdt_thread_info_fini(info);
1673 * Handler of layout intent RPC requiring the layout modification
1675 * \param[in] info thread environment
1676 * \param[in] obj object
1677 * \param[out] lhc object ldlm lock handle
1678 * \param[in] layout layout change descriptor
1680 * \retval 0 on success
1681 * \retval < 0 error code
1683 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1684 struct mdt_lock_handle *lhc,
1685 struct md_layout_change *layout)
1691 if (!mdt_object_exists(obj))
1694 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1697 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1702 rc = mdt_check_resent_lock(info, obj, lhc);
1708 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1710 /* take layout lock to prepare layout change */
1711 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1712 lockpart |= MDS_INODELOCK_UPDATE;
1714 mdt_lock_handle_init(lhc);
1715 mdt_lock_reg_init(lhc, LCK_EX);
1716 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1721 mutex_lock(&obj->mot_som_mutex);
1722 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1723 mutex_unlock(&obj->mot_som_mutex);
1726 mdt_object_unlock(info, obj, lhc, 1);
1732 * Exchange MOF_LOV_CREATED flags between two objects after a
1733 * layout swap. No assumption is made on whether o1 or o2 have
1734 * created objects or not.
1736 * \param[in,out] o1 First swap layout object
1737 * \param[in,out] o2 Second swap layout object
1739 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1741 unsigned int o1_lov_created = o1->mot_lov_created;
1743 mutex_lock(&o1->mot_lov_mutex);
1744 mutex_lock(&o2->mot_lov_mutex);
1746 o1->mot_lov_created = o2->mot_lov_created;
1747 o2->mot_lov_created = o1_lov_created;
1749 mutex_unlock(&o2->mot_lov_mutex);
1750 mutex_unlock(&o1->mot_lov_mutex);
1753 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1755 struct mdt_thread_info *info;
1756 struct ptlrpc_request *req = tgt_ses_req(tsi);
1757 struct obd_export *exp = req->rq_export;
1758 struct mdt_object *o1, *o2, *o;
1759 struct mdt_lock_handle *lh1, *lh2;
1760 struct mdc_swap_layouts *msl;
1764 /* client does not support layout lock, so layout swaping
1766 * FIXME: there is a problem for old clients which don't support
1767 * layout lock yet. If those clients have already opened the file
1768 * they won't be notified at all so that old layout may still be
1769 * used to do IO. This can be fixed after file release is landed by
1770 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1771 if (!exp_connect_layout(exp))
1772 RETURN(-EOPNOTSUPP);
1774 info = tsi2mdt_info(tsi);
1775 if (unlikely(info->mti_object == NULL))
1778 if (info->mti_dlm_req != NULL)
1779 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1781 o1 = info->mti_object;
1782 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1783 &info->mti_body->mbo_fid2);
1785 GOTO(out, rc = PTR_ERR(o));
1787 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1788 GOTO(put, rc = -ENOENT);
1790 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1791 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1797 /* permission check. Make sure the calling process having permission
1798 * to write both files. */
1799 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1804 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1809 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1811 GOTO(put, rc = -EPROTO);
1813 lh1 = &info->mti_lh[MDT_LH_NEW];
1814 mdt_lock_reg_init(lh1, LCK_EX);
1815 lh2 = &info->mti_lh[MDT_LH_OLD];
1816 mdt_lock_reg_init(lh2, LCK_EX);
1818 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1819 MDS_INODELOCK_XATTR);
1823 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1824 MDS_INODELOCK_XATTR);
1828 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1829 mdt_object_child(o2), msl->msl_flags);
1833 mdt_swap_lov_flag(o1, o2);
1836 mdt_object_unlock(info, o2, lh2, rc);
1838 mdt_object_unlock(info, o1, lh1, rc);
1840 mdt_object_put(info->mti_env, o);
1842 mdt_thread_info_fini(info);
1846 static int mdt_raw_lookup(struct mdt_thread_info *info,
1847 struct mdt_object *parent,
1848 const struct lu_name *lname)
1850 struct lu_fid *fid = &info->mti_tmp_fid1;
1851 struct mdt_body *repbody;
1852 bool is_dotdot = false;
1853 bool is_old_parent_stripe = false;
1854 bool is_new_parent_checked = false;
1859 LASSERT(!info->mti_cross_ref);
1860 /* Always allow to lookup ".." */
1861 if (lname->ln_namelen == 2 &&
1862 lname->ln_name[0] == '.' && lname->ln_name[1] == '.') {
1863 info->mti_spec.sp_permitted = 1;
1865 if (mdt_is_dir_stripe(info, parent) == 1)
1866 is_old_parent_stripe = true;
1869 mdt_object_get(info->mti_env, parent);
1871 /* Only got the fid of this obj by name */
1873 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname, fid,
1875 mdt_object_put(info->mti_env, parent);
1879 /* getattr_name("..") should return master object FID for striped dir */
1880 if (is_dotdot && (is_old_parent_stripe || !is_new_parent_checked)) {
1881 parent = mdt_object_find(info->mti_env, info->mti_mdt, fid);
1883 RETURN(PTR_ERR(parent));
1885 /* old client getattr_name("..") with stripe FID */
1886 if (unlikely(is_old_parent_stripe)) {
1887 is_old_parent_stripe = false;
1891 /* ".." may be a stripe */
1892 if (unlikely(mdt_is_dir_stripe(info, parent) == 1)) {
1893 is_new_parent_checked = true;
1897 mdt_object_put(info->mti_env, parent);
1900 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1901 repbody->mbo_fid1 = *fid;
1902 repbody->mbo_valid = OBD_MD_FLID;
1908 * Find name matching hash
1910 * We search \a child LinkEA for a name whose hash matches \a lname
1911 * (it contains an encoded hash).
1913 * \param info mdt thread info
1914 * \param lname encoded hash to find
1915 * \param parent parent object
1916 * \param child object to search with LinkEA
1917 * \param force_check true to check hash even if LinkEA has only one entry
1919 * \retval 1 match found
1920 * \retval 0 no match found
1921 * \retval -ev negative errno upon error
1923 int find_name_matching_hash(struct mdt_thread_info *info, struct lu_name *lname,
1924 struct mdt_object *parent, struct mdt_object *child,
1927 /* Here, lname is an encoded hash of on-disk name, and
1928 * client is doing access without encryption key.
1929 * So we need to get LinkEA, check parent fid is correct and
1930 * compare name hash with the one in the request.
1932 struct lu_buf *buf = &info->mti_big_buf;
1933 struct lu_name name;
1935 struct linkea_data ldata = { NULL };
1936 struct link_ea_header *leh;
1937 struct link_ea_entry *lee;
1938 struct lu_buf link = { 0 };
1940 int reclen, count, rc;
1944 if (lname->ln_namelen < LLCRYPT_FNAME_DIGEST_SIZE)
1947 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
1952 rc = mdt_links_read(info, child, &ldata);
1957 if (force_check || leh->leh_reccount > 1) {
1958 hash = kmalloc(lname->ln_namelen, GFP_NOFS);
1961 rc = critical_decode(lname->ln_name, lname->ln_namelen, hash);
1963 lee = (struct link_ea_entry *)(leh + 1);
1964 for (count = 0; count < leh->leh_reccount; count++) {
1965 linkea_entry_unpack(lee, &reclen, &name, &pfid);
1966 if (!force_check && leh->leh_reccount == 1) {
1967 /* if there is only one rec, it has to be it */
1971 if (!parent || lu_fid_eq(&pfid, mdt_object_fid(parent))) {
1972 lu_buf_check_and_alloc(&link, name.ln_namelen);
1974 GOTO(out_match, rc = -ENOMEM);
1975 rc = critical_decode(name.ln_name, name.ln_namelen,
1978 if (memcmp(LLCRYPT_FNAME_DIGEST(link.lb_buf, rc),
1979 hash, LLCRYPT_FNAME_DIGEST_SIZE) == 0) {
1984 lee = (struct link_ea_entry *) ((char *)lee + reclen);
1986 if (count == leh->leh_reccount)
1999 * UPDATE lock should be taken against parent, and be released before exit;
2000 * child_bits lock should be taken against child, and be returned back:
2001 * (1)normal request should release the child lock;
2002 * (2)intent request will grant the lock to client.
2004 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
2005 struct mdt_lock_handle *lhc,
2007 struct ldlm_reply *ldlm_rep)
2009 struct ptlrpc_request *req = mdt_info_req(info);
2010 struct mdt_body *reqbody = NULL;
2011 struct mdt_object *parent = info->mti_object;
2012 struct mdt_object *child = NULL;
2013 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2014 struct lu_name *lname = NULL;
2015 struct mdt_lock_handle *lhp = NULL;
2016 struct ldlm_lock *lock;
2017 struct req_capsule *pill = info->mti_pill;
2025 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
2026 LASSERT(ergo(is_resent,
2027 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
2032 if (info->mti_cross_ref) {
2033 /* Only getattr on the child. Parent is on another node. */
2034 mdt_set_disposition(info, ldlm_rep,
2035 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
2037 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
2039 PFID(mdt_object_fid(child)), ldlm_rep);
2041 rc = mdt_check_resent_lock(info, child, lhc);
2044 } else if (rc > 0) {
2045 mdt_lock_handle_init(lhc);
2046 mdt_lock_reg_init(lhc, LCK_PR);
2049 * Object's name entry is on another MDS, it will
2050 * request PERM lock only because LOOKUP lock is owned
2051 * by the MDS where name entry resides.
2053 * TODO: it should try layout lock too. - Jinshan
2055 child_bits &= ~(MDS_INODELOCK_LOOKUP |
2056 MDS_INODELOCK_LAYOUT);
2057 child_bits |= MDS_INODELOCK_PERM;
2059 rc = mdt_object_lock(info, child, lhc, child_bits);
2064 /* Finally, we can get attr for child. */
2065 if (!mdt_object_exists(child)) {
2066 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
2068 "remote object doesn't exist.");
2069 mdt_object_unlock(info, child, lhc, 1);
2073 rc = mdt_getattr_internal(info, child, 0);
2074 if (unlikely(rc != 0)) {
2075 mdt_object_unlock(info, child, lhc, 1);
2079 rc = mdt_pack_secctx_in_reply(info, child);
2081 mdt_object_unlock(info, child, lhc, 1);
2085 rc = mdt_pack_encctx_in_reply(info, child);
2087 mdt_object_unlock(info, child, lhc, 1);
2091 lname = &info->mti_name;
2092 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
2094 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2095 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2096 if (unlikely(reqbody == NULL))
2097 RETURN(err_serious(-EPROTO));
2099 *child_fid = reqbody->mbo_fid2;
2100 if (unlikely(!fid_is_sane(child_fid)))
2101 RETURN(err_serious(-EINVAL));
2103 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2104 mdt_object_get(info->mti_env, parent);
2107 child = mdt_object_find(info->mti_env, info->mti_mdt,
2110 RETURN(PTR_ERR(child));
2113 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2115 PFID(mdt_object_fid(parent)),
2116 PFID(&reqbody->mbo_fid2), ldlm_rep);
2117 } else if (lu_name_is_valid(lname)) {
2118 if (mdt_object_remote(parent)) {
2119 CERROR("%s: parent "DFID" is on remote target\n",
2120 mdt_obd_name(info->mti_mdt),
2121 PFID(mdt_object_fid(parent)));
2125 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
2126 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
2127 PNAME(lname), ldlm_rep);
2129 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
2130 if (unlikely(reqbody == NULL))
2131 RETURN(err_serious(-EPROTO));
2133 *child_fid = reqbody->mbo_fid2;
2134 if (unlikely(!fid_is_sane(child_fid)))
2135 RETURN(err_serious(-EINVAL));
2137 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
2138 mdt_object_get(info->mti_env, parent);
2141 child = mdt_object_find(info->mti_env, info->mti_mdt,
2144 RETURN(PTR_ERR(child));
2147 if (mdt_object_remote(child)) {
2148 CERROR("%s: child "DFID" is on remote target\n",
2149 mdt_obd_name(info->mti_mdt),
2150 PFID(mdt_object_fid(child)));
2151 GOTO(out_child, rc = -EPROTO);
2154 /* don't fetch LOOKUP lock if it's remote object */
2155 rc = mdt_is_remote_object(info, parent, child);
2157 GOTO(out_child, rc);
2159 child_bits &= ~MDS_INODELOCK_LOOKUP;
2161 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
2163 PFID(mdt_object_fid(parent)),
2164 PFID(&reqbody->mbo_fid2), ldlm_rep);
2167 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
2169 if (unlikely(!mdt_object_exists(parent)) &&
2170 !(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2171 lu_name_is_valid(lname)) {
2172 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2174 "Parent doesn't exist!");
2175 GOTO(out_child, rc = -ESTALE);
2178 if (!(info->mti_body->mbo_valid & OBD_MD_NAMEHASH) &&
2179 lu_name_is_valid(lname)) {
2180 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
2181 rc = mdt_raw_lookup(info, parent, lname);
2186 /* step 1: lock parent only if parent is a directory */
2187 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2188 lhp = &info->mti_lh[MDT_LH_PARENT];
2189 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2190 rc = mdt_object_lock(info, parent, lhp,
2191 MDS_INODELOCK_UPDATE);
2192 if (unlikely(rc != 0))
2196 /* step 2: lookup child's fid by name */
2197 fid_zero(child_fid);
2198 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2199 child_fid, &info->mti_spec);
2201 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2204 GOTO(unlock_parent, rc);
2206 child = mdt_object_find(info->mti_env, info->mti_mdt,
2208 if (unlikely(IS_ERR(child)))
2209 GOTO(unlock_parent, rc = PTR_ERR(child));
2212 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2214 /* step 3: lock child regardless if it is local or remote. */
2217 if (info->mti_body->mbo_valid & OBD_MD_NAMEHASH) {
2218 /* Here, lname is an encoded hash of on-disk name, and
2219 * client is doing access without encryption key.
2220 * So we need to compare name hash with the one in the request.
2222 if (!find_name_matching_hash(info, lname, parent,
2224 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2225 mdt_clear_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2226 GOTO(out_child, rc = -ENOENT);
2230 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2231 if (!mdt_object_exists(child)) {
2232 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2234 "Object doesn't exist!");
2235 GOTO(out_child, rc = -ENOENT);
2238 rc = mdt_check_resent_lock(info, child, lhc);
2240 GOTO(out_child, rc);
2241 } else if (rc > 0) {
2242 mdt_lock_handle_init(lhc);
2243 mdt_lock_reg_init(lhc, LCK_PR);
2245 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2246 !mdt_object_remote(child)) {
2247 struct md_attr *ma = &info->mti_attr;
2250 ma->ma_need = MA_INODE;
2251 rc = mdt_attr_get_complex(info, child, ma);
2252 if (unlikely(rc != 0))
2253 GOTO(out_child, rc);
2255 /* If the file has not been changed for some time, we
2256 * return not only a LOOKUP lock, but also an UPDATE
2257 * lock and this might save us RPC on later STAT. For
2258 * directories, it also let negative dentry cache start
2259 * working for this dir. */
2260 if (ma->ma_valid & MA_INODE &&
2261 ma->ma_attr.la_valid & LA_CTIME &&
2262 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2263 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2264 child_bits |= MDS_INODELOCK_UPDATE;
2267 /* layout lock must be granted in a best-effort way
2268 * for IT operations */
2269 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2270 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2271 !mdt_object_remote(child) && ldlm_rep != NULL) {
2272 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2273 exp_connect_layout(info->mti_exp)) {
2274 /* try to grant layout lock for regular file. */
2275 try_bits = MDS_INODELOCK_LAYOUT;
2277 /* Acquire DOM lock in advance for data-on-mdt file */
2278 if (child != parent)
2279 try_bits |= MDS_INODELOCK_DOM;
2282 if (try_bits != 0) {
2283 /* try layout lock, it may fail to be granted due to
2284 * contention at LOOKUP or UPDATE */
2285 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2287 if (child_bits & MDS_INODELOCK_LAYOUT)
2290 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2291 * client will enqueue the lock to the remote MDT */
2292 if (mdt_object_remote(child))
2293 child_bits &= ~MDS_INODELOCK_UPDATE;
2294 rc = mdt_object_lock(info, child, lhc, child_bits);
2296 if (unlikely(rc != 0))
2297 GOTO(out_child, rc);
2300 /* finally, we can get attr for child. */
2301 rc = mdt_getattr_internal(info, child, ma_need);
2302 if (unlikely(rc != 0)) {
2303 mdt_object_unlock(info, child, lhc, 1);
2304 GOTO(out_child, rc);
2307 rc = mdt_pack_secctx_in_reply(info, child);
2309 mdt_object_unlock(info, child, lhc, 1);
2310 GOTO(out_child, rc);
2313 rc = mdt_pack_encctx_in_reply(info, child);
2315 mdt_object_unlock(info, child, lhc, 1);
2316 GOTO(out_child, rc);
2319 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2321 /* Debugging code. */
2322 LDLM_DEBUG(lock, "Returning lock to client");
2323 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2324 &lock->l_resource->lr_name),
2325 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2326 PLDLMRES(lock->l_resource),
2327 PFID(mdt_object_fid(child)));
2329 if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_ENQ_RESEND))) {
2330 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
2331 OBD_FAIL_TIMEOUT(OBD_FAIL_PTLRPC_ENQ_RESEND,
2333 req->rq_arrival_time.tv_sec +
2335 /* Put the lock to the waiting list and force the cancel */
2336 ldlm_set_ast_sent(lock);
2339 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2340 !mdt_object_remote(child) && child != parent) {
2341 mdt_object_put(info->mti_env, child);
2342 rc = mdt_pack_size2body(info, child_fid,
2344 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2345 /* DOM lock was taken in advance but this is
2346 * not DoM file. Drop the lock.
2348 lock_res_and_lock(lock);
2349 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2350 unlock_res_and_lock(lock);
2352 LDLM_LOCK_PUT(lock);
2353 GOTO(unlock_parent, rc = 0);
2355 LDLM_LOCK_PUT(lock);
2361 mdt_object_put(info->mti_env, child);
2364 mdt_object_unlock(info, parent, lhp, 1);
2368 /* normal handler: should release the child lock */
2369 static int mdt_getattr_name(struct tgt_session_info *tsi)
2371 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2372 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2373 struct mdt_body *reqbody;
2374 struct mdt_body *repbody;
2379 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2380 LASSERT(reqbody != NULL);
2381 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2382 LASSERT(repbody != NULL);
2384 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2385 repbody->mbo_eadatasize = 0;
2386 repbody->mbo_aclsize = 0;
2388 rc = mdt_init_ucred(info, reqbody);
2390 GOTO(out_shrink, rc);
2392 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2393 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2394 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2395 lhc->mlh_reg_lh.cookie = 0;
2397 mdt_exit_ucred(info);
2400 mdt_client_compatibility(info);
2401 rc2 = mdt_fix_reply(info);
2404 mdt_thread_info_fini(info);
2408 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2409 const struct lu_fid *pfid,
2410 const struct lu_name *name,
2411 struct mdt_object *obj, s64 ctime)
2413 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2414 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2415 struct mdt_device *mdt = info->mti_mdt;
2416 struct md_attr *ma = &info->mti_attr;
2417 struct mdt_lock_handle *parent_lh;
2418 struct mdt_lock_handle *child_lh;
2419 struct mdt_object *pobj;
2420 bool cos_incompat = false;
2424 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2426 GOTO(out, rc = PTR_ERR(pobj));
2428 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2429 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2430 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2432 GOTO(put_parent, rc);
2434 if (mdt_object_remote(pobj))
2435 cos_incompat = true;
2437 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2438 name, child_fid, &info->mti_spec);
2440 GOTO(unlock_parent, rc);
2442 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2443 GOTO(unlock_parent, rc = -EREMCHG);
2445 child_lh = &info->mti_lh[MDT_LH_CHILD];
2446 mdt_lock_reg_init(child_lh, LCK_EX);
2447 rc = mdt_reint_striped_lock(info, obj, child_lh,
2448 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2449 einfo, cos_incompat);
2451 GOTO(unlock_parent, rc);
2453 if (atomic_read(&obj->mot_open_count)) {
2454 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2455 PFID(mdt_object_fid(obj)));
2456 GOTO(unlock_child, rc = -EBUSY);
2460 ma->ma_valid = MA_INODE;
2461 ma->ma_attr.la_valid = LA_CTIME;
2462 ma->ma_attr.la_ctime = ctime;
2464 mutex_lock(&obj->mot_lov_mutex);
2466 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2467 mdt_object_child(obj), name, ma, 0);
2469 mutex_unlock(&obj->mot_lov_mutex);
2472 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2474 mdt_object_unlock(info, pobj, parent_lh, 1);
2476 mdt_object_put(info->mti_env, pobj);
2481 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2482 struct mdt_object *obj)
2484 struct lu_ucred *uc = lu_ucred(info->mti_env);
2485 struct md_attr *ma = &info->mti_attr;
2486 struct lu_attr *la = &ma->ma_attr;
2490 ma->ma_need = MA_INODE;
2491 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2495 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2498 if (cap_raised(uc->uc_cap, CAP_DAC_OVERRIDE))
2500 if (uc->uc_fsuid == la->la_uid) {
2501 if ((la->la_mode & S_IWUSR) == 0)
2503 } else if (uc->uc_fsgid == la->la_gid) {
2504 if ((la->la_mode & S_IWGRP) == 0)
2506 } else if ((la->la_mode & S_IWOTH) == 0) {
2514 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2517 struct mdt_device *mdt = info->mti_mdt;
2518 struct mdt_object *obj = NULL;
2519 struct linkea_data ldata = { NULL };
2520 struct lu_buf *buf = &info->mti_big_buf;
2521 struct lu_name *name = &info->mti_name;
2522 struct lu_fid *pfid = &info->mti_tmp_fid1;
2523 struct link_ea_header *leh;
2524 struct link_ea_entry *lee;
2525 int reclen, count, rc = 0;
2528 if (!fid_is_sane(fid))
2529 GOTO(out, rc = -EINVAL);
2531 if (!fid_is_namespace_visible(fid))
2532 GOTO(out, rc = -EINVAL);
2534 obj = mdt_object_find(info->mti_env, mdt, fid);
2536 GOTO(out, rc = PTR_ERR(obj));
2538 if (mdt_object_remote(obj))
2539 GOTO(out, rc = -EREMOTE);
2540 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2541 GOTO(out, rc = -ENOENT);
2543 rc = mdt_rmfid_check_permission(info, obj);
2548 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2550 GOTO(out, rc = -ENOMEM);
2553 rc = mdt_links_read(info, obj, &ldata);
2558 lee = (struct link_ea_entry *)(leh + 1);
2559 for (count = 0; count < leh->leh_reccount; count++) {
2560 /* remove every hardlink */
2561 linkea_entry_unpack(lee, &reclen, name, pfid);
2562 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2563 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2569 if (obj && !IS_ERR(obj))
2570 mdt_object_put(info->mti_env, obj);
2571 if (info->mti_big_buf.lb_buf)
2572 lu_buf_free(&info->mti_big_buf);
2577 static int mdt_rmfid(struct tgt_session_info *tsi)
2579 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2580 struct mdt_body *reqbody;
2581 struct lu_fid *fids, *rfids;
2587 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2588 if (reqbody == NULL)
2590 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2592 nr = bufsize / sizeof(struct lu_fid);
2593 if (nr * sizeof(struct lu_fid) != bufsize)
2595 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2596 RCL_SERVER, nr * sizeof(__u32));
2597 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2598 RCL_SERVER, nr * sizeof(struct lu_fid));
2599 rc = req_capsule_server_pack(tsi->tsi_pill);
2601 GOTO(out, rc = err_serious(rc));
2602 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2605 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2607 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2610 mdt_init_ucred(mti, reqbody);
2611 for (i = 0; i < nr; i++) {
2613 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2615 mdt_exit_ucred(mti);
2621 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2622 void *karg, void __user *uarg);
2624 static int mdt_set_info(struct tgt_session_info *tsi)
2626 struct ptlrpc_request *req = tgt_ses_req(tsi);
2629 int keylen, vallen, rc = 0;
2633 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2635 DEBUG_REQ(D_HA, req, "no set_info key");
2636 RETURN(err_serious(-EFAULT));
2639 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2642 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2644 DEBUG_REQ(D_HA, req, "no set_info val");
2645 RETURN(err_serious(-EFAULT));
2648 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2651 /* Swab any part of val you need to here */
2652 if (KEY_IS(KEY_READ_ONLY)) {
2653 spin_lock(&req->rq_export->exp_lock);
2655 *exp_connect_flags_ptr(req->rq_export) |=
2658 *exp_connect_flags_ptr(req->rq_export) &=
2659 ~OBD_CONNECT_RDONLY;
2660 spin_unlock(&req->rq_export->exp_lock);
2661 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2662 struct changelog_setinfo *cs = val;
2664 if (vallen != sizeof(*cs)) {
2665 CERROR("%s: bad changelog_clear setinfo size %d\n",
2666 tgt_name(tsi->tsi_tgt), vallen);
2669 if (req_capsule_req_need_swab(&req->rq_pill)) {
2670 __swab64s(&cs->cs_recno);
2671 __swab32s(&cs->cs_id);
2674 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2676 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2678 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2680 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2687 static int mdt_readpage(struct tgt_session_info *tsi)
2689 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2690 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2691 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2692 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2693 struct mdt_body *repbody;
2699 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2700 RETURN(err_serious(-ENOMEM));
2702 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2703 if (repbody == NULL || reqbody == NULL)
2704 RETURN(err_serious(-EFAULT));
2707 * prepare @rdpg before calling lower layers and transfer itself. Here
2708 * reqbody->size contains offset of where to start to read and
2709 * reqbody->nlink contains number bytes to read.
2711 rdpg->rp_hash = reqbody->mbo_size;
2712 if (rdpg->rp_hash != reqbody->mbo_size) {
2713 CERROR("Invalid hash: %#llx != %#llx\n",
2714 rdpg->rp_hash, reqbody->mbo_size);
2718 rdpg->rp_attrs = reqbody->mbo_mode;
2719 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2720 rdpg->rp_attrs |= LUDA_64BITHASH;
2721 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2722 exp_max_brw_size(tsi->tsi_exp));
2723 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2725 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2726 if (rdpg->rp_pages == NULL)
2729 for (i = 0; i < rdpg->rp_npages; ++i) {
2730 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2731 if (rdpg->rp_pages[i] == NULL)
2732 GOTO(free_rdpg, rc = -ENOMEM);
2735 /* call lower layers to fill allocated pages with directory data */
2736 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2738 GOTO(free_rdpg, rc);
2740 /* send pages to client */
2741 rc = tgt_sendpage(tsi, rdpg, rc);
2746 for (i = 0; i < rdpg->rp_npages; i++)
2747 if (rdpg->rp_pages[i] != NULL)
2748 __free_page(rdpg->rp_pages[i]);
2749 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2751 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2757 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2759 struct lu_ucred *uc = mdt_ucred_check(info);
2760 struct lu_attr *attr = &info->mti_attr.ma_attr;
2765 if (op != REINT_SETATTR) {
2766 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2767 attr->la_uid = uc->uc_fsuid;
2768 /* for S_ISGID, inherit gid from his parent, such work will be
2769 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2770 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2771 attr->la_gid = uc->uc_fsgid;
2777 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2779 return op == REINT_OPEN &&
2780 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2783 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2785 struct req_capsule *pill = info->mti_pill;
2787 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2789 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2791 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2793 /* pre-set size in server part with max size */
2794 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2796 OBD_MAX_DEFAULT_EA_SIZE);
2798 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2803 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
2805 struct req_capsule *pill = info->mti_pill;
2807 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
2809 /* pre-set size in server part with max size */
2810 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
2812 info->mti_mdt->mdt_max_mdsize);
2815 static int mdt_reint_internal(struct mdt_thread_info *info,
2816 struct mdt_lock_handle *lhc,
2819 struct req_capsule *pill = info->mti_pill;
2820 struct mdt_body *repbody;
2825 rc = mdt_reint_unpack(info, op);
2827 CERROR("Can't unpack reint, rc %d\n", rc);
2828 RETURN(err_serious(rc));
2832 /* check if the file system is set to readonly. O_RDONLY open
2833 * is still allowed even the file system is set to readonly mode */
2834 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2835 RETURN(err_serious(-EROFS));
2837 /* for replay (no_create) lmm is not needed, client has it already */
2838 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2839 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2842 /* llog cookies are always 0, the field is kept for compatibility */
2843 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2844 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2846 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2847 * by default. If the target object has more ACL entries, then
2848 * enlarge the buffer when necessary. */
2849 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2850 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2851 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2853 mdt_preset_secctx_size(info);
2854 mdt_preset_encctx_size(info);
2856 rc = req_capsule_server_pack(pill);
2858 CERROR("Can't pack response, rc %d\n", rc);
2859 RETURN(err_serious(rc));
2862 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2863 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2865 repbody->mbo_eadatasize = 0;
2866 repbody->mbo_aclsize = 0;
2869 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2871 /* for replay no cookkie / lmm need, because client have this already */
2872 if (info->mti_spec.no_create)
2873 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2874 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2876 rc = mdt_init_ucred_reint(info);
2878 GOTO(out_shrink, rc);
2880 rc = mdt_fix_attr_ucred(info, op);
2882 GOTO(out_ucred, rc = err_serious(rc));
2884 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2886 GOTO(out_ucred, rc);
2887 } else if (rc == 1) {
2888 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2889 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2890 GOTO(out_ucred, rc);
2892 rc = mdt_reint_rec(info, lhc);
2895 mdt_exit_ucred(info);
2897 mdt_client_compatibility(info);
2899 rc2 = mdt_fix_reply(info);
2904 * Data-on-MDT optimization - read data along with OPEN and return it
2905 * in reply when possible.
2907 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2908 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2914 static long mdt_reint_opcode(struct ptlrpc_request *req,
2915 const struct req_format **fmt)
2917 struct mdt_device *mdt;
2918 struct mdt_rec_reint *rec;
2921 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2923 opc = rec->rr_opcode;
2924 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2925 if (opc < REINT_MAX && fmt[opc] != NULL)
2926 req_capsule_extend(&req->rq_pill, fmt[opc]);
2928 mdt = mdt_exp2dev(req->rq_export);
2929 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2930 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2931 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2932 opc = err_serious(-EFAULT);
2935 opc = err_serious(-EFAULT);
2940 static int mdt_reint(struct tgt_session_info *tsi)
2944 static const struct req_format *reint_fmts[REINT_MAX] = {
2945 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2946 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2947 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2948 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2949 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2950 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2951 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2952 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2953 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
2954 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2959 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2961 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2963 * No lock possible here from client to pass it to reint code
2966 rc = mdt_reint_internal(info, NULL, opc);
2967 mdt_thread_info_fini(info);
2972 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2976 /* this should sync the whole device */
2977 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2979 struct dt_device *dt = mdt->mdt_bottom;
2983 rc = dt->dd_ops->dt_sync(env, dt);
2987 /* this should sync this object */
2988 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2989 struct mdt_object *mo)
2995 if (!mdt_object_exists(mo)) {
2996 CWARN("%s: non existing object "DFID": rc = %d\n",
2997 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
3002 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
3003 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
3004 dt_obj_version_t version;
3006 version = dt_version_get(env, mdt_obj2dt(mo));
3007 if (version > tgt->lut_obd->obd_last_committed)
3008 rc = mo_object_sync(env, mdt_object_child(mo));
3010 rc = mo_object_sync(env, mdt_object_child(mo));
3016 static int mdt_sync(struct tgt_session_info *tsi)
3018 struct ptlrpc_request *req = tgt_ses_req(tsi);
3019 struct req_capsule *pill = tsi->tsi_pill;
3020 struct mdt_body *body;
3021 ktime_t kstart = ktime_get();
3026 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
3027 RETURN(err_serious(-ENOMEM));
3029 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
3030 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
3032 struct mdt_thread_info *info = tsi2mdt_info(tsi);
3034 if (unlikely(info->mti_object == NULL))
3037 /* sync an object */
3038 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
3041 const struct lu_fid *fid;
3042 struct lu_attr *la = &info->mti_attr.ma_attr;
3044 info->mti_attr.ma_need = MA_INODE;
3045 info->mti_attr.ma_valid = 0;
3046 rc = mdt_attr_get_complex(info, info->mti_object,
3049 body = req_capsule_server_get(pill,
3051 fid = mdt_object_fid(info->mti_object);
3052 mdt_pack_attr2body(info, body, la, fid);
3055 mdt_thread_info_fini(info);
3058 mdt_counter_incr(req, LPROC_MDT_SYNC,
3059 ktime_us_delta(ktime_get(), kstart));
3064 static int mdt_data_sync(struct tgt_session_info *tsi)
3066 struct mdt_thread_info *info;
3067 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3068 struct ost_body *body = tsi->tsi_ost_body;
3069 struct ost_body *repbody;
3070 struct mdt_object *mo = NULL;
3076 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
3078 /* if no fid is specified then do nothing,
3079 * device sync is done via MDS_SYNC */
3080 if (fid_is_zero(&tsi->tsi_fid))
3083 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
3085 RETURN(PTR_ERR(mo));
3087 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
3091 repbody->oa.o_oi = body->oa.o_oi;
3092 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
3094 info = tsi2mdt_info(tsi);
3095 ma = &info->mti_attr;
3096 ma->ma_need = MA_INODE;
3098 rc = mdt_attr_get_complex(info, mo, ma);
3100 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
3103 mdt_thread_info_fini(info);
3108 mdt_object_put(tsi->tsi_env, mo);
3113 * Handle quota control requests to consult current usage/limit, but also
3114 * to configure quota enforcement
3116 static int mdt_quotactl(struct tgt_session_info *tsi)
3118 struct obd_export *exp = tsi->tsi_exp;
3119 struct req_capsule *pill = tsi->tsi_pill;
3120 struct obd_quotactl *oqctl, *repoqc;
3122 struct mdt_device *mdt = mdt_exp2dev(exp);
3123 struct lu_device *qmt = mdt->mdt_qmt_dev;
3124 struct lu_nodemap *nodemap;
3127 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
3129 RETURN(err_serious(-EPROTO));
3131 rc = req_capsule_server_pack(pill);
3133 RETURN(err_serious(rc));
3135 nodemap = nodemap_get_from_exp(exp);
3136 if (IS_ERR(nodemap))
3137 RETURN(PTR_ERR(nodemap));
3139 switch (oqctl->qc_cmd) {
3140 /* master quotactl */
3143 case LUSTRE_Q_SETDEFAULT:
3144 case LUSTRE_Q_SETQUOTAPOOL:
3145 case LUSTRE_Q_SETINFOPOOL:
3146 case LUSTRE_Q_SETDEFAULT_POOL:
3147 if (!nodemap_can_setquota(nodemap, oqctl->qc_type,
3149 GOTO(out_nodemap, rc = -EPERM);
3153 case LUSTRE_Q_GETDEFAULT:
3154 case LUSTRE_Q_GETQUOTAPOOL:
3155 case LUSTRE_Q_GETINFOPOOL:
3156 case LUSTRE_Q_GETDEFAULT_POOL:
3158 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3159 /* slave quotactl */
3166 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
3167 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
3168 GOTO(out_nodemap, rc);
3172 switch (oqctl->qc_type) {
3174 id = nodemap_map_id(nodemap, NODEMAP_UID,
3175 NODEMAP_CLIENT_TO_FS, id);
3178 id = nodemap_map_id(nodemap, NODEMAP_GID,
3179 NODEMAP_CLIENT_TO_FS, id);
3182 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
3183 NODEMAP_CLIENT_TO_FS, id);
3186 GOTO(out_nodemap, rc = -EOPNOTSUPP);
3188 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
3190 GOTO(out_nodemap, rc = err_serious(-EFAULT));
3192 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
3193 barrier_exit(tsi->tsi_tgt->lut_bottom);
3195 if (oqctl->qc_id != id)
3196 swap(oqctl->qc_id, id);
3198 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
3199 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
3200 RETURN(-EINPROGRESS);
3203 switch (oqctl->qc_cmd) {
3209 case LUSTRE_Q_SETDEFAULT:
3210 case LUSTRE_Q_GETDEFAULT:
3211 case LUSTRE_Q_SETQUOTAPOOL:
3212 case LUSTRE_Q_GETQUOTAPOOL:
3213 case LUSTRE_Q_SETINFOPOOL:
3214 case LUSTRE_Q_GETINFOPOOL:
3215 case LUSTRE_Q_SETDEFAULT_POOL:
3216 case LUSTRE_Q_GETDEFAULT_POOL:
3217 /* forward quotactl request to QMT */
3218 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3223 /* slave quotactl */
3224 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3229 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3230 GOTO(out_nodemap, rc = -EFAULT);
3233 if (oqctl->qc_id != id)
3234 swap(oqctl->qc_id, id);
3236 QCTL_COPY(repoqc, oqctl);
3240 nodemap_putref(nodemap);
3245 /** clone llog ctxt from child (mdd)
3246 * This allows remote llog (replicator) access.
3247 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3248 * context was originally set up, or we can handle them directly.
3249 * I choose the latter, but that means I need any llog
3250 * contexts set up by child to be accessable by the mdt. So we clone the
3251 * context into our context list here.
3253 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3256 struct md_device *next = mdt->mdt_child;
3257 struct llog_ctxt *ctxt;
3260 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3263 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3264 if (rc || ctxt == NULL) {
3268 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3270 CERROR("Can't set mdt ctxt %d\n", rc);
3275 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3276 struct mdt_device *mdt, int idx)
3278 struct llog_ctxt *ctxt;
3280 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3283 /* Put once for the get we just did, and once for the clone */
3284 llog_ctxt_put(ctxt);
3285 llog_ctxt_put(ctxt);
3290 * sec context handlers
3292 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3294 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3300 * quota request handlers
3302 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3304 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3305 struct lu_device *qmt = mdt->mdt_qmt_dev;
3310 RETURN(err_serious(-EOPNOTSUPP));
3312 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3316 struct mdt_object *mdt_object_new(const struct lu_env *env,
3317 struct mdt_device *d,
3318 const struct lu_fid *f)
3320 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3321 struct lu_object *o;
3322 struct mdt_object *m;
3325 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3326 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3327 if (unlikely(IS_ERR(o)))
3328 m = (struct mdt_object *)o;
3334 struct mdt_object *mdt_object_find(const struct lu_env *env,
3335 struct mdt_device *d,
3336 const struct lu_fid *f)
3338 struct lu_object *o;
3339 struct mdt_object *m;
3342 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3343 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3344 if (unlikely(IS_ERR(o)))
3345 m = (struct mdt_object *)o;
3353 * Asyncronous commit for mdt device.
3355 * Pass asynchonous commit call down the MDS stack.
3357 * \param env environment
3358 * \param mdt the mdt device
3360 static void mdt_device_commit_async(const struct lu_env *env,
3361 struct mdt_device *mdt)
3363 struct dt_device *dt = mdt->mdt_bottom;
3367 rc = dt->dd_ops->dt_commit_async(env, dt);
3368 if (unlikely(rc != 0))
3369 CWARN("%s: async commit start failed: rc = %d\n",
3370 mdt_obd_name(mdt), rc);
3371 atomic_inc(&mdt->mdt_async_commit_count);
3376 * Mark the lock as "synchonous".
3378 * Mark the lock to deffer transaction commit to the unlock time.
3380 * \param lock the lock to mark as "synchonous"
3382 * \see mdt_is_lock_sync
3383 * \see mdt_save_lock
3385 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3387 lock->l_ast_data = (void*)1;
3391 * Check whehter the lock "synchonous" or not.
3393 * \param lock the lock to check
3394 * \retval 1 the lock is "synchonous"
3395 * \retval 0 the lock isn't "synchronous"
3397 * \see mdt_set_lock_sync
3398 * \see mdt_save_lock
3400 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3402 return lock->l_ast_data != NULL;
3406 * Blocking AST for mdt locks.
3408 * Starts transaction commit if in case of COS lock conflict or
3409 * deffers such a commit to the mdt_save_lock.
3411 * \param lock the lock which blocks a request or cancelling lock
3412 * \param desc unused
3413 * \param data unused
3414 * \param flag indicates whether this cancelling or blocking callback
3416 * \see ldlm_blocking_ast_nocheck
3418 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3419 void *data, int flag)
3421 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3422 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3423 struct ldlm_cb_set_arg *arg = data;
3424 bool commit_async = false;
3428 if (flag == LDLM_CB_CANCELING)
3431 lock_res_and_lock(lock);
3432 if (lock->l_blocking_ast != mdt_blocking_ast) {
3433 unlock_res_and_lock(lock);
3437 /* A blocking ast may be sent from ldlm_lock_decref_internal
3438 * when the last reference to a local lock was released and
3439 * during blocking event from ldlm_work_bl_ast_lock().
3440 * The 'data' parameter is l_ast_data in the first case and
3441 * callback arguments in the second one. Distinguish them by that.
3443 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3444 goto skip_cos_checks;
3446 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3447 if (mdt_cos_is_enabled(mdt)) {
3448 if (!arg->bl_desc->bl_same_client)
3449 mdt_set_lock_sync(lock);
3450 } else if (mdt_slc_is_enabled(mdt) &&
3451 arg->bl_desc->bl_cos_incompat) {
3452 mdt_set_lock_sync(lock);
3454 * we may do extra commit here, but there is a small
3455 * window to miss a commit: lock was unlocked (saved),
3456 * then a conflict lock queued and we come here, but
3457 * REP-ACK not received, so lock was not converted to
3459 * Fortunately this window is quite small, so the
3460 * extra commit should be rare (not to say distributed
3461 * operation is rare too).
3463 commit_async = true;
3465 } else if (lock->l_req_mode == LCK_COS) {
3466 commit_async = true;
3470 rc = ldlm_blocking_ast_nocheck(lock);
3475 rc = lu_env_init(&env, LCT_LOCAL);
3476 if (unlikely(rc != 0))
3477 CWARN("%s: lu_env initialization failed, cannot "
3478 "start asynchronous commit: rc = %d\n",
3481 mdt_device_commit_async(&env, mdt);
3488 * Blocking AST for cross-MDT lock
3490 * Discard lock from uncommitted_slc_locks and cancel it.
3492 * \param lock the lock which blocks a request or cancelling lock
3493 * \param desc unused
3494 * \param data unused
3495 * \param flag indicates whether this cancelling or blocking callback
3496 * \retval 0 on success
3497 * \retval negative number on error
3499 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3500 void *data, int flag)
3506 case LDLM_CB_BLOCKING: {
3507 struct lustre_handle lockh;
3509 ldlm_lock2handle(lock, &lockh);
3510 rc = ldlm_cli_cancel(&lockh,
3511 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3513 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3518 case LDLM_CB_CANCELING: {
3519 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3520 struct mdt_device *mdt =
3521 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3523 LDLM_DEBUG(lock, "Revoke remote lock\n");
3525 /* discard slc lock here so that it can be cleaned anytime,
3526 * especially for cleanup_resource() */
3527 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3529 /* once we cache lock, l_ast_data is set to mdt_object */
3530 if (lock->l_ast_data != NULL) {
3531 struct mdt_object *mo = lock->l_ast_data;
3534 rc = lu_env_init(&env, LCT_MD_THREAD);
3535 if (unlikely(rc != 0)) {
3536 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3538 PFID(mdt_object_fid(mo)), rc);
3542 if (lock->l_policy_data.l_inodebits.bits &
3543 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3544 rc = mo_invalidate(&env, mdt_object_child(mo));
3545 mo->mot_cache_attr = 0;
3547 mdt_object_put(&env, mo);
3559 int mdt_check_resent_lock(struct mdt_thread_info *info,
3560 struct mdt_object *mo,
3561 struct mdt_lock_handle *lhc)
3563 /* the lock might already be gotten in ldlm_handle_enqueue() */
3564 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3565 struct ptlrpc_request *req = mdt_info_req(info);
3566 struct ldlm_lock *lock;
3568 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3569 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3571 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3572 * a resend case, however, it could be already destroyed
3573 * due to client eviction or a raced cancel RPC. */
3574 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3575 lhc->mlh_reg_lh.cookie);
3579 if (!fid_res_name_eq(mdt_object_fid(mo),
3580 &lock->l_resource->lr_name)) {
3581 CWARN("%s: Although resent, but still not "
3582 "get child lock:"DFID"\n",
3583 info->mti_exp->exp_obd->obd_name,
3584 PFID(mdt_object_fid(mo)));
3585 LDLM_LOCK_PUT(lock);
3588 LDLM_LOCK_PUT(lock);
3594 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3596 mdt_object_get(NULL, lock->l_ast_data);
3599 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3600 struct mdt_object *o, const struct lu_fid *fid,
3601 struct lustre_handle *lh, enum ldlm_mode mode,
3602 __u64 *ibits, __u64 trybits, bool cache)
3604 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3605 union ldlm_policy_data *policy = &mti->mti_policy;
3606 struct ldlm_res_id *res_id = &mti->mti_res_id;
3610 LASSERT(mdt_object_remote(o));
3612 fid_build_reg_res_name(fid, res_id);
3614 memset(einfo, 0, sizeof(*einfo));
3615 einfo->ei_type = LDLM_IBITS;
3616 einfo->ei_mode = mode;
3617 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3618 einfo->ei_cb_cp = ldlm_completion_ast;
3619 einfo->ei_enq_slave = 0;
3620 einfo->ei_res_id = res_id;
3624 * if we cache lock, couple lock with mdt_object, so that object
3625 * can be easily found in lock ASTs.
3627 einfo->ei_cbdata = o;
3628 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3631 memset(policy, 0, sizeof(*policy));
3632 policy->l_inodebits.bits = *ibits;
3633 policy->l_inodebits.try_bits = trybits;
3635 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3638 /* Return successfully acquired bits to a caller */
3640 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3643 *ibits = lock->l_policy_data.l_inodebits.bits;
3644 LDLM_LOCK_PUT(lock);
3649 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3650 const struct lu_fid *fid, struct lustre_handle *lh,
3651 enum ldlm_mode mode, __u64 ibits, bool cache)
3653 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3657 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3658 struct mdt_lock_handle *lh, __u64 *ibits,
3659 __u64 trybits, bool cos_incompat)
3661 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3662 union ldlm_policy_data *policy = &info->mti_policy;
3663 struct ldlm_res_id *res_id = &info->mti_res_id;
3664 __u64 dlmflags = 0, *cookie = NULL;
3668 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3669 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3670 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3671 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3674 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3675 lh->mlh_reg_mode == LCK_EX);
3676 dlmflags |= LDLM_FL_COS_INCOMPAT;
3677 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3678 dlmflags |= LDLM_FL_COS_ENABLED;
3681 /* Only enqueue LOOKUP lock for remote object */
3682 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3684 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3685 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3686 *ibits == MDS_INODELOCK_OPEN)
3687 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3689 if (lh->mlh_type == MDT_PDO_LOCK) {
3690 /* check for exists after object is locked */
3691 if (mdt_object_exists(o) == 0) {
3692 /* Non-existent object shouldn't have PDO lock */
3695 /* Non-dir object shouldn't have PDO lock */
3696 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3701 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3702 dlmflags |= LDLM_FL_ATOMIC_CB;
3705 cookie = &info->mti_exp->exp_handle.h_cookie;
3708 * Take PDO lock on whole directory and build correct @res_id for lock
3709 * on part of directory.
3711 if (lh->mlh_pdo_hash != 0) {
3712 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3713 mdt_lock_pdo_mode(info, o, lh);
3714 if (lh->mlh_pdo_mode != LCK_NL) {
3716 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3717 * is never going to be sent to client and we do not
3718 * want it slowed down due to possible cancels.
3720 policy->l_inodebits.bits =
3721 *ibits & MDS_INODELOCK_UPDATE;
3722 policy->l_inodebits.try_bits =
3723 trybits & MDS_INODELOCK_UPDATE;
3724 /* at least one of them should be set */
3725 LASSERT(policy->l_inodebits.bits |
3726 policy->l_inodebits.try_bits);
3727 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3728 lh->mlh_pdo_mode, policy, res_id,
3730 if (unlikely(rc != 0))
3731 GOTO(out_unlock, rc);
3735 * Finish res_id initializing by name hash marking part of
3736 * directory which is taking modification.
3738 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3741 policy->l_inodebits.bits = *ibits;
3742 policy->l_inodebits.try_bits = trybits;
3743 policy->l_inodebits.li_gid = lh->mlh_gid;
3746 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3747 * going to be sent to client. If it is - mdt_intent_policy() path will
3748 * fix it up and turn FL_LOCAL flag off.
3750 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3751 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3755 mdt_object_unlock(info, o, lh, 1);
3756 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3757 lh->mlh_pdo_hash != 0 &&
3758 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3759 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3761 /* Return successfully acquired bits to a caller */
3763 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3766 *ibits = lock->l_policy_data.l_inodebits.bits;
3767 LDLM_LOCK_PUT(lock);
3773 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3774 struct mdt_lock_handle *lh, __u64 *ibits,
3775 __u64 trybits, bool cos_incompat)
3777 struct mdt_lock_handle *local_lh = NULL;
3781 if (!mdt_object_remote(o)) {
3782 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3787 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3788 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3789 MDS_INODELOCK_XATTR);
3791 /* Only enqueue LOOKUP lock for remote object */
3792 if (*ibits & MDS_INODELOCK_LOOKUP) {
3793 __u64 local = MDS_INODELOCK_LOOKUP;
3795 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3803 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3804 /* Sigh, PDO needs to enqueue 2 locks right now, but
3805 * enqueue RPC can only request 1 lock, to avoid extra
3806 * RPC, so it will instead enqueue EX lock for remote
3807 * object anyway XXX*/
3808 if (lh->mlh_type == MDT_PDO_LOCK &&
3809 lh->mlh_pdo_hash != 0) {
3811 "%s: "DFID" convert PDO lock to EX lock.\n",
3812 mdt_obd_name(info->mti_mdt),
3813 PFID(mdt_object_fid(o)));
3814 lh->mlh_pdo_hash = 0;
3815 lh->mlh_rreg_mode = LCK_EX;
3816 lh->mlh_type = MDT_REG_LOCK;
3819 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3822 ibits, trybits, false);
3823 if (rc != ELDLM_OK) {
3824 if (local_lh != NULL)
3825 mdt_object_unlock(info, o, local_lh, rc);
3830 /* other components like LFSCK can use lockless access
3831 * and populate cache, so we better invalidate it */
3832 mo_invalidate(info->mti_env, mdt_object_child(o));
3837 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3838 struct mdt_lock_handle *lh, __u64 ibits)
3840 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3843 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3844 struct mdt_lock_handle *lh, __u64 ibits,
3847 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3848 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3852 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3853 struct mdt_lock_handle *lh, __u64 *ibits,
3854 __u64 trybits, bool cos_incompat)
3856 bool trylock_only = *ibits == 0;
3859 LASSERT(!(*ibits & trybits));
3860 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3862 if (rc && trylock_only) { /* clear error for try ibits lock only */
3863 LASSERT(*ibits == 0);
3870 * Save a lock within request object.
3872 * Keep the lock referenced until whether client ACK or transaction
3873 * commit happens or release the lock immediately depending on input
3874 * parameters. If COS is ON, a write lock is converted to COS lock
3877 * \param info thead info object
3878 * \param h lock handle
3879 * \param mode lock mode
3880 * \param decref force immediate lock releasing
3882 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3883 enum ldlm_mode mode, int decref)
3887 if (lustre_handle_is_used(h)) {
3888 if (decref || !info->mti_has_trans ||
3889 !(mode & (LCK_PW | LCK_EX))) {
3890 mdt_fid_unlock(h, mode);
3892 struct mdt_device *mdt = info->mti_mdt;
3893 struct ldlm_lock *lock = ldlm_handle2lock(h);
3894 struct ptlrpc_request *req = mdt_info_req(info);
3895 bool cos = mdt_cos_is_enabled(mdt);
3896 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3898 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3901 /* there is no request if mdt_object_unlock() is called
3902 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3903 if (likely(req != NULL)) {
3904 LDLM_DEBUG(lock, "save lock request %p reply "
3905 "state %p transno %lld\n", req,
3906 req->rq_reply_state, req->rq_transno);
3908 ldlm_lock_mode_downgrade(lock, LCK_COS);
3911 if (req->rq_export->exp_disconnected)
3912 mdt_fid_unlock(h, mode);
3914 ptlrpc_save_lock(req, h, mode, cos,
3917 mdt_fid_unlock(h, mode);
3919 if (mdt_is_lock_sync(lock)) {
3920 CDEBUG(D_HA, "found sync-lock,"
3921 " async commit started\n");
3922 mdt_device_commit_async(info->mti_env,
3925 LDLM_LOCK_PUT(lock);
3934 * Save cross-MDT lock in uncommitted_slc_locks
3936 * Keep the lock referenced until transaction commit happens or release the lock
3937 * immediately depending on input parameters.
3939 * \param info thead info object
3940 * \param h lock handle
3941 * \param mode lock mode
3942 * \param decref force immediate lock releasing
3944 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3945 struct mdt_object *o, struct lustre_handle *h,
3946 enum ldlm_mode mode, int decref)
3950 if (lustre_handle_is_used(h)) {
3951 struct ldlm_lock *lock = ldlm_handle2lock(h);
3952 struct ptlrpc_request *req = mdt_info_req(info);
3955 (lock->l_policy_data.l_inodebits.bits &
3956 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3957 mo_invalidate(info->mti_env, mdt_object_child(o));
3959 if (decref || !info->mti_has_trans || !req ||
3960 !(mode & (LCK_PW | LCK_EX))) {
3961 ldlm_lock_decref_and_cancel(h, mode);
3962 LDLM_LOCK_PUT(lock);
3964 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3966 ldlm_lock_decref(h, mode);
3975 * Unlock mdt object.
3977 * Immeditely release the regular lock and the PDO lock or save the
3978 * lock in request and keep them referenced until client ACK or
3979 * transaction commit.
3981 * \param info thread info object
3982 * \param o mdt object
3983 * \param lh mdt lock handle referencing regular and PDO locks
3984 * \param decref force immediate lock releasing
3986 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3988 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3989 struct mdt_lock_handle *lh, int decref)
3993 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3994 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3995 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
4001 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
4002 const struct lu_fid *f,
4003 struct mdt_lock_handle *lh,
4006 struct mdt_object *o;
4008 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
4012 rc = mdt_object_lock(info, o, lh, ibits);
4014 mdt_object_put(info->mti_env, o);
4021 void mdt_object_unlock_put(struct mdt_thread_info * info,
4022 struct mdt_object * o,
4023 struct mdt_lock_handle *lh,
4026 mdt_object_unlock(info, o, lh, decref);
4027 mdt_object_put(info->mti_env, o);
4031 * Generic code handling requests that have struct mdt_body passed in:
4033 * - extract mdt_body from request and save it in @info, if present;
4035 * - create lu_object, corresponding to the fid in mdt_body, and save it in
4038 * - if HAS_BODY flag is set for this request type check whether object
4039 * actually exists on storage (lu_object_exists()).
4042 static int mdt_body_unpack(struct mdt_thread_info *info,
4043 enum tgt_handler_flags flags)
4045 const struct mdt_body *body;
4046 struct mdt_object *obj;
4047 const struct lu_env *env;
4048 struct req_capsule *pill;
4052 env = info->mti_env;
4053 pill = info->mti_pill;
4055 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
4059 if (!(body->mbo_valid & OBD_MD_FLID))
4062 if (!fid_is_sane(&body->mbo_fid1)) {
4063 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
4067 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
4069 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
4070 mdt_object_put(env, obj);
4073 info->mti_object = obj;
4082 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
4083 enum tgt_handler_flags flags)
4085 struct req_capsule *pill = info->mti_pill;
4090 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
4091 rc = mdt_body_unpack(info, flags);
4095 if (rc == 0 && (flags & HAS_REPLY)) {
4097 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
4098 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
4100 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
4101 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
4104 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
4105 * by default. If the target object has more ACL entries, then
4106 * enlarge the buffer when necessary. */
4107 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
4108 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
4109 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
4111 mdt_preset_secctx_size(info);
4112 mdt_preset_encctx_size(info);
4114 rc = req_capsule_server_pack(pill);
4116 CWARN("%s: cannot pack response: rc = %d\n",
4117 mdt_obd_name(info->mti_mdt), rc);
4122 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
4124 lh->mlh_type = MDT_NUL_LOCK;
4125 lh->mlh_reg_lh.cookie = 0ull;
4126 lh->mlh_reg_mode = LCK_MINMODE;
4127 lh->mlh_pdo_lh.cookie = 0ull;
4128 lh->mlh_pdo_mode = LCK_MINMODE;
4129 lh->mlh_rreg_lh.cookie = 0ull;
4130 lh->mlh_rreg_mode = LCK_MINMODE;
4133 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
4135 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
4136 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
4140 * Initialize fields of struct mdt_thread_info. Other fields are left in
4141 * uninitialized state, because it's too expensive to zero out whole
4142 * mdt_thread_info (> 1K) on each request arrival.
4144 void mdt_thread_info_init(struct ptlrpc_request *req,
4145 struct mdt_thread_info *info)
4149 info->mti_pill = &req->rq_pill;
4152 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4153 mdt_lock_handle_init(&info->mti_lh[i]);
4155 /* mdt device: it can be NULL while CONNECT */
4156 if (req->rq_export) {
4157 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
4158 info->mti_exp = req->rq_export;
4160 info->mti_mdt = NULL;
4161 info->mti_env = req->rq_svc_thread->t_env;
4162 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
4164 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
4165 info->mti_big_buf = LU_BUF_NULL;
4166 info->mti_body = NULL;
4167 info->mti_object = NULL;
4168 info->mti_dlm_req = NULL;
4169 info->mti_has_trans = 0;
4170 info->mti_cross_ref = 0;
4171 info->mti_opdata = 0;
4172 info->mti_big_lmm_used = 0;
4173 info->mti_big_acl_used = 0;
4174 info->mti_som_valid = 0;
4176 info->mti_spec.no_create = 0;
4177 info->mti_spec.sp_rm_entry = 0;
4178 info->mti_spec.sp_permitted = 0;
4180 info->mti_spec.u.sp_ea.eadata = NULL;
4181 info->mti_spec.u.sp_ea.eadatalen = 0;
4184 void mdt_thread_info_fini(struct mdt_thread_info *info)
4188 if (info->mti_object != NULL) {
4189 mdt_object_put(info->mti_env, info->mti_object);
4190 info->mti_object = NULL;
4193 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
4194 mdt_lock_handle_fini(&info->mti_lh[i]);
4195 info->mti_env = NULL;
4196 info->mti_pill = NULL;
4197 info->mti_exp = NULL;
4198 info->mti_mdt = NULL;
4200 if (unlikely(info->mti_big_buf.lb_buf != NULL))
4201 lu_buf_free(&info->mti_big_buf);
4204 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
4206 struct mdt_thread_info *mti;
4208 mti = mdt_th_info(tsi->tsi_env);
4209 LASSERT(mti != NULL);
4211 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4212 if (tsi->tsi_corpus != NULL) {
4213 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4214 lu_object_get(tsi->tsi_corpus);
4216 mti->mti_body = tsi->tsi_mdt_body;
4217 mti->mti_dlm_req = tsi->tsi_dlm_req;
4222 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4224 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4226 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4227 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4229 return tgt_connect(tsi);
4232 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4233 struct mdt_thread_info *info,
4234 struct ldlm_lock **lockp, __u64 flags)
4236 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4239 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4240 struct mdt_thread_info *info,
4241 struct ldlm_lock **lockp, __u64 flags)
4243 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4247 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4248 struct ldlm_lock **lockp,
4249 struct mdt_lock_handle *lh,
4250 __u64 flags, int result)
4252 struct ptlrpc_request *req = mdt_info_req(info);
4253 struct ldlm_lock *lock = *lockp;
4254 struct ldlm_lock *new_lock;
4256 /* If possible resent found a lock, @lh is set to its handle */
4257 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4259 if (new_lock == NULL) {
4260 if (flags & LDLM_FL_INTENT_ONLY) {
4262 } else if (flags & LDLM_FL_RESENT) {
4263 /* Lock is pinned by ldlm_handle_enqueue0() as it is a
4264 * resend case, however, it could be already destroyed
4265 * due to client eviction or a raced cancel RPC.
4267 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4268 lh->mlh_reg_lh.cookie);
4271 CERROR("%s: Invalid lockh=%#llx flags=%#llx fid1="DFID" fid2="DFID": rc = %d\n",
4272 mdt_obd_name(info->mti_mdt),
4273 lh->mlh_reg_lh.cookie, flags,
4274 PFID(&info->mti_tmp_fid1),
4275 PFID(&info->mti_tmp_fid2), result);
4278 lh->mlh_reg_lh.cookie = 0;
4283 * If we've already given this lock to a client once, then we should
4284 * have no readers or writers. Otherwise, we should have one reader
4285 * _or_ writer ref (which will be zeroed below) before returning the
4288 if (new_lock->l_export == req->rq_export) {
4289 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4291 LASSERT(new_lock->l_export == NULL);
4292 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4297 if (new_lock->l_export == req->rq_export) {
4299 * Already gave this to the client, which means that we
4300 * reconstructed a reply.
4302 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4305 LDLM_LOCK_RELEASE(new_lock);
4306 lh->mlh_reg_lh.cookie = 0;
4307 RETURN(ELDLM_LOCK_REPLACED);
4311 * Fixup the lock to be given to the client.
4313 lock_res_and_lock(new_lock);
4314 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4315 * possible blocking AST. */
4316 while (new_lock->l_readers > 0) {
4317 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4318 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4319 new_lock->l_readers--;
4321 while (new_lock->l_writers > 0) {
4322 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4323 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4324 new_lock->l_writers--;
4327 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4328 new_lock->l_blocking_ast = lock->l_blocking_ast;
4329 new_lock->l_completion_ast = lock->l_completion_ast;
4330 if (ldlm_has_dom(new_lock))
4331 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4332 new_lock->l_remote_handle = lock->l_remote_handle;
4333 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4335 unlock_res_and_lock(new_lock);
4337 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4338 &new_lock->l_remote_handle,
4339 &new_lock->l_exp_hash);
4341 LDLM_LOCK_RELEASE(new_lock);
4342 lh->mlh_reg_lh.cookie = 0;
4344 RETURN(ELDLM_LOCK_REPLACED);
4347 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4348 struct ldlm_lock *new_lock,
4349 struct mdt_lock_handle *lh, __u64 flags)
4351 struct ptlrpc_request *req = mdt_info_req(info);
4352 struct ldlm_request *dlmreq;
4354 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4357 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4359 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4360 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4362 if (flags & LDLM_FL_RESENT) {
4363 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4364 lh->mlh_reg_mode = new_lock->l_granted_mode;
4366 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4367 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4368 lh->mlh_reg_lh.cookie);
4373 * If the xid matches, then we know this is a resent request, and allow
4374 * it. (It's probably an OPEN, for which we don't send a lock.
4376 if (req_can_reconstruct(req, NULL) != 0)
4380 * This remote handle isn't enqueued, so we never received or processed
4381 * this request. Clear MSG_RESENT, because it can be handled like any
4382 * normal request now.
4384 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4386 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4387 dlmreq->lock_handle[0].cookie);
4390 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4391 struct mdt_thread_info *info,
4392 struct ldlm_lock **lockp,
4395 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4396 struct ldlm_reply *ldlm_rep = NULL;
4401 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4402 * (for the resend case) or a new lock. Below we will use it to
4403 * replace the original lock.
4405 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4406 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4407 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4408 rc = mdt_object_lock(info, info->mti_object, lhc,
4409 MDS_INODELOCK_XATTR);
4414 rc = mdt_getxattr(info);
4416 if (mdt_info_req(info)->rq_repmsg != NULL)
4417 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4419 if (ldlm_rep == NULL ||
4420 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4421 mdt_object_unlock(info, info->mti_object, lhc, 1);
4425 RETURN(err_serious(-EFAULT));
4428 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4430 /* This is left for interop instead of adding a new interop flag.
4432 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4433 if (ldlm_rep->lock_policy_res2) {
4434 mdt_object_unlock(info, info->mti_object, lhc, 1);
4435 RETURN(ELDLM_LOCK_ABORTED);
4439 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4443 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4444 struct mdt_thread_info *info,
4445 struct ldlm_lock **lockp,
4448 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4450 struct ldlm_reply *ldlm_rep;
4451 struct mdt_body *reqbody;
4452 struct mdt_body *repbody;
4456 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4459 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4462 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4463 repbody->mbo_eadatasize = 0;
4464 repbody->mbo_aclsize = 0;
4468 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4471 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4475 CERROR("%s: unsupported intent %#x\n",
4476 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4477 GOTO(out_shrink, rc = -EINVAL);
4480 rc = mdt_init_ucred(info, reqbody);
4482 GOTO(out_shrink, rc);
4484 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4485 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4487 /* Get lock from request for possible resent case. */
4488 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4490 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4491 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4493 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4494 ldlm_rep->lock_policy_res2 = 0;
4495 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4496 ldlm_rep->lock_policy_res2) {
4497 lhc->mlh_reg_lh.cookie = 0ull;
4498 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4501 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4504 mdt_exit_ucred(info);
4506 mdt_client_compatibility(info);
4507 rc2 = mdt_fix_reply(info);
4513 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4514 struct mdt_thread_info *info,
4515 struct ldlm_lock **lockp,
4518 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4519 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4520 struct layout_intent *intent;
4521 struct ldlm_reply *ldlm_rep;
4522 struct lu_fid *fid = &info->mti_tmp_fid2;
4523 struct mdt_object *obj = NULL;
4524 int layout_size = 0;
4525 struct lu_buf *buf = &layout.mlc_buf;
4530 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4532 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4536 CDEBUG(D_INFO, DFID "got layout change request from client: "
4537 "opc:%u flags:%#x extent "DEXT"\n",
4538 PFID(fid), intent->li_opc, intent->li_flags,
4539 PEXT(&intent->li_extent));
4541 switch (intent->li_opc) {
4542 case LAYOUT_INTENT_TRUNC:
4543 case LAYOUT_INTENT_WRITE:
4544 layout.mlc_opc = MD_LAYOUT_WRITE;
4545 layout.mlc_intent = intent;
4547 case LAYOUT_INTENT_ACCESS:
4549 case LAYOUT_INTENT_READ:
4550 case LAYOUT_INTENT_GLIMPSE:
4551 case LAYOUT_INTENT_RELEASE:
4552 case LAYOUT_INTENT_RESTORE:
4553 CERROR("%s: Unsupported layout intent opc %d\n",
4554 mdt_obd_name(info->mti_mdt), intent->li_opc);
4557 CERROR("%s: Unknown layout intent opc %d\n",
4558 mdt_obd_name(info->mti_mdt), intent->li_opc);
4562 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4564 RETURN(PTR_ERR(obj));
4566 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4567 /* if layout is going to be changed don't use the current EA
4568 * size but the maximum one. That buffer will be shrinked
4569 * to the actual size in req_capsule_shrink() before reply.
4571 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4572 layout_size = info->mti_mdt->mdt_max_mdsize;
4574 layout_size = mdt_attr_get_eabuf_size(info, obj);
4575 if (layout_size < 0)
4576 GOTO(out, rc = layout_size);
4578 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4579 info->mti_mdt->mdt_max_mdsize = layout_size;
4581 CDEBUG(D_INFO, "%s: layout_size %d\n",
4582 mdt_obd_name(info->mti_mdt), layout_size);
4586 * set reply buffer size, so that ldlm_handle_enqueue0()->
4587 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4589 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4591 rc = req_capsule_server_pack(info->mti_pill);
4595 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4597 GOTO(out, rc = -EPROTO);
4599 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4601 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4602 if (layout.mlc_opc == MD_LAYOUT_NOP)
4605 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4609 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4610 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4616 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4617 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4619 buf->lb_len = req_capsule_get_size(info->mti_pill,
4620 &RMF_EADATA, RCL_CLIENT);
4622 * If it's a replay of layout write intent RPC, the client has
4623 * saved the extended lovea when it get reply then.
4625 if (buf->lb_len > 0)
4626 mdt_fix_lov_magic(info, buf->lb_buf);
4629 /* Get lock from request for possible resent case. */
4630 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4631 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4634 * Instantiate some layout components, if @buf contains lovea, then it's
4635 * a replay of the layout intent write RPC.
4637 rc = mdt_layout_change(info, obj, lhc, &layout);
4638 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4640 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4641 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4642 if (rc == ELDLM_LOCK_REPLACED &&
4643 (*lockp)->l_granted_mode == LCK_EX)
4644 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4649 mdt_object_put(info->mti_env, obj);
4653 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4654 struct mdt_thread_info *info,
4655 struct ldlm_lock **lockp,
4658 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4659 struct ldlm_reply *rep = NULL;
4662 struct ptlrpc_request *req = mdt_info_req(info);
4664 static const struct req_format *intent_fmts[REINT_MAX] = {
4665 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4666 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4671 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4675 /* Get lock from request for possible resent case. */
4676 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4678 rc = mdt_reint_internal(info, lhc, opc);
4680 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4681 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4683 /* Check whether the reply has been packed successfully. */
4684 if (mdt_info_req(info)->rq_repmsg != NULL)
4685 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4690 RETURN(err_serious(-EFAULT));
4693 /* MDC expects this in any case */
4695 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4697 /* the open lock or the lock for cross-ref object should be
4698 * returned to the client */
4699 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4700 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4701 rep->lock_policy_res2 = 0;
4702 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4706 rep->lock_policy_res2 = clear_serious(rc);
4708 if (rep->lock_policy_res2 == -ENOENT &&
4709 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4710 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4711 rep->lock_policy_res2 = 0;
4713 lhc->mlh_reg_lh.cookie = 0ull;
4714 if (rc == -ENOTCONN || rc == -ENODEV ||
4715 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4717 * If it is the disconnect error (ENODEV & ENOCONN), the error
4718 * will be returned by rq_status, and client at ptlrpc layer
4719 * will detect this, then disconnect, reconnect the import
4720 * immediately, instead of impacting the following the rpc.
4725 * For other cases, the error will be returned by intent, and client
4726 * will retrieve the result from intent.
4728 RETURN(ELDLM_LOCK_ABORTED);
4731 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4732 struct mdt_thread_info *info,
4733 struct ldlm_lock **lockp,
4734 u64 flags /* LDLM_FL_* */)
4736 struct req_capsule *pill = info->mti_pill;
4737 struct ptlrpc_request *req = mdt_info_req(info);
4738 const struct req_format *it_format;
4739 int (*it_handler)(enum ldlm_intent_flags,
4740 struct mdt_thread_info *,
4741 struct ldlm_lock **,
4743 enum tgt_handler_flags it_handler_flags = 0;
4744 struct ldlm_reply *rep;
4745 bool check_mdt_object = false;
4751 case IT_OPEN|IT_CREAT:
4753 * OCREAT is not a IS_MUTABLE request since the file may
4754 * already exist. We do the extra check of
4755 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4756 * really need to create the object.
4758 it_format = &RQF_LDLM_INTENT;
4759 it_handler = &mdt_intent_open;
4762 check_mdt_object = true;
4765 it_format = &RQF_LDLM_INTENT_GETATTR;
4766 it_handler = &mdt_intent_getattr;
4767 it_handler_flags = HAS_REPLY;
4770 check_mdt_object = true;
4771 it_format = &RQF_LDLM_INTENT_GETXATTR;
4772 it_handler = &mdt_intent_getxattr;
4773 it_handler_flags = HAS_BODY;
4776 it_format = &RQF_LDLM_INTENT_LAYOUT;
4777 it_handler = &mdt_intent_layout;
4780 it_format = &RQF_LDLM_INTENT;
4781 it_handler = &mdt_intent_glimpse;
4784 it_format = &RQF_LDLM_INTENT;
4785 it_handler = &mdt_intent_brw;
4787 case IT_QUOTA_DQACQ:
4788 case IT_QUOTA_CONN: {
4789 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4792 RETURN(-EOPNOTSUPP);
4794 if (mdt_rdonly(req->rq_export))
4797 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4798 /* pass the request to quota master */
4799 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4800 mdt_info_req(info), lockp,
4805 CERROR("%s: unknown intent code %#x\n",
4806 mdt_obd_name(info->mti_mdt), it_opc);
4810 req_capsule_extend(pill, it_format);
4812 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4816 if (unlikely(info->mti_object == NULL && check_mdt_object))
4819 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4822 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4824 /* execute policy */
4825 rc = (*it_handler)(it_opc, info, lockp, flags);
4827 /* Check whether the reply has been packed successfully. */
4828 if (req->rq_repmsg != NULL) {
4829 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4830 rep->lock_policy_res2 =
4831 ptlrpc_status_hton(rep->lock_policy_res2);
4837 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4838 enum ldlm_intent_flags it_opc)
4840 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4842 /* update stats when IT code is known */
4843 if (srv_stats != NULL)
4844 lprocfs_counter_incr(srv_stats,
4845 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4846 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4849 static int mdt_intent_policy(const struct lu_env *env,
4850 struct ldlm_namespace *ns,
4851 struct ldlm_lock **lockp,
4853 enum ldlm_mode mode,
4854 __u64 flags, void *data)
4856 struct tgt_session_info *tsi;
4857 struct mdt_thread_info *info;
4858 struct ptlrpc_request *req = req_cookie;
4859 struct ldlm_intent *it;
4860 struct req_capsule *pill;
4861 const struct ldlm_lock_desc *ldesc;
4866 LASSERT(req != NULL);
4868 tsi = tgt_ses_info(env);
4870 info = tsi2mdt_info(tsi);
4871 LASSERT(info != NULL);
4872 pill = info->mti_pill;
4873 LASSERT(pill->rc_req == req);
4874 ldesc = &info->mti_dlm_req->lock_desc;
4876 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4877 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4878 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4880 mdt_ptlrpc_stats_update(req, it->opc);
4881 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4885 /* Lock without inodebits makes no sense and will oops
4886 * later in ldlm. Let's check it now to see if we have
4887 * ibits corrupted somewhere in mdt_intent_opc().
4888 * The case for client miss to set ibits has been
4889 * processed by others. */
4890 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4891 ldesc->l_policy_data.l_inodebits.bits != 0));
4893 rc = err_serious(-EFAULT);
4895 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4896 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4897 struct ldlm_reply *rep;
4899 /* No intent was provided but INTENT flag is set along with
4900 * DOM bit, this is considered as GLIMPSE request.
4901 * This logic is common for MDT and OST glimpse
4903 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4904 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4905 /* Check whether the reply has been packed successfully. */
4906 if (req->rq_repmsg != NULL) {
4907 rep = req_capsule_server_get(info->mti_pill,
4909 rep->lock_policy_res2 =
4910 ptlrpc_status_hton(rep->lock_policy_res2);
4913 /* No intent was provided */
4914 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4915 rc = req_capsule_server_pack(pill);
4917 rc = err_serious(rc);
4919 mdt_thread_info_fini(info);
4923 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4925 struct seq_server_site *ss = mdt_seq_site(mdt);
4927 if (ss->ss_node_id == 0)
4930 if (ss->ss_client_seq != NULL) {
4931 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4932 ss->ss_client_seq->lcs_exp = NULL;
4935 if (ss->ss_server_fld != NULL) {
4936 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4937 ss->ss_server_fld->lsf_control_exp = NULL;
4941 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4943 struct seq_server_site *ss = mdt_seq_site(mdt);
4948 if (ss->ss_server_seq != NULL)
4949 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4952 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4954 mdt_seq_fini_cli(mdt);
4955 mdt_deregister_seq_exp(mdt);
4957 return seq_site_fini(env, mdt_seq_site(mdt));
4961 * It will retrieve its FLDB entries from MDT0, and it only happens
4962 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4963 * and it needs to refresh FLDB from the MDT0.
4965 static int mdt_register_lwp_callback(void *data)
4968 struct mdt_device *mdt = data;
4969 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4973 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4975 rc = lu_env_init(&env, LCT_MD_THREAD);
4977 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4981 /* Allocate new sequence now to avoid creating local transaction
4982 * in the normal transaction process */
4983 rc = seq_server_check_and_alloc_super(&env,
4984 mdt_seq_site(mdt)->ss_server_seq);
4989 rc = fld_update_from_controller(&env, fld);
4991 CERROR("%s: cannot update controller: rc = %d\n",
4992 mdt_obd_name(mdt), rc);
5001 static int mdt_register_seq_exp(struct mdt_device *mdt)
5003 struct seq_server_site *ss = mdt_seq_site(mdt);
5004 char *lwp_name = NULL;
5007 if (ss->ss_node_id == 0)
5010 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
5011 if (lwp_name == NULL)
5012 GOTO(out_free, rc = -ENOMEM);
5014 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
5018 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
5023 rc = lustre_register_lwp_item(lwp_name,
5024 &ss->ss_server_fld->lsf_control_exp,
5025 mdt_register_lwp_callback, mdt);
5027 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
5028 ss->ss_client_seq->lcs_exp = NULL;
5032 if (lwp_name != NULL)
5033 OBD_FREE(lwp_name, MAX_OBD_NAME);
5039 * Init client sequence manager which is used by local MDS to talk to sequence
5040 * controller on remote node.
5042 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
5044 struct seq_server_site *ss = mdt_seq_site(mdt);
5048 /* check if this is adding the first MDC and controller is not yet
5050 OBD_ALLOC_PTR(ss->ss_client_seq);
5051 if (ss->ss_client_seq == NULL)
5054 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
5055 if (prefix == NULL) {
5056 OBD_FREE_PTR(ss->ss_client_seq);
5057 ss->ss_client_seq = NULL;
5061 /* Note: seq_client_fini will be called in seq_site_fini */
5062 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
5063 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
5064 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
5066 OBD_FREE(prefix, MAX_OBD_NAME + 5);
5068 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
5071 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
5073 struct seq_server_site *ss;
5077 ss = mdt_seq_site(mdt);
5078 /* init sequence controller server(MDT0) */
5079 if (ss->ss_node_id == 0) {
5080 OBD_ALLOC_PTR(ss->ss_control_seq);
5081 if (ss->ss_control_seq == NULL)
5084 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
5085 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
5088 GOTO(out_seq_fini, rc);
5091 /* Init normal sequence server */
5092 OBD_ALLOC_PTR(ss->ss_server_seq);
5093 if (ss->ss_server_seq == NULL)
5094 GOTO(out_seq_fini, rc = -ENOMEM);
5096 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
5097 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
5099 GOTO(out_seq_fini, rc);
5101 /* init seq client for seq server to talk to seq controller(MDT0) */
5102 rc = mdt_seq_init_cli(env, mdt);
5104 GOTO(out_seq_fini, rc);
5106 if (ss->ss_node_id != 0)
5107 /* register controller export through lwp */
5108 rc = mdt_register_seq_exp(mdt);
5113 mdt_seq_fini(env, mdt);
5121 static int mdt_fld_fini(const struct lu_env *env,
5122 struct mdt_device *m)
5124 struct seq_server_site *ss = mdt_seq_site(m);
5127 if (ss && ss->ss_server_fld) {
5128 fld_server_fini(env, ss->ss_server_fld);
5129 OBD_FREE_PTR(ss->ss_server_fld);
5130 ss->ss_server_fld = NULL;
5136 static int mdt_fld_init(const struct lu_env *env,
5138 struct mdt_device *m)
5140 struct seq_server_site *ss;
5144 ss = mdt_seq_site(m);
5146 OBD_ALLOC_PTR(ss->ss_server_fld);
5147 if (ss->ss_server_fld == NULL)
5148 RETURN(rc = -ENOMEM);
5150 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
5153 OBD_FREE_PTR(ss->ss_server_fld);
5154 ss->ss_server_fld = NULL;
5161 static void mdt_stack_pre_fini(const struct lu_env *env,
5162 struct mdt_device *m, struct lu_device *top)
5164 struct lustre_cfg_bufs *bufs;
5165 struct lustre_cfg *lcfg;
5166 struct mdt_thread_info *info;
5171 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5172 LASSERT(info != NULL);
5174 bufs = &info->mti_u.bufs;
5176 LASSERT(m->mdt_child_exp);
5177 LASSERT(m->mdt_child_exp->exp_obd);
5179 /* process cleanup, pass mdt obd name to get obd umount flags */
5180 /* XXX: this is needed because all layers are referenced by
5181 * objects (some of them are pinned by osd, for example *
5182 * the proper solution should be a model where object used
5183 * by osd only doesn't have mdt/mdd slices -bzzz */
5184 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5185 lustre_cfg_bufs_set_string(bufs, 1, NULL);
5186 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5189 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
5191 top->ld_ops->ldo_process_config(env, top, lcfg);
5192 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5196 static void mdt_stack_fini(const struct lu_env *env,
5197 struct mdt_device *m, struct lu_device *top)
5199 struct obd_device *obd = mdt2obd_dev(m);
5200 struct lustre_cfg_bufs *bufs;
5201 struct lustre_cfg *lcfg;
5202 struct mdt_thread_info *info;
5206 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5207 LASSERT(info != NULL);
5209 lu_dev_del_linkage(top->ld_site, top);
5211 lu_site_purge(env, top->ld_site, -1);
5213 bufs = &info->mti_u.bufs;
5214 /* process cleanup, pass mdt obd name to get obd umount flags */
5215 /* another purpose is to let all layers to release their objects */
5216 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5221 lustre_cfg_bufs_set_string(bufs, 1, flags);
5222 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5225 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5228 top->ld_ops->ldo_process_config(env, top, lcfg);
5229 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5231 lu_site_purge(env, top->ld_site, -1);
5233 m->mdt_child = NULL;
5234 m->mdt_bottom = NULL;
5236 obd_disconnect(m->mdt_child_exp);
5237 m->mdt_child_exp = NULL;
5239 obd_disconnect(m->mdt_bottom_exp);
5240 m->mdt_child_exp = NULL;
5243 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5244 const char *next, struct obd_export **exp)
5246 struct obd_connect_data *data = NULL;
5247 struct obd_device *obd;
5251 OBD_ALLOC_PTR(data);
5253 GOTO(out, rc = -ENOMEM);
5255 obd = class_name2obd(next);
5257 CERROR("%s: can't locate next device: %s\n",
5258 mdt_obd_name(m), next);
5259 GOTO(out, rc = -ENOTCONN);
5262 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5263 data->ocd_version = LUSTRE_VERSION_CODE;
5265 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5267 CERROR("%s: cannot connect to next dev %s (%d)\n",
5268 mdt_obd_name(m), next, rc);
5278 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5279 struct lustre_cfg *cfg)
5281 char *dev = lustre_cfg_string(cfg, 0);
5282 int rc, name_size, uuid_size;
5283 char *name, *uuid, *p;
5284 struct lustre_cfg_bufs *bufs;
5285 struct lustre_cfg *lcfg;
5286 struct obd_device *obd;
5287 struct lustre_profile *lprof;
5288 struct lu_site *site;
5291 /* in 1.8 we had the only device in the stack - MDS.
5292 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5293 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5294 * to generate names and setup MDT, MDD. MDT will be using
5295 * generated name to connect to MDD. for MDD the next device
5296 * will be LOD with name taken from so called "profile" which
5297 * is generated by mount_option line
5299 * 1.8 MGS generates config. commands like this:
5300 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5301 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5302 * 2.0 MGS generates config. commands like this:
5303 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5304 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5305 * 3:lustre-MDT0000-mdtlov 4:f
5307 * we generate MDD name from MDT one, just replacing T with D
5309 * after all the preparations, the logical equivalent will be
5310 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5311 * 3:lustre-MDT0000-mdtlov 4:f
5312 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5313 * 3:lustre-MDD0000 4:f
5315 * notice we build the stack from down to top: MDD first, then MDT */
5317 name_size = MAX_OBD_NAME;
5318 uuid_size = MAX_OBD_NAME;
5320 OBD_ALLOC(name, name_size);
5321 OBD_ALLOC(uuid, uuid_size);
5322 if (name == NULL || uuid == NULL)
5323 GOTO(cleanup_mem, rc = -ENOMEM);
5325 OBD_ALLOC_PTR(bufs);
5327 GOTO(cleanup_mem, rc = -ENOMEM);
5330 p = strstr(name, "-MDT");
5332 GOTO(free_bufs, rc = -ENOMEM);
5335 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5337 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5338 if (lprof == NULL || lprof->lp_dt == NULL) {
5339 CERROR("can't find the profile: %s\n",
5340 lustre_cfg_string(cfg, 0));
5341 GOTO(free_bufs, rc = -EINVAL);
5344 lustre_cfg_bufs_reset(bufs, name);
5345 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5346 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5347 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5349 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5351 GOTO(put_profile, rc = -ENOMEM);
5352 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5354 rc = class_attach(lcfg);
5356 GOTO(lcfg_cleanup, rc);
5358 obd = class_name2obd(name);
5360 CERROR("Can not find obd %s (%s in config)\n",
5361 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5362 GOTO(lcfg_cleanup, rc = -EINVAL);
5365 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5367 lustre_cfg_bufs_reset(bufs, name);
5368 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5369 lustre_cfg_bufs_set_string(bufs, 2, dev);
5370 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5372 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5374 GOTO(class_detach, rc = -ENOMEM);
5375 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5377 rc = class_setup(obd, lcfg);
5379 GOTO(class_detach, rc);
5381 /* connect to MDD we just setup */
5382 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5384 GOTO(class_detach, rc);
5386 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5388 LASSERT(mdt_lu_site(mdt) == NULL);
5389 mdt->mdt_lu_dev.ld_site = site;
5390 site->ls_top_dev = &mdt->mdt_lu_dev;
5391 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5393 /* now connect to bottom OSD */
5394 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5395 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5397 GOTO(class_detach, rc);
5399 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5401 rc = lu_env_refill((struct lu_env *)env);
5403 CERROR("Failure to refill session: '%d'\n", rc);
5405 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5410 class_detach(obd, lcfg);
5412 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5414 class_put_profile(lprof);
5419 OBD_FREE(name, name_size);
5421 OBD_FREE(uuid, uuid_size);
5425 /* setup quota master target on MDT0 */
5426 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5427 struct lustre_cfg *cfg)
5429 struct obd_device *obd;
5430 char *dev = lustre_cfg_string(cfg, 0);
5431 char *qmtname, *uuid, *p;
5432 struct lustre_cfg_bufs *bufs;
5433 struct lustre_cfg *lcfg;
5434 struct lustre_profile *lprof;
5435 struct obd_connect_data *data;
5439 LASSERT(mdt->mdt_qmt_exp == NULL);
5440 LASSERT(mdt->mdt_qmt_dev == NULL);
5442 /* quota master is on MDT0 only for now */
5443 if (mdt->mdt_seq_site.ss_node_id != 0)
5446 /* MGS generates config commands which look as follows:
5447 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5448 * 3:lustre-MDT0000-mdtlov 4:f
5450 * We generate the QMT name from the MDT one, just replacing MD with QM
5451 * after all the preparations, the logical equivalent will be:
5452 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5453 * 3:lustre-MDT0000-osd 4:f */
5454 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5455 OBD_ALLOC(uuid, UUID_MAX);
5456 OBD_ALLOC_PTR(bufs);
5457 OBD_ALLOC_PTR(data);
5458 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5459 GOTO(cleanup_mem, rc = -ENOMEM);
5461 strcpy(qmtname, dev);
5462 p = strstr(qmtname, "-MDT");
5464 GOTO(cleanup_mem, rc = -ENOMEM);
5465 /* replace MD with QM */
5469 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5471 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5472 if (lprof == NULL || lprof->lp_dt == NULL) {
5473 CERROR("can't find profile for %s\n",
5474 lustre_cfg_string(cfg, 0));
5475 GOTO(cleanup_mem, rc = -EINVAL);
5478 lustre_cfg_bufs_reset(bufs, qmtname);
5479 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5480 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5481 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5483 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5485 GOTO(put_profile, rc = -ENOMEM);
5486 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5488 rc = class_attach(lcfg);
5490 GOTO(lcfg_cleanup, rc);
5492 obd = class_name2obd(qmtname);
5494 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5495 lustre_cfg_string(cfg, 0));
5496 GOTO(lcfg_cleanup, rc = -EINVAL);
5499 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5501 lustre_cfg_bufs_reset(bufs, qmtname);
5502 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5503 lustre_cfg_bufs_set_string(bufs, 2, dev);
5505 /* for quota, the next device should be the OSD device */
5506 lustre_cfg_bufs_set_string(bufs, 3,
5507 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5509 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5511 GOTO(class_detach, rc = -ENOMEM);
5512 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5514 rc = class_setup(obd, lcfg);
5516 GOTO(class_detach, rc);
5518 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5520 /* configure local quota objects */
5521 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5524 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5528 GOTO(class_cleanup, rc);
5530 /* connect to quota master target */
5531 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5532 data->ocd_version = LUSTRE_VERSION_CODE;
5533 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5536 CERROR("cannot connect to quota master device %s (%d)\n",
5538 GOTO(class_cleanup, rc);
5544 class_manual_cleanup(obd);
5545 mdt->mdt_qmt_dev = NULL;
5546 GOTO(lcfg_cleanup, rc);
5550 class_detach(obd, lcfg);
5552 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5554 class_put_profile(lprof);
5559 OBD_FREE(qmtname, MAX_OBD_NAME);
5561 OBD_FREE(uuid, UUID_MAX);
5567 /* Shutdown quota master target associated with mdt */
5568 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5572 if (mdt->mdt_qmt_exp == NULL)
5574 LASSERT(mdt->mdt_qmt_dev != NULL);
5576 /* the qmt automatically shuts down when the mdt disconnects */
5577 obd_disconnect(mdt->mdt_qmt_exp);
5578 mdt->mdt_qmt_exp = NULL;
5579 mdt->mdt_qmt_dev = NULL;
5583 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5584 * for now. This will be removed along with converting rest of MDT code
5585 * to use tgt_session_info */
5586 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5588 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5591 if (unlikely(info->mti_object == NULL))
5594 rc = mdt_getxattr(info);
5596 mdt_thread_info_fini(info);
5600 static int mdt_llog_open(struct tgt_session_info *tsi)
5604 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5605 RETURN(err_serious(-EACCES));
5607 RETURN(tgt_llog_open(tsi));
5610 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5611 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5612 #define OST_BRW_READ OST_READ
5613 #define OST_BRW_WRITE OST_WRITE
5615 static struct tgt_handler mdt_tgt_handlers[] = {
5616 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5617 0, MDS_CONNECT, mdt_tgt_connect,
5618 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5619 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5620 0, MDS_DISCONNECT, tgt_disconnect,
5621 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5622 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5623 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5624 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5625 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5626 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5627 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5628 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5630 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5631 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5632 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5633 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5634 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5635 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5636 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5637 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5639 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5640 mdt_hsm_ct_register),
5641 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5642 mdt_hsm_ct_unregister),
5643 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5645 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5647 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5648 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5650 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5653 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5656 static struct tgt_handler mdt_io_ops[] = {
5657 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5659 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5661 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5662 OST_PUNCH, mdt_punch_hdl,
5664 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5665 TGT_OST_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, OST_FALLOCATE,
5667 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5670 static struct tgt_handler mdt_sec_ctx_ops[] = {
5671 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5672 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5673 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5676 static struct tgt_handler mdt_quota_ops[] = {
5677 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5680 static struct tgt_handler mdt_llog_handlers[] = {
5681 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5682 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5683 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5684 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5687 static struct tgt_opc_slice mdt_common_slice[] = {
5689 .tos_opc_start = MDS_FIRST_OPC,
5690 .tos_opc_end = MDS_LAST_OPC,
5691 .tos_hs = mdt_tgt_handlers
5694 .tos_opc_start = OBD_FIRST_OPC,
5695 .tos_opc_end = OBD_LAST_OPC,
5696 .tos_hs = tgt_obd_handlers
5699 .tos_opc_start = LDLM_FIRST_OPC,
5700 .tos_opc_end = LDLM_LAST_OPC,
5701 .tos_hs = tgt_dlm_handlers
5704 .tos_opc_start = SEC_FIRST_OPC,
5705 .tos_opc_end = SEC_LAST_OPC,
5706 .tos_hs = mdt_sec_ctx_ops
5709 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5710 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5711 .tos_hs = tgt_out_handlers
5714 .tos_opc_start = FLD_FIRST_OPC,
5715 .tos_opc_end = FLD_LAST_OPC,
5716 .tos_hs = fld_handlers
5719 .tos_opc_start = SEQ_FIRST_OPC,
5720 .tos_opc_end = SEQ_LAST_OPC,
5721 .tos_hs = seq_handlers
5724 .tos_opc_start = QUOTA_DQACQ,
5725 .tos_opc_end = QUOTA_LAST_OPC,
5726 .tos_hs = mdt_quota_ops
5729 .tos_opc_start = LLOG_FIRST_OPC,
5730 .tos_opc_end = LLOG_LAST_OPC,
5731 .tos_hs = mdt_llog_handlers
5734 .tos_opc_start = LFSCK_FIRST_OPC,
5735 .tos_opc_end = LFSCK_LAST_OPC,
5736 .tos_hs = tgt_lfsck_handlers
5739 .tos_opc_start = OST_FIRST_OPC,
5740 .tos_opc_end = OST_LAST_OPC,
5741 .tos_hs = mdt_io_ops
5748 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5750 struct md_device *next = m->mdt_child;
5751 struct lu_device *d = &m->mdt_lu_dev;
5752 struct obd_device *obd = mdt2obd_dev(m);
5753 struct lfsck_stop stop;
5756 stop.ls_status = LS_PAUSED;
5758 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5760 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5762 mdt_restriper_stop(m);
5763 ping_evictor_stop();
5765 /* Remove the HSM /proc entry so the coordinator cannot be
5766 * restarted by a user while it's shutting down.
5768 mdt_hsm_cdt_stop(m);
5770 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5771 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5773 if (m->mdt_namespace != NULL)
5774 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5775 d->ld_obd->obd_force);
5777 obd_exports_barrier(obd);
5778 obd_zombie_barrier();
5780 mdt_quota_fini(env, m);
5782 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5784 /* Calling the cleanup functions in the same order as in the mdt_init0
5787 mdt_tunables_fini(m);
5789 target_recovery_fini(obd);
5790 upcall_cache_cleanup(m->mdt_identity_cache);
5791 m->mdt_identity_cache = NULL;
5793 mdt_fs_cleanup(env, m);
5795 tgt_fini(env, &m->mdt_lut);
5797 mdt_hsm_cdt_fini(m);
5799 if (m->mdt_los != NULL) {
5800 local_oid_storage_fini(env, m->mdt_los);
5804 if (m->mdt_namespace != NULL) {
5805 ldlm_namespace_free_post(m->mdt_namespace);
5806 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5809 if (m->mdt_md_root != NULL) {
5810 mdt_object_put(env, m->mdt_md_root);
5811 m->mdt_md_root = NULL;
5814 mdt_seq_fini(env, m);
5816 mdt_fld_fini(env, m);
5821 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5823 LASSERT(atomic_read(&d->ld_ref) == 0);
5825 server_put_mount(mdt_obd_name(m), true);
5830 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5832 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5833 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5835 const struct dt_device_param *dt_conf;
5836 struct mdt_thread_info *info;
5837 struct obd_device *obd;
5838 const char *dev = lustre_cfg_string(cfg, 0);
5839 const char *num = lustre_cfg_string(cfg, 2);
5840 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5841 struct lustre_mount_info *lmi = NULL;
5842 struct lustre_sb_info *lsi;
5844 struct seq_server_site *ss_site;
5845 const char *identity_upcall = "NONE";
5846 struct md_device *next;
5853 lu_device_init(&m->mdt_lu_dev, ldt);
5855 * Environment (env) might be missing mdt_thread_key values at that
5856 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5859 * Usually device allocation path doesn't use module key values, but
5860 * mdt has to do a lot of work here, so allocate key value.
5862 rc = lu_env_refill((struct lu_env *)env);
5866 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5867 LASSERT(info != NULL);
5869 obd = class_name2obd(dev);
5870 LASSERT(obd != NULL);
5872 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5873 m->mdt_opts.mo_evict_tgt_nids = 1;
5874 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5876 lmi = server_get_mount(dev);
5878 CERROR("Cannot get mount info for %s!\n", dev);
5881 lsi = s2lsi(lmi->lmi_sb);
5882 LASSERT(lsi->lsi_lmd);
5883 /* CMD is supported only in IAM mode */
5885 rc = kstrtol(num, 10, &node_id);
5889 obd->u.obt.obt_magic = OBT_MAGIC;
5890 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5891 m->mdt_skip_lfsck = 1;
5894 /* Just try to get a DoM lock by default. Otherwise, having a group
5895 * lock granted, it may get blocked for a long time. */
5896 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5897 /* DoM files are read at open and data is packed in the reply */
5898 m->mdt_opts.mo_dom_read_open = 1;
5900 m->mdt_squash.rsi_uid = 0;
5901 m->mdt_squash.rsi_gid = 0;
5902 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5903 spin_lock_init(&m->mdt_squash.rsi_lock);
5904 spin_lock_init(&m->mdt_lock);
5905 m->mdt_enable_remote_dir = 1;
5906 m->mdt_enable_striped_dir = 1;
5907 m->mdt_enable_dir_migration = 1;
5908 m->mdt_enable_dir_restripe = 0;
5909 m->mdt_enable_dir_auto_split = 0;
5910 m->mdt_enable_remote_dir_gid = 0;
5911 m->mdt_enable_chprojid_gid = 0;
5912 m->mdt_enable_remote_rename = 1;
5913 m->mdt_dir_restripe_nsonly = 1;
5914 m->mdt_enable_remote_subdir_mount = 1;
5916 atomic_set(&m->mdt_mds_mds_conns, 0);
5917 atomic_set(&m->mdt_async_commit_count, 0);
5919 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5920 m->mdt_lu_dev.ld_obd = obd;
5921 /* Set this lu_device to obd for error handling purposes. */
5922 obd->obd_lu_dev = &m->mdt_lu_dev;
5924 /* init the stack */
5925 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5927 CERROR("%s: Can't init device stack, rc %d\n",
5928 mdt_obd_name(m), rc);
5933 ss_site = mdt_seq_site(m);
5934 s->ld_seq_site = ss_site;
5937 /* set server index */
5938 ss_site->ss_node_id = node_id;
5940 /* failover is the default
5941 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5942 * assumed whose ss_node_id == 0 XXX
5944 obd->obd_replayable = 1;
5945 /* No connection accepted until configurations will finish */
5946 obd->obd_no_conn = 1;
5948 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5949 char *str = lustre_cfg_string(cfg, 4);
5950 if (strchr(str, 'n')) {
5951 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5952 obd->obd_replayable = 0;
5956 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5958 GOTO(err_fini_stack, rc);
5960 rc = mdt_seq_init(env, m);
5962 GOTO(err_fini_fld, rc);
5964 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5965 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5966 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5967 LDLM_NAMESPACE_SERVER,
5968 LDLM_NAMESPACE_GREEDY,
5970 if (IS_ERR(m->mdt_namespace)) {
5971 rc = PTR_ERR(m->mdt_namespace);
5972 CERROR("%s: unable to create server namespace: rc = %d\n",
5974 m->mdt_namespace = NULL;
5975 GOTO(err_fini_seq, rc);
5978 m->mdt_namespace->ns_lvbp = m;
5979 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5981 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5982 /* set obd_namespace for compatibility with old code */
5983 obd->obd_namespace = m->mdt_namespace;
5985 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5986 OBD_FAIL_MDS_ALL_REQUEST_NET,
5987 OBD_FAIL_MDS_ALL_REPLY_NET);
5989 GOTO(err_free_ns, rc);
5991 /* Amount of available space excluded from granting and reserved
5992 * for metadata. It is a percentage of the total MDT size. */
5993 tgd->tgd_reserved_pcnt = 10;
5995 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5996 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5998 m->mdt_brw_size = ONE_MB_BRW_SIZE;
6000 rc = mdt_fs_setup(env, m, obd, lsi);
6004 fid.f_seq = FID_SEQ_LOCAL_NAME;
6007 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
6009 GOTO(err_fs_cleanup, rc);
6011 rc = mdt_hsm_cdt_init(m);
6013 CERROR("%s: error initializing coordinator, rc %d\n",
6014 mdt_obd_name(m), rc);
6015 GOTO(err_los_fini, rc);
6018 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
6020 next = m->mdt_child;
6021 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
6023 mntopts = dt_conf->ddp_mntopts;
6025 if (mntopts & MNTOPT_USERXATTR)
6026 m->mdt_opts.mo_user_xattr = 1;
6028 m->mdt_opts.mo_user_xattr = 0;
6030 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
6032 if (mntopts & MNTOPT_ACL)
6033 m->mdt_opts.mo_acl = 1;
6035 m->mdt_opts.mo_acl = 0;
6037 /* XXX: to support suppgid for ACL, we enable identity_upcall
6038 * by default, otherwise, maybe got unexpected -EACCESS. */
6039 if (m->mdt_opts.mo_acl)
6040 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
6042 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
6044 &mdt_identity_upcall_cache_ops);
6045 if (IS_ERR(m->mdt_identity_cache)) {
6046 rc = PTR_ERR(m->mdt_identity_cache);
6047 m->mdt_identity_cache = NULL;
6048 GOTO(err_free_hsm, rc);
6051 rc = mdt_tunables_init(m, dev);
6053 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
6054 GOTO(err_recovery, rc);
6057 rc = mdt_quota_init(env, m, cfg);
6059 GOTO(err_procfs, rc);
6061 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
6062 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
6063 "mdt_ldlm_client", m->mdt_ldlm_client);
6065 ping_evictor_start();
6067 /* recovery will be started upon mdt_prepare()
6068 * when the whole stack is complete and ready
6069 * to serve the requests */
6071 /* Reduce the initial timeout on an MDS because it doesn't need such
6072 * a long timeout as an OST does. Adaptive timeouts will adjust this
6073 * value appropriately. */
6074 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
6075 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
6077 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
6078 m->mdt_lut.lut_local_recovery = 1;
6080 rc = mdt_restriper_start(m);
6082 GOTO(err_ping_evictor, rc);
6087 ping_evictor_stop();
6089 mdt_tunables_fini(m);
6091 upcall_cache_cleanup(m->mdt_identity_cache);
6092 m->mdt_identity_cache = NULL;
6094 mdt_hsm_cdt_fini(m);
6096 local_oid_storage_fini(env, m->mdt_los);
6099 mdt_fs_cleanup(env, m);
6101 /* keep recoverable clients */
6103 target_recovery_fini(obd);
6104 obd_exports_barrier(obd);
6105 obd_zombie_barrier();
6106 tgt_fini(env, &m->mdt_lut);
6108 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
6109 obd->obd_namespace = m->mdt_namespace = NULL;
6111 mdt_seq_fini(env, m);
6113 mdt_fld_fini(env, m);
6115 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
6118 server_put_mount(dev, true);
6122 /* For interoperability, the left element is old parameter, the right one
6123 * is the new version of the parameter, if some parameter is deprecated,
6124 * the new version should be set as NULL. */
6125 static struct cfg_interop_param mdt_interop_param[] = {
6126 { "mdt.group_upcall", NULL },
6127 { "mdt.quota_type", NULL },
6128 { "mdd.quota_type", NULL },
6129 { "mdt.som", NULL },
6130 { "mdt.rootsquash", "mdt.root_squash" },
6131 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
6135 /* used by MGS to process specific configurations */
6136 static int mdt_process_config(const struct lu_env *env,
6137 struct lu_device *d, struct lustre_cfg *cfg)
6139 struct mdt_device *m = mdt_dev(d);
6140 struct md_device *md_next = m->mdt_child;
6141 struct lu_device *next = md2lu_dev(md_next);
6145 switch (cfg->lcfg_command) {
6147 struct obd_device *obd = d->ld_obd;
6148 /* For interoperability */
6149 struct cfg_interop_param *ptr = NULL;
6150 struct lustre_cfg *old_cfg = NULL;
6154 param = lustre_cfg_string(cfg, 1);
6155 if (param == NULL) {
6156 CERROR("param is empty\n");
6161 ptr = class_find_old_param(param, mdt_interop_param);
6163 if (ptr->new_param == NULL) {
6165 CWARN("For interoperability, skip this %s."
6166 " It is obsolete.\n", ptr->old_param);
6170 CWARN("Found old param %s, changed it to %s.\n",
6171 ptr->old_param, ptr->new_param);
6174 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
6181 count = class_modify_config(cfg, PARAM_MDT,
6182 &obd->obd_kset.kobj);
6184 struct coordinator *cdt = &m->mdt_coordinator;
6186 /* is it an HSM var ? */
6187 count = class_modify_config(cfg, PARAM_HSM,
6188 &cdt->cdt_hsm_kobj);
6190 /* we don't understand; pass it on */
6191 rc = next->ld_ops->ldo_process_config(env, next,
6194 rc = count > 0 ? 0 : count;
6196 rc = count > 0 ? 0 : count;
6200 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
6201 cfg->lcfg_buflens));
6205 /* others are passed further */
6206 rc = next->ld_ops->ldo_process_config(env, next, cfg);
6212 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
6213 const struct lu_object_header *hdr,
6214 struct lu_device *d)
6216 struct mdt_object *mo;
6220 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6222 struct lu_object *o;
6223 struct lu_object_header *h;
6226 h = &mo->mot_header;
6227 lu_object_header_init(h);
6228 lu_object_init(o, h, d);
6229 lu_object_add_top(h, o);
6230 o->lo_ops = &mdt_obj_ops;
6231 spin_lock_init(&mo->mot_write_lock);
6232 mutex_init(&mo->mot_som_mutex);
6233 mutex_init(&mo->mot_lov_mutex);
6234 init_rwsem(&mo->mot_dom_sem);
6235 init_rwsem(&mo->mot_open_sem);
6236 atomic_set(&mo->mot_open_count, 0);
6237 mo->mot_restripe_offset = 0;
6238 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6244 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6245 const struct lu_object_conf *unused)
6247 struct mdt_device *d = mdt_dev(o->lo_dev);
6248 struct lu_device *under;
6249 struct lu_object *below;
6253 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6254 PFID(lu_object_fid(o)));
6256 under = &d->mdt_child->md_lu_dev;
6257 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6258 if (below != NULL) {
6259 lu_object_add(o, below);
6266 static void mdt_object_free_rcu(struct rcu_head *head)
6268 struct mdt_object *mo = container_of(head, struct mdt_object,
6269 mot_header.loh_rcu);
6271 kmem_cache_free(mdt_object_kmem, mo);
6274 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6276 struct mdt_object *mo = mdt_obj(o);
6277 struct lu_object_header *h;
6281 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6282 PFID(lu_object_fid(o)));
6284 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6285 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6288 lu_object_header_fini(h);
6289 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6290 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6295 static int mdt_object_print(const struct lu_env *env, void *cookie,
6296 lu_printer_t p, const struct lu_object *o)
6298 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6300 return (*p)(env, cookie,
6301 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6302 mdto, mdto->mot_lov_created ? "lov_created" : "",
6303 mdto->mot_cache_attr ? "cache_attr" : "",
6304 mdto->mot_write_count);
6307 static int mdt_prepare(const struct lu_env *env,
6308 struct lu_device *pdev,
6309 struct lu_device *cdev)
6311 struct mdt_device *mdt = mdt_dev(cdev);
6312 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6313 struct obd_device *obd = cdev->ld_obd;
6320 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6324 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6328 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6332 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6333 /* The LFSCK instance is registered just now, so it must be there when
6334 * register the namespace to such instance. */
6335 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6337 if (mdt->mdt_seq_site.ss_node_id == 0) {
6338 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6339 &mdt->mdt_md_root_fid);
6344 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6346 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6347 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6348 LASSERT(obd->obd_no_conn);
6349 spin_lock(&obd->obd_dev_lock);
6350 obd->obd_no_conn = 0;
6351 spin_unlock(&obd->obd_dev_lock);
6353 if (obd->obd_recovering == 0)
6354 mdt_postrecov(env, mdt);
6359 const struct lu_device_operations mdt_lu_ops = {
6360 .ldo_object_alloc = mdt_object_alloc,
6361 .ldo_process_config = mdt_process_config,
6362 .ldo_prepare = mdt_prepare,
6365 static const struct lu_object_operations mdt_obj_ops = {
6366 .loo_object_init = mdt_object_init,
6367 .loo_object_free = mdt_object_free,
6368 .loo_object_print = mdt_object_print
6371 static int mdt_obd_set_info_async(const struct lu_env *env,
6372 struct obd_export *exp,
6373 __u32 keylen, void *key,
6374 __u32 vallen, void *val,
6375 struct ptlrpc_request_set *set)
6381 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6382 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6389 static inline void mdt_enable_slc(struct mdt_device *mdt)
6391 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6392 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6395 static inline void mdt_disable_slc(struct mdt_device *mdt)
6397 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6398 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6402 * Match client and server connection feature flags.
6404 * Compute the compatibility flags for a connection request based on
6405 * features mutually supported by client and server.
6407 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6408 * must not be updated here, otherwise a partially initialized value may
6409 * be exposed. After the connection request is successfully processed,
6410 * the top-level MDT connect request handler atomically updates the export
6411 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6412 * reply. \see mdt_connect().
6414 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6415 * full struct obd_connect_data. So care must be taken when accessing fields
6416 * that are not present in struct obd_connect_data_v1. See LU-16.
6418 * \param exp the obd_export associated with this client/target pair
6419 * \param mdt the target device for the connection
6420 * \param data stores data for this connect request
6423 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6424 * \retval -EBADE client and server feature requirements are incompatible
6426 static int mdt_connect_internal(const struct lu_env *env,
6427 struct obd_export *exp,
6428 struct mdt_device *mdt,
6429 struct obd_connect_data *data, bool reconnect)
6431 const char *obd_name = mdt_obd_name(mdt);
6432 LASSERT(data != NULL);
6434 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6436 if (mdt->mdt_bottom->dd_rdonly &&
6437 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6438 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6441 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6442 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6444 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6446 if (!mdt->mdt_opts.mo_acl)
6447 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6449 if (!mdt->mdt_opts.mo_user_xattr)
6450 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6452 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6453 data->ocd_brw_size = min(data->ocd_brw_size,
6455 if (data->ocd_brw_size == 0) {
6456 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6457 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6458 "ocd_brw_size unexpectedly zero, network data "
6459 "corruption? Refusing to connect this client\n",
6460 obd_name, exp->exp_client_uuid.uuid,
6461 exp, data->ocd_connect_flags, data->ocd_version,
6462 data->ocd_grant, data->ocd_index);
6467 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6468 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6470 /* client is reporting its page size, for future use */
6471 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6472 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6473 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6474 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6475 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6476 /* ocd_grant_tax_kb is in 1K byte blocks */
6477 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6478 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6481 /* Save connect_data we have so far because tgt_grant_connect()
6482 * uses it to calculate grant, and we want to save the client
6483 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6484 exp->exp_connect_data = *data;
6485 if (OCD_HAS_FLAG(data, GRANT))
6486 tgt_grant_connect(env, exp, data, !reconnect);
6488 if (OCD_HAS_FLAG(data, MAXBYTES))
6489 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6491 /* NB: Disregard the rule against updating
6492 * exp_connect_data.ocd_connect_flags in this case, since
6493 * tgt_client_new() needs to know if this is a lightweight
6494 * connection, and it is safe to expose this flag before
6495 * connection processing completes. */
6496 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6497 spin_lock(&exp->exp_lock);
6498 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6499 spin_unlock(&exp->exp_lock);
6502 data->ocd_version = LUSTRE_VERSION_CODE;
6504 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6505 CWARN("%s: MDS requires FID support, but client not\n",
6510 if (OCD_HAS_FLAG(data, PINGLESS)) {
6511 if (ptlrpc_pinger_suppress_pings()) {
6512 spin_lock(&exp->exp_obd->obd_dev_lock);
6513 list_del_init(&exp->exp_obd_chain_timed);
6514 spin_unlock(&exp->exp_obd->obd_dev_lock);
6516 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6520 data->ocd_max_easize = mdt->mdt_max_ea_size;
6522 /* NB: Disregard the rule against updating
6523 * exp_connect_data.ocd_connect_flags in this case, since
6524 * tgt_client_new() needs to know if this is client supports
6525 * multiple modify RPCs, and it is safe to expose this flag before
6526 * connection processing completes. */
6527 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6528 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6529 spin_lock(&exp->exp_lock);
6530 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6531 spin_unlock(&exp->exp_lock);
6534 if (OCD_HAS_FLAG(data, CKSUM)) {
6535 __u32 cksum_types = data->ocd_cksum_types;
6537 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6539 if (unlikely(data->ocd_cksum_types == 0)) {
6540 CERROR("%s: Connect with checksum support but no "
6541 "ocd_cksum_types is set\n",
6542 exp->exp_obd->obd_name);
6546 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6547 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6548 cksum_types, data->ocd_cksum_types);
6550 /* This client does not support OBD_CONNECT_CKSUM
6551 * fall back to CRC32 */
6552 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6553 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6554 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6557 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6558 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6559 atomic_inc(&mdt->mdt_mds_mds_conns);
6560 mdt_enable_slc(mdt);
6563 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6564 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6569 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6570 struct mdt_thread_info *info,
6571 struct mdt_file_data *mfd)
6573 struct lu_context ses;
6577 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6582 lu_context_enter(&ses);
6584 mdt_ucred(info)->uc_valid = UCRED_OLD;
6585 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6587 lu_context_exit(&ses);
6588 lu_context_fini(&ses);
6594 static int mdt_export_cleanup(struct obd_export *exp)
6596 LIST_HEAD(closing_list);
6597 struct mdt_export_data *med = &exp->exp_mdt_data;
6598 struct obd_device *obd = exp->exp_obd;
6599 struct mdt_device *mdt;
6600 struct mdt_thread_info *info;
6602 struct mdt_file_data *mfd, *n;
6606 spin_lock(&med->med_open_lock);
6607 while (!list_empty(&med->med_open_head)) {
6608 struct list_head *tmp = med->med_open_head.next;
6609 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6611 /* Remove mfd handle so it can't be found again.
6612 * We are consuming the mfd_list reference here. */
6613 class_handle_unhash(&mfd->mfd_open_handle);
6614 list_move_tail(&mfd->mfd_list, &closing_list);
6616 spin_unlock(&med->med_open_lock);
6617 mdt = mdt_dev(obd->obd_lu_dev);
6618 LASSERT(mdt != NULL);
6620 rc = lu_env_init(&env, LCT_MD_THREAD);
6624 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6625 LASSERT(info != NULL);
6626 memset(info, 0, sizeof *info);
6627 info->mti_env = &env;
6628 info->mti_mdt = mdt;
6629 info->mti_exp = exp;
6631 if (!list_empty(&closing_list)) {
6632 struct md_attr *ma = &info->mti_attr;
6634 /* Close any open files (which may also cause orphan
6636 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6637 list_del_init(&mfd->mfd_list);
6638 ma->ma_need = ma->ma_valid = 0;
6640 /* This file is being closed due to an eviction, it
6641 * could have been modified and now dirty regarding to
6642 * HSM archive, check this!
6643 * The logic here is to mark a file dirty if there's a
6644 * chance it was dirtied before the client was evicted,
6645 * so that we don't have to wait for a release attempt
6646 * before finding out the file was actually dirty and
6647 * fail the release. Aggressively marking it dirty here
6648 * will cause the policy engine to attempt to
6649 * re-archive it; when rearchiving, we can compare the
6650 * current version to the HSM data_version and make the
6651 * archive request into a noop if it's not actually
6654 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6655 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6657 /* Don't unlink orphan on failover umount, LU-184 */
6658 if (exp->exp_flags & OBD_OPT_FAILOVER ||
6659 exp->exp_obd->obd_stopping) {
6660 ma->ma_valid = MA_FLAGS;
6661 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6663 ma->ma_valid |= MA_FORCE_LOG;
6664 mdt_mfd_close(info, mfd);
6667 info->mti_mdt = NULL;
6668 /* cleanup client slot early */
6669 /* Do not erase record for recoverable client. */
6670 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6671 tgt_client_del(&env, exp);
6677 static int mdt_obd_disconnect(struct obd_export *exp)
6684 class_export_get(exp);
6686 if (!(exp->exp_flags & OBD_OPT_FORCE))
6687 tgt_grant_sanity_check(exp->exp_obd, __func__);
6689 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6690 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6691 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6693 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6694 mdt_disable_slc(mdt);
6697 rc = server_disconnect_export(exp);
6699 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6701 tgt_grant_discard(exp);
6703 rc = mdt_export_cleanup(exp);
6704 nodemap_del_member(exp);
6705 class_export_put(exp);
6709 /* mds_connect copy */
6710 static int mdt_obd_connect(const struct lu_env *env,
6711 struct obd_export **exp, struct obd_device *obd,
6712 struct obd_uuid *cluuid,
6713 struct obd_connect_data *data,
6716 struct obd_export *lexp;
6717 struct lustre_handle conn = { 0 };
6718 struct mdt_device *mdt;
6720 lnet_nid_t *client_nid = localdata;
6723 LASSERT(env != NULL);
6724 LASSERT(data != NULL);
6726 if (!exp || !obd || !cluuid)
6729 mdt = mdt_dev(obd->obd_lu_dev);
6732 * first, check whether the stack is ready to handle requests
6733 * XXX: probably not very appropriate method is used now
6734 * at some point we should find a better one
6736 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6737 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6738 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6739 rc = obd_get_info(env, mdt->mdt_child_exp,
6740 sizeof(KEY_OSP_CONNECTED),
6741 KEY_OSP_CONNECTED, NULL, NULL);
6744 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6747 rc = class_connect(&conn, obd, cluuid);
6751 lexp = class_conn2export(&conn);
6752 LASSERT(lexp != NULL);
6754 rc = nodemap_add_member(*client_nid, lexp);
6755 if (rc != 0 && rc != -EEXIST)
6758 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6760 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6763 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6764 rc = tgt_client_new(env, lexp);
6766 mdt_export_stats_init(obd, lexp, localdata);
6770 class_disconnect(lexp);
6771 nodemap_del_member(lexp);
6775 /* Because we do not want this export to be evicted by pinger,
6776 * let's not add this export to the timed chain list. */
6777 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6778 spin_lock(&lexp->exp_obd->obd_dev_lock);
6779 list_del_init(&lexp->exp_obd_chain_timed);
6780 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6787 static int mdt_obd_reconnect(const struct lu_env *env,
6788 struct obd_export *exp, struct obd_device *obd,
6789 struct obd_uuid *cluuid,
6790 struct obd_connect_data *data,
6793 lnet_nid_t *client_nid = localdata;
6797 if (exp == NULL || obd == NULL || cluuid == NULL)
6800 rc = nodemap_add_member(*client_nid, exp);
6801 if (rc != 0 && rc != -EEXIST)
6804 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6807 mdt_export_stats_init(obd, exp, localdata);
6809 nodemap_del_member(exp);
6814 /* FIXME: Can we avoid using these two interfaces? */
6815 static int mdt_init_export(struct obd_export *exp)
6817 struct mdt_export_data *med = &exp->exp_mdt_data;
6821 INIT_LIST_HEAD(&med->med_open_head);
6822 spin_lock_init(&med->med_open_lock);
6823 spin_lock(&exp->exp_lock);
6824 exp->exp_connecting = 1;
6825 spin_unlock(&exp->exp_lock);
6827 OBD_ALLOC(exp->exp_used_slots,
6828 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6829 if (exp->exp_used_slots == NULL)
6832 /* self-export doesn't need client data and ldlm initialization */
6833 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6834 &exp->exp_client_uuid)))
6837 rc = tgt_client_alloc(exp);
6841 rc = ldlm_init_export(exp);
6848 tgt_client_free(exp);
6850 OBD_FREE(exp->exp_used_slots,
6851 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6852 exp->exp_used_slots = NULL;
6854 CERROR("%s: Failed to initialize export: rc = %d\n",
6855 exp->exp_obd->obd_name, rc);
6859 static int mdt_destroy_export(struct obd_export *exp)
6863 target_destroy_export(exp);
6864 if (exp->exp_used_slots)
6865 OBD_FREE(exp->exp_used_slots,
6866 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6868 /* destroy can be called from failed obd_setup, so
6869 * checking uuid is safer than obd_self_export */
6870 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6871 &exp->exp_client_uuid)))
6874 ldlm_destroy_export(exp);
6875 tgt_client_free(exp);
6877 LASSERT(list_empty(&exp->exp_outstanding_replies));
6878 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6881 * discard grants once we're sure no more
6882 * interaction with the client is possible
6884 tgt_grant_discard(exp);
6885 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6886 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6888 if (!(exp->exp_flags & OBD_OPT_FORCE))
6889 tgt_grant_sanity_check(exp->exp_obd, __func__);
6894 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6895 struct linkea_data *ldata)
6899 LASSERT(ldata->ld_buf->lb_buf != NULL);
6901 if (!mdt_object_exists(mdt_obj))
6904 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6905 ldata->ld_buf, XATTR_NAME_LINK);
6906 if (rc == -ERANGE) {
6907 /* Buf was too small, figure out what we need. */
6908 lu_buf_free(ldata->ld_buf);
6909 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6910 ldata->ld_buf, XATTR_NAME_LINK);
6913 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6914 if (ldata->ld_buf->lb_buf == NULL)
6916 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6917 ldata->ld_buf, XATTR_NAME_LINK);
6922 return linkea_init_with_rec(ldata);
6926 * Given an MDT object, try to look up the full path to the object.
6927 * Part of the MDT layer implementation of lfs fid2path.
6929 * \param[in] info Per-thread common data shared by MDT level handlers.
6930 * \param[in] obj Object to do path lookup of
6931 * \param[in,out] fp User-provided struct to store path information
6932 * \param[in] root_fid Root FID of current path should reach
6934 * \retval 0 Lookup successful, path information stored in fp
6935 * \retval -EAGAIN Lookup failed, usually because object is being moved
6936 * \retval negative errno if there was a problem
6938 static int mdt_path_current(struct mdt_thread_info *info,
6939 struct mdt_object *obj,
6940 struct getinfo_fid2path *fp,
6941 struct lu_fid *root_fid)
6943 struct mdt_device *mdt = info->mti_mdt;
6944 struct lu_name *tmpname = &info->mti_name;
6945 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6946 struct lu_buf *buf = &info->mti_big_buf;
6947 struct linkea_data ldata = { NULL };
6949 struct mdt_object *mdt_obj;
6950 struct link_ea_header *leh;
6951 struct link_ea_entry *lee;
6958 /* temp buffer for path element, the buffer will be finally freed
6959 * in mdt_thread_info_fini */
6960 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6961 if (buf->lb_buf == NULL)
6965 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6968 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6970 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6971 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6972 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6973 GOTO(out, rc = -ENOENT);
6975 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6977 mdt_object_get(info->mti_env, mdt_obj);
6979 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6980 if (IS_ERR(mdt_obj))
6981 GOTO(out, rc = PTR_ERR(mdt_obj));
6984 if (!mdt_object_exists(mdt_obj)) {
6985 mdt_object_put(info->mti_env, mdt_obj);
6986 GOTO(out, rc = -ENOENT);
6989 if (mdt_object_remote(mdt_obj)) {
6990 mdt_object_put(info->mti_env, mdt_obj);
6991 GOTO(remote_out, rc = -EREMOTE);
6994 rc = mdt_links_read(info, mdt_obj, &ldata);
6996 mdt_object_put(info->mti_env, mdt_obj);
7001 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
7002 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
7003 /* If set, use link #linkno for path lookup, otherwise use
7004 link #0. Only do this for the final path element. */
7005 if (first && fp->gf_linkno < leh->leh_reccount) {
7007 for (count = 0; count < fp->gf_linkno; count++) {
7008 lee = (struct link_ea_entry *)
7009 ((char *)lee + reclen);
7010 linkea_entry_unpack(lee, &reclen, tmpname,
7013 if (fp->gf_linkno < leh->leh_reccount - 1)
7014 /* indicate to user there are more links */
7018 /* Check if it is slave stripes */
7019 rc = mdt_is_dir_stripe(info, mdt_obj);
7020 mdt_object_put(info->mti_env, mdt_obj);
7024 fp->gf_fid = *tmpfid;
7028 /* Pack the name in the end of the buffer */
7029 ptr -= tmpname->ln_namelen;
7030 if (ptr - 1 <= fp->gf_u.gf_path)
7031 GOTO(out, rc = -EOVERFLOW);
7032 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
7035 /* keep the last resolved fid to the client, so the
7036 * client will build the left path on another MDT for
7038 fp->gf_fid = *tmpfid;
7043 /* non-zero will be treated as an error */
7047 ptr++; /* skip leading / */
7048 memmove(fp->gf_u.gf_path, ptr,
7049 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
7056 * Given an MDT object, use mdt_path_current to get the path.
7057 * Essentially a wrapper to retry mdt_path_current a set number of times
7058 * if -EAGAIN is returned (usually because an object is being moved).
7060 * Part of the MDT layer implementation of lfs fid2path.
7062 * \param[in] info Per-thread common data shared by mdt level handlers.
7063 * \param[in] obj Object to do path lookup of
7064 * \param[in,out] fp User-provided struct for arguments and to store path
7067 * \retval 0 Lookup successful, path information stored in fp
7068 * \retval negative errno if there was a problem
7070 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
7071 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
7073 struct mdt_device *mdt = info->mti_mdt;
7078 if (fp->gf_pathlen < 3)
7081 if (root_fid == NULL)
7082 root_fid = &mdt->mdt_md_root_fid;
7084 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
7085 fp->gf_u.gf_path[0] = '\0';
7089 /* Retry multiple times in case file is being moved */
7090 while (tries-- && rc == -EAGAIN)
7091 rc = mdt_path_current(info, obj, fp, root_fid);
7097 * Get the full path of the provided FID, as of changelog record recno.
7099 * This checks sanity and looks up object for user provided FID
7100 * before calling the actual path lookup code.
7102 * Part of the MDT layer implementation of lfs fid2path.
7104 * \param[in] info Per-thread common data shared by mdt level handlers.
7105 * \param[in,out] fp User-provided struct for arguments and to store path
7108 * \retval 0 Lookup successful, path information and recno stored in fp
7109 * \retval -ENOENT, object does not exist
7110 * \retval negative errno if there was a problem
7112 static int mdt_fid2path(struct mdt_thread_info *info,
7113 struct lu_fid *root_fid,
7114 struct getinfo_fid2path *fp)
7116 struct mdt_device *mdt = info->mti_mdt;
7117 struct mdt_object *obj;
7121 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
7122 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
7124 if (!fid_is_sane(&fp->gf_fid))
7127 if (!fid_is_namespace_visible(&fp->gf_fid)) {
7128 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
7129 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
7130 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
7134 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
7137 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
7138 PFID(&fp->gf_fid), rc);
7142 if (mdt_object_remote(obj)) {
7144 } else if (!mdt_object_exists(obj)) {
7147 struct lu_attr la = { 0 };
7148 struct dt_object *dt = mdt_obj2dt(obj);
7150 if (dt && dt->do_ops && dt->do_ops->do_attr_get)
7151 dt_attr_get(info->mti_env, mdt_obj2dt(obj), &la);
7152 if (la.la_valid & LA_FLAGS && la.la_flags & LUSTRE_ENCRYPT_FL)
7153 /* path resolution cannot be carried out on server
7154 * side for encrypted files
7162 mdt_object_put(info->mti_env, obj);
7163 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
7164 PFID(&fp->gf_fid), rc);
7168 rc = mdt_path(info, obj, fp, root_fid);
7170 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
7171 PFID(&fp->gf_fid), fp->gf_u.gf_path,
7172 fp->gf_recno, fp->gf_linkno);
7174 mdt_object_put(info->mti_env, obj);
7179 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
7180 void *val, int vallen)
7182 struct getinfo_fid2path *fpout, *fpin;
7183 struct lu_fid *root_fid = NULL;
7186 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
7189 if (req_capsule_req_need_swab(info->mti_pill))
7190 lustre_swab_fid2path(fpin);
7192 memcpy(fpout, fpin, sizeof(*fpin));
7193 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
7196 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
7197 sizeof(struct lu_fid)) {
7198 /* client sent its root FID, which is normally fileset FID */
7199 root_fid = fpin->gf_u.gf_root_fid;
7200 if (req_capsule_req_need_swab(info->mti_pill))
7201 lustre_swab_lu_fid(root_fid);
7203 if (root_fid != NULL && !fid_is_sane(root_fid))
7207 rc = mdt_fid2path(info, root_fid, fpout);
7211 int mdt_get_info(struct tgt_session_info *tsi)
7221 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7223 CDEBUG(D_IOCTL, "No GETINFO key\n");
7224 RETURN(err_serious(-EFAULT));
7226 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7229 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7230 if (vallen == NULL) {
7231 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7232 tgt_name(tsi->tsi_tgt));
7233 RETURN(err_serious(-EFAULT));
7236 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7238 rc = req_capsule_server_pack(tsi->tsi_pill);
7240 RETURN(err_serious(rc));
7242 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7243 if (valout == NULL) {
7244 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7245 tgt_name(tsi->tsi_tgt));
7246 RETURN(err_serious(-EFAULT));
7249 if (KEY_IS(KEY_FID2PATH)) {
7250 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7252 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7253 mdt_thread_info_fini(info);
7260 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7262 struct obd_ioctl_data *data = karg;
7265 struct mdt_object *obj;
7266 struct mdt_lock_handle *lh;
7270 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7271 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7274 fid = (struct lu_fid *)data->ioc_inlbuf1;
7276 if (!fid_is_sane(fid))
7279 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7281 lh = &mti->mti_lh[MDT_LH_PARENT];
7282 mdt_lock_reg_init(lh, LCK_CR);
7284 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7286 RETURN(PTR_ERR(obj));
7288 if (mdt_object_remote(obj)) {
7291 * before calling version get the correct MDS should be
7292 * fid, this is error to find remote object here
7294 CERROR("nonlocal object "DFID"\n", PFID(fid));
7295 } else if (!mdt_object_exists(obj)) {
7296 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7299 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7300 *(__u64 *)data->ioc_inlbuf2 = version;
7303 mdt_object_unlock_put(mti, obj, lh, 1);
7307 /* ioctls on obd dev */
7308 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7309 void *karg, void __user *uarg)
7312 struct obd_device *obd = exp->exp_obd;
7313 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7314 struct dt_device *dt = mdt->mdt_bottom;
7318 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7319 rc = lu_env_init(&env, LCT_MD_THREAD);
7325 rc = mdt_device_sync(&env, mdt);
7327 case OBD_IOC_SET_READONLY:
7328 rc = dt_sync(&env, dt);
7330 rc = dt_ro(&env, dt);
7332 case OBD_IOC_ABORT_RECOVERY: {
7333 struct obd_ioctl_data *data = karg;
7335 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7336 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT) {
7337 obd->obd_abort_recov_mdt = 1;
7338 wake_up(&obd->obd_next_transno_waitq);
7339 } else { /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7340 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7341 obd->obd_abort_recovery = 1;
7342 target_stop_recovery_thread(obd);
7347 case OBD_IOC_CHANGELOG_REG:
7348 case OBD_IOC_CHANGELOG_DEREG:
7349 case OBD_IOC_CHANGELOG_CLEAR:
7350 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7354 case OBD_IOC_START_LFSCK: {
7355 struct md_device *next = mdt->mdt_child;
7356 struct obd_ioctl_data *data = karg;
7357 struct lfsck_start_param lsp;
7359 if (unlikely(data == NULL)) {
7364 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7365 lsp.lsp_index_valid = 0;
7366 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7369 case OBD_IOC_STOP_LFSCK: {
7370 struct md_device *next = mdt->mdt_child;
7371 struct obd_ioctl_data *data = karg;
7372 struct lfsck_stop stop;
7374 stop.ls_status = LS_STOPPED;
7375 /* Old lfsck utils may pass NULL @stop. */
7376 if (data->ioc_inlbuf1 == NULL)
7380 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7382 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7385 case OBD_IOC_QUERY_LFSCK: {
7386 struct md_device *next = mdt->mdt_child;
7387 struct obd_ioctl_data *data = karg;
7389 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7393 case OBD_IOC_GET_OBJ_VERSION: {
7394 struct mdt_thread_info *mti;
7395 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7396 memset(mti, 0, sizeof *mti);
7397 mti->mti_env = &env;
7401 rc = mdt_ioc_version_get(mti, karg);
7404 case OBD_IOC_CATLOGLIST: {
7405 struct mdt_thread_info *mti;
7407 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7408 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7409 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7410 &mti->mti_tmp_fid1);
7415 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7416 mdt_obd_name(mdt), cmd, rc);
7423 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7425 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7429 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7430 struct lfsck_start_param lsp;
7432 lsp.lsp_start = NULL;
7433 lsp.lsp_index_valid = 0;
7434 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7435 OBD_IOC_START_LFSCK,
7437 if (rc != 0 && rc != -EALREADY)
7438 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7439 mdt_obd_name(mdt), rc);
7442 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7446 static int mdt_obd_postrecov(struct obd_device *obd)
7451 rc = lu_env_init(&env, LCT_MD_THREAD);
7454 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7459 static const struct obd_ops mdt_obd_device_ops = {
7460 .o_owner = THIS_MODULE,
7461 .o_set_info_async = mdt_obd_set_info_async,
7462 .o_connect = mdt_obd_connect,
7463 .o_reconnect = mdt_obd_reconnect,
7464 .o_disconnect = mdt_obd_disconnect,
7465 .o_init_export = mdt_init_export,
7466 .o_destroy_export = mdt_destroy_export,
7467 .o_iocontrol = mdt_iocontrol,
7468 .o_postrecov = mdt_obd_postrecov,
7469 /* Data-on-MDT IO methods */
7470 .o_preprw = mdt_obd_preprw,
7471 .o_commitrw = mdt_obd_commitrw,
7474 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7475 struct lu_device *d)
7477 struct mdt_device *m = mdt_dev(d);
7484 static struct lu_device *mdt_device_free(const struct lu_env *env,
7485 struct lu_device *d)
7487 struct mdt_device *m = mdt_dev(d);
7490 lu_device_fini(&m->mdt_lu_dev);
7496 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7497 struct lu_device_type *t,
7498 struct lustre_cfg *cfg)
7500 struct lu_device *l;
7501 struct mdt_device *m;
7508 rc = mdt_init0(env, m, t, cfg);
7510 mdt_device_free(env, l);
7515 l = ERR_PTR(-ENOMEM);
7519 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7520 LU_KEY_INIT(mdt, struct mdt_thread_info);
7522 static void mdt_key_fini(const struct lu_context *ctx,
7523 struct lu_context_key *key, void* data)
7525 struct mdt_thread_info *info = data;
7527 if (info->mti_big_lmm) {
7528 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7529 info->mti_big_lmm = NULL;
7530 info->mti_big_lmmsize = 0;
7533 if (info->mti_big_acl) {
7534 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7535 info->mti_big_acl = NULL;
7536 info->mti_big_aclsize = 0;
7542 /* context key: mdt_thread_key */
7543 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7545 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7547 return lu_ucred(info->mti_env);
7550 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7552 return lu_ucred_check(info->mti_env);
7556 * Enable/disable COS (Commit On Sharing).
7558 * Set/Clear the COS flag in mdt options.
7560 * \param mdt mdt device
7561 * \param val 0 disables COS, other values enable COS
7563 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7568 mdt->mdt_opts.mo_cos = val;
7569 rc = lu_env_init(&env, LCT_LOCAL);
7570 if (unlikely(rc != 0)) {
7571 CWARN("%s: lu_env initialization failed, cannot "
7572 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7575 mdt_device_sync(&env, mdt);
7580 * Check COS (Commit On Sharing) status.
7582 * Return COS flag status.
7584 * \param mdt mdt device
7586 int mdt_cos_is_enabled(struct mdt_device *mdt)
7588 return mdt->mdt_opts.mo_cos != 0;
7591 static const struct lu_device_type_operations mdt_device_type_ops = {
7592 .ldto_device_alloc = mdt_device_alloc,
7593 .ldto_device_free = mdt_device_free,
7594 .ldto_device_fini = mdt_device_fini
7597 static struct lu_device_type mdt_device_type = {
7598 .ldt_tags = LU_DEVICE_MD,
7599 .ldt_name = LUSTRE_MDT_NAME,
7600 .ldt_ops = &mdt_device_type_ops,
7601 .ldt_ctx_tags = LCT_MD_THREAD
7604 static int __init mdt_init(void)
7608 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7609 FID_NOBRACE_LEN + 1);
7610 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7612 rc = lu_kmem_init(mdt_caches);
7616 rc = mds_mod_init();
7620 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7621 LUSTRE_MDT_NAME, &mdt_device_type);
7626 lu_kmem_fini(mdt_caches);
7633 static void __exit mdt_exit(void)
7635 class_unregister_type(LUSTRE_MDT_NAME);
7637 lu_kmem_fini(mdt_caches);
7640 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7641 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7642 MODULE_VERSION(LUSTRE_VERSION_STRING);
7643 MODULE_LICENSE("GPL");
7645 module_init(mdt_init);
7646 module_exit(mdt_exit);