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>
67 #include "mdt_internal.h"
69 static unsigned int max_mod_rpcs_per_client = 8;
70 module_param(max_mod_rpcs_per_client, uint, 0644);
71 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 enum ldlm_mode mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static const struct lu_object_operations mdt_obj_ops;
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
108 static struct lu_kmem_descr mdt_caches[] = {
110 .ckd_cache = &mdt_object_kmem,
111 .ckd_name = "mdt_obj",
112 .ckd_size = sizeof(struct mdt_object)
115 .ckd_cache = &mdt_hsm_cdt_kmem,
116 .ckd_name = "mdt_cdt_restore_handle",
117 .ckd_size = sizeof(struct cdt_restore_handle)
120 .ckd_cache = &mdt_hsm_car_kmem,
121 .ckd_name = "mdt_cdt_agent_req",
122 .ckd_size = sizeof(struct cdt_agent_req)
129 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
133 return rep->lock_policy_res1 & op_flag;
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137 struct ldlm_reply *rep, __u64 op_flag)
140 info->mti_opdata &= ~op_flag;
141 tgt_opdata_clear(info->mti_env, op_flag);
144 rep->lock_policy_res1 &= ~op_flag;
147 void mdt_set_disposition(struct mdt_thread_info *info,
148 struct ldlm_reply *rep, __u64 op_flag)
151 info->mti_opdata |= op_flag;
152 tgt_opdata_set(info->mti_env, op_flag);
155 rep->lock_policy_res1 |= op_flag;
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
160 lh->mlh_pdo_hash = 0;
161 lh->mlh_reg_mode = lm;
162 lh->mlh_rreg_mode = lm;
163 lh->mlh_type = MDT_REG_LOCK;
166 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
168 mdt_lock_reg_init(lh, lock->l_req_mode);
169 if (lock->l_req_mode == LCK_GROUP)
170 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
173 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
174 const struct lu_name *lname)
176 lh->mlh_reg_mode = lock_mode;
177 lh->mlh_pdo_mode = LCK_MINMODE;
178 lh->mlh_rreg_mode = lock_mode;
179 lh->mlh_type = MDT_PDO_LOCK;
181 if (lu_name_is_valid(lname)) {
182 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
184 /* XXX Workaround for LU-2856
186 * Zero is a valid return value of full_name_hash, but
187 * several users of mlh_pdo_hash assume a non-zero
188 * hash value. We therefore map zero onto an
189 * arbitrary, but consistent value (1) to avoid
190 * problems further down the road. */
191 if (unlikely(lh->mlh_pdo_hash == 0))
192 lh->mlh_pdo_hash = 1;
194 lh->mlh_pdo_hash = 0;
198 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
199 struct mdt_lock_handle *lh)
205 * Any dir access needs couple of locks:
207 * 1) on part of dir we gonna take lookup/modify;
209 * 2) on whole dir to protect it from concurrent splitting and/or to
210 * flush client's cache for readdir().
212 * so, for a given mode and object this routine decides what lock mode
213 * to use for lock #2:
215 * 1) if caller's gonna lookup in dir then we need to protect dir from
216 * being splitted only - LCK_CR
218 * 2) if caller's gonna modify dir then we need to protect dir from
219 * being splitted and to flush cache - LCK_CW
221 * 3) if caller's gonna modify dir and that dir seems ready for
222 * splitting then we need to protect it from any type of access
223 * (lookup/modify/split) - LCK_EX --bzzz
226 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
227 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
230 * Ask underlaying level its opinion about preferable PDO lock mode
231 * having access type passed as regular lock mode:
233 * - MDL_MINMODE means that lower layer does not want to specify lock
236 * - MDL_NL means that no PDO lock should be taken. This is used in some
237 * cases. Say, for non-splittable directories no need to use PDO locks
240 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
241 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
243 if (mode != MDL_MINMODE) {
244 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
247 * Lower layer does not want to specify locking mode. We do it
248 * our selves. No special protection is needed, just flush
249 * client's cache on modification and allow concurrent
252 switch (lh->mlh_reg_mode) {
254 lh->mlh_pdo_mode = LCK_EX;
257 lh->mlh_pdo_mode = LCK_CR;
260 lh->mlh_pdo_mode = LCK_CW;
263 CERROR("Not expected lock type (0x%x)\n",
264 (int)lh->mlh_reg_mode);
269 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
273 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
276 struct mdt_device *mdt = info->mti_mdt;
277 struct lu_name *lname = &info->mti_name;
278 const char *start = fileset;
279 char *filename = info->mti_filename;
280 struct mdt_object *parent;
284 LASSERT(!info->mti_cross_ref);
287 * We may want to allow this to mount a completely separate
288 * fileset from the MDT in the future, but keeping it to
289 * ROOT/ only for now avoid potential security issues.
291 *fid = mdt->mdt_md_root_fid;
293 while (rc == 0 && start != NULL && *start != '\0') {
294 const char *s1 = start;
300 while (*s2 != '/' && *s2 != '\0')
308 lname->ln_namelen = s2 - s1;
309 if (lname->ln_namelen > NAME_MAX) {
314 /* reject .. as a path component */
315 if (lname->ln_namelen == 2 &&
316 strncmp(s1, "..", 2) == 0) {
321 strncpy(filename, s1, lname->ln_namelen);
322 filename[lname->ln_namelen] = '\0';
323 lname->ln_name = filename;
325 parent = mdt_object_find(info->mti_env, mdt, fid);
326 if (IS_ERR(parent)) {
327 rc = PTR_ERR(parent);
330 /* Only got the fid of this obj by name */
332 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
333 fid, &info->mti_spec);
334 mdt_object_put(info->mti_env, parent);
337 parent = mdt_object_find(info->mti_env, mdt, fid);
339 rc = PTR_ERR(parent);
341 mode = lu_object_attr(&parent->mot_obj);
342 if (!S_ISDIR(mode)) {
344 } else if (mdt_is_remote_object(info, parent, parent)) {
345 if (!mdt->mdt_enable_remote_subdir_mount) {
347 LCONSOLE_WARN("%s: subdir mount '%s' refused because 'enable_remote_subdir_mount=0': rc = %d\n",
351 LCONSOLE_INFO("%s: subdir mount '%s' is remote and may be slow\n",
356 mdt_object_put(info->mti_env, parent);
363 static int mdt_get_root(struct tgt_session_info *tsi)
365 struct mdt_thread_info *info = tsi2mdt_info(tsi);
366 struct mdt_device *mdt = info->mti_mdt;
367 struct mdt_body *repbody;
368 char *fileset = NULL, *buffer = NULL;
370 struct obd_export *exp = info->mti_exp;
371 char *nodemap_fileset;
375 rc = mdt_check_ucred(info);
377 GOTO(out, rc = err_serious(rc));
379 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
380 GOTO(out, rc = err_serious(-ENOMEM));
382 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
383 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
384 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
386 GOTO(out, rc = err_serious(-EFAULT));
389 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
390 if (nodemap_fileset && nodemap_fileset[0]) {
391 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
393 /* consider fileset from client as a sub-fileset
394 * of the nodemap one */
395 OBD_ALLOC(buffer, PATH_MAX + 1);
397 GOTO(out, rc = err_serious(-ENOMEM));
398 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
399 nodemap_fileset, fileset) >= PATH_MAX + 1)
400 GOTO(out, rc = err_serious(-EINVAL));
403 /* enforce fileset as specified in the nodemap */
404 fileset = nodemap_fileset;
409 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
410 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
412 GOTO(out, rc = err_serious(rc));
414 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
416 repbody->mbo_valid |= OBD_MD_FLID;
420 mdt_thread_info_fini(info);
422 OBD_FREE(buffer, PATH_MAX+1);
426 static int mdt_statfs(struct tgt_session_info *tsi)
428 struct ptlrpc_request *req = tgt_ses_req(tsi);
429 struct mdt_thread_info *info = tsi2mdt_info(tsi);
430 struct mdt_device *mdt = info->mti_mdt;
431 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
432 struct md_device *next = mdt->mdt_child;
433 struct ptlrpc_service_part *svcpt;
434 struct obd_statfs *osfs;
435 struct mdt_body *reqbody = NULL;
436 struct mdt_statfs_cache *msf;
437 ktime_t kstart = ktime_get();
442 svcpt = req->rq_rqbd->rqbd_svcpt;
444 /* This will trigger a watchdog timeout */
445 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
446 (MDT_SERVICE_WATCHDOG_FACTOR *
447 at_get(&svcpt->scp_at_estimate)) + 1);
449 rc = mdt_check_ucred(info);
451 GOTO(out, rc = err_serious(rc));
453 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
454 GOTO(out, rc = err_serious(-ENOMEM));
456 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
458 GOTO(out, rc = -EPROTO);
460 if (mdt_is_sum_statfs_client(req->rq_export) &&
461 lustre_packed_msg_size(req->rq_reqmsg) ==
462 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
463 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
464 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
465 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
468 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
469 msf = &mdt->mdt_sum_osfs;
471 msf = &mdt->mdt_osfs;
473 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
474 /** statfs data is too old, get up-to-date one */
475 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
476 rc = next->md_ops->mdo_statfs(info->mti_env,
479 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
483 spin_lock(&mdt->mdt_lock);
484 msf->msf_osfs = *osfs;
485 msf->msf_age = ktime_get_seconds();
486 spin_unlock(&mdt->mdt_lock);
488 /** use cached statfs data */
489 spin_lock(&mdt->mdt_lock);
490 *osfs = msf->msf_osfs;
491 spin_unlock(&mdt->mdt_lock);
494 /* at least try to account for cached pages. its still racy and
495 * might be under-reporting if clients haven't announced their
496 * caches with brw recently */
497 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
498 " pending %llu free %llu avail %llu\n",
499 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
500 tgd->tgd_tot_pending,
501 osfs->os_bfree << tgd->tgd_blockbits,
502 osfs->os_bavail << tgd->tgd_blockbits);
504 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
505 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
506 osfs->os_bsize - 1) >> tgd->tgd_blockbits));
508 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
509 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
510 "%llu objects: %llu free; state %x\n",
511 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
512 osfs->os_files, osfs->os_ffree, osfs->os_state);
514 if (!exp_grant_param_supp(tsi->tsi_exp) &&
515 tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
516 /* clients which don't support OBD_CONNECT_GRANT_PARAM
517 * should not see a block size > page size, otherwise
518 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
519 * block size which is the biggest block size known to work
520 * with all client's page size. */
521 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
522 osfs->os_bfree <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
523 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
524 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
527 mdt_counter_incr(req, LPROC_MDT_STATFS,
528 ktime_us_delta(ktime_get(), kstart));
530 mdt_thread_info_fini(info);
534 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
536 struct lov_comp_md_v1 *comp_v1;
537 struct lov_mds_md *v1;
539 __u32 dom_stripesize = 0;
541 bool has_ost_stripes = false;
548 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
551 comp_v1 = (struct lov_comp_md_v1 *)lmm;
552 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
553 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
555 /* Fast check for DoM entry with no mirroring, should be the first */
556 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
557 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
560 /* check all entries otherwise */
561 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
562 struct lov_comp_md_entry_v1 *lcme;
564 lcme = &comp_v1->lcm_entries[i];
565 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
568 off = le32_to_cpu(lcme->lcme_offset);
569 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
571 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
573 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
575 has_ost_stripes = true;
577 if (dom_stripesize && has_ost_stripes)
578 RETURN(dom_stripesize);
580 /* DoM-only case exits here */
581 if (is_dom_only && dom_stripesize)
583 RETURN(dom_stripesize);
587 * Pack size attributes into the reply.
589 int mdt_pack_size2body(struct mdt_thread_info *info,
590 const struct lu_fid *fid, struct lustre_handle *lh)
593 struct md_attr *ma = &info->mti_attr;
595 bool dom_lock = false;
599 LASSERT(ma->ma_attr.la_valid & LA_MODE);
601 if (!S_ISREG(ma->ma_attr.la_mode) ||
602 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
605 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
606 /* no DoM stripe, no size in reply */
610 if (lustre_handle_is_used(lh)) {
611 struct ldlm_lock *lock;
613 lock = ldlm_handle2lock(lh);
615 dom_lock = ldlm_has_dom(lock);
620 /* no DoM lock, no size in reply */
624 /* Either DoM lock exists or LMM has only DoM stripe then
625 * return size on body. */
626 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
628 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
632 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
634 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
636 * \param info thread info object
637 * \param repbody reply to pack ACLs into
638 * \param o mdt object of file to examine
639 * \param nodemap nodemap of client to reply to
641 * \retval -errno error getting or parsing ACL from disk
643 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
644 struct mdt_object *o, struct lu_nodemap *nodemap)
646 const struct lu_env *env = info->mti_env;
647 struct md_object *next = mdt_object_child(o);
648 struct lu_buf *buf = &info->mti_buf;
649 struct mdt_device *mdt = info->mti_mdt;
650 struct req_capsule *pill = info->mti_pill;
655 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
656 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
657 if (buf->lb_len == 0)
660 LASSERT(!info->mti_big_acl_used);
662 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
664 if (rc == -ENODATA) {
665 repbody->mbo_aclsize = 0;
666 repbody->mbo_valid |= OBD_MD_FLACL;
668 } else if (rc == -EOPNOTSUPP) {
670 } else if (rc == -ERANGE) {
671 if (exp_connect_large_acl(info->mti_exp) &&
672 !info->mti_big_acl_used) {
673 if (info->mti_big_acl == NULL) {
674 info->mti_big_aclsize =
676 mdt->mdt_max_ea_size,
678 OBD_ALLOC_LARGE(info->mti_big_acl,
679 info->mti_big_aclsize);
680 if (info->mti_big_acl == NULL) {
681 info->mti_big_aclsize = 0;
682 CERROR("%s: unable to grow "
685 PFID(mdt_object_fid(o)));
690 CDEBUG(D_INODE, "%s: grow the "DFID
691 " ACL buffer to size %d\n",
693 PFID(mdt_object_fid(o)),
694 info->mti_big_aclsize);
696 buf->lb_buf = info->mti_big_acl;
697 buf->lb_len = info->mti_big_aclsize;
698 info->mti_big_acl_used = 1;
701 /* FS has ACL bigger that our limits */
702 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
703 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
704 info->mti_big_aclsize);
707 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
708 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
711 rc = nodemap_map_acl(nodemap, buf->lb_buf,
712 rc, NODEMAP_FS_TO_CLIENT);
713 /* if all ACLs mapped out, rc is still >= 0 */
715 CERROR("%s: nodemap_map_acl unable to parse "DFID
716 " ACL: rc = %d\n", mdt_obd_name(mdt),
717 PFID(mdt_object_fid(o)), rc);
718 repbody->mbo_aclsize = 0;
719 repbody->mbo_valid &= ~OBD_MD_FLACL;
721 repbody->mbo_aclsize = rc;
722 repbody->mbo_valid |= OBD_MD_FLACL;
731 /* XXX Look into layout in MDT layer. */
732 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
734 struct lov_comp_md_v1 *comp_v1;
735 struct lov_mds_md *v1;
738 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
739 comp_v1 = (struct lov_comp_md_v1 *)lmm;
741 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
742 v1 = (struct lov_mds_md *)((char *)comp_v1 +
743 comp_v1->lcm_entries[i].lcme_offset);
744 /* We don't support partial release for now */
745 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
750 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
755 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
756 const struct lu_attr *attr, const struct lu_fid *fid)
758 struct md_attr *ma = &info->mti_attr;
759 struct obd_export *exp = info->mti_exp;
760 struct lu_nodemap *nodemap = NULL;
762 LASSERT(ma->ma_valid & MA_INODE);
764 if (attr->la_valid & LA_ATIME) {
765 b->mbo_atime = attr->la_atime;
766 b->mbo_valid |= OBD_MD_FLATIME;
768 if (attr->la_valid & LA_MTIME) {
769 b->mbo_mtime = attr->la_mtime;
770 b->mbo_valid |= OBD_MD_FLMTIME;
772 if (attr->la_valid & LA_CTIME) {
773 b->mbo_ctime = attr->la_ctime;
774 b->mbo_valid |= OBD_MD_FLCTIME;
776 if (attr->la_valid & LA_BTIME) {
777 b->mbo_btime = attr->la_btime;
778 b->mbo_valid |= OBD_MD_FLBTIME;
780 if (attr->la_valid & LA_FLAGS) {
781 b->mbo_flags = attr->la_flags;
782 b->mbo_valid |= OBD_MD_FLFLAGS;
784 if (attr->la_valid & LA_NLINK) {
785 b->mbo_nlink = attr->la_nlink;
786 b->mbo_valid |= OBD_MD_FLNLINK;
788 if (attr->la_valid & (LA_UID|LA_GID)) {
789 nodemap = nodemap_get_from_exp(exp);
793 if (attr->la_valid & LA_UID) {
794 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
795 NODEMAP_FS_TO_CLIENT,
797 b->mbo_valid |= OBD_MD_FLUID;
799 if (attr->la_valid & LA_GID) {
800 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
801 NODEMAP_FS_TO_CLIENT,
803 b->mbo_valid |= OBD_MD_FLGID;
806 if (attr->la_valid & LA_PROJID) {
807 /* TODO, nodemap for project id */
808 b->mbo_projid = attr->la_projid;
809 b->mbo_valid |= OBD_MD_FLPROJID;
812 b->mbo_mode = attr->la_mode;
813 if (attr->la_valid & LA_MODE)
814 b->mbo_valid |= OBD_MD_FLMODE;
815 if (attr->la_valid & LA_TYPE)
816 b->mbo_valid |= OBD_MD_FLTYPE;
820 b->mbo_valid |= OBD_MD_FLID;
821 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
822 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
825 if (!(attr->la_valid & LA_TYPE))
828 b->mbo_rdev = attr->la_rdev;
829 b->mbo_size = attr->la_size;
830 b->mbo_blocks = attr->la_blocks;
832 if (!S_ISREG(attr->la_mode)) {
833 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
834 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
835 /* means no objects are allocated on osts. */
836 LASSERT(!(ma->ma_valid & MA_LOV));
837 /* just ignore blocks occupied by extend attributes on MDS */
839 /* if no object is allocated on osts, the size on mds is valid.
841 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
842 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
843 if (mdt_hsm_is_released(ma->ma_lmm)) {
844 /* A released file stores its size on MDS. */
845 /* But return 1 block for released file, unless tools
846 * like tar will consider it fully sparse. (LU-3864)
848 if (unlikely(b->mbo_size == 0))
852 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
853 } else if (info->mti_som_valid) { /* som is valid */
854 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
855 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
856 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
857 b->mbo_size = ma->ma_som.ms_size;
858 b->mbo_blocks = ma->ma_som.ms_blocks;
862 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
863 b->mbo_valid & OBD_MD_FLLAZYSIZE))
864 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
865 PFID(fid), (unsigned long long)b->mbo_size);
868 if (!IS_ERR_OR_NULL(nodemap))
869 nodemap_putref(nodemap);
872 static inline int mdt_body_has_lov(const struct lu_attr *la,
873 const struct mdt_body *body)
875 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
876 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
879 void mdt_client_compatibility(struct mdt_thread_info *info)
881 struct mdt_body *body;
882 struct ptlrpc_request *req = mdt_info_req(info);
883 struct obd_export *exp = req->rq_export;
884 struct md_attr *ma = &info->mti_attr;
885 struct lu_attr *la = &ma->ma_attr;
888 if (exp_connect_layout(exp))
889 /* the client can deal with 16-bit lmm_stripe_count */
892 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
894 if (!mdt_body_has_lov(la, body))
897 /* now we have a reply with a lov for a client not compatible with the
898 * layout lock so we have to clean the layout generation number */
899 if (S_ISREG(la->la_mode))
900 ma->ma_lmm->lmm_layout_gen = 0;
904 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
905 struct mdt_object *o)
907 const struct lu_env *env = info->mti_env;
910 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
919 /* Is it a directory? Let's check for the LMV as well */
920 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
921 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
925 rc2 = mo_xattr_get(env, mdt_object_child(o),
927 XATTR_NAME_DEFAULT_LMV);
929 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
937 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
940 const struct lu_env *env = info->mti_env;
944 LASSERT(info->mti_big_lmm_used == 0);
945 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
949 /* big_lmm may need to be grown */
950 if (info->mti_big_lmmsize < rc) {
951 int size = size_roundup_power2(rc);
953 if (info->mti_big_lmmsize > 0) {
954 /* free old buffer */
955 LASSERT(info->mti_big_lmm);
956 OBD_FREE_LARGE(info->mti_big_lmm,
957 info->mti_big_lmmsize);
958 info->mti_big_lmm = NULL;
959 info->mti_big_lmmsize = 0;
962 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
963 if (info->mti_big_lmm == NULL)
965 info->mti_big_lmmsize = size;
967 LASSERT(info->mti_big_lmmsize >= rc);
969 info->mti_buf.lb_buf = info->mti_big_lmm;
970 info->mti_buf.lb_len = info->mti_big_lmmsize;
971 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
976 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
977 struct md_attr *ma, const char *name)
979 struct md_object *next = mdt_object_child(o);
980 struct lu_buf *buf = &info->mti_buf;
983 if (strcmp(name, XATTR_NAME_LOV) == 0) {
984 buf->lb_buf = ma->ma_lmm;
985 buf->lb_len = ma->ma_lmm_size;
986 LASSERT(!(ma->ma_valid & MA_LOV));
987 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
988 buf->lb_buf = ma->ma_lmv;
989 buf->lb_len = ma->ma_lmv_size;
990 LASSERT(!(ma->ma_valid & MA_LMV));
991 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
992 buf->lb_buf = ma->ma_default_lmv;
993 buf->lb_len = ma->ma_default_lmv_size;
994 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
999 LASSERT(buf->lb_buf);
1001 rc = mo_xattr_get(info->mti_env, next, buf, name);
1005 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1006 if (info->mti_big_lmm_used)
1007 ma->ma_lmm = info->mti_big_lmm;
1009 /* NOT return LOV EA with hole to old client. */
1010 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1011 LOV_PATTERN_F_HOLE) &&
1012 !(exp_connect_flags(info->mti_exp) &
1013 OBD_CONNECT_LFSCK)) {
1016 ma->ma_lmm_size = rc;
1017 ma->ma_valid |= MA_LOV;
1019 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1020 if (info->mti_big_lmm_used)
1021 ma->ma_lmv = info->mti_big_lmm;
1023 ma->ma_lmv_size = rc;
1024 ma->ma_valid |= MA_LMV;
1025 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1026 ma->ma_default_lmv_size = rc;
1027 ma->ma_valid |= MA_LMV_DEF;
1030 /* Update mdt_max_mdsize so all clients will be aware that */
1031 if (info->mti_mdt->mdt_max_mdsize < rc)
1032 info->mti_mdt->mdt_max_mdsize = rc;
1035 } else if (rc == -ENODATA) {
1038 } else if (rc == -ERANGE) {
1039 /* Default LMV has fixed size, so it must be able to fit
1040 * in the original buffer */
1041 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1043 rc = mdt_big_xattr_get(info, o, name);
1045 info->mti_big_lmm_used = 1;
1053 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1054 struct md_attr *ma, const char *name)
1058 if (!info->mti_big_lmm) {
1059 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1060 if (!info->mti_big_lmm)
1062 info->mti_big_lmmsize = PAGE_SIZE;
1065 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1066 ma->ma_lmm = info->mti_big_lmm;
1067 ma->ma_lmm_size = info->mti_big_lmmsize;
1068 ma->ma_valid &= ~MA_LOV;
1069 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1070 ma->ma_lmv = info->mti_big_lmm;
1071 ma->ma_lmv_size = info->mti_big_lmmsize;
1072 ma->ma_valid &= ~MA_LMV;
1077 LASSERT(!info->mti_big_lmm_used);
1078 rc = __mdt_stripe_get(info, o, ma, name);
1079 /* since big_lmm is always used here, clear 'used' flag to avoid
1080 * assertion in mdt_big_xattr_get().
1082 info->mti_big_lmm_used = 0;
1087 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1088 struct lu_fid *pfid)
1090 struct lu_buf *buf = &info->mti_buf;
1091 struct link_ea_header *leh;
1092 struct link_ea_entry *lee;
1096 buf->lb_buf = info->mti_big_lmm;
1097 buf->lb_len = info->mti_big_lmmsize;
1098 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1099 buf, XATTR_NAME_LINK);
1100 /* ignore errors, MA_PFID won't be set and it is
1101 * up to the caller to treat this as an error */
1102 if (rc == -ERANGE || buf->lb_len == 0) {
1103 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1104 buf->lb_buf = info->mti_big_lmm;
1105 buf->lb_len = info->mti_big_lmmsize;
1110 if (rc < sizeof(*leh)) {
1111 CERROR("short LinkEA on "DFID": rc = %d\n",
1112 PFID(mdt_object_fid(o)), rc);
1116 leh = (struct link_ea_header *) buf->lb_buf;
1117 lee = (struct link_ea_entry *)(leh + 1);
1118 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1119 leh->leh_magic = LINK_EA_MAGIC;
1120 leh->leh_reccount = __swab32(leh->leh_reccount);
1121 leh->leh_len = __swab64(leh->leh_len);
1123 if (leh->leh_magic != LINK_EA_MAGIC)
1125 if (leh->leh_reccount == 0)
1128 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1129 fid_be_to_cpu(pfid, pfid);
1134 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1135 struct lu_fid *pfid, struct lu_name *lname)
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_xattr_buf;
1144 buf->lb_len = sizeof(info->mti_xattr_buf);
1145 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1147 if (rc == -ERANGE) {
1148 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1149 buf->lb_buf = info->mti_big_lmm;
1150 buf->lb_len = info->mti_big_lmmsize;
1155 if (rc < sizeof(*leh)) {
1156 CERROR("short LinkEA on "DFID": rc = %d\n",
1157 PFID(mdt_object_fid(o)), rc);
1161 leh = (struct link_ea_header *)buf->lb_buf;
1162 lee = (struct link_ea_entry *)(leh + 1);
1163 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1164 leh->leh_magic = LINK_EA_MAGIC;
1165 leh->leh_reccount = __swab32(leh->leh_reccount);
1166 leh->leh_len = __swab64(leh->leh_len);
1168 if (leh->leh_magic != LINK_EA_MAGIC)
1171 if (leh->leh_reccount == 0)
1174 linkea_entry_unpack(lee, &reclen, lname, pfid);
1179 int mdt_attr_get_complex(struct mdt_thread_info *info,
1180 struct mdt_object *o, struct md_attr *ma)
1182 const struct lu_env *env = info->mti_env;
1183 struct md_object *next = mdt_object_child(o);
1184 struct lu_buf *buf = &info->mti_buf;
1185 int need = ma->ma_need;
1192 if (mdt_object_exists(o) == 0)
1193 GOTO(out, rc = -ENOENT);
1194 mode = lu_object_attr(&next->mo_lu);
1196 if (need & MA_INODE) {
1197 ma->ma_need = MA_INODE;
1198 rc = mo_attr_get(env, next, ma);
1203 (void) mdt_get_som(info, o, ma);
1204 ma->ma_valid |= MA_INODE;
1207 if (need & MA_PFID) {
1208 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1210 ma->ma_valid |= MA_PFID;
1211 /* ignore this error, parent fid is not mandatory */
1215 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1216 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1221 if (need & MA_LMV && S_ISDIR(mode)) {
1222 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1227 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1228 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1234 * In the handle of MA_INODE, we may already get the SOM attr.
1236 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1237 rc = mdt_get_som(info, o, ma);
1242 if (need & MA_HSM && S_ISREG(mode)) {
1243 buf->lb_buf = info->mti_xattr_buf;
1244 buf->lb_len = sizeof(info->mti_xattr_buf);
1245 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1246 sizeof(info->mti_xattr_buf));
1247 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1248 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1250 ma->ma_valid |= MA_HSM;
1251 else if (rc2 < 0 && rc2 != -ENODATA)
1252 GOTO(out, rc = rc2);
1255 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1256 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1257 buf->lb_buf = ma->ma_acl;
1258 buf->lb_len = ma->ma_acl_size;
1259 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1261 ma->ma_acl_size = rc2;
1262 ma->ma_valid |= MA_ACL_DEF;
1263 } else if (rc2 == -ENODATA) {
1265 ma->ma_acl_size = 0;
1267 GOTO(out, rc = rc2);
1272 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1273 rc, ma->ma_valid, ma->ma_lmm);
1277 static int mdt_getattr_internal(struct mdt_thread_info *info,
1278 struct mdt_object *o, int ma_need)
1280 struct mdt_device *mdt = info->mti_mdt;
1281 struct md_object *next = mdt_object_child(o);
1282 const struct mdt_body *reqbody = info->mti_body;
1283 struct ptlrpc_request *req = mdt_info_req(info);
1284 struct md_attr *ma = &info->mti_attr;
1285 struct lu_attr *la = &ma->ma_attr;
1286 struct req_capsule *pill = info->mti_pill;
1287 const struct lu_env *env = info->mti_env;
1288 struct mdt_body *repbody;
1289 struct lu_buf *buffer = &info->mti_buf;
1290 struct obd_export *exp = info->mti_exp;
1291 ktime_t kstart = ktime_get();
1296 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1297 RETURN(err_serious(-ENOMEM));
1299 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1303 if (mdt_object_remote(o)) {
1304 /* This object is located on remote node.*/
1305 /* Return -ENOTSUPP for old client */
1306 if (!mdt_is_dne_client(req->rq_export))
1307 GOTO(out, rc = -ENOTSUPP);
1309 repbody->mbo_fid1 = *mdt_object_fid(o);
1310 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1314 if (reqbody->mbo_eadatasize > 0) {
1315 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1316 if (buffer->lb_buf == NULL)
1317 GOTO(out, rc = -EPROTO);
1318 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1321 buffer->lb_buf = NULL;
1323 ma_need &= ~(MA_LOV | MA_LMV);
1324 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1325 mdt_obd_name(info->mti_mdt),
1326 req->rq_export->exp_client_uuid.uuid);
1329 /* from 2.12.58 intent_getattr pack default LMV in reply */
1330 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1331 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1332 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1333 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1335 ma->ma_lmv = buffer->lb_buf;
1336 ma->ma_lmv_size = buffer->lb_len;
1337 ma->ma_default_lmv = req_capsule_server_get(pill,
1338 &RMF_DEFAULT_MDT_MD);
1339 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1340 &RMF_DEFAULT_MDT_MD,
1342 ma->ma_need = MA_INODE;
1343 if (ma->ma_lmv_size > 0)
1344 ma->ma_need |= MA_LMV;
1345 if (ma->ma_default_lmv_size > 0)
1346 ma->ma_need |= MA_LMV_DEF;
1347 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1348 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1349 /* If it is dir and client require MEA, then we got MEA */
1350 /* Assumption: MDT_MD size is enough for lmv size. */
1351 ma->ma_lmv = buffer->lb_buf;
1352 ma->ma_lmv_size = buffer->lb_len;
1353 ma->ma_need = MA_INODE;
1354 if (ma->ma_lmv_size > 0) {
1355 if (reqbody->mbo_valid & OBD_MD_MEA) {
1356 ma->ma_need |= MA_LMV;
1357 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1358 ma->ma_need |= MA_LMV_DEF;
1359 ma->ma_default_lmv = buffer->lb_buf;
1361 ma->ma_default_lmv_size = buffer->lb_len;
1362 ma->ma_lmv_size = 0;
1366 ma->ma_lmm = buffer->lb_buf;
1367 ma->ma_lmm_size = buffer->lb_len;
1368 ma->ma_need = MA_INODE | MA_HSM;
1369 if (ma->ma_lmm_size > 0) {
1370 ma->ma_need |= MA_LOV;
1371 /* Older clients may crash if they getattr overstriped
1374 if (!exp_connect_overstriping(exp) &&
1375 mdt_lmm_is_overstriping(ma->ma_lmm))
1376 RETURN(-EOPNOTSUPP);
1380 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1381 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1382 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1383 /* get default stripe info for this dir. */
1384 ma->ma_need |= MA_LOV_DEF;
1386 ma->ma_need |= ma_need;
1388 rc = mdt_attr_get_complex(info, o, ma);
1390 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1391 "%s: getattr error for "DFID": rc = %d\n",
1392 mdt_obd_name(info->mti_mdt),
1393 PFID(mdt_object_fid(o)), rc);
1397 /* if file is released, check if a restore is running */
1398 if (ma->ma_valid & MA_HSM) {
1399 repbody->mbo_valid |= OBD_MD_TSTATE;
1400 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1401 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1402 repbody->mbo_t_state = MS_RESTORE;
1405 if (unlikely(!(ma->ma_valid & MA_INODE)))
1408 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1410 if (mdt_body_has_lov(la, reqbody)) {
1411 u32 stripe_count = 1;
1413 if (ma->ma_valid & MA_LOV) {
1414 LASSERT(ma->ma_lmm_size);
1415 repbody->mbo_eadatasize = ma->ma_lmm_size;
1416 if (S_ISDIR(la->la_mode))
1417 repbody->mbo_valid |= OBD_MD_FLDIREA;
1419 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1420 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1422 if (ma->ma_valid & MA_LMV) {
1423 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1424 u32 magic = le32_to_cpu(lmv->lmv_magic);
1426 /* Return -ENOTSUPP for old client */
1427 if (!mdt_is_striped_client(req->rq_export))
1430 LASSERT(S_ISDIR(la->la_mode));
1431 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1432 repbody->mbo_eadatasize = ma->ma_lmv_size;
1433 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1435 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1436 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1437 mdt_restripe_migrate_add(info, o);
1438 else if (magic == LMV_MAGIC_V1 &&
1439 lmv_is_restriping(lmv))
1440 mdt_restripe_update_add(info, o);
1442 if (ma->ma_valid & MA_LMV_DEF) {
1443 /* Return -ENOTSUPP for old client */
1444 if (!mdt_is_striped_client(req->rq_export))
1446 LASSERT(S_ISDIR(la->la_mode));
1448 * when ll_dir_getstripe() gets default LMV, it
1449 * checks mbo_eadatasize.
1451 if (!(ma->ma_valid & MA_LMV))
1452 repbody->mbo_eadatasize =
1453 ma->ma_default_lmv_size;
1454 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1455 OBD_MD_DEFAULT_MEA);
1458 "dirent count %llu stripe count %u MDT count %d\n",
1459 ma->ma_attr.la_dirent_count, stripe_count,
1460 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1461 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1462 ma->ma_attr.la_dirent_count >
1463 mdt->mdt_restriper.mdr_dir_split_count &&
1464 !fid_is_root(mdt_object_fid(o)) &&
1465 mdt->mdt_enable_dir_auto_split &&
1466 !o->mot_restriping &&
1467 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1468 mdt_auto_split_add(info, o);
1469 } else if (S_ISLNK(la->la_mode) &&
1470 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1471 buffer->lb_buf = ma->ma_lmm;
1472 /* eadatasize from client includes NULL-terminator, so
1473 * there is no need to read it */
1474 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1475 rc = mo_readlink(env, next, buffer);
1476 if (unlikely(rc <= 0)) {
1477 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1478 mdt_obd_name(info->mti_mdt),
1479 PFID(mdt_object_fid(o)), rc);
1482 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1484 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1486 repbody->mbo_valid |= OBD_MD_LINKNAME;
1487 /* we need to report back size with NULL-terminator
1488 * because client expects that */
1489 repbody->mbo_eadatasize = rc + 1;
1490 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1491 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1492 "on "DFID ", expected %d\n",
1493 mdt_obd_name(info->mti_mdt),
1494 rc, PFID(mdt_object_fid(o)),
1495 reqbody->mbo_eadatasize - 1);
1496 /* NULL terminate */
1497 ((char *)ma->ma_lmm)[rc] = 0;
1499 /* If the total CDEBUG() size is larger than a page, it
1500 * will print a warning to the console, avoid this by
1501 * printing just the last part of the symlink. */
1502 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1503 print_limit < rc ? "..." : "", print_limit,
1504 (char *)ma->ma_lmm + rc - print_limit, rc);
1509 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1510 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1511 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1512 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1513 repbody->mbo_max_mdsize);
1516 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1517 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1518 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1519 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1520 if (IS_ERR(nodemap))
1521 RETURN(PTR_ERR(nodemap));
1523 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1524 nodemap_putref(nodemap);
1530 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1531 ktime_us_delta(ktime_get(), kstart));
1536 static int mdt_getattr(struct tgt_session_info *tsi)
1538 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1539 struct mdt_object *obj = info->mti_object;
1540 struct req_capsule *pill = info->mti_pill;
1541 struct mdt_body *reqbody;
1542 struct mdt_body *repbody;
1546 if (unlikely(info->mti_object == NULL))
1549 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1551 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1553 /* Special case for Data-on-MDT files to get data version */
1554 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1555 rc = mdt_data_version_get(tsi);
1559 /* Unlike intent case where we need to pre-fill out buffers early on
1560 * in intent policy for ldlm reasons, here we can have a much better
1561 * guess at EA size by just reading it from disk.
1562 * Exceptions are readdir and (missing) directory striping */
1564 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1565 /* No easy way to know how long is the symlink, but it cannot
1566 * be more than PATH_MAX, so we allocate +1 */
1568 /* A special case for fs ROOT: getattr there might fetch
1569 * default EA for entire fs, not just for this dir!
1571 } else if (lu_fid_eq(mdt_object_fid(obj),
1572 &info->mti_mdt->mdt_md_root_fid) &&
1573 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1574 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1576 /* Should the default strping be bigger, mdt_fix_reply
1577 * will reallocate */
1578 rc = DEF_REP_MD_SIZE;
1580 /* Read the actual EA size from disk */
1581 rc = mdt_attr_get_eabuf_size(info, obj);
1585 GOTO(out, rc = err_serious(rc));
1587 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1589 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1590 * by default. If the target object has more ACL entries, then
1591 * enlarge the buffer when necessary. */
1592 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1593 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1595 rc = req_capsule_server_pack(pill);
1596 if (unlikely(rc != 0))
1597 GOTO(out, rc = err_serious(rc));
1599 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1600 LASSERT(repbody != NULL);
1601 repbody->mbo_eadatasize = 0;
1602 repbody->mbo_aclsize = 0;
1604 rc = mdt_check_ucred(info);
1606 GOTO(out_shrink, rc);
1608 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1610 rc = mdt_getattr_internal(info, obj, 0);
1613 mdt_client_compatibility(info);
1614 rc2 = mdt_fix_reply(info);
1618 mdt_thread_info_fini(info);
1623 * Handler of layout intent RPC requiring the layout modification
1625 * \param[in] info thread environment
1626 * \param[in] obj object
1627 * \param[out] lhc object ldlm lock handle
1628 * \param[in] layout layout change descriptor
1630 * \retval 0 on success
1631 * \retval < 0 error code
1633 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1634 struct mdt_lock_handle *lhc,
1635 struct md_layout_change *layout)
1641 if (!mdt_object_exists(obj))
1644 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1647 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1652 rc = mdt_check_resent_lock(info, obj, lhc);
1658 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1660 /* take layout lock to prepare layout change */
1661 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1662 lockpart |= MDS_INODELOCK_UPDATE;
1664 mdt_lock_handle_init(lhc);
1665 mdt_lock_reg_init(lhc, LCK_EX);
1666 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1671 mutex_lock(&obj->mot_som_mutex);
1672 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1673 mutex_unlock(&obj->mot_som_mutex);
1676 mdt_object_unlock(info, obj, lhc, 1);
1682 * Exchange MOF_LOV_CREATED flags between two objects after a
1683 * layout swap. No assumption is made on whether o1 or o2 have
1684 * created objects or not.
1686 * \param[in,out] o1 First swap layout object
1687 * \param[in,out] o2 Second swap layout object
1689 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1691 unsigned int o1_lov_created = o1->mot_lov_created;
1693 mutex_lock(&o1->mot_lov_mutex);
1694 mutex_lock(&o2->mot_lov_mutex);
1696 o1->mot_lov_created = o2->mot_lov_created;
1697 o2->mot_lov_created = o1_lov_created;
1699 mutex_unlock(&o2->mot_lov_mutex);
1700 mutex_unlock(&o1->mot_lov_mutex);
1703 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1705 struct mdt_thread_info *info;
1706 struct ptlrpc_request *req = tgt_ses_req(tsi);
1707 struct obd_export *exp = req->rq_export;
1708 struct mdt_object *o1, *o2, *o;
1709 struct mdt_lock_handle *lh1, *lh2;
1710 struct mdc_swap_layouts *msl;
1714 /* client does not support layout lock, so layout swaping
1716 * FIXME: there is a problem for old clients which don't support
1717 * layout lock yet. If those clients have already opened the file
1718 * they won't be notified at all so that old layout may still be
1719 * used to do IO. This can be fixed after file release is landed by
1720 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1721 if (!exp_connect_layout(exp))
1722 RETURN(-EOPNOTSUPP);
1724 info = tsi2mdt_info(tsi);
1725 if (unlikely(info->mti_object == NULL))
1728 if (info->mti_dlm_req != NULL)
1729 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1731 o1 = info->mti_object;
1732 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1733 &info->mti_body->mbo_fid2);
1735 GOTO(out, rc = PTR_ERR(o));
1737 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1738 GOTO(put, rc = -ENOENT);
1740 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1741 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1747 /* permission check. Make sure the calling process having permission
1748 * to write both files. */
1749 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1754 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1759 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1761 GOTO(put, rc = -EPROTO);
1763 lh1 = &info->mti_lh[MDT_LH_NEW];
1764 mdt_lock_reg_init(lh1, LCK_EX);
1765 lh2 = &info->mti_lh[MDT_LH_OLD];
1766 mdt_lock_reg_init(lh2, LCK_EX);
1768 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1769 MDS_INODELOCK_XATTR);
1773 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1774 MDS_INODELOCK_XATTR);
1778 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1779 mdt_object_child(o2), msl->msl_flags);
1783 mdt_swap_lov_flag(o1, o2);
1786 mdt_object_unlock(info, o2, lh2, rc);
1788 mdt_object_unlock(info, o1, lh1, rc);
1790 mdt_object_put(info->mti_env, o);
1792 mdt_thread_info_fini(info);
1796 static int mdt_raw_lookup(struct mdt_thread_info *info,
1797 struct mdt_object *parent,
1798 const struct lu_name *lname,
1799 struct ldlm_reply *ldlm_rep)
1801 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1805 LASSERT(!info->mti_cross_ref);
1807 /* Only got the fid of this obj by name */
1808 fid_zero(child_fid);
1809 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1810 lname, child_fid, &info->mti_spec);
1812 struct mdt_body *repbody;
1814 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1815 repbody->mbo_fid1 = *child_fid;
1816 repbody->mbo_valid = OBD_MD_FLID;
1817 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1818 } else if (rc == -ENOENT) {
1819 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1826 * UPDATE lock should be taken against parent, and be released before exit;
1827 * child_bits lock should be taken against child, and be returned back:
1828 * (1)normal request should release the child lock;
1829 * (2)intent request will grant the lock to client.
1831 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1832 struct mdt_lock_handle *lhc,
1834 struct ldlm_reply *ldlm_rep)
1836 struct ptlrpc_request *req = mdt_info_req(info);
1837 struct mdt_body *reqbody = NULL;
1838 struct mdt_object *parent = info->mti_object;
1839 struct mdt_object *child = NULL;
1840 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1841 struct lu_name *lname = NULL;
1842 struct mdt_lock_handle *lhp = NULL;
1843 struct ldlm_lock *lock;
1844 struct req_capsule *pill = info->mti_pill;
1852 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1853 LASSERT(ergo(is_resent,
1854 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1859 if (info->mti_cross_ref) {
1860 /* Only getattr on the child. Parent is on another node. */
1861 mdt_set_disposition(info, ldlm_rep,
1862 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1864 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1866 PFID(mdt_object_fid(child)), ldlm_rep);
1868 rc = mdt_check_resent_lock(info, child, lhc);
1871 } else if (rc > 0) {
1872 mdt_lock_handle_init(lhc);
1873 mdt_lock_reg_init(lhc, LCK_PR);
1876 * Object's name entry is on another MDS, it will
1877 * request PERM lock only because LOOKUP lock is owned
1878 * by the MDS where name entry resides.
1880 * TODO: it should try layout lock too. - Jinshan
1882 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1883 MDS_INODELOCK_LAYOUT);
1884 child_bits |= MDS_INODELOCK_PERM;
1886 rc = mdt_object_lock(info, child, lhc, child_bits);
1891 /* Finally, we can get attr for child. */
1892 if (!mdt_object_exists(child)) {
1893 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1895 "remote object doesn't exist.");
1896 mdt_object_unlock(info, child, lhc, 1);
1900 rc = mdt_getattr_internal(info, child, 0);
1901 if (unlikely(rc != 0)) {
1902 mdt_object_unlock(info, child, lhc, 1);
1906 rc = mdt_pack_secctx_in_reply(info, child);
1908 mdt_object_unlock(info, child, lhc, 1);
1912 rc = mdt_pack_encctx_in_reply(info, child);
1914 mdt_object_unlock(info, child, lhc, 1);
1918 lname = &info->mti_name;
1919 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1921 if (lu_name_is_valid(lname)) {
1922 if (mdt_object_remote(parent)) {
1923 CERROR("%s: parent "DFID" is on remote target\n",
1924 mdt_obd_name(info->mti_mdt),
1925 PFID(mdt_object_fid(parent)));
1929 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1930 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1931 PNAME(lname), ldlm_rep);
1933 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1934 if (unlikely(reqbody == NULL))
1935 RETURN(err_serious(-EPROTO));
1937 *child_fid = reqbody->mbo_fid2;
1938 if (unlikely(!fid_is_sane(child_fid)))
1939 RETURN(err_serious(-EINVAL));
1941 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1942 mdt_object_get(info->mti_env, parent);
1945 child = mdt_object_find(info->mti_env, info->mti_mdt,
1948 RETURN(PTR_ERR(child));
1951 if (mdt_object_remote(child)) {
1952 CERROR("%s: child "DFID" is on remote target\n",
1953 mdt_obd_name(info->mti_mdt),
1954 PFID(mdt_object_fid(child)));
1955 GOTO(out_child, rc = -EPROTO);
1958 /* don't fetch LOOKUP lock if it's remote object */
1959 rc = mdt_is_remote_object(info, parent, child);
1961 GOTO(out_child, rc);
1963 child_bits &= ~MDS_INODELOCK_LOOKUP;
1965 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1967 PFID(mdt_object_fid(parent)),
1968 PFID(&reqbody->mbo_fid2), ldlm_rep);
1971 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1973 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1974 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1976 "Parent doesn't exist!");
1977 GOTO(out_child, rc = -ESTALE);
1980 if (lu_name_is_valid(lname)) {
1981 /* Always allow to lookup ".." */
1982 if (unlikely(lname->ln_namelen == 2 &&
1983 lname->ln_name[0] == '.' &&
1984 lname->ln_name[1] == '.'))
1985 info->mti_spec.sp_permitted = 1;
1987 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1988 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1993 /* step 1: lock parent only if parent is a directory */
1994 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1995 lhp = &info->mti_lh[MDT_LH_PARENT];
1996 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1997 rc = mdt_object_lock(info, parent, lhp,
1998 MDS_INODELOCK_UPDATE);
1999 if (unlikely(rc != 0))
2003 /* step 2: lookup child's fid by name */
2004 fid_zero(child_fid);
2005 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2006 child_fid, &info->mti_spec);
2008 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2011 GOTO(unlock_parent, rc);
2013 child = mdt_object_find(info->mti_env, info->mti_mdt,
2015 if (unlikely(IS_ERR(child)))
2016 GOTO(unlock_parent, rc = PTR_ERR(child));
2019 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2021 /* step 3: lock child regardless if it is local or remote. */
2024 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2025 if (!mdt_object_exists(child)) {
2026 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2028 "Object doesn't exist!");
2029 GOTO(out_child, rc = -ENOENT);
2032 rc = mdt_check_resent_lock(info, child, lhc);
2034 GOTO(out_child, rc);
2035 } else if (rc > 0) {
2036 mdt_lock_handle_init(lhc);
2037 mdt_lock_reg_init(lhc, LCK_PR);
2039 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2040 !mdt_object_remote(child)) {
2041 struct md_attr *ma = &info->mti_attr;
2044 ma->ma_need = MA_INODE;
2045 rc = mdt_attr_get_complex(info, child, ma);
2046 if (unlikely(rc != 0))
2047 GOTO(out_child, rc);
2049 /* If the file has not been changed for some time, we
2050 * return not only a LOOKUP lock, but also an UPDATE
2051 * lock and this might save us RPC on later STAT. For
2052 * directories, it also let negative dentry cache start
2053 * working for this dir. */
2054 if (ma->ma_valid & MA_INODE &&
2055 ma->ma_attr.la_valid & LA_CTIME &&
2056 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2057 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2058 child_bits |= MDS_INODELOCK_UPDATE;
2061 /* layout lock must be granted in a best-effort way
2062 * for IT operations */
2063 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2064 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2065 !mdt_object_remote(child) && ldlm_rep != NULL) {
2066 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2067 exp_connect_layout(info->mti_exp)) {
2068 /* try to grant layout lock for regular file. */
2069 try_bits = MDS_INODELOCK_LAYOUT;
2071 /* Acquire DOM lock in advance for data-on-mdt file */
2072 if (child != parent)
2073 try_bits |= MDS_INODELOCK_DOM;
2076 if (try_bits != 0) {
2077 /* try layout lock, it may fail to be granted due to
2078 * contention at LOOKUP or UPDATE */
2079 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2081 if (child_bits & MDS_INODELOCK_LAYOUT)
2084 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2085 * client will enqueue the lock to the remote MDT */
2086 if (mdt_object_remote(child))
2087 child_bits &= ~MDS_INODELOCK_UPDATE;
2088 rc = mdt_object_lock(info, child, lhc, child_bits);
2090 if (unlikely(rc != 0))
2091 GOTO(out_child, rc);
2094 /* finally, we can get attr for child. */
2095 rc = mdt_getattr_internal(info, child, ma_need);
2096 if (unlikely(rc != 0)) {
2097 mdt_object_unlock(info, child, lhc, 1);
2098 GOTO(out_child, rc);
2101 rc = mdt_pack_secctx_in_reply(info, child);
2103 mdt_object_unlock(info, child, lhc, 1);
2104 GOTO(out_child, rc);
2107 rc = mdt_pack_encctx_in_reply(info, child);
2109 mdt_object_unlock(info, child, lhc, 1);
2110 GOTO(out_child, rc);
2113 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2115 /* Debugging code. */
2116 LDLM_DEBUG(lock, "Returning lock to client");
2117 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2118 &lock->l_resource->lr_name),
2119 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2120 PLDLMRES(lock->l_resource),
2121 PFID(mdt_object_fid(child)));
2123 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2124 !mdt_object_remote(child) && child != parent) {
2125 mdt_object_put(info->mti_env, child);
2126 rc = mdt_pack_size2body(info, child_fid,
2128 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2129 /* DOM lock was taken in advance but this is
2130 * not DoM file. Drop the lock.
2132 lock_res_and_lock(lock);
2133 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2134 unlock_res_and_lock(lock);
2136 LDLM_LOCK_PUT(lock);
2137 GOTO(unlock_parent, rc = 0);
2139 LDLM_LOCK_PUT(lock);
2145 mdt_object_put(info->mti_env, child);
2148 mdt_object_unlock(info, parent, lhp, 1);
2152 /* normal handler: should release the child lock */
2153 static int mdt_getattr_name(struct tgt_session_info *tsi)
2155 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2156 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2157 struct mdt_body *reqbody;
2158 struct mdt_body *repbody;
2163 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2164 LASSERT(reqbody != NULL);
2165 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2166 LASSERT(repbody != NULL);
2168 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2169 repbody->mbo_eadatasize = 0;
2170 repbody->mbo_aclsize = 0;
2172 rc = mdt_init_ucred(info, reqbody);
2174 GOTO(out_shrink, rc);
2176 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2177 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2178 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2179 lhc->mlh_reg_lh.cookie = 0;
2181 mdt_exit_ucred(info);
2184 mdt_client_compatibility(info);
2185 rc2 = mdt_fix_reply(info);
2188 mdt_thread_info_fini(info);
2192 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2193 const struct lu_fid *pfid,
2194 const struct lu_name *name,
2195 struct mdt_object *obj, s64 ctime)
2197 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2198 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2199 struct mdt_device *mdt = info->mti_mdt;
2200 struct md_attr *ma = &info->mti_attr;
2201 struct mdt_lock_handle *parent_lh;
2202 struct mdt_lock_handle *child_lh;
2203 struct mdt_object *pobj;
2204 bool cos_incompat = false;
2208 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2210 GOTO(out, rc = PTR_ERR(pobj));
2212 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2213 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2214 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2216 GOTO(put_parent, rc);
2218 if (mdt_object_remote(pobj))
2219 cos_incompat = true;
2221 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2222 name, child_fid, &info->mti_spec);
2224 GOTO(unlock_parent, rc);
2226 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2227 GOTO(unlock_parent, rc = -EREMCHG);
2229 child_lh = &info->mti_lh[MDT_LH_CHILD];
2230 mdt_lock_reg_init(child_lh, LCK_EX);
2231 rc = mdt_reint_striped_lock(info, obj, child_lh,
2232 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2233 einfo, cos_incompat);
2235 GOTO(unlock_parent, rc);
2237 if (atomic_read(&obj->mot_open_count)) {
2238 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2239 PFID(mdt_object_fid(obj)));
2240 GOTO(unlock_child, rc = -EBUSY);
2244 ma->ma_valid = MA_INODE;
2245 ma->ma_attr.la_valid = LA_CTIME;
2246 ma->ma_attr.la_ctime = ctime;
2248 mutex_lock(&obj->mot_lov_mutex);
2250 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2251 mdt_object_child(obj), name, ma, 0);
2253 mutex_unlock(&obj->mot_lov_mutex);
2256 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2258 mdt_object_unlock(info, pobj, parent_lh, 1);
2260 mdt_object_put(info->mti_env, pobj);
2265 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2266 struct mdt_object *obj)
2268 struct lu_ucred *uc = lu_ucred(info->mti_env);
2269 struct md_attr *ma = &info->mti_attr;
2270 struct lu_attr *la = &ma->ma_attr;
2274 ma->ma_need = MA_INODE;
2275 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2279 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2282 if (md_capable(uc, CAP_DAC_OVERRIDE))
2284 if (uc->uc_fsuid == la->la_uid) {
2285 if ((la->la_mode & S_IWUSR) == 0)
2287 } else if (uc->uc_fsgid == la->la_gid) {
2288 if ((la->la_mode & S_IWGRP) == 0)
2290 } else if ((la->la_mode & S_IWOTH) == 0) {
2298 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2301 struct mdt_device *mdt = info->mti_mdt;
2302 struct mdt_object *obj = NULL;
2303 struct linkea_data ldata = { NULL };
2304 struct lu_buf *buf = &info->mti_big_buf;
2305 struct lu_name *name = &info->mti_name;
2306 struct lu_fid *pfid = &info->mti_tmp_fid1;
2307 struct link_ea_header *leh;
2308 struct link_ea_entry *lee;
2309 int reclen, count, rc = 0;
2312 if (!fid_is_sane(fid))
2313 GOTO(out, rc = -EINVAL);
2315 if (!fid_is_namespace_visible(fid))
2316 GOTO(out, rc = -EINVAL);
2318 obj = mdt_object_find(info->mti_env, mdt, fid);
2320 GOTO(out, rc = PTR_ERR(obj));
2322 if (mdt_object_remote(obj))
2323 GOTO(out, rc = -EREMOTE);
2324 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2325 GOTO(out, rc = -ENOENT);
2327 rc = mdt_rmfid_check_permission(info, obj);
2332 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2334 GOTO(out, rc = -ENOMEM);
2337 rc = mdt_links_read(info, obj, &ldata);
2342 lee = (struct link_ea_entry *)(leh + 1);
2343 for (count = 0; count < leh->leh_reccount; count++) {
2344 /* remove every hardlink */
2345 linkea_entry_unpack(lee, &reclen, name, pfid);
2346 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2347 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2353 if (obj && !IS_ERR(obj))
2354 mdt_object_put(info->mti_env, obj);
2355 if (info->mti_big_buf.lb_buf)
2356 lu_buf_free(&info->mti_big_buf);
2361 static int mdt_rmfid(struct tgt_session_info *tsi)
2363 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2364 struct mdt_body *reqbody;
2365 struct lu_fid *fids, *rfids;
2371 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2372 if (reqbody == NULL)
2374 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2376 nr = bufsize / sizeof(struct lu_fid);
2377 if (nr * sizeof(struct lu_fid) != bufsize)
2379 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2380 RCL_SERVER, nr * sizeof(__u32));
2381 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2382 RCL_SERVER, nr * sizeof(struct lu_fid));
2383 rc = req_capsule_server_pack(tsi->tsi_pill);
2385 GOTO(out, rc = err_serious(rc));
2386 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2389 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2391 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2394 mdt_init_ucred(mti, reqbody);
2395 for (i = 0; i < nr; i++) {
2397 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2399 mdt_exit_ucred(mti);
2405 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2406 void *karg, void __user *uarg);
2408 static int mdt_set_info(struct tgt_session_info *tsi)
2410 struct ptlrpc_request *req = tgt_ses_req(tsi);
2413 int keylen, vallen, rc = 0;
2417 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2419 DEBUG_REQ(D_HA, req, "no set_info key");
2420 RETURN(err_serious(-EFAULT));
2423 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2426 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2428 DEBUG_REQ(D_HA, req, "no set_info val");
2429 RETURN(err_serious(-EFAULT));
2432 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2435 /* Swab any part of val you need to here */
2436 if (KEY_IS(KEY_READ_ONLY)) {
2437 spin_lock(&req->rq_export->exp_lock);
2439 *exp_connect_flags_ptr(req->rq_export) |=
2442 *exp_connect_flags_ptr(req->rq_export) &=
2443 ~OBD_CONNECT_RDONLY;
2444 spin_unlock(&req->rq_export->exp_lock);
2445 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2446 struct changelog_setinfo *cs = val;
2448 if (vallen != sizeof(*cs)) {
2449 CERROR("%s: bad changelog_clear setinfo size %d\n",
2450 tgt_name(tsi->tsi_tgt), vallen);
2453 if (ptlrpc_req_need_swab(req)) {
2454 __swab64s(&cs->cs_recno);
2455 __swab32s(&cs->cs_id);
2458 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2460 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2462 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2464 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2471 static int mdt_readpage(struct tgt_session_info *tsi)
2473 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2474 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2475 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2476 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2477 struct mdt_body *repbody;
2483 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2484 RETURN(err_serious(-ENOMEM));
2486 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2487 if (repbody == NULL || reqbody == NULL)
2488 RETURN(err_serious(-EFAULT));
2491 * prepare @rdpg before calling lower layers and transfer itself. Here
2492 * reqbody->size contains offset of where to start to read and
2493 * reqbody->nlink contains number bytes to read.
2495 rdpg->rp_hash = reqbody->mbo_size;
2496 if (rdpg->rp_hash != reqbody->mbo_size) {
2497 CERROR("Invalid hash: %#llx != %#llx\n",
2498 rdpg->rp_hash, reqbody->mbo_size);
2502 rdpg->rp_attrs = reqbody->mbo_mode;
2503 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2504 rdpg->rp_attrs |= LUDA_64BITHASH;
2505 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2506 exp_max_brw_size(tsi->tsi_exp));
2507 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2509 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2510 if (rdpg->rp_pages == NULL)
2513 for (i = 0; i < rdpg->rp_npages; ++i) {
2514 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2515 if (rdpg->rp_pages[i] == NULL)
2516 GOTO(free_rdpg, rc = -ENOMEM);
2519 /* call lower layers to fill allocated pages with directory data */
2520 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2522 GOTO(free_rdpg, rc);
2524 /* send pages to client */
2525 rc = tgt_sendpage(tsi, rdpg, rc);
2530 for (i = 0; i < rdpg->rp_npages; i++)
2531 if (rdpg->rp_pages[i] != NULL)
2532 __free_page(rdpg->rp_pages[i]);
2533 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2535 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2541 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2543 struct lu_ucred *uc = mdt_ucred_check(info);
2544 struct lu_attr *attr = &info->mti_attr.ma_attr;
2549 if (op != REINT_SETATTR) {
2550 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2551 attr->la_uid = uc->uc_fsuid;
2552 /* for S_ISGID, inherit gid from his parent, such work will be
2553 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2554 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2555 attr->la_gid = uc->uc_fsgid;
2561 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2563 return op == REINT_OPEN &&
2564 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2567 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2569 struct req_capsule *pill = info->mti_pill;
2571 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2573 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2575 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2577 /* pre-set size in server part with max size */
2578 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2580 OBD_MAX_DEFAULT_EA_SIZE);
2582 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2587 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
2589 struct req_capsule *pill = info->mti_pill;
2591 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
2593 /* pre-set size in server part with max size */
2594 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
2596 info->mti_mdt->mdt_max_mdsize);
2599 static int mdt_reint_internal(struct mdt_thread_info *info,
2600 struct mdt_lock_handle *lhc,
2603 struct req_capsule *pill = info->mti_pill;
2604 struct mdt_body *repbody;
2609 rc = mdt_reint_unpack(info, op);
2611 CERROR("Can't unpack reint, rc %d\n", rc);
2612 RETURN(err_serious(rc));
2616 /* check if the file system is set to readonly. O_RDONLY open
2617 * is still allowed even the file system is set to readonly mode */
2618 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2619 RETURN(err_serious(-EROFS));
2621 /* for replay (no_create) lmm is not needed, client has it already */
2622 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2623 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2626 /* llog cookies are always 0, the field is kept for compatibility */
2627 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2628 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2630 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2631 * by default. If the target object has more ACL entries, then
2632 * enlarge the buffer when necessary. */
2633 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2634 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2635 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2637 mdt_preset_secctx_size(info);
2638 mdt_preset_encctx_size(info);
2640 rc = req_capsule_server_pack(pill);
2642 CERROR("Can't pack response, rc %d\n", rc);
2643 RETURN(err_serious(rc));
2646 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2647 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2649 repbody->mbo_eadatasize = 0;
2650 repbody->mbo_aclsize = 0;
2653 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2655 /* for replay no cookkie / lmm need, because client have this already */
2656 if (info->mti_spec.no_create)
2657 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2658 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2660 rc = mdt_init_ucred_reint(info);
2662 GOTO(out_shrink, rc);
2664 rc = mdt_fix_attr_ucred(info, op);
2666 GOTO(out_ucred, rc = err_serious(rc));
2668 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2670 GOTO(out_ucred, rc);
2671 } else if (rc == 1) {
2672 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2673 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2674 GOTO(out_ucred, rc);
2676 rc = mdt_reint_rec(info, lhc);
2679 mdt_exit_ucred(info);
2681 mdt_client_compatibility(info);
2683 rc2 = mdt_fix_reply(info);
2688 * Data-on-MDT optimization - read data along with OPEN and return it
2689 * in reply when possible.
2691 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2692 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2698 static long mdt_reint_opcode(struct ptlrpc_request *req,
2699 const struct req_format **fmt)
2701 struct mdt_device *mdt;
2702 struct mdt_rec_reint *rec;
2705 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2707 opc = rec->rr_opcode;
2708 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2709 if (opc < REINT_MAX && fmt[opc] != NULL)
2710 req_capsule_extend(&req->rq_pill, fmt[opc]);
2712 mdt = mdt_exp2dev(req->rq_export);
2713 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2714 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2715 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2716 opc = err_serious(-EFAULT);
2719 opc = err_serious(-EFAULT);
2724 static int mdt_reint(struct tgt_session_info *tsi)
2728 static const struct req_format *reint_fmts[REINT_MAX] = {
2729 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2730 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2731 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2732 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2733 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2734 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2735 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2736 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2737 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
2738 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2743 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2745 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2747 * No lock possible here from client to pass it to reint code
2750 rc = mdt_reint_internal(info, NULL, opc);
2751 mdt_thread_info_fini(info);
2756 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2760 /* this should sync the whole device */
2761 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2763 struct dt_device *dt = mdt->mdt_bottom;
2767 rc = dt->dd_ops->dt_sync(env, dt);
2771 /* this should sync this object */
2772 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2773 struct mdt_object *mo)
2779 if (!mdt_object_exists(mo)) {
2780 CWARN("%s: non existing object "DFID": rc = %d\n",
2781 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2786 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2787 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2788 dt_obj_version_t version;
2790 version = dt_version_get(env, mdt_obj2dt(mo));
2791 if (version > tgt->lut_obd->obd_last_committed)
2792 rc = mo_object_sync(env, mdt_object_child(mo));
2794 rc = mo_object_sync(env, mdt_object_child(mo));
2800 static int mdt_sync(struct tgt_session_info *tsi)
2802 struct ptlrpc_request *req = tgt_ses_req(tsi);
2803 struct req_capsule *pill = tsi->tsi_pill;
2804 struct mdt_body *body;
2805 ktime_t kstart = ktime_get();
2810 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2811 RETURN(err_serious(-ENOMEM));
2813 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2814 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2816 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2818 if (unlikely(info->mti_object == NULL))
2821 /* sync an object */
2822 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2825 const struct lu_fid *fid;
2826 struct lu_attr *la = &info->mti_attr.ma_attr;
2828 info->mti_attr.ma_need = MA_INODE;
2829 info->mti_attr.ma_valid = 0;
2830 rc = mdt_attr_get_complex(info, info->mti_object,
2833 body = req_capsule_server_get(pill,
2835 fid = mdt_object_fid(info->mti_object);
2836 mdt_pack_attr2body(info, body, la, fid);
2839 mdt_thread_info_fini(info);
2842 mdt_counter_incr(req, LPROC_MDT_SYNC,
2843 ktime_us_delta(ktime_get(), kstart));
2848 static int mdt_data_sync(struct tgt_session_info *tsi)
2850 struct mdt_thread_info *info;
2851 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2852 struct ost_body *body = tsi->tsi_ost_body;
2853 struct ost_body *repbody;
2854 struct mdt_object *mo = NULL;
2860 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2862 /* if no fid is specified then do nothing,
2863 * device sync is done via MDS_SYNC */
2864 if (fid_is_zero(&tsi->tsi_fid))
2867 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2869 RETURN(PTR_ERR(mo));
2871 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2875 repbody->oa.o_oi = body->oa.o_oi;
2876 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2878 info = tsi2mdt_info(tsi);
2879 ma = &info->mti_attr;
2880 ma->ma_need = MA_INODE;
2882 rc = mdt_attr_get_complex(info, mo, ma);
2884 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2887 mdt_thread_info_fini(info);
2892 mdt_object_put(tsi->tsi_env, mo);
2897 * Handle quota control requests to consult current usage/limit, but also
2898 * to configure quota enforcement
2900 static int mdt_quotactl(struct tgt_session_info *tsi)
2902 struct obd_export *exp = tsi->tsi_exp;
2903 struct req_capsule *pill = tsi->tsi_pill;
2904 struct obd_quotactl *oqctl, *repoqc;
2906 struct mdt_device *mdt = mdt_exp2dev(exp);
2907 struct lu_device *qmt = mdt->mdt_qmt_dev;
2908 struct lu_nodemap *nodemap;
2911 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2913 RETURN(err_serious(-EPROTO));
2915 rc = req_capsule_server_pack(pill);
2917 RETURN(err_serious(rc));
2919 nodemap = nodemap_get_from_exp(exp);
2920 if (IS_ERR(nodemap))
2921 RETURN(PTR_ERR(nodemap));
2923 switch (oqctl->qc_cmd) {
2924 /* master quotactl */
2927 case LUSTRE_Q_SETDEFAULT:
2928 case LUSTRE_Q_SETQUOTAPOOL:
2929 case LUSTRE_Q_SETINFOPOOL:
2930 if (!nodemap_can_setquota(nodemap))
2931 GOTO(out_nodemap, rc = -EPERM);
2935 case LUSTRE_Q_GETDEFAULT:
2936 case LUSTRE_Q_GETQUOTAPOOL:
2937 case LUSTRE_Q_GETINFOPOOL:
2939 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2940 /* slave quotactl */
2947 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2948 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2949 GOTO(out_nodemap, rc);
2953 switch (oqctl->qc_type) {
2955 id = nodemap_map_id(nodemap, NODEMAP_UID,
2956 NODEMAP_CLIENT_TO_FS, id);
2959 id = nodemap_map_id(nodemap, NODEMAP_GID,
2960 NODEMAP_CLIENT_TO_FS, id);
2963 /* todo: check/map project id */
2967 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2969 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2971 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2973 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2974 barrier_exit(tsi->tsi_tgt->lut_bottom);
2976 if (oqctl->qc_id != id)
2977 swap(oqctl->qc_id, id);
2979 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2980 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2981 RETURN(-EINPROGRESS);
2984 switch (oqctl->qc_cmd) {
2990 case LUSTRE_Q_SETDEFAULT:
2991 case LUSTRE_Q_GETDEFAULT:
2992 case LUSTRE_Q_SETQUOTAPOOL:
2993 case LUSTRE_Q_GETQUOTAPOOL:
2994 case LUSTRE_Q_SETINFOPOOL:
2995 case LUSTRE_Q_GETINFOPOOL:
2996 /* forward quotactl request to QMT */
2997 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3002 /* slave quotactl */
3003 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3008 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3009 GOTO(out_nodemap, rc = -EFAULT);
3012 if (oqctl->qc_id != id)
3013 swap(oqctl->qc_id, id);
3015 QCTL_COPY(repoqc, oqctl);
3019 nodemap_putref(nodemap);
3024 /** clone llog ctxt from child (mdd)
3025 * This allows remote llog (replicator) access.
3026 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3027 * context was originally set up, or we can handle them directly.
3028 * I choose the latter, but that means I need any llog
3029 * contexts set up by child to be accessable by the mdt. So we clone the
3030 * context into our context list here.
3032 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3035 struct md_device *next = mdt->mdt_child;
3036 struct llog_ctxt *ctxt;
3039 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3042 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3043 if (rc || ctxt == NULL) {
3047 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3049 CERROR("Can't set mdt ctxt %d\n", rc);
3054 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3055 struct mdt_device *mdt, int idx)
3057 struct llog_ctxt *ctxt;
3059 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3062 /* Put once for the get we just did, and once for the clone */
3063 llog_ctxt_put(ctxt);
3064 llog_ctxt_put(ctxt);
3069 * sec context handlers
3071 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3073 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3079 * quota request handlers
3081 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3083 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3084 struct lu_device *qmt = mdt->mdt_qmt_dev;
3089 RETURN(err_serious(-EOPNOTSUPP));
3091 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3095 struct mdt_object *mdt_object_new(const struct lu_env *env,
3096 struct mdt_device *d,
3097 const struct lu_fid *f)
3099 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3100 struct lu_object *o;
3101 struct mdt_object *m;
3104 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3105 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3106 if (unlikely(IS_ERR(o)))
3107 m = (struct mdt_object *)o;
3113 struct mdt_object *mdt_object_find(const struct lu_env *env,
3114 struct mdt_device *d,
3115 const struct lu_fid *f)
3117 struct lu_object *o;
3118 struct mdt_object *m;
3121 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3122 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3123 if (unlikely(IS_ERR(o)))
3124 m = (struct mdt_object *)o;
3132 * Asyncronous commit for mdt device.
3134 * Pass asynchonous commit call down the MDS stack.
3136 * \param env environment
3137 * \param mdt the mdt device
3139 static void mdt_device_commit_async(const struct lu_env *env,
3140 struct mdt_device *mdt)
3142 struct dt_device *dt = mdt->mdt_bottom;
3146 rc = dt->dd_ops->dt_commit_async(env, dt);
3147 if (unlikely(rc != 0))
3148 CWARN("%s: async commit start failed: rc = %d\n",
3149 mdt_obd_name(mdt), rc);
3150 atomic_inc(&mdt->mdt_async_commit_count);
3155 * Mark the lock as "synchonous".
3157 * Mark the lock to deffer transaction commit to the unlock time.
3159 * \param lock the lock to mark as "synchonous"
3161 * \see mdt_is_lock_sync
3162 * \see mdt_save_lock
3164 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3166 lock->l_ast_data = (void*)1;
3170 * Check whehter the lock "synchonous" or not.
3172 * \param lock the lock to check
3173 * \retval 1 the lock is "synchonous"
3174 * \retval 0 the lock isn't "synchronous"
3176 * \see mdt_set_lock_sync
3177 * \see mdt_save_lock
3179 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3181 return lock->l_ast_data != NULL;
3185 * Blocking AST for mdt locks.
3187 * Starts transaction commit if in case of COS lock conflict or
3188 * deffers such a commit to the mdt_save_lock.
3190 * \param lock the lock which blocks a request or cancelling lock
3191 * \param desc unused
3192 * \param data unused
3193 * \param flag indicates whether this cancelling or blocking callback
3195 * \see ldlm_blocking_ast_nocheck
3197 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3198 void *data, int flag)
3200 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3201 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3202 struct ldlm_cb_set_arg *arg = data;
3203 bool commit_async = false;
3207 if (flag == LDLM_CB_CANCELING)
3210 lock_res_and_lock(lock);
3211 if (lock->l_blocking_ast != mdt_blocking_ast) {
3212 unlock_res_and_lock(lock);
3216 /* A blocking ast may be sent from ldlm_lock_decref_internal
3217 * when the last reference to a local lock was released and
3218 * during blocking event from ldlm_work_bl_ast_lock().
3219 * The 'data' parameter is l_ast_data in the first case and
3220 * callback arguments in the second one. Distinguish them by that.
3222 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3223 goto skip_cos_checks;
3225 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3226 if (mdt_cos_is_enabled(mdt)) {
3227 if (!arg->bl_desc->bl_same_client)
3228 mdt_set_lock_sync(lock);
3229 } else if (mdt_slc_is_enabled(mdt) &&
3230 arg->bl_desc->bl_cos_incompat) {
3231 mdt_set_lock_sync(lock);
3233 * we may do extra commit here, but there is a small
3234 * window to miss a commit: lock was unlocked (saved),
3235 * then a conflict lock queued and we come here, but
3236 * REP-ACK not received, so lock was not converted to
3238 * Fortunately this window is quite small, so the
3239 * extra commit should be rare (not to say distributed
3240 * operation is rare too).
3242 commit_async = true;
3244 } else if (lock->l_req_mode == LCK_COS) {
3245 commit_async = true;
3249 rc = ldlm_blocking_ast_nocheck(lock);
3254 rc = lu_env_init(&env, LCT_LOCAL);
3255 if (unlikely(rc != 0))
3256 CWARN("%s: lu_env initialization failed, cannot "
3257 "start asynchronous commit: rc = %d\n",
3260 mdt_device_commit_async(&env, mdt);
3267 * Blocking AST for cross-MDT lock
3269 * Discard lock from uncommitted_slc_locks and cancel it.
3271 * \param lock the lock which blocks a request or cancelling lock
3272 * \param desc unused
3273 * \param data unused
3274 * \param flag indicates whether this cancelling or blocking callback
3275 * \retval 0 on success
3276 * \retval negative number on error
3278 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3279 void *data, int flag)
3285 case LDLM_CB_BLOCKING: {
3286 struct lustre_handle lockh;
3288 ldlm_lock2handle(lock, &lockh);
3289 rc = ldlm_cli_cancel(&lockh,
3290 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3292 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3297 case LDLM_CB_CANCELING: {
3298 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3299 struct mdt_device *mdt =
3300 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3302 LDLM_DEBUG(lock, "Revoke remote lock\n");
3304 /* discard slc lock here so that it can be cleaned anytime,
3305 * especially for cleanup_resource() */
3306 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3308 /* once we cache lock, l_ast_data is set to mdt_object */
3309 if (lock->l_ast_data != NULL) {
3310 struct mdt_object *mo = lock->l_ast_data;
3313 rc = lu_env_init(&env, LCT_MD_THREAD);
3314 if (unlikely(rc != 0)) {
3315 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3317 PFID(mdt_object_fid(mo)), rc);
3321 if (lock->l_policy_data.l_inodebits.bits &
3322 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3323 rc = mo_invalidate(&env, mdt_object_child(mo));
3324 mo->mot_cache_attr = 0;
3326 mdt_object_put(&env, mo);
3338 int mdt_check_resent_lock(struct mdt_thread_info *info,
3339 struct mdt_object *mo,
3340 struct mdt_lock_handle *lhc)
3342 /* the lock might already be gotten in ldlm_handle_enqueue() */
3343 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3344 struct ptlrpc_request *req = mdt_info_req(info);
3345 struct ldlm_lock *lock;
3347 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3348 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3350 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3351 * a resend case, however, it could be already destroyed
3352 * due to client eviction or a raced cancel RPC. */
3353 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3354 lhc->mlh_reg_lh.cookie);
3358 if (!fid_res_name_eq(mdt_object_fid(mo),
3359 &lock->l_resource->lr_name)) {
3360 CWARN("%s: Although resent, but still not "
3361 "get child lock:"DFID"\n",
3362 info->mti_exp->exp_obd->obd_name,
3363 PFID(mdt_object_fid(mo)));
3364 LDLM_LOCK_PUT(lock);
3367 LDLM_LOCK_PUT(lock);
3373 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3375 mdt_object_get(NULL, lock->l_ast_data);
3378 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3379 struct mdt_object *o, const struct lu_fid *fid,
3380 struct lustre_handle *lh, enum ldlm_mode mode,
3381 __u64 *ibits, __u64 trybits, bool cache)
3383 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3384 union ldlm_policy_data *policy = &mti->mti_policy;
3385 struct ldlm_res_id *res_id = &mti->mti_res_id;
3389 LASSERT(mdt_object_remote(o));
3391 fid_build_reg_res_name(fid, res_id);
3393 memset(einfo, 0, sizeof(*einfo));
3394 einfo->ei_type = LDLM_IBITS;
3395 einfo->ei_mode = mode;
3396 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3397 einfo->ei_cb_cp = ldlm_completion_ast;
3398 einfo->ei_enq_slave = 0;
3399 einfo->ei_res_id = res_id;
3403 * if we cache lock, couple lock with mdt_object, so that object
3404 * can be easily found in lock ASTs.
3406 einfo->ei_cbdata = o;
3407 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3410 memset(policy, 0, sizeof(*policy));
3411 policy->l_inodebits.bits = *ibits;
3412 policy->l_inodebits.try_bits = trybits;
3414 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3417 /* Return successfully acquired bits to a caller */
3419 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3422 *ibits = lock->l_policy_data.l_inodebits.bits;
3423 LDLM_LOCK_PUT(lock);
3428 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3429 const struct lu_fid *fid, struct lustre_handle *lh,
3430 enum ldlm_mode mode, __u64 ibits, bool cache)
3432 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3436 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3437 struct mdt_lock_handle *lh, __u64 *ibits,
3438 __u64 trybits, bool cos_incompat)
3440 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3441 union ldlm_policy_data *policy = &info->mti_policy;
3442 struct ldlm_res_id *res_id = &info->mti_res_id;
3443 __u64 dlmflags = 0, *cookie = NULL;
3447 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3448 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3449 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3450 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3453 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3454 lh->mlh_reg_mode == LCK_EX);
3455 dlmflags |= LDLM_FL_COS_INCOMPAT;
3456 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3457 dlmflags |= LDLM_FL_COS_ENABLED;
3460 /* Only enqueue LOOKUP lock for remote object */
3461 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3463 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3464 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3465 *ibits == MDS_INODELOCK_OPEN)
3466 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3468 if (lh->mlh_type == MDT_PDO_LOCK) {
3469 /* check for exists after object is locked */
3470 if (mdt_object_exists(o) == 0) {
3471 /* Non-existent object shouldn't have PDO lock */
3474 /* Non-dir object shouldn't have PDO lock */
3475 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3480 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3481 dlmflags |= LDLM_FL_ATOMIC_CB;
3484 cookie = &info->mti_exp->exp_handle.h_cookie;
3487 * Take PDO lock on whole directory and build correct @res_id for lock
3488 * on part of directory.
3490 if (lh->mlh_pdo_hash != 0) {
3491 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3492 mdt_lock_pdo_mode(info, o, lh);
3493 if (lh->mlh_pdo_mode != LCK_NL) {
3495 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3496 * is never going to be sent to client and we do not
3497 * want it slowed down due to possible cancels.
3499 policy->l_inodebits.bits =
3500 *ibits & MDS_INODELOCK_UPDATE;
3501 policy->l_inodebits.try_bits =
3502 trybits & MDS_INODELOCK_UPDATE;
3503 /* at least one of them should be set */
3504 LASSERT(policy->l_inodebits.bits |
3505 policy->l_inodebits.try_bits);
3506 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3507 lh->mlh_pdo_mode, policy, res_id,
3509 if (unlikely(rc != 0))
3510 GOTO(out_unlock, rc);
3514 * Finish res_id initializing by name hash marking part of
3515 * directory which is taking modification.
3517 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3520 policy->l_inodebits.bits = *ibits;
3521 policy->l_inodebits.try_bits = trybits;
3522 policy->l_inodebits.li_gid = lh->mlh_gid;
3525 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3526 * going to be sent to client. If it is - mdt_intent_policy() path will
3527 * fix it up and turn FL_LOCAL flag off.
3529 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3530 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3534 mdt_object_unlock(info, o, lh, 1);
3535 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3536 lh->mlh_pdo_hash != 0 &&
3537 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3538 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3540 /* Return successfully acquired bits to a caller */
3542 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3545 *ibits = lock->l_policy_data.l_inodebits.bits;
3546 LDLM_LOCK_PUT(lock);
3552 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3553 struct mdt_lock_handle *lh, __u64 *ibits,
3554 __u64 trybits, bool cos_incompat)
3556 struct mdt_lock_handle *local_lh = NULL;
3560 if (!mdt_object_remote(o)) {
3561 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3566 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3567 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3568 MDS_INODELOCK_XATTR);
3570 /* Only enqueue LOOKUP lock for remote object */
3571 if (*ibits & MDS_INODELOCK_LOOKUP) {
3572 __u64 local = MDS_INODELOCK_LOOKUP;
3574 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3582 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3583 /* Sigh, PDO needs to enqueue 2 locks right now, but
3584 * enqueue RPC can only request 1 lock, to avoid extra
3585 * RPC, so it will instead enqueue EX lock for remote
3586 * object anyway XXX*/
3587 if (lh->mlh_type == MDT_PDO_LOCK &&
3588 lh->mlh_pdo_hash != 0) {
3590 "%s: "DFID" convert PDO lock to EX lock.\n",
3591 mdt_obd_name(info->mti_mdt),
3592 PFID(mdt_object_fid(o)));
3593 lh->mlh_pdo_hash = 0;
3594 lh->mlh_rreg_mode = LCK_EX;
3595 lh->mlh_type = MDT_REG_LOCK;
3598 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3601 ibits, trybits, false);
3602 if (rc != ELDLM_OK) {
3603 if (local_lh != NULL)
3604 mdt_object_unlock(info, o, local_lh, rc);
3609 /* other components like LFSCK can use lockless access
3610 * and populate cache, so we better invalidate it */
3611 mo_invalidate(info->mti_env, mdt_object_child(o));
3616 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3617 struct mdt_lock_handle *lh, __u64 ibits)
3619 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3622 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3623 struct mdt_lock_handle *lh, __u64 ibits,
3626 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3627 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3631 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3632 struct mdt_lock_handle *lh, __u64 *ibits,
3633 __u64 trybits, bool cos_incompat)
3635 bool trylock_only = *ibits == 0;
3638 LASSERT(!(*ibits & trybits));
3639 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3641 if (rc && trylock_only) { /* clear error for try ibits lock only */
3642 LASSERT(*ibits == 0);
3649 * Save a lock within request object.
3651 * Keep the lock referenced until whether client ACK or transaction
3652 * commit happens or release the lock immediately depending on input
3653 * parameters. If COS is ON, a write lock is converted to COS lock
3656 * \param info thead info object
3657 * \param h lock handle
3658 * \param mode lock mode
3659 * \param decref force immediate lock releasing
3661 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3662 enum ldlm_mode mode, int decref)
3666 if (lustre_handle_is_used(h)) {
3667 if (decref || !info->mti_has_trans ||
3668 !(mode & (LCK_PW | LCK_EX))) {
3669 mdt_fid_unlock(h, mode);
3671 struct mdt_device *mdt = info->mti_mdt;
3672 struct ldlm_lock *lock = ldlm_handle2lock(h);
3673 struct ptlrpc_request *req = mdt_info_req(info);
3674 bool cos = mdt_cos_is_enabled(mdt);
3675 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3677 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3680 /* there is no request if mdt_object_unlock() is called
3681 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3682 if (likely(req != NULL)) {
3683 LDLM_DEBUG(lock, "save lock request %p reply "
3684 "state %p transno %lld\n", req,
3685 req->rq_reply_state, req->rq_transno);
3687 ldlm_lock_mode_downgrade(lock, LCK_COS);
3690 if (req->rq_export->exp_disconnected)
3691 mdt_fid_unlock(h, mode);
3693 ptlrpc_save_lock(req, h, mode, cos,
3696 mdt_fid_unlock(h, mode);
3698 if (mdt_is_lock_sync(lock)) {
3699 CDEBUG(D_HA, "found sync-lock,"
3700 " async commit started\n");
3701 mdt_device_commit_async(info->mti_env,
3704 LDLM_LOCK_PUT(lock);
3713 * Save cross-MDT lock in uncommitted_slc_locks
3715 * Keep the lock referenced until transaction commit happens or release the lock
3716 * immediately depending on input parameters.
3718 * \param info thead info object
3719 * \param h lock handle
3720 * \param mode lock mode
3721 * \param decref force immediate lock releasing
3723 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3724 struct mdt_object *o, struct lustre_handle *h,
3725 enum ldlm_mode mode, int decref)
3729 if (lustre_handle_is_used(h)) {
3730 struct ldlm_lock *lock = ldlm_handle2lock(h);
3731 struct ptlrpc_request *req = mdt_info_req(info);
3734 (lock->l_policy_data.l_inodebits.bits &
3735 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3736 mo_invalidate(info->mti_env, mdt_object_child(o));
3738 if (decref || !info->mti_has_trans || !req ||
3739 !(mode & (LCK_PW | LCK_EX))) {
3740 ldlm_lock_decref_and_cancel(h, mode);
3741 LDLM_LOCK_PUT(lock);
3743 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3745 ldlm_lock_decref(h, mode);
3754 * Unlock mdt object.
3756 * Immeditely release the regular lock and the PDO lock or save the
3757 * lock in request and keep them referenced until client ACK or
3758 * transaction commit.
3760 * \param info thread info object
3761 * \param o mdt object
3762 * \param lh mdt lock handle referencing regular and PDO locks
3763 * \param decref force immediate lock releasing
3765 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3767 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3768 struct mdt_lock_handle *lh, int decref)
3772 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3773 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3774 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3780 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3781 const struct lu_fid *f,
3782 struct mdt_lock_handle *lh,
3785 struct mdt_object *o;
3787 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3791 rc = mdt_object_lock(info, o, lh, ibits);
3793 mdt_object_put(info->mti_env, o);
3800 void mdt_object_unlock_put(struct mdt_thread_info * info,
3801 struct mdt_object * o,
3802 struct mdt_lock_handle *lh,
3805 mdt_object_unlock(info, o, lh, decref);
3806 mdt_object_put(info->mti_env, o);
3810 * Generic code handling requests that have struct mdt_body passed in:
3812 * - extract mdt_body from request and save it in @info, if present;
3814 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3817 * - if HAS_BODY flag is set for this request type check whether object
3818 * actually exists on storage (lu_object_exists()).
3821 static int mdt_body_unpack(struct mdt_thread_info *info,
3822 enum tgt_handler_flags flags)
3824 const struct mdt_body *body;
3825 struct mdt_object *obj;
3826 const struct lu_env *env;
3827 struct req_capsule *pill;
3831 env = info->mti_env;
3832 pill = info->mti_pill;
3834 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3838 if (!(body->mbo_valid & OBD_MD_FLID))
3841 if (!fid_is_sane(&body->mbo_fid1)) {
3842 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3846 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3848 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
3849 mdt_object_put(env, obj);
3852 info->mti_object = obj;
3861 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
3862 enum tgt_handler_flags flags)
3864 struct req_capsule *pill = info->mti_pill;
3869 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3870 rc = mdt_body_unpack(info, flags);
3874 if (rc == 0 && (flags & HAS_REPLY)) {
3876 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3877 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3879 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3880 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3883 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3884 * by default. If the target object has more ACL entries, then
3885 * enlarge the buffer when necessary. */
3886 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3887 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3888 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3890 mdt_preset_secctx_size(info);
3891 mdt_preset_encctx_size(info);
3893 rc = req_capsule_server_pack(pill);
3895 CWARN("%s: cannot pack response: rc = %d\n",
3896 mdt_obd_name(info->mti_mdt), rc);
3901 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3903 lh->mlh_type = MDT_NUL_LOCK;
3904 lh->mlh_reg_lh.cookie = 0ull;
3905 lh->mlh_reg_mode = LCK_MINMODE;
3906 lh->mlh_pdo_lh.cookie = 0ull;
3907 lh->mlh_pdo_mode = LCK_MINMODE;
3908 lh->mlh_rreg_lh.cookie = 0ull;
3909 lh->mlh_rreg_mode = LCK_MINMODE;
3912 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3914 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3915 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3919 * Initialize fields of struct mdt_thread_info. Other fields are left in
3920 * uninitialized state, because it's too expensive to zero out whole
3921 * mdt_thread_info (> 1K) on each request arrival.
3923 void mdt_thread_info_init(struct ptlrpc_request *req,
3924 struct mdt_thread_info *info)
3928 info->mti_pill = &req->rq_pill;
3931 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3932 mdt_lock_handle_init(&info->mti_lh[i]);
3934 /* mdt device: it can be NULL while CONNECT */
3935 if (req->rq_export) {
3936 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3937 info->mti_exp = req->rq_export;
3939 info->mti_mdt = NULL;
3940 info->mti_env = req->rq_svc_thread->t_env;
3941 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3943 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3944 info->mti_big_buf = LU_BUF_NULL;
3945 info->mti_body = NULL;
3946 info->mti_object = NULL;
3947 info->mti_dlm_req = NULL;
3948 info->mti_has_trans = 0;
3949 info->mti_cross_ref = 0;
3950 info->mti_opdata = 0;
3951 info->mti_big_lmm_used = 0;
3952 info->mti_big_acl_used = 0;
3953 info->mti_som_valid = 0;
3955 info->mti_spec.no_create = 0;
3956 info->mti_spec.sp_rm_entry = 0;
3957 info->mti_spec.sp_permitted = 0;
3959 info->mti_spec.u.sp_ea.eadata = NULL;
3960 info->mti_spec.u.sp_ea.eadatalen = 0;
3963 void mdt_thread_info_fini(struct mdt_thread_info *info)
3967 if (info->mti_object != NULL) {
3968 mdt_object_put(info->mti_env, info->mti_object);
3969 info->mti_object = NULL;
3972 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3973 mdt_lock_handle_fini(&info->mti_lh[i]);
3974 info->mti_env = NULL;
3975 info->mti_pill = NULL;
3976 info->mti_exp = NULL;
3977 info->mti_mdt = NULL;
3979 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3980 lu_buf_free(&info->mti_big_buf);
3983 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3985 struct mdt_thread_info *mti;
3987 mti = mdt_th_info(tsi->tsi_env);
3988 LASSERT(mti != NULL);
3990 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3991 if (tsi->tsi_corpus != NULL) {
3992 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3993 lu_object_get(tsi->tsi_corpus);
3995 mti->mti_body = tsi->tsi_mdt_body;
3996 mti->mti_dlm_req = tsi->tsi_dlm_req;
4001 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4003 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4005 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4006 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4008 return tgt_connect(tsi);
4011 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4012 struct mdt_thread_info *info,
4013 struct ldlm_lock **lockp, __u64 flags)
4015 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4018 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4019 struct mdt_thread_info *info,
4020 struct ldlm_lock **lockp, __u64 flags)
4022 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4026 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4027 struct ldlm_lock **lockp,
4028 struct mdt_lock_handle *lh,
4029 __u64 flags, int result)
4031 struct ptlrpc_request *req = mdt_info_req(info);
4032 struct ldlm_lock *lock = *lockp;
4033 struct ldlm_lock *new_lock;
4035 /* If possible resent found a lock, @lh is set to its handle */
4036 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4038 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
4039 lh->mlh_reg_lh.cookie = 0;
4043 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
4044 /* Lock is pinned by ldlm_handle_enqueue0() as it is
4045 * a resend case, however, it could be already destroyed
4046 * due to client eviction or a raced cancel RPC. */
4047 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4048 lh->mlh_reg_lh.cookie);
4049 lh->mlh_reg_lh.cookie = 0;
4053 LASSERTF(new_lock != NULL,
4054 "lockh %#llx flags %#llx : rc = %d\n",
4055 lh->mlh_reg_lh.cookie, flags, result);
4058 * If we've already given this lock to a client once, then we should
4059 * have no readers or writers. Otherwise, we should have one reader
4060 * _or_ writer ref (which will be zeroed below) before returning the
4063 if (new_lock->l_export == req->rq_export) {
4064 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4066 LASSERT(new_lock->l_export == NULL);
4067 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4072 if (new_lock->l_export == req->rq_export) {
4074 * Already gave this to the client, which means that we
4075 * reconstructed a reply.
4077 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4080 LDLM_LOCK_RELEASE(new_lock);
4081 lh->mlh_reg_lh.cookie = 0;
4082 RETURN(ELDLM_LOCK_REPLACED);
4086 * Fixup the lock to be given to the client.
4088 lock_res_and_lock(new_lock);
4089 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4090 * possible blocking AST. */
4091 while (new_lock->l_readers > 0) {
4092 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4093 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4094 new_lock->l_readers--;
4096 while (new_lock->l_writers > 0) {
4097 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4098 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4099 new_lock->l_writers--;
4102 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4103 new_lock->l_blocking_ast = lock->l_blocking_ast;
4104 new_lock->l_completion_ast = lock->l_completion_ast;
4105 if (ldlm_has_dom(new_lock))
4106 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4107 new_lock->l_remote_handle = lock->l_remote_handle;
4108 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4110 unlock_res_and_lock(new_lock);
4112 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4113 &new_lock->l_remote_handle,
4114 &new_lock->l_exp_hash);
4116 LDLM_LOCK_RELEASE(new_lock);
4117 lh->mlh_reg_lh.cookie = 0;
4119 RETURN(ELDLM_LOCK_REPLACED);
4122 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4123 struct ldlm_lock *new_lock,
4124 struct mdt_lock_handle *lh, __u64 flags)
4126 struct ptlrpc_request *req = mdt_info_req(info);
4127 struct ldlm_request *dlmreq;
4129 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4132 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4134 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4135 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4137 if (flags & LDLM_FL_RESENT) {
4138 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4139 lh->mlh_reg_mode = new_lock->l_granted_mode;
4141 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4142 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4143 lh->mlh_reg_lh.cookie);
4148 * If the xid matches, then we know this is a resent request, and allow
4149 * it. (It's probably an OPEN, for which we don't send a lock.
4151 if (req_can_reconstruct(req, NULL) != 0)
4155 * This remote handle isn't enqueued, so we never received or processed
4156 * this request. Clear MSG_RESENT, because it can be handled like any
4157 * normal request now.
4159 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4161 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4162 dlmreq->lock_handle[0].cookie);
4165 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4166 struct mdt_thread_info *info,
4167 struct ldlm_lock **lockp,
4170 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4171 struct ldlm_reply *ldlm_rep = NULL;
4176 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4177 * (for the resend case) or a new lock. Below we will use it to
4178 * replace the original lock.
4180 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4181 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4182 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4183 rc = mdt_object_lock(info, info->mti_object, lhc,
4184 MDS_INODELOCK_XATTR);
4189 rc = mdt_getxattr(info);
4191 if (mdt_info_req(info)->rq_repmsg != NULL)
4192 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4194 if (ldlm_rep == NULL ||
4195 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4196 mdt_object_unlock(info, info->mti_object, lhc, 1);
4200 RETURN(err_serious(-EFAULT));
4203 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4205 /* This is left for interop instead of adding a new interop flag.
4207 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4208 if (ldlm_rep->lock_policy_res2) {
4209 mdt_object_unlock(info, info->mti_object, lhc, 1);
4210 RETURN(ELDLM_LOCK_ABORTED);
4214 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4218 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4219 struct mdt_thread_info *info,
4220 struct ldlm_lock **lockp,
4223 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4225 struct ldlm_reply *ldlm_rep;
4226 struct mdt_body *reqbody;
4227 struct mdt_body *repbody;
4231 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4234 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4237 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4238 repbody->mbo_eadatasize = 0;
4239 repbody->mbo_aclsize = 0;
4243 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4246 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4250 CERROR("%s: unsupported intent %#x\n",
4251 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4252 GOTO(out_shrink, rc = -EINVAL);
4255 rc = mdt_init_ucred(info, reqbody);
4257 GOTO(out_shrink, rc);
4259 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4260 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4262 /* Get lock from request for possible resent case. */
4263 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4265 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4266 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4268 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4269 ldlm_rep->lock_policy_res2 = 0;
4270 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4271 ldlm_rep->lock_policy_res2) {
4272 lhc->mlh_reg_lh.cookie = 0ull;
4273 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4276 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4279 mdt_exit_ucred(info);
4281 mdt_client_compatibility(info);
4282 rc2 = mdt_fix_reply(info);
4288 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4289 struct mdt_thread_info *info,
4290 struct ldlm_lock **lockp,
4293 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4294 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4295 struct layout_intent *intent;
4296 struct ldlm_reply *ldlm_rep;
4297 struct lu_fid *fid = &info->mti_tmp_fid2;
4298 struct mdt_object *obj = NULL;
4299 int layout_size = 0;
4300 struct lu_buf *buf = &layout.mlc_buf;
4305 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4307 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4311 CDEBUG(D_INFO, DFID "got layout change request from client: "
4312 "opc:%u flags:%#x extent "DEXT"\n",
4313 PFID(fid), intent->li_opc, intent->li_flags,
4314 PEXT(&intent->li_extent));
4316 switch (intent->li_opc) {
4317 case LAYOUT_INTENT_TRUNC:
4318 case LAYOUT_INTENT_WRITE:
4319 layout.mlc_opc = MD_LAYOUT_WRITE;
4320 layout.mlc_intent = intent;
4322 case LAYOUT_INTENT_ACCESS:
4324 case LAYOUT_INTENT_READ:
4325 case LAYOUT_INTENT_GLIMPSE:
4326 case LAYOUT_INTENT_RELEASE:
4327 case LAYOUT_INTENT_RESTORE:
4328 CERROR("%s: Unsupported layout intent opc %d\n",
4329 mdt_obd_name(info->mti_mdt), intent->li_opc);
4332 CERROR("%s: Unknown layout intent opc %d\n",
4333 mdt_obd_name(info->mti_mdt), intent->li_opc);
4337 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4339 RETURN(PTR_ERR(obj));
4341 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4342 /* if layout is going to be changed don't use the current EA
4343 * size but the maximum one. That buffer will be shrinked
4344 * to the actual size in req_capsule_shrink() before reply.
4346 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4347 layout_size = info->mti_mdt->mdt_max_mdsize;
4349 layout_size = mdt_attr_get_eabuf_size(info, obj);
4350 if (layout_size < 0)
4351 GOTO(out, rc = layout_size);
4353 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4354 info->mti_mdt->mdt_max_mdsize = layout_size;
4356 CDEBUG(D_INFO, "%s: layout_size %d\n",
4357 mdt_obd_name(info->mti_mdt), layout_size);
4361 * set reply buffer size, so that ldlm_handle_enqueue0()->
4362 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4364 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4366 rc = req_capsule_server_pack(info->mti_pill);
4370 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4372 GOTO(out, rc = -EPROTO);
4374 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4376 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4377 if (layout.mlc_opc == MD_LAYOUT_NOP)
4380 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4384 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4385 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4391 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4392 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4394 buf->lb_len = req_capsule_get_size(info->mti_pill,
4395 &RMF_EADATA, RCL_CLIENT);
4397 * If it's a replay of layout write intent RPC, the client has
4398 * saved the extended lovea when it get reply then.
4400 if (buf->lb_len > 0)
4401 mdt_fix_lov_magic(info, buf->lb_buf);
4404 /* Get lock from request for possible resent case. */
4405 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4406 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4409 * Instantiate some layout components, if @buf contains lovea, then it's
4410 * a replay of the layout intent write RPC.
4412 rc = mdt_layout_change(info, obj, lhc, &layout);
4413 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4415 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4416 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4417 if (rc == ELDLM_LOCK_REPLACED &&
4418 (*lockp)->l_granted_mode == LCK_EX)
4419 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4424 mdt_object_put(info->mti_env, obj);
4428 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4429 struct mdt_thread_info *info,
4430 struct ldlm_lock **lockp,
4433 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4434 struct ldlm_reply *rep = NULL;
4437 struct ptlrpc_request *req = mdt_info_req(info);
4439 static const struct req_format *intent_fmts[REINT_MAX] = {
4440 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4441 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4446 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4450 /* Get lock from request for possible resent case. */
4451 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4453 rc = mdt_reint_internal(info, lhc, opc);
4455 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4456 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4458 /* Check whether the reply has been packed successfully. */
4459 if (mdt_info_req(info)->rq_repmsg != NULL)
4460 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4465 RETURN(err_serious(-EFAULT));
4468 /* MDC expects this in any case */
4470 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4472 /* the open lock or the lock for cross-ref object should be
4473 * returned to the client */
4474 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4475 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4476 rep->lock_policy_res2 = 0;
4477 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4481 rep->lock_policy_res2 = clear_serious(rc);
4483 if (rep->lock_policy_res2 == -ENOENT &&
4484 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4485 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4486 rep->lock_policy_res2 = 0;
4488 lhc->mlh_reg_lh.cookie = 0ull;
4489 if (rc == -ENOTCONN || rc == -ENODEV ||
4490 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4492 * If it is the disconnect error (ENODEV & ENOCONN), the error
4493 * will be returned by rq_status, and client at ptlrpc layer
4494 * will detect this, then disconnect, reconnect the import
4495 * immediately, instead of impacting the following the rpc.
4500 * For other cases, the error will be returned by intent, and client
4501 * will retrieve the result from intent.
4503 RETURN(ELDLM_LOCK_ABORTED);
4506 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4507 struct mdt_thread_info *info,
4508 struct ldlm_lock **lockp,
4509 u64 flags /* LDLM_FL_* */)
4511 struct req_capsule *pill = info->mti_pill;
4512 struct ptlrpc_request *req = mdt_info_req(info);
4513 const struct req_format *it_format;
4514 int (*it_handler)(enum ldlm_intent_flags,
4515 struct mdt_thread_info *,
4516 struct ldlm_lock **,
4518 enum tgt_handler_flags it_handler_flags = 0;
4519 struct ldlm_reply *rep;
4520 bool check_mdt_object = false;
4526 case IT_OPEN|IT_CREAT:
4528 * OCREAT is not a IS_MUTABLE request since the file may
4529 * already exist. We do the extra check of
4530 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4531 * really need to create the object.
4533 it_format = &RQF_LDLM_INTENT;
4534 it_handler = &mdt_intent_open;
4537 check_mdt_object = true;
4540 it_format = &RQF_LDLM_INTENT_GETATTR;
4541 it_handler = &mdt_intent_getattr;
4542 it_handler_flags = HAS_REPLY;
4545 check_mdt_object = true;
4546 it_format = &RQF_LDLM_INTENT_GETXATTR;
4547 it_handler = &mdt_intent_getxattr;
4548 it_handler_flags = HAS_BODY;
4551 it_format = &RQF_LDLM_INTENT_LAYOUT;
4552 it_handler = &mdt_intent_layout;
4555 it_format = &RQF_LDLM_INTENT;
4556 it_handler = &mdt_intent_glimpse;
4559 it_format = &RQF_LDLM_INTENT;
4560 it_handler = &mdt_intent_brw;
4562 case IT_QUOTA_DQACQ:
4563 case IT_QUOTA_CONN: {
4564 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4567 RETURN(-EOPNOTSUPP);
4569 if (mdt_rdonly(req->rq_export))
4572 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4573 /* pass the request to quota master */
4574 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4575 mdt_info_req(info), lockp,
4580 CERROR("%s: unknown intent code %#x\n",
4581 mdt_obd_name(info->mti_mdt), it_opc);
4585 req_capsule_extend(pill, it_format);
4587 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4591 if (unlikely(info->mti_object == NULL && check_mdt_object))
4594 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4597 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4599 /* execute policy */
4600 rc = (*it_handler)(it_opc, info, lockp, flags);
4602 /* Check whether the reply has been packed successfully. */
4603 if (req->rq_repmsg != NULL) {
4604 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4605 rep->lock_policy_res2 =
4606 ptlrpc_status_hton(rep->lock_policy_res2);
4612 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4613 enum ldlm_intent_flags it_opc)
4615 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4617 /* update stats when IT code is known */
4618 if (srv_stats != NULL)
4619 lprocfs_counter_incr(srv_stats,
4620 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4621 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4624 static int mdt_intent_policy(const struct lu_env *env,
4625 struct ldlm_namespace *ns,
4626 struct ldlm_lock **lockp,
4628 enum ldlm_mode mode,
4629 __u64 flags, void *data)
4631 struct tgt_session_info *tsi;
4632 struct mdt_thread_info *info;
4633 struct ptlrpc_request *req = req_cookie;
4634 struct ldlm_intent *it;
4635 struct req_capsule *pill;
4636 const struct ldlm_lock_desc *ldesc;
4641 LASSERT(req != NULL);
4643 tsi = tgt_ses_info(env);
4645 info = tsi2mdt_info(tsi);
4646 LASSERT(info != NULL);
4647 pill = info->mti_pill;
4648 LASSERT(pill->rc_req == req);
4649 ldesc = &info->mti_dlm_req->lock_desc;
4651 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4652 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4653 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4655 mdt_ptlrpc_stats_update(req, it->opc);
4656 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4660 /* Lock without inodebits makes no sense and will oops
4661 * later in ldlm. Let's check it now to see if we have
4662 * ibits corrupted somewhere in mdt_intent_opc().
4663 * The case for client miss to set ibits has been
4664 * processed by others. */
4665 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4666 ldesc->l_policy_data.l_inodebits.bits != 0));
4668 rc = err_serious(-EFAULT);
4670 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4671 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4672 struct ldlm_reply *rep;
4674 /* No intent was provided but INTENT flag is set along with
4675 * DOM bit, this is considered as GLIMPSE request.
4676 * This logic is common for MDT and OST glimpse
4678 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4679 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4680 /* Check whether the reply has been packed successfully. */
4681 if (req->rq_repmsg != NULL) {
4682 rep = req_capsule_server_get(info->mti_pill,
4684 rep->lock_policy_res2 =
4685 ptlrpc_status_hton(rep->lock_policy_res2);
4688 /* No intent was provided */
4689 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4690 rc = req_capsule_server_pack(pill);
4692 rc = err_serious(rc);
4694 mdt_thread_info_fini(info);
4698 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4700 struct seq_server_site *ss = mdt_seq_site(mdt);
4702 if (ss->ss_node_id == 0)
4705 if (ss->ss_client_seq != NULL) {
4706 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4707 ss->ss_client_seq->lcs_exp = NULL;
4710 if (ss->ss_server_fld != NULL) {
4711 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4712 ss->ss_server_fld->lsf_control_exp = NULL;
4716 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4718 struct seq_server_site *ss = mdt_seq_site(mdt);
4723 if (ss->ss_server_seq != NULL)
4724 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4727 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4729 mdt_seq_fini_cli(mdt);
4730 mdt_deregister_seq_exp(mdt);
4732 return seq_site_fini(env, mdt_seq_site(mdt));
4736 * It will retrieve its FLDB entries from MDT0, and it only happens
4737 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4738 * and it needs to refresh FLDB from the MDT0.
4740 static int mdt_register_lwp_callback(void *data)
4743 struct mdt_device *mdt = data;
4744 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4748 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4750 rc = lu_env_init(&env, LCT_MD_THREAD);
4752 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4756 /* Allocate new sequence now to avoid creating local transaction
4757 * in the normal transaction process */
4758 rc = seq_server_check_and_alloc_super(&env,
4759 mdt_seq_site(mdt)->ss_server_seq);
4764 rc = fld_update_from_controller(&env, fld);
4766 CERROR("%s: cannot update controller: rc = %d\n",
4767 mdt_obd_name(mdt), rc);
4776 static int mdt_register_seq_exp(struct mdt_device *mdt)
4778 struct seq_server_site *ss = mdt_seq_site(mdt);
4779 char *lwp_name = NULL;
4782 if (ss->ss_node_id == 0)
4785 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4786 if (lwp_name == NULL)
4787 GOTO(out_free, rc = -ENOMEM);
4789 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4793 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4798 rc = lustre_register_lwp_item(lwp_name,
4799 &ss->ss_server_fld->lsf_control_exp,
4800 mdt_register_lwp_callback, mdt);
4802 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4803 ss->ss_client_seq->lcs_exp = NULL;
4807 if (lwp_name != NULL)
4808 OBD_FREE(lwp_name, MAX_OBD_NAME);
4814 * Init client sequence manager which is used by local MDS to talk to sequence
4815 * controller on remote node.
4817 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4819 struct seq_server_site *ss = mdt_seq_site(mdt);
4823 /* check if this is adding the first MDC and controller is not yet
4825 OBD_ALLOC_PTR(ss->ss_client_seq);
4826 if (ss->ss_client_seq == NULL)
4829 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4830 if (prefix == NULL) {
4831 OBD_FREE_PTR(ss->ss_client_seq);
4832 ss->ss_client_seq = NULL;
4836 /* Note: seq_client_fini will be called in seq_site_fini */
4837 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4838 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4839 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4841 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4843 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
4846 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4848 struct seq_server_site *ss;
4852 ss = mdt_seq_site(mdt);
4853 /* init sequence controller server(MDT0) */
4854 if (ss->ss_node_id == 0) {
4855 OBD_ALLOC_PTR(ss->ss_control_seq);
4856 if (ss->ss_control_seq == NULL)
4859 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4860 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4863 GOTO(out_seq_fini, rc);
4866 /* Init normal sequence server */
4867 OBD_ALLOC_PTR(ss->ss_server_seq);
4868 if (ss->ss_server_seq == NULL)
4869 GOTO(out_seq_fini, rc = -ENOMEM);
4871 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4872 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4874 GOTO(out_seq_fini, rc);
4876 /* init seq client for seq server to talk to seq controller(MDT0) */
4877 rc = mdt_seq_init_cli(env, mdt);
4879 GOTO(out_seq_fini, rc);
4881 if (ss->ss_node_id != 0)
4882 /* register controller export through lwp */
4883 rc = mdt_register_seq_exp(mdt);
4888 mdt_seq_fini(env, mdt);
4896 static int mdt_fld_fini(const struct lu_env *env,
4897 struct mdt_device *m)
4899 struct seq_server_site *ss = mdt_seq_site(m);
4902 if (ss && ss->ss_server_fld) {
4903 fld_server_fini(env, ss->ss_server_fld);
4904 OBD_FREE_PTR(ss->ss_server_fld);
4905 ss->ss_server_fld = NULL;
4911 static int mdt_fld_init(const struct lu_env *env,
4913 struct mdt_device *m)
4915 struct seq_server_site *ss;
4919 ss = mdt_seq_site(m);
4921 OBD_ALLOC_PTR(ss->ss_server_fld);
4922 if (ss->ss_server_fld == NULL)
4923 RETURN(rc = -ENOMEM);
4925 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4928 OBD_FREE_PTR(ss->ss_server_fld);
4929 ss->ss_server_fld = NULL;
4936 static void mdt_stack_pre_fini(const struct lu_env *env,
4937 struct mdt_device *m, struct lu_device *top)
4939 struct lustre_cfg_bufs *bufs;
4940 struct lustre_cfg *lcfg;
4941 struct mdt_thread_info *info;
4946 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4947 LASSERT(info != NULL);
4949 bufs = &info->mti_u.bufs;
4951 LASSERT(m->mdt_child_exp);
4952 LASSERT(m->mdt_child_exp->exp_obd);
4954 /* process cleanup, pass mdt obd name to get obd umount flags */
4955 /* XXX: this is needed because all layers are referenced by
4956 * objects (some of them are pinned by osd, for example *
4957 * the proper solution should be a model where object used
4958 * by osd only doesn't have mdt/mdd slices -bzzz */
4959 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4960 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4961 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4964 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4966 top->ld_ops->ldo_process_config(env, top, lcfg);
4967 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4971 static void mdt_stack_fini(const struct lu_env *env,
4972 struct mdt_device *m, struct lu_device *top)
4974 struct obd_device *obd = mdt2obd_dev(m);
4975 struct lustre_cfg_bufs *bufs;
4976 struct lustre_cfg *lcfg;
4977 struct mdt_thread_info *info;
4981 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4982 LASSERT(info != NULL);
4984 lu_dev_del_linkage(top->ld_site, top);
4986 lu_site_purge(env, top->ld_site, -1);
4988 bufs = &info->mti_u.bufs;
4989 /* process cleanup, pass mdt obd name to get obd umount flags */
4990 /* another purpose is to let all layers to release their objects */
4991 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4996 lustre_cfg_bufs_set_string(bufs, 1, flags);
4997 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5000 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5003 top->ld_ops->ldo_process_config(env, top, lcfg);
5004 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5006 lu_site_purge(env, top->ld_site, -1);
5008 m->mdt_child = NULL;
5009 m->mdt_bottom = NULL;
5011 obd_disconnect(m->mdt_child_exp);
5012 m->mdt_child_exp = NULL;
5014 obd_disconnect(m->mdt_bottom_exp);
5015 m->mdt_child_exp = NULL;
5018 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5019 const char *next, struct obd_export **exp)
5021 struct obd_connect_data *data = NULL;
5022 struct obd_device *obd;
5026 OBD_ALLOC_PTR(data);
5028 GOTO(out, rc = -ENOMEM);
5030 obd = class_name2obd(next);
5032 CERROR("%s: can't locate next device: %s\n",
5033 mdt_obd_name(m), next);
5034 GOTO(out, rc = -ENOTCONN);
5037 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5038 data->ocd_version = LUSTRE_VERSION_CODE;
5040 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5042 CERROR("%s: cannot connect to next dev %s (%d)\n",
5043 mdt_obd_name(m), next, rc);
5053 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5054 struct lustre_cfg *cfg)
5056 char *dev = lustre_cfg_string(cfg, 0);
5057 int rc, name_size, uuid_size;
5058 char *name, *uuid, *p;
5059 struct lustre_cfg_bufs *bufs;
5060 struct lustre_cfg *lcfg;
5061 struct obd_device *obd;
5062 struct lustre_profile *lprof;
5063 struct lu_site *site;
5066 /* in 1.8 we had the only device in the stack - MDS.
5067 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5068 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5069 * to generate names and setup MDT, MDD. MDT will be using
5070 * generated name to connect to MDD. for MDD the next device
5071 * will be LOD with name taken from so called "profile" which
5072 * is generated by mount_option line
5074 * 1.8 MGS generates config. commands like this:
5075 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5076 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5077 * 2.0 MGS generates config. commands like this:
5078 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5079 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5080 * 3:lustre-MDT0000-mdtlov 4:f
5082 * we generate MDD name from MDT one, just replacing T with D
5084 * after all the preparations, the logical equivalent will be
5085 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5086 * 3:lustre-MDT0000-mdtlov 4:f
5087 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5088 * 3:lustre-MDD0000 4:f
5090 * notice we build the stack from down to top: MDD first, then MDT */
5092 name_size = MAX_OBD_NAME;
5093 uuid_size = MAX_OBD_NAME;
5095 OBD_ALLOC(name, name_size);
5096 OBD_ALLOC(uuid, uuid_size);
5097 if (name == NULL || uuid == NULL)
5098 GOTO(cleanup_mem, rc = -ENOMEM);
5100 OBD_ALLOC_PTR(bufs);
5102 GOTO(cleanup_mem, rc = -ENOMEM);
5105 p = strstr(name, "-MDT");
5107 GOTO(free_bufs, rc = -ENOMEM);
5110 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5112 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5113 if (lprof == NULL || lprof->lp_dt == NULL) {
5114 CERROR("can't find the profile: %s\n",
5115 lustre_cfg_string(cfg, 0));
5116 GOTO(free_bufs, rc = -EINVAL);
5119 lustre_cfg_bufs_reset(bufs, name);
5120 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5121 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5122 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5124 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5126 GOTO(put_profile, rc = -ENOMEM);
5127 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5129 rc = class_attach(lcfg);
5131 GOTO(lcfg_cleanup, rc);
5133 obd = class_name2obd(name);
5135 CERROR("Can not find obd %s (%s in config)\n",
5136 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5137 GOTO(lcfg_cleanup, rc = -EINVAL);
5140 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5142 lustre_cfg_bufs_reset(bufs, name);
5143 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5144 lustre_cfg_bufs_set_string(bufs, 2, dev);
5145 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5147 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5149 GOTO(class_detach, rc = -ENOMEM);
5150 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5152 rc = class_setup(obd, lcfg);
5154 GOTO(class_detach, rc);
5156 /* connect to MDD we just setup */
5157 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5159 GOTO(class_detach, rc);
5161 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5163 LASSERT(mdt_lu_site(mdt) == NULL);
5164 mdt->mdt_lu_dev.ld_site = site;
5165 site->ls_top_dev = &mdt->mdt_lu_dev;
5166 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5168 /* now connect to bottom OSD */
5169 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5170 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5172 GOTO(class_detach, rc);
5174 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5176 rc = lu_env_refill((struct lu_env *)env);
5178 CERROR("Failure to refill session: '%d'\n", rc);
5180 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5185 class_detach(obd, lcfg);
5187 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5189 class_put_profile(lprof);
5194 OBD_FREE(name, name_size);
5196 OBD_FREE(uuid, uuid_size);
5200 /* setup quota master target on MDT0 */
5201 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5202 struct lustre_cfg *cfg)
5204 struct obd_device *obd;
5205 char *dev = lustre_cfg_string(cfg, 0);
5206 char *qmtname, *uuid, *p;
5207 struct lustre_cfg_bufs *bufs;
5208 struct lustre_cfg *lcfg;
5209 struct lustre_profile *lprof;
5210 struct obd_connect_data *data;
5214 LASSERT(mdt->mdt_qmt_exp == NULL);
5215 LASSERT(mdt->mdt_qmt_dev == NULL);
5217 /* quota master is on MDT0 only for now */
5218 if (mdt->mdt_seq_site.ss_node_id != 0)
5221 /* MGS generates config commands which look as follows:
5222 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5223 * 3:lustre-MDT0000-mdtlov 4:f
5225 * We generate the QMT name from the MDT one, just replacing MD with QM
5226 * after all the preparations, the logical equivalent will be:
5227 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5228 * 3:lustre-MDT0000-osd 4:f */
5229 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5230 OBD_ALLOC(uuid, UUID_MAX);
5231 OBD_ALLOC_PTR(bufs);
5232 OBD_ALLOC_PTR(data);
5233 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5234 GOTO(cleanup_mem, rc = -ENOMEM);
5236 strcpy(qmtname, dev);
5237 p = strstr(qmtname, "-MDT");
5239 GOTO(cleanup_mem, rc = -ENOMEM);
5240 /* replace MD with QM */
5244 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5246 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5247 if (lprof == NULL || lprof->lp_dt == NULL) {
5248 CERROR("can't find profile for %s\n",
5249 lustre_cfg_string(cfg, 0));
5250 GOTO(cleanup_mem, rc = -EINVAL);
5253 lustre_cfg_bufs_reset(bufs, qmtname);
5254 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5255 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5256 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5258 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5260 GOTO(put_profile, rc = -ENOMEM);
5261 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5263 rc = class_attach(lcfg);
5265 GOTO(lcfg_cleanup, rc);
5267 obd = class_name2obd(qmtname);
5269 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5270 lustre_cfg_string(cfg, 0));
5271 GOTO(lcfg_cleanup, rc = -EINVAL);
5274 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5276 lustre_cfg_bufs_reset(bufs, qmtname);
5277 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5278 lustre_cfg_bufs_set_string(bufs, 2, dev);
5280 /* for quota, the next device should be the OSD device */
5281 lustre_cfg_bufs_set_string(bufs, 3,
5282 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5284 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5286 GOTO(class_detach, rc = -ENOMEM);
5287 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5289 rc = class_setup(obd, lcfg);
5291 GOTO(class_detach, rc);
5293 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5295 /* configure local quota objects */
5296 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5299 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5303 GOTO(class_cleanup, rc);
5305 /* connect to quota master target */
5306 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5307 data->ocd_version = LUSTRE_VERSION_CODE;
5308 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5311 CERROR("cannot connect to quota master device %s (%d)\n",
5313 GOTO(class_cleanup, rc);
5319 class_manual_cleanup(obd);
5320 mdt->mdt_qmt_dev = NULL;
5321 GOTO(lcfg_cleanup, rc);
5325 class_detach(obd, lcfg);
5327 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5329 class_put_profile(lprof);
5334 OBD_FREE(qmtname, MAX_OBD_NAME);
5336 OBD_FREE(uuid, UUID_MAX);
5342 /* Shutdown quota master target associated with mdt */
5343 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5347 if (mdt->mdt_qmt_exp == NULL)
5349 LASSERT(mdt->mdt_qmt_dev != NULL);
5351 /* the qmt automatically shuts down when the mdt disconnects */
5352 obd_disconnect(mdt->mdt_qmt_exp);
5353 mdt->mdt_qmt_exp = NULL;
5354 mdt->mdt_qmt_dev = NULL;
5358 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5359 * for now. This will be removed along with converting rest of MDT code
5360 * to use tgt_session_info */
5361 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5363 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5366 if (unlikely(info->mti_object == NULL))
5369 rc = mdt_getxattr(info);
5371 mdt_thread_info_fini(info);
5375 static int mdt_llog_open(struct tgt_session_info *tsi)
5379 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5380 RETURN(err_serious(-EACCES));
5382 RETURN(tgt_llog_open(tsi));
5385 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5386 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5387 #define OST_BRW_READ OST_READ
5388 #define OST_BRW_WRITE OST_WRITE
5390 static struct tgt_handler mdt_tgt_handlers[] = {
5391 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5392 0, MDS_CONNECT, mdt_tgt_connect,
5393 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5394 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5395 0, MDS_DISCONNECT, tgt_disconnect,
5396 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5397 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5398 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5399 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5400 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5401 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5402 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5403 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5405 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5406 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5407 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5408 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5409 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5410 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5411 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5412 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5414 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5415 mdt_hsm_ct_register),
5416 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5417 mdt_hsm_ct_unregister),
5418 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5420 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5422 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5423 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5425 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5428 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5431 static struct tgt_handler mdt_io_ops[] = {
5432 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5434 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5436 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5437 OST_PUNCH, mdt_punch_hdl,
5439 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5440 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5443 static struct tgt_handler mdt_sec_ctx_ops[] = {
5444 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5445 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5446 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5449 static struct tgt_handler mdt_quota_ops[] = {
5450 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5453 static struct tgt_handler mdt_llog_handlers[] = {
5454 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5455 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5456 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5457 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5460 static struct tgt_opc_slice mdt_common_slice[] = {
5462 .tos_opc_start = MDS_FIRST_OPC,
5463 .tos_opc_end = MDS_LAST_OPC,
5464 .tos_hs = mdt_tgt_handlers
5467 .tos_opc_start = OBD_FIRST_OPC,
5468 .tos_opc_end = OBD_LAST_OPC,
5469 .tos_hs = tgt_obd_handlers
5472 .tos_opc_start = LDLM_FIRST_OPC,
5473 .tos_opc_end = LDLM_LAST_OPC,
5474 .tos_hs = tgt_dlm_handlers
5477 .tos_opc_start = SEC_FIRST_OPC,
5478 .tos_opc_end = SEC_LAST_OPC,
5479 .tos_hs = mdt_sec_ctx_ops
5482 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5483 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5484 .tos_hs = tgt_out_handlers
5487 .tos_opc_start = FLD_FIRST_OPC,
5488 .tos_opc_end = FLD_LAST_OPC,
5489 .tos_hs = fld_handlers
5492 .tos_opc_start = SEQ_FIRST_OPC,
5493 .tos_opc_end = SEQ_LAST_OPC,
5494 .tos_hs = seq_handlers
5497 .tos_opc_start = QUOTA_DQACQ,
5498 .tos_opc_end = QUOTA_LAST_OPC,
5499 .tos_hs = mdt_quota_ops
5502 .tos_opc_start = LLOG_FIRST_OPC,
5503 .tos_opc_end = LLOG_LAST_OPC,
5504 .tos_hs = mdt_llog_handlers
5507 .tos_opc_start = LFSCK_FIRST_OPC,
5508 .tos_opc_end = LFSCK_LAST_OPC,
5509 .tos_hs = tgt_lfsck_handlers
5512 .tos_opc_start = OST_FIRST_OPC,
5513 .tos_opc_end = OST_LAST_OPC,
5514 .tos_hs = mdt_io_ops
5521 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5523 struct md_device *next = m->mdt_child;
5524 struct lu_device *d = &m->mdt_lu_dev;
5525 struct obd_device *obd = mdt2obd_dev(m);
5526 struct lfsck_stop stop;
5529 stop.ls_status = LS_PAUSED;
5531 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5533 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5535 mdt_restriper_stop(m);
5536 ping_evictor_stop();
5538 /* Remove the HSM /proc entry so the coordinator cannot be
5539 * restarted by a user while it's shutting down.
5541 mdt_hsm_cdt_stop(m);
5543 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5544 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5546 if (m->mdt_namespace != NULL)
5547 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5548 d->ld_obd->obd_force);
5550 obd_exports_barrier(obd);
5551 obd_zombie_barrier();
5553 mdt_quota_fini(env, m);
5555 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5557 /* Calling the cleanup functions in the same order as in the mdt_init0
5560 mdt_tunables_fini(m);
5562 target_recovery_fini(obd);
5563 upcall_cache_cleanup(m->mdt_identity_cache);
5564 m->mdt_identity_cache = NULL;
5566 mdt_fs_cleanup(env, m);
5568 tgt_fini(env, &m->mdt_lut);
5570 mdt_hsm_cdt_fini(m);
5572 if (m->mdt_los != NULL) {
5573 local_oid_storage_fini(env, m->mdt_los);
5577 if (m->mdt_namespace != NULL) {
5578 ldlm_namespace_free_post(m->mdt_namespace);
5579 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5582 if (m->mdt_md_root != NULL) {
5583 mdt_object_put(env, m->mdt_md_root);
5584 m->mdt_md_root = NULL;
5587 mdt_seq_fini(env, m);
5589 mdt_fld_fini(env, m);
5594 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5596 LASSERT(atomic_read(&d->ld_ref) == 0);
5598 server_put_mount(mdt_obd_name(m), true);
5603 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5605 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5606 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5608 const struct dt_device_param *dt_conf;
5609 struct mdt_thread_info *info;
5610 struct obd_device *obd;
5611 const char *dev = lustre_cfg_string(cfg, 0);
5612 const char *num = lustre_cfg_string(cfg, 2);
5613 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5614 struct lustre_mount_info *lmi = NULL;
5615 struct lustre_sb_info *lsi;
5617 struct seq_server_site *ss_site;
5618 const char *identity_upcall = "NONE";
5619 struct md_device *next;
5626 lu_device_init(&m->mdt_lu_dev, ldt);
5628 * Environment (env) might be missing mdt_thread_key values at that
5629 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5632 * Usually device allocation path doesn't use module key values, but
5633 * mdt has to do a lot of work here, so allocate key value.
5635 rc = lu_env_refill((struct lu_env *)env);
5639 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5640 LASSERT(info != NULL);
5642 obd = class_name2obd(dev);
5643 LASSERT(obd != NULL);
5645 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5646 m->mdt_opts.mo_evict_tgt_nids = 1;
5647 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5649 lmi = server_get_mount(dev);
5651 CERROR("Cannot get mount info for %s!\n", dev);
5654 lsi = s2lsi(lmi->lmi_sb);
5655 LASSERT(lsi->lsi_lmd);
5656 /* CMD is supported only in IAM mode */
5658 rc = kstrtol(num, 10, &node_id);
5662 obd->u.obt.obt_magic = OBT_MAGIC;
5663 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5664 m->mdt_skip_lfsck = 1;
5667 /* Just try to get a DoM lock by default. Otherwise, having a group
5668 * lock granted, it may get blocked for a long time. */
5669 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5670 /* DoM files are read at open and data is packed in the reply */
5671 m->mdt_opts.mo_dom_read_open = 1;
5673 m->mdt_squash.rsi_uid = 0;
5674 m->mdt_squash.rsi_gid = 0;
5675 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5676 spin_lock_init(&m->mdt_squash.rsi_lock);
5677 spin_lock_init(&m->mdt_lock);
5678 m->mdt_enable_remote_dir = 1;
5679 m->mdt_enable_striped_dir = 1;
5680 m->mdt_enable_dir_migration = 1;
5681 m->mdt_enable_dir_restripe = 0;
5682 m->mdt_enable_dir_auto_split = 0;
5683 m->mdt_enable_remote_dir_gid = 0;
5684 m->mdt_enable_chprojid_gid = 0;
5685 m->mdt_enable_remote_rename = 1;
5686 m->mdt_dir_restripe_nsonly = 1;
5687 m->mdt_enable_remote_subdir_mount = 1;
5689 atomic_set(&m->mdt_mds_mds_conns, 0);
5690 atomic_set(&m->mdt_async_commit_count, 0);
5692 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5693 m->mdt_lu_dev.ld_obd = obd;
5694 /* Set this lu_device to obd for error handling purposes. */
5695 obd->obd_lu_dev = &m->mdt_lu_dev;
5697 /* init the stack */
5698 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5700 CERROR("%s: Can't init device stack, rc %d\n",
5701 mdt_obd_name(m), rc);
5706 ss_site = mdt_seq_site(m);
5707 s->ld_seq_site = ss_site;
5710 /* set server index */
5711 ss_site->ss_node_id = node_id;
5713 /* failover is the default
5714 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5715 * assumed whose ss_node_id == 0 XXX
5717 obd->obd_replayable = 1;
5718 /* No connection accepted until configurations will finish */
5719 obd->obd_no_conn = 1;
5721 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5722 char *str = lustre_cfg_string(cfg, 4);
5723 if (strchr(str, 'n')) {
5724 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5725 obd->obd_replayable = 0;
5729 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5731 GOTO(err_fini_stack, rc);
5733 rc = mdt_seq_init(env, m);
5735 GOTO(err_fini_fld, rc);
5737 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5738 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5739 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5740 LDLM_NAMESPACE_SERVER,
5741 LDLM_NAMESPACE_GREEDY,
5743 if (IS_ERR(m->mdt_namespace)) {
5744 rc = PTR_ERR(m->mdt_namespace);
5745 CERROR("%s: unable to create server namespace: rc = %d\n",
5747 m->mdt_namespace = NULL;
5748 GOTO(err_fini_seq, rc);
5751 m->mdt_namespace->ns_lvbp = m;
5752 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5754 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5755 /* set obd_namespace for compatibility with old code */
5756 obd->obd_namespace = m->mdt_namespace;
5758 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5759 OBD_FAIL_MDS_ALL_REQUEST_NET,
5760 OBD_FAIL_MDS_ALL_REPLY_NET);
5762 GOTO(err_free_ns, rc);
5764 /* Amount of available space excluded from granting and reserved
5765 * for metadata. It is a percentage of the total MDT size. */
5766 tgd->tgd_reserved_pcnt = 10;
5768 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5769 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5771 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5773 rc = mdt_fs_setup(env, m, obd, lsi);
5777 fid.f_seq = FID_SEQ_LOCAL_NAME;
5780 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
5782 GOTO(err_fs_cleanup, rc);
5784 rc = mdt_hsm_cdt_init(m);
5786 CERROR("%s: error initializing coordinator, rc %d\n",
5787 mdt_obd_name(m), rc);
5788 GOTO(err_los_fini, rc);
5791 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5793 next = m->mdt_child;
5794 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5796 mntopts = dt_conf->ddp_mntopts;
5798 if (mntopts & MNTOPT_USERXATTR)
5799 m->mdt_opts.mo_user_xattr = 1;
5801 m->mdt_opts.mo_user_xattr = 0;
5803 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5805 if (mntopts & MNTOPT_ACL)
5806 m->mdt_opts.mo_acl = 1;
5808 m->mdt_opts.mo_acl = 0;
5810 /* XXX: to support suppgid for ACL, we enable identity_upcall
5811 * by default, otherwise, maybe got unexpected -EACCESS. */
5812 if (m->mdt_opts.mo_acl)
5813 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5815 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5817 &mdt_identity_upcall_cache_ops);
5818 if (IS_ERR(m->mdt_identity_cache)) {
5819 rc = PTR_ERR(m->mdt_identity_cache);
5820 m->mdt_identity_cache = NULL;
5821 GOTO(err_free_hsm, rc);
5824 rc = mdt_tunables_init(m, dev);
5826 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5827 GOTO(err_recovery, rc);
5830 rc = mdt_quota_init(env, m, cfg);
5832 GOTO(err_procfs, rc);
5834 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5835 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5836 "mdt_ldlm_client", m->mdt_ldlm_client);
5838 ping_evictor_start();
5840 /* recovery will be started upon mdt_prepare()
5841 * when the whole stack is complete and ready
5842 * to serve the requests */
5844 /* Reduce the initial timeout on an MDS because it doesn't need such
5845 * a long timeout as an OST does. Adaptive timeouts will adjust this
5846 * value appropriately. */
5847 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5848 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5850 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
5851 m->mdt_lut.lut_local_recovery = 1;
5853 rc = mdt_restriper_start(m);
5855 GOTO(err_ping_evictor, rc);
5860 ping_evictor_stop();
5862 mdt_tunables_fini(m);
5864 upcall_cache_cleanup(m->mdt_identity_cache);
5865 m->mdt_identity_cache = NULL;
5867 mdt_hsm_cdt_fini(m);
5869 local_oid_storage_fini(env, m->mdt_los);
5872 mdt_fs_cleanup(env, m);
5874 /* keep recoverable clients */
5876 target_recovery_fini(obd);
5877 obd_exports_barrier(obd);
5878 obd_zombie_barrier();
5879 tgt_fini(env, &m->mdt_lut);
5881 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5882 obd->obd_namespace = m->mdt_namespace = NULL;
5884 mdt_seq_fini(env, m);
5886 mdt_fld_fini(env, m);
5888 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5891 server_put_mount(dev, true);
5895 /* For interoperability, the left element is old parameter, the right one
5896 * is the new version of the parameter, if some parameter is deprecated,
5897 * the new version should be set as NULL. */
5898 static struct cfg_interop_param mdt_interop_param[] = {
5899 { "mdt.group_upcall", NULL },
5900 { "mdt.quota_type", NULL },
5901 { "mdd.quota_type", NULL },
5902 { "mdt.som", NULL },
5903 { "mdt.rootsquash", "mdt.root_squash" },
5904 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5908 /* used by MGS to process specific configurations */
5909 static int mdt_process_config(const struct lu_env *env,
5910 struct lu_device *d, struct lustre_cfg *cfg)
5912 struct mdt_device *m = mdt_dev(d);
5913 struct md_device *md_next = m->mdt_child;
5914 struct lu_device *next = md2lu_dev(md_next);
5918 switch (cfg->lcfg_command) {
5920 struct obd_device *obd = d->ld_obd;
5921 /* For interoperability */
5922 struct cfg_interop_param *ptr = NULL;
5923 struct lustre_cfg *old_cfg = NULL;
5927 param = lustre_cfg_string(cfg, 1);
5928 if (param == NULL) {
5929 CERROR("param is empty\n");
5934 ptr = class_find_old_param(param, mdt_interop_param);
5936 if (ptr->new_param == NULL) {
5938 CWARN("For interoperability, skip this %s."
5939 " It is obsolete.\n", ptr->old_param);
5943 CWARN("Found old param %s, changed it to %s.\n",
5944 ptr->old_param, ptr->new_param);
5947 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5954 count = class_modify_config(cfg, PARAM_MDT,
5955 &obd->obd_kset.kobj);
5957 struct coordinator *cdt = &m->mdt_coordinator;
5959 /* is it an HSM var ? */
5960 count = class_modify_config(cfg, PARAM_HSM,
5961 &cdt->cdt_hsm_kobj);
5963 /* we don't understand; pass it on */
5964 rc = next->ld_ops->ldo_process_config(env, next,
5967 rc = count > 0 ? 0 : count;
5969 rc = count > 0 ? 0 : count;
5973 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5974 cfg->lcfg_buflens));
5978 /* others are passed further */
5979 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5985 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5986 const struct lu_object_header *hdr,
5987 struct lu_device *d)
5989 struct mdt_object *mo;
5993 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5995 struct lu_object *o;
5996 struct lu_object_header *h;
5999 h = &mo->mot_header;
6000 lu_object_header_init(h);
6001 lu_object_init(o, h, d);
6002 lu_object_add_top(h, o);
6003 o->lo_ops = &mdt_obj_ops;
6004 spin_lock_init(&mo->mot_write_lock);
6005 mutex_init(&mo->mot_som_mutex);
6006 mutex_init(&mo->mot_lov_mutex);
6007 init_rwsem(&mo->mot_dom_sem);
6008 init_rwsem(&mo->mot_open_sem);
6009 atomic_set(&mo->mot_open_count, 0);
6010 mo->mot_restripe_offset = 0;
6011 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6017 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6018 const struct lu_object_conf *unused)
6020 struct mdt_device *d = mdt_dev(o->lo_dev);
6021 struct lu_device *under;
6022 struct lu_object *below;
6026 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6027 PFID(lu_object_fid(o)));
6029 under = &d->mdt_child->md_lu_dev;
6030 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6031 if (below != NULL) {
6032 lu_object_add(o, below);
6039 static void mdt_object_free_rcu(struct rcu_head *head)
6041 struct mdt_object *mo = container_of(head, struct mdt_object,
6042 mot_header.loh_rcu);
6044 kmem_cache_free(mdt_object_kmem, mo);
6047 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6049 struct mdt_object *mo = mdt_obj(o);
6050 struct lu_object_header *h;
6054 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6055 PFID(lu_object_fid(o)));
6057 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6058 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6061 lu_object_header_fini(h);
6062 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6063 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6068 static int mdt_object_print(const struct lu_env *env, void *cookie,
6069 lu_printer_t p, const struct lu_object *o)
6071 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6073 return (*p)(env, cookie,
6074 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6075 mdto, mdto->mot_lov_created ? "lov_created" : "",
6076 mdto->mot_cache_attr ? "cache_attr" : "",
6077 mdto->mot_write_count);
6080 static int mdt_prepare(const struct lu_env *env,
6081 struct lu_device *pdev,
6082 struct lu_device *cdev)
6084 struct mdt_device *mdt = mdt_dev(cdev);
6085 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6086 struct obd_device *obd = cdev->ld_obd;
6093 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6097 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6101 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6105 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6106 /* The LFSCK instance is registered just now, so it must be there when
6107 * register the namespace to such instance. */
6108 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6110 if (mdt->mdt_seq_site.ss_node_id == 0) {
6111 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6112 &mdt->mdt_md_root_fid);
6117 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6119 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6120 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6121 LASSERT(obd->obd_no_conn);
6122 spin_lock(&obd->obd_dev_lock);
6123 obd->obd_no_conn = 0;
6124 spin_unlock(&obd->obd_dev_lock);
6126 if (obd->obd_recovering == 0)
6127 mdt_postrecov(env, mdt);
6132 const struct lu_device_operations mdt_lu_ops = {
6133 .ldo_object_alloc = mdt_object_alloc,
6134 .ldo_process_config = mdt_process_config,
6135 .ldo_prepare = mdt_prepare,
6138 static const struct lu_object_operations mdt_obj_ops = {
6139 .loo_object_init = mdt_object_init,
6140 .loo_object_free = mdt_object_free,
6141 .loo_object_print = mdt_object_print
6144 static int mdt_obd_set_info_async(const struct lu_env *env,
6145 struct obd_export *exp,
6146 __u32 keylen, void *key,
6147 __u32 vallen, void *val,
6148 struct ptlrpc_request_set *set)
6154 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6155 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6162 static inline void mdt_enable_slc(struct mdt_device *mdt)
6164 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6165 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6168 static inline void mdt_disable_slc(struct mdt_device *mdt)
6170 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6171 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6175 * Match client and server connection feature flags.
6177 * Compute the compatibility flags for a connection request based on
6178 * features mutually supported by client and server.
6180 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6181 * must not be updated here, otherwise a partially initialized value may
6182 * be exposed. After the connection request is successfully processed,
6183 * the top-level MDT connect request handler atomically updates the export
6184 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6185 * reply. \see mdt_connect().
6187 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6188 * full struct obd_connect_data. So care must be taken when accessing fields
6189 * that are not present in struct obd_connect_data_v1. See LU-16.
6191 * \param exp the obd_export associated with this client/target pair
6192 * \param mdt the target device for the connection
6193 * \param data stores data for this connect request
6196 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6197 * \retval -EBADE client and server feature requirements are incompatible
6199 static int mdt_connect_internal(const struct lu_env *env,
6200 struct obd_export *exp,
6201 struct mdt_device *mdt,
6202 struct obd_connect_data *data, bool reconnect)
6204 const char *obd_name = mdt_obd_name(mdt);
6205 LASSERT(data != NULL);
6207 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6209 if (mdt->mdt_bottom->dd_rdonly &&
6210 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6211 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6214 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6215 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6217 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6219 if (!mdt->mdt_opts.mo_acl)
6220 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6222 if (!mdt->mdt_opts.mo_user_xattr)
6223 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6225 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6226 data->ocd_brw_size = min(data->ocd_brw_size,
6228 if (data->ocd_brw_size == 0) {
6229 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6230 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6231 "ocd_brw_size unexpectedly zero, network data "
6232 "corruption? Refusing to connect this client\n",
6233 obd_name, exp->exp_client_uuid.uuid,
6234 exp, data->ocd_connect_flags, data->ocd_version,
6235 data->ocd_grant, data->ocd_index);
6240 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6241 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6243 /* client is reporting its page size, for future use */
6244 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6245 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6246 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6247 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6248 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6249 /* ocd_grant_tax_kb is in 1K byte blocks */
6250 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6251 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6254 /* Save connect_data we have so far because tgt_grant_connect()
6255 * uses it to calculate grant, and we want to save the client
6256 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6257 exp->exp_connect_data = *data;
6258 if (OCD_HAS_FLAG(data, GRANT))
6259 tgt_grant_connect(env, exp, data, !reconnect);
6261 if (OCD_HAS_FLAG(data, MAXBYTES))
6262 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6264 /* NB: Disregard the rule against updating
6265 * exp_connect_data.ocd_connect_flags in this case, since
6266 * tgt_client_new() needs to know if this is a lightweight
6267 * connection, and it is safe to expose this flag before
6268 * connection processing completes. */
6269 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6270 spin_lock(&exp->exp_lock);
6271 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6272 spin_unlock(&exp->exp_lock);
6275 data->ocd_version = LUSTRE_VERSION_CODE;
6277 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6278 CWARN("%s: MDS requires FID support, but client not\n",
6283 if (OCD_HAS_FLAG(data, PINGLESS)) {
6284 if (ptlrpc_pinger_suppress_pings()) {
6285 spin_lock(&exp->exp_obd->obd_dev_lock);
6286 list_del_init(&exp->exp_obd_chain_timed);
6287 spin_unlock(&exp->exp_obd->obd_dev_lock);
6289 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6293 data->ocd_max_easize = mdt->mdt_max_ea_size;
6295 /* NB: Disregard the rule against updating
6296 * exp_connect_data.ocd_connect_flags in this case, since
6297 * tgt_client_new() needs to know if this is client supports
6298 * multiple modify RPCs, and it is safe to expose this flag before
6299 * connection processing completes. */
6300 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6301 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6302 spin_lock(&exp->exp_lock);
6303 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6304 spin_unlock(&exp->exp_lock);
6307 if (OCD_HAS_FLAG(data, CKSUM)) {
6308 __u32 cksum_types = data->ocd_cksum_types;
6310 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6312 if (unlikely(data->ocd_cksum_types == 0)) {
6313 CERROR("%s: Connect with checksum support but no "
6314 "ocd_cksum_types is set\n",
6315 exp->exp_obd->obd_name);
6319 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6320 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6321 cksum_types, data->ocd_cksum_types);
6323 /* This client does not support OBD_CONNECT_CKSUM
6324 * fall back to CRC32 */
6325 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6326 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6327 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6330 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6331 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6332 atomic_inc(&mdt->mdt_mds_mds_conns);
6333 mdt_enable_slc(mdt);
6336 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6337 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6342 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6343 struct mdt_thread_info *info,
6344 struct mdt_file_data *mfd)
6346 struct lu_context ses;
6350 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6355 lu_context_enter(&ses);
6357 mdt_ucred(info)->uc_valid = UCRED_OLD;
6358 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6360 lu_context_exit(&ses);
6361 lu_context_fini(&ses);
6367 static int mdt_export_cleanup(struct obd_export *exp)
6369 LIST_HEAD(closing_list);
6370 struct mdt_export_data *med = &exp->exp_mdt_data;
6371 struct obd_device *obd = exp->exp_obd;
6372 struct mdt_device *mdt;
6373 struct mdt_thread_info *info;
6375 struct mdt_file_data *mfd, *n;
6379 spin_lock(&med->med_open_lock);
6380 while (!list_empty(&med->med_open_head)) {
6381 struct list_head *tmp = med->med_open_head.next;
6382 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6384 /* Remove mfd handle so it can't be found again.
6385 * We are consuming the mfd_list reference here. */
6386 class_handle_unhash(&mfd->mfd_open_handle);
6387 list_move_tail(&mfd->mfd_list, &closing_list);
6389 spin_unlock(&med->med_open_lock);
6390 mdt = mdt_dev(obd->obd_lu_dev);
6391 LASSERT(mdt != NULL);
6393 rc = lu_env_init(&env, LCT_MD_THREAD);
6397 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6398 LASSERT(info != NULL);
6399 memset(info, 0, sizeof *info);
6400 info->mti_env = &env;
6401 info->mti_mdt = mdt;
6402 info->mti_exp = exp;
6404 if (!list_empty(&closing_list)) {
6405 struct md_attr *ma = &info->mti_attr;
6407 /* Close any open files (which may also cause orphan
6409 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6410 list_del_init(&mfd->mfd_list);
6411 ma->ma_need = ma->ma_valid = 0;
6413 /* This file is being closed due to an eviction, it
6414 * could have been modified and now dirty regarding to
6415 * HSM archive, check this!
6416 * The logic here is to mark a file dirty if there's a
6417 * chance it was dirtied before the client was evicted,
6418 * so that we don't have to wait for a release attempt
6419 * before finding out the file was actually dirty and
6420 * fail the release. Aggressively marking it dirty here
6421 * will cause the policy engine to attempt to
6422 * re-archive it; when rearchiving, we can compare the
6423 * current version to the HSM data_version and make the
6424 * archive request into a noop if it's not actually
6427 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6428 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6430 /* Don't unlink orphan on failover umount, LU-184 */
6431 if (exp->exp_flags & OBD_OPT_FAILOVER) {
6432 ma->ma_valid = MA_FLAGS;
6433 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6435 ma->ma_valid |= MA_FORCE_LOG;
6436 mdt_mfd_close(info, mfd);
6439 info->mti_mdt = NULL;
6440 /* cleanup client slot early */
6441 /* Do not erase record for recoverable client. */
6442 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6443 tgt_client_del(&env, exp);
6449 static int mdt_obd_disconnect(struct obd_export *exp)
6456 class_export_get(exp);
6458 if (!(exp->exp_flags & OBD_OPT_FORCE))
6459 tgt_grant_sanity_check(exp->exp_obd, __func__);
6461 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6462 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6463 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6465 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6466 mdt_disable_slc(mdt);
6469 rc = server_disconnect_export(exp);
6471 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6473 tgt_grant_discard(exp);
6475 rc = mdt_export_cleanup(exp);
6476 nodemap_del_member(exp);
6477 class_export_put(exp);
6481 /* mds_connect copy */
6482 static int mdt_obd_connect(const struct lu_env *env,
6483 struct obd_export **exp, struct obd_device *obd,
6484 struct obd_uuid *cluuid,
6485 struct obd_connect_data *data,
6488 struct obd_export *lexp;
6489 struct lustre_handle conn = { 0 };
6490 struct mdt_device *mdt;
6492 lnet_nid_t *client_nid = localdata;
6495 LASSERT(env != NULL);
6496 LASSERT(data != NULL);
6498 if (!exp || !obd || !cluuid)
6501 mdt = mdt_dev(obd->obd_lu_dev);
6504 * first, check whether the stack is ready to handle requests
6505 * XXX: probably not very appropriate method is used now
6506 * at some point we should find a better one
6508 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6509 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6510 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6511 rc = obd_get_info(env, mdt->mdt_child_exp,
6512 sizeof(KEY_OSP_CONNECTED),
6513 KEY_OSP_CONNECTED, NULL, NULL);
6516 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6519 rc = class_connect(&conn, obd, cluuid);
6523 lexp = class_conn2export(&conn);
6524 LASSERT(lexp != NULL);
6526 rc = nodemap_add_member(*client_nid, lexp);
6527 if (rc != 0 && rc != -EEXIST)
6530 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6532 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6535 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6536 rc = tgt_client_new(env, lexp);
6538 mdt_export_stats_init(obd, lexp, localdata);
6542 class_disconnect(lexp);
6543 nodemap_del_member(lexp);
6547 /* Because we do not want this export to be evicted by pinger,
6548 * let's not add this export to the timed chain list. */
6549 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6550 spin_lock(&lexp->exp_obd->obd_dev_lock);
6551 list_del_init(&lexp->exp_obd_chain_timed);
6552 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6559 static int mdt_obd_reconnect(const struct lu_env *env,
6560 struct obd_export *exp, struct obd_device *obd,
6561 struct obd_uuid *cluuid,
6562 struct obd_connect_data *data,
6565 lnet_nid_t *client_nid = localdata;
6569 if (exp == NULL || obd == NULL || cluuid == NULL)
6572 rc = nodemap_add_member(*client_nid, exp);
6573 if (rc != 0 && rc != -EEXIST)
6576 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6579 mdt_export_stats_init(obd, exp, localdata);
6581 nodemap_del_member(exp);
6586 /* FIXME: Can we avoid using these two interfaces? */
6587 static int mdt_init_export(struct obd_export *exp)
6589 struct mdt_export_data *med = &exp->exp_mdt_data;
6593 INIT_LIST_HEAD(&med->med_open_head);
6594 spin_lock_init(&med->med_open_lock);
6595 spin_lock(&exp->exp_lock);
6596 exp->exp_connecting = 1;
6597 spin_unlock(&exp->exp_lock);
6599 OBD_ALLOC(exp->exp_used_slots,
6600 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6601 if (exp->exp_used_slots == NULL)
6604 /* self-export doesn't need client data and ldlm initialization */
6605 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6606 &exp->exp_client_uuid)))
6609 rc = tgt_client_alloc(exp);
6613 rc = ldlm_init_export(exp);
6620 tgt_client_free(exp);
6622 OBD_FREE(exp->exp_used_slots,
6623 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6624 exp->exp_used_slots = NULL;
6626 CERROR("%s: Failed to initialize export: rc = %d\n",
6627 exp->exp_obd->obd_name, rc);
6631 static int mdt_destroy_export(struct obd_export *exp)
6635 target_destroy_export(exp);
6636 if (exp->exp_used_slots)
6637 OBD_FREE(exp->exp_used_slots,
6638 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6640 /* destroy can be called from failed obd_setup, so
6641 * checking uuid is safer than obd_self_export */
6642 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6643 &exp->exp_client_uuid)))
6646 ldlm_destroy_export(exp);
6647 tgt_client_free(exp);
6649 LASSERT(list_empty(&exp->exp_outstanding_replies));
6650 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6653 * discard grants once we're sure no more
6654 * interaction with the client is possible
6656 tgt_grant_discard(exp);
6657 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6658 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6660 if (!(exp->exp_flags & OBD_OPT_FORCE))
6661 tgt_grant_sanity_check(exp->exp_obd, __func__);
6666 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6667 struct linkea_data *ldata)
6671 LASSERT(ldata->ld_buf->lb_buf != NULL);
6673 if (!mdt_object_exists(mdt_obj))
6676 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6677 ldata->ld_buf, XATTR_NAME_LINK);
6678 if (rc == -ERANGE) {
6679 /* Buf was too small, figure out what we need. */
6680 lu_buf_free(ldata->ld_buf);
6681 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6682 ldata->ld_buf, XATTR_NAME_LINK);
6685 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6686 if (ldata->ld_buf->lb_buf == NULL)
6688 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6689 ldata->ld_buf, XATTR_NAME_LINK);
6694 return linkea_init_with_rec(ldata);
6698 * Given an MDT object, try to look up the full path to the object.
6699 * Part of the MDT layer implementation of lfs fid2path.
6701 * \param[in] info Per-thread common data shared by MDT level handlers.
6702 * \param[in] obj Object to do path lookup of
6703 * \param[in,out] fp User-provided struct to store path information
6704 * \param[in] root_fid Root FID of current path should reach
6706 * \retval 0 Lookup successful, path information stored in fp
6707 * \retval -EAGAIN Lookup failed, usually because object is being moved
6708 * \retval negative errno if there was a problem
6710 static int mdt_path_current(struct mdt_thread_info *info,
6711 struct mdt_object *obj,
6712 struct getinfo_fid2path *fp,
6713 struct lu_fid *root_fid)
6715 struct mdt_device *mdt = info->mti_mdt;
6716 struct lu_name *tmpname = &info->mti_name;
6717 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6718 struct lu_buf *buf = &info->mti_big_buf;
6719 struct md_attr *ma = &info->mti_attr;
6720 struct linkea_data ldata = { NULL };
6722 struct mdt_object *mdt_obj;
6723 struct link_ea_header *leh;
6724 struct link_ea_entry *lee;
6731 /* temp buffer for path element, the buffer will be finally freed
6732 * in mdt_thread_info_fini */
6733 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6734 if (buf->lb_buf == NULL)
6738 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6741 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6743 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6744 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6745 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6746 GOTO(out, rc = -ENOENT);
6748 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6750 mdt_object_get(info->mti_env, mdt_obj);
6752 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6753 if (IS_ERR(mdt_obj))
6754 GOTO(out, rc = PTR_ERR(mdt_obj));
6757 if (!mdt_object_exists(mdt_obj)) {
6758 mdt_object_put(info->mti_env, mdt_obj);
6759 GOTO(out, rc = -ENOENT);
6762 if (mdt_object_remote(mdt_obj)) {
6763 mdt_object_put(info->mti_env, mdt_obj);
6764 GOTO(remote_out, rc = -EREMOTE);
6767 rc = mdt_links_read(info, mdt_obj, &ldata);
6769 mdt_object_put(info->mti_env, mdt_obj);
6774 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6775 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6776 /* If set, use link #linkno for path lookup, otherwise use
6777 link #0. Only do this for the final path element. */
6778 if (first && fp->gf_linkno < leh->leh_reccount) {
6780 for (count = 0; count < fp->gf_linkno; count++) {
6781 lee = (struct link_ea_entry *)
6782 ((char *)lee + reclen);
6783 linkea_entry_unpack(lee, &reclen, tmpname,
6786 if (fp->gf_linkno < leh->leh_reccount - 1)
6787 /* indicate to user there are more links */
6791 /* Check if it is slave stripes */
6792 rc = mdt_stripe_get(info, mdt_obj, ma, XATTR_NAME_LMV);
6793 mdt_object_put(info->mti_env, mdt_obj);
6797 if (ma->ma_valid & MA_LMV) {
6798 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
6800 if (!lmv_is_sane2(lmv))
6801 GOTO(out, rc = -EBADF);
6803 /* For slave stripes, get its master */
6804 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE) {
6805 fp->gf_fid = *tmpfid;
6810 /* Pack the name in the end of the buffer */
6811 ptr -= tmpname->ln_namelen;
6812 if (ptr - 1 <= fp->gf_u.gf_path)
6813 GOTO(out, rc = -EOVERFLOW);
6814 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6817 /* keep the last resolved fid to the client, so the
6818 * client will build the left path on another MDT for
6820 fp->gf_fid = *tmpfid;
6825 /* non-zero will be treated as an error */
6829 ptr++; /* skip leading / */
6830 memmove(fp->gf_u.gf_path, ptr,
6831 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6838 * Given an MDT object, use mdt_path_current to get the path.
6839 * Essentially a wrapper to retry mdt_path_current a set number of times
6840 * if -EAGAIN is returned (usually because an object is being moved).
6842 * Part of the MDT layer implementation of lfs fid2path.
6844 * \param[in] info Per-thread common data shared by mdt level handlers.
6845 * \param[in] obj Object to do path lookup of
6846 * \param[in,out] fp User-provided struct for arguments and to store path
6849 * \retval 0 Lookup successful, path information stored in fp
6850 * \retval negative errno if there was a problem
6852 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6853 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6855 struct mdt_device *mdt = info->mti_mdt;
6860 if (fp->gf_pathlen < 3)
6863 if (root_fid == NULL)
6864 root_fid = &mdt->mdt_md_root_fid;
6866 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6867 fp->gf_u.gf_path[0] = '\0';
6871 /* Retry multiple times in case file is being moved */
6872 while (tries-- && rc == -EAGAIN)
6873 rc = mdt_path_current(info, obj, fp, root_fid);
6879 * Get the full path of the provided FID, as of changelog record recno.
6881 * This checks sanity and looks up object for user provided FID
6882 * before calling the actual path lookup code.
6884 * Part of the MDT layer implementation of lfs fid2path.
6886 * \param[in] info Per-thread common data shared by mdt level handlers.
6887 * \param[in,out] fp User-provided struct for arguments and to store path
6890 * \retval 0 Lookup successful, path information and recno stored in fp
6891 * \retval -ENOENT, object does not exist
6892 * \retval negative errno if there was a problem
6894 static int mdt_fid2path(struct mdt_thread_info *info,
6895 struct lu_fid *root_fid,
6896 struct getinfo_fid2path *fp)
6898 struct mdt_device *mdt = info->mti_mdt;
6899 struct mdt_object *obj;
6903 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6904 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6906 if (!fid_is_sane(&fp->gf_fid))
6909 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6910 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6911 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6912 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6916 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6919 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6920 PFID(&fp->gf_fid), rc);
6924 if (mdt_object_remote(obj))
6926 else if (!mdt_object_exists(obj))
6932 mdt_object_put(info->mti_env, obj);
6933 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6934 PFID(&fp->gf_fid), rc);
6938 rc = mdt_path(info, obj, fp, root_fid);
6940 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6941 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6942 fp->gf_recno, fp->gf_linkno);
6944 mdt_object_put(info->mti_env, obj);
6949 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6950 void *val, int vallen)
6952 struct getinfo_fid2path *fpout, *fpin;
6953 struct lu_fid *root_fid = NULL;
6956 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6959 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6960 lustre_swab_fid2path(fpin);
6962 memcpy(fpout, fpin, sizeof(*fpin));
6963 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6966 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6967 sizeof(struct lu_fid)) {
6968 /* client sent its root FID, which is normally fileset FID */
6969 root_fid = fpin->gf_u.gf_root_fid;
6970 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6971 lustre_swab_lu_fid(root_fid);
6973 if (root_fid != NULL && !fid_is_sane(root_fid))
6977 rc = mdt_fid2path(info, root_fid, fpout);
6981 int mdt_get_info(struct tgt_session_info *tsi)
6991 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6993 CDEBUG(D_IOCTL, "No GETINFO key\n");
6994 RETURN(err_serious(-EFAULT));
6996 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6999 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7000 if (vallen == NULL) {
7001 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7002 tgt_name(tsi->tsi_tgt));
7003 RETURN(err_serious(-EFAULT));
7006 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7008 rc = req_capsule_server_pack(tsi->tsi_pill);
7010 RETURN(err_serious(rc));
7012 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7013 if (valout == NULL) {
7014 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7015 tgt_name(tsi->tsi_tgt));
7016 RETURN(err_serious(-EFAULT));
7019 if (KEY_IS(KEY_FID2PATH)) {
7020 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7022 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7023 mdt_thread_info_fini(info);
7030 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7032 struct obd_ioctl_data *data = karg;
7035 struct mdt_object *obj;
7036 struct mdt_lock_handle *lh;
7040 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7041 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7044 fid = (struct lu_fid *)data->ioc_inlbuf1;
7046 if (!fid_is_sane(fid))
7049 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7051 lh = &mti->mti_lh[MDT_LH_PARENT];
7052 mdt_lock_reg_init(lh, LCK_CR);
7054 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7056 RETURN(PTR_ERR(obj));
7058 if (mdt_object_remote(obj)) {
7061 * before calling version get the correct MDS should be
7062 * fid, this is error to find remote object here
7064 CERROR("nonlocal object "DFID"\n", PFID(fid));
7065 } else if (!mdt_object_exists(obj)) {
7066 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7069 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7070 *(__u64 *)data->ioc_inlbuf2 = version;
7073 mdt_object_unlock_put(mti, obj, lh, 1);
7077 /* ioctls on obd dev */
7078 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7079 void *karg, void __user *uarg)
7082 struct obd_device *obd = exp->exp_obd;
7083 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7084 struct dt_device *dt = mdt->mdt_bottom;
7088 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7089 rc = lu_env_init(&env, LCT_MD_THREAD);
7095 rc = mdt_device_sync(&env, mdt);
7097 case OBD_IOC_SET_READONLY:
7098 rc = dt_sync(&env, dt);
7100 rc = dt_ro(&env, dt);
7102 case OBD_IOC_ABORT_RECOVERY: {
7103 struct obd_ioctl_data *data = karg;
7105 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7106 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT)
7107 obd->obd_abort_recov_mdt = 1;
7108 else /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7109 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7110 obd->obd_abort_recovery = 1;
7112 target_stop_recovery_thread(obd);
7116 case OBD_IOC_CHANGELOG_REG:
7117 case OBD_IOC_CHANGELOG_DEREG:
7118 case OBD_IOC_CHANGELOG_CLEAR:
7119 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7123 case OBD_IOC_START_LFSCK: {
7124 struct md_device *next = mdt->mdt_child;
7125 struct obd_ioctl_data *data = karg;
7126 struct lfsck_start_param lsp;
7128 if (unlikely(data == NULL)) {
7133 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7134 lsp.lsp_index_valid = 0;
7135 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7138 case OBD_IOC_STOP_LFSCK: {
7139 struct md_device *next = mdt->mdt_child;
7140 struct obd_ioctl_data *data = karg;
7141 struct lfsck_stop stop;
7143 stop.ls_status = LS_STOPPED;
7144 /* Old lfsck utils may pass NULL @stop. */
7145 if (data->ioc_inlbuf1 == NULL)
7149 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7151 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7154 case OBD_IOC_QUERY_LFSCK: {
7155 struct md_device *next = mdt->mdt_child;
7156 struct obd_ioctl_data *data = karg;
7158 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7162 case OBD_IOC_GET_OBJ_VERSION: {
7163 struct mdt_thread_info *mti;
7164 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7165 memset(mti, 0, sizeof *mti);
7166 mti->mti_env = &env;
7170 rc = mdt_ioc_version_get(mti, karg);
7173 case OBD_IOC_CATLOGLIST: {
7174 struct mdt_thread_info *mti;
7176 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7177 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7178 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7179 &mti->mti_tmp_fid1);
7184 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7185 mdt_obd_name(mdt), cmd, rc);
7192 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7194 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7198 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7199 struct lfsck_start_param lsp;
7201 lsp.lsp_start = NULL;
7202 lsp.lsp_index_valid = 0;
7203 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7204 OBD_IOC_START_LFSCK,
7206 if (rc != 0 && rc != -EALREADY)
7207 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7208 mdt_obd_name(mdt), rc);
7211 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7215 static int mdt_obd_postrecov(struct obd_device *obd)
7220 rc = lu_env_init(&env, LCT_MD_THREAD);
7223 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7228 static const struct obd_ops mdt_obd_device_ops = {
7229 .o_owner = THIS_MODULE,
7230 .o_set_info_async = mdt_obd_set_info_async,
7231 .o_connect = mdt_obd_connect,
7232 .o_reconnect = mdt_obd_reconnect,
7233 .o_disconnect = mdt_obd_disconnect,
7234 .o_init_export = mdt_init_export,
7235 .o_destroy_export = mdt_destroy_export,
7236 .o_iocontrol = mdt_iocontrol,
7237 .o_postrecov = mdt_obd_postrecov,
7238 /* Data-on-MDT IO methods */
7239 .o_preprw = mdt_obd_preprw,
7240 .o_commitrw = mdt_obd_commitrw,
7243 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7244 struct lu_device *d)
7246 struct mdt_device *m = mdt_dev(d);
7253 static struct lu_device *mdt_device_free(const struct lu_env *env,
7254 struct lu_device *d)
7256 struct mdt_device *m = mdt_dev(d);
7259 lu_device_fini(&m->mdt_lu_dev);
7265 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7266 struct lu_device_type *t,
7267 struct lustre_cfg *cfg)
7269 struct lu_device *l;
7270 struct mdt_device *m;
7277 rc = mdt_init0(env, m, t, cfg);
7279 mdt_device_free(env, l);
7284 l = ERR_PTR(-ENOMEM);
7288 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7289 LU_KEY_INIT(mdt, struct mdt_thread_info);
7291 static void mdt_key_fini(const struct lu_context *ctx,
7292 struct lu_context_key *key, void* data)
7294 struct mdt_thread_info *info = data;
7296 if (info->mti_big_lmm) {
7297 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7298 info->mti_big_lmm = NULL;
7299 info->mti_big_lmmsize = 0;
7302 if (info->mti_big_acl) {
7303 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7304 info->mti_big_acl = NULL;
7305 info->mti_big_aclsize = 0;
7311 /* context key: mdt_thread_key */
7312 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7314 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7316 return lu_ucred(info->mti_env);
7319 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7321 return lu_ucred_check(info->mti_env);
7325 * Enable/disable COS (Commit On Sharing).
7327 * Set/Clear the COS flag in mdt options.
7329 * \param mdt mdt device
7330 * \param val 0 disables COS, other values enable COS
7332 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7337 mdt->mdt_opts.mo_cos = val;
7338 rc = lu_env_init(&env, LCT_LOCAL);
7339 if (unlikely(rc != 0)) {
7340 CWARN("%s: lu_env initialization failed, cannot "
7341 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7344 mdt_device_sync(&env, mdt);
7349 * Check COS (Commit On Sharing) status.
7351 * Return COS flag status.
7353 * \param mdt mdt device
7355 int mdt_cos_is_enabled(struct mdt_device *mdt)
7357 return mdt->mdt_opts.mo_cos != 0;
7360 static const struct lu_device_type_operations mdt_device_type_ops = {
7361 .ldto_device_alloc = mdt_device_alloc,
7362 .ldto_device_free = mdt_device_free,
7363 .ldto_device_fini = mdt_device_fini
7366 static struct lu_device_type mdt_device_type = {
7367 .ldt_tags = LU_DEVICE_MD,
7368 .ldt_name = LUSTRE_MDT_NAME,
7369 .ldt_ops = &mdt_device_type_ops,
7370 .ldt_ctx_tags = LCT_MD_THREAD
7373 static int __init mdt_init(void)
7377 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7378 FID_NOBRACE_LEN + 1);
7379 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7381 rc = lu_kmem_init(mdt_caches);
7385 rc = mds_mod_init();
7389 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7390 LUSTRE_MDT_NAME, &mdt_device_type);
7395 lu_kmem_fini(mdt_caches);
7402 static void __exit mdt_exit(void)
7404 class_unregister_type(LUSTRE_MDT_NAME);
7406 lu_kmem_fini(mdt_caches);
7409 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7410 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7411 MODULE_VERSION(LUSTRE_VERSION_STRING);
7412 MODULE_LICENSE("GPL");
7414 module_init(mdt_init);
7415 module_exit(mdt_exit);