4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
61 #include <lustre_lmv.h>
63 #include <obd_support.h>
64 #include <lustre_barrier.h>
65 #include <obd_cksum.h>
66 #include <llog_swab.h>
68 #include "mdt_internal.h"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static const struct lu_object_operations mdt_obj_ops;
100 /* Slab for MDT object allocation */
101 static struct kmem_cache *mdt_object_kmem;
103 /* For HSM restore handles */
104 struct kmem_cache *mdt_hsm_cdt_kmem;
106 /* For HSM request handles */
107 struct kmem_cache *mdt_hsm_car_kmem;
109 static struct lu_kmem_descr mdt_caches[] = {
111 .ckd_cache = &mdt_object_kmem,
112 .ckd_name = "mdt_obj",
113 .ckd_size = sizeof(struct mdt_object)
116 .ckd_cache = &mdt_hsm_cdt_kmem,
117 .ckd_name = "mdt_cdt_restore_handle",
118 .ckd_size = sizeof(struct cdt_restore_handle)
121 .ckd_cache = &mdt_hsm_car_kmem,
122 .ckd_name = "mdt_cdt_agent_req",
123 .ckd_size = sizeof(struct cdt_agent_req)
130 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
134 return rep->lock_policy_res1 & op_flag;
137 void mdt_clear_disposition(struct mdt_thread_info *info,
138 struct ldlm_reply *rep, __u64 op_flag)
141 info->mti_opdata &= ~op_flag;
142 tgt_opdata_clear(info->mti_env, op_flag);
145 rep->lock_policy_res1 &= ~op_flag;
148 void mdt_set_disposition(struct mdt_thread_info *info,
149 struct ldlm_reply *rep, __u64 op_flag)
152 info->mti_opdata |= op_flag;
153 tgt_opdata_set(info->mti_env, op_flag);
156 rep->lock_policy_res1 |= op_flag;
159 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
161 lh->mlh_pdo_hash = 0;
162 lh->mlh_reg_mode = lm;
163 lh->mlh_rreg_mode = lm;
164 lh->mlh_type = MDT_REG_LOCK;
167 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
168 const struct lu_name *lname)
170 lh->mlh_reg_mode = lock_mode;
171 lh->mlh_pdo_mode = LCK_MINMODE;
172 lh->mlh_rreg_mode = lock_mode;
173 lh->mlh_type = MDT_PDO_LOCK;
175 if (lu_name_is_valid(lname)) {
176 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
178 /* XXX Workaround for LU-2856
180 * Zero is a valid return value of full_name_hash, but
181 * several users of mlh_pdo_hash assume a non-zero
182 * hash value. We therefore map zero onto an
183 * arbitrary, but consistent value (1) to avoid
184 * problems further down the road. */
185 if (unlikely(lh->mlh_pdo_hash == 0))
186 lh->mlh_pdo_hash = 1;
188 lh->mlh_pdo_hash = 0;
192 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
193 struct mdt_lock_handle *lh)
199 * Any dir access needs couple of locks:
201 * 1) on part of dir we gonna take lookup/modify;
203 * 2) on whole dir to protect it from concurrent splitting and/or to
204 * flush client's cache for readdir().
206 * so, for a given mode and object this routine decides what lock mode
207 * to use for lock #2:
209 * 1) if caller's gonna lookup in dir then we need to protect dir from
210 * being splitted only - LCK_CR
212 * 2) if caller's gonna modify dir then we need to protect dir from
213 * being splitted and to flush cache - LCK_CW
215 * 3) if caller's gonna modify dir and that dir seems ready for
216 * splitting then we need to protect it from any type of access
217 * (lookup/modify/split) - LCK_EX --bzzz
220 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
221 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
224 * Ask underlaying level its opinion about preferable PDO lock mode
225 * having access type passed as regular lock mode:
227 * - MDL_MINMODE means that lower layer does not want to specify lock
230 * - MDL_NL means that no PDO lock should be taken. This is used in some
231 * cases. Say, for non-splittable directories no need to use PDO locks
234 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
235 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
237 if (mode != MDL_MINMODE) {
238 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
241 * Lower layer does not want to specify locking mode. We do it
242 * our selves. No special protection is needed, just flush
243 * client's cache on modification and allow concurrent
246 switch (lh->mlh_reg_mode) {
248 lh->mlh_pdo_mode = LCK_EX;
251 lh->mlh_pdo_mode = LCK_CR;
254 lh->mlh_pdo_mode = LCK_CW;
257 CERROR("Not expected lock type (0x%x)\n",
258 (int)lh->mlh_reg_mode);
263 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
267 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
270 struct mdt_device *mdt = info->mti_mdt;
271 struct lu_name *lname = &info->mti_name;
272 char *filename = info->mti_filename;
273 struct mdt_object *parent;
277 LASSERT(!info->mti_cross_ref);
280 * We may want to allow this to mount a completely separate
281 * fileset from the MDT in the future, but keeping it to
282 * ROOT/ only for now avoid potential security issues.
284 *fid = mdt->mdt_md_root_fid;
286 while (rc == 0 && fileset != NULL && *fileset != '\0') {
287 const char *s1 = fileset;
293 while (*s2 != '/' && *s2 != '\0')
301 lname->ln_namelen = s2 - s1;
302 if (lname->ln_namelen > NAME_MAX) {
307 /* reject .. as a path component */
308 if (lname->ln_namelen == 2 &&
309 strncmp(s1, "..", 2) == 0) {
314 strncpy(filename, s1, lname->ln_namelen);
315 filename[lname->ln_namelen] = '\0';
316 lname->ln_name = filename;
318 parent = mdt_object_find(info->mti_env, mdt, fid);
319 if (IS_ERR(parent)) {
320 rc = PTR_ERR(parent);
323 /* Only got the fid of this obj by name */
325 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
326 fid, &info->mti_spec);
327 mdt_object_put(info->mti_env, parent);
330 parent = mdt_object_find(info->mti_env, mdt, fid);
332 rc = PTR_ERR(parent);
334 mode = lu_object_attr(&parent->mot_obj);
335 mdt_object_put(info->mti_env, parent);
344 static int mdt_get_root(struct tgt_session_info *tsi)
346 struct mdt_thread_info *info = tsi2mdt_info(tsi);
347 struct mdt_device *mdt = info->mti_mdt;
348 struct mdt_body *repbody;
349 char *fileset = NULL, *buffer = NULL;
351 struct obd_export *exp = info->mti_exp;
352 char *nodemap_fileset;
356 rc = mdt_check_ucred(info);
358 GOTO(out, rc = err_serious(rc));
360 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
361 GOTO(out, rc = err_serious(-ENOMEM));
363 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
364 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
365 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
367 GOTO(out, rc = err_serious(-EFAULT));
370 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
371 if (nodemap_fileset && nodemap_fileset[0]) {
372 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
374 /* consider fileset from client as a sub-fileset
375 * of the nodemap one */
376 OBD_ALLOC(buffer, PATH_MAX + 1);
378 GOTO(out, rc = err_serious(-ENOMEM));
379 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
380 nodemap_fileset, fileset) >= PATH_MAX + 1)
381 GOTO(out, rc = err_serious(-EINVAL));
384 /* enforce fileset as specified in the nodemap */
385 fileset = nodemap_fileset;
390 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
391 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
393 GOTO(out, rc = err_serious(rc));
395 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
397 repbody->mbo_valid |= OBD_MD_FLID;
401 mdt_thread_info_fini(info);
403 OBD_FREE(buffer, PATH_MAX+1);
407 static int mdt_statfs(struct tgt_session_info *tsi)
409 struct ptlrpc_request *req = tgt_ses_req(tsi);
410 struct mdt_thread_info *info = tsi2mdt_info(tsi);
411 struct mdt_device *mdt = info->mti_mdt;
412 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
413 struct md_device *next = mdt->mdt_child;
414 struct ptlrpc_service_part *svcpt;
415 struct obd_statfs *osfs;
416 struct mdt_body *reqbody = NULL;
417 struct mdt_statfs_cache *msf;
418 ktime_t kstart = ktime_get();
423 svcpt = req->rq_rqbd->rqbd_svcpt;
425 /* This will trigger a watchdog timeout */
426 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
427 (MDT_SERVICE_WATCHDOG_FACTOR *
428 at_get(&svcpt->scp_at_estimate)) + 1);
430 rc = mdt_check_ucred(info);
432 GOTO(out, rc = err_serious(rc));
434 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
435 GOTO(out, rc = err_serious(-ENOMEM));
437 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
439 GOTO(out, rc = -EPROTO);
441 if (mdt_is_sum_statfs_client(req->rq_export) &&
442 lustre_packed_msg_size(req->rq_reqmsg) ==
443 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
444 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
445 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
446 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
449 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
450 msf = &mdt->mdt_sum_osfs;
452 msf = &mdt->mdt_osfs;
454 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
455 /** statfs data is too old, get up-to-date one */
456 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
457 rc = next->md_ops->mdo_statfs(info->mti_env,
460 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
464 spin_lock(&mdt->mdt_lock);
465 msf->msf_osfs = *osfs;
466 msf->msf_age = ktime_get_seconds();
467 spin_unlock(&mdt->mdt_lock);
469 /** use cached statfs data */
470 spin_lock(&mdt->mdt_lock);
471 *osfs = msf->msf_osfs;
472 spin_unlock(&mdt->mdt_lock);
475 /* at least try to account for cached pages. its still racy and
476 * might be under-reporting if clients haven't announced their
477 * caches with brw recently */
478 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
479 " pending %llu free %llu avail %llu\n",
480 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
481 tgd->tgd_tot_pending,
482 osfs->os_bfree << tgd->tgd_blockbits,
483 osfs->os_bavail << tgd->tgd_blockbits);
485 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
486 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
487 osfs->os_bsize - 1) >> tgd->tgd_blockbits));
489 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
490 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
491 "%llu objects: %llu free; state %x\n",
492 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
493 osfs->os_files, osfs->os_ffree, osfs->os_state);
495 if (!exp_grant_param_supp(tsi->tsi_exp) &&
496 tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
497 /* clients which don't support OBD_CONNECT_GRANT_PARAM
498 * should not see a block size > page size, otherwise
499 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
500 * block size which is the biggest block size known to work
501 * with all client's page size. */
502 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
503 osfs->os_bfree <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
504 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
505 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
508 mdt_counter_incr(req, LPROC_MDT_STATFS,
509 ktime_us_delta(ktime_get(), kstart));
511 mdt_thread_info_fini(info);
515 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
517 struct lov_comp_md_v1 *comp_v1;
518 struct lov_mds_md *v1;
520 __u32 dom_stripesize = 0;
522 bool has_ost_stripes = false;
529 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
532 comp_v1 = (struct lov_comp_md_v1 *)lmm;
533 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
534 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
536 /* Fast check for DoM entry with no mirroring, should be the first */
537 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
538 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
541 /* check all entries otherwise */
542 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
543 struct lov_comp_md_entry_v1 *lcme;
545 lcme = &comp_v1->lcm_entries[i];
546 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
549 off = le32_to_cpu(lcme->lcme_offset);
550 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
552 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
554 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
556 has_ost_stripes = true;
558 if (dom_stripesize && has_ost_stripes)
559 RETURN(dom_stripesize);
561 /* DoM-only case exits here */
562 if (is_dom_only && dom_stripesize)
564 RETURN(dom_stripesize);
568 * Pack size attributes into the reply.
570 int mdt_pack_size2body(struct mdt_thread_info *info,
571 const struct lu_fid *fid, struct lustre_handle *lh)
574 struct md_attr *ma = &info->mti_attr;
576 bool dom_lock = false;
580 LASSERT(ma->ma_attr.la_valid & LA_MODE);
582 if (!S_ISREG(ma->ma_attr.la_mode) ||
583 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
586 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
587 /* no DoM stripe, no size in reply */
591 if (lustre_handle_is_used(lh)) {
592 struct ldlm_lock *lock;
594 lock = ldlm_handle2lock(lh);
596 dom_lock = ldlm_has_dom(lock);
601 /* no DoM lock, no size in reply */
605 /* Either DoM lock exists or LMM has only DoM stripe then
606 * return size on body. */
607 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
609 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
613 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
615 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
617 * \param info thread info object
618 * \param repbody reply to pack ACLs into
619 * \param o mdt object of file to examine
620 * \param nodemap nodemap of client to reply to
622 * \retval -errno error getting or parsing ACL from disk
624 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
625 struct mdt_object *o, struct lu_nodemap *nodemap)
627 const struct lu_env *env = info->mti_env;
628 struct md_object *next = mdt_object_child(o);
629 struct lu_buf *buf = &info->mti_buf;
630 struct mdt_device *mdt = info->mti_mdt;
631 struct req_capsule *pill = info->mti_pill;
636 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
637 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
638 if (buf->lb_len == 0)
642 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
644 if (rc == -ENODATA) {
645 repbody->mbo_aclsize = 0;
646 repbody->mbo_valid |= OBD_MD_FLACL;
648 } else if (rc == -EOPNOTSUPP) {
652 exp_connect_large_acl(info->mti_exp) &&
653 buf->lb_buf != info->mti_big_acl) {
654 if (info->mti_big_acl == NULL) {
655 info->mti_big_aclsize =
657 mdt->mdt_max_ea_size,
659 OBD_ALLOC_LARGE(info->mti_big_acl,
660 info->mti_big_aclsize);
661 if (info->mti_big_acl == NULL) {
662 info->mti_big_aclsize = 0;
663 CERROR("%s: unable to grow "
666 PFID(mdt_object_fid(o)));
671 CDEBUG(D_INODE, "%s: grow the "DFID
672 " ACL buffer to size %d\n",
674 PFID(mdt_object_fid(o)),
675 info->mti_big_aclsize);
677 buf->lb_buf = info->mti_big_acl;
678 buf->lb_len = info->mti_big_aclsize;
683 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
684 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
693 acl_buflen = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
694 if (acl_buflen >= rc)
697 /* If LOV/LMA EA is small, we can reuse part of their buffer */
698 client = ptlrpc_req_get_repsize(pill->rc_req);
699 server = lustre_packed_msg_size(pill->rc_req->rq_repmsg);
700 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
701 lmm_buflen = req_capsule_get_size(pill, &RMF_MDT_MD,
703 lmmsize = repbody->mbo_eadatasize;
706 if (client < server - acl_buflen - lmm_buflen + rc + lmmsize) {
707 CDEBUG(D_INODE, "%s: client prepared buffer size %d "
708 "is not big enough with the ACL size %d (%d)\n",
709 mdt_obd_name(mdt), client, rc,
710 server - acl_buflen - lmm_buflen + rc + lmmsize);
711 repbody->mbo_aclsize = 0;
712 repbody->mbo_valid &= ~OBD_MD_FLACL;
717 if (buf->lb_buf == info->mti_big_acl)
718 info->mti_big_acl_used = 1;
720 rc = nodemap_map_acl(nodemap, buf->lb_buf,
721 rc, NODEMAP_FS_TO_CLIENT);
722 /* if all ACLs mapped out, rc is still >= 0 */
724 CERROR("%s: nodemap_map_acl unable to parse "DFID
725 " ACL: rc = %d\n", mdt_obd_name(mdt),
726 PFID(mdt_object_fid(o)), rc);
727 repbody->mbo_aclsize = 0;
728 repbody->mbo_valid &= ~OBD_MD_FLACL;
730 repbody->mbo_aclsize = rc;
731 repbody->mbo_valid |= OBD_MD_FLACL;
740 /* XXX Look into layout in MDT layer. */
741 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
743 struct lov_comp_md_v1 *comp_v1;
744 struct lov_mds_md *v1;
747 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
748 comp_v1 = (struct lov_comp_md_v1 *)lmm;
750 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
751 v1 = (struct lov_mds_md *)((char *)comp_v1 +
752 comp_v1->lcm_entries[i].lcme_offset);
753 /* We don't support partial release for now */
754 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
759 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
764 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
765 const struct lu_attr *attr, const struct lu_fid *fid)
767 struct md_attr *ma = &info->mti_attr;
768 struct obd_export *exp = info->mti_exp;
769 struct lu_nodemap *nodemap = NULL;
771 LASSERT(ma->ma_valid & MA_INODE);
773 if (attr->la_valid & LA_ATIME) {
774 b->mbo_atime = attr->la_atime;
775 b->mbo_valid |= OBD_MD_FLATIME;
777 if (attr->la_valid & LA_MTIME) {
778 b->mbo_mtime = attr->la_mtime;
779 b->mbo_valid |= OBD_MD_FLMTIME;
781 if (attr->la_valid & LA_CTIME) {
782 b->mbo_ctime = attr->la_ctime;
783 b->mbo_valid |= OBD_MD_FLCTIME;
785 if (attr->la_valid & LA_BTIME) {
786 b->mbo_btime = attr->la_btime;
787 b->mbo_valid |= OBD_MD_FLBTIME;
789 if (attr->la_valid & LA_FLAGS) {
790 b->mbo_flags = attr->la_flags;
791 b->mbo_valid |= OBD_MD_FLFLAGS;
793 if (attr->la_valid & LA_NLINK) {
794 b->mbo_nlink = attr->la_nlink;
795 b->mbo_valid |= OBD_MD_FLNLINK;
797 if (attr->la_valid & (LA_UID|LA_GID)) {
798 nodemap = nodemap_get_from_exp(exp);
802 if (attr->la_valid & LA_UID) {
803 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
804 NODEMAP_FS_TO_CLIENT,
806 b->mbo_valid |= OBD_MD_FLUID;
808 if (attr->la_valid & LA_GID) {
809 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
810 NODEMAP_FS_TO_CLIENT,
812 b->mbo_valid |= OBD_MD_FLGID;
815 if (attr->la_valid & LA_PROJID) {
816 /* TODO, nodemap for project id */
817 b->mbo_projid = attr->la_projid;
818 b->mbo_valid |= OBD_MD_FLPROJID;
821 b->mbo_mode = attr->la_mode;
822 if (attr->la_valid & LA_MODE)
823 b->mbo_valid |= OBD_MD_FLMODE;
824 if (attr->la_valid & LA_TYPE)
825 b->mbo_valid |= OBD_MD_FLTYPE;
829 b->mbo_valid |= OBD_MD_FLID;
830 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
831 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
834 if (!(attr->la_valid & LA_TYPE))
837 b->mbo_rdev = attr->la_rdev;
838 b->mbo_size = attr->la_size;
839 b->mbo_blocks = attr->la_blocks;
841 if (!S_ISREG(attr->la_mode)) {
842 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
843 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
844 /* means no objects are allocated on osts. */
845 LASSERT(!(ma->ma_valid & MA_LOV));
846 /* just ignore blocks occupied by extend attributes on MDS */
848 /* if no object is allocated on osts, the size on mds is valid.
850 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
851 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
852 if (mdt_hsm_is_released(ma->ma_lmm)) {
853 /* A released file stores its size on MDS. */
854 /* But return 1 block for released file, unless tools
855 * like tar will consider it fully sparse. (LU-3864)
857 if (unlikely(b->mbo_size == 0))
861 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
862 } else if (info->mti_som_valid) { /* som is valid */
863 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
864 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
865 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
866 b->mbo_size = ma->ma_som.ms_size;
867 b->mbo_blocks = ma->ma_som.ms_blocks;
871 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
872 b->mbo_valid & OBD_MD_FLLAZYSIZE))
873 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
874 PFID(fid), (unsigned long long)b->mbo_size);
877 if (!IS_ERR_OR_NULL(nodemap))
878 nodemap_putref(nodemap);
881 static inline int mdt_body_has_lov(const struct lu_attr *la,
882 const struct mdt_body *body)
884 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
885 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
888 void mdt_client_compatibility(struct mdt_thread_info *info)
890 struct mdt_body *body;
891 struct ptlrpc_request *req = mdt_info_req(info);
892 struct obd_export *exp = req->rq_export;
893 struct md_attr *ma = &info->mti_attr;
894 struct lu_attr *la = &ma->ma_attr;
897 if (exp_connect_layout(exp))
898 /* the client can deal with 16-bit lmm_stripe_count */
901 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
903 if (!mdt_body_has_lov(la, body))
906 /* now we have a reply with a lov for a client not compatible with the
907 * layout lock so we have to clean the layout generation number */
908 if (S_ISREG(la->la_mode))
909 ma->ma_lmm->lmm_layout_gen = 0;
913 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
914 struct mdt_object *o)
916 const struct lu_env *env = info->mti_env;
919 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
928 /* Is it a directory? Let's check for the LMV as well */
929 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
930 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
934 rc2 = mo_xattr_get(env, mdt_object_child(o),
936 XATTR_NAME_DEFAULT_LMV);
938 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
946 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
949 const struct lu_env *env = info->mti_env;
953 LASSERT(info->mti_big_lmm_used == 0);
954 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
958 /* big_lmm may need to be grown */
959 if (info->mti_big_lmmsize < rc) {
960 int size = size_roundup_power2(rc);
962 if (info->mti_big_lmmsize > 0) {
963 /* free old buffer */
964 LASSERT(info->mti_big_lmm);
965 OBD_FREE_LARGE(info->mti_big_lmm,
966 info->mti_big_lmmsize);
967 info->mti_big_lmm = NULL;
968 info->mti_big_lmmsize = 0;
971 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
972 if (info->mti_big_lmm == NULL)
974 info->mti_big_lmmsize = size;
976 LASSERT(info->mti_big_lmmsize >= rc);
978 info->mti_buf.lb_buf = info->mti_big_lmm;
979 info->mti_buf.lb_len = info->mti_big_lmmsize;
980 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
985 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
986 struct md_attr *ma, const char *name)
988 struct md_object *next = mdt_object_child(o);
989 struct lu_buf *buf = &info->mti_buf;
992 if (strcmp(name, XATTR_NAME_LOV) == 0) {
993 buf->lb_buf = ma->ma_lmm;
994 buf->lb_len = ma->ma_lmm_size;
995 LASSERT(!(ma->ma_valid & MA_LOV));
996 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
997 buf->lb_buf = ma->ma_lmv;
998 buf->lb_len = ma->ma_lmv_size;
999 LASSERT(!(ma->ma_valid & MA_LMV));
1000 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1001 buf->lb_buf = ma->ma_default_lmv;
1002 buf->lb_len = ma->ma_default_lmv_size;
1003 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1008 LASSERT(buf->lb_buf);
1010 rc = mo_xattr_get(info->mti_env, next, buf, name);
1014 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1015 if (info->mti_big_lmm_used)
1016 ma->ma_lmm = info->mti_big_lmm;
1018 /* NOT return LOV EA with hole to old client. */
1019 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1020 LOV_PATTERN_F_HOLE) &&
1021 !(exp_connect_flags(info->mti_exp) &
1022 OBD_CONNECT_LFSCK)) {
1025 ma->ma_lmm_size = rc;
1026 ma->ma_valid |= MA_LOV;
1028 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1029 if (info->mti_big_lmm_used)
1030 ma->ma_lmv = info->mti_big_lmm;
1032 ma->ma_lmv_size = rc;
1033 ma->ma_valid |= MA_LMV;
1034 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1035 ma->ma_default_lmv_size = rc;
1036 ma->ma_valid |= MA_LMV_DEF;
1039 /* Update mdt_max_mdsize so all clients will be aware that */
1040 if (info->mti_mdt->mdt_max_mdsize < rc)
1041 info->mti_mdt->mdt_max_mdsize = rc;
1044 } else if (rc == -ENODATA) {
1047 } else if (rc == -ERANGE) {
1048 /* Default LMV has fixed size, so it must be able to fit
1049 * in the original buffer */
1050 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1052 rc = mdt_big_xattr_get(info, o, name);
1054 info->mti_big_lmm_used = 1;
1062 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1063 struct md_attr *ma, const char *name)
1067 if (!info->mti_big_lmm) {
1068 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1069 if (!info->mti_big_lmm)
1071 info->mti_big_lmmsize = PAGE_SIZE;
1074 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1075 ma->ma_lmm = info->mti_big_lmm;
1076 ma->ma_lmm_size = info->mti_big_lmmsize;
1077 ma->ma_valid &= ~MA_LOV;
1078 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1079 ma->ma_lmv = info->mti_big_lmm;
1080 ma->ma_lmv_size = info->mti_big_lmmsize;
1081 ma->ma_valid &= ~MA_LMV;
1086 LASSERT(!info->mti_big_lmm_used);
1087 rc = __mdt_stripe_get(info, o, ma, name);
1088 /* since big_lmm is always used here, clear 'used' flag to avoid
1089 * assertion in mdt_big_xattr_get().
1091 info->mti_big_lmm_used = 0;
1096 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1097 struct lu_fid *pfid)
1099 struct lu_buf *buf = &info->mti_buf;
1100 struct link_ea_header *leh;
1101 struct link_ea_entry *lee;
1105 buf->lb_buf = info->mti_big_lmm;
1106 buf->lb_len = info->mti_big_lmmsize;
1107 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1108 buf, XATTR_NAME_LINK);
1109 /* ignore errors, MA_PFID won't be set and it is
1110 * up to the caller to treat this as an error */
1111 if (rc == -ERANGE || buf->lb_len == 0) {
1112 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1113 buf->lb_buf = info->mti_big_lmm;
1114 buf->lb_len = info->mti_big_lmmsize;
1119 if (rc < sizeof(*leh)) {
1120 CERROR("short LinkEA on "DFID": rc = %d\n",
1121 PFID(mdt_object_fid(o)), rc);
1125 leh = (struct link_ea_header *) buf->lb_buf;
1126 lee = (struct link_ea_entry *)(leh + 1);
1127 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1128 leh->leh_magic = LINK_EA_MAGIC;
1129 leh->leh_reccount = __swab32(leh->leh_reccount);
1130 leh->leh_len = __swab64(leh->leh_len);
1132 if (leh->leh_magic != LINK_EA_MAGIC)
1134 if (leh->leh_reccount == 0)
1137 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1138 fid_be_to_cpu(pfid, pfid);
1143 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1144 struct lu_fid *pfid, struct lu_name *lname)
1146 struct lu_buf *buf = &info->mti_buf;
1147 struct link_ea_header *leh;
1148 struct link_ea_entry *lee;
1152 buf->lb_buf = info->mti_xattr_buf;
1153 buf->lb_len = sizeof(info->mti_xattr_buf);
1154 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1156 if (rc == -ERANGE) {
1157 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1158 buf->lb_buf = info->mti_big_lmm;
1159 buf->lb_len = info->mti_big_lmmsize;
1164 if (rc < sizeof(*leh)) {
1165 CERROR("short LinkEA on "DFID": rc = %d\n",
1166 PFID(mdt_object_fid(o)), rc);
1170 leh = (struct link_ea_header *)buf->lb_buf;
1171 lee = (struct link_ea_entry *)(leh + 1);
1172 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1173 leh->leh_magic = LINK_EA_MAGIC;
1174 leh->leh_reccount = __swab32(leh->leh_reccount);
1175 leh->leh_len = __swab64(leh->leh_len);
1177 if (leh->leh_magic != LINK_EA_MAGIC)
1180 if (leh->leh_reccount == 0)
1183 linkea_entry_unpack(lee, &reclen, lname, pfid);
1188 int mdt_attr_get_complex(struct mdt_thread_info *info,
1189 struct mdt_object *o, struct md_attr *ma)
1191 const struct lu_env *env = info->mti_env;
1192 struct md_object *next = mdt_object_child(o);
1193 struct lu_buf *buf = &info->mti_buf;
1194 int need = ma->ma_need;
1201 if (mdt_object_exists(o) == 0)
1202 GOTO(out, rc = -ENOENT);
1203 mode = lu_object_attr(&next->mo_lu);
1205 if (need & MA_INODE) {
1206 ma->ma_need = MA_INODE;
1207 rc = mo_attr_get(env, next, ma);
1212 (void) mdt_get_som(info, o, ma);
1213 ma->ma_valid |= MA_INODE;
1216 if (need & MA_PFID) {
1217 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1219 ma->ma_valid |= MA_PFID;
1220 /* ignore this error, parent fid is not mandatory */
1224 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1225 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1230 if (need & MA_LMV && S_ISDIR(mode)) {
1231 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1236 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1237 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1243 * In the handle of MA_INODE, we may already get the SOM attr.
1245 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1246 rc = mdt_get_som(info, o, ma);
1251 if (need & MA_HSM && S_ISREG(mode)) {
1252 buf->lb_buf = info->mti_xattr_buf;
1253 buf->lb_len = sizeof(info->mti_xattr_buf);
1254 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1255 sizeof(info->mti_xattr_buf));
1256 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1257 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1259 ma->ma_valid |= MA_HSM;
1260 else if (rc2 < 0 && rc2 != -ENODATA)
1261 GOTO(out, rc = rc2);
1264 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1265 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1266 buf->lb_buf = ma->ma_acl;
1267 buf->lb_len = ma->ma_acl_size;
1268 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1270 ma->ma_acl_size = rc2;
1271 ma->ma_valid |= MA_ACL_DEF;
1272 } else if (rc2 == -ENODATA) {
1274 ma->ma_acl_size = 0;
1276 GOTO(out, rc = rc2);
1281 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1282 rc, ma->ma_valid, ma->ma_lmm);
1286 static int mdt_getattr_internal(struct mdt_thread_info *info,
1287 struct mdt_object *o, int ma_need)
1289 struct mdt_device *mdt = info->mti_mdt;
1290 struct md_object *next = mdt_object_child(o);
1291 const struct mdt_body *reqbody = info->mti_body;
1292 struct ptlrpc_request *req = mdt_info_req(info);
1293 struct md_attr *ma = &info->mti_attr;
1294 struct lu_attr *la = &ma->ma_attr;
1295 struct req_capsule *pill = info->mti_pill;
1296 const struct lu_env *env = info->mti_env;
1297 struct mdt_body *repbody;
1298 struct lu_buf *buffer = &info->mti_buf;
1299 struct obd_export *exp = info->mti_exp;
1300 ktime_t kstart = ktime_get();
1305 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1306 RETURN(err_serious(-ENOMEM));
1308 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1312 if (mdt_object_remote(o)) {
1313 /* This object is located on remote node.*/
1314 /* Return -ENOTSUPP for old client */
1315 if (!mdt_is_dne_client(req->rq_export))
1316 GOTO(out, rc = -ENOTSUPP);
1318 repbody->mbo_fid1 = *mdt_object_fid(o);
1319 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1323 if (reqbody->mbo_eadatasize > 0) {
1324 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1325 if (buffer->lb_buf == NULL)
1326 GOTO(out, rc = -EPROTO);
1327 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1330 buffer->lb_buf = NULL;
1332 ma_need &= ~(MA_LOV | MA_LMV);
1333 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1334 mdt_obd_name(info->mti_mdt),
1335 req->rq_export->exp_client_uuid.uuid);
1338 /* from 2.12.58 intent_getattr pack default LMV in reply */
1339 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1340 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1341 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1342 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1344 ma->ma_lmv = buffer->lb_buf;
1345 ma->ma_lmv_size = buffer->lb_len;
1346 ma->ma_default_lmv = req_capsule_server_get(pill,
1347 &RMF_DEFAULT_MDT_MD);
1348 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1349 &RMF_DEFAULT_MDT_MD,
1351 ma->ma_need = MA_INODE;
1352 if (ma->ma_lmv_size > 0)
1353 ma->ma_need |= MA_LMV;
1354 if (ma->ma_default_lmv_size > 0)
1355 ma->ma_need |= MA_LMV_DEF;
1356 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1357 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1358 /* If it is dir and client require MEA, then we got MEA */
1359 /* Assumption: MDT_MD size is enough for lmv size. */
1360 ma->ma_lmv = buffer->lb_buf;
1361 ma->ma_lmv_size = buffer->lb_len;
1362 ma->ma_need = MA_INODE;
1363 if (ma->ma_lmv_size > 0) {
1364 if (reqbody->mbo_valid & OBD_MD_MEA) {
1365 ma->ma_need |= MA_LMV;
1366 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1367 ma->ma_need |= MA_LMV_DEF;
1368 ma->ma_default_lmv = buffer->lb_buf;
1370 ma->ma_default_lmv_size = buffer->lb_len;
1371 ma->ma_lmv_size = 0;
1375 ma->ma_lmm = buffer->lb_buf;
1376 ma->ma_lmm_size = buffer->lb_len;
1377 ma->ma_need = MA_INODE | MA_HSM;
1378 if (ma->ma_lmm_size > 0) {
1379 ma->ma_need |= MA_LOV;
1380 /* Older clients may crash if they getattr overstriped
1383 if (!exp_connect_overstriping(exp) &&
1384 mdt_lmm_is_overstriping(ma->ma_lmm))
1385 RETURN(-EOPNOTSUPP);
1389 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1390 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1391 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1392 /* get default stripe info for this dir. */
1393 ma->ma_need |= MA_LOV_DEF;
1395 ma->ma_need |= ma_need;
1397 rc = mdt_attr_get_complex(info, o, ma);
1399 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1400 "%s: getattr error for "DFID": rc = %d\n",
1401 mdt_obd_name(info->mti_mdt),
1402 PFID(mdt_object_fid(o)), rc);
1406 /* if file is released, check if a restore is running */
1407 if (ma->ma_valid & MA_HSM) {
1408 repbody->mbo_valid |= OBD_MD_TSTATE;
1409 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1410 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1411 repbody->mbo_t_state = MS_RESTORE;
1414 if (unlikely(!(ma->ma_valid & MA_INODE)))
1417 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1419 if (mdt_body_has_lov(la, reqbody)) {
1420 u32 stripe_count = 1;
1422 if (ma->ma_valid & MA_LOV) {
1423 LASSERT(ma->ma_lmm_size);
1424 repbody->mbo_eadatasize = ma->ma_lmm_size;
1425 if (S_ISDIR(la->la_mode))
1426 repbody->mbo_valid |= OBD_MD_FLDIREA;
1428 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1429 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1431 if (ma->ma_valid & MA_LMV) {
1432 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1433 u32 magic = le32_to_cpu(lmv->lmv_magic);
1435 /* Return -ENOTSUPP for old client */
1436 if (!mdt_is_striped_client(req->rq_export))
1439 LASSERT(S_ISDIR(la->la_mode));
1440 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1441 repbody->mbo_eadatasize = ma->ma_lmv_size;
1442 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1444 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1445 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1446 mdt_restripe_migrate_add(info, o);
1447 else if (magic == LMV_MAGIC_V1 &&
1448 lmv_is_restriping(lmv))
1449 mdt_restripe_update_add(info, o);
1451 if (ma->ma_valid & MA_LMV_DEF) {
1452 /* Return -ENOTSUPP for old client */
1453 if (!mdt_is_striped_client(req->rq_export))
1455 LASSERT(S_ISDIR(la->la_mode));
1457 * when ll_dir_getstripe() gets default LMV, it
1458 * checks mbo_eadatasize.
1460 if (!(ma->ma_valid & MA_LMV))
1461 repbody->mbo_eadatasize =
1462 ma->ma_default_lmv_size;
1463 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1464 OBD_MD_DEFAULT_MEA);
1467 "dirent count %llu stripe count %u MDT count %d\n",
1468 ma->ma_attr.la_dirent_count, stripe_count,
1469 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1470 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1471 ma->ma_attr.la_dirent_count >
1472 mdt->mdt_restriper.mdr_dir_split_count &&
1473 !fid_is_root(mdt_object_fid(o)) &&
1474 mdt->mdt_enable_dir_auto_split &&
1475 !o->mot_restriping &&
1476 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1477 mdt_auto_split_add(info, o);
1478 } else if (S_ISLNK(la->la_mode) &&
1479 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1480 buffer->lb_buf = ma->ma_lmm;
1481 /* eadatasize from client includes NULL-terminator, so
1482 * there is no need to read it */
1483 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1484 rc = mo_readlink(env, next, buffer);
1485 if (unlikely(rc <= 0)) {
1486 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1487 mdt_obd_name(info->mti_mdt),
1488 PFID(mdt_object_fid(o)), rc);
1491 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1493 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1495 repbody->mbo_valid |= OBD_MD_LINKNAME;
1496 /* we need to report back size with NULL-terminator
1497 * because client expects that */
1498 repbody->mbo_eadatasize = rc + 1;
1499 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1500 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1501 "on "DFID ", expected %d\n",
1502 mdt_obd_name(info->mti_mdt),
1503 rc, PFID(mdt_object_fid(o)),
1504 reqbody->mbo_eadatasize - 1);
1505 /* NULL terminate */
1506 ((char *)ma->ma_lmm)[rc] = 0;
1508 /* If the total CDEBUG() size is larger than a page, it
1509 * will print a warning to the console, avoid this by
1510 * printing just the last part of the symlink. */
1511 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1512 print_limit < rc ? "..." : "", print_limit,
1513 (char *)ma->ma_lmm + rc - print_limit, rc);
1518 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1519 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1520 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1521 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1522 repbody->mbo_max_mdsize);
1525 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1526 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1527 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1528 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1529 if (IS_ERR(nodemap))
1530 RETURN(PTR_ERR(nodemap));
1532 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1533 nodemap_putref(nodemap);
1539 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1540 ktime_us_delta(ktime_get(), kstart));
1545 static int mdt_getattr(struct tgt_session_info *tsi)
1547 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1548 struct mdt_object *obj = info->mti_object;
1549 struct req_capsule *pill = info->mti_pill;
1550 struct mdt_body *reqbody;
1551 struct mdt_body *repbody;
1555 if (unlikely(info->mti_object == NULL))
1558 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1560 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1562 /* Special case for Data-on-MDT files to get data version */
1563 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1564 rc = mdt_data_version_get(tsi);
1568 /* Unlike intent case where we need to pre-fill out buffers early on
1569 * in intent policy for ldlm reasons, here we can have a much better
1570 * guess at EA size by just reading it from disk.
1571 * Exceptions are readdir and (missing) directory striping */
1573 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1574 /* No easy way to know how long is the symlink, but it cannot
1575 * be more than PATH_MAX, so we allocate +1 */
1577 /* A special case for fs ROOT: getattr there might fetch
1578 * default EA for entire fs, not just for this dir!
1580 } else if (lu_fid_eq(mdt_object_fid(obj),
1581 &info->mti_mdt->mdt_md_root_fid) &&
1582 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1583 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1585 /* Should the default strping be bigger, mdt_fix_reply
1586 * will reallocate */
1587 rc = DEF_REP_MD_SIZE;
1589 /* Read the actual EA size from disk */
1590 rc = mdt_attr_get_eabuf_size(info, obj);
1594 GOTO(out, rc = err_serious(rc));
1596 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1598 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1599 * by default. If the target object has more ACL entries, then
1600 * enlarge the buffer when necessary. */
1601 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1602 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1604 rc = req_capsule_server_pack(pill);
1605 if (unlikely(rc != 0))
1606 GOTO(out, rc = err_serious(rc));
1608 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1609 LASSERT(repbody != NULL);
1610 repbody->mbo_eadatasize = 0;
1611 repbody->mbo_aclsize = 0;
1613 rc = mdt_check_ucred(info);
1615 GOTO(out_shrink, rc);
1617 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1619 rc = mdt_getattr_internal(info, obj, 0);
1622 mdt_client_compatibility(info);
1623 rc2 = mdt_fix_reply(info);
1627 mdt_thread_info_fini(info);
1632 * Handler of layout intent RPC requiring the layout modification
1634 * \param[in] info thread environment
1635 * \param[in] obj object
1636 * \param[out] lhc object ldlm lock handle
1637 * \param[in] layout layout change descriptor
1639 * \retval 0 on success
1640 * \retval < 0 error code
1642 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1643 struct mdt_lock_handle *lhc,
1644 struct md_layout_change *layout)
1650 if (!mdt_object_exists(obj))
1653 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1656 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1661 rc = mdt_check_resent_lock(info, obj, lhc);
1667 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1669 /* take layout lock to prepare layout change */
1670 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1671 lockpart |= MDS_INODELOCK_UPDATE;
1673 mdt_lock_handle_init(lhc);
1674 mdt_lock_reg_init(lhc, LCK_EX);
1675 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1680 mutex_lock(&obj->mot_som_mutex);
1681 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1682 mutex_unlock(&obj->mot_som_mutex);
1685 mdt_object_unlock(info, obj, lhc, 1);
1691 * Exchange MOF_LOV_CREATED flags between two objects after a
1692 * layout swap. No assumption is made on whether o1 or o2 have
1693 * created objects or not.
1695 * \param[in,out] o1 First swap layout object
1696 * \param[in,out] o2 Second swap layout object
1698 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1700 unsigned int o1_lov_created = o1->mot_lov_created;
1702 mutex_lock(&o1->mot_lov_mutex);
1703 mutex_lock(&o2->mot_lov_mutex);
1705 o1->mot_lov_created = o2->mot_lov_created;
1706 o2->mot_lov_created = o1_lov_created;
1708 mutex_unlock(&o2->mot_lov_mutex);
1709 mutex_unlock(&o1->mot_lov_mutex);
1712 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1714 struct mdt_thread_info *info;
1715 struct ptlrpc_request *req = tgt_ses_req(tsi);
1716 struct obd_export *exp = req->rq_export;
1717 struct mdt_object *o1, *o2, *o;
1718 struct mdt_lock_handle *lh1, *lh2;
1719 struct mdc_swap_layouts *msl;
1723 /* client does not support layout lock, so layout swaping
1725 * FIXME: there is a problem for old clients which don't support
1726 * layout lock yet. If those clients have already opened the file
1727 * they won't be notified at all so that old layout may still be
1728 * used to do IO. This can be fixed after file release is landed by
1729 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1730 if (!exp_connect_layout(exp))
1731 RETURN(-EOPNOTSUPP);
1733 info = tsi2mdt_info(tsi);
1734 if (unlikely(info->mti_object == NULL))
1737 if (info->mti_dlm_req != NULL)
1738 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1740 o1 = info->mti_object;
1741 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1742 &info->mti_body->mbo_fid2);
1744 GOTO(out, rc = PTR_ERR(o));
1746 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1747 GOTO(put, rc = -ENOENT);
1749 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1750 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1756 /* permission check. Make sure the calling process having permission
1757 * to write both files. */
1758 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1763 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1768 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1770 GOTO(put, rc = -EPROTO);
1772 lh1 = &info->mti_lh[MDT_LH_NEW];
1773 mdt_lock_reg_init(lh1, LCK_EX);
1774 lh2 = &info->mti_lh[MDT_LH_OLD];
1775 mdt_lock_reg_init(lh2, LCK_EX);
1777 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1778 MDS_INODELOCK_XATTR);
1782 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1783 MDS_INODELOCK_XATTR);
1787 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1788 mdt_object_child(o2), msl->msl_flags);
1792 mdt_swap_lov_flag(o1, o2);
1795 mdt_object_unlock(info, o2, lh2, rc);
1797 mdt_object_unlock(info, o1, lh1, rc);
1799 mdt_object_put(info->mti_env, o);
1801 mdt_thread_info_fini(info);
1805 static int mdt_raw_lookup(struct mdt_thread_info *info,
1806 struct mdt_object *parent,
1807 const struct lu_name *lname,
1808 struct ldlm_reply *ldlm_rep)
1810 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1814 LASSERT(!info->mti_cross_ref);
1816 /* Only got the fid of this obj by name */
1817 fid_zero(child_fid);
1818 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1819 lname, child_fid, &info->mti_spec);
1821 struct mdt_body *repbody;
1823 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1824 repbody->mbo_fid1 = *child_fid;
1825 repbody->mbo_valid = OBD_MD_FLID;
1826 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1827 } else if (rc == -ENOENT) {
1828 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1835 * UPDATE lock should be taken against parent, and be released before exit;
1836 * child_bits lock should be taken against child, and be returned back:
1837 * (1)normal request should release the child lock;
1838 * (2)intent request will grant the lock to client.
1840 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1841 struct mdt_lock_handle *lhc,
1843 struct ldlm_reply *ldlm_rep)
1845 struct ptlrpc_request *req = mdt_info_req(info);
1846 struct mdt_body *reqbody = NULL;
1847 struct mdt_object *parent = info->mti_object;
1848 struct mdt_object *child = NULL;
1849 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1850 struct lu_name *lname = NULL;
1851 struct mdt_lock_handle *lhp = NULL;
1852 struct ldlm_lock *lock;
1853 struct req_capsule *pill = info->mti_pill;
1861 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1862 LASSERT(ergo(is_resent,
1863 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1868 if (info->mti_cross_ref) {
1869 /* Only getattr on the child. Parent is on another node. */
1870 mdt_set_disposition(info, ldlm_rep,
1871 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1873 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1875 PFID(mdt_object_fid(child)), ldlm_rep);
1877 rc = mdt_check_resent_lock(info, child, lhc);
1880 } else if (rc > 0) {
1881 mdt_lock_handle_init(lhc);
1882 mdt_lock_reg_init(lhc, LCK_PR);
1885 * Object's name entry is on another MDS, it will
1886 * request PERM lock only because LOOKUP lock is owned
1887 * by the MDS where name entry resides.
1889 * TODO: it should try layout lock too. - Jinshan
1891 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1892 MDS_INODELOCK_LAYOUT);
1893 child_bits |= MDS_INODELOCK_PERM;
1895 rc = mdt_object_lock(info, child, lhc, child_bits);
1900 /* Finally, we can get attr for child. */
1901 if (!mdt_object_exists(child)) {
1902 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1904 "remote object doesn't exist.");
1905 mdt_object_unlock(info, child, lhc, 1);
1909 rc = mdt_getattr_internal(info, child, 0);
1910 if (unlikely(rc != 0)) {
1911 mdt_object_unlock(info, child, lhc, 1);
1915 rc = mdt_pack_secctx_in_reply(info, child);
1917 mdt_object_unlock(info, child, lhc, 1);
1921 lname = &info->mti_name;
1922 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1924 if (lu_name_is_valid(lname)) {
1925 if (mdt_object_remote(parent)) {
1926 CERROR("%s: parent "DFID" is on remote target\n",
1927 mdt_obd_name(info->mti_mdt),
1928 PFID(mdt_object_fid(parent)));
1932 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1933 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1934 PNAME(lname), ldlm_rep);
1936 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1937 if (unlikely(reqbody == NULL))
1938 RETURN(err_serious(-EPROTO));
1940 *child_fid = reqbody->mbo_fid2;
1941 if (unlikely(!fid_is_sane(child_fid)))
1942 RETURN(err_serious(-EINVAL));
1944 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1945 mdt_object_get(info->mti_env, parent);
1948 child = mdt_object_find(info->mti_env, info->mti_mdt,
1951 RETURN(PTR_ERR(child));
1954 if (mdt_object_remote(child)) {
1955 CERROR("%s: child "DFID" is on remote target\n",
1956 mdt_obd_name(info->mti_mdt),
1957 PFID(mdt_object_fid(child)));
1958 GOTO(out_child, rc = -EPROTO);
1961 /* don't fetch LOOKUP lock if it's remote object */
1962 rc = mdt_is_remote_object(info, parent, child);
1964 GOTO(out_child, rc);
1966 child_bits &= ~MDS_INODELOCK_LOOKUP;
1968 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1970 PFID(mdt_object_fid(parent)),
1971 PFID(&reqbody->mbo_fid2), ldlm_rep);
1974 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1976 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1977 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1979 "Parent doesn't exist!");
1980 GOTO(out_child, rc = -ESTALE);
1983 if (lu_name_is_valid(lname)) {
1984 /* Always allow to lookup ".." */
1985 if (unlikely(lname->ln_namelen == 2 &&
1986 lname->ln_name[0] == '.' &&
1987 lname->ln_name[1] == '.'))
1988 info->mti_spec.sp_permitted = 1;
1990 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1991 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1996 /* step 1: lock parent only if parent is a directory */
1997 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1998 lhp = &info->mti_lh[MDT_LH_PARENT];
1999 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2000 rc = mdt_object_lock(info, parent, lhp,
2001 MDS_INODELOCK_UPDATE);
2002 if (unlikely(rc != 0))
2006 /* step 2: lookup child's fid by name */
2007 fid_zero(child_fid);
2008 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2009 child_fid, &info->mti_spec);
2011 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2014 GOTO(unlock_parent, rc);
2016 child = mdt_object_find(info->mti_env, info->mti_mdt,
2018 if (unlikely(IS_ERR(child)))
2019 GOTO(unlock_parent, rc = PTR_ERR(child));
2022 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2024 /* step 3: lock child regardless if it is local or remote. */
2027 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2028 if (!mdt_object_exists(child)) {
2029 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2031 "Object doesn't exist!");
2032 GOTO(out_child, rc = -ENOENT);
2035 rc = mdt_check_resent_lock(info, child, lhc);
2037 GOTO(out_child, rc);
2038 } else if (rc > 0) {
2039 mdt_lock_handle_init(lhc);
2040 mdt_lock_reg_init(lhc, LCK_PR);
2042 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2043 !mdt_object_remote(child)) {
2044 struct md_attr *ma = &info->mti_attr;
2047 ma->ma_need = MA_INODE;
2048 rc = mdt_attr_get_complex(info, child, ma);
2049 if (unlikely(rc != 0))
2050 GOTO(out_child, rc);
2052 /* If the file has not been changed for some time, we
2053 * return not only a LOOKUP lock, but also an UPDATE
2054 * lock and this might save us RPC on later STAT. For
2055 * directories, it also let negative dentry cache start
2056 * working for this dir. */
2057 if (ma->ma_valid & MA_INODE &&
2058 ma->ma_attr.la_valid & LA_CTIME &&
2059 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2060 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2061 child_bits |= MDS_INODELOCK_UPDATE;
2064 /* layout lock must be granted in a best-effort way
2065 * for IT operations */
2066 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2067 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2068 !mdt_object_remote(child) && ldlm_rep != NULL) {
2069 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2070 exp_connect_layout(info->mti_exp)) {
2071 /* try to grant layout lock for regular file. */
2072 try_bits = MDS_INODELOCK_LAYOUT;
2074 /* Acquire DOM lock in advance for data-on-mdt file */
2075 if (child != parent)
2076 try_bits |= MDS_INODELOCK_DOM;
2079 if (try_bits != 0) {
2080 /* try layout lock, it may fail to be granted due to
2081 * contention at LOOKUP or UPDATE */
2082 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2084 if (child_bits & MDS_INODELOCK_LAYOUT)
2087 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2088 * client will enqueue the lock to the remote MDT */
2089 if (mdt_object_remote(child))
2090 child_bits &= ~MDS_INODELOCK_UPDATE;
2091 rc = mdt_object_lock(info, child, lhc, child_bits);
2093 if (unlikely(rc != 0))
2094 GOTO(out_child, rc);
2097 /* finally, we can get attr for child. */
2098 rc = mdt_getattr_internal(info, child, ma_need);
2099 if (unlikely(rc != 0)) {
2100 mdt_object_unlock(info, child, lhc, 1);
2101 GOTO(out_child, rc);
2104 rc = mdt_pack_secctx_in_reply(info, child);
2106 mdt_object_unlock(info, child, lhc, 1);
2107 GOTO(out_child, rc);
2110 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2112 /* Debugging code. */
2113 LDLM_DEBUG(lock, "Returning lock to client");
2114 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2115 &lock->l_resource->lr_name),
2116 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2117 PLDLMRES(lock->l_resource),
2118 PFID(mdt_object_fid(child)));
2120 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2121 !mdt_object_remote(child) && child != parent) {
2122 mdt_object_put(info->mti_env, child);
2123 rc = mdt_pack_size2body(info, child_fid,
2125 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2126 /* DOM lock was taken in advance but this is
2127 * not DoM file. Drop the lock.
2129 lock_res_and_lock(lock);
2130 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2131 unlock_res_and_lock(lock);
2133 LDLM_LOCK_PUT(lock);
2134 GOTO(unlock_parent, rc = 0);
2136 LDLM_LOCK_PUT(lock);
2142 mdt_object_put(info->mti_env, child);
2145 mdt_object_unlock(info, parent, lhp, 1);
2149 /* normal handler: should release the child lock */
2150 static int mdt_getattr_name(struct tgt_session_info *tsi)
2152 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2153 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2154 struct mdt_body *reqbody;
2155 struct mdt_body *repbody;
2159 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2160 LASSERT(reqbody != NULL);
2161 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2162 LASSERT(repbody != NULL);
2164 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2165 repbody->mbo_eadatasize = 0;
2166 repbody->mbo_aclsize = 0;
2168 rc = mdt_init_ucred_intent_getattr(info, reqbody);
2170 GOTO(out_shrink, rc);
2172 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2173 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2174 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2175 lhc->mlh_reg_lh.cookie = 0;
2177 mdt_exit_ucred(info);
2180 mdt_client_compatibility(info);
2181 rc2 = mdt_fix_reply(info);
2184 mdt_thread_info_fini(info);
2188 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2189 const struct lu_fid *pfid,
2190 const struct lu_name *name,
2191 struct mdt_object *obj, s64 ctime)
2193 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2194 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2195 struct mdt_device *mdt = info->mti_mdt;
2196 struct md_attr *ma = &info->mti_attr;
2197 struct mdt_lock_handle *parent_lh;
2198 struct mdt_lock_handle *child_lh;
2199 struct mdt_object *pobj;
2200 bool cos_incompat = false;
2204 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2206 GOTO(out, rc = PTR_ERR(pobj));
2208 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2209 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2210 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2212 GOTO(put_parent, rc);
2214 if (mdt_object_remote(pobj))
2215 cos_incompat = true;
2217 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2218 name, child_fid, &info->mti_spec);
2220 GOTO(unlock_parent, rc);
2222 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2223 GOTO(unlock_parent, rc = -EREMCHG);
2225 child_lh = &info->mti_lh[MDT_LH_CHILD];
2226 mdt_lock_reg_init(child_lh, LCK_EX);
2227 rc = mdt_reint_striped_lock(info, obj, child_lh,
2228 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2229 einfo, cos_incompat);
2231 GOTO(unlock_parent, rc);
2233 if (atomic_read(&obj->mot_open_count)) {
2234 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2235 PFID(mdt_object_fid(obj)));
2236 GOTO(unlock_child, rc = -EBUSY);
2240 ma->ma_valid = MA_INODE;
2241 ma->ma_attr.la_valid = LA_CTIME;
2242 ma->ma_attr.la_ctime = ctime;
2244 mutex_lock(&obj->mot_lov_mutex);
2246 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2247 mdt_object_child(obj), name, ma, 0);
2249 mutex_unlock(&obj->mot_lov_mutex);
2252 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2254 mdt_object_unlock(info, pobj, parent_lh, 1);
2256 mdt_object_put(info->mti_env, pobj);
2261 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2262 struct mdt_object *obj)
2264 struct lu_ucred *uc = lu_ucred(info->mti_env);
2265 struct md_attr *ma = &info->mti_attr;
2266 struct lu_attr *la = &ma->ma_attr;
2270 ma->ma_need = MA_INODE;
2271 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2275 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2278 if (md_capable(uc, CFS_CAP_DAC_OVERRIDE))
2280 if (uc->uc_fsuid == la->la_uid) {
2281 if ((la->la_mode & S_IWUSR) == 0)
2283 } else if (uc->uc_fsgid == la->la_gid) {
2284 if ((la->la_mode & S_IWGRP) == 0)
2286 } else if ((la->la_mode & S_IWOTH) == 0) {
2294 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2297 struct mdt_device *mdt = info->mti_mdt;
2298 struct mdt_object *obj = NULL;
2299 struct linkea_data ldata = { NULL };
2300 struct lu_buf *buf = &info->mti_big_buf;
2301 struct lu_name *name = &info->mti_name;
2302 struct lu_fid *pfid = &info->mti_tmp_fid1;
2303 struct link_ea_header *leh;
2304 struct link_ea_entry *lee;
2305 int reclen, count, rc = 0;
2308 if (!fid_is_sane(fid))
2309 GOTO(out, rc = -EINVAL);
2311 if (!fid_is_namespace_visible(fid))
2312 GOTO(out, rc = -EINVAL);
2314 obj = mdt_object_find(info->mti_env, mdt, fid);
2316 GOTO(out, rc = PTR_ERR(obj));
2318 if (mdt_object_remote(obj))
2319 GOTO(out, rc = -EREMOTE);
2320 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2321 GOTO(out, rc = -ENOENT);
2323 rc = mdt_rmfid_check_permission(info, obj);
2328 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2330 GOTO(out, rc = -ENOMEM);
2333 rc = mdt_links_read(info, obj, &ldata);
2338 lee = (struct link_ea_entry *)(leh + 1);
2339 for (count = 0; count < leh->leh_reccount; count++) {
2340 /* remove every hardlink */
2341 linkea_entry_unpack(lee, &reclen, name, pfid);
2342 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2343 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2349 if (obj && !IS_ERR(obj))
2350 mdt_object_put(info->mti_env, obj);
2351 if (info->mti_big_buf.lb_buf)
2352 lu_buf_free(&info->mti_big_buf);
2357 static int mdt_rmfid(struct tgt_session_info *tsi)
2359 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2360 struct mdt_body *reqbody;
2361 struct lu_fid *fids, *rfids;
2367 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2368 if (reqbody == NULL)
2370 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2372 nr = bufsize / sizeof(struct lu_fid);
2373 if (nr * sizeof(struct lu_fid) != bufsize)
2375 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2376 RCL_SERVER, nr * sizeof(__u32));
2377 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2378 RCL_SERVER, nr * sizeof(struct lu_fid));
2379 rc = req_capsule_server_pack(tsi->tsi_pill);
2381 GOTO(out, rc = err_serious(rc));
2382 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2385 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2387 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2390 mdt_init_ucred(mti, reqbody);
2391 for (i = 0; i < nr; i++) {
2393 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2395 mdt_exit_ucred(mti);
2401 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2402 void *karg, void __user *uarg);
2404 static int mdt_set_info(struct tgt_session_info *tsi)
2406 struct ptlrpc_request *req = tgt_ses_req(tsi);
2409 int keylen, vallen, rc = 0;
2413 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2415 DEBUG_REQ(D_HA, req, "no set_info key");
2416 RETURN(err_serious(-EFAULT));
2419 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2422 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2424 DEBUG_REQ(D_HA, req, "no set_info val");
2425 RETURN(err_serious(-EFAULT));
2428 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2431 /* Swab any part of val you need to here */
2432 if (KEY_IS(KEY_READ_ONLY)) {
2433 spin_lock(&req->rq_export->exp_lock);
2435 *exp_connect_flags_ptr(req->rq_export) |=
2438 *exp_connect_flags_ptr(req->rq_export) &=
2439 ~OBD_CONNECT_RDONLY;
2440 spin_unlock(&req->rq_export->exp_lock);
2441 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2442 struct changelog_setinfo *cs = val;
2444 if (vallen != sizeof(*cs)) {
2445 CERROR("%s: bad changelog_clear setinfo size %d\n",
2446 tgt_name(tsi->tsi_tgt), vallen);
2449 if (ptlrpc_req_need_swab(req)) {
2450 __swab64s(&cs->cs_recno);
2451 __swab32s(&cs->cs_id);
2454 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2456 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2458 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2460 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2467 static int mdt_readpage(struct tgt_session_info *tsi)
2469 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2470 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2471 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2472 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2473 struct mdt_body *repbody;
2479 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2480 RETURN(err_serious(-ENOMEM));
2482 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2483 if (repbody == NULL || reqbody == NULL)
2484 RETURN(err_serious(-EFAULT));
2487 * prepare @rdpg before calling lower layers and transfer itself. Here
2488 * reqbody->size contains offset of where to start to read and
2489 * reqbody->nlink contains number bytes to read.
2491 rdpg->rp_hash = reqbody->mbo_size;
2492 if (rdpg->rp_hash != reqbody->mbo_size) {
2493 CERROR("Invalid hash: %#llx != %#llx\n",
2494 rdpg->rp_hash, reqbody->mbo_size);
2498 rdpg->rp_attrs = reqbody->mbo_mode;
2499 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2500 rdpg->rp_attrs |= LUDA_64BITHASH;
2501 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2502 exp_max_brw_size(tsi->tsi_exp));
2503 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2505 OBD_ALLOC_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2506 if (rdpg->rp_pages == NULL)
2509 for (i = 0; i < rdpg->rp_npages; ++i) {
2510 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2511 if (rdpg->rp_pages[i] == NULL)
2512 GOTO(free_rdpg, rc = -ENOMEM);
2515 /* call lower layers to fill allocated pages with directory data */
2516 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2518 GOTO(free_rdpg, rc);
2520 /* send pages to client */
2521 rc = tgt_sendpage(tsi, rdpg, rc);
2526 for (i = 0; i < rdpg->rp_npages; i++)
2527 if (rdpg->rp_pages[i] != NULL)
2528 __free_page(rdpg->rp_pages[i]);
2529 OBD_FREE_PTR_ARRAY(rdpg->rp_pages, rdpg->rp_npages);
2531 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2537 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2539 struct lu_ucred *uc = mdt_ucred_check(info);
2540 struct lu_attr *attr = &info->mti_attr.ma_attr;
2545 if (op != REINT_SETATTR) {
2546 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2547 attr->la_uid = uc->uc_fsuid;
2548 /* for S_ISGID, inherit gid from his parent, such work will be
2549 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2550 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2551 attr->la_gid = uc->uc_fsgid;
2557 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2559 return op == REINT_OPEN &&
2560 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2563 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2565 struct req_capsule *pill = info->mti_pill;
2567 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2569 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2571 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2573 /* pre-set size in server part with max size */
2574 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2576 info->mti_mdt->mdt_max_ea_size);
2578 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2585 static int mdt_reint_internal(struct mdt_thread_info *info,
2586 struct mdt_lock_handle *lhc,
2589 struct req_capsule *pill = info->mti_pill;
2590 struct mdt_body *repbody;
2595 rc = mdt_reint_unpack(info, op);
2597 CERROR("Can't unpack reint, rc %d\n", rc);
2598 RETURN(err_serious(rc));
2602 /* check if the file system is set to readonly. O_RDONLY open
2603 * is still allowed even the file system is set to readonly mode */
2604 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2605 RETURN(err_serious(-EROFS));
2607 /* for replay (no_create) lmm is not needed, client has it already */
2608 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2609 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2612 /* llog cookies are always 0, the field is kept for compatibility */
2613 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2614 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2616 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2617 * by default. If the target object has more ACL entries, then
2618 * enlarge the buffer when necessary. */
2619 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2620 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2621 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2623 mdt_preset_secctx_size(info);
2625 rc = req_capsule_server_pack(pill);
2627 CERROR("Can't pack response, rc %d\n", rc);
2628 RETURN(err_serious(rc));
2631 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2632 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2634 repbody->mbo_eadatasize = 0;
2635 repbody->mbo_aclsize = 0;
2638 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2640 /* for replay no cookkie / lmm need, because client have this already */
2641 if (info->mti_spec.no_create)
2642 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2643 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2645 rc = mdt_init_ucred_reint(info);
2647 GOTO(out_shrink, rc);
2649 rc = mdt_fix_attr_ucred(info, op);
2651 GOTO(out_ucred, rc = err_serious(rc));
2653 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2655 GOTO(out_ucred, rc);
2656 } else if (rc == 1) {
2657 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2658 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2659 GOTO(out_ucred, rc);
2661 rc = mdt_reint_rec(info, lhc);
2664 mdt_exit_ucred(info);
2666 mdt_client_compatibility(info);
2668 rc2 = mdt_fix_reply(info);
2673 * Data-on-MDT optimization - read data along with OPEN and return it
2674 * in reply when possible.
2676 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2677 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2683 static long mdt_reint_opcode(struct ptlrpc_request *req,
2684 const struct req_format **fmt)
2686 struct mdt_device *mdt;
2687 struct mdt_rec_reint *rec;
2690 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2692 opc = rec->rr_opcode;
2693 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2694 if (opc < REINT_MAX && fmt[opc] != NULL)
2695 req_capsule_extend(&req->rq_pill, fmt[opc]);
2697 mdt = mdt_exp2dev(req->rq_export);
2698 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2699 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2700 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2701 opc = err_serious(-EFAULT);
2704 opc = err_serious(-EFAULT);
2709 static int mdt_reint(struct tgt_session_info *tsi)
2713 static const struct req_format *reint_fmts[REINT_MAX] = {
2714 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2715 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2716 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2717 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2718 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2719 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2720 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2721 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2722 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
2723 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2728 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2730 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2732 * No lock possible here from client to pass it to reint code
2735 rc = mdt_reint_internal(info, NULL, opc);
2736 mdt_thread_info_fini(info);
2741 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2745 /* this should sync the whole device */
2746 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2748 struct dt_device *dt = mdt->mdt_bottom;
2752 rc = dt->dd_ops->dt_sync(env, dt);
2756 /* this should sync this object */
2757 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2758 struct mdt_object *mo)
2764 if (!mdt_object_exists(mo)) {
2765 CWARN("%s: non existing object "DFID": rc = %d\n",
2766 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2771 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2772 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2773 dt_obj_version_t version;
2775 version = dt_version_get(env, mdt_obj2dt(mo));
2776 if (version > tgt->lut_obd->obd_last_committed)
2777 rc = mo_object_sync(env, mdt_object_child(mo));
2779 rc = mo_object_sync(env, mdt_object_child(mo));
2785 static int mdt_sync(struct tgt_session_info *tsi)
2787 struct ptlrpc_request *req = tgt_ses_req(tsi);
2788 struct req_capsule *pill = tsi->tsi_pill;
2789 struct mdt_body *body;
2790 ktime_t kstart = ktime_get();
2795 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2796 RETURN(err_serious(-ENOMEM));
2798 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2799 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2801 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2803 if (unlikely(info->mti_object == NULL))
2806 /* sync an object */
2807 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2810 const struct lu_fid *fid;
2811 struct lu_attr *la = &info->mti_attr.ma_attr;
2813 info->mti_attr.ma_need = MA_INODE;
2814 info->mti_attr.ma_valid = 0;
2815 rc = mdt_attr_get_complex(info, info->mti_object,
2818 body = req_capsule_server_get(pill,
2820 fid = mdt_object_fid(info->mti_object);
2821 mdt_pack_attr2body(info, body, la, fid);
2824 mdt_thread_info_fini(info);
2827 mdt_counter_incr(req, LPROC_MDT_SYNC,
2828 ktime_us_delta(ktime_get(), kstart));
2833 static int mdt_data_sync(struct tgt_session_info *tsi)
2835 struct mdt_thread_info *info;
2836 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2837 struct ost_body *body = tsi->tsi_ost_body;
2838 struct ost_body *repbody;
2839 struct mdt_object *mo = NULL;
2845 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2847 /* if no fid is specified then do nothing,
2848 * device sync is done via MDS_SYNC */
2849 if (fid_is_zero(&tsi->tsi_fid))
2852 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2854 RETURN(PTR_ERR(mo));
2856 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2860 repbody->oa.o_oi = body->oa.o_oi;
2861 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2863 info = tsi2mdt_info(tsi);
2864 ma = &info->mti_attr;
2865 ma->ma_need = MA_INODE;
2867 rc = mdt_attr_get_complex(info, mo, ma);
2869 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2872 mdt_thread_info_fini(info);
2877 mdt_object_put(tsi->tsi_env, mo);
2882 * Handle quota control requests to consult current usage/limit, but also
2883 * to configure quota enforcement
2885 static int mdt_quotactl(struct tgt_session_info *tsi)
2887 struct obd_export *exp = tsi->tsi_exp;
2888 struct req_capsule *pill = tsi->tsi_pill;
2889 struct obd_quotactl *oqctl, *repoqc;
2891 struct mdt_device *mdt = mdt_exp2dev(exp);
2892 struct lu_device *qmt = mdt->mdt_qmt_dev;
2893 struct lu_nodemap *nodemap;
2896 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2898 RETURN(err_serious(-EPROTO));
2900 rc = req_capsule_server_pack(pill);
2902 RETURN(err_serious(rc));
2904 nodemap = nodemap_get_from_exp(exp);
2905 if (IS_ERR(nodemap))
2906 RETURN(PTR_ERR(nodemap));
2908 switch (oqctl->qc_cmd) {
2909 /* master quotactl */
2912 case LUSTRE_Q_SETDEFAULT:
2913 case LUSTRE_Q_SETQUOTAPOOL:
2914 case LUSTRE_Q_SETINFOPOOL:
2915 if (!nodemap_can_setquota(nodemap))
2916 GOTO(out_nodemap, rc = -EPERM);
2920 case LUSTRE_Q_GETDEFAULT:
2921 case LUSTRE_Q_GETQUOTAPOOL:
2922 case LUSTRE_Q_GETINFOPOOL:
2924 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2925 /* slave quotactl */
2932 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2933 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2934 GOTO(out_nodemap, rc);
2938 switch (oqctl->qc_type) {
2940 id = nodemap_map_id(nodemap, NODEMAP_UID,
2941 NODEMAP_CLIENT_TO_FS, id);
2944 id = nodemap_map_id(nodemap, NODEMAP_GID,
2945 NODEMAP_CLIENT_TO_FS, id);
2948 /* todo: check/map project id */
2952 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2954 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2956 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2958 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2959 barrier_exit(tsi->tsi_tgt->lut_bottom);
2961 if (oqctl->qc_id != id)
2962 swap(oqctl->qc_id, id);
2964 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2965 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2966 RETURN(-EINPROGRESS);
2969 switch (oqctl->qc_cmd) {
2975 case LUSTRE_Q_SETDEFAULT:
2976 case LUSTRE_Q_GETDEFAULT:
2977 case LUSTRE_Q_SETQUOTAPOOL:
2978 case LUSTRE_Q_GETQUOTAPOOL:
2979 case LUSTRE_Q_SETINFOPOOL:
2980 case LUSTRE_Q_GETINFOPOOL:
2981 /* forward quotactl request to QMT */
2982 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2987 /* slave quotactl */
2988 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2993 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2994 GOTO(out_nodemap, rc = -EFAULT);
2997 if (oqctl->qc_id != id)
2998 swap(oqctl->qc_id, id);
3000 QCTL_COPY(repoqc, oqctl);
3004 nodemap_putref(nodemap);
3009 /** clone llog ctxt from child (mdd)
3010 * This allows remote llog (replicator) access.
3011 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3012 * context was originally set up, or we can handle them directly.
3013 * I choose the latter, but that means I need any llog
3014 * contexts set up by child to be accessable by the mdt. So we clone the
3015 * context into our context list here.
3017 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3020 struct md_device *next = mdt->mdt_child;
3021 struct llog_ctxt *ctxt;
3024 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3027 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3028 if (rc || ctxt == NULL) {
3032 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3034 CERROR("Can't set mdt ctxt %d\n", rc);
3039 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3040 struct mdt_device *mdt, int idx)
3042 struct llog_ctxt *ctxt;
3044 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3047 /* Put once for the get we just did, and once for the clone */
3048 llog_ctxt_put(ctxt);
3049 llog_ctxt_put(ctxt);
3054 * sec context handlers
3056 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3058 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3064 * quota request handlers
3066 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3068 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3069 struct lu_device *qmt = mdt->mdt_qmt_dev;
3074 RETURN(err_serious(-EOPNOTSUPP));
3076 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3080 struct mdt_object *mdt_object_new(const struct lu_env *env,
3081 struct mdt_device *d,
3082 const struct lu_fid *f)
3084 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3085 struct lu_object *o;
3086 struct mdt_object *m;
3089 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3090 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3091 if (unlikely(IS_ERR(o)))
3092 m = (struct mdt_object *)o;
3098 struct mdt_object *mdt_object_find(const struct lu_env *env,
3099 struct mdt_device *d,
3100 const struct lu_fid *f)
3102 struct lu_object *o;
3103 struct mdt_object *m;
3106 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3107 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3108 if (unlikely(IS_ERR(o)))
3109 m = (struct mdt_object *)o;
3117 * Asyncronous commit for mdt device.
3119 * Pass asynchonous commit call down the MDS stack.
3121 * \param env environment
3122 * \param mdt the mdt device
3124 static void mdt_device_commit_async(const struct lu_env *env,
3125 struct mdt_device *mdt)
3127 struct dt_device *dt = mdt->mdt_bottom;
3131 rc = dt->dd_ops->dt_commit_async(env, dt);
3132 if (unlikely(rc != 0))
3133 CWARN("%s: async commit start failed: rc = %d\n",
3134 mdt_obd_name(mdt), rc);
3135 atomic_inc(&mdt->mdt_async_commit_count);
3140 * Mark the lock as "synchonous".
3142 * Mark the lock to deffer transaction commit to the unlock time.
3144 * \param lock the lock to mark as "synchonous"
3146 * \see mdt_is_lock_sync
3147 * \see mdt_save_lock
3149 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3151 lock->l_ast_data = (void*)1;
3155 * Check whehter the lock "synchonous" or not.
3157 * \param lock the lock to check
3158 * \retval 1 the lock is "synchonous"
3159 * \retval 0 the lock isn't "synchronous"
3161 * \see mdt_set_lock_sync
3162 * \see mdt_save_lock
3164 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3166 return lock->l_ast_data != NULL;
3170 * Blocking AST for mdt locks.
3172 * Starts transaction commit if in case of COS lock conflict or
3173 * deffers such a commit to the mdt_save_lock.
3175 * \param lock the lock which blocks a request or cancelling lock
3176 * \param desc unused
3177 * \param data unused
3178 * \param flag indicates whether this cancelling or blocking callback
3180 * \see ldlm_blocking_ast_nocheck
3182 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3183 void *data, int flag)
3185 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3186 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3187 struct ldlm_cb_set_arg *arg = data;
3188 bool commit_async = false;
3192 if (flag == LDLM_CB_CANCELING)
3195 lock_res_and_lock(lock);
3196 if (lock->l_blocking_ast != mdt_blocking_ast) {
3197 unlock_res_and_lock(lock);
3201 /* A blocking ast may be sent from ldlm_lock_decref_internal
3202 * when the last reference to a local lock was released and
3203 * during blocking event from ldlm_work_bl_ast_lock().
3204 * The 'data' parameter is l_ast_data in the first case and
3205 * callback arguments in the second one. Distinguish them by that.
3207 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3208 goto skip_cos_checks;
3210 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3211 if (mdt_cos_is_enabled(mdt)) {
3212 if (!arg->bl_desc->bl_same_client)
3213 mdt_set_lock_sync(lock);
3214 } else if (mdt_slc_is_enabled(mdt) &&
3215 arg->bl_desc->bl_cos_incompat) {
3216 mdt_set_lock_sync(lock);
3218 * we may do extra commit here, but there is a small
3219 * window to miss a commit: lock was unlocked (saved),
3220 * then a conflict lock queued and we come here, but
3221 * REP-ACK not received, so lock was not converted to
3223 * Fortunately this window is quite small, so the
3224 * extra commit should be rare (not to say distributed
3225 * operation is rare too).
3227 commit_async = true;
3229 } else if (lock->l_req_mode == LCK_COS) {
3230 commit_async = true;
3234 rc = ldlm_blocking_ast_nocheck(lock);
3239 rc = lu_env_init(&env, LCT_LOCAL);
3240 if (unlikely(rc != 0))
3241 CWARN("%s: lu_env initialization failed, cannot "
3242 "start asynchronous commit: rc = %d\n",
3245 mdt_device_commit_async(&env, mdt);
3252 * Blocking AST for cross-MDT lock
3254 * Discard lock from uncommitted_slc_locks and cancel it.
3256 * \param lock the lock which blocks a request or cancelling lock
3257 * \param desc unused
3258 * \param data unused
3259 * \param flag indicates whether this cancelling or blocking callback
3260 * \retval 0 on success
3261 * \retval negative number on error
3263 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3264 void *data, int flag)
3270 case LDLM_CB_BLOCKING: {
3271 struct lustre_handle lockh;
3273 ldlm_lock2handle(lock, &lockh);
3274 rc = ldlm_cli_cancel(&lockh,
3275 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3277 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3282 case LDLM_CB_CANCELING: {
3283 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3284 struct mdt_device *mdt =
3285 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3287 LDLM_DEBUG(lock, "Revoke remote lock\n");
3289 /* discard slc lock here so that it can be cleaned anytime,
3290 * especially for cleanup_resource() */
3291 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3293 /* once we cache lock, l_ast_data is set to mdt_object */
3294 if (lock->l_ast_data != NULL) {
3295 struct mdt_object *mo = lock->l_ast_data;
3298 rc = lu_env_init(&env, LCT_MD_THREAD);
3299 if (unlikely(rc != 0)) {
3300 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3302 PFID(mdt_object_fid(mo)), rc);
3306 if (lock->l_policy_data.l_inodebits.bits &
3307 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3308 rc = mo_invalidate(&env, mdt_object_child(mo));
3309 mo->mot_cache_attr = 0;
3311 mdt_object_put(&env, mo);
3323 int mdt_check_resent_lock(struct mdt_thread_info *info,
3324 struct mdt_object *mo,
3325 struct mdt_lock_handle *lhc)
3327 /* the lock might already be gotten in ldlm_handle_enqueue() */
3328 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3329 struct ptlrpc_request *req = mdt_info_req(info);
3330 struct ldlm_lock *lock;
3332 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3333 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3335 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3336 * a resend case, however, it could be already destroyed
3337 * due to client eviction or a raced cancel RPC. */
3338 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3339 lhc->mlh_reg_lh.cookie);
3343 if (!fid_res_name_eq(mdt_object_fid(mo),
3344 &lock->l_resource->lr_name)) {
3345 CWARN("%s: Although resent, but still not "
3346 "get child lock:"DFID"\n",
3347 info->mti_exp->exp_obd->obd_name,
3348 PFID(mdt_object_fid(mo)));
3349 LDLM_LOCK_PUT(lock);
3352 LDLM_LOCK_PUT(lock);
3358 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3360 mdt_object_get(NULL, lock->l_ast_data);
3363 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3364 struct mdt_object *o, const struct lu_fid *fid,
3365 struct lustre_handle *lh, enum ldlm_mode mode,
3366 __u64 *ibits, __u64 trybits, bool cache)
3368 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3369 union ldlm_policy_data *policy = &mti->mti_policy;
3370 struct ldlm_res_id *res_id = &mti->mti_res_id;
3374 LASSERT(mdt_object_remote(o));
3376 fid_build_reg_res_name(fid, res_id);
3378 memset(einfo, 0, sizeof(*einfo));
3379 einfo->ei_type = LDLM_IBITS;
3380 einfo->ei_mode = mode;
3381 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3382 einfo->ei_cb_cp = ldlm_completion_ast;
3383 einfo->ei_enq_slave = 0;
3384 einfo->ei_res_id = res_id;
3388 * if we cache lock, couple lock with mdt_object, so that object
3389 * can be easily found in lock ASTs.
3391 einfo->ei_cbdata = o;
3392 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3395 memset(policy, 0, sizeof(*policy));
3396 policy->l_inodebits.bits = *ibits;
3397 policy->l_inodebits.try_bits = trybits;
3399 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3402 /* Return successfully acquired bits to a caller */
3404 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3407 *ibits = lock->l_policy_data.l_inodebits.bits;
3408 LDLM_LOCK_PUT(lock);
3413 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3414 const struct lu_fid *fid, struct lustre_handle *lh,
3415 enum ldlm_mode mode, __u64 ibits, bool cache)
3417 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3421 static int mdt_object_local_lock(struct mdt_thread_info *info,
3422 struct mdt_object *o,
3423 struct mdt_lock_handle *lh, __u64 *ibits,
3424 __u64 trybits, bool cos_incompat)
3426 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3427 union ldlm_policy_data *policy = &info->mti_policy;
3428 struct ldlm_res_id *res_id = &info->mti_res_id;
3429 __u64 dlmflags = 0, *cookie = NULL;
3433 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3434 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3435 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3436 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3439 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3440 lh->mlh_reg_mode == LCK_EX);
3441 dlmflags |= LDLM_FL_COS_INCOMPAT;
3442 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3443 dlmflags |= LDLM_FL_COS_ENABLED;
3446 /* Only enqueue LOOKUP lock for remote object */
3447 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3449 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3450 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3451 *ibits == MDS_INODELOCK_OPEN)
3452 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3454 if (lh->mlh_type == MDT_PDO_LOCK) {
3455 /* check for exists after object is locked */
3456 if (mdt_object_exists(o) == 0) {
3457 /* Non-existent object shouldn't have PDO lock */
3460 /* Non-dir object shouldn't have PDO lock */
3461 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3466 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3467 dlmflags |= LDLM_FL_ATOMIC_CB;
3470 cookie = &info->mti_exp->exp_handle.h_cookie;
3473 * Take PDO lock on whole directory and build correct @res_id for lock
3474 * on part of directory.
3476 if (lh->mlh_pdo_hash != 0) {
3477 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3478 mdt_lock_pdo_mode(info, o, lh);
3479 if (lh->mlh_pdo_mode != LCK_NL) {
3481 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3482 * is never going to be sent to client and we do not
3483 * want it slowed down due to possible cancels.
3485 policy->l_inodebits.bits =
3486 *ibits & MDS_INODELOCK_UPDATE;
3487 policy->l_inodebits.try_bits =
3488 trybits & MDS_INODELOCK_UPDATE;
3489 /* at least one of them should be set */
3490 LASSERT(policy->l_inodebits.bits |
3491 policy->l_inodebits.try_bits);
3492 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3493 lh->mlh_pdo_mode, policy, res_id,
3495 if (unlikely(rc != 0))
3496 GOTO(out_unlock, rc);
3500 * Finish res_id initializing by name hash marking part of
3501 * directory which is taking modification.
3503 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3506 policy->l_inodebits.bits = *ibits;
3507 policy->l_inodebits.try_bits = trybits;
3510 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3511 * going to be sent to client. If it is - mdt_intent_policy() path will
3512 * fix it up and turn FL_LOCAL flag off.
3514 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3515 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3519 mdt_object_unlock(info, o, lh, 1);
3520 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3521 lh->mlh_pdo_hash != 0 &&
3522 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3523 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3525 /* Return successfully acquired bits to a caller */
3527 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3530 *ibits = lock->l_policy_data.l_inodebits.bits;
3531 LDLM_LOCK_PUT(lock);
3537 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3538 struct mdt_lock_handle *lh, __u64 *ibits,
3539 __u64 trybits, bool cos_incompat)
3541 struct mdt_lock_handle *local_lh = NULL;
3545 if (!mdt_object_remote(o)) {
3546 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3551 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3552 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3553 MDS_INODELOCK_XATTR);
3555 /* Only enqueue LOOKUP lock for remote object */
3556 if (*ibits & MDS_INODELOCK_LOOKUP) {
3557 __u64 local = MDS_INODELOCK_LOOKUP;
3559 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3567 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3568 /* Sigh, PDO needs to enqueue 2 locks right now, but
3569 * enqueue RPC can only request 1 lock, to avoid extra
3570 * RPC, so it will instead enqueue EX lock for remote
3571 * object anyway XXX*/
3572 if (lh->mlh_type == MDT_PDO_LOCK &&
3573 lh->mlh_pdo_hash != 0) {
3575 "%s: "DFID" convert PDO lock to EX lock.\n",
3576 mdt_obd_name(info->mti_mdt),
3577 PFID(mdt_object_fid(o)));
3578 lh->mlh_pdo_hash = 0;
3579 lh->mlh_rreg_mode = LCK_EX;
3580 lh->mlh_type = MDT_REG_LOCK;
3583 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3586 ibits, trybits, false);
3587 if (rc != ELDLM_OK) {
3588 if (local_lh != NULL)
3589 mdt_object_unlock(info, o, local_lh, rc);
3594 /* other components like LFSCK can use lockless access
3595 * and populate cache, so we better invalidate it */
3596 mo_invalidate(info->mti_env, mdt_object_child(o));
3601 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3602 struct mdt_lock_handle *lh, __u64 ibits)
3604 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3607 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3608 struct mdt_lock_handle *lh, __u64 ibits,
3611 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3612 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3616 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3617 struct mdt_lock_handle *lh, __u64 *ibits,
3618 __u64 trybits, bool cos_incompat)
3620 bool trylock_only = *ibits == 0;
3623 LASSERT(!(*ibits & trybits));
3624 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3626 if (rc && trylock_only) { /* clear error for try ibits lock only */
3627 LASSERT(*ibits == 0);
3634 * Save a lock within request object.
3636 * Keep the lock referenced until whether client ACK or transaction
3637 * commit happens or release the lock immediately depending on input
3638 * parameters. If COS is ON, a write lock is converted to COS lock
3641 * \param info thead info object
3642 * \param h lock handle
3643 * \param mode lock mode
3644 * \param decref force immediate lock releasing
3646 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3647 enum ldlm_mode mode, int decref)
3651 if (lustre_handle_is_used(h)) {
3652 if (decref || !info->mti_has_trans ||
3653 !(mode & (LCK_PW | LCK_EX))) {
3654 mdt_fid_unlock(h, mode);
3656 struct mdt_device *mdt = info->mti_mdt;
3657 struct ldlm_lock *lock = ldlm_handle2lock(h);
3658 struct ptlrpc_request *req = mdt_info_req(info);
3659 bool cos = mdt_cos_is_enabled(mdt);
3660 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3662 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3665 /* there is no request if mdt_object_unlock() is called
3666 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3667 if (likely(req != NULL)) {
3668 LDLM_DEBUG(lock, "save lock request %p reply "
3669 "state %p transno %lld\n", req,
3670 req->rq_reply_state, req->rq_transno);
3672 ldlm_lock_mode_downgrade(lock, LCK_COS);
3675 if (req->rq_export->exp_disconnected)
3676 mdt_fid_unlock(h, mode);
3678 ptlrpc_save_lock(req, h, mode, cos,
3681 mdt_fid_unlock(h, mode);
3683 if (mdt_is_lock_sync(lock)) {
3684 CDEBUG(D_HA, "found sync-lock,"
3685 " async commit started\n");
3686 mdt_device_commit_async(info->mti_env,
3689 LDLM_LOCK_PUT(lock);
3698 * Save cross-MDT lock in uncommitted_slc_locks
3700 * Keep the lock referenced until transaction commit happens or release the lock
3701 * immediately depending on input parameters.
3703 * \param info thead info object
3704 * \param h lock handle
3705 * \param mode lock mode
3706 * \param decref force immediate lock releasing
3708 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3709 struct mdt_object *o, struct lustre_handle *h,
3710 enum ldlm_mode mode, int decref)
3714 if (lustre_handle_is_used(h)) {
3715 struct ldlm_lock *lock = ldlm_handle2lock(h);
3716 struct ptlrpc_request *req = mdt_info_req(info);
3719 (lock->l_policy_data.l_inodebits.bits &
3720 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3721 mo_invalidate(info->mti_env, mdt_object_child(o));
3723 if (decref || !info->mti_has_trans || !req ||
3724 !(mode & (LCK_PW | LCK_EX))) {
3725 ldlm_lock_decref_and_cancel(h, mode);
3726 LDLM_LOCK_PUT(lock);
3728 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3730 ldlm_lock_decref(h, mode);
3739 * Unlock mdt object.
3741 * Immeditely release the regular lock and the PDO lock or save the
3742 * lock in request and keep them referenced until client ACK or
3743 * transaction commit.
3745 * \param info thread info object
3746 * \param o mdt object
3747 * \param lh mdt lock handle referencing regular and PDO locks
3748 * \param decref force immediate lock releasing
3750 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3752 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3753 struct mdt_lock_handle *lh, int decref)
3757 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3758 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3759 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3765 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3766 const struct lu_fid *f,
3767 struct mdt_lock_handle *lh,
3770 struct mdt_object *o;
3772 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3776 rc = mdt_object_lock(info, o, lh, ibits);
3778 mdt_object_put(info->mti_env, o);
3785 void mdt_object_unlock_put(struct mdt_thread_info * info,
3786 struct mdt_object * o,
3787 struct mdt_lock_handle *lh,
3790 mdt_object_unlock(info, o, lh, decref);
3791 mdt_object_put(info->mti_env, o);
3795 * Generic code handling requests that have struct mdt_body passed in:
3797 * - extract mdt_body from request and save it in @info, if present;
3799 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3802 * - if HAS_BODY flag is set for this request type check whether object
3803 * actually exists on storage (lu_object_exists()).
3806 static int mdt_body_unpack(struct mdt_thread_info *info,
3807 enum tgt_handler_flags flags)
3809 const struct mdt_body *body;
3810 struct mdt_object *obj;
3811 const struct lu_env *env;
3812 struct req_capsule *pill;
3816 env = info->mti_env;
3817 pill = info->mti_pill;
3819 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3823 if (!(body->mbo_valid & OBD_MD_FLID))
3826 if (!fid_is_sane(&body->mbo_fid1)) {
3827 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3831 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3833 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
3834 mdt_object_put(env, obj);
3837 info->mti_object = obj;
3846 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
3847 enum tgt_handler_flags flags)
3849 struct req_capsule *pill = info->mti_pill;
3854 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3855 rc = mdt_body_unpack(info, flags);
3859 if (rc == 0 && (flags & HAS_REPLY)) {
3861 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3862 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3864 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3865 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3868 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3869 * by default. If the target object has more ACL entries, then
3870 * enlarge the buffer when necessary. */
3871 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3872 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3873 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3875 mdt_preset_secctx_size(info);
3877 rc = req_capsule_server_pack(pill);
3879 CWARN("%s: cannot pack response: rc = %d\n",
3880 mdt_obd_name(info->mti_mdt), rc);
3885 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3887 lh->mlh_type = MDT_NUL_LOCK;
3888 lh->mlh_reg_lh.cookie = 0ull;
3889 lh->mlh_reg_mode = LCK_MINMODE;
3890 lh->mlh_pdo_lh.cookie = 0ull;
3891 lh->mlh_pdo_mode = LCK_MINMODE;
3892 lh->mlh_rreg_lh.cookie = 0ull;
3893 lh->mlh_rreg_mode = LCK_MINMODE;
3896 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3898 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3899 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3903 * Initialize fields of struct mdt_thread_info. Other fields are left in
3904 * uninitialized state, because it's too expensive to zero out whole
3905 * mdt_thread_info (> 1K) on each request arrival.
3907 void mdt_thread_info_init(struct ptlrpc_request *req,
3908 struct mdt_thread_info *info)
3912 info->mti_pill = &req->rq_pill;
3915 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3916 mdt_lock_handle_init(&info->mti_lh[i]);
3918 /* mdt device: it can be NULL while CONNECT */
3919 if (req->rq_export) {
3920 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3921 info->mti_exp = req->rq_export;
3923 info->mti_mdt = NULL;
3924 info->mti_env = req->rq_svc_thread->t_env;
3925 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3927 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3928 info->mti_big_buf = LU_BUF_NULL;
3929 info->mti_body = NULL;
3930 info->mti_object = NULL;
3931 info->mti_dlm_req = NULL;
3932 info->mti_has_trans = 0;
3933 info->mti_cross_ref = 0;
3934 info->mti_opdata = 0;
3935 info->mti_big_lmm_used = 0;
3936 info->mti_big_acl_used = 0;
3937 info->mti_som_valid = 0;
3939 info->mti_spec.no_create = 0;
3940 info->mti_spec.sp_rm_entry = 0;
3941 info->mti_spec.sp_permitted = 0;
3943 info->mti_spec.u.sp_ea.eadata = NULL;
3944 info->mti_spec.u.sp_ea.eadatalen = 0;
3947 void mdt_thread_info_fini(struct mdt_thread_info *info)
3951 if (info->mti_object != NULL) {
3952 mdt_object_put(info->mti_env, info->mti_object);
3953 info->mti_object = NULL;
3956 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3957 mdt_lock_handle_fini(&info->mti_lh[i]);
3958 info->mti_env = NULL;
3959 info->mti_pill = NULL;
3960 info->mti_exp = NULL;
3961 info->mti_mdt = NULL;
3963 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3964 lu_buf_free(&info->mti_big_buf);
3967 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3969 struct mdt_thread_info *mti;
3971 mti = mdt_th_info(tsi->tsi_env);
3972 LASSERT(mti != NULL);
3974 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3975 if (tsi->tsi_corpus != NULL) {
3976 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3977 lu_object_get(tsi->tsi_corpus);
3979 mti->mti_body = tsi->tsi_mdt_body;
3980 mti->mti_dlm_req = tsi->tsi_dlm_req;
3985 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3987 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3989 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
3990 schedule_timeout_uninterruptible(cfs_time_seconds(3));
3992 return tgt_connect(tsi);
3995 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
3996 struct mdt_thread_info *info,
3997 struct ldlm_lock **lockp, __u64 flags)
3999 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4002 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4003 struct mdt_thread_info *info,
4004 struct ldlm_lock **lockp, __u64 flags)
4006 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4010 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4011 struct ldlm_lock **lockp,
4012 struct mdt_lock_handle *lh,
4013 __u64 flags, int result)
4015 struct ptlrpc_request *req = mdt_info_req(info);
4016 struct ldlm_lock *lock = *lockp;
4017 struct ldlm_lock *new_lock;
4019 /* If possible resent found a lock, @lh is set to its handle */
4020 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4022 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
4023 lh->mlh_reg_lh.cookie = 0;
4027 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
4028 /* Lock is pinned by ldlm_handle_enqueue0() as it is
4029 * a resend case, however, it could be already destroyed
4030 * due to client eviction or a raced cancel RPC. */
4031 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4032 lh->mlh_reg_lh.cookie);
4033 lh->mlh_reg_lh.cookie = 0;
4037 LASSERTF(new_lock != NULL,
4038 "lockh %#llx flags %#llx : rc = %d\n",
4039 lh->mlh_reg_lh.cookie, flags, result);
4042 * If we've already given this lock to a client once, then we should
4043 * have no readers or writers. Otherwise, we should have one reader
4044 * _or_ writer ref (which will be zeroed below) before returning the
4047 if (new_lock->l_export == req->rq_export) {
4048 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4050 LASSERT(new_lock->l_export == NULL);
4051 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4056 if (new_lock->l_export == req->rq_export) {
4058 * Already gave this to the client, which means that we
4059 * reconstructed a reply.
4061 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4064 LDLM_LOCK_RELEASE(new_lock);
4065 lh->mlh_reg_lh.cookie = 0;
4066 RETURN(ELDLM_LOCK_REPLACED);
4070 * Fixup the lock to be given to the client.
4072 lock_res_and_lock(new_lock);
4073 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4074 * possible blocking AST. */
4075 while (new_lock->l_readers > 0) {
4076 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4077 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4078 new_lock->l_readers--;
4080 while (new_lock->l_writers > 0) {
4081 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4082 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4083 new_lock->l_writers--;
4086 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4087 new_lock->l_blocking_ast = lock->l_blocking_ast;
4088 new_lock->l_completion_ast = lock->l_completion_ast;
4089 if (ldlm_has_dom(new_lock))
4090 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4091 new_lock->l_remote_handle = lock->l_remote_handle;
4092 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4094 unlock_res_and_lock(new_lock);
4096 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4097 &new_lock->l_remote_handle,
4098 &new_lock->l_exp_hash);
4100 LDLM_LOCK_RELEASE(new_lock);
4101 lh->mlh_reg_lh.cookie = 0;
4103 RETURN(ELDLM_LOCK_REPLACED);
4106 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4107 struct ldlm_lock *new_lock,
4108 struct mdt_lock_handle *lh, __u64 flags)
4110 struct ptlrpc_request *req = mdt_info_req(info);
4111 struct ldlm_request *dlmreq;
4113 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4116 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4118 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4119 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4121 if (flags & LDLM_FL_RESENT) {
4122 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4123 lh->mlh_reg_mode = new_lock->l_granted_mode;
4125 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4126 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4127 lh->mlh_reg_lh.cookie);
4132 * If the xid matches, then we know this is a resent request, and allow
4133 * it. (It's probably an OPEN, for which we don't send a lock.
4135 if (req_can_reconstruct(req, NULL) != 0)
4139 * This remote handle isn't enqueued, so we never received or processed
4140 * this request. Clear MSG_RESENT, because it can be handled like any
4141 * normal request now.
4143 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4145 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4146 dlmreq->lock_handle[0].cookie);
4149 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4150 struct mdt_thread_info *info,
4151 struct ldlm_lock **lockp,
4154 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4155 struct ldlm_reply *ldlm_rep = NULL;
4160 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4161 * (for the resend case) or a new lock. Below we will use it to
4162 * replace the original lock.
4164 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4165 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4166 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4167 rc = mdt_object_lock(info, info->mti_object, lhc,
4168 MDS_INODELOCK_XATTR);
4173 rc = mdt_getxattr(info);
4175 if (mdt_info_req(info)->rq_repmsg != NULL)
4176 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4178 if (ldlm_rep == NULL ||
4179 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4180 mdt_object_unlock(info, info->mti_object, lhc, 1);
4184 RETURN(err_serious(-EFAULT));
4187 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4189 /* This is left for interop instead of adding a new interop flag.
4191 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4192 if (ldlm_rep->lock_policy_res2) {
4193 mdt_object_unlock(info, info->mti_object, lhc, 1);
4194 RETURN(ELDLM_LOCK_ABORTED);
4198 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4202 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4203 struct mdt_thread_info *info,
4204 struct ldlm_lock **lockp,
4207 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4209 struct ldlm_reply *ldlm_rep;
4210 struct mdt_body *reqbody;
4211 struct mdt_body *repbody;
4215 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4218 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4221 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4222 repbody->mbo_eadatasize = 0;
4223 repbody->mbo_aclsize = 0;
4227 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4230 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4234 CERROR("%s: unsupported intent %#x\n",
4235 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4236 GOTO(out_shrink, rc = -EINVAL);
4239 rc = mdt_init_ucred_intent_getattr(info, reqbody);
4241 GOTO(out_shrink, rc);
4243 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4244 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4246 /* Get lock from request for possible resent case. */
4247 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4249 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4250 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4252 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4253 ldlm_rep->lock_policy_res2 = 0;
4254 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4255 ldlm_rep->lock_policy_res2) {
4256 lhc->mlh_reg_lh.cookie = 0ull;
4257 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4260 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4263 mdt_exit_ucred(info);
4265 mdt_client_compatibility(info);
4266 rc2 = mdt_fix_reply(info);
4272 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4273 struct mdt_thread_info *info,
4274 struct ldlm_lock **lockp,
4277 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4278 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4279 struct layout_intent *intent;
4280 struct ldlm_reply *ldlm_rep;
4281 struct lu_fid *fid = &info->mti_tmp_fid2;
4282 struct mdt_object *obj = NULL;
4283 int layout_size = 0;
4284 struct lu_buf *buf = &layout.mlc_buf;
4289 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4291 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4295 CDEBUG(D_INFO, DFID "got layout change request from client: "
4296 "opc:%u flags:%#x extent "DEXT"\n",
4297 PFID(fid), intent->li_opc, intent->li_flags,
4298 PEXT(&intent->li_extent));
4300 switch (intent->li_opc) {
4301 case LAYOUT_INTENT_TRUNC:
4302 case LAYOUT_INTENT_WRITE:
4303 layout.mlc_opc = MD_LAYOUT_WRITE;
4304 layout.mlc_intent = intent;
4306 case LAYOUT_INTENT_ACCESS:
4308 case LAYOUT_INTENT_READ:
4309 case LAYOUT_INTENT_GLIMPSE:
4310 case LAYOUT_INTENT_RELEASE:
4311 case LAYOUT_INTENT_RESTORE:
4312 CERROR("%s: Unsupported layout intent opc %d\n",
4313 mdt_obd_name(info->mti_mdt), intent->li_opc);
4316 CERROR("%s: Unknown layout intent opc %d\n",
4317 mdt_obd_name(info->mti_mdt), intent->li_opc);
4321 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4323 RETURN(PTR_ERR(obj));
4325 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4326 /* if layout is going to be changed don't use the current EA
4327 * size but the maximum one. That buffer will be shrinked
4328 * to the actual size in req_capsule_shrink() before reply.
4330 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4331 layout_size = info->mti_mdt->mdt_max_mdsize;
4333 layout_size = mdt_attr_get_eabuf_size(info, obj);
4334 if (layout_size < 0)
4335 GOTO(out, rc = layout_size);
4337 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4338 info->mti_mdt->mdt_max_mdsize = layout_size;
4340 CDEBUG(D_INFO, "%s: layout_size %d\n",
4341 mdt_obd_name(info->mti_mdt), layout_size);
4345 * set reply buffer size, so that ldlm_handle_enqueue0()->
4346 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4348 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4350 rc = req_capsule_server_pack(info->mti_pill);
4354 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4356 GOTO(out, rc = -EPROTO);
4358 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4360 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4361 if (layout.mlc_opc == MD_LAYOUT_NOP)
4364 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4368 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4369 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4375 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4376 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4378 buf->lb_len = req_capsule_get_size(info->mti_pill,
4379 &RMF_EADATA, RCL_CLIENT);
4381 * If it's a replay of layout write intent RPC, the client has
4382 * saved the extended lovea when it get reply then.
4384 if (buf->lb_len > 0)
4385 mdt_fix_lov_magic(info, buf->lb_buf);
4388 /* Get lock from request for possible resent case. */
4389 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4390 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4393 * Instantiate some layout components, if @buf contains lovea, then it's
4394 * a replay of the layout intent write RPC.
4396 rc = mdt_layout_change(info, obj, lhc, &layout);
4397 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4399 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4400 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4401 if (rc == ELDLM_LOCK_REPLACED &&
4402 (*lockp)->l_granted_mode == LCK_EX)
4403 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4408 mdt_object_put(info->mti_env, obj);
4412 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4413 struct mdt_thread_info *info,
4414 struct ldlm_lock **lockp,
4417 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4418 struct ldlm_reply *rep = NULL;
4422 static const struct req_format *intent_fmts[REINT_MAX] = {
4423 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4424 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4429 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4433 /* Get lock from request for possible resent case. */
4434 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4436 rc = mdt_reint_internal(info, lhc, opc);
4438 /* Check whether the reply has been packed successfully. */
4439 if (mdt_info_req(info)->rq_repmsg != NULL)
4440 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4445 RETURN(err_serious(-EFAULT));
4448 /* MDC expects this in any case */
4450 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4452 /* the open lock or the lock for cross-ref object should be
4453 * returned to the client */
4454 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4455 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4456 rep->lock_policy_res2 = 0;
4457 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4461 rep->lock_policy_res2 = clear_serious(rc);
4463 if (rep->lock_policy_res2 == -ENOENT &&
4464 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4465 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4466 rep->lock_policy_res2 = 0;
4468 lhc->mlh_reg_lh.cookie = 0ull;
4469 if (rc == -ENOTCONN || rc == -ENODEV ||
4470 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4472 * If it is the disconnect error (ENODEV & ENOCONN), the error
4473 * will be returned by rq_status, and client at ptlrpc layer
4474 * will detect this, then disconnect, reconnect the import
4475 * immediately, instead of impacting the following the rpc.
4480 * For other cases, the error will be returned by intent, and client
4481 * will retrieve the result from intent.
4483 RETURN(ELDLM_LOCK_ABORTED);
4486 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4487 struct mdt_thread_info *info,
4488 struct ldlm_lock **lockp,
4489 u64 flags /* LDLM_FL_* */)
4491 struct req_capsule *pill = info->mti_pill;
4492 struct ptlrpc_request *req = mdt_info_req(info);
4493 const struct req_format *it_format;
4494 int (*it_handler)(enum ldlm_intent_flags,
4495 struct mdt_thread_info *,
4496 struct ldlm_lock **,
4498 enum tgt_handler_flags it_handler_flags = 0;
4499 struct ldlm_reply *rep;
4500 bool check_mdt_object = false;
4506 case IT_OPEN|IT_CREAT:
4508 * OCREAT is not a IS_MUTABLE request since the file may
4509 * already exist. We do the extra check of
4510 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4511 * really need to create the object.
4513 it_format = &RQF_LDLM_INTENT;
4514 it_handler = &mdt_intent_open;
4517 check_mdt_object = true;
4520 it_format = &RQF_LDLM_INTENT_GETATTR;
4521 it_handler = &mdt_intent_getattr;
4522 it_handler_flags = HAS_REPLY;
4525 check_mdt_object = true;
4526 it_format = &RQF_LDLM_INTENT_GETXATTR;
4527 it_handler = &mdt_intent_getxattr;
4528 it_handler_flags = HAS_BODY;
4531 it_format = &RQF_LDLM_INTENT_LAYOUT;
4532 it_handler = &mdt_intent_layout;
4535 it_format = &RQF_LDLM_INTENT;
4536 it_handler = &mdt_intent_glimpse;
4539 it_format = &RQF_LDLM_INTENT;
4540 it_handler = &mdt_intent_brw;
4542 case IT_QUOTA_DQACQ:
4543 case IT_QUOTA_CONN: {
4544 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4547 RETURN(-EOPNOTSUPP);
4549 if (mdt_rdonly(req->rq_export))
4552 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4553 /* pass the request to quota master */
4554 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4555 mdt_info_req(info), lockp,
4560 CERROR("%s: unknown intent code %#x\n",
4561 mdt_obd_name(info->mti_mdt), it_opc);
4565 req_capsule_extend(pill, it_format);
4567 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4571 if (unlikely(info->mti_object == NULL && check_mdt_object))
4574 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4577 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4579 /* execute policy */
4580 rc = (*it_handler)(it_opc, info, lockp, flags);
4582 /* Check whether the reply has been packed successfully. */
4583 if (req->rq_repmsg != NULL) {
4584 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4585 rep->lock_policy_res2 =
4586 ptlrpc_status_hton(rep->lock_policy_res2);
4592 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4593 enum ldlm_intent_flags it_opc)
4595 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4597 /* update stats when IT code is known */
4598 if (srv_stats != NULL)
4599 lprocfs_counter_incr(srv_stats,
4600 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4601 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4604 static int mdt_intent_policy(const struct lu_env *env,
4605 struct ldlm_namespace *ns,
4606 struct ldlm_lock **lockp,
4608 enum ldlm_mode mode,
4609 __u64 flags, void *data)
4611 struct tgt_session_info *tsi;
4612 struct mdt_thread_info *info;
4613 struct ptlrpc_request *req = req_cookie;
4614 struct ldlm_intent *it;
4615 struct req_capsule *pill;
4616 const struct ldlm_lock_desc *ldesc;
4621 LASSERT(req != NULL);
4623 tsi = tgt_ses_info(env);
4625 info = tsi2mdt_info(tsi);
4626 LASSERT(info != NULL);
4627 pill = info->mti_pill;
4628 LASSERT(pill->rc_req == req);
4629 ldesc = &info->mti_dlm_req->lock_desc;
4631 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4632 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4633 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4635 mdt_ptlrpc_stats_update(req, it->opc);
4636 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4640 /* Lock without inodebits makes no sense and will oops
4641 * later in ldlm. Let's check it now to see if we have
4642 * ibits corrupted somewhere in mdt_intent_opc().
4643 * The case for client miss to set ibits has been
4644 * processed by others. */
4645 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4646 ldesc->l_policy_data.l_inodebits.bits != 0));
4648 rc = err_serious(-EFAULT);
4651 /* No intent was provided */
4652 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4653 rc = req_capsule_server_pack(pill);
4655 rc = err_serious(rc);
4657 mdt_thread_info_fini(info);
4661 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4663 struct seq_server_site *ss = mdt_seq_site(mdt);
4665 if (ss->ss_node_id == 0)
4668 if (ss->ss_client_seq != NULL) {
4669 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4670 ss->ss_client_seq->lcs_exp = NULL;
4673 if (ss->ss_server_fld != NULL) {
4674 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4675 ss->ss_server_fld->lsf_control_exp = NULL;
4679 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4681 struct seq_server_site *ss = mdt_seq_site(mdt);
4686 if (ss->ss_server_seq != NULL)
4687 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4690 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4692 mdt_seq_fini_cli(mdt);
4693 mdt_deregister_seq_exp(mdt);
4695 return seq_site_fini(env, mdt_seq_site(mdt));
4699 * It will retrieve its FLDB entries from MDT0, and it only happens
4700 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4701 * and it needs to refresh FLDB from the MDT0.
4703 static int mdt_register_lwp_callback(void *data)
4706 struct mdt_device *mdt = data;
4707 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4711 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4713 rc = lu_env_init(&env, LCT_MD_THREAD);
4715 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4719 /* Allocate new sequence now to avoid creating local transaction
4720 * in the normal transaction process */
4721 rc = seq_server_check_and_alloc_super(&env,
4722 mdt_seq_site(mdt)->ss_server_seq);
4727 rc = fld_update_from_controller(&env, fld);
4729 CERROR("%s: cannot update controller: rc = %d\n",
4730 mdt_obd_name(mdt), rc);
4739 static int mdt_register_seq_exp(struct mdt_device *mdt)
4741 struct seq_server_site *ss = mdt_seq_site(mdt);
4742 char *lwp_name = NULL;
4745 if (ss->ss_node_id == 0)
4748 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4749 if (lwp_name == NULL)
4750 GOTO(out_free, rc = -ENOMEM);
4752 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4756 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4761 rc = lustre_register_lwp_item(lwp_name,
4762 &ss->ss_server_fld->lsf_control_exp,
4763 mdt_register_lwp_callback, mdt);
4765 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4766 ss->ss_client_seq->lcs_exp = NULL;
4770 if (lwp_name != NULL)
4771 OBD_FREE(lwp_name, MAX_OBD_NAME);
4777 * Init client sequence manager which is used by local MDS to talk to sequence
4778 * controller on remote node.
4780 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4782 struct seq_server_site *ss = mdt_seq_site(mdt);
4786 /* check if this is adding the first MDC and controller is not yet
4788 OBD_ALLOC_PTR(ss->ss_client_seq);
4789 if (ss->ss_client_seq == NULL)
4792 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4793 if (prefix == NULL) {
4794 OBD_FREE_PTR(ss->ss_client_seq);
4795 ss->ss_client_seq = NULL;
4799 /* Note: seq_client_fini will be called in seq_site_fini */
4800 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4801 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4802 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4804 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4806 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
4809 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4811 struct seq_server_site *ss;
4815 ss = mdt_seq_site(mdt);
4816 /* init sequence controller server(MDT0) */
4817 if (ss->ss_node_id == 0) {
4818 OBD_ALLOC_PTR(ss->ss_control_seq);
4819 if (ss->ss_control_seq == NULL)
4822 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4823 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4826 GOTO(out_seq_fini, rc);
4829 /* Init normal sequence server */
4830 OBD_ALLOC_PTR(ss->ss_server_seq);
4831 if (ss->ss_server_seq == NULL)
4832 GOTO(out_seq_fini, rc = -ENOMEM);
4834 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4835 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4837 GOTO(out_seq_fini, rc);
4839 /* init seq client for seq server to talk to seq controller(MDT0) */
4840 rc = mdt_seq_init_cli(env, mdt);
4842 GOTO(out_seq_fini, rc);
4844 if (ss->ss_node_id != 0)
4845 /* register controller export through lwp */
4846 rc = mdt_register_seq_exp(mdt);
4851 mdt_seq_fini(env, mdt);
4859 static int mdt_fld_fini(const struct lu_env *env,
4860 struct mdt_device *m)
4862 struct seq_server_site *ss = mdt_seq_site(m);
4865 if (ss && ss->ss_server_fld) {
4866 fld_server_fini(env, ss->ss_server_fld);
4867 OBD_FREE_PTR(ss->ss_server_fld);
4868 ss->ss_server_fld = NULL;
4874 static int mdt_fld_init(const struct lu_env *env,
4876 struct mdt_device *m)
4878 struct seq_server_site *ss;
4882 ss = mdt_seq_site(m);
4884 OBD_ALLOC_PTR(ss->ss_server_fld);
4885 if (ss->ss_server_fld == NULL)
4886 RETURN(rc = -ENOMEM);
4888 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4891 OBD_FREE_PTR(ss->ss_server_fld);
4892 ss->ss_server_fld = NULL;
4899 static void mdt_stack_pre_fini(const struct lu_env *env,
4900 struct mdt_device *m, struct lu_device *top)
4902 struct lustre_cfg_bufs *bufs;
4903 struct lustre_cfg *lcfg;
4904 struct mdt_thread_info *info;
4909 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4910 LASSERT(info != NULL);
4912 bufs = &info->mti_u.bufs;
4914 LASSERT(m->mdt_child_exp);
4915 LASSERT(m->mdt_child_exp->exp_obd);
4917 /* process cleanup, pass mdt obd name to get obd umount flags */
4918 /* XXX: this is needed because all layers are referenced by
4919 * objects (some of them are pinned by osd, for example *
4920 * the proper solution should be a model where object used
4921 * by osd only doesn't have mdt/mdd slices -bzzz */
4922 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4923 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4924 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4927 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4929 top->ld_ops->ldo_process_config(env, top, lcfg);
4930 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4934 static void mdt_stack_fini(const struct lu_env *env,
4935 struct mdt_device *m, struct lu_device *top)
4937 struct obd_device *obd = mdt2obd_dev(m);
4938 struct lustre_cfg_bufs *bufs;
4939 struct lustre_cfg *lcfg;
4940 struct mdt_thread_info *info;
4944 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4945 LASSERT(info != NULL);
4947 lu_dev_del_linkage(top->ld_site, top);
4949 lu_site_purge(env, top->ld_site, -1);
4951 bufs = &info->mti_u.bufs;
4952 /* process cleanup, pass mdt obd name to get obd umount flags */
4953 /* another purpose is to let all layers to release their objects */
4954 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4959 lustre_cfg_bufs_set_string(bufs, 1, flags);
4960 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4963 lustre_cfg_init(lcfg, LCFG_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));
4969 lu_site_purge(env, top->ld_site, -1);
4971 m->mdt_child = NULL;
4972 m->mdt_bottom = NULL;
4974 obd_disconnect(m->mdt_child_exp);
4975 m->mdt_child_exp = NULL;
4977 obd_disconnect(m->mdt_bottom_exp);
4978 m->mdt_child_exp = NULL;
4981 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4982 const char *next, struct obd_export **exp)
4984 struct obd_connect_data *data = NULL;
4985 struct obd_device *obd;
4989 OBD_ALLOC_PTR(data);
4991 GOTO(out, rc = -ENOMEM);
4993 obd = class_name2obd(next);
4995 CERROR("%s: can't locate next device: %s\n",
4996 mdt_obd_name(m), next);
4997 GOTO(out, rc = -ENOTCONN);
5000 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5001 data->ocd_version = LUSTRE_VERSION_CODE;
5003 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5005 CERROR("%s: cannot connect to next dev %s (%d)\n",
5006 mdt_obd_name(m), next, rc);
5016 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5017 struct lustre_cfg *cfg)
5019 char *dev = lustre_cfg_string(cfg, 0);
5020 int rc, name_size, uuid_size;
5021 char *name, *uuid, *p;
5022 struct lustre_cfg_bufs *bufs;
5023 struct lustre_cfg *lcfg;
5024 struct obd_device *obd;
5025 struct lustre_profile *lprof;
5026 struct lu_site *site;
5029 /* in 1.8 we had the only device in the stack - MDS.
5030 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5031 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5032 * to generate names and setup MDT, MDD. MDT will be using
5033 * generated name to connect to MDD. for MDD the next device
5034 * will be LOD with name taken from so called "profile" which
5035 * is generated by mount_option line
5037 * 1.8 MGS generates config. commands like this:
5038 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5039 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5040 * 2.0 MGS generates config. commands like this:
5041 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5042 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5043 * 3:lustre-MDT0000-mdtlov 4:f
5045 * we generate MDD name from MDT one, just replacing T with D
5047 * after all the preparations, the logical equivalent will be
5048 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5049 * 3:lustre-MDT0000-mdtlov 4:f
5050 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5051 * 3:lustre-MDD0000 4:f
5053 * notice we build the stack from down to top: MDD first, then MDT */
5055 name_size = MAX_OBD_NAME;
5056 uuid_size = MAX_OBD_NAME;
5058 OBD_ALLOC(name, name_size);
5059 OBD_ALLOC(uuid, uuid_size);
5060 if (name == NULL || uuid == NULL)
5061 GOTO(cleanup_mem, rc = -ENOMEM);
5063 OBD_ALLOC_PTR(bufs);
5065 GOTO(cleanup_mem, rc = -ENOMEM);
5068 p = strstr(name, "-MDT");
5070 GOTO(free_bufs, rc = -ENOMEM);
5073 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5075 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5076 if (lprof == NULL || lprof->lp_dt == NULL) {
5077 CERROR("can't find the profile: %s\n",
5078 lustre_cfg_string(cfg, 0));
5079 GOTO(free_bufs, rc = -EINVAL);
5082 lustre_cfg_bufs_reset(bufs, name);
5083 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5084 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5085 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5087 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5089 GOTO(put_profile, rc = -ENOMEM);
5090 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5092 rc = class_attach(lcfg);
5094 GOTO(lcfg_cleanup, rc);
5096 obd = class_name2obd(name);
5098 CERROR("Can not find obd %s (%s in config)\n",
5099 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5100 GOTO(lcfg_cleanup, rc = -EINVAL);
5103 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5105 lustre_cfg_bufs_reset(bufs, name);
5106 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5107 lustre_cfg_bufs_set_string(bufs, 2, dev);
5108 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5110 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5112 GOTO(class_detach, rc = -ENOMEM);
5113 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5115 rc = class_setup(obd, lcfg);
5117 GOTO(class_detach, rc);
5119 /* connect to MDD we just setup */
5120 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5122 GOTO(class_detach, rc);
5124 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5126 LASSERT(mdt_lu_site(mdt) == NULL);
5127 mdt->mdt_lu_dev.ld_site = site;
5128 site->ls_top_dev = &mdt->mdt_lu_dev;
5129 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5131 /* now connect to bottom OSD */
5132 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5133 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5135 GOTO(class_detach, rc);
5137 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5139 rc = lu_env_refill((struct lu_env *)env);
5141 CERROR("Failure to refill session: '%d'\n", rc);
5143 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5148 class_detach(obd, lcfg);
5150 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5152 class_put_profile(lprof);
5157 OBD_FREE(name, name_size);
5159 OBD_FREE(uuid, uuid_size);
5163 /* setup quota master target on MDT0 */
5164 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5165 struct lustre_cfg *cfg)
5167 struct obd_device *obd;
5168 char *dev = lustre_cfg_string(cfg, 0);
5169 char *qmtname, *uuid, *p;
5170 struct lustre_cfg_bufs *bufs;
5171 struct lustre_cfg *lcfg;
5172 struct lustre_profile *lprof;
5173 struct obd_connect_data *data;
5177 LASSERT(mdt->mdt_qmt_exp == NULL);
5178 LASSERT(mdt->mdt_qmt_dev == NULL);
5180 /* quota master is on MDT0 only for now */
5181 if (mdt->mdt_seq_site.ss_node_id != 0)
5184 /* MGS generates config commands which look as follows:
5185 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5186 * 3:lustre-MDT0000-mdtlov 4:f
5188 * We generate the QMT name from the MDT one, just replacing MD with QM
5189 * after all the preparations, the logical equivalent will be:
5190 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5191 * 3:lustre-MDT0000-osd 4:f */
5192 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5193 OBD_ALLOC(uuid, UUID_MAX);
5194 OBD_ALLOC_PTR(bufs);
5195 OBD_ALLOC_PTR(data);
5196 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5197 GOTO(cleanup_mem, rc = -ENOMEM);
5199 strcpy(qmtname, dev);
5200 p = strstr(qmtname, "-MDT");
5202 GOTO(cleanup_mem, rc = -ENOMEM);
5203 /* replace MD with QM */
5207 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5209 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5210 if (lprof == NULL || lprof->lp_dt == NULL) {
5211 CERROR("can't find profile for %s\n",
5212 lustre_cfg_string(cfg, 0));
5213 GOTO(cleanup_mem, rc = -EINVAL);
5216 lustre_cfg_bufs_reset(bufs, qmtname);
5217 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5218 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5219 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5221 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5223 GOTO(put_profile, rc = -ENOMEM);
5224 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5226 rc = class_attach(lcfg);
5228 GOTO(lcfg_cleanup, rc);
5230 obd = class_name2obd(qmtname);
5232 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5233 lustre_cfg_string(cfg, 0));
5234 GOTO(lcfg_cleanup, rc = -EINVAL);
5237 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5239 lustre_cfg_bufs_reset(bufs, qmtname);
5240 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5241 lustre_cfg_bufs_set_string(bufs, 2, dev);
5243 /* for quota, the next device should be the OSD device */
5244 lustre_cfg_bufs_set_string(bufs, 3,
5245 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5247 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5249 GOTO(class_detach, rc = -ENOMEM);
5250 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5252 rc = class_setup(obd, lcfg);
5254 GOTO(class_detach, rc);
5256 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5258 /* configure local quota objects */
5259 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5262 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5266 GOTO(class_cleanup, rc);
5268 /* connect to quota master target */
5269 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5270 data->ocd_version = LUSTRE_VERSION_CODE;
5271 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5274 CERROR("cannot connect to quota master device %s (%d)\n",
5276 GOTO(class_cleanup, rc);
5282 class_manual_cleanup(obd);
5283 mdt->mdt_qmt_dev = NULL;
5284 GOTO(lcfg_cleanup, rc);
5288 class_detach(obd, lcfg);
5290 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5292 class_put_profile(lprof);
5297 OBD_FREE(qmtname, MAX_OBD_NAME);
5299 OBD_FREE(uuid, UUID_MAX);
5305 /* Shutdown quota master target associated with mdt */
5306 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5310 if (mdt->mdt_qmt_exp == NULL)
5312 LASSERT(mdt->mdt_qmt_dev != NULL);
5314 /* the qmt automatically shuts down when the mdt disconnects */
5315 obd_disconnect(mdt->mdt_qmt_exp);
5316 mdt->mdt_qmt_exp = NULL;
5317 mdt->mdt_qmt_dev = NULL;
5321 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5322 * for now. This will be removed along with converting rest of MDT code
5323 * to use tgt_session_info */
5324 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5326 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5329 if (unlikely(info->mti_object == NULL))
5332 rc = mdt_getxattr(info);
5334 mdt_thread_info_fini(info);
5338 static int mdt_llog_open(struct tgt_session_info *tsi)
5342 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5343 RETURN(err_serious(-EACCES));
5345 RETURN(tgt_llog_open(tsi));
5348 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5349 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5350 #define OST_BRW_READ OST_READ
5351 #define OST_BRW_WRITE OST_WRITE
5353 static struct tgt_handler mdt_tgt_handlers[] = {
5354 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5355 0, MDS_CONNECT, mdt_tgt_connect,
5356 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5357 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5358 0, MDS_DISCONNECT, tgt_disconnect,
5359 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5360 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5361 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5362 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5363 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5364 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5365 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5366 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5368 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5369 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5370 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5371 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5372 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5373 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5374 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5375 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5377 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5378 mdt_hsm_ct_register),
5379 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5380 mdt_hsm_ct_unregister),
5381 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5383 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5385 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5386 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5388 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5391 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5394 static struct tgt_handler mdt_io_ops[] = {
5395 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5397 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5399 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5400 OST_PUNCH, mdt_punch_hdl,
5402 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5405 static struct tgt_handler mdt_sec_ctx_ops[] = {
5406 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5407 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5408 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5411 static struct tgt_handler mdt_quota_ops[] = {
5412 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5415 static struct tgt_handler mdt_llog_handlers[] = {
5416 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5417 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5418 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5419 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5422 static struct tgt_opc_slice mdt_common_slice[] = {
5424 .tos_opc_start = MDS_FIRST_OPC,
5425 .tos_opc_end = MDS_LAST_OPC,
5426 .tos_hs = mdt_tgt_handlers
5429 .tos_opc_start = OBD_FIRST_OPC,
5430 .tos_opc_end = OBD_LAST_OPC,
5431 .tos_hs = tgt_obd_handlers
5434 .tos_opc_start = LDLM_FIRST_OPC,
5435 .tos_opc_end = LDLM_LAST_OPC,
5436 .tos_hs = tgt_dlm_handlers
5439 .tos_opc_start = SEC_FIRST_OPC,
5440 .tos_opc_end = SEC_LAST_OPC,
5441 .tos_hs = mdt_sec_ctx_ops
5444 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5445 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5446 .tos_hs = tgt_out_handlers
5449 .tos_opc_start = FLD_FIRST_OPC,
5450 .tos_opc_end = FLD_LAST_OPC,
5451 .tos_hs = fld_handlers
5454 .tos_opc_start = SEQ_FIRST_OPC,
5455 .tos_opc_end = SEQ_LAST_OPC,
5456 .tos_hs = seq_handlers
5459 .tos_opc_start = QUOTA_DQACQ,
5460 .tos_opc_end = QUOTA_LAST_OPC,
5461 .tos_hs = mdt_quota_ops
5464 .tos_opc_start = LLOG_FIRST_OPC,
5465 .tos_opc_end = LLOG_LAST_OPC,
5466 .tos_hs = mdt_llog_handlers
5469 .tos_opc_start = LFSCK_FIRST_OPC,
5470 .tos_opc_end = LFSCK_LAST_OPC,
5471 .tos_hs = tgt_lfsck_handlers
5474 .tos_opc_start = OST_FIRST_OPC,
5475 .tos_opc_end = OST_LAST_OPC,
5476 .tos_hs = mdt_io_ops
5483 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5485 struct md_device *next = m->mdt_child;
5486 struct lu_device *d = &m->mdt_lu_dev;
5487 struct obd_device *obd = mdt2obd_dev(m);
5488 struct lfsck_stop stop;
5491 stop.ls_status = LS_PAUSED;
5493 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5495 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5497 mdt_restriper_stop(m);
5498 ping_evictor_stop();
5500 /* Remove the HSM /proc entry so the coordinator cannot be
5501 * restarted by a user while it's shutting down.
5503 mdt_hsm_cdt_stop(m);
5505 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5506 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5508 if (m->mdt_namespace != NULL)
5509 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5510 d->ld_obd->obd_force);
5512 obd_exports_barrier(obd);
5513 obd_zombie_barrier();
5515 mdt_quota_fini(env, m);
5517 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5519 /* Calling the cleanup functions in the same order as in the mdt_init0
5522 mdt_tunables_fini(m);
5524 target_recovery_fini(obd);
5525 upcall_cache_cleanup(m->mdt_identity_cache);
5526 m->mdt_identity_cache = NULL;
5528 mdt_fs_cleanup(env, m);
5530 tgt_fini(env, &m->mdt_lut);
5532 mdt_hsm_cdt_fini(m);
5534 if (m->mdt_los != NULL) {
5535 local_oid_storage_fini(env, m->mdt_los);
5539 if (m->mdt_namespace != NULL) {
5540 ldlm_namespace_free_post(m->mdt_namespace);
5541 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5544 if (m->mdt_md_root != NULL) {
5545 mdt_object_put(env, m->mdt_md_root);
5546 m->mdt_md_root = NULL;
5549 mdt_seq_fini(env, m);
5551 mdt_fld_fini(env, m);
5556 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5558 LASSERT(atomic_read(&d->ld_ref) == 0);
5560 server_put_mount(mdt_obd_name(m), true);
5565 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5567 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5568 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5570 const struct dt_device_param *dt_conf;
5571 struct mdt_thread_info *info;
5572 struct obd_device *obd;
5573 const char *dev = lustre_cfg_string(cfg, 0);
5574 const char *num = lustre_cfg_string(cfg, 2);
5575 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5576 struct lustre_mount_info *lmi = NULL;
5577 struct lustre_sb_info *lsi;
5579 struct seq_server_site *ss_site;
5580 const char *identity_upcall = "NONE";
5581 struct md_device *next;
5588 lu_device_init(&m->mdt_lu_dev, ldt);
5590 * Environment (env) might be missing mdt_thread_key values at that
5591 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5594 * Usually device allocation path doesn't use module key values, but
5595 * mdt has to do a lot of work here, so allocate key value.
5597 rc = lu_env_refill((struct lu_env *)env);
5601 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5602 LASSERT(info != NULL);
5604 obd = class_name2obd(dev);
5605 LASSERT(obd != NULL);
5607 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5608 m->mdt_opts.mo_evict_tgt_nids = 1;
5609 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5611 lmi = server_get_mount(dev);
5613 CERROR("Cannot get mount info for %s!\n", dev);
5616 lsi = s2lsi(lmi->lmi_sb);
5617 LASSERT(lsi->lsi_lmd);
5618 /* CMD is supported only in IAM mode */
5620 rc = kstrtol(num, 10, &node_id);
5624 obd->u.obt.obt_magic = OBT_MAGIC;
5625 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5626 m->mdt_skip_lfsck = 1;
5629 /* DoM files get IO lock at open optionally by default */
5630 m->mdt_opts.mo_dom_lock = ALWAYS_DOM_LOCK_ON_OPEN;
5631 /* DoM files are read at open and data is packed in the reply */
5632 m->mdt_opts.mo_dom_read_open = 1;
5634 m->mdt_squash.rsi_uid = 0;
5635 m->mdt_squash.rsi_gid = 0;
5636 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5637 spin_lock_init(&m->mdt_squash.rsi_lock);
5638 spin_lock_init(&m->mdt_lock);
5639 m->mdt_enable_remote_dir = 1;
5640 m->mdt_enable_striped_dir = 1;
5641 m->mdt_enable_dir_migration = 1;
5642 m->mdt_enable_dir_restripe = 0;
5643 m->mdt_enable_dir_auto_split = 0;
5644 m->mdt_enable_remote_dir_gid = 0;
5645 m->mdt_enable_chprojid_gid = 0;
5646 m->mdt_enable_remote_rename = 1;
5647 m->mdt_dir_restripe_nsonly = 1;
5649 atomic_set(&m->mdt_mds_mds_conns, 0);
5650 atomic_set(&m->mdt_async_commit_count, 0);
5652 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5653 m->mdt_lu_dev.ld_obd = obd;
5654 /* Set this lu_device to obd for error handling purposes. */
5655 obd->obd_lu_dev = &m->mdt_lu_dev;
5657 /* init the stack */
5658 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5660 CERROR("%s: Can't init device stack, rc %d\n",
5661 mdt_obd_name(m), rc);
5666 ss_site = mdt_seq_site(m);
5667 s->ld_seq_site = ss_site;
5670 /* set server index */
5671 ss_site->ss_node_id = node_id;
5673 /* failover is the default
5674 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5675 * assumed whose ss_node_id == 0 XXX
5677 obd->obd_replayable = 1;
5678 /* No connection accepted until configurations will finish */
5679 obd->obd_no_conn = 1;
5681 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5682 char *str = lustre_cfg_string(cfg, 4);
5683 if (strchr(str, 'n')) {
5684 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5685 obd->obd_replayable = 0;
5689 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5691 GOTO(err_fini_stack, rc);
5693 rc = mdt_seq_init(env, m);
5695 GOTO(err_fini_fld, rc);
5697 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5698 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5699 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5700 LDLM_NAMESPACE_SERVER,
5701 LDLM_NAMESPACE_GREEDY,
5703 if (m->mdt_namespace == NULL)
5704 GOTO(err_fini_seq, rc = -ENOMEM);
5706 m->mdt_namespace->ns_lvbp = m;
5707 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5709 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5710 /* set obd_namespace for compatibility with old code */
5711 obd->obd_namespace = m->mdt_namespace;
5713 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5714 OBD_FAIL_MDS_ALL_REQUEST_NET,
5715 OBD_FAIL_MDS_ALL_REPLY_NET);
5717 GOTO(err_free_ns, rc);
5719 /* Amount of available space excluded from granting and reserved
5720 * for metadata. It is a percentage of the total MDT size. */
5721 tgd->tgd_reserved_pcnt = 10;
5723 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5724 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5726 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5728 rc = mdt_fs_setup(env, m, obd, lsi);
5732 fid.f_seq = FID_SEQ_LOCAL_NAME;
5735 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
5737 GOTO(err_fs_cleanup, rc);
5739 rc = mdt_hsm_cdt_init(m);
5741 CERROR("%s: error initializing coordinator, rc %d\n",
5742 mdt_obd_name(m), rc);
5743 GOTO(err_los_fini, rc);
5746 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5748 next = m->mdt_child;
5749 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5751 mntopts = dt_conf->ddp_mntopts;
5753 if (mntopts & MNTOPT_USERXATTR)
5754 m->mdt_opts.mo_user_xattr = 1;
5756 m->mdt_opts.mo_user_xattr = 0;
5758 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5760 if (mntopts & MNTOPT_ACL)
5761 m->mdt_opts.mo_acl = 1;
5763 m->mdt_opts.mo_acl = 0;
5765 /* XXX: to support suppgid for ACL, we enable identity_upcall
5766 * by default, otherwise, maybe got unexpected -EACCESS. */
5767 if (m->mdt_opts.mo_acl)
5768 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5770 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5772 &mdt_identity_upcall_cache_ops);
5773 if (IS_ERR(m->mdt_identity_cache)) {
5774 rc = PTR_ERR(m->mdt_identity_cache);
5775 m->mdt_identity_cache = NULL;
5776 GOTO(err_free_hsm, rc);
5779 rc = mdt_tunables_init(m, dev);
5781 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5782 GOTO(err_recovery, rc);
5785 rc = mdt_quota_init(env, m, cfg);
5787 GOTO(err_procfs, rc);
5789 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5790 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5791 "mdt_ldlm_client", m->mdt_ldlm_client);
5793 ping_evictor_start();
5795 /* recovery will be started upon mdt_prepare()
5796 * when the whole stack is complete and ready
5797 * to serve the requests */
5799 /* Reduce the initial timeout on an MDS because it doesn't need such
5800 * a long timeout as an OST does. Adaptive timeouts will adjust this
5801 * value appropriately. */
5802 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5803 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5805 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
5806 m->mdt_lut.lut_local_recovery = 1;
5808 rc = mdt_restriper_start(m);
5810 GOTO(err_ping_evictor, rc);
5815 ping_evictor_stop();
5817 mdt_tunables_fini(m);
5819 upcall_cache_cleanup(m->mdt_identity_cache);
5820 m->mdt_identity_cache = NULL;
5822 mdt_hsm_cdt_fini(m);
5824 local_oid_storage_fini(env, m->mdt_los);
5827 mdt_fs_cleanup(env, m);
5829 /* keep recoverable clients */
5831 target_recovery_fini(obd);
5832 obd_exports_barrier(obd);
5833 obd_zombie_barrier();
5834 tgt_fini(env, &m->mdt_lut);
5836 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5837 obd->obd_namespace = m->mdt_namespace = NULL;
5839 mdt_seq_fini(env, m);
5841 mdt_fld_fini(env, m);
5843 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5846 server_put_mount(dev, true);
5850 /* For interoperability, the left element is old parameter, the right one
5851 * is the new version of the parameter, if some parameter is deprecated,
5852 * the new version should be set as NULL. */
5853 static struct cfg_interop_param mdt_interop_param[] = {
5854 { "mdt.group_upcall", NULL },
5855 { "mdt.quota_type", NULL },
5856 { "mdd.quota_type", NULL },
5857 { "mdt.som", NULL },
5858 { "mdt.rootsquash", "mdt.root_squash" },
5859 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5863 /* used by MGS to process specific configurations */
5864 static int mdt_process_config(const struct lu_env *env,
5865 struct lu_device *d, struct lustre_cfg *cfg)
5867 struct mdt_device *m = mdt_dev(d);
5868 struct md_device *md_next = m->mdt_child;
5869 struct lu_device *next = md2lu_dev(md_next);
5873 switch (cfg->lcfg_command) {
5875 struct obd_device *obd = d->ld_obd;
5876 /* For interoperability */
5877 struct cfg_interop_param *ptr = NULL;
5878 struct lustre_cfg *old_cfg = NULL;
5882 param = lustre_cfg_string(cfg, 1);
5883 if (param == NULL) {
5884 CERROR("param is empty\n");
5889 ptr = class_find_old_param(param, mdt_interop_param);
5891 if (ptr->new_param == NULL) {
5893 CWARN("For interoperability, skip this %s."
5894 " It is obsolete.\n", ptr->old_param);
5898 CWARN("Found old param %s, changed it to %s.\n",
5899 ptr->old_param, ptr->new_param);
5902 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5909 count = class_modify_config(cfg, PARAM_MDT,
5910 &obd->obd_kset.kobj);
5912 struct coordinator *cdt = &m->mdt_coordinator;
5914 /* is it an HSM var ? */
5915 count = class_modify_config(cfg, PARAM_HSM,
5916 &cdt->cdt_hsm_kobj);
5918 /* we don't understand; pass it on */
5919 rc = next->ld_ops->ldo_process_config(env, next,
5922 rc = count > 0 ? 0 : count;
5924 rc = count > 0 ? 0 : count;
5928 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5929 cfg->lcfg_buflens));
5933 /* others are passed further */
5934 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5940 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5941 const struct lu_object_header *hdr,
5942 struct lu_device *d)
5944 struct mdt_object *mo;
5948 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5950 struct lu_object *o;
5951 struct lu_object_header *h;
5954 h = &mo->mot_header;
5955 lu_object_header_init(h);
5956 lu_object_init(o, h, d);
5957 lu_object_add_top(h, o);
5958 o->lo_ops = &mdt_obj_ops;
5959 spin_lock_init(&mo->mot_write_lock);
5960 mutex_init(&mo->mot_som_mutex);
5961 mutex_init(&mo->mot_lov_mutex);
5962 init_rwsem(&mo->mot_dom_sem);
5963 init_rwsem(&mo->mot_open_sem);
5964 atomic_set(&mo->mot_open_count, 0);
5965 mo->mot_restripe_offset = 0;
5966 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
5972 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5973 const struct lu_object_conf *unused)
5975 struct mdt_device *d = mdt_dev(o->lo_dev);
5976 struct lu_device *under;
5977 struct lu_object *below;
5981 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5982 PFID(lu_object_fid(o)));
5984 under = &d->mdt_child->md_lu_dev;
5985 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5986 if (below != NULL) {
5987 lu_object_add(o, below);
5994 static void mdt_object_free_rcu(struct rcu_head *head)
5996 struct mdt_object *mo = container_of(head, struct mdt_object,
5997 mot_header.loh_rcu);
5999 kmem_cache_free(mdt_object_kmem, mo);
6002 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6004 struct mdt_object *mo = mdt_obj(o);
6005 struct lu_object_header *h;
6009 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6010 PFID(lu_object_fid(o)));
6012 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6013 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6016 lu_object_header_fini(h);
6017 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6018 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6023 static int mdt_object_print(const struct lu_env *env, void *cookie,
6024 lu_printer_t p, const struct lu_object *o)
6026 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6028 return (*p)(env, cookie,
6029 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6030 mdto, mdto->mot_lov_created ? "lov_created" : "",
6031 mdto->mot_cache_attr ? "cache_attr" : "",
6032 mdto->mot_write_count);
6035 static int mdt_prepare(const struct lu_env *env,
6036 struct lu_device *pdev,
6037 struct lu_device *cdev)
6039 struct mdt_device *mdt = mdt_dev(cdev);
6040 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6041 struct obd_device *obd = cdev->ld_obd;
6048 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6052 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6056 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6060 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6061 /* The LFSCK instance is registered just now, so it must be there when
6062 * register the namespace to such instance. */
6063 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6065 if (mdt->mdt_seq_site.ss_node_id == 0) {
6066 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6067 &mdt->mdt_md_root_fid);
6072 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6074 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6075 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6076 LASSERT(obd->obd_no_conn);
6077 spin_lock(&obd->obd_dev_lock);
6078 obd->obd_no_conn = 0;
6079 spin_unlock(&obd->obd_dev_lock);
6081 if (obd->obd_recovering == 0)
6082 mdt_postrecov(env, mdt);
6087 const struct lu_device_operations mdt_lu_ops = {
6088 .ldo_object_alloc = mdt_object_alloc,
6089 .ldo_process_config = mdt_process_config,
6090 .ldo_prepare = mdt_prepare,
6093 static const struct lu_object_operations mdt_obj_ops = {
6094 .loo_object_init = mdt_object_init,
6095 .loo_object_free = mdt_object_free,
6096 .loo_object_print = mdt_object_print
6099 static int mdt_obd_set_info_async(const struct lu_env *env,
6100 struct obd_export *exp,
6101 __u32 keylen, void *key,
6102 __u32 vallen, void *val,
6103 struct ptlrpc_request_set *set)
6109 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6110 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6117 static inline void mdt_enable_slc(struct mdt_device *mdt)
6119 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6120 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6123 static inline void mdt_disable_slc(struct mdt_device *mdt)
6125 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6126 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6130 * Match client and server connection feature flags.
6132 * Compute the compatibility flags for a connection request based on
6133 * features mutually supported by client and server.
6135 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6136 * must not be updated here, otherwise a partially initialized value may
6137 * be exposed. After the connection request is successfully processed,
6138 * the top-level MDT connect request handler atomically updates the export
6139 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6140 * reply. \see mdt_connect().
6142 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6143 * full struct obd_connect_data. So care must be taken when accessing fields
6144 * that are not present in struct obd_connect_data_v1. See LU-16.
6146 * \param exp the obd_export associated with this client/target pair
6147 * \param mdt the target device for the connection
6148 * \param data stores data for this connect request
6151 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6152 * \retval -EBADE client and server feature requirements are incompatible
6154 static int mdt_connect_internal(const struct lu_env *env,
6155 struct obd_export *exp,
6156 struct mdt_device *mdt,
6157 struct obd_connect_data *data, bool reconnect)
6159 const char *obd_name = mdt_obd_name(mdt);
6160 LASSERT(data != NULL);
6162 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6164 if (mdt->mdt_bottom->dd_rdonly &&
6165 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6166 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6169 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6170 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6172 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6174 if (!mdt->mdt_opts.mo_acl)
6175 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6177 if (!mdt->mdt_opts.mo_user_xattr)
6178 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6180 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6181 data->ocd_brw_size = min(data->ocd_brw_size,
6183 if (data->ocd_brw_size == 0) {
6184 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6185 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6186 "ocd_brw_size unexpectedly zero, network data "
6187 "corruption? Refusing to connect this client\n",
6188 obd_name, exp->exp_client_uuid.uuid,
6189 exp, data->ocd_connect_flags, data->ocd_version,
6190 data->ocd_grant, data->ocd_index);
6195 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6196 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6198 /* client is reporting its page size, for future use */
6199 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6200 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6201 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6202 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6203 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6204 /* ocd_grant_tax_kb is in 1K byte blocks */
6205 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6206 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6209 /* Save connect_data we have so far because tgt_grant_connect()
6210 * uses it to calculate grant, and we want to save the client
6211 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6212 exp->exp_connect_data = *data;
6213 if (OCD_HAS_FLAG(data, GRANT))
6214 tgt_grant_connect(env, exp, data, !reconnect);
6216 if (OCD_HAS_FLAG(data, MAXBYTES))
6217 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6219 /* NB: Disregard the rule against updating
6220 * exp_connect_data.ocd_connect_flags in this case, since
6221 * tgt_client_new() needs to know if this is a lightweight
6222 * connection, and it is safe to expose this flag before
6223 * connection processing completes. */
6224 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6225 spin_lock(&exp->exp_lock);
6226 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6227 spin_unlock(&exp->exp_lock);
6230 data->ocd_version = LUSTRE_VERSION_CODE;
6232 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6233 CWARN("%s: MDS requires FID support, but client not\n",
6238 if (OCD_HAS_FLAG(data, PINGLESS)) {
6239 if (ptlrpc_pinger_suppress_pings()) {
6240 spin_lock(&exp->exp_obd->obd_dev_lock);
6241 list_del_init(&exp->exp_obd_chain_timed);
6242 spin_unlock(&exp->exp_obd->obd_dev_lock);
6244 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6248 data->ocd_max_easize = mdt->mdt_max_ea_size;
6250 /* NB: Disregard the rule against updating
6251 * exp_connect_data.ocd_connect_flags in this case, since
6252 * tgt_client_new() needs to know if this is client supports
6253 * multiple modify RPCs, and it is safe to expose this flag before
6254 * connection processing completes. */
6255 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6256 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6257 spin_lock(&exp->exp_lock);
6258 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6259 spin_unlock(&exp->exp_lock);
6262 if (OCD_HAS_FLAG(data, CKSUM)) {
6263 __u32 cksum_types = data->ocd_cksum_types;
6265 /* The client set in ocd_cksum_types the checksum types it
6266 * supports. We have to mask off the algorithms that we don't
6268 data->ocd_cksum_types &=
6269 obd_cksum_types_supported_server(obd_name);
6271 if (unlikely(data->ocd_cksum_types == 0)) {
6272 CERROR("%s: Connect with checksum support but no "
6273 "ocd_cksum_types is set\n",
6274 exp->exp_obd->obd_name);
6278 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6279 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6280 cksum_types, data->ocd_cksum_types);
6282 /* This client does not support OBD_CONNECT_CKSUM
6283 * fall back to CRC32 */
6284 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6285 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6286 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6289 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6290 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6291 atomic_inc(&mdt->mdt_mds_mds_conns);
6292 mdt_enable_slc(mdt);
6298 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6299 struct mdt_thread_info *info,
6300 struct mdt_file_data *mfd)
6302 struct lu_context ses;
6306 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6311 lu_context_enter(&ses);
6313 mdt_ucred(info)->uc_valid = UCRED_OLD;
6314 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6316 lu_context_exit(&ses);
6317 lu_context_fini(&ses);
6323 static int mdt_export_cleanup(struct obd_export *exp)
6325 LIST_HEAD(closing_list);
6326 struct mdt_export_data *med = &exp->exp_mdt_data;
6327 struct obd_device *obd = exp->exp_obd;
6328 struct mdt_device *mdt;
6329 struct mdt_thread_info *info;
6331 struct mdt_file_data *mfd, *n;
6335 spin_lock(&med->med_open_lock);
6336 while (!list_empty(&med->med_open_head)) {
6337 struct list_head *tmp = med->med_open_head.next;
6338 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6340 /* Remove mfd handle so it can't be found again.
6341 * We are consuming the mfd_list reference here. */
6342 class_handle_unhash(&mfd->mfd_open_handle);
6343 list_move_tail(&mfd->mfd_list, &closing_list);
6345 spin_unlock(&med->med_open_lock);
6346 mdt = mdt_dev(obd->obd_lu_dev);
6347 LASSERT(mdt != NULL);
6349 rc = lu_env_init(&env, LCT_MD_THREAD);
6353 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6354 LASSERT(info != NULL);
6355 memset(info, 0, sizeof *info);
6356 info->mti_env = &env;
6357 info->mti_mdt = mdt;
6358 info->mti_exp = exp;
6360 if (!list_empty(&closing_list)) {
6361 struct md_attr *ma = &info->mti_attr;
6363 /* Close any open files (which may also cause orphan
6365 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6366 list_del_init(&mfd->mfd_list);
6367 ma->ma_need = ma->ma_valid = 0;
6369 /* This file is being closed due to an eviction, it
6370 * could have been modified and now dirty regarding to
6371 * HSM archive, check this!
6372 * The logic here is to mark a file dirty if there's a
6373 * chance it was dirtied before the client was evicted,
6374 * so that we don't have to wait for a release attempt
6375 * before finding out the file was actually dirty and
6376 * fail the release. Aggressively marking it dirty here
6377 * will cause the policy engine to attempt to
6378 * re-archive it; when rearchiving, we can compare the
6379 * current version to the HSM data_version and make the
6380 * archive request into a noop if it's not actually
6383 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6384 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6386 /* Don't unlink orphan on failover umount, LU-184 */
6387 if (exp->exp_flags & OBD_OPT_FAILOVER) {
6388 ma->ma_valid = MA_FLAGS;
6389 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6391 ma->ma_valid |= MA_FORCE_LOG;
6392 mdt_mfd_close(info, mfd);
6395 info->mti_mdt = NULL;
6396 /* cleanup client slot early */
6397 /* Do not erase record for recoverable client. */
6398 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6399 tgt_client_del(&env, exp);
6405 static int mdt_obd_disconnect(struct obd_export *exp)
6412 class_export_get(exp);
6414 if (!(exp->exp_flags & OBD_OPT_FORCE))
6415 tgt_grant_sanity_check(exp->exp_obd, __func__);
6417 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6418 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6419 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6421 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6422 mdt_disable_slc(mdt);
6425 rc = server_disconnect_export(exp);
6427 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6429 tgt_grant_discard(exp);
6431 rc = mdt_export_cleanup(exp);
6432 nodemap_del_member(exp);
6433 class_export_put(exp);
6437 /* mds_connect copy */
6438 static int mdt_obd_connect(const struct lu_env *env,
6439 struct obd_export **exp, struct obd_device *obd,
6440 struct obd_uuid *cluuid,
6441 struct obd_connect_data *data,
6444 struct obd_export *lexp;
6445 struct lustre_handle conn = { 0 };
6446 struct mdt_device *mdt;
6448 lnet_nid_t *client_nid = localdata;
6451 LASSERT(env != NULL);
6452 LASSERT(data != NULL);
6454 if (!exp || !obd || !cluuid)
6457 mdt = mdt_dev(obd->obd_lu_dev);
6460 * first, check whether the stack is ready to handle requests
6461 * XXX: probably not very appropriate method is used now
6462 * at some point we should find a better one
6464 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6465 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6466 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6467 rc = obd_get_info(env, mdt->mdt_child_exp,
6468 sizeof(KEY_OSP_CONNECTED),
6469 KEY_OSP_CONNECTED, NULL, NULL);
6472 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6475 rc = class_connect(&conn, obd, cluuid);
6479 lexp = class_conn2export(&conn);
6480 LASSERT(lexp != NULL);
6482 rc = nodemap_add_member(*client_nid, lexp);
6483 if (rc != 0 && rc != -EEXIST)
6486 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6488 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6491 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6492 rc = tgt_client_new(env, lexp);
6494 mdt_export_stats_init(obd, lexp, localdata);
6498 class_disconnect(lexp);
6499 nodemap_del_member(lexp);
6503 /* Because we do not want this export to be evicted by pinger,
6504 * let's not add this export to the timed chain list. */
6505 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6506 spin_lock(&lexp->exp_obd->obd_dev_lock);
6507 list_del_init(&lexp->exp_obd_chain_timed);
6508 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6515 static int mdt_obd_reconnect(const struct lu_env *env,
6516 struct obd_export *exp, struct obd_device *obd,
6517 struct obd_uuid *cluuid,
6518 struct obd_connect_data *data,
6521 lnet_nid_t *client_nid = localdata;
6525 if (exp == NULL || obd == NULL || cluuid == NULL)
6528 rc = nodemap_add_member(*client_nid, exp);
6529 if (rc != 0 && rc != -EEXIST)
6532 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6535 mdt_export_stats_init(obd, exp, localdata);
6537 nodemap_del_member(exp);
6542 /* FIXME: Can we avoid using these two interfaces? */
6543 static int mdt_init_export(struct obd_export *exp)
6545 struct mdt_export_data *med = &exp->exp_mdt_data;
6549 INIT_LIST_HEAD(&med->med_open_head);
6550 spin_lock_init(&med->med_open_lock);
6551 spin_lock(&exp->exp_lock);
6552 exp->exp_connecting = 1;
6553 spin_unlock(&exp->exp_lock);
6555 OBD_ALLOC(exp->exp_used_slots,
6556 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6557 if (exp->exp_used_slots == NULL)
6560 /* self-export doesn't need client data and ldlm initialization */
6561 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6562 &exp->exp_client_uuid)))
6565 rc = tgt_client_alloc(exp);
6569 rc = ldlm_init_export(exp);
6576 tgt_client_free(exp);
6578 OBD_FREE(exp->exp_used_slots,
6579 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6580 exp->exp_used_slots = NULL;
6582 CERROR("%s: Failed to initialize export: rc = %d\n",
6583 exp->exp_obd->obd_name, rc);
6587 static int mdt_destroy_export(struct obd_export *exp)
6591 target_destroy_export(exp);
6592 if (exp->exp_used_slots)
6593 OBD_FREE(exp->exp_used_slots,
6594 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6596 /* destroy can be called from failed obd_setup, so
6597 * checking uuid is safer than obd_self_export */
6598 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6599 &exp->exp_client_uuid)))
6602 ldlm_destroy_export(exp);
6603 tgt_client_free(exp);
6605 LASSERT(list_empty(&exp->exp_outstanding_replies));
6606 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6609 * discard grants once we're sure no more
6610 * interaction with the client is possible
6612 tgt_grant_discard(exp);
6613 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6614 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6616 if (!(exp->exp_flags & OBD_OPT_FORCE))
6617 tgt_grant_sanity_check(exp->exp_obd, __func__);
6622 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6623 struct linkea_data *ldata)
6627 LASSERT(ldata->ld_buf->lb_buf != NULL);
6629 if (!mdt_object_exists(mdt_obj))
6632 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6633 ldata->ld_buf, XATTR_NAME_LINK);
6634 if (rc == -ERANGE) {
6635 /* Buf was too small, figure out what we need. */
6636 lu_buf_free(ldata->ld_buf);
6637 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6638 ldata->ld_buf, XATTR_NAME_LINK);
6641 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6642 if (ldata->ld_buf->lb_buf == NULL)
6644 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6645 ldata->ld_buf, XATTR_NAME_LINK);
6650 return linkea_init_with_rec(ldata);
6654 * Given an MDT object, try to look up the full path to the object.
6655 * Part of the MDT layer implementation of lfs fid2path.
6657 * \param[in] info Per-thread common data shared by MDT level handlers.
6658 * \param[in] obj Object to do path lookup of
6659 * \param[in,out] fp User-provided struct to store path information
6660 * \param[in] root_fid Root FID of current path should reach
6662 * \retval 0 Lookup successful, path information stored in fp
6663 * \retval -EAGAIN Lookup failed, usually because object is being moved
6664 * \retval negative errno if there was a problem
6666 static int mdt_path_current(struct mdt_thread_info *info,
6667 struct mdt_object *obj,
6668 struct getinfo_fid2path *fp,
6669 struct lu_fid *root_fid)
6671 struct mdt_device *mdt = info->mti_mdt;
6672 struct mdt_object *mdt_obj;
6673 struct link_ea_header *leh;
6674 struct link_ea_entry *lee;
6675 struct lu_name *tmpname = &info->mti_name;
6676 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6677 struct lu_buf *buf = &info->mti_big_buf;
6680 struct linkea_data ldata = { NULL };
6685 /* temp buffer for path element, the buffer will be finally freed
6686 * in mdt_thread_info_fini */
6687 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6688 if (buf->lb_buf == NULL)
6692 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6695 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6697 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6698 struct lu_buf lmv_buf;
6700 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6701 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6702 GOTO(out, rc = -ENOENT);
6704 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6706 mdt_object_get(info->mti_env, mdt_obj);
6708 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6709 if (IS_ERR(mdt_obj))
6710 GOTO(out, rc = PTR_ERR(mdt_obj));
6713 if (!mdt_object_exists(mdt_obj)) {
6714 mdt_object_put(info->mti_env, mdt_obj);
6715 GOTO(out, rc = -ENOENT);
6718 if (mdt_object_remote(mdt_obj)) {
6719 mdt_object_put(info->mti_env, mdt_obj);
6720 GOTO(remote_out, rc = -EREMOTE);
6723 rc = mdt_links_read(info, mdt_obj, &ldata);
6725 mdt_object_put(info->mti_env, mdt_obj);
6730 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6731 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6732 /* If set, use link #linkno for path lookup, otherwise use
6733 link #0. Only do this for the final path element. */
6734 if (first && fp->gf_linkno < leh->leh_reccount) {
6736 for (count = 0; count < fp->gf_linkno; count++) {
6737 lee = (struct link_ea_entry *)
6738 ((char *)lee + reclen);
6739 linkea_entry_unpack(lee, &reclen, tmpname,
6742 if (fp->gf_linkno < leh->leh_reccount - 1)
6743 /* indicate to user there are more links */
6747 lmv_buf.lb_buf = info->mti_xattr_buf;
6748 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
6749 /* Check if it is slave stripes */
6750 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6751 &lmv_buf, XATTR_NAME_LMV);
6752 mdt_object_put(info->mti_env, mdt_obj);
6754 union lmv_mds_md *lmm = lmv_buf.lb_buf;
6756 /* For slave stripes, get its master */
6757 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
6758 fp->gf_fid = *tmpfid;
6761 } else if (rc < 0 && rc != -ENODATA) {
6767 /* Pack the name in the end of the buffer */
6768 ptr -= tmpname->ln_namelen;
6769 if (ptr - 1 <= fp->gf_u.gf_path)
6770 GOTO(out, rc = -EOVERFLOW);
6771 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6774 /* keep the last resolved fid to the client, so the
6775 * client will build the left path on another MDT for
6777 fp->gf_fid = *tmpfid;
6783 ptr++; /* skip leading / */
6784 memmove(fp->gf_u.gf_path, ptr,
6785 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6792 * Given an MDT object, use mdt_path_current to get the path.
6793 * Essentially a wrapper to retry mdt_path_current a set number of times
6794 * if -EAGAIN is returned (usually because an object is being moved).
6796 * Part of the MDT layer implementation of lfs fid2path.
6798 * \param[in] info Per-thread common data shared by mdt level handlers.
6799 * \param[in] obj Object to do path lookup of
6800 * \param[in,out] fp User-provided struct for arguments and to store path
6803 * \retval 0 Lookup successful, path information stored in fp
6804 * \retval negative errno if there was a problem
6806 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6807 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6809 struct mdt_device *mdt = info->mti_mdt;
6814 if (fp->gf_pathlen < 3)
6817 if (root_fid == NULL)
6818 root_fid = &mdt->mdt_md_root_fid;
6820 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6821 fp->gf_u.gf_path[0] = '\0';
6825 /* Retry multiple times in case file is being moved */
6826 while (tries-- && rc == -EAGAIN)
6827 rc = mdt_path_current(info, obj, fp, root_fid);
6833 * Get the full path of the provided FID, as of changelog record recno.
6835 * This checks sanity and looks up object for user provided FID
6836 * before calling the actual path lookup code.
6838 * Part of the MDT layer implementation of lfs fid2path.
6840 * \param[in] info Per-thread common data shared by mdt level handlers.
6841 * \param[in,out] fp User-provided struct for arguments and to store path
6844 * \retval 0 Lookup successful, path information and recno stored in fp
6845 * \retval -ENOENT, object does not exist
6846 * \retval negative errno if there was a problem
6848 static int mdt_fid2path(struct mdt_thread_info *info,
6849 struct lu_fid *root_fid,
6850 struct getinfo_fid2path *fp)
6852 struct mdt_device *mdt = info->mti_mdt;
6853 struct mdt_object *obj;
6857 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6858 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6860 if (!fid_is_sane(&fp->gf_fid))
6863 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6864 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6865 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6866 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6870 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6873 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6874 PFID(&fp->gf_fid), rc);
6878 if (mdt_object_remote(obj))
6880 else if (!mdt_object_exists(obj))
6886 mdt_object_put(info->mti_env, obj);
6887 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6888 PFID(&fp->gf_fid), rc);
6892 rc = mdt_path(info, obj, fp, root_fid);
6894 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6895 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6896 fp->gf_recno, fp->gf_linkno);
6898 mdt_object_put(info->mti_env, obj);
6903 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6904 void *val, int vallen)
6906 struct getinfo_fid2path *fpout, *fpin;
6907 struct lu_fid *root_fid = NULL;
6910 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6913 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6914 lustre_swab_fid2path(fpin);
6916 memcpy(fpout, fpin, sizeof(*fpin));
6917 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6920 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6921 sizeof(struct lu_fid)) {
6922 /* client sent its root FID, which is normally fileset FID */
6923 root_fid = fpin->gf_u.gf_root_fid;
6924 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6925 lustre_swab_lu_fid(root_fid);
6927 if (root_fid != NULL && !fid_is_sane(root_fid))
6931 rc = mdt_fid2path(info, root_fid, fpout);
6935 int mdt_get_info(struct tgt_session_info *tsi)
6945 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6947 CDEBUG(D_IOCTL, "No GETINFO key\n");
6948 RETURN(err_serious(-EFAULT));
6950 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6953 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6954 if (vallen == NULL) {
6955 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6956 tgt_name(tsi->tsi_tgt));
6957 RETURN(err_serious(-EFAULT));
6960 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6962 rc = req_capsule_server_pack(tsi->tsi_pill);
6964 RETURN(err_serious(rc));
6966 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6967 if (valout == NULL) {
6968 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6969 tgt_name(tsi->tsi_tgt));
6970 RETURN(err_serious(-EFAULT));
6973 if (KEY_IS(KEY_FID2PATH)) {
6974 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6976 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6977 mdt_thread_info_fini(info);
6984 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6986 struct obd_ioctl_data *data = karg;
6989 struct mdt_object *obj;
6990 struct mdt_lock_handle *lh;
6994 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6995 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6998 fid = (struct lu_fid *)data->ioc_inlbuf1;
7000 if (!fid_is_sane(fid))
7003 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7005 lh = &mti->mti_lh[MDT_LH_PARENT];
7006 mdt_lock_reg_init(lh, LCK_CR);
7008 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7010 RETURN(PTR_ERR(obj));
7012 if (mdt_object_remote(obj)) {
7015 * before calling version get the correct MDS should be
7016 * fid, this is error to find remote object here
7018 CERROR("nonlocal object "DFID"\n", PFID(fid));
7019 } else if (!mdt_object_exists(obj)) {
7020 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7023 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7024 *(__u64 *)data->ioc_inlbuf2 = version;
7027 mdt_object_unlock_put(mti, obj, lh, 1);
7031 /* ioctls on obd dev */
7032 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7033 void *karg, void __user *uarg)
7036 struct obd_device *obd = exp->exp_obd;
7037 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7038 struct dt_device *dt = mdt->mdt_bottom;
7042 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7043 rc = lu_env_init(&env, LCT_MD_THREAD);
7049 rc = mdt_device_sync(&env, mdt);
7051 case OBD_IOC_SET_READONLY:
7052 rc = dt_sync(&env, dt);
7054 rc = dt_ro(&env, dt);
7056 case OBD_IOC_ABORT_RECOVERY:
7057 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7058 obd->obd_abort_recovery = 1;
7059 target_stop_recovery_thread(obd);
7062 case OBD_IOC_CHANGELOG_REG:
7063 case OBD_IOC_CHANGELOG_DEREG:
7064 case OBD_IOC_CHANGELOG_CLEAR:
7065 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7069 case OBD_IOC_START_LFSCK: {
7070 struct md_device *next = mdt->mdt_child;
7071 struct obd_ioctl_data *data = karg;
7072 struct lfsck_start_param lsp;
7074 if (unlikely(data == NULL)) {
7079 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7080 lsp.lsp_index_valid = 0;
7081 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7084 case OBD_IOC_STOP_LFSCK: {
7085 struct md_device *next = mdt->mdt_child;
7086 struct obd_ioctl_data *data = karg;
7087 struct lfsck_stop stop;
7089 stop.ls_status = LS_STOPPED;
7090 /* Old lfsck utils may pass NULL @stop. */
7091 if (data->ioc_inlbuf1 == NULL)
7095 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7097 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7100 case OBD_IOC_QUERY_LFSCK: {
7101 struct md_device *next = mdt->mdt_child;
7102 struct obd_ioctl_data *data = karg;
7104 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7108 case OBD_IOC_GET_OBJ_VERSION: {
7109 struct mdt_thread_info *mti;
7110 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7111 memset(mti, 0, sizeof *mti);
7112 mti->mti_env = &env;
7116 rc = mdt_ioc_version_get(mti, karg);
7119 case OBD_IOC_CATLOGLIST: {
7120 struct mdt_thread_info *mti;
7122 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7123 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7124 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7125 &mti->mti_tmp_fid1);
7130 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7131 mdt_obd_name(mdt), cmd, rc);
7138 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7140 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7144 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7145 struct lfsck_start_param lsp;
7147 lsp.lsp_start = NULL;
7148 lsp.lsp_index_valid = 0;
7149 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7150 OBD_IOC_START_LFSCK,
7152 if (rc != 0 && rc != -EALREADY)
7153 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7154 mdt_obd_name(mdt), rc);
7157 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7161 static int mdt_obd_postrecov(struct obd_device *obd)
7166 rc = lu_env_init(&env, LCT_MD_THREAD);
7169 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7174 static const struct obd_ops mdt_obd_device_ops = {
7175 .o_owner = THIS_MODULE,
7176 .o_set_info_async = mdt_obd_set_info_async,
7177 .o_connect = mdt_obd_connect,
7178 .o_reconnect = mdt_obd_reconnect,
7179 .o_disconnect = mdt_obd_disconnect,
7180 .o_init_export = mdt_init_export,
7181 .o_destroy_export = mdt_destroy_export,
7182 .o_iocontrol = mdt_iocontrol,
7183 .o_postrecov = mdt_obd_postrecov,
7184 /* Data-on-MDT IO methods */
7185 .o_preprw = mdt_obd_preprw,
7186 .o_commitrw = mdt_obd_commitrw,
7189 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7190 struct lu_device *d)
7192 struct mdt_device *m = mdt_dev(d);
7199 static struct lu_device *mdt_device_free(const struct lu_env *env,
7200 struct lu_device *d)
7202 struct mdt_device *m = mdt_dev(d);
7205 lu_device_fini(&m->mdt_lu_dev);
7211 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7212 struct lu_device_type *t,
7213 struct lustre_cfg *cfg)
7215 struct lu_device *l;
7216 struct mdt_device *m;
7223 rc = mdt_init0(env, m, t, cfg);
7225 mdt_device_free(env, l);
7230 l = ERR_PTR(-ENOMEM);
7234 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7235 LU_KEY_INIT(mdt, struct mdt_thread_info);
7237 static void mdt_key_fini(const struct lu_context *ctx,
7238 struct lu_context_key *key, void* data)
7240 struct mdt_thread_info *info = data;
7242 if (info->mti_big_lmm) {
7243 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7244 info->mti_big_lmm = NULL;
7245 info->mti_big_lmmsize = 0;
7248 if (info->mti_big_acl) {
7249 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7250 info->mti_big_acl = NULL;
7251 info->mti_big_aclsize = 0;
7257 /* context key: mdt_thread_key */
7258 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7260 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7262 return lu_ucred(info->mti_env);
7265 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7267 return lu_ucred_check(info->mti_env);
7271 * Enable/disable COS (Commit On Sharing).
7273 * Set/Clear the COS flag in mdt options.
7275 * \param mdt mdt device
7276 * \param val 0 disables COS, other values enable COS
7278 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7283 mdt->mdt_opts.mo_cos = val;
7284 rc = lu_env_init(&env, LCT_LOCAL);
7285 if (unlikely(rc != 0)) {
7286 CWARN("%s: lu_env initialization failed, cannot "
7287 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7290 mdt_device_sync(&env, mdt);
7295 * Check COS (Commit On Sharing) status.
7297 * Return COS flag status.
7299 * \param mdt mdt device
7301 int mdt_cos_is_enabled(struct mdt_device *mdt)
7303 return mdt->mdt_opts.mo_cos != 0;
7306 static struct lu_device_type_operations mdt_device_type_ops = {
7307 .ldto_device_alloc = mdt_device_alloc,
7308 .ldto_device_free = mdt_device_free,
7309 .ldto_device_fini = mdt_device_fini
7312 static struct lu_device_type mdt_device_type = {
7313 .ldt_tags = LU_DEVICE_MD,
7314 .ldt_name = LUSTRE_MDT_NAME,
7315 .ldt_ops = &mdt_device_type_ops,
7316 .ldt_ctx_tags = LCT_MD_THREAD
7319 static int __init mdt_init(void)
7323 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7324 FID_NOBRACE_LEN + 1);
7325 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7327 rc = lu_kmem_init(mdt_caches);
7331 rc = mds_mod_init();
7335 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
7336 LUSTRE_MDT_NAME, &mdt_device_type);
7341 lu_kmem_fini(mdt_caches);
7348 static void __exit mdt_exit(void)
7350 class_unregister_type(LUSTRE_MDT_NAME);
7352 lu_kmem_fini(mdt_caches);
7355 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7356 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7357 MODULE_VERSION(LUSTRE_VERSION_STRING);
7358 MODULE_LICENSE("GPL");
7360 module_init(mdt_init);
7361 module_exit(mdt_exit);