4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lustre/mdt/mdt_handler.c
33 * Lustre Metadata Target (mdt) request handler
35 * Author: Peter Braam <braam@clusterfs.com>
36 * Author: Andreas Dilger <adilger@clusterfs.com>
37 * Author: Phil Schwan <phil@clusterfs.com>
38 * Author: Mike Shaver <shaver@clusterfs.com>
39 * Author: Nikita Danilov <nikita@clusterfs.com>
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_MDS
46 #include <linux/module.h>
47 #include <linux/pagemap.h>
49 #include <dt_object.h>
50 #include <lustre_acl.h>
51 #include <lustre_export.h>
52 #include <uapi/linux/lustre/lustre_ioctl.h>
53 #include <lustre_lfsck.h>
54 #include <lustre_log.h>
55 #include <lustre_nodemap.h>
56 #include <lustre_mds.h>
57 #include <uapi/linux/lustre/lustre_param.h>
58 #include <lustre_quota.h>
59 #include <lustre_swab.h>
60 #include <lustre_lmv.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
67 #include "mdt_internal.h"
69 static unsigned int max_mod_rpcs_per_client = 8;
70 module_param(max_mod_rpcs_per_client, uint, 0644);
71 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
73 mdl_mode_t mdt_mdl_lock_modes[] = {
74 [LCK_MINMODE] = MDL_MINMODE,
81 [LCK_GROUP] = MDL_GROUP
84 enum ldlm_mode mdt_dlm_lock_modes[] = {
85 [MDL_MINMODE] = LCK_MINMODE,
92 [MDL_GROUP] = LCK_GROUP
95 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static const struct lu_object_operations mdt_obj_ops;
99 /* Slab for MDT object allocation */
100 static struct kmem_cache *mdt_object_kmem;
102 /* For HSM restore handles */
103 struct kmem_cache *mdt_hsm_cdt_kmem;
105 /* For HSM request handles */
106 struct kmem_cache *mdt_hsm_car_kmem;
108 static struct lu_kmem_descr mdt_caches[] = {
110 .ckd_cache = &mdt_object_kmem,
111 .ckd_name = "mdt_obj",
112 .ckd_size = sizeof(struct mdt_object)
115 .ckd_cache = &mdt_hsm_cdt_kmem,
116 .ckd_name = "mdt_cdt_restore_handle",
117 .ckd_size = sizeof(struct cdt_restore_handle)
120 .ckd_cache = &mdt_hsm_car_kmem,
121 .ckd_name = "mdt_cdt_agent_req",
122 .ckd_size = sizeof(struct cdt_agent_req)
129 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
133 return rep->lock_policy_res1 & op_flag;
136 void mdt_clear_disposition(struct mdt_thread_info *info,
137 struct ldlm_reply *rep, __u64 op_flag)
140 info->mti_opdata &= ~op_flag;
141 tgt_opdata_clear(info->mti_env, op_flag);
144 rep->lock_policy_res1 &= ~op_flag;
147 void mdt_set_disposition(struct mdt_thread_info *info,
148 struct ldlm_reply *rep, __u64 op_flag)
151 info->mti_opdata |= op_flag;
152 tgt_opdata_set(info->mti_env, op_flag);
155 rep->lock_policy_res1 |= op_flag;
158 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
160 lh->mlh_pdo_hash = 0;
161 lh->mlh_reg_mode = lm;
162 lh->mlh_rreg_mode = lm;
163 lh->mlh_type = MDT_REG_LOCK;
166 void mdt_lh_reg_init(struct mdt_lock_handle *lh, struct ldlm_lock *lock)
168 mdt_lock_reg_init(lh, lock->l_req_mode);
169 if (lock->l_req_mode == LCK_GROUP)
170 lh->mlh_gid = lock->l_policy_data.l_inodebits.li_gid;
173 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
174 const struct lu_name *lname)
176 lh->mlh_reg_mode = lock_mode;
177 lh->mlh_pdo_mode = LCK_MINMODE;
178 lh->mlh_rreg_mode = lock_mode;
179 lh->mlh_type = MDT_PDO_LOCK;
181 if (lu_name_is_valid(lname)) {
182 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
184 /* XXX Workaround for LU-2856
186 * Zero is a valid return value of full_name_hash, but
187 * several users of mlh_pdo_hash assume a non-zero
188 * hash value. We therefore map zero onto an
189 * arbitrary, but consistent value (1) to avoid
190 * problems further down the road. */
191 if (unlikely(lh->mlh_pdo_hash == 0))
192 lh->mlh_pdo_hash = 1;
194 lh->mlh_pdo_hash = 0;
198 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
199 struct mdt_lock_handle *lh)
205 * Any dir access needs couple of locks:
207 * 1) on part of dir we gonna take lookup/modify;
209 * 2) on whole dir to protect it from concurrent splitting and/or to
210 * flush client's cache for readdir().
212 * so, for a given mode and object this routine decides what lock mode
213 * to use for lock #2:
215 * 1) if caller's gonna lookup in dir then we need to protect dir from
216 * being splitted only - LCK_CR
218 * 2) if caller's gonna modify dir then we need to protect dir from
219 * being splitted and to flush cache - LCK_CW
221 * 3) if caller's gonna modify dir and that dir seems ready for
222 * splitting then we need to protect it from any type of access
223 * (lookup/modify/split) - LCK_EX --bzzz
226 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
227 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
230 * Ask underlaying level its opinion about preferable PDO lock mode
231 * having access type passed as regular lock mode:
233 * - MDL_MINMODE means that lower layer does not want to specify lock
236 * - MDL_NL means that no PDO lock should be taken. This is used in some
237 * cases. Say, for non-splittable directories no need to use PDO locks
240 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
241 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
243 if (mode != MDL_MINMODE) {
244 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
247 * Lower layer does not want to specify locking mode. We do it
248 * our selves. No special protection is needed, just flush
249 * client's cache on modification and allow concurrent
252 switch (lh->mlh_reg_mode) {
254 lh->mlh_pdo_mode = LCK_EX;
257 lh->mlh_pdo_mode = LCK_CR;
260 lh->mlh_pdo_mode = LCK_CW;
263 CERROR("Not expected lock type (0x%x)\n",
264 (int)lh->mlh_reg_mode);
269 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
273 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
276 struct mdt_device *mdt = info->mti_mdt;
277 struct lu_name *lname = &info->mti_name;
278 const char *start = fileset;
279 char *filename = info->mti_filename;
280 struct mdt_object *parent;
284 LASSERT(!info->mti_cross_ref);
287 * We may want to allow this to mount a completely separate
288 * fileset from the MDT in the future, but keeping it to
289 * ROOT/ only for now avoid potential security issues.
291 *fid = mdt->mdt_md_root_fid;
293 while (rc == 0 && start != NULL && *start != '\0') {
294 const char *s1 = start;
300 while (*s2 != '/' && *s2 != '\0')
308 lname->ln_namelen = s2 - s1;
309 if (lname->ln_namelen > NAME_MAX) {
314 /* reject .. as a path component */
315 if (lname->ln_namelen == 2 &&
316 strncmp(s1, "..", 2) == 0) {
321 strncpy(filename, s1, lname->ln_namelen);
322 filename[lname->ln_namelen] = '\0';
323 lname->ln_name = filename;
325 parent = mdt_object_find(info->mti_env, mdt, fid);
326 if (IS_ERR(parent)) {
327 rc = PTR_ERR(parent);
330 /* Only got the fid of this obj by name */
332 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
333 fid, &info->mti_spec);
334 mdt_object_put(info->mti_env, parent);
337 parent = mdt_object_find(info->mti_env, mdt, fid);
339 rc = PTR_ERR(parent);
341 mode = lu_object_attr(&parent->mot_obj);
342 if (!S_ISDIR(mode)) {
344 } else if (mdt_is_remote_object(info, parent, parent)) {
345 if (!mdt->mdt_enable_remote_subdir_mount) {
347 LCONSOLE_WARN("%s: subdir mount '%s' refused because 'enable_remote_subdir_mount=0': rc = %d\n",
351 LCONSOLE_INFO("%s: subdir mount '%s' is remote and may be slow\n",
356 mdt_object_put(info->mti_env, parent);
363 static int mdt_get_root(struct tgt_session_info *tsi)
365 struct mdt_thread_info *info = tsi2mdt_info(tsi);
366 struct mdt_device *mdt = info->mti_mdt;
367 struct mdt_body *repbody;
368 char *fileset = NULL, *buffer = NULL;
370 struct obd_export *exp = info->mti_exp;
371 char *nodemap_fileset;
375 rc = mdt_check_ucred(info);
377 GOTO(out, rc = err_serious(rc));
379 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
380 GOTO(out, rc = err_serious(-ENOMEM));
382 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
383 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
384 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
386 GOTO(out, rc = err_serious(-EFAULT));
389 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
390 if (nodemap_fileset && nodemap_fileset[0]) {
391 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
393 /* consider fileset from client as a sub-fileset
394 * of the nodemap one */
395 OBD_ALLOC(buffer, PATH_MAX + 1);
397 GOTO(out, rc = err_serious(-ENOMEM));
398 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
399 nodemap_fileset, fileset) >= PATH_MAX + 1)
400 GOTO(out, rc = err_serious(-EINVAL));
403 /* enforce fileset as specified in the nodemap */
404 fileset = nodemap_fileset;
409 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
410 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
412 GOTO(out, rc = err_serious(rc));
414 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
416 repbody->mbo_valid |= OBD_MD_FLID;
420 mdt_thread_info_fini(info);
422 OBD_FREE(buffer, PATH_MAX+1);
426 static int mdt_statfs(struct tgt_session_info *tsi)
428 struct ptlrpc_request *req = tgt_ses_req(tsi);
429 struct mdt_thread_info *info = tsi2mdt_info(tsi);
430 struct mdt_device *mdt = info->mti_mdt;
431 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
432 struct md_device *next = mdt->mdt_child;
433 struct ptlrpc_service_part *svcpt;
434 struct obd_statfs *osfs;
435 struct mdt_body *reqbody = NULL;
436 struct mdt_statfs_cache *msf;
437 ktime_t kstart = ktime_get();
438 int current_blockbits;
443 svcpt = req->rq_rqbd->rqbd_svcpt;
445 /* This will trigger a watchdog timeout */
446 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
447 (MDT_SERVICE_WATCHDOG_FACTOR *
448 at_get(&svcpt->scp_at_estimate)) + 1);
450 rc = mdt_check_ucred(info);
452 GOTO(out, rc = err_serious(rc));
454 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
455 GOTO(out, rc = err_serious(-ENOMEM));
457 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
459 GOTO(out, rc = -EPROTO);
461 if (mdt_is_sum_statfs_client(req->rq_export) &&
462 lustre_packed_msg_size(req->rq_reqmsg) ==
463 req_capsule_fmt_size(req->rq_reqmsg->lm_magic,
464 &RQF_MDS_STATFS_NEW, RCL_CLIENT)) {
465 req_capsule_extend(info->mti_pill, &RQF_MDS_STATFS_NEW);
466 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
469 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
470 msf = &mdt->mdt_sum_osfs;
472 msf = &mdt->mdt_osfs;
474 if (msf->msf_age + OBD_STATFS_CACHE_SECONDS <= ktime_get_seconds()) {
475 /** statfs data is too old, get up-to-date one */
476 if (reqbody && reqbody->mbo_valid & OBD_MD_FLAGSTATFS)
477 rc = next->md_ops->mdo_statfs(info->mti_env,
480 rc = dt_statfs(info->mti_env, mdt->mdt_bottom,
484 spin_lock(&mdt->mdt_lock);
485 msf->msf_osfs = *osfs;
486 msf->msf_age = ktime_get_seconds();
487 spin_unlock(&mdt->mdt_lock);
489 /** use cached statfs data */
490 spin_lock(&mdt->mdt_lock);
491 *osfs = msf->msf_osfs;
492 spin_unlock(&mdt->mdt_lock);
495 /* tgd_blockbit is recordsize bits set during mkfs.
496 * This once set does not change. However, 'zfs set'
497 * can be used to change the MDT blocksize. Instead
498 * of using cached value of 'tgd_blockbit' always
499 * calculate the blocksize bits which may have
502 current_blockbits = fls64(osfs->os_bsize) - 1;
504 /* at least try to account for cached pages. its still racy and
505 * might be under-reporting if clients haven't announced their
506 * caches with brw recently */
507 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
508 " pending %llu free %llu avail %llu\n",
509 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
510 tgd->tgd_tot_pending,
511 osfs->os_bfree << current_blockbits,
512 osfs->os_bavail << current_blockbits);
514 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
515 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
516 osfs->os_bsize - 1) >> current_blockbits));
518 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
519 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
520 "%llu objects: %llu free; state %x\n",
521 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
522 osfs->os_files, osfs->os_ffree, osfs->os_state);
524 if (!exp_grant_param_supp(tsi->tsi_exp) &&
525 current_blockbits > COMPAT_BSIZE_SHIFT) {
526 /* clients which don't support OBD_CONNECT_GRANT_PARAM
527 * should not see a block size > page size, otherwise
528 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
529 * block size which is the biggest block size known to work
530 * with all client's page size. */
531 osfs->os_blocks <<= current_blockbits - COMPAT_BSIZE_SHIFT;
532 osfs->os_bfree <<= current_blockbits - COMPAT_BSIZE_SHIFT;
533 osfs->os_bavail <<= current_blockbits - COMPAT_BSIZE_SHIFT;
534 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
537 mdt_counter_incr(req, LPROC_MDT_STATFS,
538 ktime_us_delta(ktime_get(), kstart));
540 mdt_thread_info_fini(info);
544 __u32 mdt_lmm_dom_entry_check(struct lov_mds_md *lmm, int *is_dom_only)
546 struct lov_comp_md_v1 *comp_v1;
547 struct lov_mds_md *v1;
549 __u32 dom_stripesize = 0;
551 bool has_ost_stripes = false;
558 if (le32_to_cpu(lmm->lmm_magic) != LOV_MAGIC_COMP_V1)
561 comp_v1 = (struct lov_comp_md_v1 *)lmm;
562 off = le32_to_cpu(comp_v1->lcm_entries[0].lcme_offset);
563 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
565 /* Fast check for DoM entry with no mirroring, should be the first */
566 if (le16_to_cpu(comp_v1->lcm_mirror_count) == 0 &&
567 lov_pattern(le32_to_cpu(v1->lmm_pattern)) != LOV_PATTERN_MDT)
570 /* check all entries otherwise */
571 for (i = 0; i < le16_to_cpu(comp_v1->lcm_entry_count); i++) {
572 struct lov_comp_md_entry_v1 *lcme;
574 lcme = &comp_v1->lcm_entries[i];
575 if (!(le32_to_cpu(lcme->lcme_flags) & LCME_FL_INIT))
578 off = le32_to_cpu(lcme->lcme_offset);
579 v1 = (struct lov_mds_md *)((char *)comp_v1 + off);
581 if (lov_pattern(le32_to_cpu(v1->lmm_pattern)) ==
583 dom_stripesize = le32_to_cpu(v1->lmm_stripe_size);
585 has_ost_stripes = true;
587 if (dom_stripesize && has_ost_stripes)
588 RETURN(dom_stripesize);
590 /* DoM-only case exits here */
591 if (is_dom_only && dom_stripesize)
593 RETURN(dom_stripesize);
597 * Pack size attributes into the reply.
599 int mdt_pack_size2body(struct mdt_thread_info *info,
600 const struct lu_fid *fid, struct lustre_handle *lh)
603 struct md_attr *ma = &info->mti_attr;
605 bool dom_lock = false;
609 LASSERT(ma->ma_attr.la_valid & LA_MODE);
611 if (!S_ISREG(ma->ma_attr.la_mode) ||
612 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
615 dom_stripe = mdt_lmm_dom_stripesize(ma->ma_lmm);
616 /* no DoM stripe, no size in reply */
620 if (lustre_handle_is_used(lh)) {
621 struct ldlm_lock *lock;
623 lock = ldlm_handle2lock(lh);
625 dom_lock = ldlm_has_dom(lock);
630 /* no DoM lock, no size in reply */
634 /* Either DoM lock exists or LMM has only DoM stripe then
635 * return size on body. */
636 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
638 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
642 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
644 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
646 * \param info thread info object
647 * \param repbody reply to pack ACLs into
648 * \param o mdt object of file to examine
649 * \param nodemap nodemap of client to reply to
651 * \retval -errno error getting or parsing ACL from disk
653 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
654 struct mdt_object *o, struct lu_nodemap *nodemap)
656 const struct lu_env *env = info->mti_env;
657 struct md_object *next = mdt_object_child(o);
658 struct lu_buf *buf = &info->mti_buf;
659 struct mdt_device *mdt = info->mti_mdt;
660 struct req_capsule *pill = info->mti_pill;
665 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
666 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
667 if (buf->lb_len == 0)
670 LASSERT(!info->mti_big_acl_used);
672 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
674 if (rc == -ENODATA) {
675 repbody->mbo_aclsize = 0;
676 repbody->mbo_valid |= OBD_MD_FLACL;
678 } else if (rc == -EOPNOTSUPP) {
680 } else if (rc == -ERANGE) {
681 if (exp_connect_large_acl(info->mti_exp) &&
682 !info->mti_big_acl_used) {
683 if (info->mti_big_acl == NULL) {
684 info->mti_big_aclsize =
686 mdt->mdt_max_ea_size,
688 OBD_ALLOC_LARGE(info->mti_big_acl,
689 info->mti_big_aclsize);
690 if (info->mti_big_acl == NULL) {
691 info->mti_big_aclsize = 0;
692 CERROR("%s: unable to grow "
695 PFID(mdt_object_fid(o)));
700 CDEBUG(D_INODE, "%s: grow the "DFID
701 " ACL buffer to size %d\n",
703 PFID(mdt_object_fid(o)),
704 info->mti_big_aclsize);
706 buf->lb_buf = info->mti_big_acl;
707 buf->lb_len = info->mti_big_aclsize;
708 info->mti_big_acl_used = 1;
711 /* FS has ACL bigger that our limits */
712 CDEBUG(D_INODE, "%s: "DFID" ACL can't fit into %d\n",
713 mdt_obd_name(mdt), PFID(mdt_object_fid(o)),
714 info->mti_big_aclsize);
717 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
718 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
721 rc = nodemap_map_acl(nodemap, buf->lb_buf,
722 rc, NODEMAP_FS_TO_CLIENT);
723 /* if all ACLs mapped out, rc is still >= 0 */
725 CERROR("%s: nodemap_map_acl unable to parse "DFID
726 " ACL: rc = %d\n", mdt_obd_name(mdt),
727 PFID(mdt_object_fid(o)), rc);
728 repbody->mbo_aclsize = 0;
729 repbody->mbo_valid &= ~OBD_MD_FLACL;
731 repbody->mbo_aclsize = rc;
732 repbody->mbo_valid |= OBD_MD_FLACL;
741 /* XXX Look into layout in MDT layer. */
742 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
744 struct lov_comp_md_v1 *comp_v1;
745 struct lov_mds_md *v1;
748 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
749 comp_v1 = (struct lov_comp_md_v1 *)lmm;
751 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
752 v1 = (struct lov_mds_md *)((char *)comp_v1 +
753 comp_v1->lcm_entries[i].lcme_offset);
754 /* We don't support partial release for now */
755 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
760 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
765 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
766 const struct lu_attr *attr, const struct lu_fid *fid)
768 struct md_attr *ma = &info->mti_attr;
769 struct obd_export *exp = info->mti_exp;
770 struct lu_nodemap *nodemap = NULL;
772 LASSERT(ma->ma_valid & MA_INODE);
774 if (attr->la_valid & LA_ATIME) {
775 b->mbo_atime = attr->la_atime;
776 b->mbo_valid |= OBD_MD_FLATIME;
778 if (attr->la_valid & LA_MTIME) {
779 b->mbo_mtime = attr->la_mtime;
780 b->mbo_valid |= OBD_MD_FLMTIME;
782 if (attr->la_valid & LA_CTIME) {
783 b->mbo_ctime = attr->la_ctime;
784 b->mbo_valid |= OBD_MD_FLCTIME;
786 if (attr->la_valid & LA_BTIME) {
787 b->mbo_btime = attr->la_btime;
788 b->mbo_valid |= OBD_MD_FLBTIME;
790 if (attr->la_valid & LA_FLAGS) {
791 b->mbo_flags = attr->la_flags;
792 b->mbo_valid |= OBD_MD_FLFLAGS;
794 if (attr->la_valid & LA_NLINK) {
795 b->mbo_nlink = attr->la_nlink;
796 b->mbo_valid |= OBD_MD_FLNLINK;
798 if (attr->la_valid & (LA_UID|LA_GID)) {
799 nodemap = nodemap_get_from_exp(exp);
803 if (attr->la_valid & LA_UID) {
804 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
805 NODEMAP_FS_TO_CLIENT,
807 b->mbo_valid |= OBD_MD_FLUID;
809 if (attr->la_valid & LA_GID) {
810 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
811 NODEMAP_FS_TO_CLIENT,
813 b->mbo_valid |= OBD_MD_FLGID;
816 if (attr->la_valid & LA_PROJID) {
817 /* TODO, nodemap for project id */
818 b->mbo_projid = attr->la_projid;
819 b->mbo_valid |= OBD_MD_FLPROJID;
822 b->mbo_mode = attr->la_mode;
823 if (attr->la_valid & LA_MODE)
824 b->mbo_valid |= OBD_MD_FLMODE;
825 if (attr->la_valid & LA_TYPE)
826 b->mbo_valid |= OBD_MD_FLTYPE;
830 b->mbo_valid |= OBD_MD_FLID;
831 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
832 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
835 if (!(attr->la_valid & LA_TYPE))
838 b->mbo_rdev = attr->la_rdev;
839 b->mbo_size = attr->la_size;
840 b->mbo_blocks = attr->la_blocks;
842 if (!S_ISREG(attr->la_mode)) {
843 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
844 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
845 /* means no objects are allocated on osts. */
846 LASSERT(!(ma->ma_valid & MA_LOV));
847 /* just ignore blocks occupied by extend attributes on MDS */
849 /* if no object is allocated on osts, the size on mds is valid.
851 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
852 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
853 if (mdt_hsm_is_released(ma->ma_lmm)) {
854 /* A released file stores its size on MDS. */
855 /* But return 1 block for released file, unless tools
856 * like tar will consider it fully sparse. (LU-3864)
858 if (unlikely(b->mbo_size == 0))
862 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
863 } else if (info->mti_som_valid) { /* som is valid */
864 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
865 } else if (ma->ma_valid & MA_SOM) { /* lsom is valid */
866 b->mbo_valid |= OBD_MD_FLLAZYSIZE | OBD_MD_FLLAZYBLOCKS;
867 b->mbo_size = ma->ma_som.ms_size;
868 b->mbo_blocks = ma->ma_som.ms_blocks;
872 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE ||
873 b->mbo_valid & OBD_MD_FLLAZYSIZE))
874 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
875 PFID(fid), (unsigned long long)b->mbo_size);
878 if (!IS_ERR_OR_NULL(nodemap))
879 nodemap_putref(nodemap);
882 static inline int mdt_body_has_lov(const struct lu_attr *la,
883 const struct mdt_body *body)
885 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
886 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
889 void mdt_client_compatibility(struct mdt_thread_info *info)
891 struct mdt_body *body;
892 struct ptlrpc_request *req = mdt_info_req(info);
893 struct obd_export *exp = req->rq_export;
894 struct md_attr *ma = &info->mti_attr;
895 struct lu_attr *la = &ma->ma_attr;
898 if (exp_connect_layout(exp))
899 /* the client can deal with 16-bit lmm_stripe_count */
902 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
904 if (!mdt_body_has_lov(la, body))
907 /* now we have a reply with a lov for a client not compatible with the
908 * layout lock so we have to clean the layout generation number */
909 if (S_ISREG(la->la_mode))
910 ma->ma_lmm->lmm_layout_gen = 0;
914 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
915 struct mdt_object *o)
917 const struct lu_env *env = info->mti_env;
920 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
929 /* Is it a directory? Let's check for the LMV as well */
930 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
931 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
935 rc2 = mo_xattr_get(env, mdt_object_child(o),
937 XATTR_NAME_DEFAULT_LMV);
939 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
947 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
950 const struct lu_env *env = info->mti_env;
954 LASSERT(info->mti_big_lmm_used == 0);
955 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
959 /* big_lmm may need to be grown */
960 if (info->mti_big_lmmsize < rc) {
961 int size = size_roundup_power2(rc);
963 if (info->mti_big_lmmsize > 0) {
964 /* free old buffer */
965 LASSERT(info->mti_big_lmm);
966 OBD_FREE_LARGE(info->mti_big_lmm,
967 info->mti_big_lmmsize);
968 info->mti_big_lmm = NULL;
969 info->mti_big_lmmsize = 0;
972 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
973 if (info->mti_big_lmm == NULL)
975 info->mti_big_lmmsize = size;
977 LASSERT(info->mti_big_lmmsize >= rc);
979 info->mti_buf.lb_buf = info->mti_big_lmm;
980 info->mti_buf.lb_len = info->mti_big_lmmsize;
981 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
986 int __mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
987 struct md_attr *ma, const char *name)
989 struct md_object *next = mdt_object_child(o);
990 struct lu_buf *buf = &info->mti_buf;
993 if (strcmp(name, XATTR_NAME_LOV) == 0) {
994 buf->lb_buf = ma->ma_lmm;
995 buf->lb_len = ma->ma_lmm_size;
996 LASSERT(!(ma->ma_valid & MA_LOV));
997 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
998 buf->lb_buf = ma->ma_lmv;
999 buf->lb_len = ma->ma_lmv_size;
1000 LASSERT(!(ma->ma_valid & MA_LMV));
1001 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1002 buf->lb_buf = ma->ma_default_lmv;
1003 buf->lb_len = ma->ma_default_lmv_size;
1004 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
1009 LASSERT(buf->lb_buf);
1011 rc = mo_xattr_get(info->mti_env, next, buf, name);
1015 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1016 if (info->mti_big_lmm_used)
1017 ma->ma_lmm = info->mti_big_lmm;
1019 /* NOT return LOV EA with hole to old client. */
1020 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
1021 LOV_PATTERN_F_HOLE) &&
1022 !(exp_connect_flags(info->mti_exp) &
1023 OBD_CONNECT_LFSCK)) {
1026 ma->ma_lmm_size = rc;
1027 ma->ma_valid |= MA_LOV;
1029 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1030 if (info->mti_big_lmm_used)
1031 ma->ma_lmv = info->mti_big_lmm;
1033 ma->ma_lmv_size = rc;
1034 ma->ma_valid |= MA_LMV;
1035 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1036 ma->ma_default_lmv_size = rc;
1037 ma->ma_valid |= MA_LMV_DEF;
1040 /* Update mdt_max_mdsize so all clients will be aware that */
1041 if (info->mti_mdt->mdt_max_mdsize < rc)
1042 info->mti_mdt->mdt_max_mdsize = rc;
1045 } else if (rc == -ENODATA) {
1048 } else if (rc == -ERANGE) {
1049 /* Default LMV has fixed size, so it must be able to fit
1050 * in the original buffer */
1051 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
1053 rc = mdt_big_xattr_get(info, o, name);
1055 info->mti_big_lmm_used = 1;
1063 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
1064 struct md_attr *ma, const char *name)
1068 if (!info->mti_big_lmm) {
1069 OBD_ALLOC(info->mti_big_lmm, PAGE_SIZE);
1070 if (!info->mti_big_lmm)
1072 info->mti_big_lmmsize = PAGE_SIZE;
1075 if (strcmp(name, XATTR_NAME_LOV) == 0) {
1076 ma->ma_lmm = info->mti_big_lmm;
1077 ma->ma_lmm_size = info->mti_big_lmmsize;
1078 ma->ma_valid &= ~MA_LOV;
1079 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
1080 ma->ma_lmv = info->mti_big_lmm;
1081 ma->ma_lmv_size = info->mti_big_lmmsize;
1082 ma->ma_valid &= ~MA_LMV;
1087 LASSERT(!info->mti_big_lmm_used);
1088 rc = __mdt_stripe_get(info, o, ma, name);
1089 /* since big_lmm is always used here, clear 'used' flag to avoid
1090 * assertion in mdt_big_xattr_get().
1092 info->mti_big_lmm_used = 0;
1097 int mdt_attr_get_pfid(struct mdt_thread_info *info, struct mdt_object *o,
1098 struct lu_fid *pfid)
1100 struct lu_buf *buf = &info->mti_buf;
1101 struct link_ea_header *leh;
1102 struct link_ea_entry *lee;
1106 buf->lb_buf = info->mti_big_lmm;
1107 buf->lb_len = info->mti_big_lmmsize;
1108 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
1109 buf, XATTR_NAME_LINK);
1110 /* ignore errors, MA_PFID won't be set and it is
1111 * up to the caller to treat this as an error */
1112 if (rc == -ERANGE || buf->lb_len == 0) {
1113 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1114 buf->lb_buf = info->mti_big_lmm;
1115 buf->lb_len = info->mti_big_lmmsize;
1120 if (rc < sizeof(*leh)) {
1121 CERROR("short LinkEA on "DFID": rc = %d\n",
1122 PFID(mdt_object_fid(o)), rc);
1126 leh = (struct link_ea_header *) buf->lb_buf;
1127 lee = (struct link_ea_entry *)(leh + 1);
1128 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1129 leh->leh_magic = LINK_EA_MAGIC;
1130 leh->leh_reccount = __swab32(leh->leh_reccount);
1131 leh->leh_len = __swab64(leh->leh_len);
1133 if (leh->leh_magic != LINK_EA_MAGIC)
1135 if (leh->leh_reccount == 0)
1138 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1139 fid_be_to_cpu(pfid, pfid);
1144 int mdt_attr_get_pfid_name(struct mdt_thread_info *info, struct mdt_object *o,
1145 struct lu_fid *pfid, struct lu_name *lname)
1147 struct lu_buf *buf = &info->mti_buf;
1148 struct link_ea_header *leh;
1149 struct link_ea_entry *lee;
1153 buf->lb_buf = info->mti_xattr_buf;
1154 buf->lb_len = sizeof(info->mti_xattr_buf);
1155 rc = mo_xattr_get(info->mti_env, mdt_object_child(o), buf,
1157 if (rc == -ERANGE) {
1158 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
1159 buf->lb_buf = info->mti_big_lmm;
1160 buf->lb_len = info->mti_big_lmmsize;
1165 if (rc < sizeof(*leh)) {
1166 CERROR("short LinkEA on "DFID": rc = %d\n",
1167 PFID(mdt_object_fid(o)), rc);
1171 leh = (struct link_ea_header *)buf->lb_buf;
1172 lee = (struct link_ea_entry *)(leh + 1);
1173 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
1174 leh->leh_magic = LINK_EA_MAGIC;
1175 leh->leh_reccount = __swab32(leh->leh_reccount);
1176 leh->leh_len = __swab64(leh->leh_len);
1178 if (leh->leh_magic != LINK_EA_MAGIC)
1181 if (leh->leh_reccount == 0)
1184 linkea_entry_unpack(lee, &reclen, lname, pfid);
1189 int mdt_attr_get_complex(struct mdt_thread_info *info,
1190 struct mdt_object *o, struct md_attr *ma)
1192 const struct lu_env *env = info->mti_env;
1193 struct md_object *next = mdt_object_child(o);
1194 struct lu_buf *buf = &info->mti_buf;
1195 int need = ma->ma_need;
1202 if (mdt_object_exists(o) == 0)
1203 GOTO(out, rc = -ENOENT);
1204 mode = lu_object_attr(&next->mo_lu);
1206 if (need & MA_INODE) {
1207 ma->ma_need = MA_INODE;
1208 rc = mo_attr_get(env, next, ma);
1213 (void) mdt_get_som(info, o, ma);
1214 ma->ma_valid |= MA_INODE;
1217 if (need & MA_PFID) {
1218 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1220 ma->ma_valid |= MA_PFID;
1221 /* ignore this error, parent fid is not mandatory */
1225 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1226 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1231 if (need & MA_LMV && S_ISDIR(mode)) {
1232 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1237 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1238 rc = __mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1244 * In the handle of MA_INODE, we may already get the SOM attr.
1246 if (need & MA_SOM && S_ISREG(mode) && !(ma->ma_valid & MA_SOM)) {
1247 rc = mdt_get_som(info, o, ma);
1252 if (need & MA_HSM && S_ISREG(mode)) {
1253 buf->lb_buf = info->mti_xattr_buf;
1254 buf->lb_len = sizeof(info->mti_xattr_buf);
1255 BUILD_BUG_ON(sizeof(struct hsm_attrs) >
1256 sizeof(info->mti_xattr_buf));
1257 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1258 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1260 ma->ma_valid |= MA_HSM;
1261 else if (rc2 < 0 && rc2 != -ENODATA)
1262 GOTO(out, rc = rc2);
1265 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1266 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1267 buf->lb_buf = ma->ma_acl;
1268 buf->lb_len = ma->ma_acl_size;
1269 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1271 ma->ma_acl_size = rc2;
1272 ma->ma_valid |= MA_ACL_DEF;
1273 } else if (rc2 == -ENODATA) {
1275 ma->ma_acl_size = 0;
1277 GOTO(out, rc = rc2);
1282 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1283 rc, ma->ma_valid, ma->ma_lmm);
1287 static int mdt_getattr_internal(struct mdt_thread_info *info,
1288 struct mdt_object *o, int ma_need)
1290 struct mdt_device *mdt = info->mti_mdt;
1291 struct md_object *next = mdt_object_child(o);
1292 const struct mdt_body *reqbody = info->mti_body;
1293 struct ptlrpc_request *req = mdt_info_req(info);
1294 struct md_attr *ma = &info->mti_attr;
1295 struct lu_attr *la = &ma->ma_attr;
1296 struct req_capsule *pill = info->mti_pill;
1297 const struct lu_env *env = info->mti_env;
1298 struct mdt_body *repbody;
1299 struct lu_buf *buffer = &info->mti_buf;
1300 struct obd_export *exp = info->mti_exp;
1301 ktime_t kstart = ktime_get();
1306 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1307 RETURN(err_serious(-ENOMEM));
1309 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1313 if (mdt_object_remote(o)) {
1314 /* This object is located on remote node.*/
1315 /* Return -ENOTSUPP for old client */
1316 if (!mdt_is_dne_client(req->rq_export))
1317 GOTO(out, rc = -ENOTSUPP);
1319 repbody->mbo_fid1 = *mdt_object_fid(o);
1320 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1324 if (reqbody->mbo_eadatasize > 0) {
1325 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1326 if (buffer->lb_buf == NULL)
1327 GOTO(out, rc = -EPROTO);
1328 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1331 buffer->lb_buf = NULL;
1333 ma_need &= ~(MA_LOV | MA_LMV);
1334 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1335 mdt_obd_name(info->mti_mdt),
1336 req->rq_export->exp_client_uuid.uuid);
1339 /* from 2.12.58 intent_getattr pack default LMV in reply */
1340 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1341 ((reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) ==
1342 (OBD_MD_MEA | OBD_MD_DEFAULT_MEA)) &&
1343 req_capsule_has_field(&req->rq_pill, &RMF_DEFAULT_MDT_MD,
1345 ma->ma_lmv = buffer->lb_buf;
1346 ma->ma_lmv_size = buffer->lb_len;
1347 ma->ma_default_lmv = req_capsule_server_get(pill,
1348 &RMF_DEFAULT_MDT_MD);
1349 ma->ma_default_lmv_size = req_capsule_get_size(pill,
1350 &RMF_DEFAULT_MDT_MD,
1352 ma->ma_need = MA_INODE;
1353 if (ma->ma_lmv_size > 0)
1354 ma->ma_need |= MA_LMV;
1355 if (ma->ma_default_lmv_size > 0)
1356 ma->ma_need |= MA_LMV_DEF;
1357 } else if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1358 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1359 /* If it is dir and client require MEA, then we got MEA */
1360 /* Assumption: MDT_MD size is enough for lmv size. */
1361 ma->ma_lmv = buffer->lb_buf;
1362 ma->ma_lmv_size = buffer->lb_len;
1363 ma->ma_need = MA_INODE;
1364 if (ma->ma_lmv_size > 0) {
1365 if (reqbody->mbo_valid & OBD_MD_MEA) {
1366 ma->ma_need |= MA_LMV;
1367 } else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA) {
1368 ma->ma_need |= MA_LMV_DEF;
1369 ma->ma_default_lmv = buffer->lb_buf;
1371 ma->ma_default_lmv_size = buffer->lb_len;
1372 ma->ma_lmv_size = 0;
1376 ma->ma_lmm = buffer->lb_buf;
1377 ma->ma_lmm_size = buffer->lb_len;
1378 ma->ma_need = MA_INODE | MA_HSM;
1379 if (ma->ma_lmm_size > 0) {
1380 ma->ma_need |= MA_LOV;
1381 /* Older clients may crash if they getattr overstriped
1384 if (!exp_connect_overstriping(exp) &&
1385 mdt_lmm_is_overstriping(ma->ma_lmm))
1386 RETURN(-EOPNOTSUPP);
1390 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1391 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1392 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1393 /* get default stripe info for this dir. */
1394 ma->ma_need |= MA_LOV_DEF;
1396 ma->ma_need |= ma_need;
1398 rc = mdt_attr_get_complex(info, o, ma);
1400 CDEBUG_LIMIT(rc == -ENOENT ? D_OTHER : D_ERROR,
1401 "%s: getattr error for "DFID": rc = %d\n",
1402 mdt_obd_name(info->mti_mdt),
1403 PFID(mdt_object_fid(o)), rc);
1407 /* if file is released, check if a restore is running */
1408 if (ma->ma_valid & MA_HSM) {
1409 repbody->mbo_valid |= OBD_MD_TSTATE;
1410 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1411 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1412 repbody->mbo_t_state = MS_RESTORE;
1415 if (unlikely(!(ma->ma_valid & MA_INODE)))
1418 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1420 if (mdt_body_has_lov(la, reqbody)) {
1421 u32 stripe_count = 1;
1423 if (ma->ma_valid & MA_LOV) {
1424 LASSERT(ma->ma_lmm_size);
1425 repbody->mbo_eadatasize = ma->ma_lmm_size;
1426 if (S_ISDIR(la->la_mode))
1427 repbody->mbo_valid |= OBD_MD_FLDIREA;
1429 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1430 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1432 if (ma->ma_valid & MA_LMV) {
1433 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
1434 u32 magic = le32_to_cpu(lmv->lmv_magic);
1436 /* Return -ENOTSUPP for old client */
1437 if (!mdt_is_striped_client(req->rq_export))
1440 LASSERT(S_ISDIR(la->la_mode));
1441 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1442 repbody->mbo_eadatasize = ma->ma_lmv_size;
1443 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1445 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
1446 if (magic == LMV_MAGIC_STRIPE && lmv_is_restriping(lmv))
1447 mdt_restripe_migrate_add(info, o);
1448 else if (magic == LMV_MAGIC_V1 &&
1449 lmv_is_restriping(lmv))
1450 mdt_restripe_update_add(info, o);
1452 if (ma->ma_valid & MA_LMV_DEF) {
1453 /* Return -ENOTSUPP for old client */
1454 if (!mdt_is_striped_client(req->rq_export))
1456 LASSERT(S_ISDIR(la->la_mode));
1458 * when ll_dir_getstripe() gets default LMV, it
1459 * checks mbo_eadatasize.
1461 if (!(ma->ma_valid & MA_LMV))
1462 repbody->mbo_eadatasize =
1463 ma->ma_default_lmv_size;
1464 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1465 OBD_MD_DEFAULT_MEA);
1468 "dirent count %llu stripe count %u MDT count %d\n",
1469 ma->ma_attr.la_dirent_count, stripe_count,
1470 atomic_read(&mdt->mdt_mds_mds_conns) + 1);
1471 if (ma->ma_attr.la_dirent_count != LU_DIRENT_COUNT_UNSET &&
1472 ma->ma_attr.la_dirent_count >
1473 mdt->mdt_restriper.mdr_dir_split_count &&
1474 !fid_is_root(mdt_object_fid(o)) &&
1475 mdt->mdt_enable_dir_auto_split &&
1476 !o->mot_restriping &&
1477 stripe_count < atomic_read(&mdt->mdt_mds_mds_conns) + 1)
1478 mdt_auto_split_add(info, o);
1479 } else if (S_ISLNK(la->la_mode) &&
1480 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1481 buffer->lb_buf = ma->ma_lmm;
1482 /* eadatasize from client includes NULL-terminator, so
1483 * there is no need to read it */
1484 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1485 rc = mo_readlink(env, next, buffer);
1486 if (unlikely(rc <= 0)) {
1487 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1488 mdt_obd_name(info->mti_mdt),
1489 PFID(mdt_object_fid(o)), rc);
1492 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1494 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1496 repbody->mbo_valid |= OBD_MD_LINKNAME;
1497 /* we need to report back size with NULL-terminator
1498 * because client expects that */
1499 repbody->mbo_eadatasize = rc + 1;
1500 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1501 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1502 "on "DFID ", expected %d\n",
1503 mdt_obd_name(info->mti_mdt),
1504 rc, PFID(mdt_object_fid(o)),
1505 reqbody->mbo_eadatasize - 1);
1506 /* NULL terminate */
1507 ((char *)ma->ma_lmm)[rc] = 0;
1509 /* If the total CDEBUG() size is larger than a page, it
1510 * will print a warning to the console, avoid this by
1511 * printing just the last part of the symlink. */
1512 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1513 print_limit < rc ? "..." : "", print_limit,
1514 (char *)ma->ma_lmm + rc - print_limit, rc);
1519 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1520 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1521 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1522 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1523 repbody->mbo_max_mdsize);
1526 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1527 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1528 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1529 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1530 if (IS_ERR(nodemap))
1531 RETURN(PTR_ERR(nodemap));
1533 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1534 nodemap_putref(nodemap);
1540 mdt_counter_incr(req, LPROC_MDT_GETATTR,
1541 ktime_us_delta(ktime_get(), kstart));
1546 static int mdt_getattr(struct tgt_session_info *tsi)
1548 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1549 struct mdt_object *obj = info->mti_object;
1550 struct req_capsule *pill = info->mti_pill;
1551 struct mdt_body *reqbody;
1552 struct mdt_body *repbody;
1556 if (unlikely(info->mti_object == NULL))
1559 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1561 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1563 /* Special case for Data-on-MDT files to get data version */
1564 if (unlikely(reqbody->mbo_valid & OBD_MD_FLDATAVERSION)) {
1565 rc = mdt_data_version_get(tsi);
1569 /* Unlike intent case where we need to pre-fill out buffers early on
1570 * in intent policy for ldlm reasons, here we can have a much better
1571 * guess at EA size by just reading it from disk.
1572 * Exceptions are readdir and (missing) directory striping */
1574 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1575 /* No easy way to know how long is the symlink, but it cannot
1576 * be more than PATH_MAX, so we allocate +1 */
1578 /* A special case for fs ROOT: getattr there might fetch
1579 * default EA for entire fs, not just for this dir!
1581 } else if (lu_fid_eq(mdt_object_fid(obj),
1582 &info->mti_mdt->mdt_md_root_fid) &&
1583 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1584 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1586 /* Should the default strping be bigger, mdt_fix_reply
1587 * will reallocate */
1588 rc = DEF_REP_MD_SIZE;
1590 /* Read the actual EA size from disk */
1591 rc = mdt_attr_get_eabuf_size(info, obj);
1595 GOTO(out, rc = err_serious(rc));
1597 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1599 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1600 * by default. If the target object has more ACL entries, then
1601 * enlarge the buffer when necessary. */
1602 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1603 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1605 rc = req_capsule_server_pack(pill);
1606 if (unlikely(rc != 0))
1607 GOTO(out, rc = err_serious(rc));
1609 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1610 LASSERT(repbody != NULL);
1611 repbody->mbo_eadatasize = 0;
1612 repbody->mbo_aclsize = 0;
1614 rc = mdt_check_ucred(info);
1616 GOTO(out_shrink, rc);
1618 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1620 rc = mdt_getattr_internal(info, obj, 0);
1623 mdt_client_compatibility(info);
1624 rc2 = mdt_fix_reply(info);
1628 mdt_thread_info_fini(info);
1633 * Handler of layout intent RPC requiring the layout modification
1635 * \param[in] info thread environment
1636 * \param[in] obj object
1637 * \param[out] lhc object ldlm lock handle
1638 * \param[in] layout layout change descriptor
1640 * \retval 0 on success
1641 * \retval < 0 error code
1643 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1644 struct mdt_lock_handle *lhc,
1645 struct md_layout_change *layout)
1651 if (!mdt_object_exists(obj))
1654 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1657 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1662 rc = mdt_check_resent_lock(info, obj, lhc);
1668 __u64 lockpart = MDS_INODELOCK_LAYOUT;
1670 /* take layout lock to prepare layout change */
1671 if (layout->mlc_opc == MD_LAYOUT_WRITE)
1672 lockpart |= MDS_INODELOCK_UPDATE;
1674 mdt_lock_handle_init(lhc);
1675 mdt_lock_reg_init(lhc, LCK_EX);
1676 rc = mdt_reint_object_lock(info, obj, lhc, lockpart, false);
1681 mutex_lock(&obj->mot_som_mutex);
1682 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1683 mutex_unlock(&obj->mot_som_mutex);
1686 mdt_object_unlock(info, obj, lhc, 1);
1692 * Exchange MOF_LOV_CREATED flags between two objects after a
1693 * layout swap. No assumption is made on whether o1 or o2 have
1694 * created objects or not.
1696 * \param[in,out] o1 First swap layout object
1697 * \param[in,out] o2 Second swap layout object
1699 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1701 unsigned int o1_lov_created = o1->mot_lov_created;
1703 mutex_lock(&o1->mot_lov_mutex);
1704 mutex_lock(&o2->mot_lov_mutex);
1706 o1->mot_lov_created = o2->mot_lov_created;
1707 o2->mot_lov_created = o1_lov_created;
1709 mutex_unlock(&o2->mot_lov_mutex);
1710 mutex_unlock(&o1->mot_lov_mutex);
1713 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1715 struct mdt_thread_info *info;
1716 struct ptlrpc_request *req = tgt_ses_req(tsi);
1717 struct obd_export *exp = req->rq_export;
1718 struct mdt_object *o1, *o2, *o;
1719 struct mdt_lock_handle *lh1, *lh2;
1720 struct mdc_swap_layouts *msl;
1724 /* client does not support layout lock, so layout swaping
1726 * FIXME: there is a problem for old clients which don't support
1727 * layout lock yet. If those clients have already opened the file
1728 * they won't be notified at all so that old layout may still be
1729 * used to do IO. This can be fixed after file release is landed by
1730 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1731 if (!exp_connect_layout(exp))
1732 RETURN(-EOPNOTSUPP);
1734 info = tsi2mdt_info(tsi);
1735 if (unlikely(info->mti_object == NULL))
1738 if (info->mti_dlm_req != NULL)
1739 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1741 o1 = info->mti_object;
1742 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1743 &info->mti_body->mbo_fid2);
1745 GOTO(out, rc = PTR_ERR(o));
1747 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1748 GOTO(put, rc = -ENOENT);
1750 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1751 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1757 /* permission check. Make sure the calling process having permission
1758 * to write both files. */
1759 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1764 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1769 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1771 GOTO(put, rc = -EPROTO);
1773 lh1 = &info->mti_lh[MDT_LH_NEW];
1774 mdt_lock_reg_init(lh1, LCK_EX);
1775 lh2 = &info->mti_lh[MDT_LH_OLD];
1776 mdt_lock_reg_init(lh2, LCK_EX);
1778 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1779 MDS_INODELOCK_XATTR);
1783 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1784 MDS_INODELOCK_XATTR);
1788 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1789 mdt_object_child(o2), msl->msl_flags);
1793 mdt_swap_lov_flag(o1, o2);
1796 mdt_object_unlock(info, o2, lh2, rc);
1798 mdt_object_unlock(info, o1, lh1, rc);
1800 mdt_object_put(info->mti_env, o);
1802 mdt_thread_info_fini(info);
1806 static int mdt_raw_lookup(struct mdt_thread_info *info,
1807 struct mdt_object *parent,
1808 const struct lu_name *lname,
1809 struct ldlm_reply *ldlm_rep)
1811 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1815 LASSERT(!info->mti_cross_ref);
1817 /* Only got the fid of this obj by name */
1818 fid_zero(child_fid);
1819 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1820 lname, child_fid, &info->mti_spec);
1822 struct mdt_body *repbody;
1824 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1825 repbody->mbo_fid1 = *child_fid;
1826 repbody->mbo_valid = OBD_MD_FLID;
1827 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1828 } else if (rc == -ENOENT) {
1829 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1836 * UPDATE lock should be taken against parent, and be released before exit;
1837 * child_bits lock should be taken against child, and be returned back:
1838 * (1)normal request should release the child lock;
1839 * (2)intent request will grant the lock to client.
1841 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1842 struct mdt_lock_handle *lhc,
1844 struct ldlm_reply *ldlm_rep)
1846 struct ptlrpc_request *req = mdt_info_req(info);
1847 struct mdt_body *reqbody = NULL;
1848 struct mdt_object *parent = info->mti_object;
1849 struct mdt_object *child = NULL;
1850 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1851 struct lu_name *lname = NULL;
1852 struct mdt_lock_handle *lhp = NULL;
1853 struct ldlm_lock *lock;
1854 struct req_capsule *pill = info->mti_pill;
1862 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1863 LASSERT(ergo(is_resent,
1864 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1869 if (info->mti_cross_ref) {
1870 /* Only getattr on the child. Parent is on another node. */
1871 mdt_set_disposition(info, ldlm_rep,
1872 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1874 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1876 PFID(mdt_object_fid(child)), ldlm_rep);
1878 rc = mdt_check_resent_lock(info, child, lhc);
1881 } else if (rc > 0) {
1882 mdt_lock_handle_init(lhc);
1883 mdt_lock_reg_init(lhc, LCK_PR);
1886 * Object's name entry is on another MDS, it will
1887 * request PERM lock only because LOOKUP lock is owned
1888 * by the MDS where name entry resides.
1890 * TODO: it should try layout lock too. - Jinshan
1892 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1893 MDS_INODELOCK_LAYOUT);
1894 child_bits |= MDS_INODELOCK_PERM;
1896 rc = mdt_object_lock(info, child, lhc, child_bits);
1901 /* Finally, we can get attr for child. */
1902 if (!mdt_object_exists(child)) {
1903 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1905 "remote object doesn't exist.");
1906 mdt_object_unlock(info, child, lhc, 1);
1910 rc = mdt_getattr_internal(info, child, 0);
1911 if (unlikely(rc != 0)) {
1912 mdt_object_unlock(info, child, lhc, 1);
1916 rc = mdt_pack_secctx_in_reply(info, child);
1918 mdt_object_unlock(info, child, lhc, 1);
1922 rc = mdt_pack_encctx_in_reply(info, child);
1924 mdt_object_unlock(info, child, lhc, 1);
1928 lname = &info->mti_name;
1929 mdt_name_unpack(pill, &RMF_NAME, lname, MNF_FIX_ANON);
1931 if (lu_name_is_valid(lname)) {
1932 if (mdt_object_remote(parent)) {
1933 CERROR("%s: parent "DFID" is on remote target\n",
1934 mdt_obd_name(info->mti_mdt),
1935 PFID(mdt_object_fid(parent)));
1939 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1940 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1941 PNAME(lname), ldlm_rep);
1943 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1944 if (unlikely(reqbody == NULL))
1945 RETURN(err_serious(-EPROTO));
1947 *child_fid = reqbody->mbo_fid2;
1948 if (unlikely(!fid_is_sane(child_fid)))
1949 RETURN(err_serious(-EINVAL));
1951 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1952 mdt_object_get(info->mti_env, parent);
1955 child = mdt_object_find(info->mti_env, info->mti_mdt,
1958 RETURN(PTR_ERR(child));
1961 if (mdt_object_remote(child)) {
1962 CERROR("%s: child "DFID" is on remote target\n",
1963 mdt_obd_name(info->mti_mdt),
1964 PFID(mdt_object_fid(child)));
1965 GOTO(out_child, rc = -EPROTO);
1968 /* don't fetch LOOKUP lock if it's remote object */
1969 rc = mdt_is_remote_object(info, parent, child);
1971 GOTO(out_child, rc);
1973 child_bits &= ~MDS_INODELOCK_LOOKUP;
1975 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1977 PFID(mdt_object_fid(parent)),
1978 PFID(&reqbody->mbo_fid2), ldlm_rep);
1981 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1983 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1984 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1986 "Parent doesn't exist!");
1987 GOTO(out_child, rc = -ESTALE);
1990 if (lu_name_is_valid(lname)) {
1991 /* Always allow to lookup ".." */
1992 if (unlikely(lname->ln_namelen == 2 &&
1993 lname->ln_name[0] == '.' &&
1994 lname->ln_name[1] == '.'))
1995 info->mti_spec.sp_permitted = 1;
1997 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1998 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
2003 /* step 1: lock parent only if parent is a directory */
2004 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
2005 lhp = &info->mti_lh[MDT_LH_PARENT];
2006 mdt_lock_pdo_init(lhp, LCK_PR, lname);
2007 rc = mdt_object_lock(info, parent, lhp,
2008 MDS_INODELOCK_UPDATE);
2009 if (unlikely(rc != 0))
2013 /* step 2: lookup child's fid by name */
2014 fid_zero(child_fid);
2015 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
2016 child_fid, &info->mti_spec);
2018 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
2021 GOTO(unlock_parent, rc);
2023 child = mdt_object_find(info->mti_env, info->mti_mdt,
2025 if (unlikely(IS_ERR(child)))
2026 GOTO(unlock_parent, rc = PTR_ERR(child));
2029 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
2031 /* step 3: lock child regardless if it is local or remote. */
2034 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
2035 if (!mdt_object_exists(child)) {
2036 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
2038 "Object doesn't exist!");
2039 GOTO(out_child, rc = -ENOENT);
2042 rc = mdt_check_resent_lock(info, child, lhc);
2044 GOTO(out_child, rc);
2045 } else if (rc > 0) {
2046 mdt_lock_handle_init(lhc);
2047 mdt_lock_reg_init(lhc, LCK_PR);
2049 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
2050 !mdt_object_remote(child)) {
2051 struct md_attr *ma = &info->mti_attr;
2054 ma->ma_need = MA_INODE;
2055 rc = mdt_attr_get_complex(info, child, ma);
2056 if (unlikely(rc != 0))
2057 GOTO(out_child, rc);
2059 /* If the file has not been changed for some time, we
2060 * return not only a LOOKUP lock, but also an UPDATE
2061 * lock and this might save us RPC on later STAT. For
2062 * directories, it also let negative dentry cache start
2063 * working for this dir. */
2064 if (ma->ma_valid & MA_INODE &&
2065 ma->ma_attr.la_valid & LA_CTIME &&
2066 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
2067 ma->ma_attr.la_ctime < ktime_get_real_seconds())
2068 child_bits |= MDS_INODELOCK_UPDATE;
2071 /* layout lock must be granted in a best-effort way
2072 * for IT operations */
2073 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
2074 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2075 !mdt_object_remote(child) && ldlm_rep != NULL) {
2076 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
2077 exp_connect_layout(info->mti_exp)) {
2078 /* try to grant layout lock for regular file. */
2079 try_bits = MDS_INODELOCK_LAYOUT;
2081 /* Acquire DOM lock in advance for data-on-mdt file */
2082 if (child != parent)
2083 try_bits |= MDS_INODELOCK_DOM;
2086 if (try_bits != 0) {
2087 /* try layout lock, it may fail to be granted due to
2088 * contention at LOOKUP or UPDATE */
2089 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
2091 if (child_bits & MDS_INODELOCK_LAYOUT)
2094 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
2095 * client will enqueue the lock to the remote MDT */
2096 if (mdt_object_remote(child))
2097 child_bits &= ~MDS_INODELOCK_UPDATE;
2098 rc = mdt_object_lock(info, child, lhc, child_bits);
2100 if (unlikely(rc != 0))
2101 GOTO(out_child, rc);
2104 /* finally, we can get attr for child. */
2105 rc = mdt_getattr_internal(info, child, ma_need);
2106 if (unlikely(rc != 0)) {
2107 mdt_object_unlock(info, child, lhc, 1);
2108 GOTO(out_child, rc);
2111 rc = mdt_pack_secctx_in_reply(info, child);
2113 mdt_object_unlock(info, child, lhc, 1);
2114 GOTO(out_child, rc);
2117 rc = mdt_pack_encctx_in_reply(info, child);
2119 mdt_object_unlock(info, child, lhc, 1);
2120 GOTO(out_child, rc);
2123 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2125 /* Debugging code. */
2126 LDLM_DEBUG(lock, "Returning lock to client");
2127 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
2128 &lock->l_resource->lr_name),
2129 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
2130 PLDLMRES(lock->l_resource),
2131 PFID(mdt_object_fid(child)));
2133 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
2134 !mdt_object_remote(child) && child != parent) {
2135 mdt_object_put(info->mti_env, child);
2136 rc = mdt_pack_size2body(info, child_fid,
2138 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
2139 /* DOM lock was taken in advance but this is
2140 * not DoM file. Drop the lock.
2142 lock_res_and_lock(lock);
2143 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
2144 unlock_res_and_lock(lock);
2146 LDLM_LOCK_PUT(lock);
2147 GOTO(unlock_parent, rc = 0);
2149 LDLM_LOCK_PUT(lock);
2155 mdt_object_put(info->mti_env, child);
2158 mdt_object_unlock(info, parent, lhp, 1);
2162 /* normal handler: should release the child lock */
2163 static int mdt_getattr_name(struct tgt_session_info *tsi)
2165 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2166 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
2167 struct mdt_body *reqbody;
2168 struct mdt_body *repbody;
2173 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
2174 LASSERT(reqbody != NULL);
2175 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
2176 LASSERT(repbody != NULL);
2178 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
2179 repbody->mbo_eadatasize = 0;
2180 repbody->mbo_aclsize = 0;
2182 rc = mdt_init_ucred(info, reqbody);
2184 GOTO(out_shrink, rc);
2186 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
2187 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
2188 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
2189 lhc->mlh_reg_lh.cookie = 0;
2191 mdt_exit_ucred(info);
2194 mdt_client_compatibility(info);
2195 rc2 = mdt_fix_reply(info);
2198 mdt_thread_info_fini(info);
2202 static int mdt_rmfid_unlink(struct mdt_thread_info *info,
2203 const struct lu_fid *pfid,
2204 const struct lu_name *name,
2205 struct mdt_object *obj, s64 ctime)
2207 struct lu_fid *child_fid = &info->mti_tmp_fid1;
2208 struct ldlm_enqueue_info *einfo = &info->mti_einfo[0];
2209 struct mdt_device *mdt = info->mti_mdt;
2210 struct md_attr *ma = &info->mti_attr;
2211 struct mdt_lock_handle *parent_lh;
2212 struct mdt_lock_handle *child_lh;
2213 struct mdt_object *pobj;
2214 bool cos_incompat = false;
2218 pobj = mdt_object_find(info->mti_env, mdt, pfid);
2220 GOTO(out, rc = PTR_ERR(pobj));
2222 parent_lh = &info->mti_lh[MDT_LH_PARENT];
2223 mdt_lock_pdo_init(parent_lh, LCK_PW, name);
2224 rc = mdt_object_lock(info, pobj, parent_lh, MDS_INODELOCK_UPDATE);
2226 GOTO(put_parent, rc);
2228 if (mdt_object_remote(pobj))
2229 cos_incompat = true;
2231 rc = mdo_lookup(info->mti_env, mdt_object_child(pobj),
2232 name, child_fid, &info->mti_spec);
2234 GOTO(unlock_parent, rc);
2236 if (!lu_fid_eq(child_fid, mdt_object_fid(obj)))
2237 GOTO(unlock_parent, rc = -EREMCHG);
2239 child_lh = &info->mti_lh[MDT_LH_CHILD];
2240 mdt_lock_reg_init(child_lh, LCK_EX);
2241 rc = mdt_reint_striped_lock(info, obj, child_lh,
2242 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE,
2243 einfo, cos_incompat);
2245 GOTO(unlock_parent, rc);
2247 if (atomic_read(&obj->mot_open_count)) {
2248 CDEBUG(D_OTHER, "object "DFID" open, skip\n",
2249 PFID(mdt_object_fid(obj)));
2250 GOTO(unlock_child, rc = -EBUSY);
2254 ma->ma_valid = MA_INODE;
2255 ma->ma_attr.la_valid = LA_CTIME;
2256 ma->ma_attr.la_ctime = ctime;
2258 mutex_lock(&obj->mot_lov_mutex);
2260 rc = mdo_unlink(info->mti_env, mdt_object_child(pobj),
2261 mdt_object_child(obj), name, ma, 0);
2263 mutex_unlock(&obj->mot_lov_mutex);
2266 mdt_reint_striped_unlock(info, obj, child_lh, einfo, 1);
2268 mdt_object_unlock(info, pobj, parent_lh, 1);
2270 mdt_object_put(info->mti_env, pobj);
2275 static int mdt_rmfid_check_permission(struct mdt_thread_info *info,
2276 struct mdt_object *obj)
2278 struct lu_ucred *uc = lu_ucred(info->mti_env);
2279 struct md_attr *ma = &info->mti_attr;
2280 struct lu_attr *la = &ma->ma_attr;
2284 ma->ma_need = MA_INODE;
2285 rc = mo_attr_get(info->mti_env, mdt_object_child(obj), ma);
2289 if (la->la_flags & LUSTRE_IMMUTABLE_FL)
2292 if (md_capable(uc, CAP_DAC_OVERRIDE))
2294 if (uc->uc_fsuid == la->la_uid) {
2295 if ((la->la_mode & S_IWUSR) == 0)
2297 } else if (uc->uc_fsgid == la->la_gid) {
2298 if ((la->la_mode & S_IWGRP) == 0)
2300 } else if ((la->la_mode & S_IWOTH) == 0) {
2308 static int mdt_rmfid_one(struct mdt_thread_info *info, struct lu_fid *fid,
2311 struct mdt_device *mdt = info->mti_mdt;
2312 struct mdt_object *obj = NULL;
2313 struct linkea_data ldata = { NULL };
2314 struct lu_buf *buf = &info->mti_big_buf;
2315 struct lu_name *name = &info->mti_name;
2316 struct lu_fid *pfid = &info->mti_tmp_fid1;
2317 struct link_ea_header *leh;
2318 struct link_ea_entry *lee;
2319 int reclen, count, rc = 0;
2322 if (!fid_is_sane(fid))
2323 GOTO(out, rc = -EINVAL);
2325 if (!fid_is_namespace_visible(fid))
2326 GOTO(out, rc = -EINVAL);
2328 obj = mdt_object_find(info->mti_env, mdt, fid);
2330 GOTO(out, rc = PTR_ERR(obj));
2332 if (mdt_object_remote(obj))
2333 GOTO(out, rc = -EREMOTE);
2334 if (!mdt_object_exists(obj) || lu_object_is_dying(&obj->mot_header))
2335 GOTO(out, rc = -ENOENT);
2337 rc = mdt_rmfid_check_permission(info, obj);
2342 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
2344 GOTO(out, rc = -ENOMEM);
2347 rc = mdt_links_read(info, obj, &ldata);
2352 lee = (struct link_ea_entry *)(leh + 1);
2353 for (count = 0; count < leh->leh_reccount; count++) {
2354 /* remove every hardlink */
2355 linkea_entry_unpack(lee, &reclen, name, pfid);
2356 lee = (struct link_ea_entry *) ((char *)lee + reclen);
2357 rc = mdt_rmfid_unlink(info, pfid, name, obj, ctime);
2363 if (obj && !IS_ERR(obj))
2364 mdt_object_put(info->mti_env, obj);
2365 if (info->mti_big_buf.lb_buf)
2366 lu_buf_free(&info->mti_big_buf);
2371 static int mdt_rmfid(struct tgt_session_info *tsi)
2373 struct mdt_thread_info *mti = tsi2mdt_info(tsi);
2374 struct mdt_body *reqbody;
2375 struct lu_fid *fids, *rfids;
2381 reqbody = req_capsule_client_get(tsi->tsi_pill, &RMF_MDT_BODY);
2382 if (reqbody == NULL)
2384 bufsize = req_capsule_get_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2386 nr = bufsize / sizeof(struct lu_fid);
2387 if (nr * sizeof(struct lu_fid) != bufsize)
2389 req_capsule_set_size(tsi->tsi_pill, &RMF_RCS,
2390 RCL_SERVER, nr * sizeof(__u32));
2391 req_capsule_set_size(tsi->tsi_pill, &RMF_FID_ARRAY,
2392 RCL_SERVER, nr * sizeof(struct lu_fid));
2393 rc = req_capsule_server_pack(tsi->tsi_pill);
2395 GOTO(out, rc = err_serious(rc));
2396 fids = req_capsule_client_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2399 rcs = req_capsule_server_get(tsi->tsi_pill, &RMF_RCS);
2401 rfids = req_capsule_server_get(tsi->tsi_pill, &RMF_FID_ARRAY);
2404 mdt_init_ucred(mti, reqbody);
2405 for (i = 0; i < nr; i++) {
2407 rcs[i] = mdt_rmfid_one(mti, fids + i, reqbody->mbo_ctime);
2409 mdt_exit_ucred(mti);
2415 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2416 void *karg, void __user *uarg);
2418 static int mdt_set_info(struct tgt_session_info *tsi)
2420 struct ptlrpc_request *req = tgt_ses_req(tsi);
2423 int keylen, vallen, rc = 0;
2427 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
2429 DEBUG_REQ(D_HA, req, "no set_info key");
2430 RETURN(err_serious(-EFAULT));
2433 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
2436 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
2438 DEBUG_REQ(D_HA, req, "no set_info val");
2439 RETURN(err_serious(-EFAULT));
2442 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
2445 /* Swab any part of val you need to here */
2446 if (KEY_IS(KEY_READ_ONLY)) {
2447 spin_lock(&req->rq_export->exp_lock);
2449 *exp_connect_flags_ptr(req->rq_export) |=
2452 *exp_connect_flags_ptr(req->rq_export) &=
2453 ~OBD_CONNECT_RDONLY;
2454 spin_unlock(&req->rq_export->exp_lock);
2455 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2456 struct changelog_setinfo *cs = val;
2458 if (vallen != sizeof(*cs)) {
2459 CERROR("%s: bad changelog_clear setinfo size %d\n",
2460 tgt_name(tsi->tsi_tgt), vallen);
2463 if (ptlrpc_req_need_swab(req)) {
2464 __swab64s(&cs->cs_recno);
2465 __swab32s(&cs->cs_id);
2468 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
2470 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
2472 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
2474 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
2481 static int mdt_readpage(struct tgt_session_info *tsi)
2483 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
2484 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
2485 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
2486 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
2487 struct mdt_body *repbody;
2493 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
2494 RETURN(err_serious(-ENOMEM));
2496 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
2497 if (repbody == NULL || reqbody == NULL)
2498 RETURN(err_serious(-EFAULT));
2501 * prepare @rdpg before calling lower layers and transfer itself. Here
2502 * reqbody->size contains offset of where to start to read and
2503 * reqbody->nlink contains number bytes to read.
2505 rdpg->rp_hash = reqbody->mbo_size;
2506 if (rdpg->rp_hash != reqbody->mbo_size) {
2507 CERROR("Invalid hash: %#llx != %#llx\n",
2508 rdpg->rp_hash, reqbody->mbo_size);
2512 rdpg->rp_attrs = reqbody->mbo_mode;
2513 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
2514 rdpg->rp_attrs |= LUDA_64BITHASH;
2515 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
2516 exp_max_brw_size(tsi->tsi_exp));
2517 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
2519 OBD_ALLOC_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2520 if (rdpg->rp_pages == NULL)
2523 for (i = 0; i < rdpg->rp_npages; ++i) {
2524 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
2525 if (rdpg->rp_pages[i] == NULL)
2526 GOTO(free_rdpg, rc = -ENOMEM);
2529 /* call lower layers to fill allocated pages with directory data */
2530 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
2532 GOTO(free_rdpg, rc);
2534 /* send pages to client */
2535 rc = tgt_sendpage(tsi, rdpg, rc);
2540 for (i = 0; i < rdpg->rp_npages; i++)
2541 if (rdpg->rp_pages[i] != NULL)
2542 __free_page(rdpg->rp_pages[i]);
2543 OBD_FREE_PTR_ARRAY_LARGE(rdpg->rp_pages, rdpg->rp_npages);
2545 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2551 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2553 struct lu_ucred *uc = mdt_ucred_check(info);
2554 struct lu_attr *attr = &info->mti_attr.ma_attr;
2559 if (op != REINT_SETATTR) {
2560 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2561 attr->la_uid = uc->uc_fsuid;
2562 /* for S_ISGID, inherit gid from his parent, such work will be
2563 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2564 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2565 attr->la_gid = uc->uc_fsgid;
2571 static inline bool mdt_is_readonly_open(struct mdt_thread_info *info, __u32 op)
2573 return op == REINT_OPEN &&
2574 !(info->mti_spec.sp_cr_flags & (MDS_FMODE_WRITE | MDS_OPEN_CREAT));
2577 static void mdt_preset_secctx_size(struct mdt_thread_info *info)
2579 struct req_capsule *pill = info->mti_pill;
2581 if (req_capsule_has_field(pill, &RMF_FILE_SECCTX,
2583 req_capsule_has_field(pill, &RMF_FILE_SECCTX_NAME,
2585 if (req_capsule_get_size(pill, &RMF_FILE_SECCTX_NAME,
2587 /* pre-set size in server part with max size */
2588 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2590 OBD_MAX_DEFAULT_EA_SIZE);
2592 req_capsule_set_size(pill, &RMF_FILE_SECCTX,
2597 static void mdt_preset_encctx_size(struct mdt_thread_info *info)
2599 struct req_capsule *pill = info->mti_pill;
2601 if (req_capsule_has_field(pill, &RMF_FILE_ENCCTX,
2603 /* pre-set size in server part with max size */
2604 req_capsule_set_size(pill, &RMF_FILE_ENCCTX,
2606 info->mti_mdt->mdt_max_mdsize);
2609 static int mdt_reint_internal(struct mdt_thread_info *info,
2610 struct mdt_lock_handle *lhc,
2613 struct req_capsule *pill = info->mti_pill;
2614 struct mdt_body *repbody;
2619 rc = mdt_reint_unpack(info, op);
2621 CERROR("Can't unpack reint, rc %d\n", rc);
2622 RETURN(err_serious(rc));
2626 /* check if the file system is set to readonly. O_RDONLY open
2627 * is still allowed even the file system is set to readonly mode */
2628 if (mdt_rdonly(info->mti_exp) && !mdt_is_readonly_open(info, op))
2629 RETURN(err_serious(-EROFS));
2631 /* for replay (no_create) lmm is not needed, client has it already */
2632 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2633 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2636 /* llog cookies are always 0, the field is kept for compatibility */
2637 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2638 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2640 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2641 * by default. If the target object has more ACL entries, then
2642 * enlarge the buffer when necessary. */
2643 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2644 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2645 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2647 mdt_preset_secctx_size(info);
2648 mdt_preset_encctx_size(info);
2650 rc = req_capsule_server_pack(pill);
2652 CERROR("Can't pack response, rc %d\n", rc);
2653 RETURN(err_serious(rc));
2656 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2657 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2659 repbody->mbo_eadatasize = 0;
2660 repbody->mbo_aclsize = 0;
2663 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2665 /* for replay no cookkie / lmm need, because client have this already */
2666 if (info->mti_spec.no_create)
2667 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2668 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2670 rc = mdt_init_ucred_reint(info);
2672 GOTO(out_shrink, rc);
2674 rc = mdt_fix_attr_ucred(info, op);
2676 GOTO(out_ucred, rc = err_serious(rc));
2678 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2680 GOTO(out_ucred, rc);
2681 } else if (rc == 1) {
2682 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt");
2683 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2684 GOTO(out_ucred, rc);
2686 rc = mdt_reint_rec(info, lhc);
2689 mdt_exit_ucred(info);
2691 mdt_client_compatibility(info);
2693 rc2 = mdt_fix_reply(info);
2698 * Data-on-MDT optimization - read data along with OPEN and return it
2699 * in reply when possible.
2701 if (rc == 0 && op == REINT_OPEN && !req_is_replay(pill->rc_req))
2702 rc = mdt_dom_read_on_open(info, info->mti_mdt,
2708 static long mdt_reint_opcode(struct ptlrpc_request *req,
2709 const struct req_format **fmt)
2711 struct mdt_device *mdt;
2712 struct mdt_rec_reint *rec;
2715 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2717 opc = rec->rr_opcode;
2718 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2719 if (opc < REINT_MAX && fmt[opc] != NULL)
2720 req_capsule_extend(&req->rq_pill, fmt[opc]);
2722 mdt = mdt_exp2dev(req->rq_export);
2723 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2724 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2725 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2726 opc = err_serious(-EFAULT);
2729 opc = err_serious(-EFAULT);
2734 static int mdt_reint(struct tgt_session_info *tsi)
2738 static const struct req_format *reint_fmts[REINT_MAX] = {
2739 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2740 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2741 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2742 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2743 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2744 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2745 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2746 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2747 [REINT_MIGRATE] = &RQF_MDS_REINT_MIGRATE,
2748 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2753 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2755 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2757 * No lock possible here from client to pass it to reint code
2760 rc = mdt_reint_internal(info, NULL, opc);
2761 mdt_thread_info_fini(info);
2766 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2770 /* this should sync the whole device */
2771 int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2773 struct dt_device *dt = mdt->mdt_bottom;
2777 rc = dt->dd_ops->dt_sync(env, dt);
2781 /* this should sync this object */
2782 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2783 struct mdt_object *mo)
2789 if (!mdt_object_exists(mo)) {
2790 CWARN("%s: non existing object "DFID": rc = %d\n",
2791 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2796 if (S_ISREG(lu_object_attr(&mo->mot_obj))) {
2797 struct lu_target *tgt = tgt_ses_info(env)->tsi_tgt;
2798 dt_obj_version_t version;
2800 version = dt_version_get(env, mdt_obj2dt(mo));
2801 if (version > tgt->lut_obd->obd_last_committed)
2802 rc = mo_object_sync(env, mdt_object_child(mo));
2804 rc = mo_object_sync(env, mdt_object_child(mo));
2810 static int mdt_sync(struct tgt_session_info *tsi)
2812 struct ptlrpc_request *req = tgt_ses_req(tsi);
2813 struct req_capsule *pill = tsi->tsi_pill;
2814 struct mdt_body *body;
2815 ktime_t kstart = ktime_get();
2820 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2821 RETURN(err_serious(-ENOMEM));
2823 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2824 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2826 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2828 if (unlikely(info->mti_object == NULL))
2831 /* sync an object */
2832 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2835 const struct lu_fid *fid;
2836 struct lu_attr *la = &info->mti_attr.ma_attr;
2838 info->mti_attr.ma_need = MA_INODE;
2839 info->mti_attr.ma_valid = 0;
2840 rc = mdt_attr_get_complex(info, info->mti_object,
2843 body = req_capsule_server_get(pill,
2845 fid = mdt_object_fid(info->mti_object);
2846 mdt_pack_attr2body(info, body, la, fid);
2849 mdt_thread_info_fini(info);
2852 mdt_counter_incr(req, LPROC_MDT_SYNC,
2853 ktime_us_delta(ktime_get(), kstart));
2858 static int mdt_data_sync(struct tgt_session_info *tsi)
2860 struct mdt_thread_info *info;
2861 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2862 struct ost_body *body = tsi->tsi_ost_body;
2863 struct ost_body *repbody;
2864 struct mdt_object *mo = NULL;
2870 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2872 /* if no fid is specified then do nothing,
2873 * device sync is done via MDS_SYNC */
2874 if (fid_is_zero(&tsi->tsi_fid))
2877 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2879 RETURN(PTR_ERR(mo));
2881 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2885 repbody->oa.o_oi = body->oa.o_oi;
2886 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2888 info = tsi2mdt_info(tsi);
2889 ma = &info->mti_attr;
2890 ma->ma_need = MA_INODE;
2892 rc = mdt_attr_get_complex(info, mo, ma);
2894 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2897 mdt_thread_info_fini(info);
2902 mdt_object_put(tsi->tsi_env, mo);
2907 * Handle quota control requests to consult current usage/limit, but also
2908 * to configure quota enforcement
2910 static int mdt_quotactl(struct tgt_session_info *tsi)
2912 struct obd_export *exp = tsi->tsi_exp;
2913 struct req_capsule *pill = tsi->tsi_pill;
2914 struct obd_quotactl *oqctl, *repoqc;
2916 struct mdt_device *mdt = mdt_exp2dev(exp);
2917 struct lu_device *qmt = mdt->mdt_qmt_dev;
2918 struct lu_nodemap *nodemap;
2921 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2923 RETURN(err_serious(-EPROTO));
2925 rc = req_capsule_server_pack(pill);
2927 RETURN(err_serious(rc));
2929 nodemap = nodemap_get_from_exp(exp);
2930 if (IS_ERR(nodemap))
2931 RETURN(PTR_ERR(nodemap));
2933 switch (oqctl->qc_cmd) {
2934 /* master quotactl */
2937 case LUSTRE_Q_SETDEFAULT:
2938 case LUSTRE_Q_SETQUOTAPOOL:
2939 case LUSTRE_Q_SETINFOPOOL:
2940 if (!nodemap_can_setquota(nodemap))
2941 GOTO(out_nodemap, rc = -EPERM);
2945 case LUSTRE_Q_GETDEFAULT:
2946 case LUSTRE_Q_GETQUOTAPOOL:
2947 case LUSTRE_Q_GETINFOPOOL:
2949 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2950 /* slave quotactl */
2957 CERROR("%s: unsupported quotactl command %d: rc = %d\n",
2958 mdt_obd_name(mdt), oqctl->qc_cmd, rc);
2959 GOTO(out_nodemap, rc);
2963 switch (oqctl->qc_type) {
2965 id = nodemap_map_id(nodemap, NODEMAP_UID,
2966 NODEMAP_CLIENT_TO_FS, id);
2969 id = nodemap_map_id(nodemap, NODEMAP_GID,
2970 NODEMAP_CLIENT_TO_FS, id);
2973 /* todo: check/map project id */
2977 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2979 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2981 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2983 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2984 barrier_exit(tsi->tsi_tgt->lut_bottom);
2986 if (oqctl->qc_id != id)
2987 swap(oqctl->qc_id, id);
2989 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2990 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2991 RETURN(-EINPROGRESS);
2994 switch (oqctl->qc_cmd) {
3000 case LUSTRE_Q_SETDEFAULT:
3001 case LUSTRE_Q_GETDEFAULT:
3002 case LUSTRE_Q_SETQUOTAPOOL:
3003 case LUSTRE_Q_GETQUOTAPOOL:
3004 case LUSTRE_Q_SETINFOPOOL:
3005 case LUSTRE_Q_GETINFOPOOL:
3006 /* forward quotactl request to QMT */
3007 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
3012 /* slave quotactl */
3013 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
3018 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
3019 GOTO(out_nodemap, rc = -EFAULT);
3022 if (oqctl->qc_id != id)
3023 swap(oqctl->qc_id, id);
3025 QCTL_COPY(repoqc, oqctl);
3029 nodemap_putref(nodemap);
3034 /** clone llog ctxt from child (mdd)
3035 * This allows remote llog (replicator) access.
3036 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
3037 * context was originally set up, or we can handle them directly.
3038 * I choose the latter, but that means I need any llog
3039 * contexts set up by child to be accessable by the mdt. So we clone the
3040 * context into our context list here.
3042 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
3045 struct md_device *next = mdt->mdt_child;
3046 struct llog_ctxt *ctxt;
3049 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
3052 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
3053 if (rc || ctxt == NULL) {
3057 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
3059 CERROR("Can't set mdt ctxt %d\n", rc);
3064 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
3065 struct mdt_device *mdt, int idx)
3067 struct llog_ctxt *ctxt;
3069 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
3072 /* Put once for the get we just did, and once for the clone */
3073 llog_ctxt_put(ctxt);
3074 llog_ctxt_put(ctxt);
3079 * sec context handlers
3081 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
3083 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
3089 * quota request handlers
3091 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
3093 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
3094 struct lu_device *qmt = mdt->mdt_qmt_dev;
3099 RETURN(err_serious(-EOPNOTSUPP));
3101 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
3105 struct mdt_object *mdt_object_new(const struct lu_env *env,
3106 struct mdt_device *d,
3107 const struct lu_fid *f)
3109 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
3110 struct lu_object *o;
3111 struct mdt_object *m;
3114 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
3115 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
3116 if (unlikely(IS_ERR(o)))
3117 m = (struct mdt_object *)o;
3123 struct mdt_object *mdt_object_find(const struct lu_env *env,
3124 struct mdt_device *d,
3125 const struct lu_fid *f)
3127 struct lu_object *o;
3128 struct mdt_object *m;
3131 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
3132 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
3133 if (unlikely(IS_ERR(o)))
3134 m = (struct mdt_object *)o;
3142 * Asyncronous commit for mdt device.
3144 * Pass asynchonous commit call down the MDS stack.
3146 * \param env environment
3147 * \param mdt the mdt device
3149 static void mdt_device_commit_async(const struct lu_env *env,
3150 struct mdt_device *mdt)
3152 struct dt_device *dt = mdt->mdt_bottom;
3156 rc = dt->dd_ops->dt_commit_async(env, dt);
3157 if (unlikely(rc != 0))
3158 CWARN("%s: async commit start failed: rc = %d\n",
3159 mdt_obd_name(mdt), rc);
3160 atomic_inc(&mdt->mdt_async_commit_count);
3165 * Mark the lock as "synchonous".
3167 * Mark the lock to deffer transaction commit to the unlock time.
3169 * \param lock the lock to mark as "synchonous"
3171 * \see mdt_is_lock_sync
3172 * \see mdt_save_lock
3174 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
3176 lock->l_ast_data = (void*)1;
3180 * Check whehter the lock "synchonous" or not.
3182 * \param lock the lock to check
3183 * \retval 1 the lock is "synchonous"
3184 * \retval 0 the lock isn't "synchronous"
3186 * \see mdt_set_lock_sync
3187 * \see mdt_save_lock
3189 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
3191 return lock->l_ast_data != NULL;
3195 * Blocking AST for mdt locks.
3197 * Starts transaction commit if in case of COS lock conflict or
3198 * deffers such a commit to the mdt_save_lock.
3200 * \param lock the lock which blocks a request or cancelling lock
3201 * \param desc unused
3202 * \param data unused
3203 * \param flag indicates whether this cancelling or blocking callback
3205 * \see ldlm_blocking_ast_nocheck
3207 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3208 void *data, int flag)
3210 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3211 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
3212 struct ldlm_cb_set_arg *arg = data;
3213 bool commit_async = false;
3217 if (flag == LDLM_CB_CANCELING)
3220 lock_res_and_lock(lock);
3221 if (lock->l_blocking_ast != mdt_blocking_ast) {
3222 unlock_res_and_lock(lock);
3226 /* A blocking ast may be sent from ldlm_lock_decref_internal
3227 * when the last reference to a local lock was released and
3228 * during blocking event from ldlm_work_bl_ast_lock().
3229 * The 'data' parameter is l_ast_data in the first case and
3230 * callback arguments in the second one. Distinguish them by that.
3232 if (!data || data == lock->l_ast_data || !arg->bl_desc)
3233 goto skip_cos_checks;
3235 if (lock->l_req_mode & (LCK_PW | LCK_EX)) {
3236 if (mdt_cos_is_enabled(mdt)) {
3237 if (!arg->bl_desc->bl_same_client)
3238 mdt_set_lock_sync(lock);
3239 } else if (mdt_slc_is_enabled(mdt) &&
3240 arg->bl_desc->bl_cos_incompat) {
3241 mdt_set_lock_sync(lock);
3243 * we may do extra commit here, but there is a small
3244 * window to miss a commit: lock was unlocked (saved),
3245 * then a conflict lock queued and we come here, but
3246 * REP-ACK not received, so lock was not converted to
3248 * Fortunately this window is quite small, so the
3249 * extra commit should be rare (not to say distributed
3250 * operation is rare too).
3252 commit_async = true;
3254 } else if (lock->l_req_mode == LCK_COS) {
3255 commit_async = true;
3259 rc = ldlm_blocking_ast_nocheck(lock);
3264 rc = lu_env_init(&env, LCT_LOCAL);
3265 if (unlikely(rc != 0))
3266 CWARN("%s: lu_env initialization failed, cannot "
3267 "start asynchronous commit: rc = %d\n",
3270 mdt_device_commit_async(&env, mdt);
3277 * Blocking AST for cross-MDT lock
3279 * Discard lock from uncommitted_slc_locks and cancel it.
3281 * \param lock the lock which blocks a request or cancelling lock
3282 * \param desc unused
3283 * \param data unused
3284 * \param flag indicates whether this cancelling or blocking callback
3285 * \retval 0 on success
3286 * \retval negative number on error
3288 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
3289 void *data, int flag)
3295 case LDLM_CB_BLOCKING: {
3296 struct lustre_handle lockh;
3298 ldlm_lock2handle(lock, &lockh);
3299 rc = ldlm_cli_cancel(&lockh,
3300 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
3302 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
3307 case LDLM_CB_CANCELING: {
3308 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
3309 struct mdt_device *mdt =
3310 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
3312 LDLM_DEBUG(lock, "Revoke remote lock\n");
3314 /* discard slc lock here so that it can be cleaned anytime,
3315 * especially for cleanup_resource() */
3316 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
3318 /* once we cache lock, l_ast_data is set to mdt_object */
3319 if (lock->l_ast_data != NULL) {
3320 struct mdt_object *mo = lock->l_ast_data;
3323 rc = lu_env_init(&env, LCT_MD_THREAD);
3324 if (unlikely(rc != 0)) {
3325 CWARN("%s: lu_env initialization failed, object %p "DFID" is leaked!: rc = %d\n",
3327 PFID(mdt_object_fid(mo)), rc);
3331 if (lock->l_policy_data.l_inodebits.bits &
3332 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
3333 rc = mo_invalidate(&env, mdt_object_child(mo));
3334 mo->mot_cache_attr = 0;
3336 mdt_object_put(&env, mo);
3348 int mdt_check_resent_lock(struct mdt_thread_info *info,
3349 struct mdt_object *mo,
3350 struct mdt_lock_handle *lhc)
3352 /* the lock might already be gotten in ldlm_handle_enqueue() */
3353 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
3354 struct ptlrpc_request *req = mdt_info_req(info);
3355 struct ldlm_lock *lock;
3357 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
3358 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
3360 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3361 * a resend case, however, it could be already destroyed
3362 * due to client eviction or a raced cancel RPC. */
3363 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
3364 lhc->mlh_reg_lh.cookie);
3368 if (!fid_res_name_eq(mdt_object_fid(mo),
3369 &lock->l_resource->lr_name)) {
3370 CWARN("%s: Although resent, but still not "
3371 "get child lock:"DFID"\n",
3372 info->mti_exp->exp_obd->obd_name,
3373 PFID(mdt_object_fid(mo)));
3374 LDLM_LOCK_PUT(lock);
3377 LDLM_LOCK_PUT(lock);
3383 static void mdt_remote_object_lock_created_cb(struct ldlm_lock *lock)
3385 mdt_object_get(NULL, lock->l_ast_data);
3388 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
3389 struct mdt_object *o, const struct lu_fid *fid,
3390 struct lustre_handle *lh, enum ldlm_mode mode,
3391 __u64 *ibits, __u64 trybits, bool cache)
3393 struct ldlm_enqueue_info *einfo = &mti->mti_remote_einfo;
3394 union ldlm_policy_data *policy = &mti->mti_policy;
3395 struct ldlm_res_id *res_id = &mti->mti_res_id;
3399 LASSERT(mdt_object_remote(o));
3401 fid_build_reg_res_name(fid, res_id);
3403 memset(einfo, 0, sizeof(*einfo));
3404 einfo->ei_type = LDLM_IBITS;
3405 einfo->ei_mode = mode;
3406 einfo->ei_cb_bl = mdt_remote_blocking_ast;
3407 einfo->ei_cb_cp = ldlm_completion_ast;
3408 einfo->ei_enq_slave = 0;
3409 einfo->ei_res_id = res_id;
3413 * if we cache lock, couple lock with mdt_object, so that object
3414 * can be easily found in lock ASTs.
3416 einfo->ei_cbdata = o;
3417 einfo->ei_cb_created = mdt_remote_object_lock_created_cb;
3420 memset(policy, 0, sizeof(*policy));
3421 policy->l_inodebits.bits = *ibits;
3422 policy->l_inodebits.try_bits = trybits;
3424 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
3427 /* Return successfully acquired bits to a caller */
3429 struct ldlm_lock *lock = ldlm_handle2lock(lh);
3432 *ibits = lock->l_policy_data.l_inodebits.bits;
3433 LDLM_LOCK_PUT(lock);
3438 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
3439 const struct lu_fid *fid, struct lustre_handle *lh,
3440 enum ldlm_mode mode, __u64 ibits, bool cache)
3442 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
3446 int mdt_object_local_lock(struct mdt_thread_info *info, struct mdt_object *o,
3447 struct mdt_lock_handle *lh, __u64 *ibits,
3448 __u64 trybits, bool cos_incompat)
3450 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
3451 union ldlm_policy_data *policy = &info->mti_policy;
3452 struct ldlm_res_id *res_id = &info->mti_res_id;
3453 __u64 dlmflags = 0, *cookie = NULL;
3457 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3458 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3459 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
3460 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
3463 LASSERT(lh->mlh_reg_mode == LCK_PW ||
3464 lh->mlh_reg_mode == LCK_EX);
3465 dlmflags |= LDLM_FL_COS_INCOMPAT;
3466 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
3467 dlmflags |= LDLM_FL_COS_ENABLED;
3470 /* Only enqueue LOOKUP lock for remote object */
3471 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
3473 /* Lease lock are granted with LDLM_FL_CANCEL_ON_BLOCK */
3474 if (lh->mlh_type == MDT_REG_LOCK && lh->mlh_reg_mode == LCK_EX &&
3475 *ibits == MDS_INODELOCK_OPEN)
3476 dlmflags |= LDLM_FL_CANCEL_ON_BLOCK;
3478 if (lh->mlh_type == MDT_PDO_LOCK) {
3479 /* check for exists after object is locked */
3480 if (mdt_object_exists(o) == 0) {
3481 /* Non-existent object shouldn't have PDO lock */
3484 /* Non-dir object shouldn't have PDO lock */
3485 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
3490 fid_build_reg_res_name(mdt_object_fid(o), res_id);
3491 dlmflags |= LDLM_FL_ATOMIC_CB;
3494 cookie = &info->mti_exp->exp_handle.h_cookie;
3497 * Take PDO lock on whole directory and build correct @res_id for lock
3498 * on part of directory.
3500 if (lh->mlh_pdo_hash != 0) {
3501 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
3502 mdt_lock_pdo_mode(info, o, lh);
3503 if (lh->mlh_pdo_mode != LCK_NL) {
3505 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
3506 * is never going to be sent to client and we do not
3507 * want it slowed down due to possible cancels.
3509 policy->l_inodebits.bits =
3510 *ibits & MDS_INODELOCK_UPDATE;
3511 policy->l_inodebits.try_bits =
3512 trybits & MDS_INODELOCK_UPDATE;
3513 /* at least one of them should be set */
3514 LASSERT(policy->l_inodebits.bits |
3515 policy->l_inodebits.try_bits);
3516 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_pdo_lh,
3517 lh->mlh_pdo_mode, policy, res_id,
3519 if (unlikely(rc != 0))
3520 GOTO(out_unlock, rc);
3524 * Finish res_id initializing by name hash marking part of
3525 * directory which is taking modification.
3527 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
3530 policy->l_inodebits.bits = *ibits;
3531 policy->l_inodebits.try_bits = trybits;
3532 policy->l_inodebits.li_gid = lh->mlh_gid;
3535 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
3536 * going to be sent to client. If it is - mdt_intent_policy() path will
3537 * fix it up and turn FL_LOCAL flag off.
3539 rc = mdt_fid_lock(info->mti_env, ns, &lh->mlh_reg_lh, lh->mlh_reg_mode,
3540 policy, res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
3544 mdt_object_unlock(info, o, lh, 1);
3545 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
3546 lh->mlh_pdo_hash != 0 &&
3547 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
3548 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
3550 /* Return successfully acquired bits to a caller */
3552 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
3555 *ibits = lock->l_policy_data.l_inodebits.bits;
3556 LDLM_LOCK_PUT(lock);
3562 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
3563 struct mdt_lock_handle *lh, __u64 *ibits,
3564 __u64 trybits, bool cos_incompat)
3566 struct mdt_lock_handle *local_lh = NULL;
3570 if (!mdt_object_remote(o)) {
3571 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
3576 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
3577 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
3578 MDS_INODELOCK_XATTR);
3580 /* Only enqueue LOOKUP lock for remote object */
3581 if (*ibits & MDS_INODELOCK_LOOKUP) {
3582 __u64 local = MDS_INODELOCK_LOOKUP;
3584 rc = mdt_object_local_lock(info, o, lh, &local, 0,
3592 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
3593 /* Sigh, PDO needs to enqueue 2 locks right now, but
3594 * enqueue RPC can only request 1 lock, to avoid extra
3595 * RPC, so it will instead enqueue EX lock for remote
3596 * object anyway XXX*/
3597 if (lh->mlh_type == MDT_PDO_LOCK &&
3598 lh->mlh_pdo_hash != 0) {
3600 "%s: "DFID" convert PDO lock to EX lock.\n",
3601 mdt_obd_name(info->mti_mdt),
3602 PFID(mdt_object_fid(o)));
3603 lh->mlh_pdo_hash = 0;
3604 lh->mlh_rreg_mode = LCK_EX;
3605 lh->mlh_type = MDT_REG_LOCK;
3608 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
3611 ibits, trybits, false);
3612 if (rc != ELDLM_OK) {
3613 if (local_lh != NULL)
3614 mdt_object_unlock(info, o, local_lh, rc);
3619 /* other components like LFSCK can use lockless access
3620 * and populate cache, so we better invalidate it */
3621 mo_invalidate(info->mti_env, mdt_object_child(o));
3626 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3627 struct mdt_lock_handle *lh, __u64 ibits)
3629 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
3632 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
3633 struct mdt_lock_handle *lh, __u64 ibits,
3636 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
3637 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3641 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3642 struct mdt_lock_handle *lh, __u64 *ibits,
3643 __u64 trybits, bool cos_incompat)
3645 bool trylock_only = *ibits == 0;
3648 LASSERT(!(*ibits & trybits));
3649 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3651 if (rc && trylock_only) { /* clear error for try ibits lock only */
3652 LASSERT(*ibits == 0);
3659 * Save a lock within request object.
3661 * Keep the lock referenced until whether client ACK or transaction
3662 * commit happens or release the lock immediately depending on input
3663 * parameters. If COS is ON, a write lock is converted to COS lock
3666 * \param info thead info object
3667 * \param h lock handle
3668 * \param mode lock mode
3669 * \param decref force immediate lock releasing
3671 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3672 enum ldlm_mode mode, int decref)
3676 if (lustre_handle_is_used(h)) {
3677 if (decref || !info->mti_has_trans ||
3678 !(mode & (LCK_PW | LCK_EX))) {
3679 mdt_fid_unlock(h, mode);
3681 struct mdt_device *mdt = info->mti_mdt;
3682 struct ldlm_lock *lock = ldlm_handle2lock(h);
3683 struct ptlrpc_request *req = mdt_info_req(info);
3684 bool cos = mdt_cos_is_enabled(mdt);
3685 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3687 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3690 /* there is no request if mdt_object_unlock() is called
3691 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3692 if (likely(req != NULL)) {
3693 LDLM_DEBUG(lock, "save lock request %p reply "
3694 "state %p transno %lld\n", req,
3695 req->rq_reply_state, req->rq_transno);
3697 ldlm_lock_mode_downgrade(lock, LCK_COS);
3700 if (req->rq_export->exp_disconnected)
3701 mdt_fid_unlock(h, mode);
3703 ptlrpc_save_lock(req, h, mode, cos,
3706 mdt_fid_unlock(h, mode);
3708 if (mdt_is_lock_sync(lock)) {
3709 CDEBUG(D_HA, "found sync-lock,"
3710 " async commit started\n");
3711 mdt_device_commit_async(info->mti_env,
3714 LDLM_LOCK_PUT(lock);
3723 * Save cross-MDT lock in uncommitted_slc_locks
3725 * Keep the lock referenced until transaction commit happens or release the lock
3726 * immediately depending on input parameters.
3728 * \param info thead info object
3729 * \param h lock handle
3730 * \param mode lock mode
3731 * \param decref force immediate lock releasing
3733 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3734 struct mdt_object *o, struct lustre_handle *h,
3735 enum ldlm_mode mode, int decref)
3739 if (lustre_handle_is_used(h)) {
3740 struct ldlm_lock *lock = ldlm_handle2lock(h);
3741 struct ptlrpc_request *req = mdt_info_req(info);
3744 (lock->l_policy_data.l_inodebits.bits &
3745 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3746 mo_invalidate(info->mti_env, mdt_object_child(o));
3748 if (decref || !info->mti_has_trans || !req ||
3749 !(mode & (LCK_PW | LCK_EX))) {
3750 ldlm_lock_decref_and_cancel(h, mode);
3751 LDLM_LOCK_PUT(lock);
3753 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3755 ldlm_lock_decref(h, mode);
3764 * Unlock mdt object.
3766 * Immeditely release the regular lock and the PDO lock or save the
3767 * lock in request and keep them referenced until client ACK or
3768 * transaction commit.
3770 * \param info thread info object
3771 * \param o mdt object
3772 * \param lh mdt lock handle referencing regular and PDO locks
3773 * \param decref force immediate lock releasing
3775 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3777 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3778 struct mdt_lock_handle *lh, int decref)
3782 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3783 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3784 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3790 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3791 const struct lu_fid *f,
3792 struct mdt_lock_handle *lh,
3795 struct mdt_object *o;
3797 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3801 rc = mdt_object_lock(info, o, lh, ibits);
3803 mdt_object_put(info->mti_env, o);
3810 void mdt_object_unlock_put(struct mdt_thread_info * info,
3811 struct mdt_object * o,
3812 struct mdt_lock_handle *lh,
3815 mdt_object_unlock(info, o, lh, decref);
3816 mdt_object_put(info->mti_env, o);
3820 * Generic code handling requests that have struct mdt_body passed in:
3822 * - extract mdt_body from request and save it in @info, if present;
3824 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3827 * - if HAS_BODY flag is set for this request type check whether object
3828 * actually exists on storage (lu_object_exists()).
3831 static int mdt_body_unpack(struct mdt_thread_info *info,
3832 enum tgt_handler_flags flags)
3834 const struct mdt_body *body;
3835 struct mdt_object *obj;
3836 const struct lu_env *env;
3837 struct req_capsule *pill;
3841 env = info->mti_env;
3842 pill = info->mti_pill;
3844 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3848 if (!(body->mbo_valid & OBD_MD_FLID))
3851 if (!fid_is_sane(&body->mbo_fid1)) {
3852 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3856 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3858 if ((flags & HAS_BODY) && !mdt_object_exists(obj)) {
3859 mdt_object_put(env, obj);
3862 info->mti_object = obj;
3871 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info,
3872 enum tgt_handler_flags flags)
3874 struct req_capsule *pill = info->mti_pill;
3879 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3880 rc = mdt_body_unpack(info, flags);
3884 if (rc == 0 && (flags & HAS_REPLY)) {
3886 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3887 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3889 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3890 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3893 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3894 * by default. If the target object has more ACL entries, then
3895 * enlarge the buffer when necessary. */
3896 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3897 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3898 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3900 mdt_preset_secctx_size(info);
3901 mdt_preset_encctx_size(info);
3903 rc = req_capsule_server_pack(pill);
3905 CWARN("%s: cannot pack response: rc = %d\n",
3906 mdt_obd_name(info->mti_mdt), rc);
3911 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3913 lh->mlh_type = MDT_NUL_LOCK;
3914 lh->mlh_reg_lh.cookie = 0ull;
3915 lh->mlh_reg_mode = LCK_MINMODE;
3916 lh->mlh_pdo_lh.cookie = 0ull;
3917 lh->mlh_pdo_mode = LCK_MINMODE;
3918 lh->mlh_rreg_lh.cookie = 0ull;
3919 lh->mlh_rreg_mode = LCK_MINMODE;
3922 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3924 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3925 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3929 * Initialize fields of struct mdt_thread_info. Other fields are left in
3930 * uninitialized state, because it's too expensive to zero out whole
3931 * mdt_thread_info (> 1K) on each request arrival.
3933 void mdt_thread_info_init(struct ptlrpc_request *req,
3934 struct mdt_thread_info *info)
3938 info->mti_pill = &req->rq_pill;
3941 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3942 mdt_lock_handle_init(&info->mti_lh[i]);
3944 /* mdt device: it can be NULL while CONNECT */
3945 if (req->rq_export) {
3946 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3947 info->mti_exp = req->rq_export;
3949 info->mti_mdt = NULL;
3950 info->mti_env = req->rq_svc_thread->t_env;
3951 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3953 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3954 info->mti_big_buf = LU_BUF_NULL;
3955 info->mti_body = NULL;
3956 info->mti_object = NULL;
3957 info->mti_dlm_req = NULL;
3958 info->mti_has_trans = 0;
3959 info->mti_cross_ref = 0;
3960 info->mti_opdata = 0;
3961 info->mti_big_lmm_used = 0;
3962 info->mti_big_acl_used = 0;
3963 info->mti_som_valid = 0;
3965 info->mti_spec.no_create = 0;
3966 info->mti_spec.sp_rm_entry = 0;
3967 info->mti_spec.sp_permitted = 0;
3969 info->mti_spec.u.sp_ea.eadata = NULL;
3970 info->mti_spec.u.sp_ea.eadatalen = 0;
3973 void mdt_thread_info_fini(struct mdt_thread_info *info)
3977 if (info->mti_object != NULL) {
3978 mdt_object_put(info->mti_env, info->mti_object);
3979 info->mti_object = NULL;
3982 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3983 mdt_lock_handle_fini(&info->mti_lh[i]);
3984 info->mti_env = NULL;
3985 info->mti_pill = NULL;
3986 info->mti_exp = NULL;
3987 info->mti_mdt = NULL;
3989 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3990 lu_buf_free(&info->mti_big_buf);
3993 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3995 struct mdt_thread_info *mti;
3997 mti = mdt_th_info(tsi->tsi_env);
3998 LASSERT(mti != NULL);
4000 mdt_thread_info_init(tgt_ses_req(tsi), mti);
4001 if (tsi->tsi_corpus != NULL) {
4002 mti->mti_object = mdt_obj(tsi->tsi_corpus);
4003 lu_object_get(tsi->tsi_corpus);
4005 mti->mti_body = tsi->tsi_mdt_body;
4006 mti->mti_dlm_req = tsi->tsi_dlm_req;
4011 static int mdt_tgt_connect(struct tgt_session_info *tsi)
4013 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
4015 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id)
4016 schedule_timeout_uninterruptible(cfs_time_seconds(3));
4018 return tgt_connect(tsi);
4021 static int mdt_intent_glimpse(enum ldlm_intent_flags it_opc,
4022 struct mdt_thread_info *info,
4023 struct ldlm_lock **lockp, __u64 flags)
4025 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
4028 static int mdt_intent_brw(enum ldlm_intent_flags it_opc,
4029 struct mdt_thread_info *info,
4030 struct ldlm_lock **lockp, __u64 flags)
4032 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
4036 int mdt_intent_lock_replace(struct mdt_thread_info *info,
4037 struct ldlm_lock **lockp,
4038 struct mdt_lock_handle *lh,
4039 __u64 flags, int result)
4041 struct ptlrpc_request *req = mdt_info_req(info);
4042 struct ldlm_lock *lock = *lockp;
4043 struct ldlm_lock *new_lock;
4045 /* If possible resent found a lock, @lh is set to its handle */
4046 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
4048 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
4049 lh->mlh_reg_lh.cookie = 0;
4053 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
4054 /* Lock is pinned by ldlm_handle_enqueue0() as it is
4055 * a resend case, however, it could be already destroyed
4056 * due to client eviction or a raced cancel RPC. */
4057 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
4058 lh->mlh_reg_lh.cookie);
4059 lh->mlh_reg_lh.cookie = 0;
4063 LASSERTF(new_lock != NULL,
4064 "lockh %#llx flags %#llx : rc = %d\n",
4065 lh->mlh_reg_lh.cookie, flags, result);
4068 * If we've already given this lock to a client once, then we should
4069 * have no readers or writers. Otherwise, we should have one reader
4070 * _or_ writer ref (which will be zeroed below) before returning the
4073 if (new_lock->l_export == req->rq_export) {
4074 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
4076 LASSERT(new_lock->l_export == NULL);
4077 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
4082 if (new_lock->l_export == req->rq_export) {
4084 * Already gave this to the client, which means that we
4085 * reconstructed a reply.
4087 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
4090 LDLM_LOCK_RELEASE(new_lock);
4091 lh->mlh_reg_lh.cookie = 0;
4092 RETURN(ELDLM_LOCK_REPLACED);
4096 * Fixup the lock to be given to the client.
4098 lock_res_and_lock(new_lock);
4099 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
4100 * possible blocking AST. */
4101 while (new_lock->l_readers > 0) {
4102 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
4103 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4104 new_lock->l_readers--;
4106 while (new_lock->l_writers > 0) {
4107 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
4108 lu_ref_del(&new_lock->l_reference, "user", new_lock);
4109 new_lock->l_writers--;
4112 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
4113 new_lock->l_blocking_ast = lock->l_blocking_ast;
4114 new_lock->l_completion_ast = lock->l_completion_ast;
4115 if (ldlm_has_dom(new_lock))
4116 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
4117 new_lock->l_remote_handle = lock->l_remote_handle;
4118 new_lock->l_flags &= ~LDLM_FL_LOCAL;
4120 unlock_res_and_lock(new_lock);
4122 cfs_hash_add(new_lock->l_export->exp_lock_hash,
4123 &new_lock->l_remote_handle,
4124 &new_lock->l_exp_hash);
4126 LDLM_LOCK_RELEASE(new_lock);
4127 lh->mlh_reg_lh.cookie = 0;
4129 RETURN(ELDLM_LOCK_REPLACED);
4132 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
4133 struct ldlm_lock *new_lock,
4134 struct mdt_lock_handle *lh, __u64 flags)
4136 struct ptlrpc_request *req = mdt_info_req(info);
4137 struct ldlm_request *dlmreq;
4139 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
4142 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
4144 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
4145 * lock was found by ldlm_handle_enqueue(); if so @lh must be
4147 if (flags & LDLM_FL_RESENT) {
4148 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
4149 lh->mlh_reg_mode = new_lock->l_granted_mode;
4151 LDLM_DEBUG(new_lock, "Restoring lock cookie");
4152 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
4153 lh->mlh_reg_lh.cookie);
4158 * If the xid matches, then we know this is a resent request, and allow
4159 * it. (It's probably an OPEN, for which we don't send a lock.
4161 if (req_can_reconstruct(req, NULL) != 0)
4165 * This remote handle isn't enqueued, so we never received or processed
4166 * this request. Clear MSG_RESENT, because it can be handled like any
4167 * normal request now.
4169 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
4171 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
4172 dlmreq->lock_handle[0].cookie);
4175 static int mdt_intent_getxattr(enum ldlm_intent_flags it_opc,
4176 struct mdt_thread_info *info,
4177 struct ldlm_lock **lockp,
4180 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4181 struct ldlm_reply *ldlm_rep = NULL;
4186 * Initialize lhc->mlh_reg_lh either from a previously granted lock
4187 * (for the resend case) or a new lock. Below we will use it to
4188 * replace the original lock.
4190 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4191 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4192 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
4193 rc = mdt_object_lock(info, info->mti_object, lhc,
4194 MDS_INODELOCK_XATTR);
4199 rc = mdt_getxattr(info);
4201 if (mdt_info_req(info)->rq_repmsg != NULL)
4202 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4204 if (ldlm_rep == NULL ||
4205 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
4206 mdt_object_unlock(info, info->mti_object, lhc, 1);
4210 RETURN(err_serious(-EFAULT));
4213 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4215 /* This is left for interop instead of adding a new interop flag.
4217 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
4218 if (ldlm_rep->lock_policy_res2) {
4219 mdt_object_unlock(info, info->mti_object, lhc, 1);
4220 RETURN(ELDLM_LOCK_ABORTED);
4224 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4228 static int mdt_intent_getattr(enum ldlm_intent_flags it_opc,
4229 struct mdt_thread_info *info,
4230 struct ldlm_lock **lockp,
4233 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4235 struct ldlm_reply *ldlm_rep;
4236 struct mdt_body *reqbody;
4237 struct mdt_body *repbody;
4241 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
4244 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
4247 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
4248 repbody->mbo_eadatasize = 0;
4249 repbody->mbo_aclsize = 0;
4253 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
4256 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
4260 CERROR("%s: unsupported intent %#x\n",
4261 mdt_obd_name(info->mti_mdt), (unsigned int)it_opc);
4262 GOTO(out_shrink, rc = -EINVAL);
4265 rc = mdt_init_ucred(info, reqbody);
4267 GOTO(out_shrink, rc);
4269 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4270 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4272 /* Get lock from request for possible resent case. */
4273 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4275 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
4276 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4278 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
4279 ldlm_rep->lock_policy_res2 = 0;
4280 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
4281 ldlm_rep->lock_policy_res2) {
4282 lhc->mlh_reg_lh.cookie = 0ull;
4283 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
4286 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4289 mdt_exit_ucred(info);
4291 mdt_client_compatibility(info);
4292 rc2 = mdt_fix_reply(info);
4298 static int mdt_intent_layout(enum ldlm_intent_flags it_opc,
4299 struct mdt_thread_info *info,
4300 struct ldlm_lock **lockp,
4303 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4304 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
4305 struct layout_intent *intent;
4306 struct ldlm_reply *ldlm_rep;
4307 struct lu_fid *fid = &info->mti_tmp_fid2;
4308 struct mdt_object *obj = NULL;
4309 int layout_size = 0;
4310 struct lu_buf *buf = &layout.mlc_buf;
4315 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
4317 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
4321 CDEBUG(D_INFO, DFID "got layout change request from client: "
4322 "opc:%u flags:%#x extent "DEXT"\n",
4323 PFID(fid), intent->li_opc, intent->li_flags,
4324 PEXT(&intent->li_extent));
4326 switch (intent->li_opc) {
4327 case LAYOUT_INTENT_TRUNC:
4328 case LAYOUT_INTENT_WRITE:
4329 layout.mlc_opc = MD_LAYOUT_WRITE;
4330 layout.mlc_intent = intent;
4332 case LAYOUT_INTENT_ACCESS:
4334 case LAYOUT_INTENT_READ:
4335 case LAYOUT_INTENT_GLIMPSE:
4336 case LAYOUT_INTENT_RELEASE:
4337 case LAYOUT_INTENT_RESTORE:
4338 CERROR("%s: Unsupported layout intent opc %d\n",
4339 mdt_obd_name(info->mti_mdt), intent->li_opc);
4342 CERROR("%s: Unknown layout intent opc %d\n",
4343 mdt_obd_name(info->mti_mdt), intent->li_opc);
4347 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
4349 RETURN(PTR_ERR(obj));
4351 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
4352 /* if layout is going to be changed don't use the current EA
4353 * size but the maximum one. That buffer will be shrinked
4354 * to the actual size in req_capsule_shrink() before reply.
4356 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
4357 layout_size = info->mti_mdt->mdt_max_mdsize;
4359 layout_size = mdt_attr_get_eabuf_size(info, obj);
4360 if (layout_size < 0)
4361 GOTO(out, rc = layout_size);
4363 if (layout_size > info->mti_mdt->mdt_max_mdsize)
4364 info->mti_mdt->mdt_max_mdsize = layout_size;
4366 CDEBUG(D_INFO, "%s: layout_size %d\n",
4367 mdt_obd_name(info->mti_mdt), layout_size);
4371 * set reply buffer size, so that ldlm_handle_enqueue0()->
4372 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
4374 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
4376 rc = req_capsule_server_pack(info->mti_pill);
4380 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4382 GOTO(out, rc = -EPROTO);
4384 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
4386 /* take lock in ldlm_lock_enqueue() for LAYOUT_INTENT_ACCESS */
4387 if (layout.mlc_opc == MD_LAYOUT_NOP)
4390 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
4394 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
4395 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
4401 if (unlikely(req_is_replay(mdt_info_req(info)))) {
4402 buf->lb_buf = req_capsule_client_get(info->mti_pill,
4404 buf->lb_len = req_capsule_get_size(info->mti_pill,
4405 &RMF_EADATA, RCL_CLIENT);
4407 * If it's a replay of layout write intent RPC, the client has
4408 * saved the extended lovea when it get reply then.
4410 if (buf->lb_len > 0)
4411 mdt_fix_lov_magic(info, buf->lb_buf);
4414 /* Get lock from request for possible resent case. */
4415 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4416 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
4419 * Instantiate some layout components, if @buf contains lovea, then it's
4420 * a replay of the layout intent write RPC.
4422 rc = mdt_layout_change(info, obj, lhc, &layout);
4423 ldlm_rep->lock_policy_res2 = clear_serious(rc);
4425 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
4426 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4427 if (rc == ELDLM_LOCK_REPLACED &&
4428 (*lockp)->l_granted_mode == LCK_EX)
4429 ldlm_lock_mode_downgrade(*lockp, LCK_CR);
4434 mdt_object_put(info->mti_env, obj);
4438 static int mdt_intent_open(enum ldlm_intent_flags it_opc,
4439 struct mdt_thread_info *info,
4440 struct ldlm_lock **lockp,
4443 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
4444 struct ldlm_reply *rep = NULL;
4447 struct ptlrpc_request *req = mdt_info_req(info);
4449 static const struct req_format *intent_fmts[REINT_MAX] = {
4450 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
4451 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
4456 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
4460 /* Get lock from request for possible resent case. */
4461 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
4463 rc = mdt_reint_internal(info, lhc, opc);
4465 if (rc < 0 && lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
4466 DEBUG_REQ(D_ERROR, req, "Replay open failed with %d", rc);
4468 /* Check whether the reply has been packed successfully. */
4469 if (mdt_info_req(info)->rq_repmsg != NULL)
4470 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4475 RETURN(err_serious(-EFAULT));
4478 /* MDC expects this in any case */
4480 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
4482 /* the open lock or the lock for cross-ref object should be
4483 * returned to the client */
4484 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
4485 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
4486 rep->lock_policy_res2 = 0;
4487 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
4491 rep->lock_policy_res2 = clear_serious(rc);
4493 if (rep->lock_policy_res2 == -ENOENT &&
4494 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
4495 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
4496 rep->lock_policy_res2 = 0;
4498 lhc->mlh_reg_lh.cookie = 0ull;
4499 if (rc == -ENOTCONN || rc == -ENODEV ||
4500 rc == -EOVERFLOW) { /**< if VBR failure then return error */
4502 * If it is the disconnect error (ENODEV & ENOCONN), the error
4503 * will be returned by rq_status, and client at ptlrpc layer
4504 * will detect this, then disconnect, reconnect the import
4505 * immediately, instead of impacting the following the rpc.
4510 * For other cases, the error will be returned by intent, and client
4511 * will retrieve the result from intent.
4513 RETURN(ELDLM_LOCK_ABORTED);
4516 static int mdt_intent_opc(enum ldlm_intent_flags it_opc,
4517 struct mdt_thread_info *info,
4518 struct ldlm_lock **lockp,
4519 u64 flags /* LDLM_FL_* */)
4521 struct req_capsule *pill = info->mti_pill;
4522 struct ptlrpc_request *req = mdt_info_req(info);
4523 const struct req_format *it_format;
4524 int (*it_handler)(enum ldlm_intent_flags,
4525 struct mdt_thread_info *,
4526 struct ldlm_lock **,
4528 enum tgt_handler_flags it_handler_flags = 0;
4529 struct ldlm_reply *rep;
4530 bool check_mdt_object = false;
4536 case IT_OPEN|IT_CREAT:
4538 * OCREAT is not a IS_MUTABLE request since the file may
4539 * already exist. We do the extra check of
4540 * OBD_CONNECT_RDONLY in mdt_reint_open() when we
4541 * really need to create the object.
4543 it_format = &RQF_LDLM_INTENT;
4544 it_handler = &mdt_intent_open;
4547 check_mdt_object = true;
4550 it_format = &RQF_LDLM_INTENT_GETATTR;
4551 it_handler = &mdt_intent_getattr;
4552 it_handler_flags = HAS_REPLY;
4555 check_mdt_object = true;
4556 it_format = &RQF_LDLM_INTENT_GETXATTR;
4557 it_handler = &mdt_intent_getxattr;
4558 it_handler_flags = HAS_BODY;
4561 it_format = &RQF_LDLM_INTENT_LAYOUT;
4562 it_handler = &mdt_intent_layout;
4565 it_format = &RQF_LDLM_INTENT;
4566 it_handler = &mdt_intent_glimpse;
4569 it_format = &RQF_LDLM_INTENT;
4570 it_handler = &mdt_intent_brw;
4572 case IT_QUOTA_DQACQ:
4573 case IT_QUOTA_CONN: {
4574 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4577 RETURN(-EOPNOTSUPP);
4579 if (mdt_rdonly(req->rq_export))
4582 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4583 /* pass the request to quota master */
4584 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4585 mdt_info_req(info), lockp,
4590 CERROR("%s: unknown intent code %#x\n",
4591 mdt_obd_name(info->mti_mdt), it_opc);
4595 req_capsule_extend(pill, it_format);
4597 rc = mdt_unpack_req_pack_rep(info, it_handler_flags);
4601 if (unlikely(info->mti_object == NULL && check_mdt_object))
4604 if (it_handler_flags & IS_MUTABLE && mdt_rdonly(req->rq_export))
4607 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4609 /* execute policy */
4610 rc = (*it_handler)(it_opc, info, lockp, flags);
4612 /* Check whether the reply has been packed successfully. */
4613 if (req->rq_repmsg != NULL) {
4614 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
4615 rep->lock_policy_res2 =
4616 ptlrpc_status_hton(rep->lock_policy_res2);
4622 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4623 enum ldlm_intent_flags it_opc)
4625 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4627 /* update stats when IT code is known */
4628 if (srv_stats != NULL)
4629 lprocfs_counter_incr(srv_stats,
4630 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4631 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4634 static int mdt_intent_policy(const struct lu_env *env,
4635 struct ldlm_namespace *ns,
4636 struct ldlm_lock **lockp,
4638 enum ldlm_mode mode,
4639 __u64 flags, void *data)
4641 struct tgt_session_info *tsi;
4642 struct mdt_thread_info *info;
4643 struct ptlrpc_request *req = req_cookie;
4644 struct ldlm_intent *it;
4645 struct req_capsule *pill;
4646 const struct ldlm_lock_desc *ldesc;
4651 LASSERT(req != NULL);
4653 tsi = tgt_ses_info(env);
4655 info = tsi2mdt_info(tsi);
4656 LASSERT(info != NULL);
4657 pill = info->mti_pill;
4658 LASSERT(pill->rc_req == req);
4659 ldesc = &info->mti_dlm_req->lock_desc;
4661 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4662 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4663 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4665 mdt_ptlrpc_stats_update(req, it->opc);
4666 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4670 /* Lock without inodebits makes no sense and will oops
4671 * later in ldlm. Let's check it now to see if we have
4672 * ibits corrupted somewhere in mdt_intent_opc().
4673 * The case for client miss to set ibits has been
4674 * processed by others. */
4675 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4676 ldesc->l_policy_data.l_inodebits.bits != 0));
4678 rc = err_serious(-EFAULT);
4680 } else if (ldesc->l_resource.lr_type == LDLM_IBITS &&
4681 ldesc->l_policy_data.l_inodebits.bits == MDS_INODELOCK_DOM) {
4682 struct ldlm_reply *rep;
4684 /* No intent was provided but INTENT flag is set along with
4685 * DOM bit, this is considered as GLIMPSE request.
4686 * This logic is common for MDT and OST glimpse
4688 mdt_ptlrpc_stats_update(req, IT_GLIMPSE);
4689 rc = mdt_glimpse_enqueue(info, ns, lockp, flags);
4690 /* Check whether the reply has been packed successfully. */
4691 if (req->rq_repmsg != NULL) {
4692 rep = req_capsule_server_get(info->mti_pill,
4694 rep->lock_policy_res2 =
4695 ptlrpc_status_hton(rep->lock_policy_res2);
4698 /* No intent was provided */
4699 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4700 rc = req_capsule_server_pack(pill);
4702 rc = err_serious(rc);
4704 mdt_thread_info_fini(info);
4708 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4710 struct seq_server_site *ss = mdt_seq_site(mdt);
4712 if (ss->ss_node_id == 0)
4715 if (ss->ss_client_seq != NULL) {
4716 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4717 ss->ss_client_seq->lcs_exp = NULL;
4720 if (ss->ss_server_fld != NULL) {
4721 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4722 ss->ss_server_fld->lsf_control_exp = NULL;
4726 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4728 struct seq_server_site *ss = mdt_seq_site(mdt);
4733 if (ss->ss_server_seq != NULL)
4734 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4737 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4739 mdt_seq_fini_cli(mdt);
4740 mdt_deregister_seq_exp(mdt);
4742 return seq_site_fini(env, mdt_seq_site(mdt));
4746 * It will retrieve its FLDB entries from MDT0, and it only happens
4747 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4748 * and it needs to refresh FLDB from the MDT0.
4750 static int mdt_register_lwp_callback(void *data)
4753 struct mdt_device *mdt = data;
4754 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4758 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4760 rc = lu_env_init(&env, LCT_MD_THREAD);
4762 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4766 /* Allocate new sequence now to avoid creating local transaction
4767 * in the normal transaction process */
4768 rc = seq_server_check_and_alloc_super(&env,
4769 mdt_seq_site(mdt)->ss_server_seq);
4774 rc = fld_update_from_controller(&env, fld);
4776 CERROR("%s: cannot update controller: rc = %d\n",
4777 mdt_obd_name(mdt), rc);
4786 static int mdt_register_seq_exp(struct mdt_device *mdt)
4788 struct seq_server_site *ss = mdt_seq_site(mdt);
4789 char *lwp_name = NULL;
4792 if (ss->ss_node_id == 0)
4795 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4796 if (lwp_name == NULL)
4797 GOTO(out_free, rc = -ENOMEM);
4799 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4803 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4808 rc = lustre_register_lwp_item(lwp_name,
4809 &ss->ss_server_fld->lsf_control_exp,
4810 mdt_register_lwp_callback, mdt);
4812 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4813 ss->ss_client_seq->lcs_exp = NULL;
4817 if (lwp_name != NULL)
4818 OBD_FREE(lwp_name, MAX_OBD_NAME);
4824 * Init client sequence manager which is used by local MDS to talk to sequence
4825 * controller on remote node.
4827 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4829 struct seq_server_site *ss = mdt_seq_site(mdt);
4833 /* check if this is adding the first MDC and controller is not yet
4835 OBD_ALLOC_PTR(ss->ss_client_seq);
4836 if (ss->ss_client_seq == NULL)
4839 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4840 if (prefix == NULL) {
4841 OBD_FREE_PTR(ss->ss_client_seq);
4842 ss->ss_client_seq = NULL;
4846 /* Note: seq_client_fini will be called in seq_site_fini */
4847 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4848 seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4849 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4851 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4853 RETURN(seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq));
4856 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4858 struct seq_server_site *ss;
4862 ss = mdt_seq_site(mdt);
4863 /* init sequence controller server(MDT0) */
4864 if (ss->ss_node_id == 0) {
4865 OBD_ALLOC_PTR(ss->ss_control_seq);
4866 if (ss->ss_control_seq == NULL)
4869 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4870 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4873 GOTO(out_seq_fini, rc);
4876 /* Init normal sequence server */
4877 OBD_ALLOC_PTR(ss->ss_server_seq);
4878 if (ss->ss_server_seq == NULL)
4879 GOTO(out_seq_fini, rc = -ENOMEM);
4881 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4882 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4884 GOTO(out_seq_fini, rc);
4886 /* init seq client for seq server to talk to seq controller(MDT0) */
4887 rc = mdt_seq_init_cli(env, mdt);
4889 GOTO(out_seq_fini, rc);
4891 if (ss->ss_node_id != 0)
4892 /* register controller export through lwp */
4893 rc = mdt_register_seq_exp(mdt);
4898 mdt_seq_fini(env, mdt);
4906 static int mdt_fld_fini(const struct lu_env *env,
4907 struct mdt_device *m)
4909 struct seq_server_site *ss = mdt_seq_site(m);
4912 if (ss && ss->ss_server_fld) {
4913 fld_server_fini(env, ss->ss_server_fld);
4914 OBD_FREE_PTR(ss->ss_server_fld);
4915 ss->ss_server_fld = NULL;
4921 static int mdt_fld_init(const struct lu_env *env,
4923 struct mdt_device *m)
4925 struct seq_server_site *ss;
4929 ss = mdt_seq_site(m);
4931 OBD_ALLOC_PTR(ss->ss_server_fld);
4932 if (ss->ss_server_fld == NULL)
4933 RETURN(rc = -ENOMEM);
4935 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4938 OBD_FREE_PTR(ss->ss_server_fld);
4939 ss->ss_server_fld = NULL;
4946 static void mdt_stack_pre_fini(const struct lu_env *env,
4947 struct mdt_device *m, struct lu_device *top)
4949 struct lustre_cfg_bufs *bufs;
4950 struct lustre_cfg *lcfg;
4951 struct mdt_thread_info *info;
4956 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4957 LASSERT(info != NULL);
4959 bufs = &info->mti_u.bufs;
4961 LASSERT(m->mdt_child_exp);
4962 LASSERT(m->mdt_child_exp->exp_obd);
4964 /* process cleanup, pass mdt obd name to get obd umount flags */
4965 /* XXX: this is needed because all layers are referenced by
4966 * objects (some of them are pinned by osd, for example *
4967 * the proper solution should be a model where object used
4968 * by osd only doesn't have mdt/mdd slices -bzzz */
4969 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4970 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4971 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4974 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4976 top->ld_ops->ldo_process_config(env, top, lcfg);
4977 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4981 static void mdt_stack_fini(const struct lu_env *env,
4982 struct mdt_device *m, struct lu_device *top)
4984 struct obd_device *obd = mdt2obd_dev(m);
4985 struct lustre_cfg_bufs *bufs;
4986 struct lustre_cfg *lcfg;
4987 struct mdt_thread_info *info;
4991 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4992 LASSERT(info != NULL);
4994 lu_dev_del_linkage(top->ld_site, top);
4996 lu_site_purge(env, top->ld_site, -1);
4998 bufs = &info->mti_u.bufs;
4999 /* process cleanup, pass mdt obd name to get obd umount flags */
5000 /* another purpose is to let all layers to release their objects */
5001 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
5006 lustre_cfg_bufs_set_string(bufs, 1, flags);
5007 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5010 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
5013 top->ld_ops->ldo_process_config(env, top, lcfg);
5014 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5016 lu_site_purge(env, top->ld_site, -1);
5018 m->mdt_child = NULL;
5019 m->mdt_bottom = NULL;
5021 obd_disconnect(m->mdt_child_exp);
5022 m->mdt_child_exp = NULL;
5024 obd_disconnect(m->mdt_bottom_exp);
5025 m->mdt_child_exp = NULL;
5028 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
5029 const char *next, struct obd_export **exp)
5031 struct obd_connect_data *data = NULL;
5032 struct obd_device *obd;
5036 OBD_ALLOC_PTR(data);
5038 GOTO(out, rc = -ENOMEM);
5040 obd = class_name2obd(next);
5042 CERROR("%s: can't locate next device: %s\n",
5043 mdt_obd_name(m), next);
5044 GOTO(out, rc = -ENOTCONN);
5047 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5048 data->ocd_version = LUSTRE_VERSION_CODE;
5050 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
5052 CERROR("%s: cannot connect to next dev %s (%d)\n",
5053 mdt_obd_name(m), next, rc);
5063 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
5064 struct lustre_cfg *cfg)
5066 char *dev = lustre_cfg_string(cfg, 0);
5067 int rc, name_size, uuid_size;
5068 char *name, *uuid, *p;
5069 struct lustre_cfg_bufs *bufs;
5070 struct lustre_cfg *lcfg;
5071 struct obd_device *obd;
5072 struct lustre_profile *lprof;
5073 struct lu_site *site;
5076 /* in 1.8 we had the only device in the stack - MDS.
5077 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
5078 * in 2.3 OSD is instantiated by obd_mount.c, so we need
5079 * to generate names and setup MDT, MDD. MDT will be using
5080 * generated name to connect to MDD. for MDD the next device
5081 * will be LOD with name taken from so called "profile" which
5082 * is generated by mount_option line
5084 * 1.8 MGS generates config. commands like this:
5085 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
5086 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
5087 * 2.0 MGS generates config. commands like this:
5088 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
5089 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5090 * 3:lustre-MDT0000-mdtlov 4:f
5092 * we generate MDD name from MDT one, just replacing T with D
5094 * after all the preparations, the logical equivalent will be
5095 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
5096 * 3:lustre-MDT0000-mdtlov 4:f
5097 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5098 * 3:lustre-MDD0000 4:f
5100 * notice we build the stack from down to top: MDD first, then MDT */
5102 name_size = MAX_OBD_NAME;
5103 uuid_size = MAX_OBD_NAME;
5105 OBD_ALLOC(name, name_size);
5106 OBD_ALLOC(uuid, uuid_size);
5107 if (name == NULL || uuid == NULL)
5108 GOTO(cleanup_mem, rc = -ENOMEM);
5110 OBD_ALLOC_PTR(bufs);
5112 GOTO(cleanup_mem, rc = -ENOMEM);
5115 p = strstr(name, "-MDT");
5117 GOTO(free_bufs, rc = -ENOMEM);
5120 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
5122 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5123 if (lprof == NULL || lprof->lp_dt == NULL) {
5124 CERROR("can't find the profile: %s\n",
5125 lustre_cfg_string(cfg, 0));
5126 GOTO(free_bufs, rc = -EINVAL);
5129 lustre_cfg_bufs_reset(bufs, name);
5130 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
5131 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5132 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5134 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5136 GOTO(put_profile, rc = -ENOMEM);
5137 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5139 rc = class_attach(lcfg);
5141 GOTO(lcfg_cleanup, rc);
5143 obd = class_name2obd(name);
5145 CERROR("Can not find obd %s (%s in config)\n",
5146 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
5147 GOTO(lcfg_cleanup, rc = -EINVAL);
5150 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5152 lustre_cfg_bufs_reset(bufs, name);
5153 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5154 lustre_cfg_bufs_set_string(bufs, 2, dev);
5155 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5157 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5159 GOTO(class_detach, rc = -ENOMEM);
5160 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5162 rc = class_setup(obd, lcfg);
5164 GOTO(class_detach, rc);
5166 /* connect to MDD we just setup */
5167 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
5169 GOTO(class_detach, rc);
5171 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
5173 LASSERT(mdt_lu_site(mdt) == NULL);
5174 mdt->mdt_lu_dev.ld_site = site;
5175 site->ls_top_dev = &mdt->mdt_lu_dev;
5176 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
5178 /* now connect to bottom OSD */
5179 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
5180 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
5182 GOTO(class_detach, rc);
5184 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
5186 rc = lu_env_refill((struct lu_env *)env);
5188 CERROR("Failure to refill session: '%d'\n", rc);
5190 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
5195 class_detach(obd, lcfg);
5197 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5199 class_put_profile(lprof);
5204 OBD_FREE(name, name_size);
5206 OBD_FREE(uuid, uuid_size);
5210 /* setup quota master target on MDT0 */
5211 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
5212 struct lustre_cfg *cfg)
5214 struct obd_device *obd;
5215 char *dev = lustre_cfg_string(cfg, 0);
5216 char *qmtname, *uuid, *p;
5217 struct lustre_cfg_bufs *bufs;
5218 struct lustre_cfg *lcfg;
5219 struct lustre_profile *lprof;
5220 struct obd_connect_data *data;
5224 LASSERT(mdt->mdt_qmt_exp == NULL);
5225 LASSERT(mdt->mdt_qmt_dev == NULL);
5227 /* quota master is on MDT0 only for now */
5228 if (mdt->mdt_seq_site.ss_node_id != 0)
5231 /* MGS generates config commands which look as follows:
5232 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
5233 * 3:lustre-MDT0000-mdtlov 4:f
5235 * We generate the QMT name from the MDT one, just replacing MD with QM
5236 * after all the preparations, the logical equivalent will be:
5237 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
5238 * 3:lustre-MDT0000-osd 4:f */
5239 OBD_ALLOC(qmtname, MAX_OBD_NAME);
5240 OBD_ALLOC(uuid, UUID_MAX);
5241 OBD_ALLOC_PTR(bufs);
5242 OBD_ALLOC_PTR(data);
5243 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
5244 GOTO(cleanup_mem, rc = -ENOMEM);
5246 strcpy(qmtname, dev);
5247 p = strstr(qmtname, "-MDT");
5249 GOTO(cleanup_mem, rc = -ENOMEM);
5250 /* replace MD with QM */
5254 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
5256 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
5257 if (lprof == NULL || lprof->lp_dt == NULL) {
5258 CERROR("can't find profile for %s\n",
5259 lustre_cfg_string(cfg, 0));
5260 GOTO(cleanup_mem, rc = -EINVAL);
5263 lustre_cfg_bufs_reset(bufs, qmtname);
5264 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
5265 lustre_cfg_bufs_set_string(bufs, 2, uuid);
5266 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
5268 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5270 GOTO(put_profile, rc = -ENOMEM);
5271 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
5273 rc = class_attach(lcfg);
5275 GOTO(lcfg_cleanup, rc);
5277 obd = class_name2obd(qmtname);
5279 CERROR("Can not find obd %s (%s in config)\n", qmtname,
5280 lustre_cfg_string(cfg, 0));
5281 GOTO(lcfg_cleanup, rc = -EINVAL);
5284 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5286 lustre_cfg_bufs_reset(bufs, qmtname);
5287 lustre_cfg_bufs_set_string(bufs, 1, uuid);
5288 lustre_cfg_bufs_set_string(bufs, 2, dev);
5290 /* for quota, the next device should be the OSD device */
5291 lustre_cfg_bufs_set_string(bufs, 3,
5292 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
5294 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
5296 GOTO(class_detach, rc = -ENOMEM);
5297 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
5299 rc = class_setup(obd, lcfg);
5301 GOTO(class_detach, rc);
5303 mdt->mdt_qmt_dev = obd->obd_lu_dev;
5305 /* configure local quota objects */
5306 if (OBD_FAIL_CHECK(OBD_FAIL_QUOTA_INIT))
5309 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
5313 GOTO(class_cleanup, rc);
5315 /* connect to quota master target */
5316 data->ocd_connect_flags = OBD_CONNECT_VERSION;
5317 data->ocd_version = LUSTRE_VERSION_CODE;
5318 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
5321 CERROR("cannot connect to quota master device %s (%d)\n",
5323 GOTO(class_cleanup, rc);
5329 class_manual_cleanup(obd);
5330 mdt->mdt_qmt_dev = NULL;
5331 GOTO(lcfg_cleanup, rc);
5335 class_detach(obd, lcfg);
5337 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
5339 class_put_profile(lprof);
5344 OBD_FREE(qmtname, MAX_OBD_NAME);
5346 OBD_FREE(uuid, UUID_MAX);
5352 /* Shutdown quota master target associated with mdt */
5353 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
5357 if (mdt->mdt_qmt_exp == NULL)
5359 LASSERT(mdt->mdt_qmt_dev != NULL);
5361 /* the qmt automatically shuts down when the mdt disconnects */
5362 obd_disconnect(mdt->mdt_qmt_exp);
5363 mdt->mdt_qmt_exp = NULL;
5364 mdt->mdt_qmt_dev = NULL;
5368 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
5369 * for now. This will be removed along with converting rest of MDT code
5370 * to use tgt_session_info */
5371 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
5373 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5376 if (unlikely(info->mti_object == NULL))
5379 rc = mdt_getxattr(info);
5381 mdt_thread_info_fini(info);
5385 static int mdt_llog_open(struct tgt_session_info *tsi)
5389 if (!mdt_is_rootadmin(tsi2mdt_info(tsi)))
5390 RETURN(err_serious(-EACCES));
5392 RETURN(tgt_llog_open(tsi));
5395 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
5396 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
5397 #define OST_BRW_READ OST_READ
5398 #define OST_BRW_WRITE OST_WRITE
5400 static struct tgt_handler mdt_tgt_handlers[] = {
5401 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5402 0, MDS_CONNECT, mdt_tgt_connect,
5403 &RQF_CONNECT, LUSTRE_OBD_VERSION),
5404 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5405 0, MDS_DISCONNECT, tgt_disconnect,
5406 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
5407 TGT_RPC_HANDLER(MDS_FIRST_OPC,
5408 HAS_REPLY, MDS_SET_INFO, mdt_set_info,
5409 &RQF_MDT_SET_INFO, LUSTRE_MDS_VERSION),
5410 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
5411 TGT_MDT_HDL(HAS_REPLY, MDS_GET_ROOT, mdt_get_root),
5412 TGT_MDT_HDL(HAS_BODY, MDS_GETATTR, mdt_getattr),
5413 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_GETATTR_NAME,
5415 TGT_MDT_HDL(HAS_BODY, MDS_GETXATTR, mdt_tgt_getxattr),
5416 TGT_MDT_HDL(HAS_REPLY, MDS_STATFS, mdt_statfs),
5417 TGT_MDT_HDL(IS_MUTABLE, MDS_REINT, mdt_reint),
5418 TGT_MDT_HDL(HAS_BODY, MDS_CLOSE, mdt_close),
5419 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_READPAGE, mdt_readpage),
5420 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_SYNC, mdt_sync),
5421 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
5422 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_PROGRESS,
5424 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_REGISTER,
5425 mdt_hsm_ct_register),
5426 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_CT_UNREGISTER,
5427 mdt_hsm_ct_unregister),
5428 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_STATE_GET,
5430 TGT_MDT_HDL(HAS_BODY | HAS_REPLY | IS_MUTABLE, MDS_HSM_STATE_SET,
5432 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_ACTION, mdt_hsm_action),
5433 TGT_MDT_HDL(HAS_BODY | HAS_REPLY, MDS_HSM_REQUEST,
5435 TGT_MDT_HDL(HAS_KEY | HAS_BODY | HAS_REPLY | IS_MUTABLE,
5438 TGT_MDT_HDL(IS_MUTABLE, MDS_RMFID, mdt_rmfid),
5441 static struct tgt_handler mdt_io_ops[] = {
5442 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY, OST_BRW_READ, tgt_brw_read,
5444 TGT_OST_HDL_HP(HAS_BODY | IS_MUTABLE, OST_BRW_WRITE, tgt_brw_write,
5446 TGT_OST_HDL_HP(HAS_BODY | HAS_REPLY | IS_MUTABLE,
5447 OST_PUNCH, mdt_punch_hdl,
5449 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SYNC, mdt_data_sync),
5450 TGT_OST_HDL(HAS_BODY | HAS_REPLY, OST_SEEK, tgt_lseek),
5453 static struct tgt_handler mdt_sec_ctx_ops[] = {
5454 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
5455 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
5456 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
5459 static struct tgt_handler mdt_quota_ops[] = {
5460 TGT_QUOTA_HDL(HAS_REPLY, QUOTA_DQACQ, mdt_quota_dqacq),
5463 static struct tgt_handler mdt_llog_handlers[] = {
5464 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_CREATE, mdt_llog_open),
5465 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, tgt_llog_next_block),
5466 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_READ_HEADER, tgt_llog_read_header),
5467 TGT_LLOG_HDL(0, LLOG_ORIGIN_HANDLE_PREV_BLOCK, tgt_llog_prev_block),
5470 static struct tgt_opc_slice mdt_common_slice[] = {
5472 .tos_opc_start = MDS_FIRST_OPC,
5473 .tos_opc_end = MDS_LAST_OPC,
5474 .tos_hs = mdt_tgt_handlers
5477 .tos_opc_start = OBD_FIRST_OPC,
5478 .tos_opc_end = OBD_LAST_OPC,
5479 .tos_hs = tgt_obd_handlers
5482 .tos_opc_start = LDLM_FIRST_OPC,
5483 .tos_opc_end = LDLM_LAST_OPC,
5484 .tos_hs = tgt_dlm_handlers
5487 .tos_opc_start = SEC_FIRST_OPC,
5488 .tos_opc_end = SEC_LAST_OPC,
5489 .tos_hs = mdt_sec_ctx_ops
5492 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
5493 .tos_opc_end = OUT_UPDATE_LAST_OPC,
5494 .tos_hs = tgt_out_handlers
5497 .tos_opc_start = FLD_FIRST_OPC,
5498 .tos_opc_end = FLD_LAST_OPC,
5499 .tos_hs = fld_handlers
5502 .tos_opc_start = SEQ_FIRST_OPC,
5503 .tos_opc_end = SEQ_LAST_OPC,
5504 .tos_hs = seq_handlers
5507 .tos_opc_start = QUOTA_DQACQ,
5508 .tos_opc_end = QUOTA_LAST_OPC,
5509 .tos_hs = mdt_quota_ops
5512 .tos_opc_start = LLOG_FIRST_OPC,
5513 .tos_opc_end = LLOG_LAST_OPC,
5514 .tos_hs = mdt_llog_handlers
5517 .tos_opc_start = LFSCK_FIRST_OPC,
5518 .tos_opc_end = LFSCK_LAST_OPC,
5519 .tos_hs = tgt_lfsck_handlers
5522 .tos_opc_start = OST_FIRST_OPC,
5523 .tos_opc_end = OST_LAST_OPC,
5524 .tos_hs = mdt_io_ops
5531 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
5533 struct md_device *next = m->mdt_child;
5534 struct lu_device *d = &m->mdt_lu_dev;
5535 struct obd_device *obd = mdt2obd_dev(m);
5536 struct lfsck_stop stop;
5539 stop.ls_status = LS_PAUSED;
5541 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
5543 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
5545 mdt_restriper_stop(m);
5546 ping_evictor_stop();
5548 /* Remove the HSM /proc entry so the coordinator cannot be
5549 * restarted by a user while it's shutting down.
5551 mdt_hsm_cdt_stop(m);
5553 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5554 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5556 if (m->mdt_namespace != NULL)
5557 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5558 d->ld_obd->obd_force);
5560 obd_exports_barrier(obd);
5561 obd_zombie_barrier();
5563 mdt_quota_fini(env, m);
5565 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5567 /* Calling the cleanup functions in the same order as in the mdt_init0
5570 mdt_tunables_fini(m);
5572 target_recovery_fini(obd);
5573 upcall_cache_cleanup(m->mdt_identity_cache);
5574 m->mdt_identity_cache = NULL;
5576 mdt_fs_cleanup(env, m);
5578 tgt_fini(env, &m->mdt_lut);
5580 mdt_hsm_cdt_fini(m);
5582 if (m->mdt_los != NULL) {
5583 local_oid_storage_fini(env, m->mdt_los);
5587 if (m->mdt_namespace != NULL) {
5588 ldlm_namespace_free_post(m->mdt_namespace);
5589 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5592 if (m->mdt_md_root != NULL) {
5593 mdt_object_put(env, m->mdt_md_root);
5594 m->mdt_md_root = NULL;
5597 mdt_seq_fini(env, m);
5599 mdt_fld_fini(env, m);
5604 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5606 LASSERT(atomic_read(&d->ld_ref) == 0);
5608 server_put_mount(mdt_obd_name(m), true);
5613 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5615 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5616 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5618 const struct dt_device_param *dt_conf;
5619 struct mdt_thread_info *info;
5620 struct obd_device *obd;
5621 const char *dev = lustre_cfg_string(cfg, 0);
5622 const char *num = lustre_cfg_string(cfg, 2);
5623 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5624 struct lustre_mount_info *lmi = NULL;
5625 struct lustre_sb_info *lsi;
5627 struct seq_server_site *ss_site;
5628 const char *identity_upcall = "NONE";
5629 struct md_device *next;
5636 lu_device_init(&m->mdt_lu_dev, ldt);
5638 * Environment (env) might be missing mdt_thread_key values at that
5639 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5642 * Usually device allocation path doesn't use module key values, but
5643 * mdt has to do a lot of work here, so allocate key value.
5645 rc = lu_env_refill((struct lu_env *)env);
5649 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5650 LASSERT(info != NULL);
5652 obd = class_name2obd(dev);
5653 LASSERT(obd != NULL);
5655 m->mdt_max_mdsize = MAX_MD_SIZE_OLD;
5656 m->mdt_opts.mo_evict_tgt_nids = 1;
5657 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5659 lmi = server_get_mount(dev);
5661 CERROR("Cannot get mount info for %s!\n", dev);
5664 lsi = s2lsi(lmi->lmi_sb);
5665 LASSERT(lsi->lsi_lmd);
5666 /* CMD is supported only in IAM mode */
5668 rc = kstrtol(num, 10, &node_id);
5672 obd->u.obt.obt_magic = OBT_MAGIC;
5673 if (lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5674 m->mdt_skip_lfsck = 1;
5677 /* Just try to get a DoM lock by default. Otherwise, having a group
5678 * lock granted, it may get blocked for a long time. */
5679 m->mdt_opts.mo_dom_lock = TRYLOCK_DOM_ON_OPEN;
5680 /* DoM files are read at open and data is packed in the reply */
5681 m->mdt_opts.mo_dom_read_open = 1;
5683 m->mdt_squash.rsi_uid = 0;
5684 m->mdt_squash.rsi_gid = 0;
5685 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5686 spin_lock_init(&m->mdt_squash.rsi_lock);
5687 spin_lock_init(&m->mdt_lock);
5688 m->mdt_enable_remote_dir = 1;
5689 m->mdt_enable_striped_dir = 1;
5690 m->mdt_enable_dir_migration = 1;
5691 m->mdt_enable_dir_restripe = 0;
5692 m->mdt_enable_dir_auto_split = 0;
5693 m->mdt_enable_remote_dir_gid = 0;
5694 m->mdt_enable_chprojid_gid = 0;
5695 m->mdt_enable_remote_rename = 1;
5696 m->mdt_dir_restripe_nsonly = 1;
5697 m->mdt_enable_remote_subdir_mount = 1;
5699 atomic_set(&m->mdt_mds_mds_conns, 0);
5700 atomic_set(&m->mdt_async_commit_count, 0);
5702 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5703 m->mdt_lu_dev.ld_obd = obd;
5704 /* Set this lu_device to obd for error handling purposes. */
5705 obd->obd_lu_dev = &m->mdt_lu_dev;
5707 /* init the stack */
5708 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5710 CERROR("%s: Can't init device stack, rc %d\n",
5711 mdt_obd_name(m), rc);
5716 ss_site = mdt_seq_site(m);
5717 s->ld_seq_site = ss_site;
5720 /* set server index */
5721 ss_site->ss_node_id = node_id;
5723 /* failover is the default
5724 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5725 * assumed whose ss_node_id == 0 XXX
5727 obd->obd_replayable = 1;
5728 /* No connection accepted until configurations will finish */
5729 obd->obd_no_conn = 1;
5731 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5732 char *str = lustre_cfg_string(cfg, 4);
5733 if (strchr(str, 'n')) {
5734 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5735 obd->obd_replayable = 0;
5739 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5741 GOTO(err_fini_stack, rc);
5743 rc = mdt_seq_init(env, m);
5745 GOTO(err_fini_fld, rc);
5747 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5748 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5749 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5750 LDLM_NAMESPACE_SERVER,
5751 LDLM_NAMESPACE_GREEDY,
5753 if (IS_ERR(m->mdt_namespace)) {
5754 rc = PTR_ERR(m->mdt_namespace);
5755 CERROR("%s: unable to create server namespace: rc = %d\n",
5757 m->mdt_namespace = NULL;
5758 GOTO(err_fini_seq, rc);
5761 m->mdt_namespace->ns_lvbp = m;
5762 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5764 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5765 /* set obd_namespace for compatibility with old code */
5766 obd->obd_namespace = m->mdt_namespace;
5768 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5769 OBD_FAIL_MDS_ALL_REQUEST_NET,
5770 OBD_FAIL_MDS_ALL_REPLY_NET);
5772 GOTO(err_free_ns, rc);
5774 /* Amount of available space excluded from granting and reserved
5775 * for metadata. It is a percentage of the total MDT size. */
5776 tgd->tgd_reserved_pcnt = 10;
5778 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5779 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5781 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5783 rc = mdt_fs_setup(env, m, obd, lsi);
5787 fid.f_seq = FID_SEQ_LOCAL_NAME;
5790 rc = local_oid_storage_init(env, m->mdt_bottom, &fid, &m->mdt_los);
5792 GOTO(err_fs_cleanup, rc);
5794 rc = mdt_hsm_cdt_init(m);
5796 CERROR("%s: error initializing coordinator, rc %d\n",
5797 mdt_obd_name(m), rc);
5798 GOTO(err_los_fini, rc);
5801 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5803 next = m->mdt_child;
5804 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5806 mntopts = dt_conf->ddp_mntopts;
5808 if (mntopts & MNTOPT_USERXATTR)
5809 m->mdt_opts.mo_user_xattr = 1;
5811 m->mdt_opts.mo_user_xattr = 0;
5813 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5815 if (mntopts & MNTOPT_ACL)
5816 m->mdt_opts.mo_acl = 1;
5818 m->mdt_opts.mo_acl = 0;
5820 /* XXX: to support suppgid for ACL, we enable identity_upcall
5821 * by default, otherwise, maybe got unexpected -EACCESS. */
5822 if (m->mdt_opts.mo_acl)
5823 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5825 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5827 &mdt_identity_upcall_cache_ops);
5828 if (IS_ERR(m->mdt_identity_cache)) {
5829 rc = PTR_ERR(m->mdt_identity_cache);
5830 m->mdt_identity_cache = NULL;
5831 GOTO(err_free_hsm, rc);
5834 rc = mdt_tunables_init(m, dev);
5836 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5837 GOTO(err_recovery, rc);
5840 rc = mdt_quota_init(env, m, cfg);
5842 GOTO(err_procfs, rc);
5844 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5845 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5846 "mdt_ldlm_client", m->mdt_ldlm_client);
5848 ping_evictor_start();
5850 /* recovery will be started upon mdt_prepare()
5851 * when the whole stack is complete and ready
5852 * to serve the requests */
5854 /* Reduce the initial timeout on an MDS because it doesn't need such
5855 * a long timeout as an OST does. Adaptive timeouts will adjust this
5856 * value appropriately. */
5857 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5858 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5860 if ((lsi->lsi_lmd->lmd_flags & LMD_FLG_LOCAL_RECOV))
5861 m->mdt_lut.lut_local_recovery = 1;
5863 rc = mdt_restriper_start(m);
5865 GOTO(err_ping_evictor, rc);
5870 ping_evictor_stop();
5872 mdt_tunables_fini(m);
5874 upcall_cache_cleanup(m->mdt_identity_cache);
5875 m->mdt_identity_cache = NULL;
5877 mdt_hsm_cdt_fini(m);
5879 local_oid_storage_fini(env, m->mdt_los);
5882 mdt_fs_cleanup(env, m);
5884 /* keep recoverable clients */
5886 target_recovery_fini(obd);
5887 obd_exports_barrier(obd);
5888 obd_zombie_barrier();
5889 tgt_fini(env, &m->mdt_lut);
5891 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5892 obd->obd_namespace = m->mdt_namespace = NULL;
5894 mdt_seq_fini(env, m);
5896 mdt_fld_fini(env, m);
5898 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5901 server_put_mount(dev, true);
5905 /* For interoperability, the left element is old parameter, the right one
5906 * is the new version of the parameter, if some parameter is deprecated,
5907 * the new version should be set as NULL. */
5908 static struct cfg_interop_param mdt_interop_param[] = {
5909 { "mdt.group_upcall", NULL },
5910 { "mdt.quota_type", NULL },
5911 { "mdd.quota_type", NULL },
5912 { "mdt.som", NULL },
5913 { "mdt.rootsquash", "mdt.root_squash" },
5914 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5918 /* used by MGS to process specific configurations */
5919 static int mdt_process_config(const struct lu_env *env,
5920 struct lu_device *d, struct lustre_cfg *cfg)
5922 struct mdt_device *m = mdt_dev(d);
5923 struct md_device *md_next = m->mdt_child;
5924 struct lu_device *next = md2lu_dev(md_next);
5928 switch (cfg->lcfg_command) {
5930 struct obd_device *obd = d->ld_obd;
5931 /* For interoperability */
5932 struct cfg_interop_param *ptr = NULL;
5933 struct lustre_cfg *old_cfg = NULL;
5937 param = lustre_cfg_string(cfg, 1);
5938 if (param == NULL) {
5939 CERROR("param is empty\n");
5944 ptr = class_find_old_param(param, mdt_interop_param);
5946 if (ptr->new_param == NULL) {
5948 CWARN("For interoperability, skip this %s."
5949 " It is obsolete.\n", ptr->old_param);
5953 CWARN("Found old param %s, changed it to %s.\n",
5954 ptr->old_param, ptr->new_param);
5957 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5964 count = class_modify_config(cfg, PARAM_MDT,
5965 &obd->obd_kset.kobj);
5967 struct coordinator *cdt = &m->mdt_coordinator;
5969 /* is it an HSM var ? */
5970 count = class_modify_config(cfg, PARAM_HSM,
5971 &cdt->cdt_hsm_kobj);
5973 /* we don't understand; pass it on */
5974 rc = next->ld_ops->ldo_process_config(env, next,
5977 rc = count > 0 ? 0 : count;
5979 rc = count > 0 ? 0 : count;
5983 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5984 cfg->lcfg_buflens));
5988 /* others are passed further */
5989 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5995 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5996 const struct lu_object_header *hdr,
5997 struct lu_device *d)
5999 struct mdt_object *mo;
6003 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
6005 struct lu_object *o;
6006 struct lu_object_header *h;
6009 h = &mo->mot_header;
6010 lu_object_header_init(h);
6011 lu_object_init(o, h, d);
6012 lu_object_add_top(h, o);
6013 o->lo_ops = &mdt_obj_ops;
6014 spin_lock_init(&mo->mot_write_lock);
6015 mutex_init(&mo->mot_som_mutex);
6016 mutex_init(&mo->mot_lov_mutex);
6017 init_rwsem(&mo->mot_dom_sem);
6018 init_rwsem(&mo->mot_open_sem);
6019 atomic_set(&mo->mot_open_count, 0);
6020 mo->mot_restripe_offset = 0;
6021 INIT_LIST_HEAD(&mo->mot_restripe_linkage);
6027 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
6028 const struct lu_object_conf *unused)
6030 struct mdt_device *d = mdt_dev(o->lo_dev);
6031 struct lu_device *under;
6032 struct lu_object *below;
6036 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
6037 PFID(lu_object_fid(o)));
6039 under = &d->mdt_child->md_lu_dev;
6040 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
6041 if (below != NULL) {
6042 lu_object_add(o, below);
6049 static void mdt_object_free_rcu(struct rcu_head *head)
6051 struct mdt_object *mo = container_of(head, struct mdt_object,
6052 mot_header.loh_rcu);
6054 kmem_cache_free(mdt_object_kmem, mo);
6057 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
6059 struct mdt_object *mo = mdt_obj(o);
6060 struct lu_object_header *h;
6064 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
6065 PFID(lu_object_fid(o)));
6067 LASSERT(atomic_read(&mo->mot_open_count) == 0);
6068 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
6071 lu_object_header_fini(h);
6072 OBD_FREE_PRE(mo, sizeof(*mo), "slab-freed");
6073 call_rcu(&mo->mot_header.loh_rcu, mdt_object_free_rcu);
6078 static int mdt_object_print(const struct lu_env *env, void *cookie,
6079 lu_printer_t p, const struct lu_object *o)
6081 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
6083 return (*p)(env, cookie,
6084 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
6085 mdto, mdto->mot_lov_created ? "lov_created" : "",
6086 mdto->mot_cache_attr ? "cache_attr" : "",
6087 mdto->mot_write_count);
6090 static int mdt_prepare(const struct lu_env *env,
6091 struct lu_device *pdev,
6092 struct lu_device *cdev)
6094 struct mdt_device *mdt = mdt_dev(cdev);
6095 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
6096 struct obd_device *obd = cdev->ld_obd;
6103 rc = next->ld_ops->ldo_prepare(env, cdev, next);
6107 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
6111 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
6115 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
6116 /* The LFSCK instance is registered just now, so it must be there when
6117 * register the namespace to such instance. */
6118 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
6120 if (mdt->mdt_seq_site.ss_node_id == 0) {
6121 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
6122 &mdt->mdt_md_root_fid);
6127 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
6129 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
6130 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
6131 LASSERT(obd->obd_no_conn);
6132 spin_lock(&obd->obd_dev_lock);
6133 obd->obd_no_conn = 0;
6134 spin_unlock(&obd->obd_dev_lock);
6136 if (obd->obd_recovering == 0)
6137 mdt_postrecov(env, mdt);
6142 const struct lu_device_operations mdt_lu_ops = {
6143 .ldo_object_alloc = mdt_object_alloc,
6144 .ldo_process_config = mdt_process_config,
6145 .ldo_prepare = mdt_prepare,
6148 static const struct lu_object_operations mdt_obj_ops = {
6149 .loo_object_init = mdt_object_init,
6150 .loo_object_free = mdt_object_free,
6151 .loo_object_print = mdt_object_print
6154 static int mdt_obd_set_info_async(const struct lu_env *env,
6155 struct obd_export *exp,
6156 __u32 keylen, void *key,
6157 __u32 vallen, void *val,
6158 struct ptlrpc_request_set *set)
6164 if (KEY_IS(KEY_SPTLRPC_CONF)) {
6165 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
6172 static inline void mdt_enable_slc(struct mdt_device *mdt)
6174 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_NEVER)
6175 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_BLOCKING;
6178 static inline void mdt_disable_slc(struct mdt_device *mdt)
6180 if (mdt->mdt_lut.lut_sync_lock_cancel == SYNC_LOCK_CANCEL_BLOCKING)
6181 mdt->mdt_lut.lut_sync_lock_cancel = SYNC_LOCK_CANCEL_NEVER;
6185 * Match client and server connection feature flags.
6187 * Compute the compatibility flags for a connection request based on
6188 * features mutually supported by client and server.
6190 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
6191 * must not be updated here, otherwise a partially initialized value may
6192 * be exposed. After the connection request is successfully processed,
6193 * the top-level MDT connect request handler atomically updates the export
6194 * connect flags from the obd_connect_data::ocd_connect_flags field of the
6195 * reply. \see mdt_connect().
6197 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
6198 * full struct obd_connect_data. So care must be taken when accessing fields
6199 * that are not present in struct obd_connect_data_v1. See LU-16.
6201 * \param exp the obd_export associated with this client/target pair
6202 * \param mdt the target device for the connection
6203 * \param data stores data for this connect request
6206 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
6207 * \retval -EBADE client and server feature requirements are incompatible
6209 static int mdt_connect_internal(const struct lu_env *env,
6210 struct obd_export *exp,
6211 struct mdt_device *mdt,
6212 struct obd_connect_data *data, bool reconnect)
6214 const char *obd_name = mdt_obd_name(mdt);
6215 LASSERT(data != NULL);
6217 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
6219 if (mdt->mdt_bottom->dd_rdonly &&
6220 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6221 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
6224 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
6225 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
6227 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
6229 if (!mdt->mdt_opts.mo_acl)
6230 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
6232 if (!mdt->mdt_opts.mo_user_xattr)
6233 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
6235 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
6236 data->ocd_brw_size = min(data->ocd_brw_size,
6238 if (data->ocd_brw_size == 0) {
6239 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
6240 "ocd_version: %x ocd_grant: %d ocd_index: %u "
6241 "ocd_brw_size unexpectedly zero, network data "
6242 "corruption? Refusing to connect this client\n",
6243 obd_name, exp->exp_client_uuid.uuid,
6244 exp, data->ocd_connect_flags, data->ocd_version,
6245 data->ocd_grant, data->ocd_index);
6250 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
6251 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
6253 /* client is reporting its page size, for future use */
6254 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
6255 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
6256 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
6257 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
6258 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
6259 /* ocd_grant_tax_kb is in 1K byte blocks */
6260 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
6261 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
6264 /* Save connect_data we have so far because tgt_grant_connect()
6265 * uses it to calculate grant, and we want to save the client
6266 * version before it is overwritten by LUSTRE_VERSION_CODE. */
6267 exp->exp_connect_data = *data;
6268 if (OCD_HAS_FLAG(data, GRANT))
6269 tgt_grant_connect(env, exp, data, !reconnect);
6271 if (OCD_HAS_FLAG(data, MAXBYTES))
6272 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
6274 /* NB: Disregard the rule against updating
6275 * exp_connect_data.ocd_connect_flags in this case, since
6276 * tgt_client_new() needs to know if this is a lightweight
6277 * connection, and it is safe to expose this flag before
6278 * connection processing completes. */
6279 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
6280 spin_lock(&exp->exp_lock);
6281 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
6282 spin_unlock(&exp->exp_lock);
6285 data->ocd_version = LUSTRE_VERSION_CODE;
6287 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
6288 CWARN("%s: MDS requires FID support, but client not\n",
6293 if (OCD_HAS_FLAG(data, PINGLESS)) {
6294 if (ptlrpc_pinger_suppress_pings()) {
6295 spin_lock(&exp->exp_obd->obd_dev_lock);
6296 list_del_init(&exp->exp_obd_chain_timed);
6297 spin_unlock(&exp->exp_obd->obd_dev_lock);
6299 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
6303 data->ocd_max_easize = mdt->mdt_max_ea_size;
6305 /* NB: Disregard the rule against updating
6306 * exp_connect_data.ocd_connect_flags in this case, since
6307 * tgt_client_new() needs to know if this is client supports
6308 * multiple modify RPCs, and it is safe to expose this flag before
6309 * connection processing completes. */
6310 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
6311 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
6312 spin_lock(&exp->exp_lock);
6313 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
6314 spin_unlock(&exp->exp_lock);
6317 if (OCD_HAS_FLAG(data, CKSUM)) {
6318 __u32 cksum_types = data->ocd_cksum_types;
6320 tgt_mask_cksum_types(&mdt->mdt_lut, &data->ocd_cksum_types);
6322 if (unlikely(data->ocd_cksum_types == 0)) {
6323 CERROR("%s: Connect with checksum support but no "
6324 "ocd_cksum_types is set\n",
6325 exp->exp_obd->obd_name);
6329 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
6330 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
6331 cksum_types, data->ocd_cksum_types);
6333 /* This client does not support OBD_CONNECT_CKSUM
6334 * fall back to CRC32 */
6335 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
6336 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
6337 exp->exp_obd->obd_name, obd_export_nid2str(exp));
6340 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
6341 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
6342 atomic_inc(&mdt->mdt_mds_mds_conns);
6343 mdt_enable_slc(mdt);
6346 if (!mdt->mdt_lut.lut_dt_conf.ddp_has_lseek_data_hole)
6347 data->ocd_connect_flags2 &= ~OBD_CONNECT2_LSEEK;
6352 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
6353 struct mdt_thread_info *info,
6354 struct mdt_file_data *mfd)
6356 struct lu_context ses;
6360 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
6365 lu_context_enter(&ses);
6367 mdt_ucred(info)->uc_valid = UCRED_OLD;
6368 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
6370 lu_context_exit(&ses);
6371 lu_context_fini(&ses);
6377 static int mdt_export_cleanup(struct obd_export *exp)
6379 LIST_HEAD(closing_list);
6380 struct mdt_export_data *med = &exp->exp_mdt_data;
6381 struct obd_device *obd = exp->exp_obd;
6382 struct mdt_device *mdt;
6383 struct mdt_thread_info *info;
6385 struct mdt_file_data *mfd, *n;
6389 spin_lock(&med->med_open_lock);
6390 while (!list_empty(&med->med_open_head)) {
6391 struct list_head *tmp = med->med_open_head.next;
6392 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
6394 /* Remove mfd handle so it can't be found again.
6395 * We are consuming the mfd_list reference here. */
6396 class_handle_unhash(&mfd->mfd_open_handle);
6397 list_move_tail(&mfd->mfd_list, &closing_list);
6399 spin_unlock(&med->med_open_lock);
6400 mdt = mdt_dev(obd->obd_lu_dev);
6401 LASSERT(mdt != NULL);
6403 rc = lu_env_init(&env, LCT_MD_THREAD);
6407 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6408 LASSERT(info != NULL);
6409 memset(info, 0, sizeof *info);
6410 info->mti_env = &env;
6411 info->mti_mdt = mdt;
6412 info->mti_exp = exp;
6414 if (!list_empty(&closing_list)) {
6415 struct md_attr *ma = &info->mti_attr;
6417 /* Close any open files (which may also cause orphan
6419 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
6420 list_del_init(&mfd->mfd_list);
6421 ma->ma_need = ma->ma_valid = 0;
6423 /* This file is being closed due to an eviction, it
6424 * could have been modified and now dirty regarding to
6425 * HSM archive, check this!
6426 * The logic here is to mark a file dirty if there's a
6427 * chance it was dirtied before the client was evicted,
6428 * so that we don't have to wait for a release attempt
6429 * before finding out the file was actually dirty and
6430 * fail the release. Aggressively marking it dirty here
6431 * will cause the policy engine to attempt to
6432 * re-archive it; when rearchiving, we can compare the
6433 * current version to the HSM data_version and make the
6434 * archive request into a noop if it's not actually
6437 if (mfd->mfd_open_flags & MDS_FMODE_WRITE)
6438 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
6440 /* Don't unlink orphan on failover umount, LU-184 */
6441 if (exp->exp_flags & OBD_OPT_FAILOVER) {
6442 ma->ma_valid = MA_FLAGS;
6443 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
6445 ma->ma_valid |= MA_FORCE_LOG;
6446 mdt_mfd_close(info, mfd);
6449 info->mti_mdt = NULL;
6450 /* cleanup client slot early */
6451 /* Do not erase record for recoverable client. */
6452 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
6453 tgt_client_del(&env, exp);
6459 static int mdt_obd_disconnect(struct obd_export *exp)
6466 class_export_get(exp);
6468 if (!(exp->exp_flags & OBD_OPT_FORCE))
6469 tgt_grant_sanity_check(exp->exp_obd, __func__);
6471 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
6472 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
6473 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
6475 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
6476 mdt_disable_slc(mdt);
6479 rc = server_disconnect_export(exp);
6481 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
6483 tgt_grant_discard(exp);
6485 rc = mdt_export_cleanup(exp);
6486 nodemap_del_member(exp);
6487 class_export_put(exp);
6491 /* mds_connect copy */
6492 static int mdt_obd_connect(const struct lu_env *env,
6493 struct obd_export **exp, struct obd_device *obd,
6494 struct obd_uuid *cluuid,
6495 struct obd_connect_data *data,
6498 struct obd_export *lexp;
6499 struct lustre_handle conn = { 0 };
6500 struct mdt_device *mdt;
6502 lnet_nid_t *client_nid = localdata;
6505 LASSERT(env != NULL);
6506 LASSERT(data != NULL);
6508 if (!exp || !obd || !cluuid)
6511 mdt = mdt_dev(obd->obd_lu_dev);
6514 * first, check whether the stack is ready to handle requests
6515 * XXX: probably not very appropriate method is used now
6516 * at some point we should find a better one
6518 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
6519 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
6520 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
6521 rc = obd_get_info(env, mdt->mdt_child_exp,
6522 sizeof(KEY_OSP_CONNECTED),
6523 KEY_OSP_CONNECTED, NULL, NULL);
6526 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
6529 rc = class_connect(&conn, obd, cluuid);
6533 lexp = class_conn2export(&conn);
6534 LASSERT(lexp != NULL);
6536 rc = nodemap_add_member(*client_nid, lexp);
6537 if (rc != 0 && rc != -EEXIST)
6540 rc = mdt_connect_internal(env, lexp, mdt, data, false);
6542 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
6545 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
6546 rc = tgt_client_new(env, lexp);
6548 mdt_export_stats_init(obd, lexp, localdata);
6552 class_disconnect(lexp);
6553 nodemap_del_member(lexp);
6557 /* Because we do not want this export to be evicted by pinger,
6558 * let's not add this export to the timed chain list. */
6559 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
6560 spin_lock(&lexp->exp_obd->obd_dev_lock);
6561 list_del_init(&lexp->exp_obd_chain_timed);
6562 spin_unlock(&lexp->exp_obd->obd_dev_lock);
6569 static int mdt_obd_reconnect(const struct lu_env *env,
6570 struct obd_export *exp, struct obd_device *obd,
6571 struct obd_uuid *cluuid,
6572 struct obd_connect_data *data,
6575 lnet_nid_t *client_nid = localdata;
6579 if (exp == NULL || obd == NULL || cluuid == NULL)
6582 rc = nodemap_add_member(*client_nid, exp);
6583 if (rc != 0 && rc != -EEXIST)
6586 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
6589 mdt_export_stats_init(obd, exp, localdata);
6591 nodemap_del_member(exp);
6596 /* FIXME: Can we avoid using these two interfaces? */
6597 static int mdt_init_export(struct obd_export *exp)
6599 struct mdt_export_data *med = &exp->exp_mdt_data;
6603 INIT_LIST_HEAD(&med->med_open_head);
6604 spin_lock_init(&med->med_open_lock);
6605 spin_lock(&exp->exp_lock);
6606 exp->exp_connecting = 1;
6607 spin_unlock(&exp->exp_lock);
6609 OBD_ALLOC(exp->exp_used_slots,
6610 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6611 if (exp->exp_used_slots == NULL)
6614 /* self-export doesn't need client data and ldlm initialization */
6615 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6616 &exp->exp_client_uuid)))
6619 rc = tgt_client_alloc(exp);
6623 rc = ldlm_init_export(exp);
6630 tgt_client_free(exp);
6632 OBD_FREE(exp->exp_used_slots,
6633 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6634 exp->exp_used_slots = NULL;
6636 CERROR("%s: Failed to initialize export: rc = %d\n",
6637 exp->exp_obd->obd_name, rc);
6641 static int mdt_destroy_export(struct obd_export *exp)
6645 target_destroy_export(exp);
6646 if (exp->exp_used_slots)
6647 OBD_FREE(exp->exp_used_slots,
6648 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
6650 /* destroy can be called from failed obd_setup, so
6651 * checking uuid is safer than obd_self_export */
6652 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6653 &exp->exp_client_uuid)))
6656 ldlm_destroy_export(exp);
6657 tgt_client_free(exp);
6659 LASSERT(list_empty(&exp->exp_outstanding_replies));
6660 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6663 * discard grants once we're sure no more
6664 * interaction with the client is possible
6666 tgt_grant_discard(exp);
6667 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6668 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6670 if (!(exp->exp_flags & OBD_OPT_FORCE))
6671 tgt_grant_sanity_check(exp->exp_obd, __func__);
6676 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6677 struct linkea_data *ldata)
6681 LASSERT(ldata->ld_buf->lb_buf != NULL);
6683 if (!mdt_object_exists(mdt_obj))
6686 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6687 ldata->ld_buf, XATTR_NAME_LINK);
6688 if (rc == -ERANGE) {
6689 /* Buf was too small, figure out what we need. */
6690 lu_buf_free(ldata->ld_buf);
6691 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6692 ldata->ld_buf, XATTR_NAME_LINK);
6695 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6696 if (ldata->ld_buf->lb_buf == NULL)
6698 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6699 ldata->ld_buf, XATTR_NAME_LINK);
6704 return linkea_init_with_rec(ldata);
6708 * Given an MDT object, try to look up the full path to the object.
6709 * Part of the MDT layer implementation of lfs fid2path.
6711 * \param[in] info Per-thread common data shared by MDT level handlers.
6712 * \param[in] obj Object to do path lookup of
6713 * \param[in,out] fp User-provided struct to store path information
6714 * \param[in] root_fid Root FID of current path should reach
6716 * \retval 0 Lookup successful, path information stored in fp
6717 * \retval -EAGAIN Lookup failed, usually because object is being moved
6718 * \retval negative errno if there was a problem
6720 static int mdt_path_current(struct mdt_thread_info *info,
6721 struct mdt_object *obj,
6722 struct getinfo_fid2path *fp,
6723 struct lu_fid *root_fid)
6725 struct mdt_device *mdt = info->mti_mdt;
6726 struct lu_name *tmpname = &info->mti_name;
6727 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6728 struct lu_buf *buf = &info->mti_big_buf;
6729 struct md_attr *ma = &info->mti_attr;
6730 struct linkea_data ldata = { NULL };
6732 struct mdt_object *mdt_obj;
6733 struct link_ea_header *leh;
6734 struct link_ea_entry *lee;
6741 /* temp buffer for path element, the buffer will be finally freed
6742 * in mdt_thread_info_fini */
6743 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6744 if (buf->lb_buf == NULL)
6748 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6751 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6753 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6754 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6755 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6756 GOTO(out, rc = -ENOENT);
6758 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6760 mdt_object_get(info->mti_env, mdt_obj);
6762 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6763 if (IS_ERR(mdt_obj))
6764 GOTO(out, rc = PTR_ERR(mdt_obj));
6767 if (!mdt_object_exists(mdt_obj)) {
6768 mdt_object_put(info->mti_env, mdt_obj);
6769 GOTO(out, rc = -ENOENT);
6772 if (mdt_object_remote(mdt_obj)) {
6773 mdt_object_put(info->mti_env, mdt_obj);
6774 GOTO(remote_out, rc = -EREMOTE);
6777 rc = mdt_links_read(info, mdt_obj, &ldata);
6779 mdt_object_put(info->mti_env, mdt_obj);
6784 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6785 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6786 /* If set, use link #linkno for path lookup, otherwise use
6787 link #0. Only do this for the final path element. */
6788 if (first && fp->gf_linkno < leh->leh_reccount) {
6790 for (count = 0; count < fp->gf_linkno; count++) {
6791 lee = (struct link_ea_entry *)
6792 ((char *)lee + reclen);
6793 linkea_entry_unpack(lee, &reclen, tmpname,
6796 if (fp->gf_linkno < leh->leh_reccount - 1)
6797 /* indicate to user there are more links */
6801 /* Check if it is slave stripes */
6802 rc = mdt_stripe_get(info, mdt_obj, ma, XATTR_NAME_LMV);
6803 mdt_object_put(info->mti_env, mdt_obj);
6807 if (ma->ma_valid & MA_LMV) {
6808 struct lmv_mds_md_v1 *lmv = &ma->ma_lmv->lmv_md_v1;
6810 if (!lmv_is_sane2(lmv))
6811 GOTO(out, rc = -EBADF);
6813 /* For slave stripes, get its master */
6814 if (le32_to_cpu(lmv->lmv_magic) == LMV_MAGIC_STRIPE) {
6815 fp->gf_fid = *tmpfid;
6820 /* Pack the name in the end of the buffer */
6821 ptr -= tmpname->ln_namelen;
6822 if (ptr - 1 <= fp->gf_u.gf_path)
6823 GOTO(out, rc = -EOVERFLOW);
6824 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6827 /* keep the last resolved fid to the client, so the
6828 * client will build the left path on another MDT for
6830 fp->gf_fid = *tmpfid;
6835 /* non-zero will be treated as an error */
6839 ptr++; /* skip leading / */
6840 memmove(fp->gf_u.gf_path, ptr,
6841 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6848 * Given an MDT object, use mdt_path_current to get the path.
6849 * Essentially a wrapper to retry mdt_path_current a set number of times
6850 * if -EAGAIN is returned (usually because an object is being moved).
6852 * Part of the MDT layer implementation of lfs fid2path.
6854 * \param[in] info Per-thread common data shared by mdt level handlers.
6855 * \param[in] obj Object to do path lookup of
6856 * \param[in,out] fp User-provided struct for arguments and to store path
6859 * \retval 0 Lookup successful, path information stored in fp
6860 * \retval negative errno if there was a problem
6862 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6863 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6865 struct mdt_device *mdt = info->mti_mdt;
6870 if (fp->gf_pathlen < 3)
6873 if (root_fid == NULL)
6874 root_fid = &mdt->mdt_md_root_fid;
6876 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6877 fp->gf_u.gf_path[0] = '\0';
6881 /* Retry multiple times in case file is being moved */
6882 while (tries-- && rc == -EAGAIN)
6883 rc = mdt_path_current(info, obj, fp, root_fid);
6889 * Get the full path of the provided FID, as of changelog record recno.
6891 * This checks sanity and looks up object for user provided FID
6892 * before calling the actual path lookup code.
6894 * Part of the MDT layer implementation of lfs fid2path.
6896 * \param[in] info Per-thread common data shared by mdt level handlers.
6897 * \param[in,out] fp User-provided struct for arguments and to store path
6900 * \retval 0 Lookup successful, path information and recno stored in fp
6901 * \retval -ENOENT, object does not exist
6902 * \retval negative errno if there was a problem
6904 static int mdt_fid2path(struct mdt_thread_info *info,
6905 struct lu_fid *root_fid,
6906 struct getinfo_fid2path *fp)
6908 struct mdt_device *mdt = info->mti_mdt;
6909 struct mdt_object *obj;
6913 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6914 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6916 if (!fid_is_sane(&fp->gf_fid))
6919 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6920 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6921 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6922 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6926 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6929 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6930 PFID(&fp->gf_fid), rc);
6934 if (mdt_object_remote(obj))
6936 else if (!mdt_object_exists(obj))
6942 mdt_object_put(info->mti_env, obj);
6943 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6944 PFID(&fp->gf_fid), rc);
6948 rc = mdt_path(info, obj, fp, root_fid);
6950 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6951 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6952 fp->gf_recno, fp->gf_linkno);
6954 mdt_object_put(info->mti_env, obj);
6959 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6960 void *val, int vallen)
6962 struct getinfo_fid2path *fpout, *fpin;
6963 struct lu_fid *root_fid = NULL;
6966 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6969 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6970 lustre_swab_fid2path(fpin);
6972 memcpy(fpout, fpin, sizeof(*fpin));
6973 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6976 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6977 sizeof(struct lu_fid)) {
6978 /* client sent its root FID, which is normally fileset FID */
6979 root_fid = fpin->gf_u.gf_root_fid;
6980 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6981 lustre_swab_lu_fid(root_fid);
6983 if (root_fid != NULL && !fid_is_sane(root_fid))
6987 rc = mdt_fid2path(info, root_fid, fpout);
6991 int mdt_get_info(struct tgt_session_info *tsi)
7001 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
7003 CDEBUG(D_IOCTL, "No GETINFO key\n");
7004 RETURN(err_serious(-EFAULT));
7006 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
7009 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
7010 if (vallen == NULL) {
7011 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
7012 tgt_name(tsi->tsi_tgt));
7013 RETURN(err_serious(-EFAULT));
7016 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
7018 rc = req_capsule_server_pack(tsi->tsi_pill);
7020 RETURN(err_serious(rc));
7022 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
7023 if (valout == NULL) {
7024 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
7025 tgt_name(tsi->tsi_tgt));
7026 RETURN(err_serious(-EFAULT));
7029 if (KEY_IS(KEY_FID2PATH)) {
7030 struct mdt_thread_info *info = tsi2mdt_info(tsi);
7032 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
7033 mdt_thread_info_fini(info);
7040 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
7042 struct obd_ioctl_data *data = karg;
7045 struct mdt_object *obj;
7046 struct mdt_lock_handle *lh;
7050 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
7051 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
7054 fid = (struct lu_fid *)data->ioc_inlbuf1;
7056 if (!fid_is_sane(fid))
7059 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
7061 lh = &mti->mti_lh[MDT_LH_PARENT];
7062 mdt_lock_reg_init(lh, LCK_CR);
7064 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
7066 RETURN(PTR_ERR(obj));
7068 if (mdt_object_remote(obj)) {
7071 * before calling version get the correct MDS should be
7072 * fid, this is error to find remote object here
7074 CERROR("nonlocal object "DFID"\n", PFID(fid));
7075 } else if (!mdt_object_exists(obj)) {
7076 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
7079 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
7080 *(__u64 *)data->ioc_inlbuf2 = version;
7083 mdt_object_unlock_put(mti, obj, lh, 1);
7087 /* ioctls on obd dev */
7088 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
7089 void *karg, void __user *uarg)
7092 struct obd_device *obd = exp->exp_obd;
7093 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
7094 struct dt_device *dt = mdt->mdt_bottom;
7098 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
7099 rc = lu_env_init(&env, LCT_MD_THREAD);
7105 rc = mdt_device_sync(&env, mdt);
7107 case OBD_IOC_SET_READONLY:
7108 rc = dt_sync(&env, dt);
7110 rc = dt_ro(&env, dt);
7112 case OBD_IOC_ABORT_RECOVERY: {
7113 struct obd_ioctl_data *data = karg;
7115 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
7116 if (data->ioc_type & OBD_FLG_ABORT_RECOV_MDT)
7117 obd->obd_abort_recov_mdt = 1;
7118 else /* if (data->ioc_type & OBD_FLG_ABORT_RECOV_OST) */
7119 /* lctl didn't set OBD_FLG_ABORT_RECOV_OST < 2.13.57 */
7120 obd->obd_abort_recovery = 1;
7122 target_stop_recovery_thread(obd);
7126 case OBD_IOC_CHANGELOG_REG:
7127 case OBD_IOC_CHANGELOG_DEREG:
7128 case OBD_IOC_CHANGELOG_CLEAR:
7129 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
7133 case OBD_IOC_START_LFSCK: {
7134 struct md_device *next = mdt->mdt_child;
7135 struct obd_ioctl_data *data = karg;
7136 struct lfsck_start_param lsp;
7138 if (unlikely(data == NULL)) {
7143 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
7144 lsp.lsp_index_valid = 0;
7145 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
7148 case OBD_IOC_STOP_LFSCK: {
7149 struct md_device *next = mdt->mdt_child;
7150 struct obd_ioctl_data *data = karg;
7151 struct lfsck_stop stop;
7153 stop.ls_status = LS_STOPPED;
7154 /* Old lfsck utils may pass NULL @stop. */
7155 if (data->ioc_inlbuf1 == NULL)
7159 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
7161 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
7164 case OBD_IOC_QUERY_LFSCK: {
7165 struct md_device *next = mdt->mdt_child;
7166 struct obd_ioctl_data *data = karg;
7168 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
7172 case OBD_IOC_GET_OBJ_VERSION: {
7173 struct mdt_thread_info *mti;
7174 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7175 memset(mti, 0, sizeof *mti);
7176 mti->mti_env = &env;
7180 rc = mdt_ioc_version_get(mti, karg);
7183 case OBD_IOC_CATLOGLIST: {
7184 struct mdt_thread_info *mti;
7186 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
7187 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
7188 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
7189 &mti->mti_tmp_fid1);
7194 CERROR("%s: Not supported cmd = %d, rc = %d\n",
7195 mdt_obd_name(mdt), cmd, rc);
7202 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
7204 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
7208 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
7209 struct lfsck_start_param lsp;
7211 lsp.lsp_start = NULL;
7212 lsp.lsp_index_valid = 0;
7213 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
7214 OBD_IOC_START_LFSCK,
7216 if (rc != 0 && rc != -EALREADY)
7217 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
7218 mdt_obd_name(mdt), rc);
7221 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
7225 static int mdt_obd_postrecov(struct obd_device *obd)
7230 rc = lu_env_init(&env, LCT_MD_THREAD);
7233 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
7238 static const struct obd_ops mdt_obd_device_ops = {
7239 .o_owner = THIS_MODULE,
7240 .o_set_info_async = mdt_obd_set_info_async,
7241 .o_connect = mdt_obd_connect,
7242 .o_reconnect = mdt_obd_reconnect,
7243 .o_disconnect = mdt_obd_disconnect,
7244 .o_init_export = mdt_init_export,
7245 .o_destroy_export = mdt_destroy_export,
7246 .o_iocontrol = mdt_iocontrol,
7247 .o_postrecov = mdt_obd_postrecov,
7248 /* Data-on-MDT IO methods */
7249 .o_preprw = mdt_obd_preprw,
7250 .o_commitrw = mdt_obd_commitrw,
7253 static struct lu_device* mdt_device_fini(const struct lu_env *env,
7254 struct lu_device *d)
7256 struct mdt_device *m = mdt_dev(d);
7263 static struct lu_device *mdt_device_free(const struct lu_env *env,
7264 struct lu_device *d)
7266 struct mdt_device *m = mdt_dev(d);
7269 lu_device_fini(&m->mdt_lu_dev);
7275 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
7276 struct lu_device_type *t,
7277 struct lustre_cfg *cfg)
7279 struct lu_device *l;
7280 struct mdt_device *m;
7287 rc = mdt_init0(env, m, t, cfg);
7289 mdt_device_free(env, l);
7294 l = ERR_PTR(-ENOMEM);
7298 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
7299 LU_KEY_INIT(mdt, struct mdt_thread_info);
7301 static void mdt_key_fini(const struct lu_context *ctx,
7302 struct lu_context_key *key, void* data)
7304 struct mdt_thread_info *info = data;
7306 if (info->mti_big_lmm) {
7307 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
7308 info->mti_big_lmm = NULL;
7309 info->mti_big_lmmsize = 0;
7312 if (info->mti_big_acl) {
7313 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
7314 info->mti_big_acl = NULL;
7315 info->mti_big_aclsize = 0;
7321 /* context key: mdt_thread_key */
7322 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
7324 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
7326 return lu_ucred(info->mti_env);
7329 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
7331 return lu_ucred_check(info->mti_env);
7335 * Enable/disable COS (Commit On Sharing).
7337 * Set/Clear the COS flag in mdt options.
7339 * \param mdt mdt device
7340 * \param val 0 disables COS, other values enable COS
7342 void mdt_enable_cos(struct mdt_device *mdt, bool val)
7347 mdt->mdt_opts.mo_cos = val;
7348 rc = lu_env_init(&env, LCT_LOCAL);
7349 if (unlikely(rc != 0)) {
7350 CWARN("%s: lu_env initialization failed, cannot "
7351 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
7354 mdt_device_sync(&env, mdt);
7359 * Check COS (Commit On Sharing) status.
7361 * Return COS flag status.
7363 * \param mdt mdt device
7365 int mdt_cos_is_enabled(struct mdt_device *mdt)
7367 return mdt->mdt_opts.mo_cos != 0;
7370 static const struct lu_device_type_operations mdt_device_type_ops = {
7371 .ldto_device_alloc = mdt_device_alloc,
7372 .ldto_device_free = mdt_device_free,
7373 .ldto_device_fini = mdt_device_fini
7376 static struct lu_device_type mdt_device_type = {
7377 .ldt_tags = LU_DEVICE_MD,
7378 .ldt_name = LUSTRE_MDT_NAME,
7379 .ldt_ops = &mdt_device_type_ops,
7380 .ldt_ctx_tags = LCT_MD_THREAD
7383 static int __init mdt_init(void)
7387 BUILD_BUG_ON(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") !=
7388 FID_NOBRACE_LEN + 1);
7389 BUILD_BUG_ON(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") !=
7391 rc = lu_kmem_init(mdt_caches);
7395 rc = mds_mod_init();
7399 rc = class_register_type(&mdt_obd_device_ops, NULL, true,
7400 LUSTRE_MDT_NAME, &mdt_device_type);
7405 lu_kmem_fini(mdt_caches);
7412 static void __exit mdt_exit(void)
7414 class_unregister_type(LUSTRE_MDT_NAME);
7416 lu_kmem_fini(mdt_caches);
7419 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
7420 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
7421 MODULE_VERSION(LUSTRE_VERSION_STRING);
7422 MODULE_LICENSE("GPL");
7424 module_init(mdt_init);
7425 module_exit(mdt_exit);