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, 2016, 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_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_net.h>
57 #include <lustre_nodemap.h>
58 #include <lustre_mds.h>
59 #include <lustre_param.h>
60 #include <lustre_quota.h>
61 #include <lustre_swab.h>
63 #include <obd_support.h>
64 #include <lustre_barrier.h>
66 #include <llog_swab.h>
68 #include "mdt_internal.h"
71 static unsigned int max_mod_rpcs_per_client = 8;
72 module_param(max_mod_rpcs_per_client, uint, 0644);
73 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
75 mdl_mode_t mdt_mdl_lock_modes[] = {
76 [LCK_MINMODE] = MDL_MINMODE,
83 [LCK_GROUP] = MDL_GROUP
86 enum ldlm_mode mdt_dlm_lock_modes[] = {
87 [MDL_MINMODE] = LCK_MINMODE,
94 [MDL_GROUP] = LCK_GROUP
97 static struct mdt_device *mdt_dev(struct lu_device *d);
98 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
100 static const struct lu_object_operations mdt_obj_ops;
102 /* Slab for MDT object allocation */
103 static struct kmem_cache *mdt_object_kmem;
105 /* For HSM restore handles */
106 struct kmem_cache *mdt_hsm_cdt_kmem;
108 /* For HSM request handles */
109 struct kmem_cache *mdt_hsm_car_kmem;
111 static struct lu_kmem_descr mdt_caches[] = {
113 .ckd_cache = &mdt_object_kmem,
114 .ckd_name = "mdt_obj",
115 .ckd_size = sizeof(struct mdt_object)
118 .ckd_cache = &mdt_hsm_cdt_kmem,
119 .ckd_name = "mdt_cdt_restore_handle",
120 .ckd_size = sizeof(struct cdt_restore_handle)
123 .ckd_cache = &mdt_hsm_car_kmem,
124 .ckd_name = "mdt_cdt_agent_req",
125 .ckd_size = sizeof(struct cdt_agent_req)
132 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
136 return rep->lock_policy_res1 & op_flag;
139 void mdt_clear_disposition(struct mdt_thread_info *info,
140 struct ldlm_reply *rep, __u64 op_flag)
143 info->mti_opdata &= ~op_flag;
144 tgt_opdata_clear(info->mti_env, op_flag);
147 rep->lock_policy_res1 &= ~op_flag;
150 void mdt_set_disposition(struct mdt_thread_info *info,
151 struct ldlm_reply *rep, __u64 op_flag)
154 info->mti_opdata |= op_flag;
155 tgt_opdata_set(info->mti_env, op_flag);
158 rep->lock_policy_res1 |= op_flag;
161 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
163 lh->mlh_pdo_hash = 0;
164 lh->mlh_reg_mode = lm;
165 lh->mlh_rreg_mode = lm;
166 lh->mlh_type = MDT_REG_LOCK;
169 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
170 const struct lu_name *lname)
172 lh->mlh_reg_mode = lock_mode;
173 lh->mlh_pdo_mode = LCK_MINMODE;
174 lh->mlh_rreg_mode = lock_mode;
175 lh->mlh_type = MDT_PDO_LOCK;
177 if (lu_name_is_valid(lname)) {
178 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
180 /* XXX Workaround for LU-2856
182 * Zero is a valid return value of full_name_hash, but
183 * several users of mlh_pdo_hash assume a non-zero
184 * hash value. We therefore map zero onto an
185 * arbitrary, but consistent value (1) to avoid
186 * problems further down the road. */
187 if (unlikely(lh->mlh_pdo_hash == 0))
188 lh->mlh_pdo_hash = 1;
190 lh->mlh_pdo_hash = 0;
194 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
195 struct mdt_lock_handle *lh)
201 * Any dir access needs couple of locks:
203 * 1) on part of dir we gonna take lookup/modify;
205 * 2) on whole dir to protect it from concurrent splitting and/or to
206 * flush client's cache for readdir().
208 * so, for a given mode and object this routine decides what lock mode
209 * to use for lock #2:
211 * 1) if caller's gonna lookup in dir then we need to protect dir from
212 * being splitted only - LCK_CR
214 * 2) if caller's gonna modify dir then we need to protect dir from
215 * being splitted and to flush cache - LCK_CW
217 * 3) if caller's gonna modify dir and that dir seems ready for
218 * splitting then we need to protect it from any type of access
219 * (lookup/modify/split) - LCK_EX --bzzz
222 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
223 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
226 * Ask underlaying level its opinion about preferable PDO lock mode
227 * having access type passed as regular lock mode:
229 * - MDL_MINMODE means that lower layer does not want to specify lock
232 * - MDL_NL means that no PDO lock should be taken. This is used in some
233 * cases. Say, for non-splittable directories no need to use PDO locks
236 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
237 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
239 if (mode != MDL_MINMODE) {
240 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
243 * Lower layer does not want to specify locking mode. We do it
244 * our selves. No special protection is needed, just flush
245 * client's cache on modification and allow concurrent
248 switch (lh->mlh_reg_mode) {
250 lh->mlh_pdo_mode = LCK_EX;
253 lh->mlh_pdo_mode = LCK_CR;
256 lh->mlh_pdo_mode = LCK_CW;
259 CERROR("Not expected lock type (0x%x)\n",
260 (int)lh->mlh_reg_mode);
265 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
269 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
272 struct mdt_device *mdt = info->mti_mdt;
273 struct lu_name *lname = &info->mti_name;
275 struct mdt_object *parent;
279 LASSERT(!info->mti_cross_ref);
281 OBD_ALLOC(name, NAME_MAX + 1);
284 lname->ln_name = name;
287 * We may want to allow this to mount a completely separate
288 * fileset from the MDT in the future, but keeping it to
289 * ROOT/ only for now avoid potential security issues.
291 *fid = mdt->mdt_md_root_fid;
293 while (rc == 0 && fileset != NULL && *fileset != '\0') {
294 const char *s1 = fileset;
300 while (*s2 != '/' && *s2 != '\0')
308 lname->ln_namelen = s2 - s1;
309 if (lname->ln_namelen > NAME_MAX) {
314 /* reject .. as a path component */
315 if (lname->ln_namelen == 2 &&
316 strncmp(s1, "..", 2) == 0) {
321 strncpy(name, s1, lname->ln_namelen);
322 name[lname->ln_namelen] = '\0';
324 parent = mdt_object_find(info->mti_env, mdt, fid);
325 if (IS_ERR(parent)) {
326 rc = PTR_ERR(parent);
329 /* Only got the fid of this obj by name */
331 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
332 fid, &info->mti_spec);
333 mdt_object_put(info->mti_env, parent);
336 parent = mdt_object_find(info->mti_env, mdt, fid);
338 rc = PTR_ERR(parent);
340 mode = lu_object_attr(&parent->mot_obj);
341 mdt_object_put(info->mti_env, parent);
347 OBD_FREE(name, NAME_MAX + 1);
352 static int mdt_get_root(struct tgt_session_info *tsi)
354 struct mdt_thread_info *info = tsi2mdt_info(tsi);
355 struct mdt_device *mdt = info->mti_mdt;
356 struct mdt_body *repbody;
357 char *fileset = NULL, *buffer = NULL;
359 struct obd_export *exp = info->mti_exp;
360 char *nodemap_fileset;
364 rc = mdt_check_ucred(info);
366 GOTO(out, rc = err_serious(rc));
368 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
369 GOTO(out, rc = err_serious(-ENOMEM));
371 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
372 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
373 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
375 GOTO(out, rc = err_serious(-EFAULT));
378 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
379 if (nodemap_fileset && nodemap_fileset[0]) {
380 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
382 /* consider fileset from client as a sub-fileset
383 * of the nodemap one */
384 OBD_ALLOC(buffer, PATH_MAX + 1);
386 GOTO(out, rc = err_serious(-ENOMEM));
387 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
388 nodemap_fileset, fileset) >= PATH_MAX + 1)
389 GOTO(out, rc = err_serious(-EINVAL));
392 /* enforce fileset as specified in the nodemap */
393 fileset = nodemap_fileset;
398 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
399 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
401 GOTO(out, rc = err_serious(rc));
403 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
405 repbody->mbo_valid |= OBD_MD_FLID;
409 mdt_thread_info_fini(info);
411 OBD_FREE(buffer, PATH_MAX+1);
415 static int mdt_statfs(struct tgt_session_info *tsi)
417 struct ptlrpc_request *req = tgt_ses_req(tsi);
418 struct mdt_thread_info *info = tsi2mdt_info(tsi);
419 struct md_device *next = info->mti_mdt->mdt_child;
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 /** statfs information are cached in the mdt_device */
445 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
446 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
447 /** statfs data is too old, get up-to-date one */
448 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
451 spin_lock(&info->mti_mdt->mdt_lock);
452 info->mti_mdt->mdt_osfs = *osfs;
453 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
454 spin_unlock(&info->mti_mdt->mdt_lock);
456 /** use cached statfs data */
457 spin_lock(&info->mti_mdt->mdt_lock);
458 *osfs = info->mti_mdt->mdt_osfs;
459 spin_unlock(&info->mti_mdt->mdt_lock);
463 mdt_counter_incr(req, LPROC_MDT_STATFS);
465 mdt_thread_info_fini(info);
469 #ifdef CONFIG_FS_POSIX_ACL
471 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
473 * \param info thread info object
474 * \param repbody reply to pack ACLs into
475 * \param o mdt object of file to examine
476 * \param nodemap nodemap of client to reply to
478 * \retval -errno error getting or parsing ACL from disk
480 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
481 struct mdt_object *o, struct lu_nodemap *nodemap)
483 const struct lu_env *env = info->mti_env;
484 struct md_object *next = mdt_object_child(o);
485 struct lu_buf *buf = &info->mti_buf;
486 struct mdt_device *mdt = info->mti_mdt;
491 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
492 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
494 if (buf->lb_len == 0)
498 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
500 if (rc == -ENODATA) {
501 repbody->mbo_aclsize = 0;
502 repbody->mbo_valid |= OBD_MD_FLACL;
504 } else if (rc == -EOPNOTSUPP) {
508 exp_connect_large_acl(info->mti_exp) &&
509 buf->lb_buf != info->mti_big_acl) {
510 if (info->mti_big_acl == NULL) {
511 OBD_ALLOC_LARGE(info->mti_big_acl,
512 mdt->mdt_max_ea_size);
513 if (info->mti_big_acl == NULL) {
514 CERROR("%s: unable to grow "
517 PFID(mdt_object_fid(o)));
521 info->mti_big_aclsize =
522 mdt->mdt_max_ea_size;
525 CDEBUG(D_INODE, "%s: grow the "DFID
526 " ACL buffer to size %d\n",
528 PFID(mdt_object_fid(o)),
529 mdt->mdt_max_ea_size);
531 buf->lb_buf = info->mti_big_acl;
532 buf->lb_len = info->mti_big_aclsize;
537 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
538 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
541 if (buf->lb_buf == info->mti_big_acl)
542 info->mti_big_acl_used = 1;
544 rc = nodemap_map_acl(nodemap, buf->lb_buf,
545 rc, NODEMAP_FS_TO_CLIENT);
546 /* if all ACLs mapped out, rc is still >= 0 */
548 CERROR("%s: nodemap_map_acl unable to parse "DFID
549 " ACL: rc = %d\n", mdt_obd_name(mdt),
550 PFID(mdt_object_fid(o)), rc);
552 repbody->mbo_aclsize = rc;
553 repbody->mbo_valid |= OBD_MD_FLACL;
562 /* XXX Look into layout in MDT layer. */
563 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
565 struct lov_comp_md_v1 *comp_v1;
566 struct lov_mds_md *v1;
569 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
570 comp_v1 = (struct lov_comp_md_v1 *)lmm;
572 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
573 v1 = (struct lov_mds_md *)((char *)comp_v1 +
574 comp_v1->lcm_entries[i].lcme_offset);
575 /* We don't support partial release for now */
576 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
581 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
586 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
587 const struct lu_attr *attr, const struct lu_fid *fid)
589 struct md_attr *ma = &info->mti_attr;
590 struct obd_export *exp = info->mti_exp;
591 struct lu_nodemap *nodemap = NULL;
593 LASSERT(ma->ma_valid & MA_INODE);
595 if (attr->la_valid & LA_ATIME) {
596 b->mbo_atime = attr->la_atime;
597 b->mbo_valid |= OBD_MD_FLATIME;
599 if (attr->la_valid & LA_MTIME) {
600 b->mbo_mtime = attr->la_mtime;
601 b->mbo_valid |= OBD_MD_FLMTIME;
603 if (attr->la_valid & LA_CTIME) {
604 b->mbo_ctime = attr->la_ctime;
605 b->mbo_valid |= OBD_MD_FLCTIME;
607 if (attr->la_valid & LA_FLAGS) {
608 b->mbo_flags = attr->la_flags;
609 b->mbo_valid |= OBD_MD_FLFLAGS;
611 if (attr->la_valid & LA_NLINK) {
612 b->mbo_nlink = attr->la_nlink;
613 b->mbo_valid |= OBD_MD_FLNLINK;
615 if (attr->la_valid & (LA_UID|LA_GID)) {
616 nodemap = nodemap_get_from_exp(exp);
620 if (attr->la_valid & LA_UID) {
621 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
622 NODEMAP_FS_TO_CLIENT,
624 b->mbo_valid |= OBD_MD_FLUID;
626 if (attr->la_valid & LA_GID) {
627 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
628 NODEMAP_FS_TO_CLIENT,
630 b->mbo_valid |= OBD_MD_FLGID;
633 if (attr->la_valid & LA_PROJID) {
634 /* TODO, nodemap for project id */
635 b->mbo_projid = attr->la_projid;
636 b->mbo_valid |= OBD_MD_FLPROJID;
639 b->mbo_mode = attr->la_mode;
640 if (attr->la_valid & LA_MODE)
641 b->mbo_valid |= OBD_MD_FLMODE;
642 if (attr->la_valid & LA_TYPE)
643 b->mbo_valid |= OBD_MD_FLTYPE;
647 b->mbo_valid |= OBD_MD_FLID;
648 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
649 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
652 if (!(attr->la_valid & LA_TYPE))
655 b->mbo_rdev = attr->la_rdev;
656 b->mbo_size = attr->la_size;
657 b->mbo_blocks = attr->la_blocks;
659 if (!S_ISREG(attr->la_mode)) {
660 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
661 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
662 /* means no objects are allocated on osts. */
663 LASSERT(!(ma->ma_valid & MA_LOV));
664 /* just ignore blocks occupied by extend attributes on MDS */
666 /* if no object is allocated on osts, the size on mds is valid.
668 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
669 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
670 mdt_hsm_is_released(ma->ma_lmm)) {
671 /* A released file stores its size on MDS. */
672 /* But return 1 block for released file, unless tools like tar
673 * will consider it fully sparse. (LU-3864)
675 if (unlikely(b->mbo_size == 0))
679 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
682 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
683 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
684 PFID(fid), (unsigned long long)b->mbo_size);
687 if (!IS_ERR_OR_NULL(nodemap))
688 nodemap_putref(nodemap);
691 static inline int mdt_body_has_lov(const struct lu_attr *la,
692 const struct mdt_body *body)
694 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
695 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
698 void mdt_client_compatibility(struct mdt_thread_info *info)
700 struct mdt_body *body;
701 struct ptlrpc_request *req = mdt_info_req(info);
702 struct obd_export *exp = req->rq_export;
703 struct md_attr *ma = &info->mti_attr;
704 struct lu_attr *la = &ma->ma_attr;
707 if (exp_connect_layout(exp))
708 /* the client can deal with 16-bit lmm_stripe_count */
711 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
713 if (!mdt_body_has_lov(la, body))
716 /* now we have a reply with a lov for a client not compatible with the
717 * layout lock so we have to clean the layout generation number */
718 if (S_ISREG(la->la_mode))
719 ma->ma_lmm->lmm_layout_gen = 0;
723 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
724 struct mdt_object *o)
726 const struct lu_env *env = info->mti_env;
729 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
738 /* Is it a directory? Let's check for the LMV as well */
739 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
740 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
744 rc2 = mo_xattr_get(env, mdt_object_child(o),
746 XATTR_NAME_DEFAULT_LMV);
748 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
756 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
759 const struct lu_env *env = info->mti_env;
763 LASSERT(info->mti_big_lmm_used == 0);
764 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
768 /* big_lmm may need to be grown */
769 if (info->mti_big_lmmsize < rc) {
770 int size = size_roundup_power2(rc);
772 if (info->mti_big_lmmsize > 0) {
773 /* free old buffer */
774 LASSERT(info->mti_big_lmm);
775 OBD_FREE_LARGE(info->mti_big_lmm,
776 info->mti_big_lmmsize);
777 info->mti_big_lmm = NULL;
778 info->mti_big_lmmsize = 0;
781 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
782 if (info->mti_big_lmm == NULL)
784 info->mti_big_lmmsize = size;
786 LASSERT(info->mti_big_lmmsize >= rc);
788 info->mti_buf.lb_buf = info->mti_big_lmm;
789 info->mti_buf.lb_len = info->mti_big_lmmsize;
790 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
795 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
796 struct md_attr *ma, const char *name)
798 struct md_object *next = mdt_object_child(o);
799 struct lu_buf *buf = &info->mti_buf;
802 if (strcmp(name, XATTR_NAME_LOV) == 0) {
803 buf->lb_buf = ma->ma_lmm;
804 buf->lb_len = ma->ma_lmm_size;
805 LASSERT(!(ma->ma_valid & MA_LOV));
806 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
807 buf->lb_buf = ma->ma_lmv;
808 buf->lb_len = ma->ma_lmv_size;
809 LASSERT(!(ma->ma_valid & MA_LMV));
810 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
811 buf->lb_buf = ma->ma_lmv;
812 buf->lb_len = ma->ma_lmv_size;
813 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
818 rc = mo_xattr_get(info->mti_env, next, buf, name);
822 if (strcmp(name, XATTR_NAME_LOV) == 0) {
823 if (info->mti_big_lmm_used)
824 ma->ma_lmm = info->mti_big_lmm;
826 /* NOT return LOV EA with hole to old client. */
827 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
828 LOV_PATTERN_F_HOLE) &&
829 !(exp_connect_flags(info->mti_exp) &
830 OBD_CONNECT_LFSCK)) {
833 ma->ma_lmm_size = rc;
834 ma->ma_valid |= MA_LOV;
836 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
837 if (info->mti_big_lmm_used)
838 ma->ma_lmv = info->mti_big_lmm;
840 ma->ma_lmv_size = rc;
841 ma->ma_valid |= MA_LMV;
842 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
843 ma->ma_lmv_size = rc;
844 ma->ma_valid |= MA_LMV_DEF;
847 /* Update mdt_max_mdsize so all clients will be aware that */
848 if (info->mti_mdt->mdt_max_mdsize < rc)
849 info->mti_mdt->mdt_max_mdsize = rc;
852 } else if (rc == -ENODATA) {
855 } else if (rc == -ERANGE) {
856 /* Default LMV has fixed size, so it must be able to fit
857 * in the original buffer */
858 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
860 rc = mdt_big_xattr_get(info, o, name);
862 info->mti_big_lmm_used = 1;
870 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
871 struct mdt_object *o, struct lu_fid *pfid)
873 struct lu_buf *buf = &info->mti_buf;
874 struct link_ea_header *leh;
875 struct link_ea_entry *lee;
879 buf->lb_buf = info->mti_big_lmm;
880 buf->lb_len = info->mti_big_lmmsize;
881 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
882 buf, XATTR_NAME_LINK);
883 /* ignore errors, MA_PFID won't be set and it is
884 * up to the caller to treat this as an error */
885 if (rc == -ERANGE || buf->lb_len == 0) {
886 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
887 buf->lb_buf = info->mti_big_lmm;
888 buf->lb_len = info->mti_big_lmmsize;
893 if (rc < sizeof(*leh)) {
894 CERROR("short LinkEA on "DFID": rc = %d\n",
895 PFID(mdt_object_fid(o)), rc);
899 leh = (struct link_ea_header *) buf->lb_buf;
900 lee = (struct link_ea_entry *)(leh + 1);
901 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
902 leh->leh_magic = LINK_EA_MAGIC;
903 leh->leh_reccount = __swab32(leh->leh_reccount);
904 leh->leh_len = __swab64(leh->leh_len);
906 if (leh->leh_magic != LINK_EA_MAGIC)
908 if (leh->leh_reccount == 0)
911 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
912 fid_be_to_cpu(pfid, pfid);
917 int mdt_attr_get_complex(struct mdt_thread_info *info,
918 struct mdt_object *o, struct md_attr *ma)
920 const struct lu_env *env = info->mti_env;
921 struct md_object *next = mdt_object_child(o);
922 struct lu_buf *buf = &info->mti_buf;
923 int need = ma->ma_need;
930 if (mdt_object_exists(o) == 0)
931 GOTO(out, rc = -ENOENT);
932 mode = lu_object_attr(&next->mo_lu);
934 if (need & MA_INODE) {
935 ma->ma_need = MA_INODE;
936 rc = mo_attr_get(env, next, ma);
939 ma->ma_valid |= MA_INODE;
942 if (need & MA_PFID) {
943 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
945 ma->ma_valid |= MA_PFID;
946 /* ignore this error, parent fid is not mandatory */
950 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
951 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
956 if (need & MA_LMV && S_ISDIR(mode)) {
957 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
962 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
963 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
968 if (need & MA_HSM && S_ISREG(mode)) {
969 buf->lb_buf = info->mti_xattr_buf;
970 buf->lb_len = sizeof(info->mti_xattr_buf);
971 CLASSERT(sizeof(struct hsm_attrs) <=
972 sizeof(info->mti_xattr_buf));
973 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
974 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
976 ma->ma_valid |= MA_HSM;
977 else if (rc2 < 0 && rc2 != -ENODATA)
981 #ifdef CONFIG_FS_POSIX_ACL
982 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
983 buf->lb_buf = ma->ma_acl;
984 buf->lb_len = ma->ma_acl_size;
985 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
987 ma->ma_acl_size = rc2;
988 ma->ma_valid |= MA_ACL_DEF;
989 } else if (rc2 == -ENODATA) {
998 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
999 rc, ma->ma_valid, ma->ma_lmm);
1003 static int mdt_getattr_internal(struct mdt_thread_info *info,
1004 struct mdt_object *o, int ma_need)
1006 struct md_object *next = mdt_object_child(o);
1007 const struct mdt_body *reqbody = info->mti_body;
1008 struct ptlrpc_request *req = mdt_info_req(info);
1009 struct md_attr *ma = &info->mti_attr;
1010 struct lu_attr *la = &ma->ma_attr;
1011 struct req_capsule *pill = info->mti_pill;
1012 const struct lu_env *env = info->mti_env;
1013 struct mdt_body *repbody;
1014 struct lu_buf *buffer = &info->mti_buf;
1015 struct obd_export *exp = info->mti_exp;
1019 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1020 RETURN(err_serious(-ENOMEM));
1022 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1026 if (mdt_object_remote(o)) {
1027 /* This object is located on remote node.*/
1028 /* Return -ENOTSUPP for old client */
1029 if (!mdt_is_dne_client(req->rq_export))
1030 GOTO(out, rc = -ENOTSUPP);
1032 repbody->mbo_fid1 = *mdt_object_fid(o);
1033 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1037 if (reqbody->mbo_eadatasize > 0) {
1038 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1039 if (buffer->lb_buf == NULL)
1040 GOTO(out, rc = -EPROTO);
1041 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1044 buffer->lb_buf = NULL;
1046 ma_need &= ~(MA_LOV | MA_LMV);
1047 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1048 mdt_obd_name(info->mti_mdt),
1049 req->rq_export->exp_client_uuid.uuid);
1052 /* If it is dir object and client require MEA, then we got MEA */
1053 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1054 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1055 /* Assumption: MDT_MD size is enough for lmv size. */
1056 ma->ma_lmv = buffer->lb_buf;
1057 ma->ma_lmv_size = buffer->lb_len;
1058 ma->ma_need = MA_INODE;
1059 if (ma->ma_lmv_size > 0) {
1060 if (reqbody->mbo_valid & OBD_MD_MEA)
1061 ma->ma_need |= MA_LMV;
1062 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1063 ma->ma_need |= MA_LMV_DEF;
1066 ma->ma_lmm = buffer->lb_buf;
1067 ma->ma_lmm_size = buffer->lb_len;
1068 ma->ma_need = MA_INODE | MA_HSM;
1069 if (ma->ma_lmm_size > 0)
1070 ma->ma_need |= MA_LOV;
1073 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1074 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1075 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1076 /* get default stripe info for this dir. */
1077 ma->ma_need |= MA_LOV_DEF;
1079 ma->ma_need |= ma_need;
1081 rc = mdt_attr_get_complex(info, o, ma);
1083 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1084 "%s: getattr error for "DFID": rc = %d\n",
1085 mdt_obd_name(info->mti_mdt),
1086 PFID(mdt_object_fid(o)), rc);
1090 /* if file is released, check if a restore is running */
1091 if (ma->ma_valid & MA_HSM) {
1092 repbody->mbo_valid |= OBD_MD_TSTATE;
1093 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1094 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1095 repbody->mbo_t_state = MS_RESTORE;
1098 if (likely(ma->ma_valid & MA_INODE))
1099 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1103 if (mdt_body_has_lov(la, reqbody)) {
1104 if (ma->ma_valid & MA_LOV) {
1105 LASSERT(ma->ma_lmm_size);
1106 repbody->mbo_eadatasize = ma->ma_lmm_size;
1107 if (S_ISDIR(la->la_mode))
1108 repbody->mbo_valid |= OBD_MD_FLDIREA;
1110 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1111 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1113 if (ma->ma_valid & MA_LMV) {
1114 /* Return -ENOTSUPP for old client */
1115 if (!mdt_is_striped_client(req->rq_export))
1118 LASSERT(S_ISDIR(la->la_mode));
1119 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1120 repbody->mbo_eadatasize = ma->ma_lmv_size;
1121 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1123 if (ma->ma_valid & MA_LMV_DEF) {
1124 /* Return -ENOTSUPP for old client */
1125 if (!mdt_is_striped_client(req->rq_export))
1127 LASSERT(S_ISDIR(la->la_mode));
1128 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1129 repbody->mbo_eadatasize = ma->ma_lmv_size;
1130 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1131 OBD_MD_DEFAULT_MEA);
1133 } else if (S_ISLNK(la->la_mode) &&
1134 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1135 buffer->lb_buf = ma->ma_lmm;
1136 /* eadatasize from client includes NULL-terminator, so
1137 * there is no need to read it */
1138 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1139 rc = mo_readlink(env, next, buffer);
1140 if (unlikely(rc <= 0)) {
1141 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1142 mdt_obd_name(info->mti_mdt),
1143 PFID(mdt_object_fid(o)), rc);
1146 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1148 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1150 repbody->mbo_valid |= OBD_MD_LINKNAME;
1151 /* we need to report back size with NULL-terminator
1152 * because client expects that */
1153 repbody->mbo_eadatasize = rc + 1;
1154 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1155 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1156 "on "DFID ", expected %d\n",
1157 mdt_obd_name(info->mti_mdt),
1158 rc, PFID(mdt_object_fid(o)),
1159 reqbody->mbo_eadatasize - 1);
1160 /* NULL terminate */
1161 ((char *)ma->ma_lmm)[rc] = 0;
1163 /* If the total CDEBUG() size is larger than a page, it
1164 * will print a warning to the console, avoid this by
1165 * printing just the last part of the symlink. */
1166 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1167 print_limit < rc ? "..." : "", print_limit,
1168 (char *)ma->ma_lmm + rc - print_limit, rc);
1173 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1174 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1175 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1176 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1177 repbody->mbo_max_mdsize);
1180 #ifdef CONFIG_FS_POSIX_ACL
1181 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1182 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1183 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1184 if (IS_ERR(nodemap))
1185 RETURN(PTR_ERR(nodemap));
1187 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1188 nodemap_putref(nodemap);
1194 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1199 static int mdt_getattr(struct tgt_session_info *tsi)
1201 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1202 struct mdt_object *obj = info->mti_object;
1203 struct req_capsule *pill = info->mti_pill;
1204 struct mdt_body *reqbody;
1205 struct mdt_body *repbody;
1209 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1211 LASSERT(obj != NULL);
1212 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1214 /* Unlike intent case where we need to pre-fill out buffers early on
1215 * in intent policy for ldlm reasons, here we can have a much better
1216 * guess at EA size by just reading it from disk.
1217 * Exceptions are readdir and (missing) directory striping */
1219 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1220 /* No easy way to know how long is the symlink, but it cannot
1221 * be more than PATH_MAX, so we allocate +1 */
1224 /* A special case for fs ROOT: getattr there might fetch
1225 * default EA for entire fs, not just for this dir!
1227 } else if (lu_fid_eq(mdt_object_fid(obj),
1228 &info->mti_mdt->mdt_md_root_fid) &&
1229 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1230 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1232 /* Should the default strping be bigger, mdt_fix_reply
1233 * will reallocate */
1234 rc = DEF_REP_MD_SIZE;
1236 /* Read the actual EA size from disk */
1237 rc = mdt_attr_get_eabuf_size(info, obj);
1241 GOTO(out, rc = err_serious(rc));
1243 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1245 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1246 * by default. If the target object has more ACL entries, then
1247 * enlarge the buffer when necessary. */
1248 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1249 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1251 rc = req_capsule_server_pack(pill);
1252 if (unlikely(rc != 0))
1253 GOTO(out, rc = err_serious(rc));
1255 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1256 LASSERT(repbody != NULL);
1257 repbody->mbo_eadatasize = 0;
1258 repbody->mbo_aclsize = 0;
1260 rc = mdt_check_ucred(info);
1262 GOTO(out_shrink, rc);
1264 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1266 rc = mdt_getattr_internal(info, obj, 0);
1269 mdt_client_compatibility(info);
1270 rc2 = mdt_fix_reply(info);
1274 mdt_thread_info_fini(info);
1279 * Handler of layout intent RPC requiring the layout modification
1281 * \param[in] info thread environment
1282 * \param[in] obj object
1283 * \param[in] layout layout intent
1284 * \param[in] buf buffer containing client's lovea, could be empty
1286 * \retval 0 on success
1287 * \retval < 0 error code
1289 static int mdt_layout_change(struct mdt_thread_info *info,
1290 struct mdt_object *obj,
1291 struct layout_intent *layout,
1292 const struct lu_buf *buf)
1294 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1298 CDEBUG(D_INFO, "got layout change request from client: "
1299 "opc:%u flags:%#x extent[%#llx,%#llx)\n",
1300 layout->li_opc, layout->li_flags,
1301 layout->li_start, layout->li_end);
1302 if (layout->li_start >= layout->li_end) {
1303 CERROR("Recieved an invalid layout change range [%llu, %llu) "
1304 "for "DFID"\n", layout->li_start, layout->li_end,
1305 PFID(mdt_object_fid(obj)));
1309 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1310 GOTO(out, rc = -EINVAL);
1312 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1317 /* take layout lock to prepare layout change */
1318 mdt_lock_reg_init(lh, LCK_EX);
1319 rc = mdt_object_lock(info, obj, lh,
1320 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1324 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1327 mdt_object_unlock(info, obj, lh, 1);
1333 * Exchange MOF_LOV_CREATED flags between two objects after a
1334 * layout swap. No assumption is made on whether o1 or o2 have
1335 * created objects or not.
1337 * \param[in,out] o1 First swap layout object
1338 * \param[in,out] o2 Second swap layout object
1340 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1342 unsigned int o1_lov_created = o1->mot_lov_created;
1344 mutex_lock(&o1->mot_lov_mutex);
1345 mutex_lock(&o2->mot_lov_mutex);
1347 o1->mot_lov_created = o2->mot_lov_created;
1348 o2->mot_lov_created = o1_lov_created;
1350 mutex_unlock(&o2->mot_lov_mutex);
1351 mutex_unlock(&o1->mot_lov_mutex);
1354 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1356 struct mdt_thread_info *info;
1357 struct ptlrpc_request *req = tgt_ses_req(tsi);
1358 struct obd_export *exp = req->rq_export;
1359 struct mdt_object *o1, *o2, *o;
1360 struct mdt_lock_handle *lh1, *lh2;
1361 struct mdc_swap_layouts *msl;
1365 /* client does not support layout lock, so layout swaping
1367 * FIXME: there is a problem for old clients which don't support
1368 * layout lock yet. If those clients have already opened the file
1369 * they won't be notified at all so that old layout may still be
1370 * used to do IO. This can be fixed after file release is landed by
1371 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1372 if (!exp_connect_layout(exp))
1373 RETURN(-EOPNOTSUPP);
1375 info = tsi2mdt_info(tsi);
1377 if (info->mti_dlm_req != NULL)
1378 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1380 o1 = info->mti_object;
1381 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1382 &info->mti_body->mbo_fid2);
1384 GOTO(out, rc = PTR_ERR(o));
1386 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1387 GOTO(put, rc = -ENOENT);
1389 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1390 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1396 /* permission check. Make sure the calling process having permission
1397 * to write both files. */
1398 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1403 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1408 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1410 GOTO(put, rc = -EPROTO);
1412 lh1 = &info->mti_lh[MDT_LH_NEW];
1413 mdt_lock_reg_init(lh1, LCK_EX);
1414 lh2 = &info->mti_lh[MDT_LH_OLD];
1415 mdt_lock_reg_init(lh2, LCK_EX);
1417 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1418 MDS_INODELOCK_XATTR);
1422 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1423 MDS_INODELOCK_XATTR);
1427 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1428 mdt_object_child(o2), msl->msl_flags);
1432 mdt_swap_lov_flag(o1, o2);
1435 mdt_object_unlock(info, o2, lh2, rc);
1437 mdt_object_unlock(info, o1, lh1, rc);
1439 mdt_object_put(info->mti_env, o);
1441 mdt_thread_info_fini(info);
1445 static int mdt_raw_lookup(struct mdt_thread_info *info,
1446 struct mdt_object *parent,
1447 const struct lu_name *lname,
1448 struct ldlm_reply *ldlm_rep)
1450 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1454 LASSERT(!info->mti_cross_ref);
1456 /* Only got the fid of this obj by name */
1457 fid_zero(child_fid);
1458 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1459 lname, child_fid, &info->mti_spec);
1461 struct mdt_body *repbody;
1463 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1464 repbody->mbo_fid1 = *child_fid;
1465 repbody->mbo_valid = OBD_MD_FLID;
1466 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1467 } else if (rc == -ENOENT) {
1468 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1475 * UPDATE lock should be taken against parent, and be released before exit;
1476 * child_bits lock should be taken against child, and be returned back:
1477 * (1)normal request should release the child lock;
1478 * (2)intent request will grant the lock to client.
1480 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1481 struct mdt_lock_handle *lhc,
1483 struct ldlm_reply *ldlm_rep)
1485 struct ptlrpc_request *req = mdt_info_req(info);
1486 struct mdt_body *reqbody = NULL;
1487 struct mdt_object *parent = info->mti_object;
1488 struct mdt_object *child;
1489 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1490 struct lu_name *lname = NULL;
1491 struct mdt_lock_handle *lhp = NULL;
1492 struct ldlm_lock *lock;
1499 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1500 LASSERT(ergo(is_resent,
1501 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1506 if (info->mti_cross_ref) {
1507 /* Only getattr on the child. Parent is on another node. */
1508 mdt_set_disposition(info, ldlm_rep,
1509 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1511 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1513 PFID(mdt_object_fid(child)), ldlm_rep);
1515 rc = mdt_check_resent_lock(info, child, lhc);
1518 } else if (rc > 0) {
1519 mdt_lock_handle_init(lhc);
1520 mdt_lock_reg_init(lhc, LCK_PR);
1523 * Object's name is on another MDS, no lookup or layout
1524 * lock is needed here but update lock is.
1526 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1527 MDS_INODELOCK_LAYOUT);
1528 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1530 rc = mdt_object_lock(info, child, lhc, child_bits);
1535 /* Finally, we can get attr for child. */
1536 if (!mdt_object_exists(child)) {
1537 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1539 "remote object doesn't exist.");
1540 mdt_object_unlock(info, child, lhc, 1);
1544 rc = mdt_getattr_internal(info, child, 0);
1545 if (unlikely(rc != 0))
1546 mdt_object_unlock(info, child, lhc, 1);
1551 lname = &info->mti_name;
1552 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1554 if (lu_name_is_valid(lname)) {
1555 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1556 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1557 PNAME(lname), ldlm_rep);
1559 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1560 if (unlikely(reqbody == NULL))
1561 RETURN(err_serious(-EPROTO));
1563 *child_fid = reqbody->mbo_fid2;
1565 if (unlikely(!fid_is_sane(child_fid)))
1566 RETURN(err_serious(-EINVAL));
1568 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1570 PFID(mdt_object_fid(parent)),
1571 PFID(&reqbody->mbo_fid2), ldlm_rep);
1574 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1576 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1577 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1579 "Parent doesn't exist!");
1583 if (mdt_object_remote(parent)) {
1584 CERROR("%s: parent "DFID" is on remote target\n",
1585 mdt_obd_name(info->mti_mdt),
1586 PFID(mdt_object_fid(parent)));
1590 if (lu_name_is_valid(lname)) {
1591 /* Always allow to lookup ".." */
1592 if (unlikely(lname->ln_namelen == 2 &&
1593 lname->ln_name[0] == '.' &&
1594 lname->ln_name[1] == '.'))
1595 info->mti_spec.sp_permitted = 1;
1597 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1598 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1603 /* step 1: lock parent only if parent is a directory */
1604 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1605 lhp = &info->mti_lh[MDT_LH_PARENT];
1606 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1607 rc = mdt_object_lock(info, parent, lhp,
1608 MDS_INODELOCK_UPDATE);
1609 if (unlikely(rc != 0))
1613 /* step 2: lookup child's fid by name */
1614 fid_zero(child_fid);
1615 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1616 child_fid, &info->mti_spec);
1618 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1621 GOTO(out_parent, rc);
1624 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1627 *step 3: find the child object by fid & lock it.
1628 * regardless if it is local or remote.
1630 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1631 * set parent dir fid the same as child fid in getattr by fid case
1632 * we should not lu_object_find() the object again, could lead
1633 * to hung if there is a concurrent unlink destroyed the object.
1635 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1636 mdt_object_get(info->mti_env, parent);
1639 child = mdt_object_find(info->mti_env, info->mti_mdt,
1643 if (unlikely(IS_ERR(child)))
1644 GOTO(out_parent, rc = PTR_ERR(child));
1646 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1647 if (!mdt_object_exists(child)) {
1648 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1650 "Object doesn't exist!");
1651 GOTO(out_child, rc = -ENOENT);
1654 rc = mdt_check_resent_lock(info, child, lhc);
1656 GOTO(out_child, rc);
1657 } else if (rc > 0) {
1658 mdt_lock_handle_init(lhc);
1659 mdt_lock_reg_init(lhc, LCK_PR);
1662 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1663 mdt_object_exists(child) && !mdt_object_remote(child)) {
1664 struct md_attr *ma = &info->mti_attr;
1667 ma->ma_need = MA_INODE;
1668 rc = mdt_attr_get_complex(info, child, ma);
1669 if (unlikely(rc != 0))
1670 GOTO(out_child, rc);
1672 /* If the file has not been changed for some time, we
1673 * return not only a LOOKUP lock, but also an UPDATE
1674 * lock and this might save us RPC on later STAT. For
1675 * directories, it also let negative dentry cache start
1676 * working for this dir. */
1677 if (ma->ma_valid & MA_INODE &&
1678 ma->ma_attr.la_valid & LA_CTIME &&
1679 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1680 ma->ma_attr.la_ctime < cfs_time_current_sec())
1681 child_bits |= MDS_INODELOCK_UPDATE;
1684 /* layout lock must be granted in a best-effort way
1685 * for IT operations */
1686 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1687 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1688 exp_connect_layout(info->mti_exp) &&
1689 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1690 !mdt_object_remote(child) && ldlm_rep != NULL) {
1691 /* try to grant layout lock for regular file. */
1697 child_bits |= MDS_INODELOCK_LAYOUT;
1698 /* try layout lock, it may fail to be granted due to
1699 * contention at LOOKUP or UPDATE */
1700 if (!mdt_object_lock_try(info, child, lhc,
1702 child_bits &= ~MDS_INODELOCK_LAYOUT;
1703 LASSERT(child_bits != 0);
1704 rc = mdt_object_lock(info, child, lhc,
1710 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1711 * client will enqueue the lock to the remote MDT */
1712 if (mdt_object_remote(child))
1713 child_bits &= ~MDS_INODELOCK_UPDATE;
1714 rc = mdt_object_lock(info, child, lhc, child_bits);
1716 if (unlikely(rc != 0))
1717 GOTO(out_child, rc);
1720 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1722 /* finally, we can get attr for child. */
1723 rc = mdt_getattr_internal(info, child, ma_need);
1724 if (unlikely(rc != 0)) {
1725 mdt_object_unlock(info, child, lhc, 1);
1727 /* Debugging code. */
1728 LDLM_DEBUG(lock, "Returning lock to client");
1729 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1730 &lock->l_resource->lr_name),
1731 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1732 PLDLMRES(lock->l_resource),
1733 PFID(mdt_object_fid(child)));
1736 LDLM_LOCK_PUT(lock);
1740 mdt_object_put(info->mti_env, child);
1743 mdt_object_unlock(info, parent, lhp, 1);
1747 /* normal handler: should release the child lock */
1748 static int mdt_getattr_name(struct tgt_session_info *tsi)
1750 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1751 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1752 struct mdt_body *reqbody;
1753 struct mdt_body *repbody;
1757 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1758 LASSERT(reqbody != NULL);
1759 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1760 LASSERT(repbody != NULL);
1762 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1763 repbody->mbo_eadatasize = 0;
1764 repbody->mbo_aclsize = 0;
1766 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1768 GOTO(out_shrink, rc);
1770 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1771 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1772 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1773 lhc->mlh_reg_lh.cookie = 0;
1775 mdt_exit_ucred(info);
1778 mdt_client_compatibility(info);
1779 rc2 = mdt_fix_reply(info);
1782 mdt_thread_info_fini(info);
1786 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1787 void *karg, void __user *uarg);
1789 static int mdt_set_info(struct tgt_session_info *tsi)
1791 struct ptlrpc_request *req = tgt_ses_req(tsi);
1794 int keylen, vallen, rc = 0;
1798 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1800 DEBUG_REQ(D_HA, req, "no set_info key");
1801 RETURN(err_serious(-EFAULT));
1804 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1807 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1809 DEBUG_REQ(D_HA, req, "no set_info val");
1810 RETURN(err_serious(-EFAULT));
1813 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1816 /* Swab any part of val you need to here */
1817 if (KEY_IS(KEY_READ_ONLY)) {
1818 spin_lock(&req->rq_export->exp_lock);
1820 *exp_connect_flags_ptr(req->rq_export) |=
1823 *exp_connect_flags_ptr(req->rq_export) &=
1824 ~OBD_CONNECT_RDONLY;
1825 spin_unlock(&req->rq_export->exp_lock);
1826 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1827 struct changelog_setinfo *cs = val;
1829 if (vallen != sizeof(*cs)) {
1830 CERROR("%s: bad changelog_clear setinfo size %d\n",
1831 tgt_name(tsi->tsi_tgt), vallen);
1834 if (ptlrpc_req_need_swab(req)) {
1835 __swab64s(&cs->cs_recno);
1836 __swab32s(&cs->cs_id);
1839 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1841 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1843 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1850 static int mdt_readpage(struct tgt_session_info *tsi)
1852 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1853 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1854 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1855 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1856 struct mdt_body *repbody;
1862 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1863 RETURN(err_serious(-ENOMEM));
1865 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1866 if (repbody == NULL || reqbody == NULL)
1867 RETURN(err_serious(-EFAULT));
1870 * prepare @rdpg before calling lower layers and transfer itself. Here
1871 * reqbody->size contains offset of where to start to read and
1872 * reqbody->nlink contains number bytes to read.
1874 rdpg->rp_hash = reqbody->mbo_size;
1875 if (rdpg->rp_hash != reqbody->mbo_size) {
1876 CERROR("Invalid hash: %#llx != %#llx\n",
1877 rdpg->rp_hash, reqbody->mbo_size);
1881 rdpg->rp_attrs = reqbody->mbo_mode;
1882 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1883 rdpg->rp_attrs |= LUDA_64BITHASH;
1884 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1885 exp_max_brw_size(tsi->tsi_exp));
1886 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1888 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1889 if (rdpg->rp_pages == NULL)
1892 for (i = 0; i < rdpg->rp_npages; ++i) {
1893 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1894 if (rdpg->rp_pages[i] == NULL)
1895 GOTO(free_rdpg, rc = -ENOMEM);
1898 /* call lower layers to fill allocated pages with directory data */
1899 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1901 GOTO(free_rdpg, rc);
1903 /* send pages to client */
1904 rc = tgt_sendpage(tsi, rdpg, rc);
1909 for (i = 0; i < rdpg->rp_npages; i++)
1910 if (rdpg->rp_pages[i] != NULL)
1911 __free_page(rdpg->rp_pages[i]);
1912 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1914 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1920 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1922 struct lu_ucred *uc = mdt_ucred_check(info);
1923 struct lu_attr *attr = &info->mti_attr.ma_attr;
1928 if (op != REINT_SETATTR) {
1929 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1930 attr->la_uid = uc->uc_fsuid;
1931 /* for S_ISGID, inherit gid from his parent, such work will be
1932 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1933 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1934 attr->la_gid = uc->uc_fsgid;
1940 static int mdt_reint_internal(struct mdt_thread_info *info,
1941 struct mdt_lock_handle *lhc,
1944 struct req_capsule *pill = info->mti_pill;
1945 struct mdt_body *repbody;
1950 rc = mdt_reint_unpack(info, op);
1952 CERROR("Can't unpack reint, rc %d\n", rc);
1953 RETURN(err_serious(rc));
1956 /* for replay (no_create) lmm is not needed, client has it already */
1957 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1958 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1961 /* llog cookies are always 0, the field is kept for compatibility */
1962 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1963 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1965 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1966 * by default. If the target object has more ACL entries, then
1967 * enlarge the buffer when necessary. */
1968 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
1969 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1970 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1972 rc = req_capsule_server_pack(pill);
1974 CERROR("Can't pack response, rc %d\n", rc);
1975 RETURN(err_serious(rc));
1978 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1979 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1981 repbody->mbo_eadatasize = 0;
1982 repbody->mbo_aclsize = 0;
1985 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1987 /* for replay no cookkie / lmm need, because client have this already */
1988 if (info->mti_spec.no_create)
1989 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1990 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1992 rc = mdt_init_ucred_reint(info);
1994 GOTO(out_shrink, rc);
1996 rc = mdt_fix_attr_ucred(info, op);
1998 GOTO(out_ucred, rc = err_serious(rc));
2000 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2002 GOTO(out_ucred, rc);
2003 } else if (rc == 1) {
2004 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
2005 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2006 GOTO(out_ucred, rc);
2008 rc = mdt_reint_rec(info, lhc);
2011 mdt_exit_ucred(info);
2013 mdt_client_compatibility(info);
2014 rc2 = mdt_fix_reply(info);
2020 static long mdt_reint_opcode(struct ptlrpc_request *req,
2021 const struct req_format **fmt)
2023 struct mdt_device *mdt;
2024 struct mdt_rec_reint *rec;
2027 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2029 opc = rec->rr_opcode;
2030 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2031 if (opc < REINT_MAX && fmt[opc] != NULL)
2032 req_capsule_extend(&req->rq_pill, fmt[opc]);
2034 mdt = mdt_exp2dev(req->rq_export);
2035 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2036 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2037 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2038 opc = err_serious(-EFAULT);
2041 opc = err_serious(-EFAULT);
2046 static int mdt_reint(struct tgt_session_info *tsi)
2050 static const struct req_format *reint_fmts[REINT_MAX] = {
2051 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2052 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2053 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2054 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2055 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2056 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2057 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2058 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2059 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
2064 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2066 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2068 * No lock possible here from client to pass it to reint code
2071 rc = mdt_reint_internal(info, NULL, opc);
2072 mdt_thread_info_fini(info);
2077 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2081 /* this should sync the whole device */
2082 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2084 struct dt_device *dt = mdt->mdt_bottom;
2088 rc = dt->dd_ops->dt_sync(env, dt);
2092 /* this should sync this object */
2093 static int mdt_object_sync(struct mdt_thread_info *info)
2095 struct md_object *next;
2099 if (!mdt_object_exists(info->mti_object)) {
2100 CWARN("%s: non existing object "DFID": rc = %d\n",
2101 mdt_obd_name(info->mti_mdt),
2102 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2105 next = mdt_object_child(info->mti_object);
2106 rc = mo_object_sync(info->mti_env, next);
2111 static int mdt_sync(struct tgt_session_info *tsi)
2113 struct ptlrpc_request *req = tgt_ses_req(tsi);
2114 struct req_capsule *pill = tsi->tsi_pill;
2115 struct mdt_body *body;
2120 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2121 RETURN(err_serious(-ENOMEM));
2123 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2124 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2126 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2128 /* sync an object */
2129 rc = mdt_object_sync(info);
2131 const struct lu_fid *fid;
2132 struct lu_attr *la = &info->mti_attr.ma_attr;
2134 info->mti_attr.ma_need = MA_INODE;
2135 info->mti_attr.ma_valid = 0;
2136 rc = mdt_attr_get_complex(info, info->mti_object,
2139 body = req_capsule_server_get(pill,
2141 fid = mdt_object_fid(info->mti_object);
2142 mdt_pack_attr2body(info, body, la, fid);
2145 mdt_thread_info_fini(info);
2148 mdt_counter_incr(req, LPROC_MDT_SYNC);
2154 * Handle quota control requests to consult current usage/limit, but also
2155 * to configure quota enforcement
2157 static int mdt_quotactl(struct tgt_session_info *tsi)
2159 struct obd_export *exp = tsi->tsi_exp;
2160 struct req_capsule *pill = tsi->tsi_pill;
2161 struct obd_quotactl *oqctl, *repoqc;
2163 struct mdt_device *mdt = mdt_exp2dev(exp);
2164 struct lu_device *qmt = mdt->mdt_qmt_dev;
2165 struct lu_nodemap *nodemap;
2168 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2170 RETURN(err_serious(-EPROTO));
2172 rc = req_capsule_server_pack(pill);
2174 RETURN(err_serious(rc));
2176 nodemap = nodemap_get_from_exp(exp);
2177 if (IS_ERR(nodemap))
2178 RETURN(PTR_ERR(nodemap));
2180 switch (oqctl->qc_cmd) {
2181 /* master quotactl */
2184 if (!nodemap_can_setquota(nodemap))
2185 GOTO(out_nodemap, rc = -EPERM);
2189 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2190 /* slave quotactl */
2195 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2196 GOTO(out_nodemap, rc = -EFAULT);
2200 switch (oqctl->qc_type) {
2202 id = nodemap_map_id(nodemap, NODEMAP_UID,
2203 NODEMAP_CLIENT_TO_FS, id);
2206 id = nodemap_map_id(nodemap, NODEMAP_GID,
2207 NODEMAP_CLIENT_TO_FS, id);
2210 /* todo: check/map project id */
2214 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2216 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2218 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2220 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2221 barrier_exit(tsi->tsi_tgt->lut_bottom);
2223 if (oqctl->qc_id != id)
2224 swap(oqctl->qc_id, id);
2226 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2227 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2228 RETURN(-EINPROGRESS);
2231 switch (oqctl->qc_cmd) {
2237 /* forward quotactl request to QMT */
2238 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2243 /* slave quotactl */
2244 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2249 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2250 GOTO(out_nodemap, rc = -EFAULT);
2253 if (oqctl->qc_id != id)
2254 swap(oqctl->qc_id, id);
2261 nodemap_putref(nodemap);
2266 /** clone llog ctxt from child (mdd)
2267 * This allows remote llog (replicator) access.
2268 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2269 * context was originally set up, or we can handle them directly.
2270 * I choose the latter, but that means I need any llog
2271 * contexts set up by child to be accessable by the mdt. So we clone the
2272 * context into our context list here.
2274 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2277 struct md_device *next = mdt->mdt_child;
2278 struct llog_ctxt *ctxt;
2281 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2284 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2285 if (rc || ctxt == NULL) {
2289 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2291 CERROR("Can't set mdt ctxt %d\n", rc);
2296 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2297 struct mdt_device *mdt, int idx)
2299 struct llog_ctxt *ctxt;
2301 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2304 /* Put once for the get we just did, and once for the clone */
2305 llog_ctxt_put(ctxt);
2306 llog_ctxt_put(ctxt);
2311 * sec context handlers
2313 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2315 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2321 * quota request handlers
2323 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2325 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2326 struct lu_device *qmt = mdt->mdt_qmt_dev;
2331 RETURN(err_serious(-EOPNOTSUPP));
2333 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2337 struct mdt_object *mdt_object_new(const struct lu_env *env,
2338 struct mdt_device *d,
2339 const struct lu_fid *f)
2341 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2342 struct lu_object *o;
2343 struct mdt_object *m;
2346 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2347 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2348 if (unlikely(IS_ERR(o)))
2349 m = (struct mdt_object *)o;
2355 struct mdt_object *mdt_object_find(const struct lu_env *env,
2356 struct mdt_device *d,
2357 const struct lu_fid *f)
2359 struct lu_object *o;
2360 struct mdt_object *m;
2363 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2364 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2365 if (unlikely(IS_ERR(o)))
2366 m = (struct mdt_object *)o;
2374 * Asyncronous commit for mdt device.
2376 * Pass asynchonous commit call down the MDS stack.
2378 * \param env environment
2379 * \param mdt the mdt device
2381 static void mdt_device_commit_async(const struct lu_env *env,
2382 struct mdt_device *mdt)
2384 struct dt_device *dt = mdt->mdt_bottom;
2388 rc = dt->dd_ops->dt_commit_async(env, dt);
2389 if (unlikely(rc != 0))
2390 CWARN("%s: async commit start failed: rc = %d\n",
2391 mdt_obd_name(mdt), rc);
2392 atomic_inc(&mdt->mdt_async_commit_count);
2397 * Mark the lock as "synchonous".
2399 * Mark the lock to deffer transaction commit to the unlock time.
2401 * \param lock the lock to mark as "synchonous"
2403 * \see mdt_is_lock_sync
2404 * \see mdt_save_lock
2406 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2408 lock->l_ast_data = (void*)1;
2412 * Check whehter the lock "synchonous" or not.
2414 * \param lock the lock to check
2415 * \retval 1 the lock is "synchonous"
2416 * \retval 0 the lock isn't "synchronous"
2418 * \see mdt_set_lock_sync
2419 * \see mdt_save_lock
2421 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2423 return lock->l_ast_data != NULL;
2427 * Blocking AST for mdt locks.
2429 * Starts transaction commit if in case of COS lock conflict or
2430 * deffers such a commit to the mdt_save_lock.
2432 * \param lock the lock which blocks a request or cancelling lock
2433 * \param desc unused
2434 * \param data unused
2435 * \param flag indicates whether this cancelling or blocking callback
2437 * \see ldlm_blocking_ast_nocheck
2439 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2440 void *data, int flag)
2442 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2443 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2444 bool commit_async = false;
2448 if (flag == LDLM_CB_CANCELING)
2451 lock_res_and_lock(lock);
2452 if (lock->l_blocking_ast != mdt_blocking_ast) {
2453 unlock_res_and_lock(lock);
2456 /* There is no lock conflict if l_blocking_lock == NULL,
2457 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2458 * when the last reference to a local lock was released */
2459 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2460 lock->l_blocking_lock != NULL) {
2461 if (mdt_cos_is_enabled(mdt)) {
2462 if (lock->l_client_cookie !=
2463 lock->l_blocking_lock->l_client_cookie)
2464 mdt_set_lock_sync(lock);
2465 } else if (mdt_slc_is_enabled(mdt) &&
2466 ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2467 mdt_set_lock_sync(lock);
2469 * we may do extra commit here, but there is a small
2470 * window to miss a commit: lock was unlocked (saved),
2471 * then a conflict lock queued and we come here, but
2472 * REP-ACK not received, so lock was not converted to
2474 * Fortunately this window is quite small, so the
2475 * extra commit should be rare (not to say distributed
2476 * operation is rare too).
2478 commit_async = true;
2480 } else if (lock->l_req_mode == LCK_COS &&
2481 lock->l_blocking_lock != NULL) {
2482 commit_async = true;
2485 rc = ldlm_blocking_ast_nocheck(lock);
2490 rc = lu_env_init(&env, LCT_LOCAL);
2491 if (unlikely(rc != 0))
2492 CWARN("%s: lu_env initialization failed, cannot "
2493 "start asynchronous commit: rc = %d\n",
2496 mdt_device_commit_async(&env, mdt);
2503 * Blocking AST for cross-MDT lock
2505 * Discard lock from uncommitted_slc_locks and cancel it.
2507 * \param lock the lock which blocks a request or cancelling lock
2508 * \param desc unused
2509 * \param data unused
2510 * \param flag indicates whether this cancelling or blocking callback
2511 * \retval 0 on success
2512 * \retval negative number on error
2514 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2515 void *data, int flag)
2521 case LDLM_CB_BLOCKING: {
2522 struct lustre_handle lockh;
2524 ldlm_lock2handle(lock, &lockh);
2525 rc = ldlm_cli_cancel(&lockh,
2526 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2528 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2533 case LDLM_CB_CANCELING: {
2534 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2535 struct mdt_device *mdt =
2536 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2538 LDLM_DEBUG(lock, "Revoke remote lock\n");
2540 /* discard slc lock here so that it can be cleaned anytime,
2541 * especially for cleanup_resource() */
2542 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2544 /* once we cache lock, l_ast_data is set to mdt_object */
2545 if (lock->l_ast_data != NULL) {
2546 struct mdt_object *mo = lock->l_ast_data;
2549 rc = lu_env_init(&env, LCT_MD_THREAD);
2550 if (unlikely(rc != 0)) {
2551 CWARN("%s: lu_env initialization failed, object"
2552 "%p "DFID" is leaked!\n",
2554 PFID(mdt_object_fid(mo)));
2558 if (lock->l_policy_data.l_inodebits.bits &
2559 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2560 rc = mo_invalidate(&env, mdt_object_child(mo));
2561 mo->mot_cache_attr = 0;
2563 mdt_object_put(&env, mo);
2575 int mdt_check_resent_lock(struct mdt_thread_info *info,
2576 struct mdt_object *mo,
2577 struct mdt_lock_handle *lhc)
2579 /* the lock might already be gotten in ldlm_handle_enqueue() */
2580 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2581 struct ptlrpc_request *req = mdt_info_req(info);
2582 struct ldlm_lock *lock;
2584 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2585 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2587 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2588 * a resend case, however, it could be already destroyed
2589 * due to client eviction or a raced cancel RPC. */
2590 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2591 lhc->mlh_reg_lh.cookie);
2595 if (!fid_res_name_eq(mdt_object_fid(mo),
2596 &lock->l_resource->lr_name)) {
2597 CWARN("%s: Although resent, but still not "
2598 "get child lock:"DFID"\n",
2599 info->mti_exp->exp_obd->obd_name,
2600 PFID(mdt_object_fid(mo)));
2601 LDLM_LOCK_PUT(lock);
2604 LDLM_LOCK_PUT(lock);
2610 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2611 const struct lu_fid *fid, struct lustre_handle *lh,
2612 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2615 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2616 union ldlm_policy_data *policy = &mti->mti_policy;
2617 struct ldlm_res_id *res_id = &mti->mti_res_id;
2621 LASSERT(mdt_object_remote(o));
2623 fid_build_reg_res_name(fid, res_id);
2625 memset(einfo, 0, sizeof(*einfo));
2626 einfo->ei_type = LDLM_IBITS;
2627 einfo->ei_mode = mode;
2628 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2629 einfo->ei_cb_cp = ldlm_completion_ast;
2630 einfo->ei_enq_slave = 0;
2631 einfo->ei_res_id = res_id;
2633 einfo->ei_nonblock = 1;
2636 * if we cache lock, couple lock with mdt_object, so that object
2637 * can be easily found in lock ASTs.
2639 mdt_object_get(mti->mti_env, o);
2640 einfo->ei_cbdata = o;
2643 memset(policy, 0, sizeof(*policy));
2644 policy->l_inodebits.bits = ibits;
2646 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2648 if (rc < 0 && cache) {
2649 mdt_object_put(mti->mti_env, o);
2650 einfo->ei_cbdata = NULL;
2655 static int mdt_object_local_lock(struct mdt_thread_info *info,
2656 struct mdt_object *o,
2657 struct mdt_lock_handle *lh, __u64 ibits,
2658 bool nonblock, bool cos_incompat)
2660 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2661 union ldlm_policy_data *policy = &info->mti_policy;
2662 struct ldlm_res_id *res_id = &info->mti_res_id;
2667 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2668 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2669 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2670 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2673 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2674 lh->mlh_reg_mode == LCK_EX);
2675 dlmflags |= LDLM_FL_COS_INCOMPAT;
2676 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2677 dlmflags |= LDLM_FL_COS_ENABLED;
2680 /* Only enqueue LOOKUP lock for remote object */
2681 if (mdt_object_remote(o))
2682 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2684 if (lh->mlh_type == MDT_PDO_LOCK) {
2685 /* check for exists after object is locked */
2686 if (mdt_object_exists(o) == 0) {
2687 /* Non-existent object shouldn't have PDO lock */
2690 /* Non-dir object shouldn't have PDO lock */
2691 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2696 memset(policy, 0, sizeof(*policy));
2697 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2699 dlmflags |= LDLM_FL_ATOMIC_CB;
2701 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2704 * Take PDO lock on whole directory and build correct @res_id for lock
2705 * on part of directory.
2707 if (lh->mlh_pdo_hash != 0) {
2708 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2709 mdt_lock_pdo_mode(info, o, lh);
2710 if (lh->mlh_pdo_mode != LCK_NL) {
2712 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2713 * is never going to be sent to client and we do not
2714 * want it slowed down due to possible cancels.
2716 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2717 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2718 policy, res_id, dlmflags,
2719 info->mti_exp == NULL ? NULL :
2720 &info->mti_exp->exp_handle.h_cookie);
2721 if (unlikely(rc != 0))
2722 GOTO(out_unlock, rc);
2726 * Finish res_id initializing by name hash marking part of
2727 * directory which is taking modification.
2729 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2732 policy->l_inodebits.bits = ibits;
2735 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2736 * going to be sent to client. If it is - mdt_intent_policy() path will
2737 * fix it up and turn FL_LOCAL flag off.
2739 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2740 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2741 info->mti_exp == NULL ? NULL :
2742 &info->mti_exp->exp_handle.h_cookie);
2745 mdt_object_unlock(info, o, lh, 1);
2746 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2747 lh->mlh_pdo_hash != 0 &&
2748 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2749 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2755 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2756 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2759 struct mdt_lock_handle *local_lh = NULL;
2763 if (!mdt_object_remote(o)) {
2764 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2769 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2770 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2771 MDS_INODELOCK_XATTR);
2773 /* Only enqueue LOOKUP lock for remote object */
2774 if (ibits & MDS_INODELOCK_LOOKUP) {
2775 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2776 nonblock, cos_incompat);
2783 if (ibits & MDS_INODELOCK_UPDATE) {
2784 /* Sigh, PDO needs to enqueue 2 locks right now, but
2785 * enqueue RPC can only request 1 lock, to avoid extra
2786 * RPC, so it will instead enqueue EX lock for remote
2787 * object anyway XXX*/
2788 if (lh->mlh_type == MDT_PDO_LOCK &&
2789 lh->mlh_pdo_hash != 0) {
2790 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2791 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2792 PFID(mdt_object_fid(o)));
2793 lh->mlh_pdo_hash = 0;
2794 lh->mlh_rreg_mode = LCK_EX;
2795 lh->mlh_type = MDT_REG_LOCK;
2797 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2800 MDS_INODELOCK_UPDATE, nonblock,
2802 if (rc != ELDLM_OK) {
2803 if (local_lh != NULL)
2804 mdt_object_unlock(info, o, local_lh, rc);
2812 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2813 struct mdt_lock_handle *lh, __u64 ibits)
2815 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2818 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2819 struct mdt_lock_handle *lh, __u64 ibits,
2822 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2823 return mdt_object_lock_internal(info, o, lh, ibits, false,
2827 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2828 struct mdt_lock_handle *lh, __u64 ibits)
2830 struct mdt_lock_handle tmp = *lh;
2833 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2840 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2841 struct mdt_object *o, struct mdt_lock_handle *lh,
2842 __u64 ibits, bool cos_incompat)
2844 struct mdt_lock_handle tmp = *lh;
2847 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2848 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2856 * Save a lock within request object.
2858 * Keep the lock referenced until whether client ACK or transaction
2859 * commit happens or release the lock immediately depending on input
2860 * parameters. If COS is ON, a write lock is converted to COS lock
2863 * \param info thead info object
2864 * \param h lock handle
2865 * \param mode lock mode
2866 * \param decref force immediate lock releasing
2868 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2869 enum ldlm_mode mode, int decref)
2873 if (lustre_handle_is_used(h)) {
2874 if (decref || !info->mti_has_trans ||
2875 !(mode & (LCK_PW | LCK_EX))) {
2876 mdt_fid_unlock(h, mode);
2878 struct mdt_device *mdt = info->mti_mdt;
2879 struct ldlm_lock *lock = ldlm_handle2lock(h);
2880 struct ptlrpc_request *req = mdt_info_req(info);
2881 bool cos = mdt_cos_is_enabled(mdt);
2882 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
2884 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2887 /* there is no request if mdt_object_unlock() is called
2888 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2889 if (likely(req != NULL)) {
2890 LDLM_DEBUG(lock, "save lock request %p reply "
2891 "state %p transno %lld\n", req,
2892 req->rq_reply_state, req->rq_transno);
2894 ldlm_lock_downgrade(lock, LCK_COS);
2897 ptlrpc_save_lock(req, h, mode, cos,
2900 mdt_fid_unlock(h, mode);
2902 if (mdt_is_lock_sync(lock)) {
2903 CDEBUG(D_HA, "found sync-lock,"
2904 " async commit started\n");
2905 mdt_device_commit_async(info->mti_env,
2908 LDLM_LOCK_PUT(lock);
2917 * Save cross-MDT lock in uncommitted_slc_locks
2919 * Keep the lock referenced until transaction commit happens or release the lock
2920 * immediately depending on input parameters.
2922 * \param info thead info object
2923 * \param h lock handle
2924 * \param mode lock mode
2925 * \param decref force immediate lock releasing
2927 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2928 struct mdt_object *o, struct lustre_handle *h,
2929 enum ldlm_mode mode, int decref)
2933 if (lustre_handle_is_used(h)) {
2934 struct ldlm_lock *lock = ldlm_handle2lock(h);
2937 (lock->l_policy_data.l_inodebits.bits &
2938 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2939 mo_invalidate(info->mti_env, mdt_object_child(o));
2941 if (decref || !info->mti_has_trans ||
2942 !(mode & (LCK_PW | LCK_EX))) {
2943 ldlm_lock_decref_and_cancel(h, mode);
2944 LDLM_LOCK_PUT(lock);
2946 struct ptlrpc_request *req = mdt_info_req(info);
2948 LASSERT(req != NULL);
2949 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2951 ldlm_lock_decref(h, mode);
2960 * Unlock mdt object.
2962 * Immeditely release the regular lock and the PDO lock or save the
2963 * lock in request and keep them referenced until client ACK or
2964 * transaction commit.
2966 * \param info thread info object
2967 * \param o mdt object
2968 * \param lh mdt lock handle referencing regular and PDO locks
2969 * \param decref force immediate lock releasing
2971 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2973 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2974 struct mdt_lock_handle *lh, int decref)
2978 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2979 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2980 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2986 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2987 const struct lu_fid *f,
2988 struct mdt_lock_handle *lh,
2991 struct mdt_object *o;
2993 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2997 rc = mdt_object_lock(info, o, lh, ibits);
2999 mdt_object_put(info->mti_env, o);
3006 void mdt_object_unlock_put(struct mdt_thread_info * info,
3007 struct mdt_object * o,
3008 struct mdt_lock_handle *lh,
3011 mdt_object_unlock(info, o, lh, decref);
3012 mdt_object_put(info->mti_env, o);
3016 * Generic code handling requests that have struct mdt_body passed in:
3018 * - extract mdt_body from request and save it in @info, if present;
3020 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3023 * - if HABEO_CORPUS flag is set for this request type check whether object
3024 * actually exists on storage (lu_object_exists()).
3027 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3029 const struct mdt_body *body;
3030 struct mdt_object *obj;
3031 const struct lu_env *env;
3032 struct req_capsule *pill;
3036 env = info->mti_env;
3037 pill = info->mti_pill;
3039 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3043 if (!(body->mbo_valid & OBD_MD_FLID))
3046 if (!fid_is_sane(&body->mbo_fid1)) {
3047 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3051 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3053 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3054 mdt_object_put(env, obj);
3057 info->mti_object = obj;
3066 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3068 struct req_capsule *pill = info->mti_pill;
3072 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3073 rc = mdt_body_unpack(info, flags);
3077 if (rc == 0 && (flags & HABEO_REFERO)) {
3079 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3080 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3082 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3083 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3086 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3087 * by default. If the target object has more ACL entries, then
3088 * enlarge the buffer when necessary. */
3089 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3090 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3091 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3093 rc = req_capsule_server_pack(pill);
3098 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3100 lh->mlh_type = MDT_NUL_LOCK;
3101 lh->mlh_reg_lh.cookie = 0ull;
3102 lh->mlh_reg_mode = LCK_MINMODE;
3103 lh->mlh_pdo_lh.cookie = 0ull;
3104 lh->mlh_pdo_mode = LCK_MINMODE;
3105 lh->mlh_rreg_lh.cookie = 0ull;
3106 lh->mlh_rreg_mode = LCK_MINMODE;
3109 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3111 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3112 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3116 * Initialize fields of struct mdt_thread_info. Other fields are left in
3117 * uninitialized state, because it's too expensive to zero out whole
3118 * mdt_thread_info (> 1K) on each request arrival.
3120 void mdt_thread_info_init(struct ptlrpc_request *req,
3121 struct mdt_thread_info *info)
3125 info->mti_pill = &req->rq_pill;
3128 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3129 mdt_lock_handle_init(&info->mti_lh[i]);
3131 /* mdt device: it can be NULL while CONNECT */
3132 if (req->rq_export) {
3133 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3134 info->mti_exp = req->rq_export;
3136 info->mti_mdt = NULL;
3137 info->mti_env = req->rq_svc_thread->t_env;
3138 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3140 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3141 info->mti_big_buf = LU_BUF_NULL;
3142 info->mti_body = NULL;
3143 info->mti_object = NULL;
3144 info->mti_dlm_req = NULL;
3145 info->mti_has_trans = 0;
3146 info->mti_cross_ref = 0;
3147 info->mti_opdata = 0;
3148 info->mti_big_lmm_used = 0;
3149 info->mti_big_acl_used = 0;
3151 info->mti_spec.no_create = 0;
3152 info->mti_spec.sp_rm_entry = 0;
3153 info->mti_spec.sp_permitted = 0;
3154 info->mti_spec.sp_migrate_close = 0;
3156 info->mti_spec.u.sp_ea.eadata = NULL;
3157 info->mti_spec.u.sp_ea.eadatalen = 0;
3160 void mdt_thread_info_fini(struct mdt_thread_info *info)
3164 if (info->mti_object != NULL) {
3165 mdt_object_put(info->mti_env, info->mti_object);
3166 info->mti_object = NULL;
3169 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3170 mdt_lock_handle_fini(&info->mti_lh[i]);
3171 info->mti_env = NULL;
3172 info->mti_pill = NULL;
3173 info->mti_exp = NULL;
3175 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3176 lu_buf_free(&info->mti_big_buf);
3179 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3181 struct mdt_thread_info *mti;
3183 mti = mdt_th_info(tsi->tsi_env);
3184 LASSERT(mti != NULL);
3186 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3187 if (tsi->tsi_corpus != NULL) {
3188 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3189 lu_object_get(tsi->tsi_corpus);
3191 mti->mti_body = tsi->tsi_mdt_body;
3192 mti->mti_dlm_req = tsi->tsi_dlm_req;
3197 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3199 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3201 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3202 set_current_state(TASK_UNINTERRUPTIBLE);
3203 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3206 return tgt_connect(tsi);
3224 static int mdt_intent_getattr(enum mdt_it_code opcode,
3225 struct mdt_thread_info *info,
3226 struct ldlm_lock **,
3229 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3230 struct mdt_thread_info *info,
3231 struct ldlm_lock **lockp,
3234 static int mdt_intent_layout(enum mdt_it_code opcode,
3235 struct mdt_thread_info *info,
3236 struct ldlm_lock **,
3238 static int mdt_intent_reint(enum mdt_it_code opcode,
3239 struct mdt_thread_info *info,
3240 struct ldlm_lock **,
3243 static struct mdt_it_flavor {
3244 const struct req_format *it_fmt;
3246 int (*it_act)(enum mdt_it_code ,
3247 struct mdt_thread_info *,
3248 struct ldlm_lock **,
3251 } mdt_it_flavor[] = {
3253 .it_fmt = &RQF_LDLM_INTENT,
3254 /*.it_flags = HABEO_REFERO,*/
3256 .it_act = mdt_intent_reint,
3257 .it_reint = REINT_OPEN
3260 .it_fmt = &RQF_LDLM_INTENT,
3262 * OCREAT is not a MUTABOR request as if the file
3264 * We do the extra check of OBD_CONNECT_RDONLY in
3265 * mdt_reint_open() when we really need to create
3269 .it_act = mdt_intent_reint,
3270 .it_reint = REINT_OPEN
3273 .it_fmt = &RQF_LDLM_INTENT,
3274 .it_flags = MUTABOR,
3275 .it_act = mdt_intent_reint,
3276 .it_reint = REINT_CREATE
3278 [MDT_IT_GETATTR] = {
3279 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3280 .it_flags = HABEO_REFERO,
3281 .it_act = mdt_intent_getattr
3283 [MDT_IT_READDIR] = {
3289 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3290 .it_flags = HABEO_REFERO,
3291 .it_act = mdt_intent_getattr
3294 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3295 .it_flags = MUTABOR,
3297 .it_reint = REINT_UNLINK
3301 .it_flags = MUTABOR,
3304 [MDT_IT_GETXATTR] = {
3305 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3306 .it_flags = HABEO_CORPUS,
3307 .it_act = mdt_intent_getxattr
3310 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3312 .it_act = mdt_intent_layout
3317 mdt_intent_lock_replace(struct mdt_thread_info *info,
3318 struct ldlm_lock **lockp,
3319 struct mdt_lock_handle *lh,
3320 __u64 flags, int result)
3322 struct ptlrpc_request *req = mdt_info_req(info);
3323 struct ldlm_lock *lock = *lockp;
3324 struct ldlm_lock *new_lock;
3326 /* If possible resent found a lock, @lh is set to its handle */
3327 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3329 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3330 lh->mlh_reg_lh.cookie = 0;
3334 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3335 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3336 * a resend case, however, it could be already destroyed
3337 * due to client eviction or a raced cancel RPC. */
3338 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3339 lh->mlh_reg_lh.cookie);
3340 lh->mlh_reg_lh.cookie = 0;
3344 LASSERTF(new_lock != NULL,
3345 "lockh %#llx flags %#llx : rc = %d\n",
3346 lh->mlh_reg_lh.cookie, flags, result);
3349 * If we've already given this lock to a client once, then we should
3350 * have no readers or writers. Otherwise, we should have one reader
3351 * _or_ writer ref (which will be zeroed below) before returning the
3354 if (new_lock->l_export == req->rq_export) {
3355 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3357 LASSERT(new_lock->l_export == NULL);
3358 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3363 if (new_lock->l_export == req->rq_export) {
3365 * Already gave this to the client, which means that we
3366 * reconstructed a reply.
3368 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3371 LDLM_LOCK_RELEASE(new_lock);
3372 lh->mlh_reg_lh.cookie = 0;
3373 RETURN(ELDLM_LOCK_REPLACED);
3377 * Fixup the lock to be given to the client.
3379 lock_res_and_lock(new_lock);
3380 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3381 * possible blocking AST. */
3382 while (new_lock->l_readers > 0) {
3383 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3384 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3385 new_lock->l_readers--;
3387 while (new_lock->l_writers > 0) {
3388 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3389 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3390 new_lock->l_writers--;
3393 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3394 new_lock->l_blocking_ast = lock->l_blocking_ast;
3395 new_lock->l_completion_ast = lock->l_completion_ast;
3396 new_lock->l_remote_handle = lock->l_remote_handle;
3397 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3399 unlock_res_and_lock(new_lock);
3401 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3402 &new_lock->l_remote_handle,
3403 &new_lock->l_exp_hash);
3405 LDLM_LOCK_RELEASE(new_lock);
3406 lh->mlh_reg_lh.cookie = 0;
3408 RETURN(ELDLM_LOCK_REPLACED);
3411 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3412 struct ldlm_lock *new_lock,
3413 struct mdt_lock_handle *lh,
3416 struct ptlrpc_request *req = mdt_info_req(info);
3417 struct ldlm_request *dlmreq;
3419 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3422 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3424 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3425 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3427 if (flags & LDLM_FL_RESENT) {
3428 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3429 lh->mlh_reg_mode = new_lock->l_granted_mode;
3431 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3432 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3433 lh->mlh_reg_lh.cookie);
3438 * If the xid matches, then we know this is a resent request, and allow
3439 * it. (It's probably an OPEN, for which we don't send a lock.
3441 if (req_can_reconstruct(req, NULL))
3445 * This remote handle isn't enqueued, so we never received or processed
3446 * this request. Clear MSG_RESENT, because it can be handled like any
3447 * normal request now.
3449 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3451 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3452 dlmreq->lock_handle[0].cookie);
3455 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3456 struct mdt_thread_info *info,
3457 struct ldlm_lock **lockp,
3460 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3461 struct ldlm_reply *ldlm_rep = NULL;
3466 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3467 * (for the resend case) or a new lock. Below we will use it to
3468 * replace the original lock.
3470 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3471 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3472 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3473 rc = mdt_object_lock(info, info->mti_object, lhc,
3474 MDS_INODELOCK_XATTR);
3479 rc = mdt_getxattr(info);
3481 if (mdt_info_req(info)->rq_repmsg != NULL)
3482 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3484 if (ldlm_rep == NULL ||
3485 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3486 mdt_object_unlock(info, info->mti_object, lhc, 1);
3487 RETURN(err_serious(-EFAULT));
3490 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3492 /* This is left for interop instead of adding a new interop flag.
3494 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3495 if (ldlm_rep->lock_policy_res2) {
3496 mdt_object_unlock(info, info->mti_object, lhc, 1);
3497 RETURN(ELDLM_LOCK_ABORTED);
3501 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3505 static int mdt_intent_getattr(enum mdt_it_code opcode,
3506 struct mdt_thread_info *info,
3507 struct ldlm_lock **lockp,
3510 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3512 struct ldlm_reply *ldlm_rep;
3513 struct mdt_body *reqbody;
3514 struct mdt_body *repbody;
3518 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3521 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3524 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3525 repbody->mbo_eadatasize = 0;
3526 repbody->mbo_aclsize = 0;
3530 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3532 case MDT_IT_GETATTR:
3533 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3537 CERROR("Unsupported intent (%d)\n", opcode);
3538 GOTO(out_shrink, rc = -EINVAL);
3541 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3543 GOTO(out_shrink, rc);
3545 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3546 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3548 /* Get lock from request for possible resent case. */
3549 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3551 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3552 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3554 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3555 ldlm_rep->lock_policy_res2 = 0;
3556 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3557 ldlm_rep->lock_policy_res2) {
3558 lhc->mlh_reg_lh.cookie = 0ull;
3559 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3562 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3565 mdt_exit_ucred(info);
3567 mdt_client_compatibility(info);
3568 rc2 = mdt_fix_reply(info);
3574 static int mdt_intent_layout(enum mdt_it_code opcode,
3575 struct mdt_thread_info *info,
3576 struct ldlm_lock **lockp,
3579 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3580 struct layout_intent *layout;
3582 struct mdt_object *obj = NULL;
3583 bool layout_change = false;
3584 int layout_size = 0;
3588 if (opcode != MDT_IT_LAYOUT) {
3589 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3594 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3598 switch (layout->li_opc) {
3599 case LAYOUT_INTENT_TRUNC:
3600 case LAYOUT_INTENT_WRITE:
3601 layout_change = true;
3603 case LAYOUT_INTENT_ACCESS:
3605 case LAYOUT_INTENT_READ:
3606 case LAYOUT_INTENT_GLIMPSE:
3607 case LAYOUT_INTENT_RELEASE:
3608 case LAYOUT_INTENT_RESTORE:
3609 CERROR("%s: Unsupported layout intent opc %d\n",
3610 mdt_obd_name(info->mti_mdt), layout->li_opc);
3614 CERROR("%s: Unknown layout intent opc %d\n",
3615 mdt_obd_name(info->mti_mdt), layout->li_opc);
3622 fid = &info->mti_tmp_fid2;
3623 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3625 /* Get lock from request for possible resent case. */
3626 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3628 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3630 GOTO(out, rc = PTR_ERR(obj));
3632 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3633 layout_size = mdt_attr_get_eabuf_size(info, obj);
3634 if (layout_size < 0)
3635 GOTO(out_obj, rc = layout_size);
3637 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3638 info->mti_mdt->mdt_max_mdsize = layout_size;
3642 * set reply buffer size, so that ldlm_handle_enqueue0()->
3643 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3645 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3646 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3648 rc = req_capsule_server_pack(info->mti_pill);
3653 if (layout_change) {
3654 struct lu_buf *buf = &info->mti_buf;
3657 * mdt_layout_change is a reint operation, when the request
3658 * is resent, layout write shouldn't reprocess it again.
3660 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
3662 GOTO(out_obj, rc = rc < 0 ? rc : 0);
3665 * There is another resent case: the client's job has been
3666 * done by another client, referring lod_declare_layout_change
3667 * -EALREADY case, and it became a operation w/o transaction,
3668 * so we should not do the layout change, otherwise
3669 * mdt_layout_change() will try to cancel the granted server
3670 * CR lock whose remote counterpart is still in hold on the
3671 * client, and a deadlock ensues.
3673 rc = mdt_check_resent_lock(info, obj, lhc);
3679 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3680 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3682 buf->lb_len = req_capsule_get_size(info->mti_pill,
3683 &RMF_EADATA, RCL_CLIENT);
3685 * If it's a replay of layout write intent RPC, the
3686 * client has saved the extended lovea when
3687 * it get reply then.
3689 if (buf->lb_len > 0)
3690 mdt_fix_lov_magic(info, buf->lb_buf);
3694 * Instantiate some layout components, if @buf contains
3695 * lovea, then it's a replay of the layout intent write
3698 rc = mdt_layout_change(info, obj, layout, buf);
3703 mdt_object_put(info->mti_env, obj);
3705 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3706 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3709 lhc->mlh_reg_lh.cookie = 0;
3714 static int mdt_intent_reint(enum mdt_it_code opcode,
3715 struct mdt_thread_info *info,
3716 struct ldlm_lock **lockp,
3719 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3720 struct ldlm_reply *rep = NULL;
3724 static const struct req_format *intent_fmts[REINT_MAX] = {
3725 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3726 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3731 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3735 if (mdt_it_flavor[opcode].it_reint != opc) {
3736 CERROR("Reint code %ld doesn't match intent: %d\n",
3738 RETURN(err_serious(-EPROTO));
3741 /* Get lock from request for possible resent case. */
3742 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3744 rc = mdt_reint_internal(info, lhc, opc);
3746 /* Check whether the reply has been packed successfully. */
3747 if (mdt_info_req(info)->rq_repmsg != NULL)
3748 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3750 RETURN(err_serious(-EFAULT));
3752 /* MDC expects this in any case */
3754 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3756 /* the open lock or the lock for cross-ref object should be
3757 * returned to the client */
3758 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3759 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3760 rep->lock_policy_res2 = 0;
3761 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3765 rep->lock_policy_res2 = clear_serious(rc);
3767 if (rep->lock_policy_res2 == -ENOENT &&
3768 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3769 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3770 rep->lock_policy_res2 = 0;
3772 lhc->mlh_reg_lh.cookie = 0ull;
3773 if (rc == -ENOTCONN || rc == -ENODEV ||
3774 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3776 * If it is the disconnect error (ENODEV & ENOCONN), the error
3777 * will be returned by rq_status, and client at ptlrpc layer
3778 * will detect this, then disconnect, reconnect the import
3779 * immediately, instead of impacting the following the rpc.
3784 * For other cases, the error will be returned by intent, and client
3785 * will retrieve the result from intent.
3787 RETURN(ELDLM_LOCK_ABORTED);
3790 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3798 case IT_OPEN|IT_CREAT:
3805 rc = MDT_IT_READDIR;
3808 rc = MDT_IT_GETATTR;
3820 rc = MDT_IT_GETXATTR;
3825 case IT_QUOTA_DQACQ:
3830 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3837 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3838 struct mdt_thread_info *info,
3839 struct ldlm_lock **lockp, __u64 flags)
3841 struct req_capsule *pill = info->mti_pill;
3842 struct ptlrpc_request *req = mdt_info_req(info);
3843 struct mdt_it_flavor *flv;
3848 opc = mdt_intent_code(itopc);
3852 if (opc == MDT_IT_QUOTA) {
3853 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3856 RETURN(-EOPNOTSUPP);
3858 if (mdt_rdonly(req->rq_export))
3861 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3862 /* pass the request to quota master */
3863 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3864 mdt_info_req(info), lockp,
3869 flv = &mdt_it_flavor[opc];
3870 if (flv->it_fmt != NULL)
3871 req_capsule_extend(pill, flv->it_fmt);
3873 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3877 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3880 if (flv->it_act != NULL) {
3881 struct ldlm_reply *rep;
3883 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3885 /* execute policy */
3886 rc = flv->it_act(opc, info, lockp, flags);
3888 /* Check whether the reply has been packed successfully. */
3889 if (req->rq_repmsg != NULL) {
3890 rep = req_capsule_server_get(info->mti_pill,
3892 rep->lock_policy_res2 =
3893 ptlrpc_status_hton(rep->lock_policy_res2);
3900 static int mdt_intent_policy(struct ldlm_namespace *ns,
3901 struct ldlm_lock **lockp, void *req_cookie,
3902 enum ldlm_mode mode, __u64 flags, void *data)
3904 struct tgt_session_info *tsi;
3905 struct mdt_thread_info *info;
3906 struct ptlrpc_request *req = req_cookie;
3907 struct ldlm_intent *it;
3908 struct req_capsule *pill;
3913 LASSERT(req != NULL);
3915 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3917 info = tsi2mdt_info(tsi);
3918 LASSERT(info != NULL);
3919 pill = info->mti_pill;
3920 LASSERT(pill->rc_req == req);
3922 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3923 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3924 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3926 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3930 /* Lock without inodebits makes no sense and will oops
3931 * later in ldlm. Let's check it now to see if we have
3932 * ibits corrupted somewhere in mdt_intent_opc().
3933 * The case for client miss to set ibits has been
3934 * processed by others. */
3935 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3936 lr_type == LDLM_IBITS,
3937 info->mti_dlm_req->lock_desc.\
3938 l_policy_data.l_inodebits.bits != 0));
3940 rc = err_serious(-EFAULT);
3942 /* No intent was provided */
3943 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3944 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3945 rc = req_capsule_server_pack(pill);
3947 rc = err_serious(rc);
3949 mdt_thread_info_fini(info);
3953 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3955 struct seq_server_site *ss = mdt_seq_site(mdt);
3957 if (ss->ss_node_id == 0)
3960 if (ss->ss_client_seq != NULL) {
3961 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3962 ss->ss_client_seq->lcs_exp = NULL;
3965 if (ss->ss_server_fld != NULL) {
3966 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3967 ss->ss_server_fld->lsf_control_exp = NULL;
3971 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3973 struct seq_server_site *ss = mdt_seq_site(mdt);
3978 if (ss->ss_server_seq != NULL)
3979 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3982 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3984 mdt_seq_fini_cli(mdt);
3985 mdt_deregister_seq_exp(mdt);
3987 return seq_site_fini(env, mdt_seq_site(mdt));
3991 * It will retrieve its FLDB entries from MDT0, and it only happens
3992 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3993 * and it needs to refresh FLDB from the MDT0.
3995 static int mdt_register_lwp_callback(void *data)
3998 struct mdt_device *mdt = data;
3999 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4003 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4005 rc = lu_env_init(&env, LCT_MD_THREAD);
4007 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4011 /* Allocate new sequence now to avoid creating local transaction
4012 * in the normal transaction process */
4013 rc = seq_server_check_and_alloc_super(&env,
4014 mdt_seq_site(mdt)->ss_server_seq);
4019 rc = fld_update_from_controller(&env, fld);
4021 CERROR("%s: cannot update controller: rc = %d\n",
4022 mdt_obd_name(mdt), rc);
4031 static int mdt_register_seq_exp(struct mdt_device *mdt)
4033 struct seq_server_site *ss = mdt_seq_site(mdt);
4034 char *lwp_name = NULL;
4037 if (ss->ss_node_id == 0)
4040 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4041 if (lwp_name == NULL)
4042 GOTO(out_free, rc = -ENOMEM);
4044 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4048 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4053 rc = lustre_register_lwp_item(lwp_name,
4054 &ss->ss_server_fld->lsf_control_exp,
4055 mdt_register_lwp_callback, mdt);
4057 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4058 ss->ss_client_seq->lcs_exp = NULL;
4062 if (lwp_name != NULL)
4063 OBD_FREE(lwp_name, MAX_OBD_NAME);
4069 * Init client sequence manager which is used by local MDS to talk to sequence
4070 * controller on remote node.
4072 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4074 struct seq_server_site *ss = mdt_seq_site(mdt);
4079 /* check if this is adding the first MDC and controller is not yet
4081 OBD_ALLOC_PTR(ss->ss_client_seq);
4082 if (ss->ss_client_seq == NULL)
4085 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4086 if (prefix == NULL) {
4087 OBD_FREE_PTR(ss->ss_client_seq);
4088 ss->ss_client_seq = NULL;
4092 /* Note: seq_client_fini will be called in seq_site_fini */
4093 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4094 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4095 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4097 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4099 OBD_FREE_PTR(ss->ss_client_seq);
4100 ss->ss_client_seq = NULL;
4104 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4109 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4111 struct seq_server_site *ss;
4115 ss = mdt_seq_site(mdt);
4116 /* init sequence controller server(MDT0) */
4117 if (ss->ss_node_id == 0) {
4118 OBD_ALLOC_PTR(ss->ss_control_seq);
4119 if (ss->ss_control_seq == NULL)
4122 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4123 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4126 GOTO(out_seq_fini, rc);
4129 /* Init normal sequence server */
4130 OBD_ALLOC_PTR(ss->ss_server_seq);
4131 if (ss->ss_server_seq == NULL)
4132 GOTO(out_seq_fini, rc = -ENOMEM);
4134 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4135 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4137 GOTO(out_seq_fini, rc);
4139 /* init seq client for seq server to talk to seq controller(MDT0) */
4140 rc = mdt_seq_init_cli(env, mdt);
4142 GOTO(out_seq_fini, rc);
4144 if (ss->ss_node_id != 0)
4145 /* register controller export through lwp */
4146 rc = mdt_register_seq_exp(mdt);
4151 mdt_seq_fini(env, mdt);
4159 static int mdt_fld_fini(const struct lu_env *env,
4160 struct mdt_device *m)
4162 struct seq_server_site *ss = mdt_seq_site(m);
4165 if (ss && ss->ss_server_fld) {
4166 fld_server_fini(env, ss->ss_server_fld);
4167 OBD_FREE_PTR(ss->ss_server_fld);
4168 ss->ss_server_fld = NULL;
4174 static int mdt_fld_init(const struct lu_env *env,
4176 struct mdt_device *m)
4178 struct seq_server_site *ss;
4182 ss = mdt_seq_site(m);
4184 OBD_ALLOC_PTR(ss->ss_server_fld);
4185 if (ss->ss_server_fld == NULL)
4186 RETURN(rc = -ENOMEM);
4188 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4191 OBD_FREE_PTR(ss->ss_server_fld);
4192 ss->ss_server_fld = NULL;
4199 static void mdt_stack_pre_fini(const struct lu_env *env,
4200 struct mdt_device *m, struct lu_device *top)
4202 struct lustre_cfg_bufs *bufs;
4203 struct lustre_cfg *lcfg;
4204 struct mdt_thread_info *info;
4209 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4210 LASSERT(info != NULL);
4212 bufs = &info->mti_u.bufs;
4214 LASSERT(m->mdt_child_exp);
4215 LASSERT(m->mdt_child_exp->exp_obd);
4217 /* process cleanup, pass mdt obd name to get obd umount flags */
4218 /* XXX: this is needed because all layers are referenced by
4219 * objects (some of them are pinned by osd, for example *
4220 * the proper solution should be a model where object used
4221 * by osd only doesn't have mdt/mdd slices -bzzz */
4222 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4223 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4224 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4228 top->ld_ops->ldo_process_config(env, top, lcfg);
4229 lustre_cfg_free(lcfg);
4233 static void mdt_stack_fini(const struct lu_env *env,
4234 struct mdt_device *m, struct lu_device *top)
4236 struct obd_device *obd = mdt2obd_dev(m);
4237 struct lustre_cfg_bufs *bufs;
4238 struct lustre_cfg *lcfg;
4239 struct mdt_thread_info *info;
4243 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4244 LASSERT(info != NULL);
4246 lu_dev_del_linkage(top->ld_site, top);
4248 lu_site_purge(env, top->ld_site, -1);
4250 bufs = &info->mti_u.bufs;
4251 /* process cleanup, pass mdt obd name to get obd umount flags */
4252 /* another purpose is to let all layers to release their objects */
4253 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4258 lustre_cfg_bufs_set_string(bufs, 1, flags);
4259 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4264 top->ld_ops->ldo_process_config(env, top, lcfg);
4265 lustre_cfg_free(lcfg);
4267 lu_site_purge(env, top->ld_site, -1);
4269 m->mdt_child = NULL;
4270 m->mdt_bottom = NULL;
4272 obd_disconnect(m->mdt_child_exp);
4273 m->mdt_child_exp = NULL;
4275 obd_disconnect(m->mdt_bottom_exp);
4276 m->mdt_child_exp = NULL;
4279 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4280 const char *next, struct obd_export **exp)
4282 struct obd_connect_data *data = NULL;
4283 struct obd_device *obd;
4287 OBD_ALLOC_PTR(data);
4289 GOTO(out, rc = -ENOMEM);
4291 obd = class_name2obd(next);
4293 CERROR("%s: can't locate next device: %s\n",
4294 mdt_obd_name(m), next);
4295 GOTO(out, rc = -ENOTCONN);
4298 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4299 data->ocd_version = LUSTRE_VERSION_CODE;
4301 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4303 CERROR("%s: cannot connect to next dev %s (%d)\n",
4304 mdt_obd_name(m), next, rc);
4314 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4315 struct lustre_cfg *cfg)
4317 char *dev = lustre_cfg_string(cfg, 0);
4318 int rc, name_size, uuid_size;
4319 char *name, *uuid, *p;
4320 struct lustre_cfg_bufs *bufs;
4321 struct lustre_cfg *lcfg;
4322 struct obd_device *obd;
4323 struct lustre_profile *lprof;
4324 struct lu_site *site;
4327 /* in 1.8 we had the only device in the stack - MDS.
4328 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4329 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4330 * to generate names and setup MDT, MDD. MDT will be using
4331 * generated name to connect to MDD. for MDD the next device
4332 * will be LOD with name taken from so called "profile" which
4333 * is generated by mount_option line
4335 * 1.8 MGS generates config. commands like this:
4336 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4337 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4338 * 2.0 MGS generates config. commands like this:
4339 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4340 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4341 * 3:lustre-MDT0000-mdtlov 4:f
4343 * we generate MDD name from MDT one, just replacing T with D
4345 * after all the preparations, the logical equivalent will be
4346 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4347 * 3:lustre-MDT0000-mdtlov 4:f
4348 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4349 * 3:lustre-MDD0000 4:f
4351 * notice we build the stack from down to top: MDD first, then MDT */
4353 name_size = MAX_OBD_NAME;
4354 uuid_size = MAX_OBD_NAME;
4356 OBD_ALLOC(name, name_size);
4357 OBD_ALLOC(uuid, uuid_size);
4358 if (name == NULL || uuid == NULL)
4359 GOTO(cleanup_mem, rc = -ENOMEM);
4361 OBD_ALLOC_PTR(bufs);
4363 GOTO(cleanup_mem, rc = -ENOMEM);
4366 p = strstr(name, "-MDT");
4368 GOTO(free_bufs, rc = -ENOMEM);
4371 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4373 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4374 if (lprof == NULL || lprof->lp_dt == NULL) {
4375 CERROR("can't find the profile: %s\n",
4376 lustre_cfg_string(cfg, 0));
4377 GOTO(free_bufs, rc = -EINVAL);
4380 lustre_cfg_bufs_reset(bufs, name);
4381 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4382 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4383 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4385 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4387 GOTO(put_profile, rc = -ENOMEM);
4389 rc = class_attach(lcfg);
4391 GOTO(lcfg_cleanup, rc);
4393 obd = class_name2obd(name);
4395 CERROR("Can not find obd %s (%s in config)\n",
4396 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4397 GOTO(lcfg_cleanup, rc = -EINVAL);
4400 lustre_cfg_free(lcfg);
4402 lustre_cfg_bufs_reset(bufs, name);
4403 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4404 lustre_cfg_bufs_set_string(bufs, 2, dev);
4405 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4407 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4409 GOTO(class_detach, rc = -ENOMEM);
4411 rc = class_setup(obd, lcfg);
4413 GOTO(class_detach, rc);
4415 /* connect to MDD we just setup */
4416 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4418 GOTO(class_detach, rc);
4420 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4422 LASSERT(mdt_lu_site(mdt) == NULL);
4423 mdt->mdt_lu_dev.ld_site = site;
4424 site->ls_top_dev = &mdt->mdt_lu_dev;
4425 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4427 /* now connect to bottom OSD */
4428 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4429 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4431 GOTO(class_detach, rc);
4433 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4435 rc = lu_env_refill((struct lu_env *)env);
4437 CERROR("Failure to refill session: '%d'\n", rc);
4439 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4444 class_detach(obd, lcfg);
4446 lustre_cfg_free(lcfg);
4448 class_put_profile(lprof);
4453 OBD_FREE(name, name_size);
4455 OBD_FREE(uuid, uuid_size);
4459 /* setup quota master target on MDT0 */
4460 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4461 struct lustre_cfg *cfg)
4463 struct obd_device *obd;
4464 char *dev = lustre_cfg_string(cfg, 0);
4465 char *qmtname, *uuid, *p;
4466 struct lustre_cfg_bufs *bufs;
4467 struct lustre_cfg *lcfg;
4468 struct lustre_profile *lprof;
4469 struct obd_connect_data *data;
4473 LASSERT(mdt->mdt_qmt_exp == NULL);
4474 LASSERT(mdt->mdt_qmt_dev == NULL);
4476 /* quota master is on MDT0 only for now */
4477 if (mdt->mdt_seq_site.ss_node_id != 0)
4480 /* MGS generates config commands which look as follows:
4481 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4482 * 3:lustre-MDT0000-mdtlov 4:f
4484 * We generate the QMT name from the MDT one, just replacing MD with QM
4485 * after all the preparations, the logical equivalent will be:
4486 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4487 * 3:lustre-MDT0000-osd 4:f */
4488 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4489 OBD_ALLOC(uuid, UUID_MAX);
4490 OBD_ALLOC_PTR(bufs);
4491 OBD_ALLOC_PTR(data);
4492 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4493 GOTO(cleanup_mem, rc = -ENOMEM);
4495 strcpy(qmtname, dev);
4496 p = strstr(qmtname, "-MDT");
4498 GOTO(cleanup_mem, rc = -ENOMEM);
4499 /* replace MD with QM */
4503 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4505 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4506 if (lprof == NULL || lprof->lp_dt == NULL) {
4507 CERROR("can't find profile for %s\n",
4508 lustre_cfg_string(cfg, 0));
4509 GOTO(cleanup_mem, rc = -EINVAL);
4512 lustre_cfg_bufs_reset(bufs, qmtname);
4513 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4514 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4515 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4517 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4519 GOTO(put_profile, rc = -ENOMEM);
4521 rc = class_attach(lcfg);
4523 GOTO(lcfg_cleanup, rc);
4525 obd = class_name2obd(qmtname);
4527 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4528 lustre_cfg_string(cfg, 0));
4529 GOTO(lcfg_cleanup, rc = -EINVAL);
4532 lustre_cfg_free(lcfg);
4534 lustre_cfg_bufs_reset(bufs, qmtname);
4535 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4536 lustre_cfg_bufs_set_string(bufs, 2, dev);
4538 /* for quota, the next device should be the OSD device */
4539 lustre_cfg_bufs_set_string(bufs, 3,
4540 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4542 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4544 GOTO(class_detach, rc = -ENOMEM);
4546 rc = class_setup(obd, lcfg);
4548 GOTO(class_detach, rc);
4550 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4552 /* configure local quota objects */
4553 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4557 GOTO(class_cleanup, rc);
4559 /* connect to quota master target */
4560 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4561 data->ocd_version = LUSTRE_VERSION_CODE;
4562 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4565 CERROR("cannot connect to quota master device %s (%d)\n",
4567 GOTO(class_cleanup, rc);
4573 class_manual_cleanup(obd);
4574 mdt->mdt_qmt_dev = NULL;
4578 class_detach(obd, lcfg);
4580 lustre_cfg_free(lcfg);
4582 class_put_profile(lprof);
4587 OBD_FREE(qmtname, MAX_OBD_NAME);
4589 OBD_FREE(uuid, UUID_MAX);
4595 /* Shutdown quota master target associated with mdt */
4596 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4600 if (mdt->mdt_qmt_exp == NULL)
4602 LASSERT(mdt->mdt_qmt_dev != NULL);
4604 /* the qmt automatically shuts down when the mdt disconnects */
4605 obd_disconnect(mdt->mdt_qmt_exp);
4606 mdt->mdt_qmt_exp = NULL;
4607 mdt->mdt_qmt_dev = NULL;
4611 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4612 * for now. This will be removed along with converting rest of MDT code
4613 * to use tgt_session_info */
4614 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4616 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4619 rc = mdt_getxattr(info);
4621 mdt_thread_info_fini(info);
4625 static struct tgt_handler mdt_tgt_handlers[] = {
4626 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4627 0, MDS_CONNECT, mdt_tgt_connect,
4628 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4629 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4630 0, MDS_DISCONNECT, tgt_disconnect,
4631 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4632 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4633 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4634 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4635 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4636 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4637 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4638 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4640 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4641 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4642 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4643 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4644 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4645 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4646 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4647 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4649 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4650 mdt_hsm_ct_register),
4651 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4652 mdt_hsm_ct_unregister),
4653 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4655 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4657 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4658 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4660 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4665 static struct tgt_handler mdt_sec_ctx_ops[] = {
4666 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4667 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4668 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4671 static struct tgt_handler mdt_quota_ops[] = {
4672 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4675 static struct tgt_opc_slice mdt_common_slice[] = {
4677 .tos_opc_start = MDS_FIRST_OPC,
4678 .tos_opc_end = MDS_LAST_OPC,
4679 .tos_hs = mdt_tgt_handlers
4682 .tos_opc_start = OBD_FIRST_OPC,
4683 .tos_opc_end = OBD_LAST_OPC,
4684 .tos_hs = tgt_obd_handlers
4687 .tos_opc_start = LDLM_FIRST_OPC,
4688 .tos_opc_end = LDLM_LAST_OPC,
4689 .tos_hs = tgt_dlm_handlers
4692 .tos_opc_start = SEC_FIRST_OPC,
4693 .tos_opc_end = SEC_LAST_OPC,
4694 .tos_hs = mdt_sec_ctx_ops
4697 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4698 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4699 .tos_hs = tgt_out_handlers
4702 .tos_opc_start = FLD_FIRST_OPC,
4703 .tos_opc_end = FLD_LAST_OPC,
4704 .tos_hs = fld_handlers
4707 .tos_opc_start = SEQ_FIRST_OPC,
4708 .tos_opc_end = SEQ_LAST_OPC,
4709 .tos_hs = seq_handlers
4712 .tos_opc_start = QUOTA_DQACQ,
4713 .tos_opc_end = QUOTA_LAST_OPC,
4714 .tos_hs = mdt_quota_ops
4717 .tos_opc_start = LLOG_FIRST_OPC,
4718 .tos_opc_end = LLOG_LAST_OPC,
4719 .tos_hs = tgt_llog_handlers
4722 .tos_opc_start = LFSCK_FIRST_OPC,
4723 .tos_opc_end = LFSCK_LAST_OPC,
4724 .tos_hs = tgt_lfsck_handlers
4732 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4734 struct md_device *next = m->mdt_child;
4735 struct lu_device *d = &m->mdt_lu_dev;
4736 struct obd_device *obd = mdt2obd_dev(m);
4737 struct lfsck_stop stop;
4740 stop.ls_status = LS_PAUSED;
4742 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4744 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4745 ping_evictor_stop();
4747 if (m->mdt_opts.mo_coordinator)
4748 mdt_hsm_cdt_stop(m);
4750 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4751 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4753 if (m->mdt_namespace != NULL)
4754 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4755 d->ld_obd->obd_force);
4757 obd_exports_barrier(obd);
4758 obd_zombie_barrier();
4760 mdt_quota_fini(env, m);
4762 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4764 /* Calling the cleanup functions in the same order as in the mdt_init0
4769 target_recovery_fini(obd);
4770 upcall_cache_cleanup(m->mdt_identity_cache);
4771 m->mdt_identity_cache = NULL;
4773 mdt_fs_cleanup(env, m);
4775 tgt_fini(env, &m->mdt_lut);
4777 mdt_hsm_cdt_fini(m);
4779 if (m->mdt_namespace != NULL) {
4780 ldlm_namespace_free_post(m->mdt_namespace);
4781 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4784 if (m->mdt_md_root != NULL) {
4785 mdt_object_put(env, m->mdt_md_root);
4786 m->mdt_md_root = NULL;
4789 mdt_seq_fini(env, m);
4791 mdt_fld_fini(env, m);
4796 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4798 LASSERT(atomic_read(&d->ld_ref) == 0);
4800 server_put_mount(mdt_obd_name(m), true);
4805 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4807 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4808 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4810 struct mdt_thread_info *info;
4811 struct obd_device *obd;
4812 const char *dev = lustre_cfg_string(cfg, 0);
4813 const char *num = lustre_cfg_string(cfg, 2);
4814 struct lustre_mount_info *lmi = NULL;
4815 struct lustre_sb_info *lsi;
4817 struct seq_server_site *ss_site;
4818 const char *identity_upcall = "NONE";
4819 struct md_device *next;
4825 lu_device_init(&m->mdt_lu_dev, ldt);
4827 * Environment (env) might be missing mdt_thread_key values at that
4828 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4831 * Usually device allocation path doesn't use module key values, but
4832 * mdt has to do a lot of work here, so allocate key value.
4834 rc = lu_env_refill((struct lu_env *)env);
4838 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4839 LASSERT(info != NULL);
4841 obd = class_name2obd(dev);
4842 LASSERT(obd != NULL);
4844 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4845 m->mdt_opts.mo_evict_tgt_nids = 1;
4846 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4848 /* default is coordinator off, it is started through conf_param
4850 m->mdt_opts.mo_coordinator = 0;
4852 lmi = server_get_mount(dev);
4854 CERROR("Cannot get mount info for %s!\n", dev);
4857 lsi = s2lsi(lmi->lmi_sb);
4858 /* CMD is supported only in IAM mode */
4860 node_id = simple_strtol(num, NULL, 10);
4861 obd->u.obt.obt_magic = OBT_MAGIC;
4862 if (lsi->lsi_lmd != NULL &&
4863 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4864 m->mdt_skip_lfsck = 1;
4867 m->mdt_squash.rsi_uid = 0;
4868 m->mdt_squash.rsi_gid = 0;
4869 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4870 init_rwsem(&m->mdt_squash.rsi_sem);
4871 spin_lock_init(&m->mdt_lock);
4872 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4873 m->mdt_enable_remote_dir = 0;
4874 m->mdt_enable_remote_dir_gid = 0;
4876 atomic_set(&m->mdt_mds_mds_conns, 0);
4877 atomic_set(&m->mdt_async_commit_count, 0);
4879 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4880 m->mdt_lu_dev.ld_obd = obd;
4881 /* Set this lu_device to obd for error handling purposes. */
4882 obd->obd_lu_dev = &m->mdt_lu_dev;
4884 /* init the stack */
4885 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4887 CERROR("%s: Can't init device stack, rc %d\n",
4888 mdt_obd_name(m), rc);
4893 ss_site = mdt_seq_site(m);
4894 s->ld_seq_site = ss_site;
4897 /* set server index */
4898 ss_site->ss_node_id = node_id;
4900 /* failover is the default
4901 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4902 * assumed whose ss_node_id == 0 XXX
4904 obd->obd_replayable = 1;
4905 /* No connection accepted until configurations will finish */
4906 obd->obd_no_conn = 1;
4908 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4909 char *str = lustre_cfg_string(cfg, 4);
4910 if (strchr(str, 'n')) {
4911 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4912 obd->obd_replayable = 0;
4916 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4918 GOTO(err_fini_stack, rc);
4920 rc = mdt_seq_init(env, m);
4922 GOTO(err_fini_fld, rc);
4924 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4925 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4926 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4927 LDLM_NAMESPACE_SERVER,
4928 LDLM_NAMESPACE_GREEDY,
4930 if (m->mdt_namespace == NULL)
4931 GOTO(err_fini_seq, rc = -ENOMEM);
4933 m->mdt_namespace->ns_lvbp = m;
4934 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4936 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4937 /* set obd_namespace for compatibility with old code */
4938 obd->obd_namespace = m->mdt_namespace;
4940 rc = mdt_hsm_cdt_init(m);
4942 CERROR("%s: error initializing coordinator, rc %d\n",
4943 mdt_obd_name(m), rc);
4944 GOTO(err_free_ns, rc);
4947 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4948 OBD_FAIL_MDS_ALL_REQUEST_NET,
4949 OBD_FAIL_MDS_ALL_REPLY_NET);
4951 GOTO(err_free_hsm, rc);
4953 rc = mdt_fs_setup(env, m, obd, lsi);
4957 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4959 next = m->mdt_child;
4960 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4963 GOTO(err_fs_cleanup, rc);
4965 if (mntopts & MNTOPT_USERXATTR)
4966 m->mdt_opts.mo_user_xattr = 1;
4968 m->mdt_opts.mo_user_xattr = 0;
4970 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4972 GOTO(err_fs_cleanup, rc);
4974 if (mntopts & MNTOPT_ACL)
4975 m->mdt_opts.mo_acl = 1;
4977 m->mdt_opts.mo_acl = 0;
4979 /* XXX: to support suppgid for ACL, we enable identity_upcall
4980 * by default, otherwise, maybe got unexpected -EACCESS. */
4981 if (m->mdt_opts.mo_acl)
4982 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4984 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4986 &mdt_identity_upcall_cache_ops);
4987 if (IS_ERR(m->mdt_identity_cache)) {
4988 rc = PTR_ERR(m->mdt_identity_cache);
4989 m->mdt_identity_cache = NULL;
4990 GOTO(err_fs_cleanup, rc);
4993 rc = mdt_procfs_init(m, dev);
4995 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4996 GOTO(err_recovery, rc);
4999 rc = mdt_quota_init(env, m, cfg);
5001 GOTO(err_procfs, rc);
5003 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5004 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5005 "mdt_ldlm_client", m->mdt_ldlm_client);
5007 ping_evictor_start();
5009 /* recovery will be started upon mdt_prepare()
5010 * when the whole stack is complete and ready
5011 * to serve the requests */
5013 /* Reduce the initial timeout on an MDS because it doesn't need such
5014 * a long timeout as an OST does. Adaptive timeouts will adjust this
5015 * value appropriately. */
5016 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5017 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5023 target_recovery_fini(obd);
5024 upcall_cache_cleanup(m->mdt_identity_cache);
5025 m->mdt_identity_cache = NULL;
5027 mdt_fs_cleanup(env, m);
5029 tgt_fini(env, &m->mdt_lut);
5031 mdt_hsm_cdt_fini(m);
5033 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5034 obd->obd_namespace = m->mdt_namespace = NULL;
5036 mdt_seq_fini(env, m);
5038 mdt_fld_fini(env, m);
5040 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5043 server_put_mount(dev, true);
5047 /* For interoperability, the left element is old parameter, the right one
5048 * is the new version of the parameter, if some parameter is deprecated,
5049 * the new version should be set as NULL. */
5050 static struct cfg_interop_param mdt_interop_param[] = {
5051 { "mdt.group_upcall", NULL },
5052 { "mdt.quota_type", NULL },
5053 { "mdd.quota_type", NULL },
5054 { "mdt.som", NULL },
5055 { "mdt.rootsquash", "mdt.root_squash" },
5056 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5060 /* used by MGS to process specific configurations */
5061 static int mdt_process_config(const struct lu_env *env,
5062 struct lu_device *d, struct lustre_cfg *cfg)
5064 struct mdt_device *m = mdt_dev(d);
5065 struct md_device *md_next = m->mdt_child;
5066 struct lu_device *next = md2lu_dev(md_next);
5070 switch (cfg->lcfg_command) {
5072 struct obd_device *obd = d->ld_obd;
5074 /* For interoperability */
5075 struct cfg_interop_param *ptr = NULL;
5076 struct lustre_cfg *old_cfg = NULL;
5079 param = lustre_cfg_string(cfg, 1);
5080 if (param == NULL) {
5081 CERROR("param is empty\n");
5086 ptr = class_find_old_param(param, mdt_interop_param);
5088 if (ptr->new_param == NULL) {
5090 CWARN("For interoperability, skip this %s."
5091 " It is obsolete.\n", ptr->old_param);
5095 CWARN("Found old param %s, changed it to %s.\n",
5096 ptr->old_param, ptr->new_param);
5099 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5106 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5108 if (rc > 0 || rc == -ENOSYS) {
5109 /* is it an HSM var ? */
5110 rc = class_process_proc_param(PARAM_HSM,
5111 hsm_cdt_get_proc_vars(),
5113 if (rc > 0 || rc == -ENOSYS)
5114 /* we don't understand; pass it on */
5115 rc = next->ld_ops->ldo_process_config(env, next,
5119 if (old_cfg != NULL)
5120 lustre_cfg_free(cfg);
5125 /* others are passed further */
5126 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5132 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5133 const struct lu_object_header *hdr,
5134 struct lu_device *d)
5136 struct mdt_object *mo;
5140 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5142 struct lu_object *o;
5143 struct lu_object_header *h;
5146 h = &mo->mot_header;
5147 lu_object_header_init(h);
5148 lu_object_init(o, h, d);
5149 lu_object_add_top(h, o);
5150 o->lo_ops = &mdt_obj_ops;
5151 spin_lock_init(&mo->mot_write_lock);
5152 mutex_init(&mo->mot_lov_mutex);
5153 init_rwsem(&mo->mot_open_sem);
5154 atomic_set(&mo->mot_open_count, 0);
5160 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5161 const struct lu_object_conf *unused)
5163 struct mdt_device *d = mdt_dev(o->lo_dev);
5164 struct lu_device *under;
5165 struct lu_object *below;
5169 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5170 PFID(lu_object_fid(o)));
5172 under = &d->mdt_child->md_lu_dev;
5173 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5174 if (below != NULL) {
5175 lu_object_add(o, below);
5182 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5184 struct mdt_object *mo = mdt_obj(o);
5185 struct lu_object_header *h;
5189 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5190 PFID(lu_object_fid(o)));
5192 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5193 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5196 lu_object_header_fini(h);
5197 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5202 static int mdt_object_print(const struct lu_env *env, void *cookie,
5203 lu_printer_t p, const struct lu_object *o)
5205 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5207 return (*p)(env, cookie,
5208 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5209 mdto, mdto->mot_lov_created ? "lov_created" : "",
5210 mdto->mot_cache_attr ? "cache_attr" : "",
5211 mdto->mot_write_count);
5214 static int mdt_prepare(const struct lu_env *env,
5215 struct lu_device *pdev,
5216 struct lu_device *cdev)
5218 struct mdt_device *mdt = mdt_dev(cdev);
5219 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5220 struct obd_device *obd = cdev->ld_obd;
5227 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5231 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5235 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5239 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5240 /* The LFSCK instance is registered just now, so it must be there when
5241 * register the namespace to such instance. */
5242 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5244 if (mdt->mdt_seq_site.ss_node_id == 0) {
5245 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5246 &mdt->mdt_md_root_fid);
5251 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5253 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5254 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5255 LASSERT(obd->obd_no_conn);
5256 spin_lock(&obd->obd_dev_lock);
5257 obd->obd_no_conn = 0;
5258 spin_unlock(&obd->obd_dev_lock);
5260 if (obd->obd_recovering == 0)
5261 mdt_postrecov(env, mdt);
5266 const struct lu_device_operations mdt_lu_ops = {
5267 .ldo_object_alloc = mdt_object_alloc,
5268 .ldo_process_config = mdt_process_config,
5269 .ldo_prepare = mdt_prepare,
5272 static const struct lu_object_operations mdt_obj_ops = {
5273 .loo_object_init = mdt_object_init,
5274 .loo_object_free = mdt_object_free,
5275 .loo_object_print = mdt_object_print
5278 static int mdt_obd_set_info_async(const struct lu_env *env,
5279 struct obd_export *exp,
5280 __u32 keylen, void *key,
5281 __u32 vallen, void *val,
5282 struct ptlrpc_request_set *set)
5288 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5289 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5297 * Match client and server connection feature flags.
5299 * Compute the compatibility flags for a connection request based on
5300 * features mutually supported by client and server.
5302 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5303 * must not be updated here, otherwise a partially initialized value may
5304 * be exposed. After the connection request is successfully processed,
5305 * the top-level MDT connect request handler atomically updates the export
5306 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5307 * reply. \see mdt_connect().
5309 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5310 * full struct obd_connect_data. So care must be taken when accessing fields
5311 * that are not present in struct obd_connect_data_v1. See LU-16.
5313 * \param exp the obd_export associated with this client/target pair
5314 * \param mdt the target device for the connection
5315 * \param data stores data for this connect request
5318 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5319 * \retval -EBADE client and server feature requirements are incompatible
5321 static int mdt_connect_internal(struct obd_export *exp,
5322 struct mdt_device *mdt,
5323 struct obd_connect_data *data)
5325 LASSERT(data != NULL);
5327 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5329 if (mdt->mdt_bottom->dd_rdonly &&
5330 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5331 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5334 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5335 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5337 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5339 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5340 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5341 CWARN("%s: client %s does not support ibits lock, either "
5342 "very old or an invalid client: flags %#llx\n",
5343 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5344 data->ocd_connect_flags);
5348 if (!mdt->mdt_opts.mo_acl)
5349 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5351 if (!mdt->mdt_opts.mo_user_xattr)
5352 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5354 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
5355 data->ocd_brw_size = min(data->ocd_brw_size, MD_MAX_BRW_SIZE);
5356 if (data->ocd_brw_size == 0) {
5357 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
5358 "ocd_version: %x ocd_grant: %d ocd_index: %u "
5359 "ocd_brw_size unexpectedly zero, network data "
5360 "corruption? Refusing to connect this client\n",
5362 exp->exp_client_uuid.uuid,
5363 exp, data->ocd_connect_flags, data->ocd_version,
5364 data->ocd_grant, data->ocd_index);
5369 /* NB: Disregard the rule against updating
5370 * exp_connect_data.ocd_connect_flags in this case, since
5371 * tgt_client_new() needs to know if this is a lightweight
5372 * connection, and it is safe to expose this flag before
5373 * connection processing completes. */
5374 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5375 spin_lock(&exp->exp_lock);
5376 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5377 spin_unlock(&exp->exp_lock);
5380 data->ocd_version = LUSTRE_VERSION_CODE;
5382 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5383 CWARN("%s: MDS requires FID support, but client not\n",
5388 if (OCD_HAS_FLAG(data, PINGLESS)) {
5389 if (ptlrpc_pinger_suppress_pings()) {
5390 spin_lock(&exp->exp_obd->obd_dev_lock);
5391 list_del_init(&exp->exp_obd_chain_timed);
5392 spin_unlock(&exp->exp_obd->obd_dev_lock);
5394 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5398 data->ocd_max_easize = mdt->mdt_max_ea_size;
5400 /* NB: Disregard the rule against updating
5401 * exp_connect_data.ocd_connect_flags in this case, since
5402 * tgt_client_new() needs to know if this is client supports
5403 * multiple modify RPCs, and it is safe to expose this flag before
5404 * connection processing completes. */
5405 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5406 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5407 spin_lock(&exp->exp_lock);
5408 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5409 spin_unlock(&exp->exp_lock);
5415 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5416 struct mdt_thread_info *info,
5417 struct mdt_file_data *mfd)
5419 struct lu_context ses;
5423 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5428 lu_context_enter(&ses);
5430 mdt_ucred(info)->uc_valid = UCRED_OLD;
5431 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5433 lu_context_exit(&ses);
5434 lu_context_fini(&ses);
5440 static int mdt_export_cleanup(struct obd_export *exp)
5442 struct list_head closing_list;
5443 struct mdt_export_data *med = &exp->exp_mdt_data;
5444 struct obd_device *obd = exp->exp_obd;
5445 struct mdt_device *mdt;
5446 struct mdt_thread_info *info;
5448 struct mdt_file_data *mfd, *n;
5452 INIT_LIST_HEAD(&closing_list);
5453 spin_lock(&med->med_open_lock);
5454 while (!list_empty(&med->med_open_head)) {
5455 struct list_head *tmp = med->med_open_head.next;
5456 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5458 /* Remove mfd handle so it can't be found again.
5459 * We are consuming the mfd_list reference here. */
5460 class_handle_unhash(&mfd->mfd_handle);
5461 list_move_tail(&mfd->mfd_list, &closing_list);
5463 spin_unlock(&med->med_open_lock);
5464 mdt = mdt_dev(obd->obd_lu_dev);
5465 LASSERT(mdt != NULL);
5467 rc = lu_env_init(&env, LCT_MD_THREAD);
5471 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5472 LASSERT(info != NULL);
5473 memset(info, 0, sizeof *info);
5474 info->mti_env = &env;
5475 info->mti_mdt = mdt;
5476 info->mti_exp = exp;
5478 if (!list_empty(&closing_list)) {
5479 struct md_attr *ma = &info->mti_attr;
5481 /* Close any open files (which may also cause orphan
5483 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5484 list_del_init(&mfd->mfd_list);
5485 ma->ma_need = ma->ma_valid = 0;
5487 /* This file is being closed due to an eviction, it
5488 * could have been modified and now dirty regarding to
5489 * HSM archive, check this!
5490 * The logic here is to mark a file dirty if there's a
5491 * chance it was dirtied before the client was evicted,
5492 * so that we don't have to wait for a release attempt
5493 * before finding out the file was actually dirty and
5494 * fail the release. Aggressively marking it dirty here
5495 * will cause the policy engine to attempt to
5496 * re-archive it; when rearchiving, we can compare the
5497 * current version to the HSM data_version and make the
5498 * archive request into a noop if it's not actually
5501 if (mfd->mfd_mode & FMODE_WRITE)
5502 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5504 /* Don't unlink orphan on failover umount, LU-184 */
5505 if (exp->exp_obd->obd_fail) {
5506 ma->ma_valid = MA_FLAGS;
5507 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5509 mdt_mfd_close(info, mfd);
5512 info->mti_mdt = NULL;
5513 /* cleanup client slot early */
5514 tgt_client_del(&env, exp);
5520 static inline void mdt_enable_slc(struct mdt_device *mdt)
5522 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5523 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5526 static inline void mdt_disable_slc(struct mdt_device *mdt)
5528 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5529 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5532 static int mdt_obd_disconnect(struct obd_export *exp)
5538 class_export_get(exp);
5540 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5541 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5542 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5544 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5545 mdt_disable_slc(mdt);
5548 rc = server_disconnect_export(exp);
5550 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5552 rc = mdt_export_cleanup(exp);
5553 nodemap_del_member(exp);
5554 class_export_put(exp);
5558 /* mds_connect copy */
5559 static int mdt_obd_connect(const struct lu_env *env,
5560 struct obd_export **exp, struct obd_device *obd,
5561 struct obd_uuid *cluuid,
5562 struct obd_connect_data *data,
5565 struct obd_export *lexp;
5566 struct lustre_handle conn = { 0 };
5567 struct mdt_device *mdt;
5569 lnet_nid_t *client_nid = localdata;
5572 LASSERT(env != NULL);
5573 LASSERT(data != NULL);
5575 if (!exp || !obd || !cluuid)
5578 mdt = mdt_dev(obd->obd_lu_dev);
5580 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5581 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5582 atomic_inc(&mdt->mdt_mds_mds_conns);
5583 mdt_enable_slc(mdt);
5587 * first, check whether the stack is ready to handle requests
5588 * XXX: probably not very appropriate method is used now
5589 * at some point we should find a better one
5591 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5592 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5593 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5594 rc = obd_get_info(env, mdt->mdt_child_exp,
5595 sizeof(KEY_OSP_CONNECTED),
5596 KEY_OSP_CONNECTED, NULL, NULL);
5599 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5602 rc = class_connect(&conn, obd, cluuid);
5606 lexp = class_conn2export(&conn);
5607 LASSERT(lexp != NULL);
5609 rc = nodemap_add_member(*client_nid, lexp);
5610 if (rc != 0 && rc != -EEXIST)
5613 rc = mdt_connect_internal(lexp, mdt, data);
5615 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5618 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5619 rc = tgt_client_new(env, lexp);
5621 mdt_export_stats_init(obd, lexp, localdata);
5625 class_disconnect(lexp);
5626 nodemap_del_member(lexp);
5630 /* Because we do not want this export to be evicted by pinger,
5631 * let's not add this export to the timed chain list. */
5632 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5633 spin_lock(&lexp->exp_obd->obd_dev_lock);
5634 list_del_init(&lexp->exp_obd_chain_timed);
5635 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5642 static int mdt_obd_reconnect(const struct lu_env *env,
5643 struct obd_export *exp, struct obd_device *obd,
5644 struct obd_uuid *cluuid,
5645 struct obd_connect_data *data,
5648 lnet_nid_t *client_nid = localdata;
5652 if (exp == NULL || obd == NULL || cluuid == NULL)
5655 rc = nodemap_add_member(*client_nid, exp);
5656 if (rc != 0 && rc != -EEXIST)
5659 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5661 mdt_export_stats_init(obd, exp, localdata);
5663 nodemap_del_member(exp);
5668 /* FIXME: Can we avoid using these two interfaces? */
5669 static int mdt_init_export(struct obd_export *exp)
5671 struct mdt_export_data *med = &exp->exp_mdt_data;
5675 INIT_LIST_HEAD(&med->med_open_head);
5676 spin_lock_init(&med->med_open_lock);
5677 spin_lock(&exp->exp_lock);
5678 exp->exp_connecting = 1;
5679 spin_unlock(&exp->exp_lock);
5681 /* self-export doesn't need client data and ldlm initialization */
5682 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5683 &exp->exp_client_uuid)))
5686 rc = tgt_client_alloc(exp);
5690 rc = ldlm_init_export(exp);
5697 tgt_client_free(exp);
5699 CERROR("%s: Failed to initialize export: rc = %d\n",
5700 exp->exp_obd->obd_name, rc);
5704 static int mdt_destroy_export(struct obd_export *exp)
5708 target_destroy_export(exp);
5709 /* destroy can be called from failed obd_setup, so
5710 * checking uuid is safer than obd_self_export */
5711 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5712 &exp->exp_client_uuid)))
5715 ldlm_destroy_export(exp);
5716 tgt_client_free(exp);
5718 LASSERT(list_empty(&exp->exp_outstanding_replies));
5719 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5724 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5725 struct linkea_data *ldata)
5729 LASSERT(ldata->ld_buf->lb_buf != NULL);
5731 if (!mdt_object_exists(mdt_obj))
5734 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5735 ldata->ld_buf, XATTR_NAME_LINK);
5736 if (rc == -ERANGE) {
5737 /* Buf was too small, figure out what we need. */
5738 lu_buf_free(ldata->ld_buf);
5739 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5740 ldata->ld_buf, XATTR_NAME_LINK);
5743 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5744 if (ldata->ld_buf->lb_buf == NULL)
5746 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5747 ldata->ld_buf, XATTR_NAME_LINK);
5752 return linkea_init_with_rec(ldata);
5756 * Given an MDT object, try to look up the full path to the object.
5757 * Part of the MDT layer implementation of lfs fid2path.
5759 * \param[in] info Per-thread common data shared by MDT level handlers.
5760 * \param[in] obj Object to do path lookup of
5761 * \param[in,out] fp User-provided struct to store path information
5762 * \param[in] root_fid Root FID of current path should reach
5764 * \retval 0 Lookup successful, path information stored in fp
5765 * \retval -EAGAIN Lookup failed, usually because object is being moved
5766 * \retval negative errno if there was a problem
5768 static int mdt_path_current(struct mdt_thread_info *info,
5769 struct mdt_object *obj,
5770 struct getinfo_fid2path *fp,
5771 struct lu_fid *root_fid)
5773 struct mdt_device *mdt = info->mti_mdt;
5774 struct mdt_object *mdt_obj;
5775 struct link_ea_header *leh;
5776 struct link_ea_entry *lee;
5777 struct lu_name *tmpname = &info->mti_name;
5778 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5779 struct lu_buf *buf = &info->mti_big_buf;
5782 struct linkea_data ldata = { NULL };
5787 /* temp buffer for path element, the buffer will be finally freed
5788 * in mdt_thread_info_fini */
5789 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5790 if (buf->lb_buf == NULL)
5794 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5797 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5799 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5800 struct lu_buf lmv_buf;
5802 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5803 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5804 GOTO(out, rc = -ENOENT);
5806 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5808 mdt_object_get(info->mti_env, mdt_obj);
5810 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5811 if (IS_ERR(mdt_obj))
5812 GOTO(out, rc = PTR_ERR(mdt_obj));
5815 if (!mdt_object_exists(mdt_obj)) {
5816 mdt_object_put(info->mti_env, mdt_obj);
5817 GOTO(out, rc = -ENOENT);
5820 if (mdt_object_remote(mdt_obj)) {
5821 mdt_object_put(info->mti_env, mdt_obj);
5822 GOTO(remote_out, rc = -EREMOTE);
5825 rc = mdt_links_read(info, mdt_obj, &ldata);
5827 mdt_object_put(info->mti_env, mdt_obj);
5832 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5833 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5834 /* If set, use link #linkno for path lookup, otherwise use
5835 link #0. Only do this for the final path element. */
5836 if (first && fp->gf_linkno < leh->leh_reccount) {
5838 for (count = 0; count < fp->gf_linkno; count++) {
5839 lee = (struct link_ea_entry *)
5840 ((char *)lee + reclen);
5841 linkea_entry_unpack(lee, &reclen, tmpname,
5844 if (fp->gf_linkno < leh->leh_reccount - 1)
5845 /* indicate to user there are more links */
5849 lmv_buf.lb_buf = info->mti_xattr_buf;
5850 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5851 /* Check if it is slave stripes */
5852 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5853 &lmv_buf, XATTR_NAME_LMV);
5854 mdt_object_put(info->mti_env, mdt_obj);
5856 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5858 /* For slave stripes, get its master */
5859 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5860 fp->gf_fid = *tmpfid;
5863 } else if (rc < 0 && rc != -ENODATA) {
5869 /* Pack the name in the end of the buffer */
5870 ptr -= tmpname->ln_namelen;
5871 if (ptr - 1 <= fp->gf_u.gf_path)
5872 GOTO(out, rc = -EOVERFLOW);
5873 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5876 /* keep the last resolved fid to the client, so the
5877 * client will build the left path on another MDT for
5879 fp->gf_fid = *tmpfid;
5885 ptr++; /* skip leading / */
5886 memmove(fp->gf_u.gf_path, ptr,
5887 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5894 * Given an MDT object, use mdt_path_current to get the path.
5895 * Essentially a wrapper to retry mdt_path_current a set number of times
5896 * if -EAGAIN is returned (usually because an object is being moved).
5898 * Part of the MDT layer implementation of lfs fid2path.
5900 * \param[in] info Per-thread common data shared by mdt level handlers.
5901 * \param[in] obj Object to do path lookup of
5902 * \param[in,out] fp User-provided struct for arguments and to store path
5905 * \retval 0 Lookup successful, path information stored in fp
5906 * \retval negative errno if there was a problem
5908 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5909 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5911 struct mdt_device *mdt = info->mti_mdt;
5916 if (fp->gf_pathlen < 3)
5919 if (root_fid == NULL)
5920 root_fid = &mdt->mdt_md_root_fid;
5922 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5923 fp->gf_u.gf_path[0] = '\0';
5927 /* Retry multiple times in case file is being moved */
5928 while (tries-- && rc == -EAGAIN)
5929 rc = mdt_path_current(info, obj, fp, root_fid);
5935 * Get the full path of the provided FID, as of changelog record recno.
5937 * This checks sanity and looks up object for user provided FID
5938 * before calling the actual path lookup code.
5940 * Part of the MDT layer implementation of lfs fid2path.
5942 * \param[in] info Per-thread common data shared by mdt level handlers.
5943 * \param[in,out] fp User-provided struct for arguments and to store path
5946 * \retval 0 Lookup successful, path information and recno stored in fp
5947 * \retval -ENOENT, object does not exist
5948 * \retval negative errno if there was a problem
5950 static int mdt_fid2path(struct mdt_thread_info *info,
5951 struct lu_fid *root_fid,
5952 struct getinfo_fid2path *fp)
5954 struct mdt_device *mdt = info->mti_mdt;
5955 struct mdt_object *obj;
5959 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5960 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5962 if (!fid_is_sane(&fp->gf_fid))
5965 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5966 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5967 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5968 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5972 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5975 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5976 PFID(&fp->gf_fid), rc);
5980 if (mdt_object_remote(obj))
5982 else if (!mdt_object_exists(obj))
5988 mdt_object_put(info->mti_env, obj);
5989 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5990 PFID(&fp->gf_fid), rc);
5994 rc = mdt_path(info, obj, fp, root_fid);
5996 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5997 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5998 fp->gf_recno, fp->gf_linkno);
6000 mdt_object_put(info->mti_env, obj);
6005 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6006 void *val, int vallen)
6008 struct getinfo_fid2path *fpout, *fpin;
6009 struct lu_fid *root_fid = NULL;
6012 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6015 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6016 lustre_swab_fid2path(fpin);
6018 memcpy(fpout, fpin, sizeof(*fpin));
6019 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6022 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6023 sizeof(struct lu_fid)) {
6024 /* client sent its root FID, which is normally fileset FID */
6025 root_fid = fpin->gf_u.gf_root_fid;
6026 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6027 lustre_swab_lu_fid(root_fid);
6029 if (root_fid != NULL && !fid_is_sane(root_fid))
6033 rc = mdt_fid2path(info, root_fid, fpout);
6037 int mdt_get_info(struct tgt_session_info *tsi)
6047 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6049 CDEBUG(D_IOCTL, "No GETINFO key\n");
6050 RETURN(err_serious(-EFAULT));
6052 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6055 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6056 if (vallen == NULL) {
6057 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6058 tgt_name(tsi->tsi_tgt));
6059 RETURN(err_serious(-EFAULT));
6062 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6064 rc = req_capsule_server_pack(tsi->tsi_pill);
6066 RETURN(err_serious(rc));
6068 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6069 if (valout == NULL) {
6070 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6071 tgt_name(tsi->tsi_tgt));
6072 RETURN(err_serious(-EFAULT));
6075 if (KEY_IS(KEY_FID2PATH)) {
6076 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6078 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6079 mdt_thread_info_fini(info);
6086 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6088 struct obd_ioctl_data *data = karg;
6091 struct mdt_object *obj;
6092 struct mdt_lock_handle *lh;
6096 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6097 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6100 fid = (struct lu_fid *)data->ioc_inlbuf1;
6102 if (!fid_is_sane(fid))
6105 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6107 lh = &mti->mti_lh[MDT_LH_PARENT];
6108 mdt_lock_reg_init(lh, LCK_CR);
6110 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6112 RETURN(PTR_ERR(obj));
6114 if (mdt_object_remote(obj)) {
6117 * before calling version get the correct MDS should be
6118 * fid, this is error to find remote object here
6120 CERROR("nonlocal object "DFID"\n", PFID(fid));
6121 } else if (!mdt_object_exists(obj)) {
6122 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6125 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6126 *(__u64 *)data->ioc_inlbuf2 = version;
6129 mdt_object_unlock_put(mti, obj, lh, 1);
6133 /* ioctls on obd dev */
6134 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6135 void *karg, void __user *uarg)
6138 struct obd_device *obd = exp->exp_obd;
6139 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6140 struct dt_device *dt = mdt->mdt_bottom;
6144 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6145 rc = lu_env_init(&env, LCT_MD_THREAD);
6151 rc = mdt_device_sync(&env, mdt);
6153 case OBD_IOC_SET_READONLY:
6154 rc = dt_sync(&env, dt);
6156 rc = dt_ro(&env, dt);
6158 case OBD_IOC_ABORT_RECOVERY:
6159 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6160 obd->obd_abort_recovery = 1;
6161 target_stop_recovery_thread(obd);
6164 case OBD_IOC_CHANGELOG_REG:
6165 case OBD_IOC_CHANGELOG_DEREG:
6166 case OBD_IOC_CHANGELOG_CLEAR:
6167 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6171 case OBD_IOC_START_LFSCK: {
6172 struct md_device *next = mdt->mdt_child;
6173 struct obd_ioctl_data *data = karg;
6174 struct lfsck_start_param lsp;
6176 if (unlikely(data == NULL)) {
6181 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6182 lsp.lsp_index_valid = 0;
6183 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6186 case OBD_IOC_STOP_LFSCK: {
6187 struct md_device *next = mdt->mdt_child;
6188 struct obd_ioctl_data *data = karg;
6189 struct lfsck_stop stop;
6191 stop.ls_status = LS_STOPPED;
6192 /* Old lfsck utils may pass NULL @stop. */
6193 if (data->ioc_inlbuf1 == NULL)
6197 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6199 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6202 case OBD_IOC_QUERY_LFSCK: {
6203 struct md_device *next = mdt->mdt_child;
6204 struct obd_ioctl_data *data = karg;
6206 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6210 case OBD_IOC_GET_OBJ_VERSION: {
6211 struct mdt_thread_info *mti;
6212 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6213 memset(mti, 0, sizeof *mti);
6214 mti->mti_env = &env;
6218 rc = mdt_ioc_version_get(mti, karg);
6221 case OBD_IOC_CATLOGLIST: {
6222 struct mdt_thread_info *mti;
6224 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6225 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6226 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6227 &mti->mti_tmp_fid1);
6232 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6233 mdt_obd_name(mdt), cmd, rc);
6240 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6242 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6246 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6247 struct lfsck_start_param lsp;
6249 lsp.lsp_start = NULL;
6250 lsp.lsp_index_valid = 0;
6251 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6252 OBD_IOC_START_LFSCK,
6254 if (rc != 0 && rc != -EALREADY)
6255 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6256 mdt_obd_name(mdt), rc);
6259 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6263 static int mdt_obd_postrecov(struct obd_device *obd)
6268 rc = lu_env_init(&env, LCT_MD_THREAD);
6271 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6276 static struct obd_ops mdt_obd_device_ops = {
6277 .o_owner = THIS_MODULE,
6278 .o_set_info_async = mdt_obd_set_info_async,
6279 .o_connect = mdt_obd_connect,
6280 .o_reconnect = mdt_obd_reconnect,
6281 .o_disconnect = mdt_obd_disconnect,
6282 .o_init_export = mdt_init_export,
6283 .o_destroy_export = mdt_destroy_export,
6284 .o_iocontrol = mdt_iocontrol,
6285 .o_postrecov = mdt_obd_postrecov,
6288 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6289 struct lu_device *d)
6291 struct mdt_device *m = mdt_dev(d);
6298 static struct lu_device *mdt_device_free(const struct lu_env *env,
6299 struct lu_device *d)
6301 struct mdt_device *m = mdt_dev(d);
6304 lu_device_fini(&m->mdt_lu_dev);
6310 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6311 struct lu_device_type *t,
6312 struct lustre_cfg *cfg)
6314 struct lu_device *l;
6315 struct mdt_device *m;
6322 rc = mdt_init0(env, m, t, cfg);
6324 mdt_device_free(env, l);
6329 l = ERR_PTR(-ENOMEM);
6333 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6334 LU_KEY_INIT(mdt, struct mdt_thread_info);
6336 static void mdt_key_fini(const struct lu_context *ctx,
6337 struct lu_context_key *key, void* data)
6339 struct mdt_thread_info *info = data;
6341 if (info->mti_big_lmm) {
6342 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6343 info->mti_big_lmm = NULL;
6344 info->mti_big_lmmsize = 0;
6347 if (info->mti_big_acl) {
6348 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
6349 info->mti_big_acl = NULL;
6350 info->mti_big_aclsize = 0;
6356 /* context key: mdt_thread_key */
6357 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6359 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6361 return lu_ucred(info->mti_env);
6364 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6366 return lu_ucred_check(info->mti_env);
6370 * Enable/disable COS (Commit On Sharing).
6372 * Set/Clear the COS flag in mdt options.
6374 * \param mdt mdt device
6375 * \param val 0 disables COS, other values enable COS
6377 void mdt_enable_cos(struct mdt_device *mdt, int val)
6382 mdt->mdt_opts.mo_cos = !!val;
6383 rc = lu_env_init(&env, LCT_LOCAL);
6384 if (unlikely(rc != 0)) {
6385 CWARN("%s: lu_env initialization failed, cannot "
6386 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6389 mdt_device_sync(&env, mdt);
6394 * Check COS (Commit On Sharing) status.
6396 * Return COS flag status.
6398 * \param mdt mdt device
6400 int mdt_cos_is_enabled(struct mdt_device *mdt)
6402 return mdt->mdt_opts.mo_cos != 0;
6405 static struct lu_device_type_operations mdt_device_type_ops = {
6406 .ldto_device_alloc = mdt_device_alloc,
6407 .ldto_device_free = mdt_device_free,
6408 .ldto_device_fini = mdt_device_fini
6411 static struct lu_device_type mdt_device_type = {
6412 .ldt_tags = LU_DEVICE_MD,
6413 .ldt_name = LUSTRE_MDT_NAME,
6414 .ldt_ops = &mdt_device_type_ops,
6415 .ldt_ctx_tags = LCT_MD_THREAD
6418 static int __init mdt_init(void)
6422 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6423 FID_NOBRACE_LEN + 1);
6424 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6426 rc = lu_kmem_init(mdt_caches);
6430 rc = mds_mod_init();
6434 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6435 LUSTRE_MDT_NAME, &mdt_device_type);
6440 lu_kmem_fini(mdt_caches);
6447 static void __exit mdt_exit(void)
6449 class_unregister_type(LUSTRE_MDT_NAME);
6451 lu_kmem_fini(mdt_caches);
6454 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6455 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6456 MODULE_VERSION(LUSTRE_VERSION_STRING);
6457 MODULE_LICENSE("GPL");
6459 module_init(mdt_init);
6460 module_exit(mdt_exit);