4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
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"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
110 static struct lu_kmem_descr mdt_caches[] = {
112 .ckd_cache = &mdt_object_kmem,
113 .ckd_name = "mdt_obj",
114 .ckd_size = sizeof(struct mdt_object)
117 .ckd_cache = &mdt_hsm_cdt_kmem,
118 .ckd_name = "mdt_cdt_restore_handle",
119 .ckd_size = sizeof(struct cdt_restore_handle)
122 .ckd_cache = &mdt_hsm_car_kmem,
123 .ckd_name = "mdt_cdt_agent_req",
124 .ckd_size = sizeof(struct cdt_agent_req)
131 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
135 return rep->lock_policy_res1 & op_flag;
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139 struct ldlm_reply *rep, __u64 op_flag)
142 info->mti_opdata &= ~op_flag;
143 tgt_opdata_clear(info->mti_env, op_flag);
146 rep->lock_policy_res1 &= ~op_flag;
149 void mdt_set_disposition(struct mdt_thread_info *info,
150 struct ldlm_reply *rep, __u64 op_flag)
153 info->mti_opdata |= op_flag;
154 tgt_opdata_set(info->mti_env, op_flag);
157 rep->lock_policy_res1 |= op_flag;
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
162 lh->mlh_pdo_hash = 0;
163 lh->mlh_reg_mode = lm;
164 lh->mlh_rreg_mode = lm;
165 lh->mlh_type = MDT_REG_LOCK;
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
169 const struct lu_name *lname)
171 lh->mlh_reg_mode = lock_mode;
172 lh->mlh_pdo_mode = LCK_MINMODE;
173 lh->mlh_rreg_mode = lock_mode;
174 lh->mlh_type = MDT_PDO_LOCK;
176 if (lu_name_is_valid(lname)) {
177 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
179 /* XXX Workaround for LU-2856
181 * Zero is a valid return value of full_name_hash, but
182 * several users of mlh_pdo_hash assume a non-zero
183 * hash value. We therefore map zero onto an
184 * arbitrary, but consistent value (1) to avoid
185 * problems further down the road. */
186 if (unlikely(lh->mlh_pdo_hash == 0))
187 lh->mlh_pdo_hash = 1;
189 lh->mlh_pdo_hash = 0;
193 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
194 struct mdt_lock_handle *lh)
200 * Any dir access needs couple of locks:
202 * 1) on part of dir we gonna take lookup/modify;
204 * 2) on whole dir to protect it from concurrent splitting and/or to
205 * flush client's cache for readdir().
207 * so, for a given mode and object this routine decides what lock mode
208 * to use for lock #2:
210 * 1) if caller's gonna lookup in dir then we need to protect dir from
211 * being splitted only - LCK_CR
213 * 2) if caller's gonna modify dir then we need to protect dir from
214 * being splitted and to flush cache - LCK_CW
216 * 3) if caller's gonna modify dir and that dir seems ready for
217 * splitting then we need to protect it from any type of access
218 * (lookup/modify/split) - LCK_EX --bzzz
221 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
222 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
225 * Ask underlaying level its opinion about preferable PDO lock mode
226 * having access type passed as regular lock mode:
228 * - MDL_MINMODE means that lower layer does not want to specify lock
231 * - MDL_NL means that no PDO lock should be taken. This is used in some
232 * cases. Say, for non-splittable directories no need to use PDO locks
235 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
236 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
238 if (mode != MDL_MINMODE) {
239 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
242 * Lower layer does not want to specify locking mode. We do it
243 * our selves. No special protection is needed, just flush
244 * client's cache on modification and allow concurrent
247 switch (lh->mlh_reg_mode) {
249 lh->mlh_pdo_mode = LCK_EX;
252 lh->mlh_pdo_mode = LCK_CR;
255 lh->mlh_pdo_mode = LCK_CW;
258 CERROR("Not expected lock type (0x%x)\n",
259 (int)lh->mlh_reg_mode);
264 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
268 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
271 struct mdt_device *mdt = info->mti_mdt;
272 struct lu_name *lname = &info->mti_name;
274 struct mdt_object *parent;
278 LASSERT(!info->mti_cross_ref);
280 OBD_ALLOC(name, NAME_MAX + 1);
283 lname->ln_name = name;
286 * We may want to allow this to mount a completely separate
287 * fileset from the MDT in the future, but keeping it to
288 * ROOT/ only for now avoid potential security issues.
290 *fid = mdt->mdt_md_root_fid;
292 while (rc == 0 && fileset != NULL && *fileset != '\0') {
293 const char *s1 = fileset;
299 while (*s2 != '/' && *s2 != '\0')
307 lname->ln_namelen = s2 - s1;
308 if (lname->ln_namelen > NAME_MAX) {
313 /* reject .. as a path component */
314 if (lname->ln_namelen == 2 &&
315 strncmp(s1, "..", 2) == 0) {
320 strncpy(name, s1, lname->ln_namelen);
321 name[lname->ln_namelen] = '\0';
323 parent = mdt_object_find(info->mti_env, mdt, fid);
324 if (IS_ERR(parent)) {
325 rc = PTR_ERR(parent);
328 /* Only got the fid of this obj by name */
330 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
331 fid, &info->mti_spec);
332 mdt_object_put(info->mti_env, parent);
335 parent = mdt_object_find(info->mti_env, mdt, fid);
337 rc = PTR_ERR(parent);
339 mode = lu_object_attr(&parent->mot_obj);
340 mdt_object_put(info->mti_env, parent);
346 OBD_FREE(name, NAME_MAX + 1);
351 static int mdt_get_root(struct tgt_session_info *tsi)
353 struct mdt_thread_info *info = tsi2mdt_info(tsi);
354 struct mdt_device *mdt = info->mti_mdt;
355 struct mdt_body *repbody;
356 char *fileset = NULL, *buffer = NULL;
358 struct obd_export *exp = info->mti_exp;
359 char *nodemap_fileset;
363 rc = mdt_check_ucred(info);
365 GOTO(out, rc = err_serious(rc));
367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
368 GOTO(out, rc = err_serious(-ENOMEM));
370 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
371 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
372 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
374 GOTO(out, rc = err_serious(-EFAULT));
377 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
378 if (nodemap_fileset && nodemap_fileset[0]) {
379 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
381 /* consider fileset from client as a sub-fileset
382 * of the nodemap one */
383 OBD_ALLOC(buffer, PATH_MAX + 1);
385 GOTO(out, rc = err_serious(-ENOMEM));
386 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
387 nodemap_fileset, fileset) >= PATH_MAX + 1)
388 GOTO(out, rc = err_serious(-EINVAL));
391 /* enforce fileset as specified in the nodemap */
392 fileset = nodemap_fileset;
397 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
398 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
400 GOTO(out, rc = err_serious(rc));
402 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
404 repbody->mbo_valid |= OBD_MD_FLID;
408 mdt_thread_info_fini(info);
410 OBD_FREE(buffer, PATH_MAX+1);
414 static int mdt_statfs(struct tgt_session_info *tsi)
416 struct ptlrpc_request *req = tgt_ses_req(tsi);
417 struct mdt_thread_info *info = tsi2mdt_info(tsi);
418 struct mdt_device *mdt = info->mti_mdt;
419 struct tg_grants_data *tgd = &mdt->mdt_lut.lut_tgd;
420 struct ptlrpc_service_part *svcpt;
421 struct obd_statfs *osfs;
426 svcpt = req->rq_rqbd->rqbd_svcpt;
428 /* This will trigger a watchdog timeout */
429 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
430 (MDT_SERVICE_WATCHDOG_FACTOR *
431 at_get(&svcpt->scp_at_estimate)) + 1);
433 rc = mdt_check_ucred(info);
435 GOTO(out, rc = err_serious(rc));
437 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
438 GOTO(out, rc = err_serious(-ENOMEM));
440 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
442 GOTO(out, rc = -EPROTO);
444 rc = tgt_statfs_internal(tsi->tsi_env, &mdt->mdt_lut, osfs,
445 ktime_get_seconds() - OBD_STATFS_CACHE_SECONDS,
450 /* at least try to account for cached pages. its still racy and
451 * might be under-reporting if clients haven't announced their
452 * caches with brw recently */
453 CDEBUG(D_SUPER | D_CACHE, "blocks cached %llu granted %llu"
454 " pending %llu free %llu avail %llu\n",
455 tgd->tgd_tot_dirty, tgd->tgd_tot_granted,
456 tgd->tgd_tot_pending,
457 osfs->os_bfree << tgd->tgd_blockbits,
458 osfs->os_bavail << tgd->tgd_blockbits);
460 osfs->os_bavail -= min_t(u64, osfs->os_bavail,
461 ((tgd->tgd_tot_dirty + tgd->tgd_tot_pending +
462 osfs->os_bsize - 1) >> tgd->tgd_blockbits));
464 tgt_grant_sanity_check(mdt->mdt_lu_dev.ld_obd, __func__);
465 CDEBUG(D_CACHE, "%llu blocks: %llu free, %llu avail; "
466 "%llu objects: %llu free; state %x\n",
467 osfs->os_blocks, osfs->os_bfree, osfs->os_bavail,
468 osfs->os_files, osfs->os_ffree, osfs->os_state);
470 if (!exp_grant_param_supp(tsi->tsi_exp) &&
471 tgd->tgd_blockbits > COMPAT_BSIZE_SHIFT) {
472 /* clients which don't support OBD_CONNECT_GRANT_PARAM
473 * should not see a block size > page size, otherwise
474 * cl_lost_grant goes mad. Therefore, we emulate a 4KB (=2^12)
475 * block size which is the biggest block size known to work
476 * with all client's page size. */
477 osfs->os_blocks <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
478 osfs->os_bfree <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
479 osfs->os_bavail <<= tgd->tgd_blockbits - COMPAT_BSIZE_SHIFT;
480 osfs->os_bsize = 1 << COMPAT_BSIZE_SHIFT;
483 mdt_counter_incr(req, LPROC_MDT_STATFS);
485 mdt_thread_info_fini(info);
490 * Pack size attributes into the reply.
492 int mdt_pack_size2body(struct mdt_thread_info *info,
493 const struct lu_fid *fid, bool dom_lock)
496 struct md_attr *ma = &info->mti_attr;
501 LASSERT(ma->ma_attr.la_valid & LA_MODE);
503 if (!S_ISREG(ma->ma_attr.la_mode) ||
504 !(ma->ma_valid & MA_LOV && ma->ma_lmm != NULL))
507 dom_stripe = mdt_lmm_dom_entry(ma->ma_lmm);
508 /* no DoM stripe, no size in reply */
509 if (dom_stripe == LMM_NO_DOM)
512 /* no DoM lock, no size in reply */
516 /* Either DoM lock exists or LMM has only DoM stripe then
517 * return size on body. */
518 b = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
520 mdt_dom_object_size(info->mti_env, info->mti_mdt, fid, b, dom_lock);
524 #ifdef CONFIG_FS_POSIX_ACL
526 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
528 * \param info thread info object
529 * \param repbody reply to pack ACLs into
530 * \param o mdt object of file to examine
531 * \param nodemap nodemap of client to reply to
533 * \retval -errno error getting or parsing ACL from disk
535 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
536 struct mdt_object *o, struct lu_nodemap *nodemap)
538 const struct lu_env *env = info->mti_env;
539 struct md_object *next = mdt_object_child(o);
540 struct lu_buf *buf = &info->mti_buf;
541 struct mdt_device *mdt = info->mti_mdt;
542 struct req_capsule *pill = info->mti_pill;
547 buf->lb_buf = req_capsule_server_get(pill, &RMF_ACL);
548 buf->lb_len = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
549 if (buf->lb_len == 0)
553 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
555 if (rc == -ENODATA) {
556 repbody->mbo_aclsize = 0;
557 repbody->mbo_valid |= OBD_MD_FLACL;
559 } else if (rc == -EOPNOTSUPP) {
563 exp_connect_large_acl(info->mti_exp) &&
564 buf->lb_buf != info->mti_big_acl) {
565 if (info->mti_big_acl == NULL) {
566 OBD_ALLOC_LARGE(info->mti_big_acl,
567 mdt->mdt_max_ea_size);
568 if (info->mti_big_acl == NULL) {
569 CERROR("%s: unable to grow "
572 PFID(mdt_object_fid(o)));
576 info->mti_big_aclsize =
577 mdt->mdt_max_ea_size;
580 CDEBUG(D_INODE, "%s: grow the "DFID
581 " ACL buffer to size %d\n",
583 PFID(mdt_object_fid(o)),
584 mdt->mdt_max_ea_size);
586 buf->lb_buf = info->mti_big_acl;
587 buf->lb_len = info->mti_big_aclsize;
592 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
593 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
602 acl_buflen = req_capsule_get_size(pill, &RMF_ACL, RCL_SERVER);
603 if (acl_buflen >= rc)
606 /* If LOV/LMA EA is small, we can reuse part of their buffer */
607 client = ptlrpc_req_get_repsize(pill->rc_req);
608 server = lustre_packed_msg_size(pill->rc_req->rq_repmsg);
609 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER)) {
610 lmm_buflen = req_capsule_get_size(pill, &RMF_MDT_MD,
612 lmmsize = repbody->mbo_eadatasize;
615 if (client < server - acl_buflen - lmm_buflen + rc + lmmsize) {
616 CDEBUG(D_INODE, "%s: client prepared buffer size %d "
617 "is not big enough with the ACL size %d (%d)\n",
618 mdt_obd_name(mdt), client, rc,
619 server - acl_buflen - lmm_buflen + rc + lmmsize);
620 repbody->mbo_aclsize = 0;
621 repbody->mbo_valid &= ~OBD_MD_FLACL;
626 if (buf->lb_buf == info->mti_big_acl)
627 info->mti_big_acl_used = 1;
629 rc = nodemap_map_acl(nodemap, buf->lb_buf,
630 rc, NODEMAP_FS_TO_CLIENT);
631 /* if all ACLs mapped out, rc is still >= 0 */
633 CERROR("%s: nodemap_map_acl unable to parse "DFID
634 " ACL: rc = %d\n", mdt_obd_name(mdt),
635 PFID(mdt_object_fid(o)), rc);
636 repbody->mbo_aclsize = 0;
637 repbody->mbo_valid &= ~OBD_MD_FLACL;
639 repbody->mbo_aclsize = rc;
640 repbody->mbo_valid |= OBD_MD_FLACL;
649 /* XXX Look into layout in MDT layer. */
650 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
652 struct lov_comp_md_v1 *comp_v1;
653 struct lov_mds_md *v1;
656 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
657 comp_v1 = (struct lov_comp_md_v1 *)lmm;
659 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
660 v1 = (struct lov_mds_md *)((char *)comp_v1 +
661 comp_v1->lcm_entries[i].lcme_offset);
662 /* We don't support partial release for now */
663 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
668 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
673 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
674 const struct lu_attr *attr, const struct lu_fid *fid)
676 struct md_attr *ma = &info->mti_attr;
677 struct obd_export *exp = info->mti_exp;
678 struct lu_nodemap *nodemap = NULL;
680 LASSERT(ma->ma_valid & MA_INODE);
682 if (attr->la_valid & LA_ATIME) {
683 b->mbo_atime = attr->la_atime;
684 b->mbo_valid |= OBD_MD_FLATIME;
686 if (attr->la_valid & LA_MTIME) {
687 b->mbo_mtime = attr->la_mtime;
688 b->mbo_valid |= OBD_MD_FLMTIME;
690 if (attr->la_valid & LA_CTIME) {
691 b->mbo_ctime = attr->la_ctime;
692 b->mbo_valid |= OBD_MD_FLCTIME;
694 if (attr->la_valid & LA_FLAGS) {
695 b->mbo_flags = attr->la_flags;
696 b->mbo_valid |= OBD_MD_FLFLAGS;
698 if (attr->la_valid & LA_NLINK) {
699 b->mbo_nlink = attr->la_nlink;
700 b->mbo_valid |= OBD_MD_FLNLINK;
702 if (attr->la_valid & (LA_UID|LA_GID)) {
703 nodemap = nodemap_get_from_exp(exp);
707 if (attr->la_valid & LA_UID) {
708 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
709 NODEMAP_FS_TO_CLIENT,
711 b->mbo_valid |= OBD_MD_FLUID;
713 if (attr->la_valid & LA_GID) {
714 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
715 NODEMAP_FS_TO_CLIENT,
717 b->mbo_valid |= OBD_MD_FLGID;
720 if (attr->la_valid & LA_PROJID) {
721 /* TODO, nodemap for project id */
722 b->mbo_projid = attr->la_projid;
723 b->mbo_valid |= OBD_MD_FLPROJID;
726 b->mbo_mode = attr->la_mode;
727 if (attr->la_valid & LA_MODE)
728 b->mbo_valid |= OBD_MD_FLMODE;
729 if (attr->la_valid & LA_TYPE)
730 b->mbo_valid |= OBD_MD_FLTYPE;
734 b->mbo_valid |= OBD_MD_FLID;
735 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
736 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
739 if (!(attr->la_valid & LA_TYPE))
742 b->mbo_rdev = attr->la_rdev;
743 b->mbo_size = attr->la_size;
744 b->mbo_blocks = attr->la_blocks;
746 if (!S_ISREG(attr->la_mode)) {
747 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
748 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
749 /* means no objects are allocated on osts. */
750 LASSERT(!(ma->ma_valid & MA_LOV));
751 /* just ignore blocks occupied by extend attributes on MDS */
753 /* if no object is allocated on osts, the size on mds is valid.
755 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
756 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
757 if (mdt_hsm_is_released(ma->ma_lmm)) {
758 /* A released file stores its size on MDS. */
759 /* But return 1 block for released file, unless tools
760 * like tar will consider it fully sparse. (LU-3864)
762 if (unlikely(b->mbo_size == 0))
766 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
767 } else if (info->mti_som_valid) { /* som is valid */
768 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
772 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
773 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
774 PFID(fid), (unsigned long long)b->mbo_size);
777 if (!IS_ERR_OR_NULL(nodemap))
778 nodemap_putref(nodemap);
781 static inline int mdt_body_has_lov(const struct lu_attr *la,
782 const struct mdt_body *body)
784 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
785 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
788 void mdt_client_compatibility(struct mdt_thread_info *info)
790 struct mdt_body *body;
791 struct ptlrpc_request *req = mdt_info_req(info);
792 struct obd_export *exp = req->rq_export;
793 struct md_attr *ma = &info->mti_attr;
794 struct lu_attr *la = &ma->ma_attr;
797 if (exp_connect_layout(exp))
798 /* the client can deal with 16-bit lmm_stripe_count */
801 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
803 if (!mdt_body_has_lov(la, body))
806 /* now we have a reply with a lov for a client not compatible with the
807 * layout lock so we have to clean the layout generation number */
808 if (S_ISREG(la->la_mode))
809 ma->ma_lmm->lmm_layout_gen = 0;
813 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
814 struct mdt_object *o)
816 const struct lu_env *env = info->mti_env;
819 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
828 /* Is it a directory? Let's check for the LMV as well */
829 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
830 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
834 rc2 = mo_xattr_get(env, mdt_object_child(o),
836 XATTR_NAME_DEFAULT_LMV);
838 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
846 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
849 const struct lu_env *env = info->mti_env;
853 LASSERT(info->mti_big_lmm_used == 0);
854 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
858 /* big_lmm may need to be grown */
859 if (info->mti_big_lmmsize < rc) {
860 int size = size_roundup_power2(rc);
862 if (info->mti_big_lmmsize > 0) {
863 /* free old buffer */
864 LASSERT(info->mti_big_lmm);
865 OBD_FREE_LARGE(info->mti_big_lmm,
866 info->mti_big_lmmsize);
867 info->mti_big_lmm = NULL;
868 info->mti_big_lmmsize = 0;
871 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
872 if (info->mti_big_lmm == NULL)
874 info->mti_big_lmmsize = size;
876 LASSERT(info->mti_big_lmmsize >= rc);
878 info->mti_buf.lb_buf = info->mti_big_lmm;
879 info->mti_buf.lb_len = info->mti_big_lmmsize;
880 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
885 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
886 struct md_attr *ma, const char *name)
888 struct md_object *next = mdt_object_child(o);
889 struct lu_buf *buf = &info->mti_buf;
892 if (strcmp(name, XATTR_NAME_LOV) == 0) {
893 buf->lb_buf = ma->ma_lmm;
894 buf->lb_len = ma->ma_lmm_size;
895 LASSERT(!(ma->ma_valid & MA_LOV));
896 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
897 buf->lb_buf = ma->ma_lmv;
898 buf->lb_len = ma->ma_lmv_size;
899 LASSERT(!(ma->ma_valid & MA_LMV));
900 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
901 buf->lb_buf = ma->ma_lmv;
902 buf->lb_len = ma->ma_lmv_size;
903 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
908 rc = mo_xattr_get(info->mti_env, next, buf, name);
912 if (strcmp(name, XATTR_NAME_LOV) == 0) {
913 if (info->mti_big_lmm_used)
914 ma->ma_lmm = info->mti_big_lmm;
916 /* NOT return LOV EA with hole to old client. */
917 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
918 LOV_PATTERN_F_HOLE) &&
919 !(exp_connect_flags(info->mti_exp) &
920 OBD_CONNECT_LFSCK)) {
923 ma->ma_lmm_size = rc;
924 ma->ma_valid |= MA_LOV;
926 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
927 if (info->mti_big_lmm_used)
928 ma->ma_lmv = info->mti_big_lmm;
930 ma->ma_lmv_size = rc;
931 ma->ma_valid |= MA_LMV;
932 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
933 ma->ma_lmv_size = rc;
934 ma->ma_valid |= MA_LMV_DEF;
937 /* Update mdt_max_mdsize so all clients will be aware that */
938 if (info->mti_mdt->mdt_max_mdsize < rc)
939 info->mti_mdt->mdt_max_mdsize = rc;
942 } else if (rc == -ENODATA) {
945 } else if (rc == -ERANGE) {
946 /* Default LMV has fixed size, so it must be able to fit
947 * in the original buffer */
948 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
950 rc = mdt_big_xattr_get(info, o, name);
952 info->mti_big_lmm_used = 1;
960 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
961 struct mdt_object *o, struct lu_fid *pfid)
963 struct lu_buf *buf = &info->mti_buf;
964 struct link_ea_header *leh;
965 struct link_ea_entry *lee;
969 buf->lb_buf = info->mti_big_lmm;
970 buf->lb_len = info->mti_big_lmmsize;
971 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
972 buf, XATTR_NAME_LINK);
973 /* ignore errors, MA_PFID won't be set and it is
974 * up to the caller to treat this as an error */
975 if (rc == -ERANGE || buf->lb_len == 0) {
976 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
977 buf->lb_buf = info->mti_big_lmm;
978 buf->lb_len = info->mti_big_lmmsize;
983 if (rc < sizeof(*leh)) {
984 CERROR("short LinkEA on "DFID": rc = %d\n",
985 PFID(mdt_object_fid(o)), rc);
989 leh = (struct link_ea_header *) buf->lb_buf;
990 lee = (struct link_ea_entry *)(leh + 1);
991 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
992 leh->leh_magic = LINK_EA_MAGIC;
993 leh->leh_reccount = __swab32(leh->leh_reccount);
994 leh->leh_len = __swab64(leh->leh_len);
996 if (leh->leh_magic != LINK_EA_MAGIC)
998 if (leh->leh_reccount == 0)
1001 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
1002 fid_be_to_cpu(pfid, pfid);
1007 int mdt_attr_get_complex(struct mdt_thread_info *info,
1008 struct mdt_object *o, struct md_attr *ma)
1010 const struct lu_env *env = info->mti_env;
1011 struct md_object *next = mdt_object_child(o);
1012 struct lu_buf *buf = &info->mti_buf;
1013 int need = ma->ma_need;
1020 if (mdt_object_exists(o) == 0)
1021 GOTO(out, rc = -ENOENT);
1022 mode = lu_object_attr(&next->mo_lu);
1024 if (need & MA_INODE) {
1025 ma->ma_need = MA_INODE;
1026 rc = mo_attr_get(env, next, ma);
1031 (void) mdt_get_som(info, o, &ma->ma_attr);
1032 ma->ma_valid |= MA_INODE;
1035 if (need & MA_PFID) {
1036 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
1038 ma->ma_valid |= MA_PFID;
1039 /* ignore this error, parent fid is not mandatory */
1043 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
1044 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
1049 if (need & MA_LMV && S_ISDIR(mode)) {
1050 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
1055 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
1056 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
1061 if (need & MA_HSM && S_ISREG(mode)) {
1062 buf->lb_buf = info->mti_xattr_buf;
1063 buf->lb_len = sizeof(info->mti_xattr_buf);
1064 CLASSERT(sizeof(struct hsm_attrs) <=
1065 sizeof(info->mti_xattr_buf));
1066 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
1067 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
1069 ma->ma_valid |= MA_HSM;
1070 else if (rc2 < 0 && rc2 != -ENODATA)
1071 GOTO(out, rc = rc2);
1074 #ifdef CONFIG_FS_POSIX_ACL
1075 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1076 buf->lb_buf = ma->ma_acl;
1077 buf->lb_len = ma->ma_acl_size;
1078 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1080 ma->ma_acl_size = rc2;
1081 ma->ma_valid |= MA_ACL_DEF;
1082 } else if (rc2 == -ENODATA) {
1084 ma->ma_acl_size = 0;
1086 GOTO(out, rc = rc2);
1091 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1092 rc, ma->ma_valid, ma->ma_lmm);
1096 static int mdt_getattr_internal(struct mdt_thread_info *info,
1097 struct mdt_object *o, int ma_need)
1099 struct md_object *next = mdt_object_child(o);
1100 const struct mdt_body *reqbody = info->mti_body;
1101 struct ptlrpc_request *req = mdt_info_req(info);
1102 struct md_attr *ma = &info->mti_attr;
1103 struct lu_attr *la = &ma->ma_attr;
1104 struct req_capsule *pill = info->mti_pill;
1105 const struct lu_env *env = info->mti_env;
1106 struct mdt_body *repbody;
1107 struct lu_buf *buffer = &info->mti_buf;
1108 struct obd_export *exp = info->mti_exp;
1112 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1113 RETURN(err_serious(-ENOMEM));
1115 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1119 if (mdt_object_remote(o)) {
1120 /* This object is located on remote node.*/
1121 /* Return -ENOTSUPP for old client */
1122 if (!mdt_is_dne_client(req->rq_export))
1123 GOTO(out, rc = -ENOTSUPP);
1125 repbody->mbo_fid1 = *mdt_object_fid(o);
1126 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1130 if (reqbody->mbo_eadatasize > 0) {
1131 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1132 if (buffer->lb_buf == NULL)
1133 GOTO(out, rc = -EPROTO);
1134 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1137 buffer->lb_buf = NULL;
1139 ma_need &= ~(MA_LOV | MA_LMV);
1140 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1141 mdt_obd_name(info->mti_mdt),
1142 req->rq_export->exp_client_uuid.uuid);
1145 /* If it is dir object and client require MEA, then we got MEA */
1146 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1147 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1148 /* Assumption: MDT_MD size is enough for lmv size. */
1149 ma->ma_lmv = buffer->lb_buf;
1150 ma->ma_lmv_size = buffer->lb_len;
1151 ma->ma_need = MA_INODE;
1152 if (ma->ma_lmv_size > 0) {
1153 if (reqbody->mbo_valid & OBD_MD_MEA)
1154 ma->ma_need |= MA_LMV;
1155 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1156 ma->ma_need |= MA_LMV_DEF;
1159 ma->ma_lmm = buffer->lb_buf;
1160 ma->ma_lmm_size = buffer->lb_len;
1161 ma->ma_need = MA_INODE | MA_HSM;
1162 if (ma->ma_lmm_size > 0)
1163 ma->ma_need |= MA_LOV;
1166 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1167 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1168 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1169 /* get default stripe info for this dir. */
1170 ma->ma_need |= MA_LOV_DEF;
1172 ma->ma_need |= ma_need;
1174 rc = mdt_attr_get_complex(info, o, ma);
1176 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1177 "%s: getattr error for "DFID": rc = %d\n",
1178 mdt_obd_name(info->mti_mdt),
1179 PFID(mdt_object_fid(o)), rc);
1183 /* if file is released, check if a restore is running */
1184 if (ma->ma_valid & MA_HSM) {
1185 repbody->mbo_valid |= OBD_MD_TSTATE;
1186 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1187 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1188 repbody->mbo_t_state = MS_RESTORE;
1191 if (likely(ma->ma_valid & MA_INODE))
1192 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1196 if (mdt_body_has_lov(la, reqbody)) {
1197 if (ma->ma_valid & MA_LOV) {
1198 LASSERT(ma->ma_lmm_size);
1199 repbody->mbo_eadatasize = ma->ma_lmm_size;
1200 if (S_ISDIR(la->la_mode))
1201 repbody->mbo_valid |= OBD_MD_FLDIREA;
1203 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1204 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1206 if (ma->ma_valid & MA_LMV) {
1207 /* Return -ENOTSUPP for old client */
1208 if (!mdt_is_striped_client(req->rq_export))
1211 LASSERT(S_ISDIR(la->la_mode));
1212 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1213 repbody->mbo_eadatasize = ma->ma_lmv_size;
1214 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1216 if (ma->ma_valid & MA_LMV_DEF) {
1217 /* Return -ENOTSUPP for old client */
1218 if (!mdt_is_striped_client(req->rq_export))
1220 LASSERT(S_ISDIR(la->la_mode));
1221 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1222 repbody->mbo_eadatasize = ma->ma_lmv_size;
1223 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1224 OBD_MD_DEFAULT_MEA);
1226 } else if (S_ISLNK(la->la_mode) &&
1227 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1228 buffer->lb_buf = ma->ma_lmm;
1229 /* eadatasize from client includes NULL-terminator, so
1230 * there is no need to read it */
1231 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1232 rc = mo_readlink(env, next, buffer);
1233 if (unlikely(rc <= 0)) {
1234 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1235 mdt_obd_name(info->mti_mdt),
1236 PFID(mdt_object_fid(o)), rc);
1239 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1241 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1243 repbody->mbo_valid |= OBD_MD_LINKNAME;
1244 /* we need to report back size with NULL-terminator
1245 * because client expects that */
1246 repbody->mbo_eadatasize = rc + 1;
1247 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1248 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1249 "on "DFID ", expected %d\n",
1250 mdt_obd_name(info->mti_mdt),
1251 rc, PFID(mdt_object_fid(o)),
1252 reqbody->mbo_eadatasize - 1);
1253 /* NULL terminate */
1254 ((char *)ma->ma_lmm)[rc] = 0;
1256 /* If the total CDEBUG() size is larger than a page, it
1257 * will print a warning to the console, avoid this by
1258 * printing just the last part of the symlink. */
1259 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1260 print_limit < rc ? "..." : "", print_limit,
1261 (char *)ma->ma_lmm + rc - print_limit, rc);
1266 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1267 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1268 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1269 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1270 repbody->mbo_max_mdsize);
1273 #ifdef CONFIG_FS_POSIX_ACL
1274 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1275 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1276 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1277 if (IS_ERR(nodemap))
1278 RETURN(PTR_ERR(nodemap));
1280 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1281 nodemap_putref(nodemap);
1287 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1292 static int mdt_getattr(struct tgt_session_info *tsi)
1294 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1295 struct mdt_object *obj = info->mti_object;
1296 struct req_capsule *pill = info->mti_pill;
1297 struct mdt_body *reqbody;
1298 struct mdt_body *repbody;
1302 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1304 LASSERT(obj != NULL);
1305 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1307 /* Unlike intent case where we need to pre-fill out buffers early on
1308 * in intent policy for ldlm reasons, here we can have a much better
1309 * guess at EA size by just reading it from disk.
1310 * Exceptions are readdir and (missing) directory striping */
1312 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1313 /* No easy way to know how long is the symlink, but it cannot
1314 * be more than PATH_MAX, so we allocate +1 */
1317 /* A special case for fs ROOT: getattr there might fetch
1318 * default EA for entire fs, not just for this dir!
1320 } else if (lu_fid_eq(mdt_object_fid(obj),
1321 &info->mti_mdt->mdt_md_root_fid) &&
1322 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1323 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1325 /* Should the default strping be bigger, mdt_fix_reply
1326 * will reallocate */
1327 rc = DEF_REP_MD_SIZE;
1329 /* Read the actual EA size from disk */
1330 rc = mdt_attr_get_eabuf_size(info, obj);
1334 GOTO(out, rc = err_serious(rc));
1336 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1338 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1339 * by default. If the target object has more ACL entries, then
1340 * enlarge the buffer when necessary. */
1341 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1342 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1344 rc = req_capsule_server_pack(pill);
1345 if (unlikely(rc != 0))
1346 GOTO(out, rc = err_serious(rc));
1348 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1349 LASSERT(repbody != NULL);
1350 repbody->mbo_eadatasize = 0;
1351 repbody->mbo_aclsize = 0;
1353 rc = mdt_check_ucred(info);
1355 GOTO(out_shrink, rc);
1357 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1359 rc = mdt_getattr_internal(info, obj, 0);
1362 mdt_client_compatibility(info);
1363 rc2 = mdt_fix_reply(info);
1367 mdt_thread_info_fini(info);
1372 * Handler of layout intent RPC requiring the layout modification
1374 * \param[in] info thread environment
1375 * \param[in] obj object
1376 * \param[in] layout layout change descriptor
1378 * \retval 0 on success
1379 * \retval < 0 error code
1381 int mdt_layout_change(struct mdt_thread_info *info, struct mdt_object *obj,
1382 struct md_layout_change *layout)
1384 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1388 if (!mdt_object_exists(obj))
1389 GOTO(out, rc = -ENOENT);
1391 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1392 GOTO(out, rc = -EINVAL);
1394 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1399 /* take layout lock to prepare layout change */
1400 mdt_lock_reg_init(lh, LCK_EX);
1401 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LAYOUT);
1405 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout);
1407 mdt_object_unlock(info, obj, lh, 1);
1413 * Exchange MOF_LOV_CREATED flags between two objects after a
1414 * layout swap. No assumption is made on whether o1 or o2 have
1415 * created objects or not.
1417 * \param[in,out] o1 First swap layout object
1418 * \param[in,out] o2 Second swap layout object
1420 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1422 unsigned int o1_lov_created = o1->mot_lov_created;
1424 mutex_lock(&o1->mot_lov_mutex);
1425 mutex_lock(&o2->mot_lov_mutex);
1427 o1->mot_lov_created = o2->mot_lov_created;
1428 o2->mot_lov_created = o1_lov_created;
1430 mutex_unlock(&o2->mot_lov_mutex);
1431 mutex_unlock(&o1->mot_lov_mutex);
1434 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1436 struct mdt_thread_info *info;
1437 struct ptlrpc_request *req = tgt_ses_req(tsi);
1438 struct obd_export *exp = req->rq_export;
1439 struct mdt_object *o1, *o2, *o;
1440 struct mdt_lock_handle *lh1, *lh2;
1441 struct mdc_swap_layouts *msl;
1445 /* client does not support layout lock, so layout swaping
1447 * FIXME: there is a problem for old clients which don't support
1448 * layout lock yet. If those clients have already opened the file
1449 * they won't be notified at all so that old layout may still be
1450 * used to do IO. This can be fixed after file release is landed by
1451 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1452 if (!exp_connect_layout(exp))
1453 RETURN(-EOPNOTSUPP);
1455 info = tsi2mdt_info(tsi);
1457 if (info->mti_dlm_req != NULL)
1458 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1460 o1 = info->mti_object;
1461 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1462 &info->mti_body->mbo_fid2);
1464 GOTO(out, rc = PTR_ERR(o));
1466 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1467 GOTO(put, rc = -ENOENT);
1469 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1470 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1476 /* permission check. Make sure the calling process having permission
1477 * to write both files. */
1478 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1483 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1488 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1490 GOTO(put, rc = -EPROTO);
1492 lh1 = &info->mti_lh[MDT_LH_NEW];
1493 mdt_lock_reg_init(lh1, LCK_EX);
1494 lh2 = &info->mti_lh[MDT_LH_OLD];
1495 mdt_lock_reg_init(lh2, LCK_EX);
1497 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1498 MDS_INODELOCK_XATTR);
1502 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1503 MDS_INODELOCK_XATTR);
1507 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1508 mdt_object_child(o2), msl->msl_flags);
1512 mdt_swap_lov_flag(o1, o2);
1515 mdt_object_unlock(info, o2, lh2, rc);
1517 mdt_object_unlock(info, o1, lh1, rc);
1519 mdt_object_put(info->mti_env, o);
1521 mdt_thread_info_fini(info);
1525 static int mdt_raw_lookup(struct mdt_thread_info *info,
1526 struct mdt_object *parent,
1527 const struct lu_name *lname,
1528 struct ldlm_reply *ldlm_rep)
1530 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1534 LASSERT(!info->mti_cross_ref);
1536 /* Only got the fid of this obj by name */
1537 fid_zero(child_fid);
1538 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1539 lname, child_fid, &info->mti_spec);
1541 struct mdt_body *repbody;
1543 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1544 repbody->mbo_fid1 = *child_fid;
1545 repbody->mbo_valid = OBD_MD_FLID;
1546 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1547 } else if (rc == -ENOENT) {
1548 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1555 * UPDATE lock should be taken against parent, and be released before exit;
1556 * child_bits lock should be taken against child, and be returned back:
1557 * (1)normal request should release the child lock;
1558 * (2)intent request will grant the lock to client.
1560 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1561 struct mdt_lock_handle *lhc,
1563 struct ldlm_reply *ldlm_rep)
1565 struct ptlrpc_request *req = mdt_info_req(info);
1566 struct mdt_body *reqbody = NULL;
1567 struct mdt_object *parent = info->mti_object;
1568 struct mdt_object *child;
1569 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1570 struct lu_name *lname = NULL;
1571 struct mdt_lock_handle *lhp = NULL;
1572 struct ldlm_lock *lock;
1580 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1581 LASSERT(ergo(is_resent,
1582 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1587 if (info->mti_cross_ref) {
1588 /* Only getattr on the child. Parent is on another node. */
1589 mdt_set_disposition(info, ldlm_rep,
1590 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1592 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1594 PFID(mdt_object_fid(child)), ldlm_rep);
1596 rc = mdt_check_resent_lock(info, child, lhc);
1599 } else if (rc > 0) {
1600 mdt_lock_handle_init(lhc);
1601 mdt_lock_reg_init(lhc, LCK_PR);
1604 * Object's name is on another MDS, no lookup or layout
1605 * lock is needed here but update lock is.
1607 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1608 MDS_INODELOCK_LAYOUT);
1609 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1611 rc = mdt_object_lock(info, child, lhc, child_bits);
1616 /* Finally, we can get attr for child. */
1617 if (!mdt_object_exists(child)) {
1618 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1620 "remote object doesn't exist.");
1621 mdt_object_unlock(info, child, lhc, 1);
1625 rc = mdt_getattr_internal(info, child, 0);
1626 if (unlikely(rc != 0))
1627 mdt_object_unlock(info, child, lhc, 1);
1632 lname = &info->mti_name;
1633 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1635 if (lu_name_is_valid(lname)) {
1636 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1637 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1638 PNAME(lname), ldlm_rep);
1640 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1641 if (unlikely(reqbody == NULL))
1642 RETURN(err_serious(-EPROTO));
1644 *child_fid = reqbody->mbo_fid2;
1646 if (unlikely(!fid_is_sane(child_fid)))
1647 RETURN(err_serious(-EINVAL));
1649 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1651 PFID(mdt_object_fid(parent)),
1652 PFID(&reqbody->mbo_fid2), ldlm_rep);
1655 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1657 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1658 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1660 "Parent doesn't exist!");
1664 if (mdt_object_remote(parent)) {
1665 CERROR("%s: parent "DFID" is on remote target\n",
1666 mdt_obd_name(info->mti_mdt),
1667 PFID(mdt_object_fid(parent)));
1671 if (lu_name_is_valid(lname)) {
1672 /* Always allow to lookup ".." */
1673 if (unlikely(lname->ln_namelen == 2 &&
1674 lname->ln_name[0] == '.' &&
1675 lname->ln_name[1] == '.'))
1676 info->mti_spec.sp_permitted = 1;
1678 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1679 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1684 /* step 1: lock parent only if parent is a directory */
1685 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1686 lhp = &info->mti_lh[MDT_LH_PARENT];
1687 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1688 rc = mdt_object_lock(info, parent, lhp,
1689 MDS_INODELOCK_UPDATE);
1690 if (unlikely(rc != 0))
1694 /* step 2: lookup child's fid by name */
1695 fid_zero(child_fid);
1696 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1697 child_fid, &info->mti_spec);
1699 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1702 GOTO(out_parent, rc);
1705 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1708 *step 3: find the child object by fid & lock it.
1709 * regardless if it is local or remote.
1711 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1712 * set parent dir fid the same as child fid in getattr by fid case
1713 * we should not lu_object_find() the object again, could lead
1714 * to hung if there is a concurrent unlink destroyed the object.
1716 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1717 mdt_object_get(info->mti_env, parent);
1720 child = mdt_object_find(info->mti_env, info->mti_mdt,
1724 if (unlikely(IS_ERR(child)))
1725 GOTO(out_parent, rc = PTR_ERR(child));
1727 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1728 if (!mdt_object_exists(child)) {
1729 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1731 "Object doesn't exist!");
1732 GOTO(out_child, rc = -ENOENT);
1735 rc = mdt_check_resent_lock(info, child, lhc);
1737 GOTO(out_child, rc);
1738 } else if (rc > 0) {
1739 mdt_lock_handle_init(lhc);
1740 mdt_lock_reg_init(lhc, LCK_PR);
1742 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1743 mdt_object_exists(child) && !mdt_object_remote(child)) {
1744 struct md_attr *ma = &info->mti_attr;
1747 ma->ma_need = MA_INODE;
1748 rc = mdt_attr_get_complex(info, child, ma);
1749 if (unlikely(rc != 0))
1750 GOTO(out_child, rc);
1752 /* If the file has not been changed for some time, we
1753 * return not only a LOOKUP lock, but also an UPDATE
1754 * lock and this might save us RPC on later STAT. For
1755 * directories, it also let negative dentry cache start
1756 * working for this dir. */
1757 if (ma->ma_valid & MA_INODE &&
1758 ma->ma_attr.la_valid & LA_CTIME &&
1759 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1760 ma->ma_attr.la_ctime < ktime_get_real_seconds())
1761 child_bits |= MDS_INODELOCK_UPDATE;
1764 /* layout lock must be granted in a best-effort way
1765 * for IT operations */
1766 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1767 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
1768 !mdt_object_remote(child) && ldlm_rep != NULL) {
1769 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1770 exp_connect_layout(info->mti_exp)) {
1771 /* try to grant layout lock for regular file. */
1772 try_bits = MDS_INODELOCK_LAYOUT;
1774 /* Acquire DOM lock in advance for data-on-mdt file */
1775 if (child != parent)
1776 try_bits |= MDS_INODELOCK_DOM;
1779 if (try_bits != 0) {
1780 /* try layout lock, it may fail to be granted due to
1781 * contention at LOOKUP or UPDATE */
1782 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
1784 if (child_bits & MDS_INODELOCK_LAYOUT)
1787 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1788 * client will enqueue the lock to the remote MDT */
1789 if (mdt_object_remote(child))
1790 child_bits &= ~MDS_INODELOCK_UPDATE;
1791 rc = mdt_object_lock(info, child, lhc, child_bits);
1793 if (unlikely(rc != 0))
1794 GOTO(out_child, rc);
1797 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1799 /* finally, we can get attr for child. */
1800 rc = mdt_getattr_internal(info, child, ma_need);
1801 if (unlikely(rc != 0)) {
1802 mdt_object_unlock(info, child, lhc, 1);
1804 /* Debugging code. */
1805 LDLM_DEBUG(lock, "Returning lock to client");
1806 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1807 &lock->l_resource->lr_name),
1808 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1809 PLDLMRES(lock->l_resource),
1810 PFID(mdt_object_fid(child)));
1812 if (S_ISREG(lu_object_attr(&child->mot_obj)) &&
1813 mdt_object_exists(child) && !mdt_object_remote(child) &&
1815 LDLM_LOCK_PUT(lock);
1816 mdt_object_put(info->mti_env, child);
1817 /* NB: call the mdt_pack_size2body always after
1818 * mdt_object_put(), that is why this speacial
1819 * exit path is used. */
1820 rc = mdt_pack_size2body(info, child_fid,
1821 child_bits & MDS_INODELOCK_DOM);
1822 if (rc != 0 && child_bits & MDS_INODELOCK_DOM) {
1823 /* DOM lock was taken in advance but this is
1824 * not DoM file. Drop the lock. */
1825 lock_res_and_lock(lock);
1826 ldlm_inodebits_drop(lock, MDS_INODELOCK_DOM);
1827 unlock_res_and_lock(lock);
1830 GOTO(out_parent, rc = 0);
1834 LDLM_LOCK_PUT(lock);
1838 mdt_object_put(info->mti_env, child);
1841 mdt_object_unlock(info, parent, lhp, 1);
1845 /* normal handler: should release the child lock */
1846 static int mdt_getattr_name(struct tgt_session_info *tsi)
1848 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1849 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1850 struct mdt_body *reqbody;
1851 struct mdt_body *repbody;
1855 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1856 LASSERT(reqbody != NULL);
1857 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1858 LASSERT(repbody != NULL);
1860 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1861 repbody->mbo_eadatasize = 0;
1862 repbody->mbo_aclsize = 0;
1864 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1866 GOTO(out_shrink, rc);
1868 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1869 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1870 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1871 lhc->mlh_reg_lh.cookie = 0;
1873 mdt_exit_ucred(info);
1876 mdt_client_compatibility(info);
1877 rc2 = mdt_fix_reply(info);
1880 mdt_thread_info_fini(info);
1884 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1885 void *karg, void __user *uarg);
1887 static int mdt_set_info(struct tgt_session_info *tsi)
1889 struct ptlrpc_request *req = tgt_ses_req(tsi);
1892 int keylen, vallen, rc = 0;
1896 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1898 DEBUG_REQ(D_HA, req, "no set_info key");
1899 RETURN(err_serious(-EFAULT));
1902 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1905 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1907 DEBUG_REQ(D_HA, req, "no set_info val");
1908 RETURN(err_serious(-EFAULT));
1911 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1914 /* Swab any part of val you need to here */
1915 if (KEY_IS(KEY_READ_ONLY)) {
1916 spin_lock(&req->rq_export->exp_lock);
1918 *exp_connect_flags_ptr(req->rq_export) |=
1921 *exp_connect_flags_ptr(req->rq_export) &=
1922 ~OBD_CONNECT_RDONLY;
1923 spin_unlock(&req->rq_export->exp_lock);
1924 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1925 struct changelog_setinfo *cs = val;
1927 if (vallen != sizeof(*cs)) {
1928 CERROR("%s: bad changelog_clear setinfo size %d\n",
1929 tgt_name(tsi->tsi_tgt), vallen);
1932 if (ptlrpc_req_need_swab(req)) {
1933 __swab64s(&cs->cs_recno);
1934 __swab32s(&cs->cs_id);
1937 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1939 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1941 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1948 static int mdt_readpage(struct tgt_session_info *tsi)
1950 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1951 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1952 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1953 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1954 struct mdt_body *repbody;
1960 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1961 RETURN(err_serious(-ENOMEM));
1963 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1964 if (repbody == NULL || reqbody == NULL)
1965 RETURN(err_serious(-EFAULT));
1968 * prepare @rdpg before calling lower layers and transfer itself. Here
1969 * reqbody->size contains offset of where to start to read and
1970 * reqbody->nlink contains number bytes to read.
1972 rdpg->rp_hash = reqbody->mbo_size;
1973 if (rdpg->rp_hash != reqbody->mbo_size) {
1974 CERROR("Invalid hash: %#llx != %#llx\n",
1975 rdpg->rp_hash, reqbody->mbo_size);
1979 rdpg->rp_attrs = reqbody->mbo_mode;
1980 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1981 rdpg->rp_attrs |= LUDA_64BITHASH;
1982 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1983 exp_max_brw_size(tsi->tsi_exp));
1984 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1986 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1987 if (rdpg->rp_pages == NULL)
1990 for (i = 0; i < rdpg->rp_npages; ++i) {
1991 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1992 if (rdpg->rp_pages[i] == NULL)
1993 GOTO(free_rdpg, rc = -ENOMEM);
1996 /* call lower layers to fill allocated pages with directory data */
1997 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1999 GOTO(free_rdpg, rc);
2001 /* send pages to client */
2002 rc = tgt_sendpage(tsi, rdpg, rc);
2007 for (i = 0; i < rdpg->rp_npages; i++)
2008 if (rdpg->rp_pages[i] != NULL)
2009 __free_page(rdpg->rp_pages[i]);
2010 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
2012 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
2018 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
2020 struct lu_ucred *uc = mdt_ucred_check(info);
2021 struct lu_attr *attr = &info->mti_attr.ma_attr;
2026 if (op != REINT_SETATTR) {
2027 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
2028 attr->la_uid = uc->uc_fsuid;
2029 /* for S_ISGID, inherit gid from his parent, such work will be
2030 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
2031 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
2032 attr->la_gid = uc->uc_fsgid;
2038 static int mdt_reint_internal(struct mdt_thread_info *info,
2039 struct mdt_lock_handle *lhc,
2042 struct req_capsule *pill = info->mti_pill;
2043 struct mdt_body *repbody;
2048 rc = mdt_reint_unpack(info, op);
2050 CERROR("Can't unpack reint, rc %d\n", rc);
2051 RETURN(err_serious(rc));
2054 /* for replay (no_create) lmm is not needed, client has it already */
2055 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2056 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2059 /* llog cookies are always 0, the field is kept for compatibility */
2060 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2061 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
2063 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
2064 * by default. If the target object has more ACL entries, then
2065 * enlarge the buffer when necessary. */
2066 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
2067 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
2068 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
2070 rc = req_capsule_server_pack(pill);
2072 CERROR("Can't pack response, rc %d\n", rc);
2073 RETURN(err_serious(rc));
2076 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
2077 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
2079 repbody->mbo_eadatasize = 0;
2080 repbody->mbo_aclsize = 0;
2083 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
2085 /* for replay no cookkie / lmm need, because client have this already */
2086 if (info->mti_spec.no_create)
2087 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2088 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
2090 rc = mdt_init_ucred_reint(info);
2092 GOTO(out_shrink, rc);
2094 rc = mdt_fix_attr_ucred(info, op);
2096 GOTO(out_ucred, rc = err_serious(rc));
2098 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2100 GOTO(out_ucred, rc);
2101 } else if (rc == 1) {
2102 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
2103 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2104 GOTO(out_ucred, rc);
2106 rc = mdt_reint_rec(info, lhc);
2109 mdt_exit_ucred(info);
2111 mdt_client_compatibility(info);
2112 rc2 = mdt_fix_reply(info);
2118 static long mdt_reint_opcode(struct ptlrpc_request *req,
2119 const struct req_format **fmt)
2121 struct mdt_device *mdt;
2122 struct mdt_rec_reint *rec;
2125 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2127 opc = rec->rr_opcode;
2128 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2129 if (opc < REINT_MAX && fmt[opc] != NULL)
2130 req_capsule_extend(&req->rq_pill, fmt[opc]);
2132 mdt = mdt_exp2dev(req->rq_export);
2133 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2134 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2135 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2136 opc = err_serious(-EFAULT);
2139 opc = err_serious(-EFAULT);
2144 static int mdt_reint(struct tgt_session_info *tsi)
2148 static const struct req_format *reint_fmts[REINT_MAX] = {
2149 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2150 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2151 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2152 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2153 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2154 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2155 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2156 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2157 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME,
2158 [REINT_RESYNC] = &RQF_MDS_REINT_RESYNC,
2163 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2165 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2167 * No lock possible here from client to pass it to reint code
2170 rc = mdt_reint_internal(info, NULL, opc);
2171 mdt_thread_info_fini(info);
2176 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2180 /* this should sync the whole device */
2181 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2183 struct dt_device *dt = mdt->mdt_bottom;
2187 rc = dt->dd_ops->dt_sync(env, dt);
2191 /* this should sync this object */
2192 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2193 struct mdt_object *mo)
2199 if (!mdt_object_exists(mo)) {
2200 CWARN("%s: non existing object "DFID": rc = %d\n",
2201 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2206 rc = mo_object_sync(env, mdt_object_child(mo));
2211 static int mdt_sync(struct tgt_session_info *tsi)
2213 struct ptlrpc_request *req = tgt_ses_req(tsi);
2214 struct req_capsule *pill = tsi->tsi_pill;
2215 struct mdt_body *body;
2220 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2221 RETURN(err_serious(-ENOMEM));
2223 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2224 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2226 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2228 /* sync an object */
2229 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2232 const struct lu_fid *fid;
2233 struct lu_attr *la = &info->mti_attr.ma_attr;
2235 info->mti_attr.ma_need = MA_INODE;
2236 info->mti_attr.ma_valid = 0;
2237 rc = mdt_attr_get_complex(info, info->mti_object,
2240 body = req_capsule_server_get(pill,
2242 fid = mdt_object_fid(info->mti_object);
2243 mdt_pack_attr2body(info, body, la, fid);
2246 mdt_thread_info_fini(info);
2249 mdt_counter_incr(req, LPROC_MDT_SYNC);
2254 static int mdt_data_sync(struct tgt_session_info *tsi)
2256 struct mdt_thread_info *info;
2257 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2258 struct ost_body *body = tsi->tsi_ost_body;
2259 struct ost_body *repbody;
2260 struct mdt_object *mo = NULL;
2266 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2268 /* if no fid is specified then do nothing,
2269 * device sync is done via MDS_SYNC */
2270 if (fid_is_zero(&tsi->tsi_fid))
2273 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2275 RETURN(PTR_ERR(mo));
2277 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2281 repbody->oa.o_oi = body->oa.o_oi;
2282 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2284 info = tsi2mdt_info(tsi);
2285 ma = &info->mti_attr;
2286 ma->ma_need = MA_INODE;
2288 rc = mdt_attr_get_complex(info, mo, ma);
2290 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2293 mdt_thread_info_fini(info);
2298 mdt_object_put(tsi->tsi_env, mo);
2303 * Handle quota control requests to consult current usage/limit, but also
2304 * to configure quota enforcement
2306 static int mdt_quotactl(struct tgt_session_info *tsi)
2308 struct obd_export *exp = tsi->tsi_exp;
2309 struct req_capsule *pill = tsi->tsi_pill;
2310 struct obd_quotactl *oqctl, *repoqc;
2312 struct mdt_device *mdt = mdt_exp2dev(exp);
2313 struct lu_device *qmt = mdt->mdt_qmt_dev;
2314 struct lu_nodemap *nodemap;
2317 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2319 RETURN(err_serious(-EPROTO));
2321 rc = req_capsule_server_pack(pill);
2323 RETURN(err_serious(rc));
2325 nodemap = nodemap_get_from_exp(exp);
2326 if (IS_ERR(nodemap))
2327 RETURN(PTR_ERR(nodemap));
2329 switch (oqctl->qc_cmd) {
2330 /* master quotactl */
2333 if (!nodemap_can_setquota(nodemap))
2334 GOTO(out_nodemap, rc = -EPERM);
2338 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2339 /* slave quotactl */
2344 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2345 GOTO(out_nodemap, rc = -EFAULT);
2349 switch (oqctl->qc_type) {
2351 id = nodemap_map_id(nodemap, NODEMAP_UID,
2352 NODEMAP_CLIENT_TO_FS, id);
2355 id = nodemap_map_id(nodemap, NODEMAP_GID,
2356 NODEMAP_CLIENT_TO_FS, id);
2359 /* todo: check/map project id */
2363 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2365 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2367 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2369 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2370 barrier_exit(tsi->tsi_tgt->lut_bottom);
2372 if (oqctl->qc_id != id)
2373 swap(oqctl->qc_id, id);
2375 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2376 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2377 RETURN(-EINPROGRESS);
2380 switch (oqctl->qc_cmd) {
2386 /* forward quotactl request to QMT */
2387 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2392 /* slave quotactl */
2393 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2398 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2399 GOTO(out_nodemap, rc = -EFAULT);
2402 if (oqctl->qc_id != id)
2403 swap(oqctl->qc_id, id);
2410 nodemap_putref(nodemap);
2415 /** clone llog ctxt from child (mdd)
2416 * This allows remote llog (replicator) access.
2417 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2418 * context was originally set up, or we can handle them directly.
2419 * I choose the latter, but that means I need any llog
2420 * contexts set up by child to be accessable by the mdt. So we clone the
2421 * context into our context list here.
2423 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2426 struct md_device *next = mdt->mdt_child;
2427 struct llog_ctxt *ctxt;
2430 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2433 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2434 if (rc || ctxt == NULL) {
2438 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2440 CERROR("Can't set mdt ctxt %d\n", rc);
2445 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2446 struct mdt_device *mdt, int idx)
2448 struct llog_ctxt *ctxt;
2450 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2453 /* Put once for the get we just did, and once for the clone */
2454 llog_ctxt_put(ctxt);
2455 llog_ctxt_put(ctxt);
2460 * sec context handlers
2462 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2464 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2470 * quota request handlers
2472 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2474 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2475 struct lu_device *qmt = mdt->mdt_qmt_dev;
2480 RETURN(err_serious(-EOPNOTSUPP));
2482 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2486 struct mdt_object *mdt_object_new(const struct lu_env *env,
2487 struct mdt_device *d,
2488 const struct lu_fid *f)
2490 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2491 struct lu_object *o;
2492 struct mdt_object *m;
2495 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2496 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2497 if (unlikely(IS_ERR(o)))
2498 m = (struct mdt_object *)o;
2504 struct mdt_object *mdt_object_find(const struct lu_env *env,
2505 struct mdt_device *d,
2506 const struct lu_fid *f)
2508 struct lu_object *o;
2509 struct mdt_object *m;
2512 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2513 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2514 if (unlikely(IS_ERR(o)))
2515 m = (struct mdt_object *)o;
2523 * Asyncronous commit for mdt device.
2525 * Pass asynchonous commit call down the MDS stack.
2527 * \param env environment
2528 * \param mdt the mdt device
2530 static void mdt_device_commit_async(const struct lu_env *env,
2531 struct mdt_device *mdt)
2533 struct dt_device *dt = mdt->mdt_bottom;
2537 rc = dt->dd_ops->dt_commit_async(env, dt);
2538 if (unlikely(rc != 0))
2539 CWARN("%s: async commit start failed: rc = %d\n",
2540 mdt_obd_name(mdt), rc);
2541 atomic_inc(&mdt->mdt_async_commit_count);
2546 * Mark the lock as "synchonous".
2548 * Mark the lock to deffer transaction commit to the unlock time.
2550 * \param lock the lock to mark as "synchonous"
2552 * \see mdt_is_lock_sync
2553 * \see mdt_save_lock
2555 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2557 lock->l_ast_data = (void*)1;
2561 * Check whehter the lock "synchonous" or not.
2563 * \param lock the lock to check
2564 * \retval 1 the lock is "synchonous"
2565 * \retval 0 the lock isn't "synchronous"
2567 * \see mdt_set_lock_sync
2568 * \see mdt_save_lock
2570 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2572 return lock->l_ast_data != NULL;
2576 * Blocking AST for mdt locks.
2578 * Starts transaction commit if in case of COS lock conflict or
2579 * deffers such a commit to the mdt_save_lock.
2581 * \param lock the lock which blocks a request or cancelling lock
2582 * \param desc unused
2583 * \param data unused
2584 * \param flag indicates whether this cancelling or blocking callback
2586 * \see ldlm_blocking_ast_nocheck
2588 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2589 void *data, int flag)
2591 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2592 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2593 bool commit_async = false;
2597 if (flag == LDLM_CB_CANCELING)
2600 lock_res_and_lock(lock);
2601 if (lock->l_blocking_ast != mdt_blocking_ast) {
2602 unlock_res_and_lock(lock);
2605 /* There is no lock conflict if l_blocking_lock == NULL,
2606 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2607 * when the last reference to a local lock was released */
2608 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2609 lock->l_blocking_lock != NULL) {
2610 if (mdt_cos_is_enabled(mdt)) {
2611 if (lock->l_client_cookie !=
2612 lock->l_blocking_lock->l_client_cookie)
2613 mdt_set_lock_sync(lock);
2614 } else if (mdt_slc_is_enabled(mdt) &&
2615 ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2616 mdt_set_lock_sync(lock);
2618 * we may do extra commit here, but there is a small
2619 * window to miss a commit: lock was unlocked (saved),
2620 * then a conflict lock queued and we come here, but
2621 * REP-ACK not received, so lock was not converted to
2623 * Fortunately this window is quite small, so the
2624 * extra commit should be rare (not to say distributed
2625 * operation is rare too).
2627 commit_async = true;
2629 } else if (lock->l_req_mode == LCK_COS &&
2630 lock->l_blocking_lock != NULL) {
2631 commit_async = true;
2634 rc = ldlm_blocking_ast_nocheck(lock);
2639 rc = lu_env_init(&env, LCT_LOCAL);
2640 if (unlikely(rc != 0))
2641 CWARN("%s: lu_env initialization failed, cannot "
2642 "start asynchronous commit: rc = %d\n",
2645 mdt_device_commit_async(&env, mdt);
2652 * Blocking AST for cross-MDT lock
2654 * Discard lock from uncommitted_slc_locks and cancel it.
2656 * \param lock the lock which blocks a request or cancelling lock
2657 * \param desc unused
2658 * \param data unused
2659 * \param flag indicates whether this cancelling or blocking callback
2660 * \retval 0 on success
2661 * \retval negative number on error
2663 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2664 void *data, int flag)
2670 case LDLM_CB_BLOCKING: {
2671 struct lustre_handle lockh;
2673 ldlm_lock2handle(lock, &lockh);
2674 rc = ldlm_cli_cancel(&lockh,
2675 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2677 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2682 case LDLM_CB_CANCELING: {
2683 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2684 struct mdt_device *mdt =
2685 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2687 LDLM_DEBUG(lock, "Revoke remote lock\n");
2689 /* discard slc lock here so that it can be cleaned anytime,
2690 * especially for cleanup_resource() */
2691 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2693 /* once we cache lock, l_ast_data is set to mdt_object */
2694 if (lock->l_ast_data != NULL) {
2695 struct mdt_object *mo = lock->l_ast_data;
2698 rc = lu_env_init(&env, LCT_MD_THREAD);
2699 if (unlikely(rc != 0)) {
2700 CWARN("%s: lu_env initialization failed, object"
2701 "%p "DFID" is leaked!\n",
2703 PFID(mdt_object_fid(mo)));
2707 if (lock->l_policy_data.l_inodebits.bits &
2708 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2709 rc = mo_invalidate(&env, mdt_object_child(mo));
2710 mo->mot_cache_attr = 0;
2712 mdt_object_put(&env, mo);
2724 int mdt_check_resent_lock(struct mdt_thread_info *info,
2725 struct mdt_object *mo,
2726 struct mdt_lock_handle *lhc)
2728 /* the lock might already be gotten in ldlm_handle_enqueue() */
2729 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2730 struct ptlrpc_request *req = mdt_info_req(info);
2731 struct ldlm_lock *lock;
2733 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2734 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2736 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2737 * a resend case, however, it could be already destroyed
2738 * due to client eviction or a raced cancel RPC. */
2739 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2740 lhc->mlh_reg_lh.cookie);
2744 if (!fid_res_name_eq(mdt_object_fid(mo),
2745 &lock->l_resource->lr_name)) {
2746 CWARN("%s: Although resent, but still not "
2747 "get child lock:"DFID"\n",
2748 info->mti_exp->exp_obd->obd_name,
2749 PFID(mdt_object_fid(mo)));
2750 LDLM_LOCK_PUT(lock);
2753 LDLM_LOCK_PUT(lock);
2759 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
2760 struct mdt_object *o, const struct lu_fid *fid,
2761 struct lustre_handle *lh, enum ldlm_mode mode,
2762 __u64 *ibits, __u64 trybits, bool cache)
2764 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2765 union ldlm_policy_data *policy = &mti->mti_policy;
2766 struct ldlm_res_id *res_id = &mti->mti_res_id;
2770 LASSERT(mdt_object_remote(o));
2772 fid_build_reg_res_name(fid, res_id);
2774 memset(einfo, 0, sizeof(*einfo));
2775 einfo->ei_type = LDLM_IBITS;
2776 einfo->ei_mode = mode;
2777 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2778 einfo->ei_cb_cp = ldlm_completion_ast;
2779 einfo->ei_enq_slave = 0;
2780 einfo->ei_res_id = res_id;
2784 * if we cache lock, couple lock with mdt_object, so that object
2785 * can be easily found in lock ASTs.
2787 mdt_object_get(mti->mti_env, o);
2788 einfo->ei_cbdata = o;
2792 memset(policy, 0, sizeof(*policy));
2793 policy->l_inodebits.bits = *ibits;
2794 policy->l_inodebits.try_bits = trybits;
2796 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2798 if (rc < 0 && cache) {
2799 mdt_object_put(mti->mti_env, o);
2800 einfo->ei_cbdata = NULL;
2803 /* Return successfully acquired bits to a caller */
2805 struct ldlm_lock *lock = ldlm_handle2lock(lh);
2808 *ibits = lock->l_policy_data.l_inodebits.bits;
2809 LDLM_LOCK_PUT(lock);
2814 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2815 const struct lu_fid *fid, struct lustre_handle *lh,
2816 enum ldlm_mode mode, __u64 ibits, bool cache)
2818 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
2822 static int mdt_object_local_lock(struct mdt_thread_info *info,
2823 struct mdt_object *o,
2824 struct mdt_lock_handle *lh, __u64 *ibits,
2825 __u64 trybits, bool cos_incompat)
2827 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2828 union ldlm_policy_data *policy = &info->mti_policy;
2829 struct ldlm_res_id *res_id = &info->mti_res_id;
2834 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2835 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2836 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2837 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2840 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2841 lh->mlh_reg_mode == LCK_EX);
2842 dlmflags |= LDLM_FL_COS_INCOMPAT;
2843 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2844 dlmflags |= LDLM_FL_COS_ENABLED;
2847 /* Only enqueue LOOKUP lock for remote object */
2848 LASSERT(ergo(mdt_object_remote(o), *ibits == MDS_INODELOCK_LOOKUP));
2850 if (lh->mlh_type == MDT_PDO_LOCK) {
2851 /* check for exists after object is locked */
2852 if (mdt_object_exists(o) == 0) {
2853 /* Non-existent object shouldn't have PDO lock */
2856 /* Non-dir object shouldn't have PDO lock */
2857 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2863 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2864 dlmflags |= LDLM_FL_ATOMIC_CB;
2867 * Take PDO lock on whole directory and build correct @res_id for lock
2868 * on part of directory.
2870 if (lh->mlh_pdo_hash != 0) {
2871 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2872 mdt_lock_pdo_mode(info, o, lh);
2873 if (lh->mlh_pdo_mode != LCK_NL) {
2875 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2876 * is never going to be sent to client and we do not
2877 * want it slowed down due to possible cancels.
2879 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2880 policy->l_inodebits.try_bits = 0;
2881 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2882 policy, res_id, dlmflags,
2883 info->mti_exp == NULL ? NULL :
2884 &info->mti_exp->exp_handle.h_cookie);
2885 if (unlikely(rc != 0))
2886 GOTO(out_unlock, rc);
2890 * Finish res_id initializing by name hash marking part of
2891 * directory which is taking modification.
2893 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2896 policy->l_inodebits.bits = *ibits;
2897 policy->l_inodebits.try_bits = trybits;
2900 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2901 * going to be sent to client. If it is - mdt_intent_policy() path will
2902 * fix it up and turn FL_LOCAL flag off.
2904 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2905 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2906 info->mti_exp == NULL ? NULL :
2907 &info->mti_exp->exp_handle.h_cookie);
2910 mdt_object_unlock(info, o, lh, 1);
2911 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2912 lh->mlh_pdo_hash != 0 &&
2913 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2914 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2916 /* Return successfully acquired bits to a caller */
2918 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2921 *ibits = lock->l_policy_data.l_inodebits.bits;
2922 LDLM_LOCK_PUT(lock);
2928 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2929 struct mdt_lock_handle *lh, __u64 *ibits,
2930 __u64 trybits, bool cos_incompat)
2932 struct mdt_lock_handle *local_lh = NULL;
2936 if (!mdt_object_remote(o)) {
2937 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
2942 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2943 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2944 MDS_INODELOCK_XATTR);
2946 /* Only enqueue LOOKUP lock for remote object */
2947 if (*ibits & MDS_INODELOCK_LOOKUP) {
2948 __u64 local = MDS_INODELOCK_LOOKUP;
2950 rc = mdt_object_local_lock(info, o, lh, &local, 0,
2958 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
2959 /* Sigh, PDO needs to enqueue 2 locks right now, but
2960 * enqueue RPC can only request 1 lock, to avoid extra
2961 * RPC, so it will instead enqueue EX lock for remote
2962 * object anyway XXX*/
2963 if (lh->mlh_type == MDT_PDO_LOCK &&
2964 lh->mlh_pdo_hash != 0) {
2965 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2966 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2967 PFID(mdt_object_fid(o)));
2968 lh->mlh_pdo_hash = 0;
2969 lh->mlh_rreg_mode = LCK_EX;
2970 lh->mlh_type = MDT_REG_LOCK;
2973 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
2976 ibits, trybits, false);
2977 if (rc != ELDLM_OK) {
2978 if (local_lh != NULL)
2979 mdt_object_unlock(info, o, local_lh, rc);
2987 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2988 struct mdt_lock_handle *lh, __u64 ibits)
2990 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
2993 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2994 struct mdt_lock_handle *lh, __u64 ibits,
2997 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2998 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
3002 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
3003 struct mdt_lock_handle *lh, __u64 *ibits,
3004 __u64 trybits, bool cos_incompat)
3006 bool trylock_only = *ibits == 0;
3009 LASSERT(!(*ibits & trybits));
3010 rc = mdt_object_lock_internal(info, o, lh, ibits, trybits,
3012 if (rc && trylock_only) { /* clear error for try ibits lock only */
3013 LASSERT(*ibits == 0);
3020 * Save a lock within request object.
3022 * Keep the lock referenced until whether client ACK or transaction
3023 * commit happens or release the lock immediately depending on input
3024 * parameters. If COS is ON, a write lock is converted to COS lock
3027 * \param info thead info object
3028 * \param h lock handle
3029 * \param mode lock mode
3030 * \param decref force immediate lock releasing
3032 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
3033 enum ldlm_mode mode, int decref)
3037 if (lustre_handle_is_used(h)) {
3038 if (decref || !info->mti_has_trans ||
3039 !(mode & (LCK_PW | LCK_EX))) {
3040 mdt_fid_unlock(h, mode);
3042 struct mdt_device *mdt = info->mti_mdt;
3043 struct ldlm_lock *lock = ldlm_handle2lock(h);
3044 struct ptlrpc_request *req = mdt_info_req(info);
3045 bool cos = mdt_cos_is_enabled(mdt);
3046 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
3048 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
3051 /* there is no request if mdt_object_unlock() is called
3052 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
3053 if (likely(req != NULL)) {
3054 LDLM_DEBUG(lock, "save lock request %p reply "
3055 "state %p transno %lld\n", req,
3056 req->rq_reply_state, req->rq_transno);
3058 ldlm_lock_mode_downgrade(lock, LCK_COS);
3061 if (req->rq_export->exp_disconnected)
3062 mdt_fid_unlock(h, mode);
3064 ptlrpc_save_lock(req, h, mode, cos,
3067 mdt_fid_unlock(h, mode);
3069 if (mdt_is_lock_sync(lock)) {
3070 CDEBUG(D_HA, "found sync-lock,"
3071 " async commit started\n");
3072 mdt_device_commit_async(info->mti_env,
3075 LDLM_LOCK_PUT(lock);
3084 * Save cross-MDT lock in uncommitted_slc_locks
3086 * Keep the lock referenced until transaction commit happens or release the lock
3087 * immediately depending on input parameters.
3089 * \param info thead info object
3090 * \param h lock handle
3091 * \param mode lock mode
3092 * \param decref force immediate lock releasing
3094 static void mdt_save_remote_lock(struct mdt_thread_info *info,
3095 struct mdt_object *o, struct lustre_handle *h,
3096 enum ldlm_mode mode, int decref)
3100 if (lustre_handle_is_used(h)) {
3101 struct ldlm_lock *lock = ldlm_handle2lock(h);
3104 (lock->l_policy_data.l_inodebits.bits &
3105 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
3106 mo_invalidate(info->mti_env, mdt_object_child(o));
3108 if (decref || !info->mti_has_trans ||
3109 !(mode & (LCK_PW | LCK_EX))) {
3110 ldlm_lock_decref_and_cancel(h, mode);
3111 LDLM_LOCK_PUT(lock);
3113 struct ptlrpc_request *req = mdt_info_req(info);
3115 LASSERT(req != NULL);
3116 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3118 ldlm_lock_decref(h, mode);
3127 * Unlock mdt object.
3129 * Immeditely release the regular lock and the PDO lock or save the
3130 * lock in request and keep them referenced until client ACK or
3131 * transaction commit.
3133 * \param info thread info object
3134 * \param o mdt object
3135 * \param lh mdt lock handle referencing regular and PDO locks
3136 * \param decref force immediate lock releasing
3138 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3140 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3141 struct mdt_lock_handle *lh, int decref)
3145 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3146 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3147 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3153 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3154 const struct lu_fid *f,
3155 struct mdt_lock_handle *lh,
3158 struct mdt_object *o;
3160 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3164 rc = mdt_object_lock(info, o, lh, ibits);
3166 mdt_object_put(info->mti_env, o);
3173 void mdt_object_unlock_put(struct mdt_thread_info * info,
3174 struct mdt_object * o,
3175 struct mdt_lock_handle *lh,
3178 mdt_object_unlock(info, o, lh, decref);
3179 mdt_object_put(info->mti_env, o);
3183 * Generic code handling requests that have struct mdt_body passed in:
3185 * - extract mdt_body from request and save it in @info, if present;
3187 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3190 * - if HABEO_CORPUS flag is set for this request type check whether object
3191 * actually exists on storage (lu_object_exists()).
3194 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3196 const struct mdt_body *body;
3197 struct mdt_object *obj;
3198 const struct lu_env *env;
3199 struct req_capsule *pill;
3203 env = info->mti_env;
3204 pill = info->mti_pill;
3206 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3210 if (!(body->mbo_valid & OBD_MD_FLID))
3213 if (!fid_is_sane(&body->mbo_fid1)) {
3214 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3218 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3220 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3221 mdt_object_put(env, obj);
3224 info->mti_object = obj;
3233 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3235 struct req_capsule *pill = info->mti_pill;
3239 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3240 rc = mdt_body_unpack(info, flags);
3244 if (rc == 0 && (flags & HABEO_REFERO)) {
3246 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3247 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3249 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3250 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3253 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3254 * by default. If the target object has more ACL entries, then
3255 * enlarge the buffer when necessary. */
3256 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3257 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3258 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3260 rc = req_capsule_server_pack(pill);
3265 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3267 lh->mlh_type = MDT_NUL_LOCK;
3268 lh->mlh_reg_lh.cookie = 0ull;
3269 lh->mlh_reg_mode = LCK_MINMODE;
3270 lh->mlh_pdo_lh.cookie = 0ull;
3271 lh->mlh_pdo_mode = LCK_MINMODE;
3272 lh->mlh_rreg_lh.cookie = 0ull;
3273 lh->mlh_rreg_mode = LCK_MINMODE;
3276 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3278 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3279 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3283 * Initialize fields of struct mdt_thread_info. Other fields are left in
3284 * uninitialized state, because it's too expensive to zero out whole
3285 * mdt_thread_info (> 1K) on each request arrival.
3287 void mdt_thread_info_init(struct ptlrpc_request *req,
3288 struct mdt_thread_info *info)
3292 info->mti_pill = &req->rq_pill;
3295 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3296 mdt_lock_handle_init(&info->mti_lh[i]);
3298 /* mdt device: it can be NULL while CONNECT */
3299 if (req->rq_export) {
3300 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3301 info->mti_exp = req->rq_export;
3303 info->mti_mdt = NULL;
3304 info->mti_env = req->rq_svc_thread->t_env;
3305 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3307 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3308 info->mti_big_buf = LU_BUF_NULL;
3309 info->mti_body = NULL;
3310 info->mti_object = NULL;
3311 info->mti_dlm_req = NULL;
3312 info->mti_has_trans = 0;
3313 info->mti_cross_ref = 0;
3314 info->mti_opdata = 0;
3315 info->mti_big_lmm_used = 0;
3316 info->mti_big_acl_used = 0;
3317 info->mti_som_valid = 0;
3319 info->mti_spec.no_create = 0;
3320 info->mti_spec.sp_rm_entry = 0;
3321 info->mti_spec.sp_permitted = 0;
3322 info->mti_spec.sp_migrate_close = 0;
3324 info->mti_spec.u.sp_ea.eadata = NULL;
3325 info->mti_spec.u.sp_ea.eadatalen = 0;
3328 void mdt_thread_info_fini(struct mdt_thread_info *info)
3332 if (info->mti_object != NULL) {
3333 mdt_object_put(info->mti_env, info->mti_object);
3334 info->mti_object = NULL;
3337 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3338 mdt_lock_handle_fini(&info->mti_lh[i]);
3339 info->mti_env = NULL;
3340 info->mti_pill = NULL;
3341 info->mti_exp = NULL;
3343 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3344 lu_buf_free(&info->mti_big_buf);
3347 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3349 struct mdt_thread_info *mti;
3351 mti = mdt_th_info(tsi->tsi_env);
3352 LASSERT(mti != NULL);
3354 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3355 if (tsi->tsi_corpus != NULL) {
3356 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3357 lu_object_get(tsi->tsi_corpus);
3359 mti->mti_body = tsi->tsi_mdt_body;
3360 mti->mti_dlm_req = tsi->tsi_dlm_req;
3365 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3367 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3369 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3370 set_current_state(TASK_UNINTERRUPTIBLE);
3371 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3374 return tgt_connect(tsi);
3394 static int mdt_intent_getattr(enum mdt_it_code opcode,
3395 struct mdt_thread_info *info,
3396 struct ldlm_lock **, __u64);
3398 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3399 struct mdt_thread_info *info,
3400 struct ldlm_lock **lockp,
3403 static int mdt_intent_layout(enum mdt_it_code opcode,
3404 struct mdt_thread_info *info,
3405 struct ldlm_lock **,
3407 static int mdt_intent_reint(enum mdt_it_code opcode,
3408 struct mdt_thread_info *info,
3409 struct ldlm_lock **,
3411 static int mdt_intent_glimpse(enum mdt_it_code opcode,
3412 struct mdt_thread_info *info,
3413 struct ldlm_lock **lockp, __u64 flags)
3415 return mdt_glimpse_enqueue(info, info->mti_mdt->mdt_namespace,
3418 static int mdt_intent_brw(enum mdt_it_code opcode,
3419 struct mdt_thread_info *info,
3420 struct ldlm_lock **lockp, __u64 flags)
3422 return mdt_brw_enqueue(info, info->mti_mdt->mdt_namespace,
3426 static struct mdt_it_flavor {
3427 const struct req_format *it_fmt;
3429 int (*it_act)(enum mdt_it_code ,
3430 struct mdt_thread_info *,
3431 struct ldlm_lock **,
3434 } mdt_it_flavor[] = {
3436 .it_fmt = &RQF_LDLM_INTENT,
3437 /*.it_flags = HABEO_REFERO,*/
3439 .it_act = mdt_intent_reint,
3440 .it_reint = REINT_OPEN
3443 .it_fmt = &RQF_LDLM_INTENT,
3445 * OCREAT is not a MUTABOR request as if the file
3447 * We do the extra check of OBD_CONNECT_RDONLY in
3448 * mdt_reint_open() when we really need to create
3452 .it_act = mdt_intent_reint,
3453 .it_reint = REINT_OPEN
3456 .it_fmt = &RQF_LDLM_INTENT,
3457 .it_flags = MUTABOR,
3458 .it_act = mdt_intent_reint,
3459 .it_reint = REINT_CREATE
3461 [MDT_IT_GETATTR] = {
3462 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3463 .it_flags = HABEO_REFERO,
3464 .it_act = mdt_intent_getattr
3466 [MDT_IT_READDIR] = {
3472 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3473 .it_flags = HABEO_REFERO,
3474 .it_act = mdt_intent_getattr
3477 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3478 .it_flags = MUTABOR,
3480 .it_reint = REINT_UNLINK
3484 .it_flags = MUTABOR,
3487 [MDT_IT_GETXATTR] = {
3488 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3489 .it_flags = HABEO_CORPUS,
3490 .it_act = mdt_intent_getxattr
3493 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3495 .it_act = mdt_intent_layout
3497 [MDT_IT_GLIMPSE] = {
3498 .it_fmt = &RQF_LDLM_INTENT,
3500 .it_act = mdt_intent_glimpse,
3503 .it_fmt = &RQF_LDLM_INTENT,
3505 .it_act = mdt_intent_brw,
3510 int mdt_intent_lock_replace(struct mdt_thread_info *info,
3511 struct ldlm_lock **lockp,
3512 struct mdt_lock_handle *lh,
3513 __u64 flags, int result)
3515 struct ptlrpc_request *req = mdt_info_req(info);
3516 struct ldlm_lock *lock = *lockp;
3517 struct ldlm_lock *new_lock;
3519 /* If possible resent found a lock, @lh is set to its handle */
3520 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3522 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3523 lh->mlh_reg_lh.cookie = 0;
3527 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3528 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3529 * a resend case, however, it could be already destroyed
3530 * due to client eviction or a raced cancel RPC. */
3531 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3532 lh->mlh_reg_lh.cookie);
3533 lh->mlh_reg_lh.cookie = 0;
3537 LASSERTF(new_lock != NULL,
3538 "lockh %#llx flags %#llx : rc = %d\n",
3539 lh->mlh_reg_lh.cookie, flags, result);
3542 * If we've already given this lock to a client once, then we should
3543 * have no readers or writers. Otherwise, we should have one reader
3544 * _or_ writer ref (which will be zeroed below) before returning the
3547 if (new_lock->l_export == req->rq_export) {
3548 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3550 LASSERT(new_lock->l_export == NULL);
3551 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3556 if (new_lock->l_export == req->rq_export) {
3558 * Already gave this to the client, which means that we
3559 * reconstructed a reply.
3561 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3564 LDLM_LOCK_RELEASE(new_lock);
3565 lh->mlh_reg_lh.cookie = 0;
3566 RETURN(ELDLM_LOCK_REPLACED);
3570 * Fixup the lock to be given to the client.
3572 lock_res_and_lock(new_lock);
3573 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3574 * possible blocking AST. */
3575 while (new_lock->l_readers > 0) {
3576 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3577 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3578 new_lock->l_readers--;
3580 while (new_lock->l_writers > 0) {
3581 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3582 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3583 new_lock->l_writers--;
3586 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3587 new_lock->l_blocking_ast = lock->l_blocking_ast;
3588 new_lock->l_completion_ast = lock->l_completion_ast;
3589 if (ldlm_has_dom(new_lock))
3590 new_lock->l_glimpse_ast = ldlm_server_glimpse_ast;
3591 new_lock->l_remote_handle = lock->l_remote_handle;
3592 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3594 unlock_res_and_lock(new_lock);
3596 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3597 &new_lock->l_remote_handle,
3598 &new_lock->l_exp_hash);
3600 LDLM_LOCK_RELEASE(new_lock);
3601 lh->mlh_reg_lh.cookie = 0;
3603 RETURN(ELDLM_LOCK_REPLACED);
3606 void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3607 struct ldlm_lock *new_lock,
3608 struct mdt_lock_handle *lh, __u64 flags)
3610 struct ptlrpc_request *req = mdt_info_req(info);
3611 struct ldlm_request *dlmreq;
3613 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3616 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3618 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3619 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3621 if (flags & LDLM_FL_RESENT) {
3622 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3623 lh->mlh_reg_mode = new_lock->l_granted_mode;
3625 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3626 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3627 lh->mlh_reg_lh.cookie);
3632 * If the xid matches, then we know this is a resent request, and allow
3633 * it. (It's probably an OPEN, for which we don't send a lock.
3635 if (req_can_reconstruct(req, NULL))
3639 * This remote handle isn't enqueued, so we never received or processed
3640 * this request. Clear MSG_RESENT, because it can be handled like any
3641 * normal request now.
3643 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3645 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3646 dlmreq->lock_handle[0].cookie);
3649 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3650 struct mdt_thread_info *info,
3651 struct ldlm_lock **lockp,
3654 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3655 struct ldlm_reply *ldlm_rep = NULL;
3660 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3661 * (for the resend case) or a new lock. Below we will use it to
3662 * replace the original lock.
3664 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3665 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3666 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3667 rc = mdt_object_lock(info, info->mti_object, lhc,
3668 MDS_INODELOCK_XATTR);
3673 rc = mdt_getxattr(info);
3675 if (mdt_info_req(info)->rq_repmsg != NULL)
3676 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3678 if (ldlm_rep == NULL ||
3679 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3680 mdt_object_unlock(info, info->mti_object, lhc, 1);
3681 RETURN(err_serious(-EFAULT));
3684 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3686 /* This is left for interop instead of adding a new interop flag.
3688 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3689 if (ldlm_rep->lock_policy_res2) {
3690 mdt_object_unlock(info, info->mti_object, lhc, 1);
3691 RETURN(ELDLM_LOCK_ABORTED);
3695 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3699 static int mdt_intent_getattr(enum mdt_it_code opcode,
3700 struct mdt_thread_info *info,
3701 struct ldlm_lock **lockp,
3704 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3706 struct ldlm_reply *ldlm_rep;
3707 struct mdt_body *reqbody;
3708 struct mdt_body *repbody;
3712 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3715 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3718 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3719 repbody->mbo_eadatasize = 0;
3720 repbody->mbo_aclsize = 0;
3724 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3726 case MDT_IT_GETATTR:
3727 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3731 CERROR("Unsupported intent (%d)\n", opcode);
3732 GOTO(out_shrink, rc = -EINVAL);
3735 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3737 GOTO(out_shrink, rc);
3739 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3740 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3742 /* Get lock from request for possible resent case. */
3743 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3745 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3746 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3748 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3749 ldlm_rep->lock_policy_res2 = 0;
3750 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3751 ldlm_rep->lock_policy_res2) {
3752 lhc->mlh_reg_lh.cookie = 0ull;
3753 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3756 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3759 mdt_exit_ucred(info);
3761 mdt_client_compatibility(info);
3762 rc2 = mdt_fix_reply(info);
3768 static int mdt_intent_layout(enum mdt_it_code opcode,
3769 struct mdt_thread_info *info,
3770 struct ldlm_lock **lockp,
3773 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3774 struct md_layout_change layout = { .mlc_opc = MD_LAYOUT_NOP };
3775 struct layout_intent *intent;
3776 struct lu_fid *fid = &info->mti_tmp_fid2;
3777 struct mdt_object *obj = NULL;
3778 int layout_size = 0;
3782 if (opcode != MDT_IT_LAYOUT) {
3783 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3788 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3790 intent = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3794 CDEBUG(D_INFO, DFID "got layout change request from client: "
3795 "opc:%u flags:%#x extent "DEXT"\n",
3796 PFID(fid), intent->li_opc, intent->li_flags,
3797 PEXT(&intent->li_extent));
3799 switch (intent->li_opc) {
3800 case LAYOUT_INTENT_TRUNC:
3801 case LAYOUT_INTENT_WRITE:
3802 layout.mlc_opc = MD_LAYOUT_WRITE;
3803 layout.mlc_intent = intent;
3805 case LAYOUT_INTENT_ACCESS:
3807 case LAYOUT_INTENT_READ:
3808 case LAYOUT_INTENT_GLIMPSE:
3809 case LAYOUT_INTENT_RELEASE:
3810 case LAYOUT_INTENT_RESTORE:
3811 CERROR("%s: Unsupported layout intent opc %d\n",
3812 mdt_obd_name(info->mti_mdt), intent->li_opc);
3816 CERROR("%s: Unknown layout intent opc %d\n",
3817 mdt_obd_name(info->mti_mdt), intent->li_opc);
3824 /* Get lock from request for possible resent case. */
3825 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3827 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3829 GOTO(out, rc = PTR_ERR(obj));
3832 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3833 /* if layout is going to be changed don't use the current EA
3834 * size but the maximum one. That buffer will be shrinked
3835 * to the actual size in req_capsule_shrink() before reply.
3837 if (layout.mlc_opc == MD_LAYOUT_WRITE) {
3838 layout_size = info->mti_mdt->mdt_max_mdsize;
3840 layout_size = mdt_attr_get_eabuf_size(info, obj);
3841 if (layout_size < 0)
3842 GOTO(out_obj, rc = layout_size);
3844 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3845 info->mti_mdt->mdt_max_mdsize = layout_size;
3850 * set reply buffer size, so that ldlm_handle_enqueue0()->
3851 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3853 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3854 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3856 rc = req_capsule_server_pack(info->mti_pill);
3861 if (layout.mlc_opc != MD_LAYOUT_NOP) {
3862 struct lu_buf *buf = &layout.mlc_buf;
3865 * mdt_layout_change is a reint operation, when the request
3866 * is resent, layout write shouldn't reprocess it again.
3868 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
3870 GOTO(out_obj, rc = rc < 0 ? rc : 0);
3873 * There is another resent case: the client's job has been
3874 * done by another client, referring lod_declare_layout_change
3875 * -EALREADY case, and it became a operation w/o transaction,
3876 * so we should not do the layout change, otherwise
3877 * mdt_layout_change() will try to cancel the granted server
3878 * CR lock whose remote counterpart is still in hold on the
3879 * client, and a deadlock ensues.
3881 rc = mdt_check_resent_lock(info, obj, lhc);
3887 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3888 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3890 buf->lb_len = req_capsule_get_size(info->mti_pill,
3891 &RMF_EADATA, RCL_CLIENT);
3893 * If it's a replay of layout write intent RPC, the
3894 * client has saved the extended lovea when
3895 * it get reply then.
3897 if (buf->lb_len > 0)
3898 mdt_fix_lov_magic(info, buf->lb_buf);
3901 * Instantiate some layout components, if @buf contains
3902 * lovea, then it's a replay of the layout intent write
3905 rc = mdt_layout_change(info, obj, &layout);
3910 mdt_object_put(info->mti_env, obj);
3912 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3913 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3916 lhc->mlh_reg_lh.cookie = 0;
3921 static int mdt_intent_reint(enum mdt_it_code opcode,
3922 struct mdt_thread_info *info,
3923 struct ldlm_lock **lockp,
3926 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3927 struct ldlm_reply *rep = NULL;
3931 static const struct req_format *intent_fmts[REINT_MAX] = {
3932 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3933 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3938 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3942 if (mdt_it_flavor[opcode].it_reint != opc) {
3943 CERROR("Reint code %ld doesn't match intent: %d\n",
3945 RETURN(err_serious(-EPROTO));
3948 /* Get lock from request for possible resent case. */
3949 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3951 rc = mdt_reint_internal(info, lhc, opc);
3953 /* Check whether the reply has been packed successfully. */
3954 if (mdt_info_req(info)->rq_repmsg != NULL)
3955 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3957 RETURN(err_serious(-EFAULT));
3959 /* MDC expects this in any case */
3961 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3963 /* the open lock or the lock for cross-ref object should be
3964 * returned to the client */
3965 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3966 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3967 rep->lock_policy_res2 = 0;
3968 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3972 rep->lock_policy_res2 = clear_serious(rc);
3974 if (rep->lock_policy_res2 == -ENOENT &&
3975 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3976 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3977 rep->lock_policy_res2 = 0;
3979 lhc->mlh_reg_lh.cookie = 0ull;
3980 if (rc == -ENOTCONN || rc == -ENODEV ||
3981 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3983 * If it is the disconnect error (ENODEV & ENOCONN), the error
3984 * will be returned by rq_status, and client at ptlrpc layer
3985 * will detect this, then disconnect, reconnect the import
3986 * immediately, instead of impacting the following the rpc.
3991 * For other cases, the error will be returned by intent, and client
3992 * will retrieve the result from intent.
3994 RETURN(ELDLM_LOCK_ABORTED);
3997 static int mdt_intent_code(enum ldlm_intent_flags itcode)
4005 case IT_OPEN|IT_CREAT:
4012 rc = MDT_IT_READDIR;
4015 rc = MDT_IT_GETATTR;
4027 rc = MDT_IT_GETXATTR;
4032 case IT_QUOTA_DQACQ:
4037 rc = MDT_IT_GLIMPSE;
4043 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
4050 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
4051 struct mdt_thread_info *info,
4052 struct ldlm_lock **lockp, __u64 flags)
4054 struct req_capsule *pill = info->mti_pill;
4055 struct ptlrpc_request *req = mdt_info_req(info);
4056 struct mdt_it_flavor *flv;
4061 opc = mdt_intent_code(itopc);
4065 if (opc == MDT_IT_QUOTA) {
4066 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
4069 RETURN(-EOPNOTSUPP);
4071 if (mdt_rdonly(req->rq_export))
4074 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
4075 /* pass the request to quota master */
4076 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
4077 mdt_info_req(info), lockp,
4082 flv = &mdt_it_flavor[opc];
4083 if (flv->it_fmt != NULL)
4084 req_capsule_extend(pill, flv->it_fmt);
4086 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
4090 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
4093 if (flv->it_act != NULL) {
4094 struct ldlm_reply *rep;
4096 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
4098 /* execute policy */
4099 rc = flv->it_act(opc, info, lockp, flags);
4101 /* Check whether the reply has been packed successfully. */
4102 if (req->rq_repmsg != NULL) {
4103 rep = req_capsule_server_get(info->mti_pill,
4105 rep->lock_policy_res2 =
4106 ptlrpc_status_hton(rep->lock_policy_res2);
4113 static void mdt_ptlrpc_stats_update(struct ptlrpc_request *req,
4114 enum ldlm_intent_flags it_opc)
4116 struct lprocfs_stats *srv_stats = ptlrpc_req2svc(req)->srv_stats;
4118 /* update stats when IT code is known */
4119 if (srv_stats != NULL)
4120 lprocfs_counter_incr(srv_stats,
4121 PTLRPC_LAST_CNTR + (it_opc == IT_GLIMPSE ?
4122 LDLM_GLIMPSE_ENQUEUE : LDLM_IBITS_ENQUEUE));
4125 static int mdt_intent_policy(struct ldlm_namespace *ns,
4126 struct ldlm_lock **lockp, void *req_cookie,
4127 enum ldlm_mode mode, __u64 flags, void *data)
4129 struct tgt_session_info *tsi;
4130 struct mdt_thread_info *info;
4131 struct ptlrpc_request *req = req_cookie;
4132 struct ldlm_intent *it;
4133 struct req_capsule *pill;
4134 const struct ldlm_lock_desc *ldesc;
4139 LASSERT(req != NULL);
4141 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
4143 info = tsi2mdt_info(tsi);
4144 LASSERT(info != NULL);
4145 pill = info->mti_pill;
4146 LASSERT(pill->rc_req == req);
4147 ldesc = &info->mti_dlm_req->lock_desc;
4149 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
4150 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
4151 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
4153 mdt_ptlrpc_stats_update(req, it->opc);
4154 rc = mdt_intent_opc(it->opc, info, lockp, flags);
4158 /* Lock without inodebits makes no sense and will oops
4159 * later in ldlm. Let's check it now to see if we have
4160 * ibits corrupted somewhere in mdt_intent_opc().
4161 * The case for client miss to set ibits has been
4162 * processed by others. */
4163 LASSERT(ergo(ldesc->l_resource.lr_type == LDLM_IBITS,
4164 ldesc->l_policy_data.l_inodebits.bits != 0));
4166 rc = err_serious(-EFAULT);
4169 /* No intent was provided */
4170 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4171 rc = req_capsule_server_pack(pill);
4173 rc = err_serious(rc);
4175 mdt_thread_info_fini(info);
4179 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4181 struct seq_server_site *ss = mdt_seq_site(mdt);
4183 if (ss->ss_node_id == 0)
4186 if (ss->ss_client_seq != NULL) {
4187 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4188 ss->ss_client_seq->lcs_exp = NULL;
4191 if (ss->ss_server_fld != NULL) {
4192 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4193 ss->ss_server_fld->lsf_control_exp = NULL;
4197 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4199 struct seq_server_site *ss = mdt_seq_site(mdt);
4204 if (ss->ss_server_seq != NULL)
4205 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4208 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4210 mdt_seq_fini_cli(mdt);
4211 mdt_deregister_seq_exp(mdt);
4213 return seq_site_fini(env, mdt_seq_site(mdt));
4217 * It will retrieve its FLDB entries from MDT0, and it only happens
4218 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4219 * and it needs to refresh FLDB from the MDT0.
4221 static int mdt_register_lwp_callback(void *data)
4224 struct mdt_device *mdt = data;
4225 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4229 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4231 rc = lu_env_init(&env, LCT_MD_THREAD);
4233 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4237 /* Allocate new sequence now to avoid creating local transaction
4238 * in the normal transaction process */
4239 rc = seq_server_check_and_alloc_super(&env,
4240 mdt_seq_site(mdt)->ss_server_seq);
4245 rc = fld_update_from_controller(&env, fld);
4247 CERROR("%s: cannot update controller: rc = %d\n",
4248 mdt_obd_name(mdt), rc);
4257 static int mdt_register_seq_exp(struct mdt_device *mdt)
4259 struct seq_server_site *ss = mdt_seq_site(mdt);
4260 char *lwp_name = NULL;
4263 if (ss->ss_node_id == 0)
4266 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4267 if (lwp_name == NULL)
4268 GOTO(out_free, rc = -ENOMEM);
4270 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4274 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4279 rc = lustre_register_lwp_item(lwp_name,
4280 &ss->ss_server_fld->lsf_control_exp,
4281 mdt_register_lwp_callback, mdt);
4283 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4284 ss->ss_client_seq->lcs_exp = NULL;
4288 if (lwp_name != NULL)
4289 OBD_FREE(lwp_name, MAX_OBD_NAME);
4295 * Init client sequence manager which is used by local MDS to talk to sequence
4296 * controller on remote node.
4298 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4300 struct seq_server_site *ss = mdt_seq_site(mdt);
4305 /* check if this is adding the first MDC and controller is not yet
4307 OBD_ALLOC_PTR(ss->ss_client_seq);
4308 if (ss->ss_client_seq == NULL)
4311 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4312 if (prefix == NULL) {
4313 OBD_FREE_PTR(ss->ss_client_seq);
4314 ss->ss_client_seq = NULL;
4318 /* Note: seq_client_fini will be called in seq_site_fini */
4319 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4320 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4321 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4323 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4325 OBD_FREE_PTR(ss->ss_client_seq);
4326 ss->ss_client_seq = NULL;
4330 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4335 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4337 struct seq_server_site *ss;
4341 ss = mdt_seq_site(mdt);
4342 /* init sequence controller server(MDT0) */
4343 if (ss->ss_node_id == 0) {
4344 OBD_ALLOC_PTR(ss->ss_control_seq);
4345 if (ss->ss_control_seq == NULL)
4348 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4349 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4352 GOTO(out_seq_fini, rc);
4355 /* Init normal sequence server */
4356 OBD_ALLOC_PTR(ss->ss_server_seq);
4357 if (ss->ss_server_seq == NULL)
4358 GOTO(out_seq_fini, rc = -ENOMEM);
4360 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4361 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4363 GOTO(out_seq_fini, rc);
4365 /* init seq client for seq server to talk to seq controller(MDT0) */
4366 rc = mdt_seq_init_cli(env, mdt);
4368 GOTO(out_seq_fini, rc);
4370 if (ss->ss_node_id != 0)
4371 /* register controller export through lwp */
4372 rc = mdt_register_seq_exp(mdt);
4377 mdt_seq_fini(env, mdt);
4385 static int mdt_fld_fini(const struct lu_env *env,
4386 struct mdt_device *m)
4388 struct seq_server_site *ss = mdt_seq_site(m);
4391 if (ss && ss->ss_server_fld) {
4392 fld_server_fini(env, ss->ss_server_fld);
4393 OBD_FREE_PTR(ss->ss_server_fld);
4394 ss->ss_server_fld = NULL;
4400 static int mdt_fld_init(const struct lu_env *env,
4402 struct mdt_device *m)
4404 struct seq_server_site *ss;
4408 ss = mdt_seq_site(m);
4410 OBD_ALLOC_PTR(ss->ss_server_fld);
4411 if (ss->ss_server_fld == NULL)
4412 RETURN(rc = -ENOMEM);
4414 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4417 OBD_FREE_PTR(ss->ss_server_fld);
4418 ss->ss_server_fld = NULL;
4425 static void mdt_stack_pre_fini(const struct lu_env *env,
4426 struct mdt_device *m, struct lu_device *top)
4428 struct lustre_cfg_bufs *bufs;
4429 struct lustre_cfg *lcfg;
4430 struct mdt_thread_info *info;
4435 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4436 LASSERT(info != NULL);
4438 bufs = &info->mti_u.bufs;
4440 LASSERT(m->mdt_child_exp);
4441 LASSERT(m->mdt_child_exp->exp_obd);
4443 /* process cleanup, pass mdt obd name to get obd umount flags */
4444 /* XXX: this is needed because all layers are referenced by
4445 * objects (some of them are pinned by osd, for example *
4446 * the proper solution should be a model where object used
4447 * by osd only doesn't have mdt/mdd slices -bzzz */
4448 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4449 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4450 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4453 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4455 top->ld_ops->ldo_process_config(env, top, lcfg);
4456 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4460 static void mdt_stack_fini(const struct lu_env *env,
4461 struct mdt_device *m, struct lu_device *top)
4463 struct obd_device *obd = mdt2obd_dev(m);
4464 struct lustre_cfg_bufs *bufs;
4465 struct lustre_cfg *lcfg;
4466 struct mdt_thread_info *info;
4470 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4471 LASSERT(info != NULL);
4473 lu_dev_del_linkage(top->ld_site, top);
4475 lu_site_purge(env, top->ld_site, -1);
4477 bufs = &info->mti_u.bufs;
4478 /* process cleanup, pass mdt obd name to get obd umount flags */
4479 /* another purpose is to let all layers to release their objects */
4480 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4485 lustre_cfg_bufs_set_string(bufs, 1, flags);
4486 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4489 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
4492 top->ld_ops->ldo_process_config(env, top, lcfg);
4493 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4495 lu_site_purge(env, top->ld_site, -1);
4497 m->mdt_child = NULL;
4498 m->mdt_bottom = NULL;
4500 obd_disconnect(m->mdt_child_exp);
4501 m->mdt_child_exp = NULL;
4503 obd_disconnect(m->mdt_bottom_exp);
4504 m->mdt_child_exp = NULL;
4507 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4508 const char *next, struct obd_export **exp)
4510 struct obd_connect_data *data = NULL;
4511 struct obd_device *obd;
4515 OBD_ALLOC_PTR(data);
4517 GOTO(out, rc = -ENOMEM);
4519 obd = class_name2obd(next);
4521 CERROR("%s: can't locate next device: %s\n",
4522 mdt_obd_name(m), next);
4523 GOTO(out, rc = -ENOTCONN);
4526 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4527 data->ocd_version = LUSTRE_VERSION_CODE;
4529 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4531 CERROR("%s: cannot connect to next dev %s (%d)\n",
4532 mdt_obd_name(m), next, rc);
4542 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4543 struct lustre_cfg *cfg)
4545 char *dev = lustre_cfg_string(cfg, 0);
4546 int rc, name_size, uuid_size;
4547 char *name, *uuid, *p;
4548 struct lustre_cfg_bufs *bufs;
4549 struct lustre_cfg *lcfg;
4550 struct obd_device *obd;
4551 struct lustre_profile *lprof;
4552 struct lu_site *site;
4555 /* in 1.8 we had the only device in the stack - MDS.
4556 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4557 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4558 * to generate names and setup MDT, MDD. MDT will be using
4559 * generated name to connect to MDD. for MDD the next device
4560 * will be LOD with name taken from so called "profile" which
4561 * is generated by mount_option line
4563 * 1.8 MGS generates config. commands like this:
4564 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4565 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4566 * 2.0 MGS generates config. commands like this:
4567 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4568 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4569 * 3:lustre-MDT0000-mdtlov 4:f
4571 * we generate MDD name from MDT one, just replacing T with D
4573 * after all the preparations, the logical equivalent will be
4574 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4575 * 3:lustre-MDT0000-mdtlov 4:f
4576 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4577 * 3:lustre-MDD0000 4:f
4579 * notice we build the stack from down to top: MDD first, then MDT */
4581 name_size = MAX_OBD_NAME;
4582 uuid_size = MAX_OBD_NAME;
4584 OBD_ALLOC(name, name_size);
4585 OBD_ALLOC(uuid, uuid_size);
4586 if (name == NULL || uuid == NULL)
4587 GOTO(cleanup_mem, rc = -ENOMEM);
4589 OBD_ALLOC_PTR(bufs);
4591 GOTO(cleanup_mem, rc = -ENOMEM);
4594 p = strstr(name, "-MDT");
4596 GOTO(free_bufs, rc = -ENOMEM);
4599 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4601 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4602 if (lprof == NULL || lprof->lp_dt == NULL) {
4603 CERROR("can't find the profile: %s\n",
4604 lustre_cfg_string(cfg, 0));
4605 GOTO(free_bufs, rc = -EINVAL);
4608 lustre_cfg_bufs_reset(bufs, name);
4609 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4610 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4611 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4613 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4615 GOTO(put_profile, rc = -ENOMEM);
4616 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4618 rc = class_attach(lcfg);
4620 GOTO(lcfg_cleanup, rc);
4622 obd = class_name2obd(name);
4624 CERROR("Can not find obd %s (%s in config)\n",
4625 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4626 GOTO(lcfg_cleanup, rc = -EINVAL);
4629 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4631 lustre_cfg_bufs_reset(bufs, name);
4632 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4633 lustre_cfg_bufs_set_string(bufs, 2, dev);
4634 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4636 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4638 GOTO(class_detach, rc = -ENOMEM);
4639 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4641 rc = class_setup(obd, lcfg);
4643 GOTO(class_detach, rc);
4645 /* connect to MDD we just setup */
4646 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4648 GOTO(class_detach, rc);
4650 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4652 LASSERT(mdt_lu_site(mdt) == NULL);
4653 mdt->mdt_lu_dev.ld_site = site;
4654 site->ls_top_dev = &mdt->mdt_lu_dev;
4655 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4657 /* now connect to bottom OSD */
4658 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4659 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4661 GOTO(class_detach, rc);
4663 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4665 rc = lu_env_refill((struct lu_env *)env);
4667 CERROR("Failure to refill session: '%d'\n", rc);
4669 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4674 class_detach(obd, lcfg);
4676 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4678 class_put_profile(lprof);
4683 OBD_FREE(name, name_size);
4685 OBD_FREE(uuid, uuid_size);
4689 /* setup quota master target on MDT0 */
4690 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4691 struct lustre_cfg *cfg)
4693 struct obd_device *obd;
4694 char *dev = lustre_cfg_string(cfg, 0);
4695 char *qmtname, *uuid, *p;
4696 struct lustre_cfg_bufs *bufs;
4697 struct lustre_cfg *lcfg;
4698 struct lustre_profile *lprof;
4699 struct obd_connect_data *data;
4703 LASSERT(mdt->mdt_qmt_exp == NULL);
4704 LASSERT(mdt->mdt_qmt_dev == NULL);
4706 /* quota master is on MDT0 only for now */
4707 if (mdt->mdt_seq_site.ss_node_id != 0)
4710 /* MGS generates config commands which look as follows:
4711 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4712 * 3:lustre-MDT0000-mdtlov 4:f
4714 * We generate the QMT name from the MDT one, just replacing MD with QM
4715 * after all the preparations, the logical equivalent will be:
4716 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4717 * 3:lustre-MDT0000-osd 4:f */
4718 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4719 OBD_ALLOC(uuid, UUID_MAX);
4720 OBD_ALLOC_PTR(bufs);
4721 OBD_ALLOC_PTR(data);
4722 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4723 GOTO(cleanup_mem, rc = -ENOMEM);
4725 strcpy(qmtname, dev);
4726 p = strstr(qmtname, "-MDT");
4728 GOTO(cleanup_mem, rc = -ENOMEM);
4729 /* replace MD with QM */
4733 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4735 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4736 if (lprof == NULL || lprof->lp_dt == NULL) {
4737 CERROR("can't find profile for %s\n",
4738 lustre_cfg_string(cfg, 0));
4739 GOTO(cleanup_mem, rc = -EINVAL);
4742 lustre_cfg_bufs_reset(bufs, qmtname);
4743 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4744 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4745 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4747 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4749 GOTO(put_profile, rc = -ENOMEM);
4750 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4752 rc = class_attach(lcfg);
4754 GOTO(lcfg_cleanup, rc);
4756 obd = class_name2obd(qmtname);
4758 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4759 lustre_cfg_string(cfg, 0));
4760 GOTO(lcfg_cleanup, rc = -EINVAL);
4763 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4765 lustre_cfg_bufs_reset(bufs, qmtname);
4766 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4767 lustre_cfg_bufs_set_string(bufs, 2, dev);
4769 /* for quota, the next device should be the OSD device */
4770 lustre_cfg_bufs_set_string(bufs, 3,
4771 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4773 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4775 GOTO(class_detach, rc = -ENOMEM);
4776 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4778 rc = class_setup(obd, lcfg);
4780 GOTO(class_detach, rc);
4782 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4784 /* configure local quota objects */
4785 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4789 GOTO(class_cleanup, rc);
4791 /* connect to quota master target */
4792 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4793 data->ocd_version = LUSTRE_VERSION_CODE;
4794 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4797 CERROR("cannot connect to quota master device %s (%d)\n",
4799 GOTO(class_cleanup, rc);
4805 class_manual_cleanup(obd);
4806 mdt->mdt_qmt_dev = NULL;
4810 class_detach(obd, lcfg);
4812 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4814 class_put_profile(lprof);
4819 OBD_FREE(qmtname, MAX_OBD_NAME);
4821 OBD_FREE(uuid, UUID_MAX);
4827 /* Shutdown quota master target associated with mdt */
4828 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4832 if (mdt->mdt_qmt_exp == NULL)
4834 LASSERT(mdt->mdt_qmt_dev != NULL);
4836 /* the qmt automatically shuts down when the mdt disconnects */
4837 obd_disconnect(mdt->mdt_qmt_exp);
4838 mdt->mdt_qmt_exp = NULL;
4839 mdt->mdt_qmt_dev = NULL;
4843 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4844 * for now. This will be removed along with converting rest of MDT code
4845 * to use tgt_session_info */
4846 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4848 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4851 rc = mdt_getxattr(info);
4853 mdt_thread_info_fini(info);
4857 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
4858 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
4859 #define OST_BRW_READ OST_READ
4860 #define OST_BRW_WRITE OST_WRITE
4862 static struct tgt_handler mdt_tgt_handlers[] = {
4863 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4864 0, MDS_CONNECT, mdt_tgt_connect,
4865 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4866 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4867 0, MDS_DISCONNECT, tgt_disconnect,
4868 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4869 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4870 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4871 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4872 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4873 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4874 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4875 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4877 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4878 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4879 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4880 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4881 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4882 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4883 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4884 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4886 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4887 mdt_hsm_ct_register),
4888 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4889 mdt_hsm_ct_unregister),
4890 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4892 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4894 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4895 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4897 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4902 static struct tgt_handler mdt_io_ops[] = {
4903 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO, OST_BRW_READ, tgt_brw_read),
4904 TGT_OST_HDL(HABEO_CORPUS | MUTABOR, OST_BRW_WRITE, tgt_brw_write),
4905 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4906 OST_PUNCH, mdt_punch_hdl),
4907 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO, OST_SYNC, mdt_data_sync),
4910 static struct tgt_handler mdt_sec_ctx_ops[] = {
4911 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4912 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4913 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4916 static struct tgt_handler mdt_quota_ops[] = {
4917 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4920 static struct tgt_opc_slice mdt_common_slice[] = {
4922 .tos_opc_start = MDS_FIRST_OPC,
4923 .tos_opc_end = MDS_LAST_OPC,
4924 .tos_hs = mdt_tgt_handlers
4927 .tos_opc_start = OBD_FIRST_OPC,
4928 .tos_opc_end = OBD_LAST_OPC,
4929 .tos_hs = tgt_obd_handlers
4932 .tos_opc_start = LDLM_FIRST_OPC,
4933 .tos_opc_end = LDLM_LAST_OPC,
4934 .tos_hs = tgt_dlm_handlers
4937 .tos_opc_start = SEC_FIRST_OPC,
4938 .tos_opc_end = SEC_LAST_OPC,
4939 .tos_hs = mdt_sec_ctx_ops
4942 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4943 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4944 .tos_hs = tgt_out_handlers
4947 .tos_opc_start = FLD_FIRST_OPC,
4948 .tos_opc_end = FLD_LAST_OPC,
4949 .tos_hs = fld_handlers
4952 .tos_opc_start = SEQ_FIRST_OPC,
4953 .tos_opc_end = SEQ_LAST_OPC,
4954 .tos_hs = seq_handlers
4957 .tos_opc_start = QUOTA_DQACQ,
4958 .tos_opc_end = QUOTA_LAST_OPC,
4959 .tos_hs = mdt_quota_ops
4962 .tos_opc_start = LLOG_FIRST_OPC,
4963 .tos_opc_end = LLOG_LAST_OPC,
4964 .tos_hs = tgt_llog_handlers
4967 .tos_opc_start = LFSCK_FIRST_OPC,
4968 .tos_opc_end = LFSCK_LAST_OPC,
4969 .tos_hs = tgt_lfsck_handlers
4972 .tos_opc_start = OST_FIRST_OPC,
4973 .tos_opc_end = OST_LAST_OPC,
4974 .tos_hs = mdt_io_ops
4981 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4983 struct md_device *next = m->mdt_child;
4984 struct lu_device *d = &m->mdt_lu_dev;
4985 struct obd_device *obd = mdt2obd_dev(m);
4986 struct lfsck_stop stop;
4989 stop.ls_status = LS_PAUSED;
4991 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4993 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4994 ping_evictor_stop();
4996 /* Remove the HSM /proc entry so the coordinator cannot be
4997 * restarted by a user while it's shutting down. */
4998 hsm_cdt_procfs_fini(m);
4999 mdt_hsm_cdt_stop(m);
5001 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
5002 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
5004 if (m->mdt_namespace != NULL)
5005 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
5006 d->ld_obd->obd_force);
5008 obd_exports_barrier(obd);
5009 obd_zombie_barrier();
5011 mdt_quota_fini(env, m);
5013 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
5015 /* Calling the cleanup functions in the same order as in the mdt_init0
5020 target_recovery_fini(obd);
5021 upcall_cache_cleanup(m->mdt_identity_cache);
5022 m->mdt_identity_cache = NULL;
5024 mdt_fs_cleanup(env, m);
5026 tgt_fini(env, &m->mdt_lut);
5028 mdt_hsm_cdt_fini(m);
5030 if (m->mdt_namespace != NULL) {
5031 ldlm_namespace_free_post(m->mdt_namespace);
5032 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
5035 if (m->mdt_md_root != NULL) {
5036 mdt_object_put(env, m->mdt_md_root);
5037 m->mdt_md_root = NULL;
5040 mdt_seq_fini(env, m);
5042 mdt_fld_fini(env, m);
5047 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5049 LASSERT(atomic_read(&d->ld_ref) == 0);
5051 server_put_mount(mdt_obd_name(m), true);
5056 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
5058 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
5059 struct lu_device_type *ldt, struct lustre_cfg *cfg)
5061 const struct dt_device_param *dt_conf;
5062 struct mdt_thread_info *info;
5063 struct obd_device *obd;
5064 const char *dev = lustre_cfg_string(cfg, 0);
5065 const char *num = lustre_cfg_string(cfg, 2);
5066 struct tg_grants_data *tgd = &m->mdt_lut.lut_tgd;
5067 struct lustre_mount_info *lmi = NULL;
5068 struct lustre_sb_info *lsi;
5070 struct seq_server_site *ss_site;
5071 const char *identity_upcall = "NONE";
5072 struct md_device *next;
5078 lu_device_init(&m->mdt_lu_dev, ldt);
5080 * Environment (env) might be missing mdt_thread_key values at that
5081 * point, if device is allocated when mdt_thread_key is in QUIESCENT
5084 * Usually device allocation path doesn't use module key values, but
5085 * mdt has to do a lot of work here, so allocate key value.
5087 rc = lu_env_refill((struct lu_env *)env);
5091 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
5092 LASSERT(info != NULL);
5094 obd = class_name2obd(dev);
5095 LASSERT(obd != NULL);
5097 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
5098 m->mdt_opts.mo_evict_tgt_nids = 1;
5099 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
5101 lmi = server_get_mount(dev);
5103 CERROR("Cannot get mount info for %s!\n", dev);
5106 lsi = s2lsi(lmi->lmi_sb);
5107 /* CMD is supported only in IAM mode */
5109 node_id = simple_strtol(num, NULL, 10);
5110 obd->u.obt.obt_magic = OBT_MAGIC;
5111 if (lsi->lsi_lmd != NULL &&
5112 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
5113 m->mdt_skip_lfsck = 1;
5116 /* DoM files get IO lock at open by default */
5117 m->mdt_opts.mo_dom_lock = 1;
5119 m->mdt_squash.rsi_uid = 0;
5120 m->mdt_squash.rsi_gid = 0;
5121 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
5122 init_rwsem(&m->mdt_squash.rsi_sem);
5123 spin_lock_init(&m->mdt_lock);
5124 m->mdt_enable_remote_dir = 0;
5125 m->mdt_enable_remote_dir_gid = 0;
5127 atomic_set(&m->mdt_mds_mds_conns, 0);
5128 atomic_set(&m->mdt_async_commit_count, 0);
5130 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
5131 m->mdt_lu_dev.ld_obd = obd;
5132 /* Set this lu_device to obd for error handling purposes. */
5133 obd->obd_lu_dev = &m->mdt_lu_dev;
5135 /* init the stack */
5136 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
5138 CERROR("%s: Can't init device stack, rc %d\n",
5139 mdt_obd_name(m), rc);
5144 ss_site = mdt_seq_site(m);
5145 s->ld_seq_site = ss_site;
5148 /* set server index */
5149 ss_site->ss_node_id = node_id;
5151 /* failover is the default
5152 * FIXME: we do not failout mds0/mgs, which may cause some problems.
5153 * assumed whose ss_node_id == 0 XXX
5155 obd->obd_replayable = 1;
5156 /* No connection accepted until configurations will finish */
5157 obd->obd_no_conn = 1;
5159 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
5160 char *str = lustre_cfg_string(cfg, 4);
5161 if (strchr(str, 'n')) {
5162 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
5163 obd->obd_replayable = 0;
5167 rc = mdt_fld_init(env, mdt_obd_name(m), m);
5169 GOTO(err_fini_stack, rc);
5171 rc = mdt_seq_init(env, m);
5173 GOTO(err_fini_fld, rc);
5175 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5176 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5177 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5178 LDLM_NAMESPACE_SERVER,
5179 LDLM_NAMESPACE_GREEDY,
5181 if (m->mdt_namespace == NULL)
5182 GOTO(err_fini_seq, rc = -ENOMEM);
5184 m->mdt_namespace->ns_lvbp = m;
5185 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5187 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5188 /* set obd_namespace for compatibility with old code */
5189 obd->obd_namespace = m->mdt_namespace;
5191 rc = mdt_hsm_cdt_init(m);
5193 CERROR("%s: error initializing coordinator, rc %d\n",
5194 mdt_obd_name(m), rc);
5195 GOTO(err_free_ns, rc);
5198 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5199 OBD_FAIL_MDS_ALL_REQUEST_NET,
5200 OBD_FAIL_MDS_ALL_REPLY_NET);
5202 GOTO(err_free_hsm, rc);
5204 /* Amount of available space excluded from granting and reserved
5205 * for metadata. It is in percentage and 50% is default value. */
5206 tgd->tgd_reserved_pcnt = 50;
5208 if (ONE_MB_BRW_SIZE < (1U << tgd->tgd_blockbits))
5209 m->mdt_brw_size = 1U << tgd->tgd_blockbits;
5211 m->mdt_brw_size = ONE_MB_BRW_SIZE;
5213 rc = mdt_fs_setup(env, m, obd, lsi);
5217 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5219 next = m->mdt_child;
5220 dt_conf = next->md_ops->mdo_dtconf_get(env, next);
5222 mntopts = dt_conf->ddp_mntopts;
5224 if (mntopts & MNTOPT_USERXATTR)
5225 m->mdt_opts.mo_user_xattr = 1;
5227 m->mdt_opts.mo_user_xattr = 0;
5229 m->mdt_max_ea_size = dt_conf->ddp_max_ea_size;
5231 if (mntopts & MNTOPT_ACL)
5232 m->mdt_opts.mo_acl = 1;
5234 m->mdt_opts.mo_acl = 0;
5236 /* XXX: to support suppgid for ACL, we enable identity_upcall
5237 * by default, otherwise, maybe got unexpected -EACCESS. */
5238 if (m->mdt_opts.mo_acl)
5239 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5241 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5243 &mdt_identity_upcall_cache_ops);
5244 if (IS_ERR(m->mdt_identity_cache)) {
5245 rc = PTR_ERR(m->mdt_identity_cache);
5246 m->mdt_identity_cache = NULL;
5247 GOTO(err_fs_cleanup, rc);
5250 rc = mdt_procfs_init(m, dev);
5252 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5253 GOTO(err_recovery, rc);
5256 rc = mdt_quota_init(env, m, cfg);
5258 GOTO(err_procfs, rc);
5260 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5261 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5262 "mdt_ldlm_client", m->mdt_ldlm_client);
5264 ping_evictor_start();
5266 /* recovery will be started upon mdt_prepare()
5267 * when the whole stack is complete and ready
5268 * to serve the requests */
5270 /* Reduce the initial timeout on an MDS because it doesn't need such
5271 * a long timeout as an OST does. Adaptive timeouts will adjust this
5272 * value appropriately. */
5273 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5274 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5280 target_recovery_fini(obd);
5281 upcall_cache_cleanup(m->mdt_identity_cache);
5282 m->mdt_identity_cache = NULL;
5284 mdt_fs_cleanup(env, m);
5286 tgt_fini(env, &m->mdt_lut);
5288 mdt_hsm_cdt_fini(m);
5290 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5291 obd->obd_namespace = m->mdt_namespace = NULL;
5293 mdt_seq_fini(env, m);
5295 mdt_fld_fini(env, m);
5297 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5300 server_put_mount(dev, true);
5304 /* For interoperability, the left element is old parameter, the right one
5305 * is the new version of the parameter, if some parameter is deprecated,
5306 * the new version should be set as NULL. */
5307 static struct cfg_interop_param mdt_interop_param[] = {
5308 { "mdt.group_upcall", NULL },
5309 { "mdt.quota_type", NULL },
5310 { "mdd.quota_type", NULL },
5311 { "mdt.som", NULL },
5312 { "mdt.rootsquash", "mdt.root_squash" },
5313 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5317 /* used by MGS to process specific configurations */
5318 static int mdt_process_config(const struct lu_env *env,
5319 struct lu_device *d, struct lustre_cfg *cfg)
5321 struct mdt_device *m = mdt_dev(d);
5322 struct md_device *md_next = m->mdt_child;
5323 struct lu_device *next = md2lu_dev(md_next);
5327 switch (cfg->lcfg_command) {
5329 struct obd_device *obd = d->ld_obd;
5331 /* For interoperability */
5332 struct cfg_interop_param *ptr = NULL;
5333 struct lustre_cfg *old_cfg = NULL;
5336 param = lustre_cfg_string(cfg, 1);
5337 if (param == NULL) {
5338 CERROR("param is empty\n");
5343 ptr = class_find_old_param(param, mdt_interop_param);
5345 if (ptr->new_param == NULL) {
5347 CWARN("For interoperability, skip this %s."
5348 " It is obsolete.\n", ptr->old_param);
5352 CWARN("Found old param %s, changed it to %s.\n",
5353 ptr->old_param, ptr->new_param);
5356 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5363 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5365 if (rc > 0 || rc == -ENOSYS) {
5366 /* is it an HSM var ? */
5367 rc = class_process_proc_param(PARAM_HSM,
5368 hsm_cdt_get_proc_vars(),
5370 if (rc > 0 || rc == -ENOSYS)
5371 /* we don't understand; pass it on */
5372 rc = next->ld_ops->ldo_process_config(env, next,
5377 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5378 cfg->lcfg_buflens));
5382 /* others are passed further */
5383 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5389 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5390 const struct lu_object_header *hdr,
5391 struct lu_device *d)
5393 struct mdt_object *mo;
5397 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5399 struct lu_object *o;
5400 struct lu_object_header *h;
5403 h = &mo->mot_header;
5404 lu_object_header_init(h);
5405 lu_object_init(o, h, d);
5406 lu_object_add_top(h, o);
5407 o->lo_ops = &mdt_obj_ops;
5408 spin_lock_init(&mo->mot_write_lock);
5409 mutex_init(&mo->mot_lov_mutex);
5410 init_rwsem(&mo->mot_dom_sem);
5411 init_rwsem(&mo->mot_open_sem);
5412 atomic_set(&mo->mot_open_count, 0);
5418 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5419 const struct lu_object_conf *unused)
5421 struct mdt_device *d = mdt_dev(o->lo_dev);
5422 struct lu_device *under;
5423 struct lu_object *below;
5427 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5428 PFID(lu_object_fid(o)));
5430 under = &d->mdt_child->md_lu_dev;
5431 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5432 if (below != NULL) {
5433 lu_object_add(o, below);
5440 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5442 struct mdt_object *mo = mdt_obj(o);
5443 struct lu_object_header *h;
5447 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5448 PFID(lu_object_fid(o)));
5450 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5451 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5454 lu_object_header_fini(h);
5455 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5460 static int mdt_object_print(const struct lu_env *env, void *cookie,
5461 lu_printer_t p, const struct lu_object *o)
5463 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5465 return (*p)(env, cookie,
5466 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5467 mdto, mdto->mot_lov_created ? "lov_created" : "",
5468 mdto->mot_cache_attr ? "cache_attr" : "",
5469 mdto->mot_write_count);
5472 static int mdt_prepare(const struct lu_env *env,
5473 struct lu_device *pdev,
5474 struct lu_device *cdev)
5476 struct mdt_device *mdt = mdt_dev(cdev);
5477 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5478 struct obd_device *obd = cdev->ld_obd;
5485 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5489 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5493 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5497 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5498 /* The LFSCK instance is registered just now, so it must be there when
5499 * register the namespace to such instance. */
5500 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5502 if (mdt->mdt_seq_site.ss_node_id == 0) {
5503 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5504 &mdt->mdt_md_root_fid);
5509 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5511 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5512 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5513 LASSERT(obd->obd_no_conn);
5514 spin_lock(&obd->obd_dev_lock);
5515 obd->obd_no_conn = 0;
5516 spin_unlock(&obd->obd_dev_lock);
5518 if (obd->obd_recovering == 0)
5519 mdt_postrecov(env, mdt);
5524 const struct lu_device_operations mdt_lu_ops = {
5525 .ldo_object_alloc = mdt_object_alloc,
5526 .ldo_process_config = mdt_process_config,
5527 .ldo_prepare = mdt_prepare,
5530 static const struct lu_object_operations mdt_obj_ops = {
5531 .loo_object_init = mdt_object_init,
5532 .loo_object_free = mdt_object_free,
5533 .loo_object_print = mdt_object_print
5536 static int mdt_obd_set_info_async(const struct lu_env *env,
5537 struct obd_export *exp,
5538 __u32 keylen, void *key,
5539 __u32 vallen, void *val,
5540 struct ptlrpc_request_set *set)
5546 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5547 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5555 * Match client and server connection feature flags.
5557 * Compute the compatibility flags for a connection request based on
5558 * features mutually supported by client and server.
5560 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5561 * must not be updated here, otherwise a partially initialized value may
5562 * be exposed. After the connection request is successfully processed,
5563 * the top-level MDT connect request handler atomically updates the export
5564 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5565 * reply. \see mdt_connect().
5567 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5568 * full struct obd_connect_data. So care must be taken when accessing fields
5569 * that are not present in struct obd_connect_data_v1. See LU-16.
5571 * \param exp the obd_export associated with this client/target pair
5572 * \param mdt the target device for the connection
5573 * \param data stores data for this connect request
5576 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5577 * \retval -EBADE client and server feature requirements are incompatible
5579 static int mdt_connect_internal(const struct lu_env *env,
5580 struct obd_export *exp,
5581 struct mdt_device *mdt,
5582 struct obd_connect_data *data, bool reconnect)
5584 LASSERT(data != NULL);
5586 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5588 if (mdt->mdt_bottom->dd_rdonly &&
5589 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5590 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5593 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5594 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5596 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5598 if (!mdt->mdt_opts.mo_acl)
5599 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5601 if (!mdt->mdt_opts.mo_user_xattr)
5602 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5604 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
5605 data->ocd_brw_size = min(data->ocd_brw_size,
5607 if (data->ocd_brw_size == 0) {
5608 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
5609 "ocd_version: %x ocd_grant: %d ocd_index: %u "
5610 "ocd_brw_size unexpectedly zero, network data "
5611 "corruption? Refusing to connect this client\n",
5613 exp->exp_client_uuid.uuid,
5614 exp, data->ocd_connect_flags, data->ocd_version,
5615 data->ocd_grant, data->ocd_index);
5620 if (OCD_HAS_FLAG(data, GRANT_PARAM)) {
5621 struct dt_device_param *ddp = &mdt->mdt_lut.lut_dt_conf;
5623 /* client is reporting its page size, for future use */
5624 exp->exp_target_data.ted_pagebits = data->ocd_grant_blkbits;
5625 data->ocd_grant_blkbits = mdt->mdt_lut.lut_tgd.tgd_blockbits;
5626 /* ddp_inodespace may not be power-of-two value, eg. for ldiskfs
5627 * it's LDISKFS_DIR_REC_LEN(20) = 28. */
5628 data->ocd_grant_inobits = fls(ddp->ddp_inodespace - 1);
5629 /* ocd_grant_tax_kb is in 1K byte blocks */
5630 data->ocd_grant_tax_kb = ddp->ddp_extent_tax >> 10;
5631 data->ocd_grant_max_blks = ddp->ddp_max_extent_blks;
5634 /* Save connect_data we have so far because tgt_grant_connect()
5635 * uses it to calculate grant, and we want to save the client
5636 * version before it is overwritten by LUSTRE_VERSION_CODE. */
5637 exp->exp_connect_data = *data;
5638 if (OCD_HAS_FLAG(data, GRANT))
5639 tgt_grant_connect(env, exp, data, !reconnect);
5641 if (OCD_HAS_FLAG(data, MAXBYTES))
5642 data->ocd_maxbytes = mdt->mdt_lut.lut_dt_conf.ddp_maxbytes;
5644 /* NB: Disregard the rule against updating
5645 * exp_connect_data.ocd_connect_flags in this case, since
5646 * tgt_client_new() needs to know if this is a lightweight
5647 * connection, and it is safe to expose this flag before
5648 * connection processing completes. */
5649 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5650 spin_lock(&exp->exp_lock);
5651 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5652 spin_unlock(&exp->exp_lock);
5655 data->ocd_version = LUSTRE_VERSION_CODE;
5657 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5658 CWARN("%s: MDS requires FID support, but client not\n",
5663 if (OCD_HAS_FLAG(data, PINGLESS)) {
5664 if (ptlrpc_pinger_suppress_pings()) {
5665 spin_lock(&exp->exp_obd->obd_dev_lock);
5666 list_del_init(&exp->exp_obd_chain_timed);
5667 spin_unlock(&exp->exp_obd->obd_dev_lock);
5669 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5673 data->ocd_max_easize = mdt->mdt_max_ea_size;
5675 /* NB: Disregard the rule against updating
5676 * exp_connect_data.ocd_connect_flags in this case, since
5677 * tgt_client_new() needs to know if this is client supports
5678 * multiple modify RPCs, and it is safe to expose this flag before
5679 * connection processing completes. */
5680 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5681 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5682 spin_lock(&exp->exp_lock);
5683 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5684 spin_unlock(&exp->exp_lock);
5687 if (OCD_HAS_FLAG(data, CKSUM)) {
5688 __u32 cksum_types = data->ocd_cksum_types;
5690 /* The client set in ocd_cksum_types the checksum types it
5691 * supports. We have to mask off the algorithms that we don't
5693 data->ocd_cksum_types &= cksum_types_supported_server();
5695 if (unlikely(data->ocd_cksum_types == 0)) {
5696 CERROR("%s: Connect with checksum support but no "
5697 "ocd_cksum_types is set\n",
5698 exp->exp_obd->obd_name);
5702 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
5703 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
5704 cksum_types, data->ocd_cksum_types);
5706 /* This client does not support OBD_CONNECT_CKSUM
5707 * fall back to CRC32 */
5708 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
5709 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
5710 exp->exp_obd->obd_name, obd_export_nid2str(exp));
5716 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5717 struct mdt_thread_info *info,
5718 struct mdt_file_data *mfd)
5720 struct lu_context ses;
5724 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5729 lu_context_enter(&ses);
5731 mdt_ucred(info)->uc_valid = UCRED_OLD;
5732 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5734 lu_context_exit(&ses);
5735 lu_context_fini(&ses);
5741 static int mdt_export_cleanup(struct obd_export *exp)
5743 struct list_head closing_list;
5744 struct mdt_export_data *med = &exp->exp_mdt_data;
5745 struct obd_device *obd = exp->exp_obd;
5746 struct mdt_device *mdt;
5747 struct mdt_thread_info *info;
5749 struct mdt_file_data *mfd, *n;
5753 INIT_LIST_HEAD(&closing_list);
5754 spin_lock(&med->med_open_lock);
5755 while (!list_empty(&med->med_open_head)) {
5756 struct list_head *tmp = med->med_open_head.next;
5757 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5759 /* Remove mfd handle so it can't be found again.
5760 * We are consuming the mfd_list reference here. */
5761 class_handle_unhash(&mfd->mfd_handle);
5762 list_move_tail(&mfd->mfd_list, &closing_list);
5764 spin_unlock(&med->med_open_lock);
5765 mdt = mdt_dev(obd->obd_lu_dev);
5766 LASSERT(mdt != NULL);
5768 rc = lu_env_init(&env, LCT_MD_THREAD);
5772 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5773 LASSERT(info != NULL);
5774 memset(info, 0, sizeof *info);
5775 info->mti_env = &env;
5776 info->mti_mdt = mdt;
5777 info->mti_exp = exp;
5779 if (!list_empty(&closing_list)) {
5780 struct md_attr *ma = &info->mti_attr;
5782 /* Close any open files (which may also cause orphan
5784 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5785 list_del_init(&mfd->mfd_list);
5786 ma->ma_need = ma->ma_valid = 0;
5788 /* This file is being closed due to an eviction, it
5789 * could have been modified and now dirty regarding to
5790 * HSM archive, check this!
5791 * The logic here is to mark a file dirty if there's a
5792 * chance it was dirtied before the client was evicted,
5793 * so that we don't have to wait for a release attempt
5794 * before finding out the file was actually dirty and
5795 * fail the release. Aggressively marking it dirty here
5796 * will cause the policy engine to attempt to
5797 * re-archive it; when rearchiving, we can compare the
5798 * current version to the HSM data_version and make the
5799 * archive request into a noop if it's not actually
5802 if (mfd->mfd_mode & FMODE_WRITE)
5803 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5805 /* Don't unlink orphan on failover umount, LU-184 */
5806 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5807 ma->ma_valid = MA_FLAGS;
5808 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5810 mdt_mfd_close(info, mfd);
5813 info->mti_mdt = NULL;
5814 /* cleanup client slot early */
5815 /* Do not erase record for recoverable client. */
5816 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5817 tgt_client_del(&env, exp);
5823 static inline void mdt_enable_slc(struct mdt_device *mdt)
5825 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5826 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5829 static inline void mdt_disable_slc(struct mdt_device *mdt)
5831 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5832 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5835 static int mdt_obd_disconnect(struct obd_export *exp)
5842 class_export_get(exp);
5844 if (!(exp->exp_flags & OBD_OPT_FORCE))
5845 tgt_grant_sanity_check(exp->exp_obd, __func__);
5847 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5848 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5849 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5851 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5852 mdt_disable_slc(mdt);
5855 rc = server_disconnect_export(exp);
5857 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5859 tgt_grant_discard(exp);
5861 rc = mdt_export_cleanup(exp);
5862 nodemap_del_member(exp);
5863 class_export_put(exp);
5867 /* mds_connect copy */
5868 static int mdt_obd_connect(const struct lu_env *env,
5869 struct obd_export **exp, struct obd_device *obd,
5870 struct obd_uuid *cluuid,
5871 struct obd_connect_data *data,
5874 struct obd_export *lexp;
5875 struct lustre_handle conn = { 0 };
5876 struct mdt_device *mdt;
5878 lnet_nid_t *client_nid = localdata;
5881 LASSERT(env != NULL);
5882 LASSERT(data != NULL);
5884 if (!exp || !obd || !cluuid)
5887 mdt = mdt_dev(obd->obd_lu_dev);
5889 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5890 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5891 atomic_inc(&mdt->mdt_mds_mds_conns);
5892 mdt_enable_slc(mdt);
5896 * first, check whether the stack is ready to handle requests
5897 * XXX: probably not very appropriate method is used now
5898 * at some point we should find a better one
5900 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5901 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5902 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5903 rc = obd_get_info(env, mdt->mdt_child_exp,
5904 sizeof(KEY_OSP_CONNECTED),
5905 KEY_OSP_CONNECTED, NULL, NULL);
5908 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5911 rc = class_connect(&conn, obd, cluuid);
5915 lexp = class_conn2export(&conn);
5916 LASSERT(lexp != NULL);
5918 rc = nodemap_add_member(*client_nid, lexp);
5919 if (rc != 0 && rc != -EEXIST)
5922 rc = mdt_connect_internal(env, lexp, mdt, data, false);
5924 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5927 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5928 rc = tgt_client_new(env, lexp);
5930 mdt_export_stats_init(obd, lexp, localdata);
5934 class_disconnect(lexp);
5935 nodemap_del_member(lexp);
5939 /* Because we do not want this export to be evicted by pinger,
5940 * let's not add this export to the timed chain list. */
5941 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5942 spin_lock(&lexp->exp_obd->obd_dev_lock);
5943 list_del_init(&lexp->exp_obd_chain_timed);
5944 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5951 static int mdt_obd_reconnect(const struct lu_env *env,
5952 struct obd_export *exp, struct obd_device *obd,
5953 struct obd_uuid *cluuid,
5954 struct obd_connect_data *data,
5957 lnet_nid_t *client_nid = localdata;
5961 if (exp == NULL || obd == NULL || cluuid == NULL)
5964 rc = nodemap_add_member(*client_nid, exp);
5965 if (rc != 0 && rc != -EEXIST)
5968 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
5971 mdt_export_stats_init(obd, exp, localdata);
5973 nodemap_del_member(exp);
5978 /* FIXME: Can we avoid using these two interfaces? */
5979 static int mdt_init_export(struct obd_export *exp)
5981 struct mdt_export_data *med = &exp->exp_mdt_data;
5985 INIT_LIST_HEAD(&med->med_open_head);
5986 spin_lock_init(&med->med_open_lock);
5987 spin_lock(&exp->exp_lock);
5988 exp->exp_connecting = 1;
5989 spin_unlock(&exp->exp_lock);
5991 /* self-export doesn't need client data and ldlm initialization */
5992 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5993 &exp->exp_client_uuid)))
5996 rc = tgt_client_alloc(exp);
6000 rc = ldlm_init_export(exp);
6007 tgt_client_free(exp);
6009 CERROR("%s: Failed to initialize export: rc = %d\n",
6010 exp->exp_obd->obd_name, rc);
6014 static int mdt_destroy_export(struct obd_export *exp)
6018 target_destroy_export(exp);
6019 /* destroy can be called from failed obd_setup, so
6020 * checking uuid is safer than obd_self_export */
6021 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
6022 &exp->exp_client_uuid)))
6025 ldlm_destroy_export(exp);
6026 tgt_client_free(exp);
6028 LASSERT(list_empty(&exp->exp_outstanding_replies));
6029 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
6032 * discard grants once we're sure no more
6033 * interaction with the client is possible
6035 tgt_grant_discard(exp);
6036 if (exp_connect_flags(exp) & OBD_CONNECT_GRANT)
6037 exp->exp_obd->u.obt.obt_lut->lut_tgd.tgd_tot_granted_clients--;
6039 if (!(exp->exp_flags & OBD_OPT_FORCE))
6040 tgt_grant_sanity_check(exp->exp_obd, __func__);
6045 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
6046 struct linkea_data *ldata)
6050 LASSERT(ldata->ld_buf->lb_buf != NULL);
6052 if (!mdt_object_exists(mdt_obj))
6055 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6056 ldata->ld_buf, XATTR_NAME_LINK);
6057 if (rc == -ERANGE) {
6058 /* Buf was too small, figure out what we need. */
6059 lu_buf_free(ldata->ld_buf);
6060 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6061 ldata->ld_buf, XATTR_NAME_LINK);
6064 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
6065 if (ldata->ld_buf->lb_buf == NULL)
6067 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6068 ldata->ld_buf, XATTR_NAME_LINK);
6073 return linkea_init_with_rec(ldata);
6077 * Given an MDT object, try to look up the full path to the object.
6078 * Part of the MDT layer implementation of lfs fid2path.
6080 * \param[in] info Per-thread common data shared by MDT level handlers.
6081 * \param[in] obj Object to do path lookup of
6082 * \param[in,out] fp User-provided struct to store path information
6083 * \param[in] root_fid Root FID of current path should reach
6085 * \retval 0 Lookup successful, path information stored in fp
6086 * \retval -EAGAIN Lookup failed, usually because object is being moved
6087 * \retval negative errno if there was a problem
6089 static int mdt_path_current(struct mdt_thread_info *info,
6090 struct mdt_object *obj,
6091 struct getinfo_fid2path *fp,
6092 struct lu_fid *root_fid)
6094 struct mdt_device *mdt = info->mti_mdt;
6095 struct mdt_object *mdt_obj;
6096 struct link_ea_header *leh;
6097 struct link_ea_entry *lee;
6098 struct lu_name *tmpname = &info->mti_name;
6099 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
6100 struct lu_buf *buf = &info->mti_big_buf;
6103 struct linkea_data ldata = { NULL };
6108 /* temp buffer for path element, the buffer will be finally freed
6109 * in mdt_thread_info_fini */
6110 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
6111 if (buf->lb_buf == NULL)
6115 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
6118 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
6120 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
6121 struct lu_buf lmv_buf;
6123 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
6124 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
6125 GOTO(out, rc = -ENOENT);
6127 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
6129 mdt_object_get(info->mti_env, mdt_obj);
6131 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
6132 if (IS_ERR(mdt_obj))
6133 GOTO(out, rc = PTR_ERR(mdt_obj));
6136 if (!mdt_object_exists(mdt_obj)) {
6137 mdt_object_put(info->mti_env, mdt_obj);
6138 GOTO(out, rc = -ENOENT);
6141 if (mdt_object_remote(mdt_obj)) {
6142 mdt_object_put(info->mti_env, mdt_obj);
6143 GOTO(remote_out, rc = -EREMOTE);
6146 rc = mdt_links_read(info, mdt_obj, &ldata);
6148 mdt_object_put(info->mti_env, mdt_obj);
6153 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
6154 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
6155 /* If set, use link #linkno for path lookup, otherwise use
6156 link #0. Only do this for the final path element. */
6157 if (first && fp->gf_linkno < leh->leh_reccount) {
6159 for (count = 0; count < fp->gf_linkno; count++) {
6160 lee = (struct link_ea_entry *)
6161 ((char *)lee + reclen);
6162 linkea_entry_unpack(lee, &reclen, tmpname,
6165 if (fp->gf_linkno < leh->leh_reccount - 1)
6166 /* indicate to user there are more links */
6170 lmv_buf.lb_buf = info->mti_xattr_buf;
6171 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
6172 /* Check if it is slave stripes */
6173 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
6174 &lmv_buf, XATTR_NAME_LMV);
6175 mdt_object_put(info->mti_env, mdt_obj);
6177 union lmv_mds_md *lmm = lmv_buf.lb_buf;
6179 /* For slave stripes, get its master */
6180 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
6181 fp->gf_fid = *tmpfid;
6184 } else if (rc < 0 && rc != -ENODATA) {
6190 /* Pack the name in the end of the buffer */
6191 ptr -= tmpname->ln_namelen;
6192 if (ptr - 1 <= fp->gf_u.gf_path)
6193 GOTO(out, rc = -EOVERFLOW);
6194 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
6197 /* keep the last resolved fid to the client, so the
6198 * client will build the left path on another MDT for
6200 fp->gf_fid = *tmpfid;
6206 ptr++; /* skip leading / */
6207 memmove(fp->gf_u.gf_path, ptr,
6208 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6215 * Given an MDT object, use mdt_path_current to get the path.
6216 * Essentially a wrapper to retry mdt_path_current a set number of times
6217 * if -EAGAIN is returned (usually because an object is being moved).
6219 * Part of the MDT layer implementation of lfs fid2path.
6221 * \param[in] info Per-thread common data shared by mdt level handlers.
6222 * \param[in] obj Object to do path lookup of
6223 * \param[in,out] fp User-provided struct for arguments and to store path
6226 * \retval 0 Lookup successful, path information stored in fp
6227 * \retval negative errno if there was a problem
6229 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6230 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6232 struct mdt_device *mdt = info->mti_mdt;
6237 if (fp->gf_pathlen < 3)
6240 if (root_fid == NULL)
6241 root_fid = &mdt->mdt_md_root_fid;
6243 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6244 fp->gf_u.gf_path[0] = '\0';
6248 /* Retry multiple times in case file is being moved */
6249 while (tries-- && rc == -EAGAIN)
6250 rc = mdt_path_current(info, obj, fp, root_fid);
6256 * Get the full path of the provided FID, as of changelog record recno.
6258 * This checks sanity and looks up object for user provided FID
6259 * before calling the actual path lookup code.
6261 * Part of the MDT layer implementation of lfs fid2path.
6263 * \param[in] info Per-thread common data shared by mdt level handlers.
6264 * \param[in,out] fp User-provided struct for arguments and to store path
6267 * \retval 0 Lookup successful, path information and recno stored in fp
6268 * \retval -ENOENT, object does not exist
6269 * \retval negative errno if there was a problem
6271 static int mdt_fid2path(struct mdt_thread_info *info,
6272 struct lu_fid *root_fid,
6273 struct getinfo_fid2path *fp)
6275 struct mdt_device *mdt = info->mti_mdt;
6276 struct mdt_object *obj;
6280 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6281 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6283 if (!fid_is_sane(&fp->gf_fid))
6286 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6287 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6288 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6289 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6293 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6296 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6297 PFID(&fp->gf_fid), rc);
6301 if (mdt_object_remote(obj))
6303 else if (!mdt_object_exists(obj))
6309 mdt_object_put(info->mti_env, obj);
6310 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6311 PFID(&fp->gf_fid), rc);
6315 rc = mdt_path(info, obj, fp, root_fid);
6317 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6318 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6319 fp->gf_recno, fp->gf_linkno);
6321 mdt_object_put(info->mti_env, obj);
6326 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6327 void *val, int vallen)
6329 struct getinfo_fid2path *fpout, *fpin;
6330 struct lu_fid *root_fid = NULL;
6333 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6336 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6337 lustre_swab_fid2path(fpin);
6339 memcpy(fpout, fpin, sizeof(*fpin));
6340 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6343 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6344 sizeof(struct lu_fid)) {
6345 /* client sent its root FID, which is normally fileset FID */
6346 root_fid = fpin->gf_u.gf_root_fid;
6347 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6348 lustre_swab_lu_fid(root_fid);
6350 if (root_fid != NULL && !fid_is_sane(root_fid))
6354 rc = mdt_fid2path(info, root_fid, fpout);
6358 int mdt_get_info(struct tgt_session_info *tsi)
6368 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6370 CDEBUG(D_IOCTL, "No GETINFO key\n");
6371 RETURN(err_serious(-EFAULT));
6373 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6376 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6377 if (vallen == NULL) {
6378 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6379 tgt_name(tsi->tsi_tgt));
6380 RETURN(err_serious(-EFAULT));
6383 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6385 rc = req_capsule_server_pack(tsi->tsi_pill);
6387 RETURN(err_serious(rc));
6389 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6390 if (valout == NULL) {
6391 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6392 tgt_name(tsi->tsi_tgt));
6393 RETURN(err_serious(-EFAULT));
6396 if (KEY_IS(KEY_FID2PATH)) {
6397 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6399 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6400 mdt_thread_info_fini(info);
6407 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6409 struct obd_ioctl_data *data = karg;
6412 struct mdt_object *obj;
6413 struct mdt_lock_handle *lh;
6417 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6418 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6421 fid = (struct lu_fid *)data->ioc_inlbuf1;
6423 if (!fid_is_sane(fid))
6426 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6428 lh = &mti->mti_lh[MDT_LH_PARENT];
6429 mdt_lock_reg_init(lh, LCK_CR);
6431 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6433 RETURN(PTR_ERR(obj));
6435 if (mdt_object_remote(obj)) {
6438 * before calling version get the correct MDS should be
6439 * fid, this is error to find remote object here
6441 CERROR("nonlocal object "DFID"\n", PFID(fid));
6442 } else if (!mdt_object_exists(obj)) {
6443 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6446 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6447 *(__u64 *)data->ioc_inlbuf2 = version;
6450 mdt_object_unlock_put(mti, obj, lh, 1);
6454 /* ioctls on obd dev */
6455 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6456 void *karg, void __user *uarg)
6459 struct obd_device *obd = exp->exp_obd;
6460 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6461 struct dt_device *dt = mdt->mdt_bottom;
6465 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6466 rc = lu_env_init(&env, LCT_MD_THREAD);
6472 rc = mdt_device_sync(&env, mdt);
6474 case OBD_IOC_SET_READONLY:
6475 rc = dt_sync(&env, dt);
6477 rc = dt_ro(&env, dt);
6479 case OBD_IOC_ABORT_RECOVERY:
6480 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6481 obd->obd_abort_recovery = 1;
6482 target_stop_recovery_thread(obd);
6485 case OBD_IOC_CHANGELOG_REG:
6486 case OBD_IOC_CHANGELOG_DEREG:
6487 case OBD_IOC_CHANGELOG_CLEAR:
6488 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6492 case OBD_IOC_START_LFSCK: {
6493 struct md_device *next = mdt->mdt_child;
6494 struct obd_ioctl_data *data = karg;
6495 struct lfsck_start_param lsp;
6497 if (unlikely(data == NULL)) {
6502 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6503 lsp.lsp_index_valid = 0;
6504 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6507 case OBD_IOC_STOP_LFSCK: {
6508 struct md_device *next = mdt->mdt_child;
6509 struct obd_ioctl_data *data = karg;
6510 struct lfsck_stop stop;
6512 stop.ls_status = LS_STOPPED;
6513 /* Old lfsck utils may pass NULL @stop. */
6514 if (data->ioc_inlbuf1 == NULL)
6518 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6520 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6523 case OBD_IOC_QUERY_LFSCK: {
6524 struct md_device *next = mdt->mdt_child;
6525 struct obd_ioctl_data *data = karg;
6527 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6531 case OBD_IOC_GET_OBJ_VERSION: {
6532 struct mdt_thread_info *mti;
6533 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6534 memset(mti, 0, sizeof *mti);
6535 mti->mti_env = &env;
6539 rc = mdt_ioc_version_get(mti, karg);
6542 case OBD_IOC_CATLOGLIST: {
6543 struct mdt_thread_info *mti;
6545 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6546 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6547 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6548 &mti->mti_tmp_fid1);
6553 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6554 mdt_obd_name(mdt), cmd, rc);
6561 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6563 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6567 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6568 struct lfsck_start_param lsp;
6570 lsp.lsp_start = NULL;
6571 lsp.lsp_index_valid = 0;
6572 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6573 OBD_IOC_START_LFSCK,
6575 if (rc != 0 && rc != -EALREADY)
6576 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6577 mdt_obd_name(mdt), rc);
6580 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6584 static int mdt_obd_postrecov(struct obd_device *obd)
6589 rc = lu_env_init(&env, LCT_MD_THREAD);
6592 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6597 static struct obd_ops mdt_obd_device_ops = {
6598 .o_owner = THIS_MODULE,
6599 .o_set_info_async = mdt_obd_set_info_async,
6600 .o_connect = mdt_obd_connect,
6601 .o_reconnect = mdt_obd_reconnect,
6602 .o_disconnect = mdt_obd_disconnect,
6603 .o_init_export = mdt_init_export,
6604 .o_destroy_export = mdt_destroy_export,
6605 .o_iocontrol = mdt_iocontrol,
6606 .o_postrecov = mdt_obd_postrecov,
6607 /* Data-on-MDT IO methods */
6608 .o_preprw = mdt_obd_preprw,
6609 .o_commitrw = mdt_obd_commitrw,
6612 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6613 struct lu_device *d)
6615 struct mdt_device *m = mdt_dev(d);
6622 static struct lu_device *mdt_device_free(const struct lu_env *env,
6623 struct lu_device *d)
6625 struct mdt_device *m = mdt_dev(d);
6628 lu_device_fini(&m->mdt_lu_dev);
6634 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6635 struct lu_device_type *t,
6636 struct lustre_cfg *cfg)
6638 struct lu_device *l;
6639 struct mdt_device *m;
6646 rc = mdt_init0(env, m, t, cfg);
6648 mdt_device_free(env, l);
6653 l = ERR_PTR(-ENOMEM);
6657 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6658 LU_KEY_INIT(mdt, struct mdt_thread_info);
6660 static void mdt_key_fini(const struct lu_context *ctx,
6661 struct lu_context_key *key, void* data)
6663 struct mdt_thread_info *info = data;
6665 if (info->mti_big_lmm) {
6666 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6667 info->mti_big_lmm = NULL;
6668 info->mti_big_lmmsize = 0;
6671 if (info->mti_big_acl) {
6672 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
6673 info->mti_big_acl = NULL;
6674 info->mti_big_aclsize = 0;
6680 /* context key: mdt_thread_key */
6681 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6683 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6685 return lu_ucred(info->mti_env);
6688 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6690 return lu_ucred_check(info->mti_env);
6694 * Enable/disable COS (Commit On Sharing).
6696 * Set/Clear the COS flag in mdt options.
6698 * \param mdt mdt device
6699 * \param val 0 disables COS, other values enable COS
6701 void mdt_enable_cos(struct mdt_device *mdt, int val)
6706 mdt->mdt_opts.mo_cos = !!val;
6707 rc = lu_env_init(&env, LCT_LOCAL);
6708 if (unlikely(rc != 0)) {
6709 CWARN("%s: lu_env initialization failed, cannot "
6710 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6713 mdt_device_sync(&env, mdt);
6718 * Check COS (Commit On Sharing) status.
6720 * Return COS flag status.
6722 * \param mdt mdt device
6724 int mdt_cos_is_enabled(struct mdt_device *mdt)
6726 return mdt->mdt_opts.mo_cos != 0;
6729 static struct lu_device_type_operations mdt_device_type_ops = {
6730 .ldto_device_alloc = mdt_device_alloc,
6731 .ldto_device_free = mdt_device_free,
6732 .ldto_device_fini = mdt_device_fini
6735 static struct lu_device_type mdt_device_type = {
6736 .ldt_tags = LU_DEVICE_MD,
6737 .ldt_name = LUSTRE_MDT_NAME,
6738 .ldt_ops = &mdt_device_type_ops,
6739 .ldt_ctx_tags = LCT_MD_THREAD
6742 static int __init mdt_init(void)
6746 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6747 FID_NOBRACE_LEN + 1);
6748 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6750 rc = lu_kmem_init(mdt_caches);
6754 rc = mds_mod_init();
6758 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6759 LUSTRE_MDT_NAME, &mdt_device_type);
6764 lu_kmem_fini(mdt_caches);
6771 static void __exit mdt_exit(void)
6773 class_unregister_type(LUSTRE_MDT_NAME);
6775 lu_kmem_fini(mdt_caches);
6778 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6779 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6780 MODULE_VERSION(LUSTRE_VERSION_STRING);
6781 MODULE_LICENSE("GPL");
6783 module_init(mdt_init);
6784 module_exit(mdt_exit);