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 <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 = full_name_hash(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;
488 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
489 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
491 if (buf->lb_len == 0)
494 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
496 if (rc == -ENODATA) {
497 repbody->mbo_aclsize = 0;
498 repbody->mbo_valid |= OBD_MD_FLACL;
500 } else if (rc == -EOPNOTSUPP) {
503 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
504 mdt_obd_name(info->mti_mdt),
505 PFID(mdt_object_fid(o)), rc);
508 rc = nodemap_map_acl(nodemap, buf->lb_buf,
509 rc, NODEMAP_FS_TO_CLIENT);
510 /* if all ACLs mapped out, rc is still >= 0 */
512 CERROR("%s: nodemap_map_acl unable to parse "DFID
513 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
514 PFID(mdt_object_fid(o)), rc);
516 repbody->mbo_aclsize = rc;
517 repbody->mbo_valid |= OBD_MD_FLACL;
525 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
526 const struct lu_attr *attr, const struct lu_fid *fid)
528 struct md_attr *ma = &info->mti_attr;
529 struct obd_export *exp = info->mti_exp;
530 struct lu_nodemap *nodemap = NULL;
532 LASSERT(ma->ma_valid & MA_INODE);
534 if (attr->la_valid & LA_ATIME) {
535 b->mbo_atime = attr->la_atime;
536 b->mbo_valid |= OBD_MD_FLATIME;
538 if (attr->la_valid & LA_MTIME) {
539 b->mbo_mtime = attr->la_mtime;
540 b->mbo_valid |= OBD_MD_FLMTIME;
542 if (attr->la_valid & LA_CTIME) {
543 b->mbo_ctime = attr->la_ctime;
544 b->mbo_valid |= OBD_MD_FLCTIME;
546 if (attr->la_valid & LA_FLAGS) {
547 b->mbo_flags = attr->la_flags;
548 b->mbo_valid |= OBD_MD_FLFLAGS;
550 if (attr->la_valid & LA_NLINK) {
551 b->mbo_nlink = attr->la_nlink;
552 b->mbo_valid |= OBD_MD_FLNLINK;
554 if (attr->la_valid & (LA_UID|LA_GID)) {
555 nodemap = nodemap_get_from_exp(exp);
559 if (attr->la_valid & LA_UID) {
560 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
561 NODEMAP_FS_TO_CLIENT,
563 b->mbo_valid |= OBD_MD_FLUID;
565 if (attr->la_valid & LA_GID) {
566 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
567 NODEMAP_FS_TO_CLIENT,
569 b->mbo_valid |= OBD_MD_FLGID;
572 b->mbo_mode = attr->la_mode;
573 if (attr->la_valid & LA_MODE)
574 b->mbo_valid |= OBD_MD_FLMODE;
575 if (attr->la_valid & LA_TYPE)
576 b->mbo_valid |= OBD_MD_FLTYPE;
580 b->mbo_valid |= OBD_MD_FLID;
581 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
582 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
585 if (!(attr->la_valid & LA_TYPE))
588 b->mbo_rdev = attr->la_rdev;
589 b->mbo_size = attr->la_size;
590 b->mbo_blocks = attr->la_blocks;
592 if (!S_ISREG(attr->la_mode)) {
593 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
594 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
595 /* means no objects are allocated on osts. */
596 LASSERT(!(ma->ma_valid & MA_LOV));
597 /* just ignore blocks occupied by extend attributes on MDS */
599 /* if no object is allocated on osts, the size on mds is valid.
601 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
602 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
603 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
604 /* A released file stores its size on MDS. */
605 /* But return 1 block for released file, unless tools like tar
606 * will consider it fully sparse. (LU-3864)
608 if (unlikely(b->mbo_size == 0))
612 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
615 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
616 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
617 PFID(fid), (unsigned long long)b->mbo_size);
620 if (!IS_ERR_OR_NULL(nodemap))
621 nodemap_putref(nodemap);
624 static inline int mdt_body_has_lov(const struct lu_attr *la,
625 const struct mdt_body *body)
627 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
628 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
631 void mdt_client_compatibility(struct mdt_thread_info *info)
633 struct mdt_body *body;
634 struct ptlrpc_request *req = mdt_info_req(info);
635 struct obd_export *exp = req->rq_export;
636 struct md_attr *ma = &info->mti_attr;
637 struct lu_attr *la = &ma->ma_attr;
640 if (exp_connect_layout(exp))
641 /* the client can deal with 16-bit lmm_stripe_count */
644 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
646 if (!mdt_body_has_lov(la, body))
649 /* now we have a reply with a lov for a client not compatible with the
650 * layout lock so we have to clean the layout generation number */
651 if (S_ISREG(la->la_mode))
652 ma->ma_lmm->lmm_layout_gen = 0;
656 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
657 struct mdt_object *o)
659 const struct lu_env *env = info->mti_env;
662 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
671 /* Is it a directory? Let's check for the LMV as well */
672 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
673 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
677 rc2 = mo_xattr_get(env, mdt_object_child(o),
679 XATTR_NAME_DEFAULT_LMV);
681 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
689 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
692 const struct lu_env *env = info->mti_env;
696 LASSERT(info->mti_big_lmm_used == 0);
697 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
701 /* big_lmm may need to be grown */
702 if (info->mti_big_lmmsize < rc) {
703 int size = size_roundup_power2(rc);
705 if (info->mti_big_lmmsize > 0) {
706 /* free old buffer */
707 LASSERT(info->mti_big_lmm);
708 OBD_FREE_LARGE(info->mti_big_lmm,
709 info->mti_big_lmmsize);
710 info->mti_big_lmm = NULL;
711 info->mti_big_lmmsize = 0;
714 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
715 if (info->mti_big_lmm == NULL)
717 info->mti_big_lmmsize = size;
719 LASSERT(info->mti_big_lmmsize >= rc);
721 info->mti_buf.lb_buf = info->mti_big_lmm;
722 info->mti_buf.lb_len = info->mti_big_lmmsize;
723 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
728 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
729 struct md_attr *ma, const char *name)
731 struct md_object *next = mdt_object_child(o);
732 struct lu_buf *buf = &info->mti_buf;
735 if (strcmp(name, XATTR_NAME_LOV) == 0) {
736 buf->lb_buf = ma->ma_lmm;
737 buf->lb_len = ma->ma_lmm_size;
738 LASSERT(!(ma->ma_valid & MA_LOV));
739 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
740 buf->lb_buf = ma->ma_lmv;
741 buf->lb_len = ma->ma_lmv_size;
742 LASSERT(!(ma->ma_valid & MA_LMV));
743 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
744 buf->lb_buf = ma->ma_lmv;
745 buf->lb_len = ma->ma_lmv_size;
746 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
751 rc = mo_xattr_get(info->mti_env, next, buf, name);
755 if (strcmp(name, XATTR_NAME_LOV) == 0) {
756 if (info->mti_big_lmm_used)
757 ma->ma_lmm = info->mti_big_lmm;
759 /* NOT return LOV EA with hole to old client. */
760 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
761 LOV_PATTERN_F_HOLE) &&
762 !(exp_connect_flags(info->mti_exp) &
763 OBD_CONNECT_LFSCK)) {
766 ma->ma_lmm_size = rc;
767 ma->ma_valid |= MA_LOV;
769 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
770 if (info->mti_big_lmm_used)
771 ma->ma_lmv = info->mti_big_lmm;
773 ma->ma_lmv_size = rc;
774 ma->ma_valid |= MA_LMV;
775 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
776 ma->ma_lmv_size = rc;
777 ma->ma_valid |= MA_LMV_DEF;
780 /* Update mdt_max_mdsize so all clients will be aware that */
781 if (info->mti_mdt->mdt_max_mdsize < rc)
782 info->mti_mdt->mdt_max_mdsize = rc;
785 } else if (rc == -ENODATA) {
788 } else if (rc == -ERANGE) {
789 /* Default LMV has fixed size, so it must be able to fit
790 * in the original buffer */
791 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
793 rc = mdt_big_xattr_get(info, o, name);
795 info->mti_big_lmm_used = 1;
803 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
804 struct mdt_object *o, struct lu_fid *pfid)
806 struct lu_buf *buf = &info->mti_buf;
807 struct link_ea_header *leh;
808 struct link_ea_entry *lee;
812 buf->lb_buf = info->mti_big_lmm;
813 buf->lb_len = info->mti_big_lmmsize;
814 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
815 buf, XATTR_NAME_LINK);
816 /* ignore errors, MA_PFID won't be set and it is
817 * up to the caller to treat this as an error */
818 if (rc == -ERANGE || buf->lb_len == 0) {
819 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
820 buf->lb_buf = info->mti_big_lmm;
821 buf->lb_len = info->mti_big_lmmsize;
826 if (rc < sizeof(*leh)) {
827 CERROR("short LinkEA on "DFID": rc = %d\n",
828 PFID(mdt_object_fid(o)), rc);
832 leh = (struct link_ea_header *) buf->lb_buf;
833 lee = (struct link_ea_entry *)(leh + 1);
834 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
835 leh->leh_magic = LINK_EA_MAGIC;
836 leh->leh_reccount = __swab32(leh->leh_reccount);
837 leh->leh_len = __swab64(leh->leh_len);
839 if (leh->leh_magic != LINK_EA_MAGIC)
841 if (leh->leh_reccount == 0)
844 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
845 fid_be_to_cpu(pfid, pfid);
850 int mdt_attr_get_complex(struct mdt_thread_info *info,
851 struct mdt_object *o, struct md_attr *ma)
853 const struct lu_env *env = info->mti_env;
854 struct md_object *next = mdt_object_child(o);
855 struct lu_buf *buf = &info->mti_buf;
856 int need = ma->ma_need;
863 if (mdt_object_exists(o) == 0)
864 GOTO(out, rc = -ENOENT);
865 mode = lu_object_attr(&next->mo_lu);
867 if (need & MA_INODE) {
868 ma->ma_need = MA_INODE;
869 rc = mo_attr_get(env, next, ma);
872 ma->ma_valid |= MA_INODE;
875 if (need & MA_PFID) {
876 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
878 ma->ma_valid |= MA_PFID;
879 /* ignore this error, parent fid is not mandatory */
883 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
884 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
889 if (need & MA_LMV && S_ISDIR(mode)) {
890 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
895 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
896 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
901 if (need & MA_HSM && S_ISREG(mode)) {
902 buf->lb_buf = info->mti_xattr_buf;
903 buf->lb_len = sizeof(info->mti_xattr_buf);
904 CLASSERT(sizeof(struct hsm_attrs) <=
905 sizeof(info->mti_xattr_buf));
906 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
907 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
909 ma->ma_valid |= MA_HSM;
910 else if (rc2 < 0 && rc2 != -ENODATA)
914 #ifdef CONFIG_FS_POSIX_ACL
915 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
916 buf->lb_buf = ma->ma_acl;
917 buf->lb_len = ma->ma_acl_size;
918 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
920 ma->ma_acl_size = rc2;
921 ma->ma_valid |= MA_ACL_DEF;
922 } else if (rc2 == -ENODATA) {
931 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
932 rc, ma->ma_valid, ma->ma_lmm);
936 static int mdt_getattr_internal(struct mdt_thread_info *info,
937 struct mdt_object *o, int ma_need)
939 struct md_object *next = mdt_object_child(o);
940 const struct mdt_body *reqbody = info->mti_body;
941 struct ptlrpc_request *req = mdt_info_req(info);
942 struct md_attr *ma = &info->mti_attr;
943 struct lu_attr *la = &ma->ma_attr;
944 struct req_capsule *pill = info->mti_pill;
945 const struct lu_env *env = info->mti_env;
946 struct mdt_body *repbody;
947 struct lu_buf *buffer = &info->mti_buf;
948 struct obd_export *exp = info->mti_exp;
953 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
954 RETURN(err_serious(-ENOMEM));
956 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
960 if (mdt_object_remote(o)) {
961 /* This object is located on remote node.*/
962 /* Return -ENOTSUPP for old client */
963 if (!mdt_is_dne_client(req->rq_export))
964 GOTO(out, rc = -ENOTSUPP);
966 repbody->mbo_fid1 = *mdt_object_fid(o);
967 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
971 if (reqbody->mbo_eadatasize > 0) {
972 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
973 if (buffer->lb_buf == NULL)
974 GOTO(out, rc = -EPROTO);
975 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
978 buffer->lb_buf = NULL;
980 ma_need &= ~(MA_LOV | MA_LMV);
981 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
982 mdt_obd_name(info->mti_mdt),
983 req->rq_export->exp_client_uuid.uuid);
986 /* If it is dir object and client require MEA, then we got MEA */
987 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
988 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
989 /* Assumption: MDT_MD size is enough for lmv size. */
990 ma->ma_lmv = buffer->lb_buf;
991 ma->ma_lmv_size = buffer->lb_len;
992 ma->ma_need = MA_INODE;
993 if (ma->ma_lmv_size > 0) {
994 if (reqbody->mbo_valid & OBD_MD_MEA)
995 ma->ma_need |= MA_LMV;
996 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
997 ma->ma_need |= MA_LMV_DEF;
1000 ma->ma_lmm = buffer->lb_buf;
1001 ma->ma_lmm_size = buffer->lb_len;
1002 ma->ma_need = MA_INODE | MA_HSM;
1003 if (ma->ma_lmm_size > 0)
1004 ma->ma_need |= MA_LOV;
1007 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1008 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1009 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1010 /* get default stripe info for this dir. */
1011 ma->ma_need |= MA_LOV_DEF;
1013 ma->ma_need |= ma_need;
1015 rc = mdt_attr_get_complex(info, o, ma);
1017 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1018 "%s: getattr error for "DFID": rc = %d\n",
1019 mdt_obd_name(info->mti_mdt),
1020 PFID(mdt_object_fid(o)), rc);
1024 /* if file is released, check if a restore is running */
1025 if (ma->ma_valid & MA_HSM) {
1026 repbody->mbo_valid |= OBD_MD_TSTATE;
1027 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1028 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1029 repbody->mbo_t_state = MS_RESTORE;
1032 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
1034 /* the Lustre protocol supposes to return default striping
1035 * on the user-visible root if explicitly requested */
1036 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
1037 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
1038 struct lu_fid rootfid;
1039 struct mdt_object *root;
1040 struct mdt_device *mdt = info->mti_mdt;
1042 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
1045 root = mdt_object_find(env, mdt, &rootfid);
1047 RETURN(PTR_ERR(root));
1048 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
1049 mdt_object_put(info->mti_env, root);
1051 CERROR("%s: getattr error for "DFID": rc = %d\n",
1052 mdt_obd_name(info->mti_mdt),
1053 PFID(mdt_object_fid(o)), rc);
1058 if (likely(ma->ma_valid & MA_INODE))
1059 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1063 if (mdt_body_has_lov(la, reqbody)) {
1064 if (ma->ma_valid & MA_LOV) {
1065 LASSERT(ma->ma_lmm_size);
1066 repbody->mbo_eadatasize = ma->ma_lmm_size;
1067 if (S_ISDIR(la->la_mode))
1068 repbody->mbo_valid |= OBD_MD_FLDIREA;
1070 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1071 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1073 if (ma->ma_valid & MA_LMV) {
1074 /* Return -ENOTSUPP for old client */
1075 if (!mdt_is_striped_client(req->rq_export))
1078 LASSERT(S_ISDIR(la->la_mode));
1079 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1080 repbody->mbo_eadatasize = ma->ma_lmv_size;
1081 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1083 if (ma->ma_valid & MA_LMV_DEF) {
1084 /* Return -ENOTSUPP for old client */
1085 if (!mdt_is_striped_client(req->rq_export))
1087 LASSERT(S_ISDIR(la->la_mode));
1088 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1089 repbody->mbo_eadatasize = ma->ma_lmv_size;
1090 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1091 OBD_MD_DEFAULT_MEA);
1093 } else if (S_ISLNK(la->la_mode) &&
1094 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1095 buffer->lb_buf = ma->ma_lmm;
1096 /* eadatasize from client includes NULL-terminator, so
1097 * there is no need to read it */
1098 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1099 rc = mo_readlink(env, next, buffer);
1100 if (unlikely(rc <= 0)) {
1101 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1102 mdt_obd_name(info->mti_mdt),
1103 PFID(mdt_object_fid(o)), rc);
1106 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1108 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1110 repbody->mbo_valid |= OBD_MD_LINKNAME;
1111 /* we need to report back size with NULL-terminator
1112 * because client expects that */
1113 repbody->mbo_eadatasize = rc + 1;
1114 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1115 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1116 "on "DFID ", expected %d\n",
1117 mdt_obd_name(info->mti_mdt),
1118 rc, PFID(mdt_object_fid(o)),
1119 reqbody->mbo_eadatasize - 1);
1120 /* NULL terminate */
1121 ((char *)ma->ma_lmm)[rc] = 0;
1123 /* If the total CDEBUG() size is larger than a page, it
1124 * will print a warning to the console, avoid this by
1125 * printing just the last part of the symlink. */
1126 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1127 print_limit < rc ? "..." : "", print_limit,
1128 (char *)ma->ma_lmm + rc - print_limit, rc);
1133 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1134 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1135 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1136 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1137 repbody->mbo_max_mdsize);
1140 #ifdef CONFIG_FS_POSIX_ACL
1141 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1142 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1143 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1144 if (IS_ERR(nodemap))
1145 RETURN(PTR_ERR(nodemap));
1147 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1148 nodemap_putref(nodemap);
1154 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1159 static int mdt_getattr(struct tgt_session_info *tsi)
1161 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1162 struct mdt_object *obj = info->mti_object;
1163 struct req_capsule *pill = info->mti_pill;
1164 struct mdt_body *reqbody;
1165 struct mdt_body *repbody;
1169 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1171 LASSERT(obj != NULL);
1172 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1174 /* Unlike intent case where we need to pre-fill out buffers early on
1175 * in intent policy for ldlm reasons, here we can have a much better
1176 * guess at EA size by just reading it from disk.
1177 * Exceptions are readdir and (missing) directory striping */
1179 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1180 /* No easy way to know how long is the symlink, but it cannot
1181 * be more than PATH_MAX, so we allocate +1 */
1184 /* A special case for fs ROOT: getattr there might fetch
1185 * default EA for entire fs, not just for this dir!
1187 } else if (lu_fid_eq(mdt_object_fid(obj),
1188 &info->mti_mdt->mdt_md_root_fid) &&
1189 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1190 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1192 /* Should the default strping be bigger, mdt_fix_reply
1193 * will reallocate */
1194 rc = DEF_REP_MD_SIZE;
1196 /* Read the actual EA size from disk */
1197 rc = mdt_attr_get_eabuf_size(info, obj);
1201 GOTO(out, rc = err_serious(rc));
1203 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1205 rc = req_capsule_server_pack(pill);
1206 if (unlikely(rc != 0))
1207 GOTO(out, rc = err_serious(rc));
1209 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1210 LASSERT(repbody != NULL);
1211 repbody->mbo_eadatasize = 0;
1212 repbody->mbo_aclsize = 0;
1214 rc = mdt_check_ucred(info);
1216 GOTO(out_shrink, rc);
1218 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1220 rc = mdt_getattr_internal(info, obj, 0);
1223 mdt_client_compatibility(info);
1224 rc2 = mdt_fix_reply(info);
1228 mdt_thread_info_fini(info);
1233 * Exchange MOF_LOV_CREATED flags between two objects after a
1234 * layout swap. No assumption is made on whether o1 or o2 have
1235 * created objects or not.
1237 * \param[in,out] o1 First swap layout object
1238 * \param[in,out] o2 Second swap layout object
1240 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1242 unsigned int o1_lov_created = o1->mot_lov_created;
1244 mutex_lock(&o1->mot_lov_mutex);
1245 mutex_lock(&o2->mot_lov_mutex);
1247 o1->mot_lov_created = o2->mot_lov_created;
1248 o2->mot_lov_created = o1_lov_created;
1250 mutex_unlock(&o2->mot_lov_mutex);
1251 mutex_unlock(&o1->mot_lov_mutex);
1254 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1256 struct mdt_thread_info *info;
1257 struct ptlrpc_request *req = tgt_ses_req(tsi);
1258 struct obd_export *exp = req->rq_export;
1259 struct mdt_object *o1, *o2, *o;
1260 struct mdt_lock_handle *lh1, *lh2;
1261 struct mdc_swap_layouts *msl;
1265 /* client does not support layout lock, so layout swaping
1267 * FIXME: there is a problem for old clients which don't support
1268 * layout lock yet. If those clients have already opened the file
1269 * they won't be notified at all so that old layout may still be
1270 * used to do IO. This can be fixed after file release is landed by
1271 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1272 if (!exp_connect_layout(exp))
1273 RETURN(-EOPNOTSUPP);
1275 info = tsi2mdt_info(tsi);
1277 if (info->mti_dlm_req != NULL)
1278 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1280 o1 = info->mti_object;
1281 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1282 &info->mti_body->mbo_fid2);
1284 GOTO(out, rc = PTR_ERR(o));
1286 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1287 GOTO(put, rc = -ENOENT);
1289 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1290 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1296 /* permission check. Make sure the calling process having permission
1297 * to write both files. */
1298 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1303 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1308 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1310 GOTO(put, rc = -EPROTO);
1312 lh1 = &info->mti_lh[MDT_LH_NEW];
1313 mdt_lock_reg_init(lh1, LCK_EX);
1314 lh2 = &info->mti_lh[MDT_LH_OLD];
1315 mdt_lock_reg_init(lh2, LCK_EX);
1317 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1318 MDS_INODELOCK_XATTR);
1322 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1323 MDS_INODELOCK_XATTR);
1327 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1328 mdt_object_child(o2), msl->msl_flags);
1332 mdt_swap_lov_flag(o1, o2);
1335 mdt_object_unlock(info, o2, lh2, rc);
1337 mdt_object_unlock(info, o1, lh1, rc);
1339 mdt_object_put(info->mti_env, o);
1341 mdt_thread_info_fini(info);
1345 static int mdt_raw_lookup(struct mdt_thread_info *info,
1346 struct mdt_object *parent,
1347 const struct lu_name *lname,
1348 struct ldlm_reply *ldlm_rep)
1350 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1354 LASSERT(!info->mti_cross_ref);
1356 /* Only got the fid of this obj by name */
1357 fid_zero(child_fid);
1358 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1359 lname, child_fid, &info->mti_spec);
1361 struct mdt_body *repbody;
1363 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1364 repbody->mbo_fid1 = *child_fid;
1365 repbody->mbo_valid = OBD_MD_FLID;
1366 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1367 } else if (rc == -ENOENT) {
1368 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1375 * UPDATE lock should be taken against parent, and be released before exit;
1376 * child_bits lock should be taken against child, and be returned back:
1377 * (1)normal request should release the child lock;
1378 * (2)intent request will grant the lock to client.
1380 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1381 struct mdt_lock_handle *lhc,
1383 struct ldlm_reply *ldlm_rep)
1385 struct ptlrpc_request *req = mdt_info_req(info);
1386 struct mdt_body *reqbody = NULL;
1387 struct mdt_object *parent = info->mti_object;
1388 struct mdt_object *child;
1389 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1390 struct lu_name *lname = NULL;
1391 struct mdt_lock_handle *lhp = NULL;
1392 struct ldlm_lock *lock;
1399 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1400 LASSERT(ergo(is_resent,
1401 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1406 if (info->mti_cross_ref) {
1407 /* Only getattr on the child. Parent is on another node. */
1408 mdt_set_disposition(info, ldlm_rep,
1409 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1411 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1413 PFID(mdt_object_fid(child)), ldlm_rep);
1415 rc = mdt_check_resent_lock(info, child, lhc);
1418 } else if (rc > 0) {
1419 mdt_lock_handle_init(lhc);
1420 mdt_lock_reg_init(lhc, LCK_PR);
1423 * Object's name is on another MDS, no lookup or layout
1424 * lock is needed here but update lock is.
1426 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1427 MDS_INODELOCK_LAYOUT);
1428 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1430 rc = mdt_object_lock(info, child, lhc, child_bits);
1435 /* Finally, we can get attr for child. */
1436 if (!mdt_object_exists(child)) {
1437 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1439 "remote object doesn't exist.");
1440 mdt_object_unlock(info, child, lhc, 1);
1444 rc = mdt_getattr_internal(info, child, 0);
1445 if (unlikely(rc != 0))
1446 mdt_object_unlock(info, child, lhc, 1);
1451 lname = &info->mti_name;
1452 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1454 if (lu_name_is_valid(lname)) {
1455 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1456 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1457 PNAME(lname), ldlm_rep);
1459 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1460 if (unlikely(reqbody == NULL))
1461 RETURN(err_serious(-EPROTO));
1463 *child_fid = reqbody->mbo_fid2;
1465 if (unlikely(!fid_is_sane(child_fid)))
1466 RETURN(err_serious(-EINVAL));
1468 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1470 PFID(mdt_object_fid(parent)),
1471 PFID(&reqbody->mbo_fid2), ldlm_rep);
1474 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1476 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1477 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1479 "Parent doesn't exist!");
1483 if (mdt_object_remote(parent)) {
1484 CERROR("%s: parent "DFID" is on remote target\n",
1485 mdt_obd_name(info->mti_mdt),
1486 PFID(mdt_object_fid(parent)));
1490 if (lu_name_is_valid(lname)) {
1491 /* Always allow to lookup ".." */
1492 if (unlikely(lname->ln_namelen == 2 &&
1493 lname->ln_name[0] == '.' &&
1494 lname->ln_name[1] == '.'))
1495 info->mti_spec.sp_permitted = 1;
1497 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1498 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1503 /* step 1: lock parent only if parent is a directory */
1504 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1505 lhp = &info->mti_lh[MDT_LH_PARENT];
1506 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1507 rc = mdt_object_lock(info, parent, lhp,
1508 MDS_INODELOCK_UPDATE);
1509 if (unlikely(rc != 0))
1513 /* step 2: lookup child's fid by name */
1514 fid_zero(child_fid);
1515 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1516 child_fid, &info->mti_spec);
1518 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1521 GOTO(out_parent, rc);
1524 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1527 *step 3: find the child object by fid & lock it.
1528 * regardless if it is local or remote.
1530 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1531 * set parent dir fid the same as child fid in getattr by fid case
1532 * we should not lu_object_find() the object again, could lead
1533 * to hung if there is a concurrent unlink destroyed the object.
1535 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1536 mdt_object_get(info->mti_env, parent);
1539 child = mdt_object_find(info->mti_env, info->mti_mdt,
1543 if (unlikely(IS_ERR(child)))
1544 GOTO(out_parent, rc = PTR_ERR(child));
1546 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1547 if (!mdt_object_exists(child)) {
1548 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1550 "Object doesn't exist!");
1551 GOTO(out_child, rc = -ENOENT);
1554 rc = mdt_check_resent_lock(info, child, lhc);
1556 GOTO(out_child, rc);
1557 } else if (rc > 0) {
1558 mdt_lock_handle_init(lhc);
1559 mdt_lock_reg_init(lhc, LCK_PR);
1562 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1563 mdt_object_exists(child) && !mdt_object_remote(child)) {
1564 struct md_attr *ma = &info->mti_attr;
1567 ma->ma_need = MA_INODE;
1568 rc = mdt_attr_get_complex(info, child, ma);
1569 if (unlikely(rc != 0))
1570 GOTO(out_child, rc);
1572 /* If the file has not been changed for some time, we
1573 * return not only a LOOKUP lock, but also an UPDATE
1574 * lock and this might save us RPC on later STAT. For
1575 * directories, it also let negative dentry cache start
1576 * working for this dir. */
1577 if (ma->ma_valid & MA_INODE &&
1578 ma->ma_attr.la_valid & LA_CTIME &&
1579 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1580 ma->ma_attr.la_ctime < cfs_time_current_sec())
1581 child_bits |= MDS_INODELOCK_UPDATE;
1584 /* layout lock must be granted in a best-effort way
1585 * for IT operations */
1586 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1587 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1588 exp_connect_layout(info->mti_exp) &&
1589 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1590 !mdt_object_remote(child) && ldlm_rep != NULL) {
1591 /* try to grant layout lock for regular file. */
1597 child_bits |= MDS_INODELOCK_LAYOUT;
1598 /* try layout lock, it may fail to be granted due to
1599 * contention at LOOKUP or UPDATE */
1600 if (!mdt_object_lock_try(info, child, lhc,
1602 child_bits &= ~MDS_INODELOCK_LAYOUT;
1603 LASSERT(child_bits != 0);
1604 rc = mdt_object_lock(info, child, lhc,
1610 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1611 * client will enqueue the lock to the remote MDT */
1612 if (mdt_object_remote(child))
1613 child_bits &= ~MDS_INODELOCK_UPDATE;
1614 rc = mdt_object_lock(info, child, lhc, child_bits);
1616 if (unlikely(rc != 0))
1617 GOTO(out_child, rc);
1620 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1622 /* finally, we can get attr for child. */
1623 rc = mdt_getattr_internal(info, child, ma_need);
1624 if (unlikely(rc != 0)) {
1625 mdt_object_unlock(info, child, lhc, 1);
1627 /* Debugging code. */
1628 LDLM_DEBUG(lock, "Returning lock to client");
1629 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1630 &lock->l_resource->lr_name),
1631 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1632 PLDLMRES(lock->l_resource),
1633 PFID(mdt_object_fid(child)));
1636 LDLM_LOCK_PUT(lock);
1640 mdt_object_put(info->mti_env, child);
1643 mdt_object_unlock(info, parent, lhp, 1);
1647 /* normal handler: should release the child lock */
1648 static int mdt_getattr_name(struct tgt_session_info *tsi)
1650 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1651 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1652 struct mdt_body *reqbody;
1653 struct mdt_body *repbody;
1657 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1658 LASSERT(reqbody != NULL);
1659 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1660 LASSERT(repbody != NULL);
1662 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1663 repbody->mbo_eadatasize = 0;
1664 repbody->mbo_aclsize = 0;
1666 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1668 GOTO(out_shrink, rc);
1670 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1671 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1672 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1673 lhc->mlh_reg_lh.cookie = 0;
1675 mdt_exit_ucred(info);
1678 mdt_client_compatibility(info);
1679 rc2 = mdt_fix_reply(info);
1682 mdt_thread_info_fini(info);
1686 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1687 void *karg, void __user *uarg);
1689 static int mdt_set_info(struct tgt_session_info *tsi)
1691 struct ptlrpc_request *req = tgt_ses_req(tsi);
1694 int keylen, vallen, rc = 0;
1698 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1700 DEBUG_REQ(D_HA, req, "no set_info key");
1701 RETURN(err_serious(-EFAULT));
1704 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1707 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1709 DEBUG_REQ(D_HA, req, "no set_info val");
1710 RETURN(err_serious(-EFAULT));
1713 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1716 /* Swab any part of val you need to here */
1717 if (KEY_IS(KEY_READ_ONLY)) {
1718 spin_lock(&req->rq_export->exp_lock);
1720 *exp_connect_flags_ptr(req->rq_export) |=
1723 *exp_connect_flags_ptr(req->rq_export) &=
1724 ~OBD_CONNECT_RDONLY;
1725 spin_unlock(&req->rq_export->exp_lock);
1726 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1727 struct changelog_setinfo *cs = val;
1729 if (vallen != sizeof(*cs)) {
1730 CERROR("%s: bad changelog_clear setinfo size %d\n",
1731 tgt_name(tsi->tsi_tgt), vallen);
1734 if (ptlrpc_req_need_swab(req)) {
1735 __swab64s(&cs->cs_recno);
1736 __swab32s(&cs->cs_id);
1739 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1741 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1743 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1750 static int mdt_readpage(struct tgt_session_info *tsi)
1752 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1753 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1754 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1755 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1756 struct mdt_body *repbody;
1762 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1763 RETURN(err_serious(-ENOMEM));
1765 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1766 if (repbody == NULL || reqbody == NULL)
1767 RETURN(err_serious(-EFAULT));
1770 * prepare @rdpg before calling lower layers and transfer itself. Here
1771 * reqbody->size contains offset of where to start to read and
1772 * reqbody->nlink contains number bytes to read.
1774 rdpg->rp_hash = reqbody->mbo_size;
1775 if (rdpg->rp_hash != reqbody->mbo_size) {
1776 CERROR("Invalid hash: %#llx != %#llx\n",
1777 rdpg->rp_hash, reqbody->mbo_size);
1781 rdpg->rp_attrs = reqbody->mbo_mode;
1782 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1783 rdpg->rp_attrs |= LUDA_64BITHASH;
1784 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1785 exp_max_brw_size(tsi->tsi_exp));
1786 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1788 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1789 if (rdpg->rp_pages == NULL)
1792 for (i = 0; i < rdpg->rp_npages; ++i) {
1793 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1794 if (rdpg->rp_pages[i] == NULL)
1795 GOTO(free_rdpg, rc = -ENOMEM);
1798 /* call lower layers to fill allocated pages with directory data */
1799 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1801 GOTO(free_rdpg, rc);
1803 /* send pages to client */
1804 rc = tgt_sendpage(tsi, rdpg, rc);
1809 for (i = 0; i < rdpg->rp_npages; i++)
1810 if (rdpg->rp_pages[i] != NULL)
1811 __free_page(rdpg->rp_pages[i]);
1812 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1814 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1820 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1822 struct lu_ucred *uc = mdt_ucred_check(info);
1823 struct lu_attr *attr = &info->mti_attr.ma_attr;
1828 if (op != REINT_SETATTR) {
1829 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1830 attr->la_uid = uc->uc_fsuid;
1831 /* for S_ISGID, inherit gid from his parent, such work will be
1832 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1833 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1834 attr->la_gid = uc->uc_fsgid;
1840 static int mdt_reint_internal(struct mdt_thread_info *info,
1841 struct mdt_lock_handle *lhc,
1844 struct req_capsule *pill = info->mti_pill;
1845 struct mdt_body *repbody;
1850 rc = mdt_reint_unpack(info, op);
1852 CERROR("Can't unpack reint, rc %d\n", rc);
1853 RETURN(err_serious(rc));
1856 /* for replay (no_create) lmm is not needed, client has it already */
1857 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1858 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1861 /* llog cookies are always 0, the field is kept for compatibility */
1862 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1863 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1865 rc = req_capsule_server_pack(pill);
1867 CERROR("Can't pack response, rc %d\n", rc);
1868 RETURN(err_serious(rc));
1871 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1872 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1874 repbody->mbo_eadatasize = 0;
1875 repbody->mbo_aclsize = 0;
1878 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1880 /* for replay no cookkie / lmm need, because client have this already */
1881 if (info->mti_spec.no_create)
1882 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1883 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1885 rc = mdt_init_ucred_reint(info);
1887 GOTO(out_shrink, rc);
1889 rc = mdt_fix_attr_ucred(info, op);
1891 GOTO(out_ucred, rc = err_serious(rc));
1893 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1895 GOTO(out_ucred, rc);
1896 } else if (rc == 1) {
1897 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1898 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1899 GOTO(out_ucred, rc);
1901 rc = mdt_reint_rec(info, lhc);
1904 mdt_exit_ucred(info);
1906 mdt_client_compatibility(info);
1907 rc2 = mdt_fix_reply(info);
1913 static long mdt_reint_opcode(struct ptlrpc_request *req,
1914 const struct req_format **fmt)
1916 struct mdt_device *mdt;
1917 struct mdt_rec_reint *rec;
1920 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1922 opc = rec->rr_opcode;
1923 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1924 if (opc < REINT_MAX && fmt[opc] != NULL)
1925 req_capsule_extend(&req->rq_pill, fmt[opc]);
1927 mdt = mdt_exp2dev(req->rq_export);
1928 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1929 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1930 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1931 opc = err_serious(-EFAULT);
1934 opc = err_serious(-EFAULT);
1939 static int mdt_reint(struct tgt_session_info *tsi)
1943 static const struct req_format *reint_fmts[REINT_MAX] = {
1944 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1945 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1946 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1947 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1948 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1949 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1950 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1951 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1952 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1957 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1959 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1961 * No lock possible here from client to pass it to reint code
1964 rc = mdt_reint_internal(info, NULL, opc);
1965 mdt_thread_info_fini(info);
1970 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1974 /* this should sync the whole device */
1975 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1977 struct dt_device *dt = mdt->mdt_bottom;
1981 rc = dt->dd_ops->dt_sync(env, dt);
1985 /* this should sync this object */
1986 static int mdt_object_sync(struct mdt_thread_info *info)
1988 struct md_object *next;
1992 if (!mdt_object_exists(info->mti_object)) {
1993 CWARN("%s: non existing object "DFID": rc = %d\n",
1994 mdt_obd_name(info->mti_mdt),
1995 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1998 next = mdt_object_child(info->mti_object);
1999 rc = mo_object_sync(info->mti_env, next);
2004 static int mdt_sync(struct tgt_session_info *tsi)
2006 struct ptlrpc_request *req = tgt_ses_req(tsi);
2007 struct req_capsule *pill = tsi->tsi_pill;
2008 struct mdt_body *body;
2013 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2014 RETURN(err_serious(-ENOMEM));
2016 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2017 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2019 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2021 /* sync an object */
2022 rc = mdt_object_sync(info);
2024 const struct lu_fid *fid;
2025 struct lu_attr *la = &info->mti_attr.ma_attr;
2027 info->mti_attr.ma_need = MA_INODE;
2028 info->mti_attr.ma_valid = 0;
2029 rc = mdt_attr_get_complex(info, info->mti_object,
2032 body = req_capsule_server_get(pill,
2034 fid = mdt_object_fid(info->mti_object);
2035 mdt_pack_attr2body(info, body, la, fid);
2038 mdt_thread_info_fini(info);
2041 mdt_counter_incr(req, LPROC_MDT_SYNC);
2047 * Handle quota control requests to consult current usage/limit, but also
2048 * to configure quota enforcement
2050 static int mdt_quotactl(struct tgt_session_info *tsi)
2052 struct obd_export *exp = tsi->tsi_exp;
2053 struct req_capsule *pill = tsi->tsi_pill;
2054 struct obd_quotactl *oqctl, *repoqc;
2056 struct mdt_device *mdt = mdt_exp2dev(exp);
2057 struct lu_device *qmt = mdt->mdt_qmt_dev;
2058 struct lu_nodemap *nodemap;
2061 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2063 RETURN(err_serious(-EPROTO));
2065 rc = req_capsule_server_pack(pill);
2067 RETURN(err_serious(rc));
2069 nodemap = nodemap_get_from_exp(exp);
2070 if (IS_ERR(nodemap))
2071 RETURN(PTR_ERR(nodemap));
2073 switch (oqctl->qc_cmd) {
2074 /* master quotactl */
2077 if (!nodemap_can_setquota(nodemap))
2078 GOTO(out_nodemap, rc = -EPERM);
2082 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2083 /* slave quotactl */
2088 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2089 GOTO(out_nodemap, rc = -EFAULT);
2093 if (oqctl->qc_type == USRQUOTA)
2094 id = nodemap_map_id(nodemap, NODEMAP_UID,
2095 NODEMAP_CLIENT_TO_FS, id);
2096 else if (oqctl->qc_type == GRPQUOTA)
2097 id = nodemap_map_id(nodemap, NODEMAP_UID,
2098 NODEMAP_CLIENT_TO_FS, id);
2100 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2102 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2104 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2105 barrier_exit(tsi->tsi_tgt->lut_bottom);
2107 if (oqctl->qc_id != id)
2108 swap(oqctl->qc_id, id);
2110 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2111 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2112 RETURN(-EINPROGRESS);
2115 switch (oqctl->qc_cmd) {
2121 /* forward quotactl request to QMT */
2122 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2127 /* slave quotactl */
2128 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2133 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2134 GOTO(out_nodemap, rc = -EFAULT);
2137 if (oqctl->qc_id != id)
2138 swap(oqctl->qc_id, id);
2145 nodemap_putref(nodemap);
2150 /** clone llog ctxt from child (mdd)
2151 * This allows remote llog (replicator) access.
2152 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2153 * context was originally set up, or we can handle them directly.
2154 * I choose the latter, but that means I need any llog
2155 * contexts set up by child to be accessable by the mdt. So we clone the
2156 * context into our context list here.
2158 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2161 struct md_device *next = mdt->mdt_child;
2162 struct llog_ctxt *ctxt;
2165 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2168 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2169 if (rc || ctxt == NULL) {
2173 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2175 CERROR("Can't set mdt ctxt %d\n", rc);
2180 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2181 struct mdt_device *mdt, int idx)
2183 struct llog_ctxt *ctxt;
2185 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2188 /* Put once for the get we just did, and once for the clone */
2189 llog_ctxt_put(ctxt);
2190 llog_ctxt_put(ctxt);
2195 * sec context handlers
2197 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2199 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2205 * quota request handlers
2207 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2209 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2210 struct lu_device *qmt = mdt->mdt_qmt_dev;
2215 RETURN(err_serious(-EOPNOTSUPP));
2217 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2221 struct mdt_object *mdt_object_new(const struct lu_env *env,
2222 struct mdt_device *d,
2223 const struct lu_fid *f)
2225 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2226 struct lu_object *o;
2227 struct mdt_object *m;
2230 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2231 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2232 if (unlikely(IS_ERR(o)))
2233 m = (struct mdt_object *)o;
2239 struct mdt_object *mdt_object_find(const struct lu_env *env,
2240 struct mdt_device *d,
2241 const struct lu_fid *f)
2243 struct lu_object *o;
2244 struct mdt_object *m;
2247 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2248 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2249 if (unlikely(IS_ERR(o)))
2250 m = (struct mdt_object *)o;
2258 * Asyncronous commit for mdt device.
2260 * Pass asynchonous commit call down the MDS stack.
2262 * \param env environment
2263 * \param mdt the mdt device
2265 static void mdt_device_commit_async(const struct lu_env *env,
2266 struct mdt_device *mdt)
2268 struct dt_device *dt = mdt->mdt_bottom;
2272 rc = dt->dd_ops->dt_commit_async(env, dt);
2273 if (unlikely(rc != 0))
2274 CWARN("%s: async commit start failed: rc = %d\n",
2275 mdt_obd_name(mdt), rc);
2276 atomic_inc(&mdt->mdt_async_commit_count);
2281 * Mark the lock as "synchonous".
2283 * Mark the lock to deffer transaction commit to the unlock time.
2285 * \param lock the lock to mark as "synchonous"
2287 * \see mdt_is_lock_sync
2288 * \see mdt_save_lock
2290 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2292 lock->l_ast_data = (void*)1;
2296 * Check whehter the lock "synchonous" or not.
2298 * \param lock the lock to check
2299 * \retval 1 the lock is "synchonous"
2300 * \retval 0 the lock isn't "synchronous"
2302 * \see mdt_set_lock_sync
2303 * \see mdt_save_lock
2305 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2307 return lock->l_ast_data != NULL;
2311 * Blocking AST for mdt locks.
2313 * Starts transaction commit if in case of COS lock conflict or
2314 * deffers such a commit to the mdt_save_lock.
2316 * \param lock the lock which blocks a request or cancelling lock
2317 * \param desc unused
2318 * \param data unused
2319 * \param flag indicates whether this cancelling or blocking callback
2321 * \see ldlm_blocking_ast_nocheck
2323 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2324 void *data, int flag)
2326 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2327 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2331 if (flag == LDLM_CB_CANCELING)
2334 lock_res_and_lock(lock);
2335 if (lock->l_blocking_ast != mdt_blocking_ast) {
2336 unlock_res_and_lock(lock);
2339 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2340 lock->l_blocking_lock != NULL) {
2341 if (mdt_cos_is_enabled(mdt) &&
2342 lock->l_client_cookie !=
2343 lock->l_blocking_lock->l_client_cookie)
2344 mdt_set_lock_sync(lock);
2345 else if (mdt_slc_is_enabled(mdt) &&
2346 ldlm_is_cos_incompat(lock->l_blocking_lock))
2347 mdt_set_lock_sync(lock);
2349 rc = ldlm_blocking_ast_nocheck(lock);
2351 /* There is no lock conflict if l_blocking_lock == NULL,
2352 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2353 * when the last reference to a local lock was released */
2354 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2357 rc = lu_env_init(&env, LCT_LOCAL);
2358 if (unlikely(rc != 0))
2359 CWARN("%s: lu_env initialization failed, cannot "
2360 "start asynchronous commit: rc = %d\n",
2363 mdt_device_commit_async(&env, mdt);
2370 * Blocking AST for cross-MDT lock
2372 * Discard lock from uncommitted_slc_locks and cancel it.
2374 * \param lock the lock which blocks a request or cancelling lock
2375 * \param desc unused
2376 * \param data unused
2377 * \param flag indicates whether this cancelling or blocking callback
2378 * \retval 0 on success
2379 * \retval negative number on error
2381 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2382 void *data, int flag)
2388 case LDLM_CB_BLOCKING: {
2389 struct lustre_handle lockh;
2391 ldlm_lock2handle(lock, &lockh);
2392 rc = ldlm_cli_cancel(&lockh,
2393 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2395 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2400 case LDLM_CB_CANCELING: {
2401 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2402 struct mdt_device *mdt =
2403 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2405 LDLM_DEBUG(lock, "Revoke remote lock\n");
2407 /* discard slc lock here so that it can be cleaned anytime,
2408 * especially for cleanup_resource() */
2409 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2411 /* once we cache lock, l_ast_data is set to mdt_object */
2412 if (lock->l_ast_data != NULL) {
2413 struct mdt_object *mo = lock->l_ast_data;
2416 rc = lu_env_init(&env, LCT_MD_THREAD);
2417 if (unlikely(rc != 0)) {
2418 CWARN("%s: lu_env initialization failed, object"
2419 "%p "DFID" is leaked!\n",
2421 PFID(mdt_object_fid(mo)));
2425 if (lock->l_policy_data.l_inodebits.bits &
2426 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2427 rc = mo_invalidate(&env, mdt_object_child(mo));
2428 mo->mot_cache_attr = 0;
2430 mdt_object_put(&env, mo);
2442 int mdt_check_resent_lock(struct mdt_thread_info *info,
2443 struct mdt_object *mo,
2444 struct mdt_lock_handle *lhc)
2446 /* the lock might already be gotten in ldlm_handle_enqueue() */
2447 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2448 struct ptlrpc_request *req = mdt_info_req(info);
2449 struct ldlm_lock *lock;
2451 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2452 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2454 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2455 * a resend case, however, it could be already destroyed
2456 * due to client eviction or a raced cancel RPC. */
2457 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2458 lhc->mlh_reg_lh.cookie);
2462 if (!fid_res_name_eq(mdt_object_fid(mo),
2463 &lock->l_resource->lr_name)) {
2464 CWARN("%s: Although resent, but still not "
2465 "get child lock:"DFID"\n",
2466 info->mti_exp->exp_obd->obd_name,
2467 PFID(mdt_object_fid(mo)));
2468 LDLM_LOCK_PUT(lock);
2471 LDLM_LOCK_PUT(lock);
2477 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2478 const struct lu_fid *fid, struct lustre_handle *lh,
2479 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2482 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2483 union ldlm_policy_data *policy = &mti->mti_policy;
2484 struct ldlm_res_id *res_id = &mti->mti_res_id;
2488 LASSERT(mdt_object_remote(o));
2490 fid_build_reg_res_name(fid, res_id);
2492 memset(einfo, 0, sizeof(*einfo));
2493 einfo->ei_type = LDLM_IBITS;
2494 einfo->ei_mode = mode;
2495 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2496 einfo->ei_cb_cp = ldlm_completion_ast;
2497 einfo->ei_enq_slave = 0;
2498 einfo->ei_res_id = res_id;
2500 einfo->ei_nonblock = 1;
2503 * if we cache lock, couple lock with mdt_object, so that object
2504 * can be easily found in lock ASTs.
2506 mdt_object_get(mti->mti_env, o);
2507 einfo->ei_cbdata = o;
2510 memset(policy, 0, sizeof(*policy));
2511 policy->l_inodebits.bits = ibits;
2513 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2515 if (rc < 0 && cache) {
2516 mdt_object_put(mti->mti_env, o);
2517 einfo->ei_cbdata = NULL;
2522 static int mdt_object_local_lock(struct mdt_thread_info *info,
2523 struct mdt_object *o,
2524 struct mdt_lock_handle *lh, __u64 ibits,
2525 bool nonblock, bool cos_incompat)
2527 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2528 union ldlm_policy_data *policy = &info->mti_policy;
2529 struct ldlm_res_id *res_id = &info->mti_res_id;
2534 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2535 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2536 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2537 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2540 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2541 lh->mlh_reg_mode == LCK_EX);
2542 dlmflags |= LDLM_FL_COS_INCOMPAT;
2543 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2544 dlmflags |= LDLM_FL_COS_ENABLED;
2547 /* Only enqueue LOOKUP lock for remote object */
2548 if (mdt_object_remote(o))
2549 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2551 if (lh->mlh_type == MDT_PDO_LOCK) {
2552 /* check for exists after object is locked */
2553 if (mdt_object_exists(o) == 0) {
2554 /* Non-existent object shouldn't have PDO lock */
2557 /* Non-dir object shouldn't have PDO lock */
2558 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2563 memset(policy, 0, sizeof(*policy));
2564 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2566 dlmflags |= LDLM_FL_ATOMIC_CB;
2568 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2571 * Take PDO lock on whole directory and build correct @res_id for lock
2572 * on part of directory.
2574 if (lh->mlh_pdo_hash != 0) {
2575 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2576 mdt_lock_pdo_mode(info, o, lh);
2577 if (lh->mlh_pdo_mode != LCK_NL) {
2579 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2580 * is never going to be sent to client and we do not
2581 * want it slowed down due to possible cancels.
2583 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2584 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2585 policy, res_id, dlmflags,
2586 info->mti_exp == NULL ? NULL :
2587 &info->mti_exp->exp_handle.h_cookie);
2588 if (unlikely(rc != 0))
2589 GOTO(out_unlock, rc);
2593 * Finish res_id initializing by name hash marking part of
2594 * directory which is taking modification.
2596 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2599 policy->l_inodebits.bits = ibits;
2602 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2603 * going to be sent to client. If it is - mdt_intent_policy() path will
2604 * fix it up and turn FL_LOCAL flag off.
2606 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2607 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2608 info->mti_exp == NULL ? NULL :
2609 &info->mti_exp->exp_handle.h_cookie);
2612 mdt_object_unlock(info, o, lh, 1);
2613 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2614 lh->mlh_pdo_hash != 0 &&
2615 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2616 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2622 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2623 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2626 struct mdt_lock_handle *local_lh = NULL;
2630 if (!mdt_object_remote(o)) {
2631 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2636 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2637 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2638 MDS_INODELOCK_XATTR);
2640 /* Only enqueue LOOKUP lock for remote object */
2641 if (ibits & MDS_INODELOCK_LOOKUP) {
2642 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2643 nonblock, cos_incompat);
2650 if (ibits & MDS_INODELOCK_UPDATE) {
2651 /* Sigh, PDO needs to enqueue 2 locks right now, but
2652 * enqueue RPC can only request 1 lock, to avoid extra
2653 * RPC, so it will instead enqueue EX lock for remote
2654 * object anyway XXX*/
2655 if (lh->mlh_type == MDT_PDO_LOCK &&
2656 lh->mlh_pdo_hash != 0) {
2657 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2658 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2659 PFID(mdt_object_fid(o)));
2660 lh->mlh_pdo_hash = 0;
2661 lh->mlh_rreg_mode = LCK_EX;
2662 lh->mlh_type = MDT_REG_LOCK;
2664 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2667 MDS_INODELOCK_UPDATE, nonblock,
2669 if (rc != ELDLM_OK) {
2670 if (local_lh != NULL)
2671 mdt_object_unlock(info, o, local_lh, rc);
2679 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2680 struct mdt_lock_handle *lh, __u64 ibits)
2682 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2685 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2686 struct mdt_lock_handle *lh, __u64 ibits,
2689 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2690 return mdt_object_lock_internal(info, o, lh, ibits, false,
2694 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2695 struct mdt_lock_handle *lh, __u64 ibits)
2697 struct mdt_lock_handle tmp = *lh;
2700 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2707 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2708 struct mdt_object *o, struct mdt_lock_handle *lh,
2709 __u64 ibits, bool cos_incompat)
2711 struct mdt_lock_handle tmp = *lh;
2714 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2715 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2723 * Save a lock within request object.
2725 * Keep the lock referenced until whether client ACK or transaction
2726 * commit happens or release the lock immediately depending on input
2727 * parameters. If COS is ON, a write lock is converted to COS lock
2730 * \param info thead info object
2731 * \param h lock handle
2732 * \param mode lock mode
2733 * \param decref force immediate lock releasing
2735 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2736 enum ldlm_mode mode, int decref)
2740 if (lustre_handle_is_used(h)) {
2741 if (decref || !info->mti_has_trans ||
2742 !(mode & (LCK_PW | LCK_EX))) {
2743 mdt_fid_unlock(h, mode);
2745 struct mdt_device *mdt = info->mti_mdt;
2746 struct ldlm_lock *lock = ldlm_handle2lock(h);
2747 struct ptlrpc_request *req = mdt_info_req(info);
2750 cos = (mdt_cos_is_enabled(mdt) ||
2751 mdt_slc_is_enabled(mdt));
2753 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2756 /* there is no request if mdt_object_unlock() is called
2757 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2758 if (likely(req != NULL)) {
2759 CDEBUG(D_HA, "request = %p reply state = %p"
2760 " transno = %lld\n", req,
2761 req->rq_reply_state, req->rq_transno);
2763 ldlm_lock_downgrade(lock, LCK_COS);
2766 ptlrpc_save_lock(req, h, mode, cos);
2768 mdt_fid_unlock(h, mode);
2770 if (mdt_is_lock_sync(lock)) {
2771 CDEBUG(D_HA, "found sync-lock,"
2772 " async commit started\n");
2773 mdt_device_commit_async(info->mti_env,
2776 LDLM_LOCK_PUT(lock);
2785 * Save cross-MDT lock in uncommitted_slc_locks
2787 * Keep the lock referenced until transaction commit happens or release the lock
2788 * immediately depending on input parameters.
2790 * \param info thead info object
2791 * \param h lock handle
2792 * \param mode lock mode
2793 * \param decref force immediate lock releasing
2795 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2796 struct mdt_object *o, struct lustre_handle *h,
2797 enum ldlm_mode mode, int decref)
2801 if (lustre_handle_is_used(h)) {
2802 struct ldlm_lock *lock = ldlm_handle2lock(h);
2805 (lock->l_policy_data.l_inodebits.bits &
2806 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2807 mo_invalidate(info->mti_env, mdt_object_child(o));
2809 if (decref || !info->mti_has_trans ||
2810 !(mode & (LCK_PW | LCK_EX))) {
2811 ldlm_lock_decref_and_cancel(h, mode);
2812 LDLM_LOCK_PUT(lock);
2814 struct ptlrpc_request *req = mdt_info_req(info);
2816 LASSERT(req != NULL);
2817 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2819 ldlm_lock_decref(h, mode);
2828 * Unlock mdt object.
2830 * Immeditely release the regular lock and the PDO lock or save the
2831 * lock in request and keep them referenced until client ACK or
2832 * transaction commit.
2834 * \param info thread info object
2835 * \param o mdt object
2836 * \param lh mdt lock handle referencing regular and PDO locks
2837 * \param decref force immediate lock releasing
2839 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2841 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2842 struct mdt_lock_handle *lh, int decref)
2846 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2847 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2848 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2854 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2855 const struct lu_fid *f,
2856 struct mdt_lock_handle *lh,
2859 struct mdt_object *o;
2861 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2865 rc = mdt_object_lock(info, o, lh, ibits);
2867 mdt_object_put(info->mti_env, o);
2874 void mdt_object_unlock_put(struct mdt_thread_info * info,
2875 struct mdt_object * o,
2876 struct mdt_lock_handle *lh,
2879 mdt_object_unlock(info, o, lh, decref);
2880 mdt_object_put(info->mti_env, o);
2884 * Generic code handling requests that have struct mdt_body passed in:
2886 * - extract mdt_body from request and save it in @info, if present;
2888 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2891 * - if HABEO_CORPUS flag is set for this request type check whether object
2892 * actually exists on storage (lu_object_exists()).
2895 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2897 const struct mdt_body *body;
2898 struct mdt_object *obj;
2899 const struct lu_env *env;
2900 struct req_capsule *pill;
2904 env = info->mti_env;
2905 pill = info->mti_pill;
2907 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2911 if (!(body->mbo_valid & OBD_MD_FLID))
2914 if (!fid_is_sane(&body->mbo_fid1)) {
2915 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2919 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2921 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2922 mdt_object_put(env, obj);
2925 info->mti_object = obj;
2934 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2936 struct req_capsule *pill = info->mti_pill;
2940 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2941 rc = mdt_body_unpack(info, flags);
2945 if (rc == 0 && (flags & HABEO_REFERO)) {
2947 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2948 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2950 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2951 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2954 rc = req_capsule_server_pack(pill);
2959 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2961 lh->mlh_type = MDT_NUL_LOCK;
2962 lh->mlh_reg_lh.cookie = 0ull;
2963 lh->mlh_reg_mode = LCK_MINMODE;
2964 lh->mlh_pdo_lh.cookie = 0ull;
2965 lh->mlh_pdo_mode = LCK_MINMODE;
2966 lh->mlh_rreg_lh.cookie = 0ull;
2967 lh->mlh_rreg_mode = LCK_MINMODE;
2970 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2972 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2973 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2977 * Initialize fields of struct mdt_thread_info. Other fields are left in
2978 * uninitialized state, because it's too expensive to zero out whole
2979 * mdt_thread_info (> 1K) on each request arrival.
2981 void mdt_thread_info_init(struct ptlrpc_request *req,
2982 struct mdt_thread_info *info)
2986 info->mti_pill = &req->rq_pill;
2989 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2990 mdt_lock_handle_init(&info->mti_lh[i]);
2992 /* mdt device: it can be NULL while CONNECT */
2993 if (req->rq_export) {
2994 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2995 info->mti_exp = req->rq_export;
2997 info->mti_mdt = NULL;
2998 info->mti_env = req->rq_svc_thread->t_env;
2999 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3001 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3002 info->mti_big_buf = LU_BUF_NULL;
3003 info->mti_body = NULL;
3004 info->mti_object = NULL;
3005 info->mti_dlm_req = NULL;
3006 info->mti_has_trans = 0;
3007 info->mti_cross_ref = 0;
3008 info->mti_opdata = 0;
3009 info->mti_big_lmm_used = 0;
3011 info->mti_spec.no_create = 0;
3012 info->mti_spec.sp_rm_entry = 0;
3013 info->mti_spec.sp_permitted = 0;
3014 info->mti_spec.sp_migrate_close = 0;
3016 info->mti_spec.u.sp_ea.eadata = NULL;
3017 info->mti_spec.u.sp_ea.eadatalen = 0;
3020 void mdt_thread_info_fini(struct mdt_thread_info *info)
3024 if (info->mti_object != NULL) {
3025 mdt_object_put(info->mti_env, info->mti_object);
3026 info->mti_object = NULL;
3029 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3030 mdt_lock_handle_fini(&info->mti_lh[i]);
3031 info->mti_env = NULL;
3032 info->mti_pill = NULL;
3033 info->mti_exp = NULL;
3035 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3036 lu_buf_free(&info->mti_big_buf);
3039 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3041 struct mdt_thread_info *mti;
3043 mti = mdt_th_info(tsi->tsi_env);
3044 LASSERT(mti != NULL);
3046 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3047 if (tsi->tsi_corpus != NULL) {
3048 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3049 lu_object_get(tsi->tsi_corpus);
3051 mti->mti_body = tsi->tsi_mdt_body;
3052 mti->mti_dlm_req = tsi->tsi_dlm_req;
3057 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3059 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3061 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3062 set_current_state(TASK_UNINTERRUPTIBLE);
3063 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3066 return tgt_connect(tsi);
3084 static int mdt_intent_getattr(enum mdt_it_code opcode,
3085 struct mdt_thread_info *info,
3086 struct ldlm_lock **,
3089 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3090 struct mdt_thread_info *info,
3091 struct ldlm_lock **lockp,
3094 static int mdt_intent_layout(enum mdt_it_code opcode,
3095 struct mdt_thread_info *info,
3096 struct ldlm_lock **,
3098 static int mdt_intent_reint(enum mdt_it_code opcode,
3099 struct mdt_thread_info *info,
3100 struct ldlm_lock **,
3103 static struct mdt_it_flavor {
3104 const struct req_format *it_fmt;
3106 int (*it_act)(enum mdt_it_code ,
3107 struct mdt_thread_info *,
3108 struct ldlm_lock **,
3111 } mdt_it_flavor[] = {
3113 .it_fmt = &RQF_LDLM_INTENT,
3114 /*.it_flags = HABEO_REFERO,*/
3116 .it_act = mdt_intent_reint,
3117 .it_reint = REINT_OPEN
3120 .it_fmt = &RQF_LDLM_INTENT,
3122 * OCREAT is not a MUTABOR request as if the file
3124 * We do the extra check of OBD_CONNECT_RDONLY in
3125 * mdt_reint_open() when we really need to create
3129 .it_act = mdt_intent_reint,
3130 .it_reint = REINT_OPEN
3133 .it_fmt = &RQF_LDLM_INTENT,
3134 .it_flags = MUTABOR,
3135 .it_act = mdt_intent_reint,
3136 .it_reint = REINT_CREATE
3138 [MDT_IT_GETATTR] = {
3139 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3140 .it_flags = HABEO_REFERO,
3141 .it_act = mdt_intent_getattr
3143 [MDT_IT_READDIR] = {
3149 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3150 .it_flags = HABEO_REFERO,
3151 .it_act = mdt_intent_getattr
3154 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3155 .it_flags = MUTABOR,
3157 .it_reint = REINT_UNLINK
3161 .it_flags = MUTABOR,
3164 [MDT_IT_GETXATTR] = {
3165 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3166 .it_flags = HABEO_CORPUS,
3167 .it_act = mdt_intent_getxattr
3170 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3172 .it_act = mdt_intent_layout
3177 mdt_intent_lock_replace(struct mdt_thread_info *info,
3178 struct ldlm_lock **lockp,
3179 struct mdt_lock_handle *lh,
3180 __u64 flags, int result)
3182 struct ptlrpc_request *req = mdt_info_req(info);
3183 struct ldlm_lock *lock = *lockp;
3184 struct ldlm_lock *new_lock;
3186 /* If possible resent found a lock, @lh is set to its handle */
3187 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3189 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3190 lh->mlh_reg_lh.cookie = 0;
3194 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3195 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3196 * a resend case, however, it could be already destroyed
3197 * due to client eviction or a raced cancel RPC. */
3198 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3199 lh->mlh_reg_lh.cookie);
3200 lh->mlh_reg_lh.cookie = 0;
3204 LASSERTF(new_lock != NULL,
3205 "lockh %#llx flags %#llx : rc = %d\n",
3206 lh->mlh_reg_lh.cookie, flags, result);
3209 * If we've already given this lock to a client once, then we should
3210 * have no readers or writers. Otherwise, we should have one reader
3211 * _or_ writer ref (which will be zeroed below) before returning the
3214 if (new_lock->l_export == req->rq_export) {
3215 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3217 LASSERT(new_lock->l_export == NULL);
3218 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3223 if (new_lock->l_export == req->rq_export) {
3225 * Already gave this to the client, which means that we
3226 * reconstructed a reply.
3228 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3231 LDLM_LOCK_RELEASE(new_lock);
3232 lh->mlh_reg_lh.cookie = 0;
3233 RETURN(ELDLM_LOCK_REPLACED);
3237 * Fixup the lock to be given to the client.
3239 lock_res_and_lock(new_lock);
3240 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3241 * possible blocking AST. */
3242 while (new_lock->l_readers > 0) {
3243 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3244 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3245 new_lock->l_readers--;
3247 while (new_lock->l_writers > 0) {
3248 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3249 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3250 new_lock->l_writers--;
3253 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3254 new_lock->l_blocking_ast = lock->l_blocking_ast;
3255 new_lock->l_completion_ast = lock->l_completion_ast;
3256 new_lock->l_remote_handle = lock->l_remote_handle;
3257 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3259 unlock_res_and_lock(new_lock);
3261 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3262 &new_lock->l_remote_handle,
3263 &new_lock->l_exp_hash);
3265 LDLM_LOCK_RELEASE(new_lock);
3266 lh->mlh_reg_lh.cookie = 0;
3268 RETURN(ELDLM_LOCK_REPLACED);
3271 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3272 struct ldlm_lock *new_lock,
3273 struct mdt_lock_handle *lh,
3276 struct ptlrpc_request *req = mdt_info_req(info);
3277 struct ldlm_request *dlmreq;
3279 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3282 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3284 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3285 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3287 if (flags & LDLM_FL_RESENT) {
3288 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3289 lh->mlh_reg_mode = new_lock->l_granted_mode;
3291 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3292 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3293 lh->mlh_reg_lh.cookie);
3298 * If the xid matches, then we know this is a resent request, and allow
3299 * it. (It's probably an OPEN, for which we don't send a lock.
3301 if (req_can_reconstruct(req, NULL))
3305 * This remote handle isn't enqueued, so we never received or processed
3306 * this request. Clear MSG_RESENT, because it can be handled like any
3307 * normal request now.
3309 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3311 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3312 dlmreq->lock_handle[0].cookie);
3315 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3316 struct mdt_thread_info *info,
3317 struct ldlm_lock **lockp,
3320 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3321 struct ldlm_reply *ldlm_rep = NULL;
3326 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3327 * (for the resend case) or a new lock. Below we will use it to
3328 * replace the original lock.
3330 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3331 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3332 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3333 rc = mdt_object_lock(info, info->mti_object, lhc,
3334 MDS_INODELOCK_XATTR);
3339 rc = mdt_getxattr(info);
3341 if (mdt_info_req(info)->rq_repmsg != NULL)
3342 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3344 if (ldlm_rep == NULL ||
3345 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3346 mdt_object_unlock(info, info->mti_object, lhc, 1);
3347 RETURN(err_serious(-EFAULT));
3350 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3352 /* This is left for interop instead of adding a new interop flag.
3354 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3355 if (ldlm_rep->lock_policy_res2) {
3356 mdt_object_unlock(info, info->mti_object, lhc, 1);
3357 RETURN(ELDLM_LOCK_ABORTED);
3361 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3365 static int mdt_intent_getattr(enum mdt_it_code opcode,
3366 struct mdt_thread_info *info,
3367 struct ldlm_lock **lockp,
3370 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3372 struct ldlm_reply *ldlm_rep;
3373 struct mdt_body *reqbody;
3374 struct mdt_body *repbody;
3378 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3381 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3384 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3385 repbody->mbo_eadatasize = 0;
3386 repbody->mbo_aclsize = 0;
3390 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3392 case MDT_IT_GETATTR:
3393 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3397 CERROR("Unsupported intent (%d)\n", opcode);
3398 GOTO(out_shrink, rc = -EINVAL);
3401 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3403 GOTO(out_shrink, rc);
3405 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3406 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3408 /* Get lock from request for possible resent case. */
3409 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3411 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3412 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3414 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3415 ldlm_rep->lock_policy_res2 = 0;
3416 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3417 ldlm_rep->lock_policy_res2) {
3418 lhc->mlh_reg_lh.cookie = 0ull;
3419 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3422 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3425 mdt_exit_ucred(info);
3427 mdt_client_compatibility(info);
3428 rc2 = mdt_fix_reply(info);
3434 static int mdt_intent_layout(enum mdt_it_code opcode,
3435 struct mdt_thread_info *info,
3436 struct ldlm_lock **lockp,
3439 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3440 struct layout_intent *layout;
3442 struct mdt_object *obj = NULL;
3443 int layout_size = 0;
3447 if (opcode != MDT_IT_LAYOUT) {
3448 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3453 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3457 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3458 CERROR("%s: Unsupported layout intent opc %d\n",
3459 mdt_obd_name(info->mti_mdt), layout->li_opc);
3463 fid = &info->mti_tmp_fid2;
3464 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3466 /* Get lock from request for possible resent case. */
3467 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3469 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3471 GOTO(out, rc = PTR_ERR(obj));
3473 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3474 layout_size = mdt_attr_get_eabuf_size(info, obj);
3475 if (layout_size < 0)
3476 GOTO(out_obj, rc = layout_size);
3478 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3479 info->mti_mdt->mdt_max_mdsize = layout_size;
3482 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3483 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3485 rc = req_capsule_server_pack(info->mti_pill);
3489 mdt_object_put(info->mti_env, obj);
3491 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3492 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3495 lhc->mlh_reg_lh.cookie = 0;
3500 static int mdt_intent_reint(enum mdt_it_code opcode,
3501 struct mdt_thread_info *info,
3502 struct ldlm_lock **lockp,
3505 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3506 struct ldlm_reply *rep = NULL;
3510 static const struct req_format *intent_fmts[REINT_MAX] = {
3511 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3512 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3517 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3521 if (mdt_it_flavor[opcode].it_reint != opc) {
3522 CERROR("Reint code %ld doesn't match intent: %d\n",
3524 RETURN(err_serious(-EPROTO));
3527 /* Get lock from request for possible resent case. */
3528 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3530 rc = mdt_reint_internal(info, lhc, opc);
3532 /* Check whether the reply has been packed successfully. */
3533 if (mdt_info_req(info)->rq_repmsg != NULL)
3534 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3536 RETURN(err_serious(-EFAULT));
3538 /* MDC expects this in any case */
3540 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3542 /* the open lock or the lock for cross-ref object should be
3543 * returned to the client */
3544 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3545 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3546 rep->lock_policy_res2 = 0;
3547 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3551 rep->lock_policy_res2 = clear_serious(rc);
3553 if (rep->lock_policy_res2 == -ENOENT &&
3554 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3555 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3556 rep->lock_policy_res2 = 0;
3558 lhc->mlh_reg_lh.cookie = 0ull;
3559 if (rc == -ENOTCONN || rc == -ENODEV ||
3560 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3562 * If it is the disconnect error (ENODEV & ENOCONN), the error
3563 * will be returned by rq_status, and client at ptlrpc layer
3564 * will detect this, then disconnect, reconnect the import
3565 * immediately, instead of impacting the following the rpc.
3570 * For other cases, the error will be returned by intent, and client
3571 * will retrieve the result from intent.
3573 RETURN(ELDLM_LOCK_ABORTED);
3576 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3584 case IT_OPEN|IT_CREAT:
3591 rc = MDT_IT_READDIR;
3594 rc = MDT_IT_GETATTR;
3606 rc = MDT_IT_GETXATTR;
3611 case IT_QUOTA_DQACQ:
3616 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3623 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3624 struct mdt_thread_info *info,
3625 struct ldlm_lock **lockp, __u64 flags)
3627 struct req_capsule *pill = info->mti_pill;
3628 struct ptlrpc_request *req = mdt_info_req(info);
3629 struct mdt_it_flavor *flv;
3634 opc = mdt_intent_code(itopc);
3638 if (opc == MDT_IT_QUOTA) {
3639 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3642 RETURN(-EOPNOTSUPP);
3644 if (mdt_rdonly(req->rq_export))
3647 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3648 /* pass the request to quota master */
3649 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3650 mdt_info_req(info), lockp,
3655 flv = &mdt_it_flavor[opc];
3656 if (flv->it_fmt != NULL)
3657 req_capsule_extend(pill, flv->it_fmt);
3659 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3663 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3666 if (flv->it_act != NULL) {
3667 struct ldlm_reply *rep;
3669 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3671 /* execute policy */
3672 rc = flv->it_act(opc, info, lockp, flags);
3674 /* Check whether the reply has been packed successfully. */
3675 if (req->rq_repmsg != NULL) {
3676 rep = req_capsule_server_get(info->mti_pill,
3678 rep->lock_policy_res2 =
3679 ptlrpc_status_hton(rep->lock_policy_res2);
3686 static int mdt_intent_policy(struct ldlm_namespace *ns,
3687 struct ldlm_lock **lockp, void *req_cookie,
3688 enum ldlm_mode mode, __u64 flags, void *data)
3690 struct tgt_session_info *tsi;
3691 struct mdt_thread_info *info;
3692 struct ptlrpc_request *req = req_cookie;
3693 struct ldlm_intent *it;
3694 struct req_capsule *pill;
3699 LASSERT(req != NULL);
3701 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3703 info = tsi2mdt_info(tsi);
3704 LASSERT(info != NULL);
3705 pill = info->mti_pill;
3706 LASSERT(pill->rc_req == req);
3708 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3709 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3710 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3712 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3716 /* Lock without inodebits makes no sense and will oops
3717 * later in ldlm. Let's check it now to see if we have
3718 * ibits corrupted somewhere in mdt_intent_opc().
3719 * The case for client miss to set ibits has been
3720 * processed by others. */
3721 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3722 lr_type == LDLM_IBITS,
3723 info->mti_dlm_req->lock_desc.\
3724 l_policy_data.l_inodebits.bits != 0));
3726 rc = err_serious(-EFAULT);
3728 /* No intent was provided */
3729 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3730 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3731 rc = req_capsule_server_pack(pill);
3733 rc = err_serious(rc);
3735 mdt_thread_info_fini(info);
3739 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3741 struct seq_server_site *ss = mdt_seq_site(mdt);
3743 if (ss->ss_node_id == 0)
3746 if (ss->ss_client_seq != NULL) {
3747 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3748 ss->ss_client_seq->lcs_exp = NULL;
3751 if (ss->ss_server_fld != NULL) {
3752 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3753 ss->ss_server_fld->lsf_control_exp = NULL;
3757 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3759 struct seq_server_site *ss = mdt_seq_site(mdt);
3764 if (ss->ss_server_seq != NULL)
3765 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3768 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3770 mdt_seq_fini_cli(mdt);
3771 mdt_deregister_seq_exp(mdt);
3773 return seq_site_fini(env, mdt_seq_site(mdt));
3777 * It will retrieve its FLDB entries from MDT0, and it only happens
3778 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3779 * and it needs to refresh FLDB from the MDT0.
3781 static int mdt_register_lwp_callback(void *data)
3784 struct mdt_device *mdt = data;
3785 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3789 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3791 rc = lu_env_init(&env, LCT_MD_THREAD);
3793 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3797 /* Allocate new sequence now to avoid creating local transaction
3798 * in the normal transaction process */
3799 rc = seq_server_check_and_alloc_super(&env,
3800 mdt_seq_site(mdt)->ss_server_seq);
3805 rc = fld_update_from_controller(&env, fld);
3807 CERROR("%s: cannot update controller: rc = %d\n",
3808 mdt_obd_name(mdt), rc);
3817 static int mdt_register_seq_exp(struct mdt_device *mdt)
3819 struct seq_server_site *ss = mdt_seq_site(mdt);
3820 char *lwp_name = NULL;
3823 if (ss->ss_node_id == 0)
3826 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3827 if (lwp_name == NULL)
3828 GOTO(out_free, rc = -ENOMEM);
3830 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3834 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3839 rc = lustre_register_lwp_item(lwp_name,
3840 &ss->ss_server_fld->lsf_control_exp,
3841 mdt_register_lwp_callback, mdt);
3843 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3844 ss->ss_client_seq->lcs_exp = NULL;
3848 if (lwp_name != NULL)
3849 OBD_FREE(lwp_name, MAX_OBD_NAME);
3855 * Init client sequence manager which is used by local MDS to talk to sequence
3856 * controller on remote node.
3858 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3860 struct seq_server_site *ss = mdt_seq_site(mdt);
3865 /* check if this is adding the first MDC and controller is not yet
3867 OBD_ALLOC_PTR(ss->ss_client_seq);
3868 if (ss->ss_client_seq == NULL)
3871 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3872 if (prefix == NULL) {
3873 OBD_FREE_PTR(ss->ss_client_seq);
3874 ss->ss_client_seq = NULL;
3878 /* Note: seq_client_fini will be called in seq_site_fini */
3879 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3880 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3881 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3883 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3885 OBD_FREE_PTR(ss->ss_client_seq);
3886 ss->ss_client_seq = NULL;
3890 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3895 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3897 struct seq_server_site *ss;
3901 ss = mdt_seq_site(mdt);
3902 /* init sequence controller server(MDT0) */
3903 if (ss->ss_node_id == 0) {
3904 OBD_ALLOC_PTR(ss->ss_control_seq);
3905 if (ss->ss_control_seq == NULL)
3908 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3909 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3912 GOTO(out_seq_fini, rc);
3915 /* Init normal sequence server */
3916 OBD_ALLOC_PTR(ss->ss_server_seq);
3917 if (ss->ss_server_seq == NULL)
3918 GOTO(out_seq_fini, rc = -ENOMEM);
3920 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3921 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3923 GOTO(out_seq_fini, rc);
3925 /* init seq client for seq server to talk to seq controller(MDT0) */
3926 rc = mdt_seq_init_cli(env, mdt);
3928 GOTO(out_seq_fini, rc);
3930 if (ss->ss_node_id != 0)
3931 /* register controller export through lwp */
3932 rc = mdt_register_seq_exp(mdt);
3937 mdt_seq_fini(env, mdt);
3945 static int mdt_fld_fini(const struct lu_env *env,
3946 struct mdt_device *m)
3948 struct seq_server_site *ss = mdt_seq_site(m);
3951 if (ss && ss->ss_server_fld) {
3952 fld_server_fini(env, ss->ss_server_fld);
3953 OBD_FREE_PTR(ss->ss_server_fld);
3954 ss->ss_server_fld = NULL;
3960 static int mdt_fld_init(const struct lu_env *env,
3962 struct mdt_device *m)
3964 struct seq_server_site *ss;
3968 ss = mdt_seq_site(m);
3970 OBD_ALLOC_PTR(ss->ss_server_fld);
3971 if (ss->ss_server_fld == NULL)
3972 RETURN(rc = -ENOMEM);
3974 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3977 OBD_FREE_PTR(ss->ss_server_fld);
3978 ss->ss_server_fld = NULL;
3985 static void mdt_stack_pre_fini(const struct lu_env *env,
3986 struct mdt_device *m, struct lu_device *top)
3988 struct lustre_cfg_bufs *bufs;
3989 struct lustre_cfg *lcfg;
3990 struct mdt_thread_info *info;
3995 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3996 LASSERT(info != NULL);
3998 bufs = &info->mti_u.bufs;
4000 LASSERT(m->mdt_child_exp);
4001 LASSERT(m->mdt_child_exp->exp_obd);
4003 /* process cleanup, pass mdt obd name to get obd umount flags */
4004 /* XXX: this is needed because all layers are referenced by
4005 * objects (some of them are pinned by osd, for example *
4006 * the proper solution should be a model where object used
4007 * by osd only doesn't have mdt/mdd slices -bzzz */
4008 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4009 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4010 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4014 top->ld_ops->ldo_process_config(env, top, lcfg);
4015 lustre_cfg_free(lcfg);
4019 static void mdt_stack_fini(const struct lu_env *env,
4020 struct mdt_device *m, struct lu_device *top)
4022 struct obd_device *obd = mdt2obd_dev(m);
4023 struct lustre_cfg_bufs *bufs;
4024 struct lustre_cfg *lcfg;
4025 struct mdt_thread_info *info;
4029 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4030 LASSERT(info != NULL);
4032 lu_dev_del_linkage(top->ld_site, top);
4034 lu_site_purge(env, top->ld_site, -1);
4036 bufs = &info->mti_u.bufs;
4037 /* process cleanup, pass mdt obd name to get obd umount flags */
4038 /* another purpose is to let all layers to release their objects */
4039 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4044 lustre_cfg_bufs_set_string(bufs, 1, flags);
4045 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4050 top->ld_ops->ldo_process_config(env, top, lcfg);
4051 lustre_cfg_free(lcfg);
4053 lu_site_purge(env, top->ld_site, -1);
4055 m->mdt_child = NULL;
4056 m->mdt_bottom = NULL;
4058 obd_disconnect(m->mdt_child_exp);
4059 m->mdt_child_exp = NULL;
4061 obd_disconnect(m->mdt_bottom_exp);
4062 m->mdt_child_exp = NULL;
4065 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4066 const char *next, struct obd_export **exp)
4068 struct obd_connect_data *data = NULL;
4069 struct obd_device *obd;
4073 OBD_ALLOC_PTR(data);
4075 GOTO(out, rc = -ENOMEM);
4077 obd = class_name2obd(next);
4079 CERROR("%s: can't locate next device: %s\n",
4080 mdt_obd_name(m), next);
4081 GOTO(out, rc = -ENOTCONN);
4084 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4085 data->ocd_version = LUSTRE_VERSION_CODE;
4087 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4089 CERROR("%s: cannot connect to next dev %s (%d)\n",
4090 mdt_obd_name(m), next, rc);
4100 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4101 struct lustre_cfg *cfg)
4103 char *dev = lustre_cfg_string(cfg, 0);
4104 int rc, name_size, uuid_size;
4105 char *name, *uuid, *p;
4106 struct lustre_cfg_bufs *bufs;
4107 struct lustre_cfg *lcfg;
4108 struct obd_device *obd;
4109 struct lustre_profile *lprof;
4110 struct lu_site *site;
4113 /* in 1.8 we had the only device in the stack - MDS.
4114 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4115 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4116 * to generate names and setup MDT, MDD. MDT will be using
4117 * generated name to connect to MDD. for MDD the next device
4118 * will be LOD with name taken from so called "profile" which
4119 * is generated by mount_option line
4121 * 1.8 MGS generates config. commands like this:
4122 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4123 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4124 * 2.0 MGS generates config. commands like this:
4125 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4126 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4127 * 3:lustre-MDT0000-mdtlov 4:f
4129 * we generate MDD name from MDT one, just replacing T with D
4131 * after all the preparations, the logical equivalent will be
4132 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4133 * 3:lustre-MDT0000-mdtlov 4:f
4134 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4135 * 3:lustre-MDD0000 4:f
4137 * notice we build the stack from down to top: MDD first, then MDT */
4139 name_size = MAX_OBD_NAME;
4140 uuid_size = MAX_OBD_NAME;
4142 OBD_ALLOC(name, name_size);
4143 OBD_ALLOC(uuid, uuid_size);
4144 if (name == NULL || uuid == NULL)
4145 GOTO(cleanup_mem, rc = -ENOMEM);
4147 OBD_ALLOC_PTR(bufs);
4149 GOTO(cleanup_mem, rc = -ENOMEM);
4152 p = strstr(name, "-MDT");
4154 GOTO(free_bufs, rc = -ENOMEM);
4157 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4159 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4160 if (lprof == NULL || lprof->lp_dt == NULL) {
4161 CERROR("can't find the profile: %s\n",
4162 lustre_cfg_string(cfg, 0));
4163 GOTO(free_bufs, rc = -EINVAL);
4166 lustre_cfg_bufs_reset(bufs, name);
4167 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4168 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4169 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4171 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4173 GOTO(put_profile, rc = -ENOMEM);
4175 rc = class_attach(lcfg);
4177 GOTO(lcfg_cleanup, rc);
4179 obd = class_name2obd(name);
4181 CERROR("Can not find obd %s (%s in config)\n",
4182 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4183 GOTO(lcfg_cleanup, rc = -EINVAL);
4186 lustre_cfg_free(lcfg);
4188 lustre_cfg_bufs_reset(bufs, name);
4189 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4190 lustre_cfg_bufs_set_string(bufs, 2, dev);
4191 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4193 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4195 GOTO(class_detach, rc = -ENOMEM);
4197 rc = class_setup(obd, lcfg);
4199 GOTO(class_detach, rc);
4201 /* connect to MDD we just setup */
4202 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4204 GOTO(class_detach, rc);
4206 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4208 LASSERT(mdt_lu_site(mdt) == NULL);
4209 mdt->mdt_lu_dev.ld_site = site;
4210 site->ls_top_dev = &mdt->mdt_lu_dev;
4211 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4213 /* now connect to bottom OSD */
4214 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4215 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4217 GOTO(class_detach, rc);
4219 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4221 rc = lu_env_refill((struct lu_env *)env);
4223 CERROR("Failure to refill session: '%d'\n", rc);
4225 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4230 class_detach(obd, lcfg);
4232 lustre_cfg_free(lcfg);
4234 class_put_profile(lprof);
4239 OBD_FREE(name, name_size);
4241 OBD_FREE(uuid, uuid_size);
4245 /* setup quota master target on MDT0 */
4246 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4247 struct lustre_cfg *cfg)
4249 struct obd_device *obd;
4250 char *dev = lustre_cfg_string(cfg, 0);
4251 char *qmtname, *uuid, *p;
4252 struct lustre_cfg_bufs *bufs;
4253 struct lustre_cfg *lcfg;
4254 struct lustre_profile *lprof;
4255 struct obd_connect_data *data;
4259 LASSERT(mdt->mdt_qmt_exp == NULL);
4260 LASSERT(mdt->mdt_qmt_dev == NULL);
4262 /* quota master is on MDT0 only for now */
4263 if (mdt->mdt_seq_site.ss_node_id != 0)
4266 /* MGS generates config commands which look as follows:
4267 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4268 * 3:lustre-MDT0000-mdtlov 4:f
4270 * We generate the QMT name from the MDT one, just replacing MD with QM
4271 * after all the preparations, the logical equivalent will be:
4272 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4273 * 3:lustre-MDT0000-osd 4:f */
4274 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4275 OBD_ALLOC(uuid, UUID_MAX);
4276 OBD_ALLOC_PTR(bufs);
4277 OBD_ALLOC_PTR(data);
4278 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4279 GOTO(cleanup_mem, rc = -ENOMEM);
4281 strcpy(qmtname, dev);
4282 p = strstr(qmtname, "-MDT");
4284 GOTO(cleanup_mem, rc = -ENOMEM);
4285 /* replace MD with QM */
4289 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4291 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4292 if (lprof == NULL || lprof->lp_dt == NULL) {
4293 CERROR("can't find profile for %s\n",
4294 lustre_cfg_string(cfg, 0));
4295 GOTO(cleanup_mem, rc = -EINVAL);
4298 lustre_cfg_bufs_reset(bufs, qmtname);
4299 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4300 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4301 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4303 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4305 GOTO(put_profile, rc = -ENOMEM);
4307 rc = class_attach(lcfg);
4309 GOTO(lcfg_cleanup, rc);
4311 obd = class_name2obd(qmtname);
4313 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4314 lustre_cfg_string(cfg, 0));
4315 GOTO(lcfg_cleanup, rc = -EINVAL);
4318 lustre_cfg_free(lcfg);
4320 lustre_cfg_bufs_reset(bufs, qmtname);
4321 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4322 lustre_cfg_bufs_set_string(bufs, 2, dev);
4324 /* for quota, the next device should be the OSD device */
4325 lustre_cfg_bufs_set_string(bufs, 3,
4326 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4328 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4330 GOTO(class_detach, rc = -ENOMEM);
4332 rc = class_setup(obd, lcfg);
4334 GOTO(class_detach, rc);
4336 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4338 /* configure local quota objects */
4339 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4343 GOTO(class_cleanup, rc);
4345 /* connect to quota master target */
4346 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4347 data->ocd_version = LUSTRE_VERSION_CODE;
4348 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4351 CERROR("cannot connect to quota master device %s (%d)\n",
4353 GOTO(class_cleanup, rc);
4359 class_manual_cleanup(obd);
4360 mdt->mdt_qmt_dev = NULL;
4364 class_detach(obd, lcfg);
4366 lustre_cfg_free(lcfg);
4368 class_put_profile(lprof);
4373 OBD_FREE(qmtname, MAX_OBD_NAME);
4375 OBD_FREE(uuid, UUID_MAX);
4381 /* Shutdown quota master target associated with mdt */
4382 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4386 if (mdt->mdt_qmt_exp == NULL)
4388 LASSERT(mdt->mdt_qmt_dev != NULL);
4390 /* the qmt automatically shuts down when the mdt disconnects */
4391 obd_disconnect(mdt->mdt_qmt_exp);
4392 mdt->mdt_qmt_exp = NULL;
4393 mdt->mdt_qmt_dev = NULL;
4397 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4398 * for now. This will be removed along with converting rest of MDT code
4399 * to use tgt_session_info */
4400 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4402 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4405 rc = mdt_getxattr(info);
4407 mdt_thread_info_fini(info);
4411 static struct tgt_handler mdt_tgt_handlers[] = {
4412 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4413 0, MDS_CONNECT, mdt_tgt_connect,
4414 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4415 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4416 0, MDS_DISCONNECT, tgt_disconnect,
4417 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4418 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4419 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4420 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4421 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4422 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4423 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4424 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4426 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4427 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4428 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4429 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4430 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4431 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4432 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4433 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4435 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4436 mdt_hsm_ct_register),
4437 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4438 mdt_hsm_ct_unregister),
4439 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4441 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4443 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4444 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4446 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4451 static struct tgt_handler mdt_sec_ctx_ops[] = {
4452 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4453 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4454 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4457 static struct tgt_handler mdt_quota_ops[] = {
4458 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4461 static struct tgt_opc_slice mdt_common_slice[] = {
4463 .tos_opc_start = MDS_FIRST_OPC,
4464 .tos_opc_end = MDS_LAST_OPC,
4465 .tos_hs = mdt_tgt_handlers
4468 .tos_opc_start = OBD_FIRST_OPC,
4469 .tos_opc_end = OBD_LAST_OPC,
4470 .tos_hs = tgt_obd_handlers
4473 .tos_opc_start = LDLM_FIRST_OPC,
4474 .tos_opc_end = LDLM_LAST_OPC,
4475 .tos_hs = tgt_dlm_handlers
4478 .tos_opc_start = SEC_FIRST_OPC,
4479 .tos_opc_end = SEC_LAST_OPC,
4480 .tos_hs = mdt_sec_ctx_ops
4483 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4484 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4485 .tos_hs = tgt_out_handlers
4488 .tos_opc_start = FLD_FIRST_OPC,
4489 .tos_opc_end = FLD_LAST_OPC,
4490 .tos_hs = fld_handlers
4493 .tos_opc_start = SEQ_FIRST_OPC,
4494 .tos_opc_end = SEQ_LAST_OPC,
4495 .tos_hs = seq_handlers
4498 .tos_opc_start = QUOTA_DQACQ,
4499 .tos_opc_end = QUOTA_LAST_OPC,
4500 .tos_hs = mdt_quota_ops
4503 .tos_opc_start = LLOG_FIRST_OPC,
4504 .tos_opc_end = LLOG_LAST_OPC,
4505 .tos_hs = tgt_llog_handlers
4508 .tos_opc_start = LFSCK_FIRST_OPC,
4509 .tos_opc_end = LFSCK_LAST_OPC,
4510 .tos_hs = tgt_lfsck_handlers
4518 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4520 struct md_device *next = m->mdt_child;
4521 struct lu_device *d = &m->mdt_lu_dev;
4522 struct obd_device *obd = mdt2obd_dev(m);
4523 struct lfsck_stop stop;
4526 stop.ls_status = LS_PAUSED;
4528 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4530 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4531 ping_evictor_stop();
4533 if (m->mdt_opts.mo_coordinator)
4534 mdt_hsm_cdt_stop(m);
4536 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4537 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4539 if (m->mdt_namespace != NULL)
4540 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4541 d->ld_obd->obd_force);
4543 obd_exports_barrier(obd);
4544 obd_zombie_barrier();
4546 mdt_quota_fini(env, m);
4548 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4550 /* Calling the cleanup functions in the same order as in the mdt_init0
4555 target_recovery_fini(obd);
4556 upcall_cache_cleanup(m->mdt_identity_cache);
4557 m->mdt_identity_cache = NULL;
4559 mdt_fs_cleanup(env, m);
4561 tgt_fini(env, &m->mdt_lut);
4563 mdt_hsm_cdt_fini(m);
4565 if (m->mdt_namespace != NULL) {
4566 ldlm_namespace_free_post(m->mdt_namespace);
4567 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4570 if (m->mdt_md_root != NULL) {
4571 mdt_object_put(env, m->mdt_md_root);
4572 m->mdt_md_root = NULL;
4575 mdt_seq_fini(env, m);
4577 mdt_fld_fini(env, m);
4582 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4584 LASSERT(atomic_read(&d->ld_ref) == 0);
4586 server_put_mount(mdt_obd_name(m), true);
4591 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4593 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4594 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4596 struct mdt_thread_info *info;
4597 struct obd_device *obd;
4598 const char *dev = lustre_cfg_string(cfg, 0);
4599 const char *num = lustre_cfg_string(cfg, 2);
4600 struct lustre_mount_info *lmi = NULL;
4601 struct lustre_sb_info *lsi;
4603 struct seq_server_site *ss_site;
4604 const char *identity_upcall = "NONE";
4605 struct md_device *next;
4611 lu_device_init(&m->mdt_lu_dev, ldt);
4613 * Environment (env) might be missing mdt_thread_key values at that
4614 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4617 * Usually device allocation path doesn't use module key values, but
4618 * mdt has to do a lot of work here, so allocate key value.
4620 rc = lu_env_refill((struct lu_env *)env);
4624 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4625 LASSERT(info != NULL);
4627 obd = class_name2obd(dev);
4628 LASSERT(obd != NULL);
4630 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4631 m->mdt_opts.mo_evict_tgt_nids = 1;
4632 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4634 /* default is coordinator off, it is started through conf_param
4636 m->mdt_opts.mo_coordinator = 0;
4638 lmi = server_get_mount(dev);
4640 CERROR("Cannot get mount info for %s!\n", dev);
4643 lsi = s2lsi(lmi->lmi_sb);
4644 /* CMD is supported only in IAM mode */
4646 node_id = simple_strtol(num, NULL, 10);
4647 obd->u.obt.obt_magic = OBT_MAGIC;
4648 if (lsi->lsi_lmd != NULL &&
4649 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4650 m->mdt_skip_lfsck = 1;
4653 m->mdt_squash.rsi_uid = 0;
4654 m->mdt_squash.rsi_gid = 0;
4655 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4656 init_rwsem(&m->mdt_squash.rsi_sem);
4657 spin_lock_init(&m->mdt_lock);
4658 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4659 m->mdt_enable_remote_dir = 0;
4660 m->mdt_enable_remote_dir_gid = 0;
4662 atomic_set(&m->mdt_mds_mds_conns, 0);
4663 atomic_set(&m->mdt_async_commit_count, 0);
4665 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4666 m->mdt_lu_dev.ld_obd = obd;
4667 /* Set this lu_device to obd for error handling purposes. */
4668 obd->obd_lu_dev = &m->mdt_lu_dev;
4670 /* init the stack */
4671 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4673 CERROR("%s: Can't init device stack, rc %d\n",
4674 mdt_obd_name(m), rc);
4679 ss_site = mdt_seq_site(m);
4680 s->ld_seq_site = ss_site;
4683 /* set server index */
4684 ss_site->ss_node_id = node_id;
4686 /* failover is the default
4687 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4688 * assumed whose ss_node_id == 0 XXX
4690 obd->obd_replayable = 1;
4691 /* No connection accepted until configurations will finish */
4692 obd->obd_no_conn = 1;
4694 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4695 char *str = lustre_cfg_string(cfg, 4);
4696 if (strchr(str, 'n')) {
4697 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4698 obd->obd_replayable = 0;
4702 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4704 GOTO(err_fini_stack, rc);
4706 rc = mdt_seq_init(env, m);
4708 GOTO(err_fini_fld, rc);
4710 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4711 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4712 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4713 LDLM_NAMESPACE_SERVER,
4714 LDLM_NAMESPACE_GREEDY,
4716 if (m->mdt_namespace == NULL)
4717 GOTO(err_fini_seq, rc = -ENOMEM);
4719 m->mdt_namespace->ns_lvbp = m;
4720 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4722 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4723 /* set obd_namespace for compatibility with old code */
4724 obd->obd_namespace = m->mdt_namespace;
4726 rc = mdt_hsm_cdt_init(m);
4728 CERROR("%s: error initializing coordinator, rc %d\n",
4729 mdt_obd_name(m), rc);
4730 GOTO(err_free_ns, rc);
4733 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4734 OBD_FAIL_MDS_ALL_REQUEST_NET,
4735 OBD_FAIL_MDS_ALL_REPLY_NET);
4737 GOTO(err_free_hsm, rc);
4739 rc = mdt_fs_setup(env, m, obd, lsi);
4743 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4745 next = m->mdt_child;
4746 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4749 GOTO(err_fs_cleanup, rc);
4751 if (mntopts & MNTOPT_USERXATTR)
4752 m->mdt_opts.mo_user_xattr = 1;
4754 m->mdt_opts.mo_user_xattr = 0;
4756 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4758 GOTO(err_fs_cleanup, rc);
4760 if (mntopts & MNTOPT_ACL)
4761 m->mdt_opts.mo_acl = 1;
4763 m->mdt_opts.mo_acl = 0;
4765 /* XXX: to support suppgid for ACL, we enable identity_upcall
4766 * by default, otherwise, maybe got unexpected -EACCESS. */
4767 if (m->mdt_opts.mo_acl)
4768 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4770 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4772 &mdt_identity_upcall_cache_ops);
4773 if (IS_ERR(m->mdt_identity_cache)) {
4774 rc = PTR_ERR(m->mdt_identity_cache);
4775 m->mdt_identity_cache = NULL;
4776 GOTO(err_fs_cleanup, rc);
4779 rc = mdt_procfs_init(m, dev);
4781 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4782 GOTO(err_recovery, rc);
4785 rc = mdt_quota_init(env, m, cfg);
4787 GOTO(err_procfs, rc);
4789 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4790 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4791 "mdt_ldlm_client", m->mdt_ldlm_client);
4793 ping_evictor_start();
4795 /* recovery will be started upon mdt_prepare()
4796 * when the whole stack is complete and ready
4797 * to serve the requests */
4799 /* Reduce the initial timeout on an MDS because it doesn't need such
4800 * a long timeout as an OST does. Adaptive timeouts will adjust this
4801 * value appropriately. */
4802 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4803 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4809 target_recovery_fini(obd);
4810 upcall_cache_cleanup(m->mdt_identity_cache);
4811 m->mdt_identity_cache = NULL;
4813 mdt_fs_cleanup(env, m);
4815 tgt_fini(env, &m->mdt_lut);
4817 mdt_hsm_cdt_fini(m);
4819 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4820 obd->obd_namespace = m->mdt_namespace = NULL;
4822 mdt_seq_fini(env, m);
4824 mdt_fld_fini(env, m);
4826 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4829 server_put_mount(dev, true);
4833 /* For interoperability, the left element is old parameter, the right one
4834 * is the new version of the parameter, if some parameter is deprecated,
4835 * the new version should be set as NULL. */
4836 static struct cfg_interop_param mdt_interop_param[] = {
4837 { "mdt.group_upcall", NULL },
4838 { "mdt.quota_type", NULL },
4839 { "mdd.quota_type", NULL },
4840 { "mdt.som", NULL },
4841 { "mdt.rootsquash", "mdt.root_squash" },
4842 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4846 /* used by MGS to process specific configurations */
4847 static int mdt_process_config(const struct lu_env *env,
4848 struct lu_device *d, struct lustre_cfg *cfg)
4850 struct mdt_device *m = mdt_dev(d);
4851 struct md_device *md_next = m->mdt_child;
4852 struct lu_device *next = md2lu_dev(md_next);
4856 switch (cfg->lcfg_command) {
4858 struct obd_device *obd = d->ld_obd;
4860 /* For interoperability */
4861 struct cfg_interop_param *ptr = NULL;
4862 struct lustre_cfg *old_cfg = NULL;
4865 param = lustre_cfg_string(cfg, 1);
4866 if (param == NULL) {
4867 CERROR("param is empty\n");
4872 ptr = class_find_old_param(param, mdt_interop_param);
4874 if (ptr->new_param == NULL) {
4876 CWARN("For interoperability, skip this %s."
4877 " It is obsolete.\n", ptr->old_param);
4881 CWARN("Found old param %s, changed it to %s.\n",
4882 ptr->old_param, ptr->new_param);
4885 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4892 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4894 if (rc > 0 || rc == -ENOSYS) {
4895 /* is it an HSM var ? */
4896 rc = class_process_proc_param(PARAM_HSM,
4897 hsm_cdt_get_proc_vars(),
4899 if (rc > 0 || rc == -ENOSYS)
4900 /* we don't understand; pass it on */
4901 rc = next->ld_ops->ldo_process_config(env, next,
4905 if (old_cfg != NULL)
4906 lustre_cfg_free(cfg);
4911 /* others are passed further */
4912 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4918 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4919 const struct lu_object_header *hdr,
4920 struct lu_device *d)
4922 struct mdt_object *mo;
4926 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4928 struct lu_object *o;
4929 struct lu_object_header *h;
4932 h = &mo->mot_header;
4933 lu_object_header_init(h);
4934 lu_object_init(o, h, d);
4935 lu_object_add_top(h, o);
4936 o->lo_ops = &mdt_obj_ops;
4937 spin_lock_init(&mo->mot_write_lock);
4938 mutex_init(&mo->mot_lov_mutex);
4939 init_rwsem(&mo->mot_open_sem);
4940 atomic_set(&mo->mot_open_count, 0);
4946 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4947 const struct lu_object_conf *unused)
4949 struct mdt_device *d = mdt_dev(o->lo_dev);
4950 struct lu_device *under;
4951 struct lu_object *below;
4955 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4956 PFID(lu_object_fid(o)));
4958 under = &d->mdt_child->md_lu_dev;
4959 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4960 if (below != NULL) {
4961 lu_object_add(o, below);
4968 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4970 struct mdt_object *mo = mdt_obj(o);
4971 struct lu_object_header *h;
4975 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4976 PFID(lu_object_fid(o)));
4978 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4979 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4982 lu_object_header_fini(h);
4983 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4988 static int mdt_object_print(const struct lu_env *env, void *cookie,
4989 lu_printer_t p, const struct lu_object *o)
4991 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4993 return (*p)(env, cookie,
4994 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
4995 mdto, mdto->mot_lov_created ? "lov_created" : "",
4996 mdto->mot_cache_attr ? "cache_attr" : "",
4997 mdto->mot_write_count);
5000 static int mdt_prepare(const struct lu_env *env,
5001 struct lu_device *pdev,
5002 struct lu_device *cdev)
5004 struct mdt_device *mdt = mdt_dev(cdev);
5005 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5006 struct obd_device *obd = cdev->ld_obd;
5013 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5017 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5021 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5025 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5026 /* The LFSCK instance is registered just now, so it must be there when
5027 * register the namespace to such instance. */
5028 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5030 if (mdt->mdt_seq_site.ss_node_id == 0) {
5031 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5032 &mdt->mdt_md_root_fid);
5037 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5039 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5040 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5041 LASSERT(obd->obd_no_conn);
5042 spin_lock(&obd->obd_dev_lock);
5043 obd->obd_no_conn = 0;
5044 spin_unlock(&obd->obd_dev_lock);
5046 if (obd->obd_recovering == 0)
5047 mdt_postrecov(env, mdt);
5052 const struct lu_device_operations mdt_lu_ops = {
5053 .ldo_object_alloc = mdt_object_alloc,
5054 .ldo_process_config = mdt_process_config,
5055 .ldo_prepare = mdt_prepare,
5058 static const struct lu_object_operations mdt_obj_ops = {
5059 .loo_object_init = mdt_object_init,
5060 .loo_object_free = mdt_object_free,
5061 .loo_object_print = mdt_object_print
5064 static int mdt_obd_set_info_async(const struct lu_env *env,
5065 struct obd_export *exp,
5066 __u32 keylen, void *key,
5067 __u32 vallen, void *val,
5068 struct ptlrpc_request_set *set)
5074 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5075 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5083 * Match client and server connection feature flags.
5085 * Compute the compatibility flags for a connection request based on
5086 * features mutually supported by client and server.
5088 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5089 * must not be updated here, otherwise a partially initialized value may
5090 * be exposed. After the connection request is successfully processed,
5091 * the top-level MDT connect request handler atomically updates the export
5092 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5093 * reply. \see mdt_connect().
5095 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5096 * full struct obd_connect_data. So care must be taken when accessing fields
5097 * that are not present in struct obd_connect_data_v1. See LU-16.
5099 * \param exp the obd_export associated with this client/target pair
5100 * \param mdt the target device for the connection
5101 * \param data stores data for this connect request
5104 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5105 * \retval -EBADE client and server feature requirements are incompatible
5107 static int mdt_connect_internal(struct obd_export *exp,
5108 struct mdt_device *mdt,
5109 struct obd_connect_data *data)
5111 LASSERT(data != NULL);
5113 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5115 if (mdt->mdt_bottom->dd_rdonly &&
5116 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5117 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5120 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5121 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5123 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5125 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5126 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5127 CWARN("%s: client %s does not support ibits lock, either "
5128 "very old or an invalid client: flags %#llx\n",
5129 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5130 data->ocd_connect_flags);
5134 if (!mdt->mdt_opts.mo_acl)
5135 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5137 if (!mdt->mdt_opts.mo_user_xattr)
5138 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5140 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5141 data->ocd_brw_size = min(data->ocd_brw_size,
5142 (__u32)MD_MAX_BRW_SIZE);
5143 if (data->ocd_brw_size == 0) {
5144 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx"
5145 " ocd_version: %x ocd_grant: %d "
5146 "ocd_index: %u ocd_brw_size is "
5147 "unexpectedly zero, network data "
5148 "corruption? Refusing connection of this"
5151 exp->exp_client_uuid.uuid,
5152 exp, data->ocd_connect_flags, data->ocd_version,
5153 data->ocd_grant, data->ocd_index);
5158 /* NB: Disregard the rule against updating
5159 * exp_connect_data.ocd_connect_flags in this case, since
5160 * tgt_client_new() needs to know if this is a lightweight
5161 * connection, and it is safe to expose this flag before
5162 * connection processing completes. */
5163 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5164 spin_lock(&exp->exp_lock);
5165 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5166 spin_unlock(&exp->exp_lock);
5169 data->ocd_version = LUSTRE_VERSION_CODE;
5171 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5172 CWARN("%s: MDS requires FID support, but client not\n",
5177 if (OCD_HAS_FLAG(data, PINGLESS)) {
5178 if (ptlrpc_pinger_suppress_pings()) {
5179 spin_lock(&exp->exp_obd->obd_dev_lock);
5180 list_del_init(&exp->exp_obd_chain_timed);
5181 spin_unlock(&exp->exp_obd->obd_dev_lock);
5183 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5187 data->ocd_max_easize = mdt->mdt_max_ea_size;
5189 /* NB: Disregard the rule against updating
5190 * exp_connect_data.ocd_connect_flags in this case, since
5191 * tgt_client_new() needs to know if this is client supports
5192 * multiple modify RPCs, and it is safe to expose this flag before
5193 * connection processing completes. */
5194 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5195 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5196 spin_lock(&exp->exp_lock);
5197 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5198 spin_unlock(&exp->exp_lock);
5204 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5205 struct mdt_thread_info *info,
5206 struct mdt_file_data *mfd)
5208 struct lu_context ses;
5212 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5217 lu_context_enter(&ses);
5219 mdt_ucred(info)->uc_valid = UCRED_OLD;
5220 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5222 lu_context_exit(&ses);
5223 lu_context_fini(&ses);
5229 static int mdt_export_cleanup(struct obd_export *exp)
5231 struct list_head closing_list;
5232 struct mdt_export_data *med = &exp->exp_mdt_data;
5233 struct obd_device *obd = exp->exp_obd;
5234 struct mdt_device *mdt;
5235 struct mdt_thread_info *info;
5237 struct mdt_file_data *mfd, *n;
5241 INIT_LIST_HEAD(&closing_list);
5242 spin_lock(&med->med_open_lock);
5243 while (!list_empty(&med->med_open_head)) {
5244 struct list_head *tmp = med->med_open_head.next;
5245 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5247 /* Remove mfd handle so it can't be found again.
5248 * We are consuming the mfd_list reference here. */
5249 class_handle_unhash(&mfd->mfd_handle);
5250 list_move_tail(&mfd->mfd_list, &closing_list);
5252 spin_unlock(&med->med_open_lock);
5253 mdt = mdt_dev(obd->obd_lu_dev);
5254 LASSERT(mdt != NULL);
5256 rc = lu_env_init(&env, LCT_MD_THREAD);
5260 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5261 LASSERT(info != NULL);
5262 memset(info, 0, sizeof *info);
5263 info->mti_env = &env;
5264 info->mti_mdt = mdt;
5265 info->mti_exp = exp;
5267 if (!list_empty(&closing_list)) {
5268 struct md_attr *ma = &info->mti_attr;
5270 /* Close any open files (which may also cause orphan
5272 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5273 list_del_init(&mfd->mfd_list);
5274 ma->ma_need = ma->ma_valid = 0;
5276 /* This file is being closed due to an eviction, it
5277 * could have been modified and now dirty regarding to
5278 * HSM archive, check this!
5279 * The logic here is to mark a file dirty if there's a
5280 * chance it was dirtied before the client was evicted,
5281 * so that we don't have to wait for a release attempt
5282 * before finding out the file was actually dirty and
5283 * fail the release. Aggressively marking it dirty here
5284 * will cause the policy engine to attempt to
5285 * re-archive it; when rearchiving, we can compare the
5286 * current version to the HSM data_version and make the
5287 * archive request into a noop if it's not actually
5290 if (mfd->mfd_mode & FMODE_WRITE)
5291 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5293 /* Don't unlink orphan on failover umount, LU-184 */
5294 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5295 ma->ma_valid = MA_FLAGS;
5296 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5298 mdt_mfd_close(info, mfd);
5301 info->mti_mdt = NULL;
5302 /* cleanup client slot early */
5303 /* Do not erase record for recoverable client. */
5304 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5305 tgt_client_del(&env, exp);
5311 static inline void mdt_enable_slc(struct mdt_device *mdt)
5313 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5314 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5317 static inline void mdt_disable_slc(struct mdt_device *mdt)
5319 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5320 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5323 static int mdt_obd_disconnect(struct obd_export *exp)
5329 class_export_get(exp);
5331 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5332 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5333 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5335 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5336 mdt_disable_slc(mdt);
5339 rc = server_disconnect_export(exp);
5341 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5343 rc = mdt_export_cleanup(exp);
5344 nodemap_del_member(exp);
5345 class_export_put(exp);
5349 /* mds_connect copy */
5350 static int mdt_obd_connect(const struct lu_env *env,
5351 struct obd_export **exp, struct obd_device *obd,
5352 struct obd_uuid *cluuid,
5353 struct obd_connect_data *data,
5356 struct obd_export *lexp;
5357 struct lustre_handle conn = { 0 };
5358 struct mdt_device *mdt;
5360 lnet_nid_t *client_nid = localdata;
5363 LASSERT(env != NULL);
5364 LASSERT(data != NULL);
5366 if (!exp || !obd || !cluuid)
5369 mdt = mdt_dev(obd->obd_lu_dev);
5371 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5372 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5373 atomic_inc(&mdt->mdt_mds_mds_conns);
5374 mdt_enable_slc(mdt);
5378 * first, check whether the stack is ready to handle requests
5379 * XXX: probably not very appropriate method is used now
5380 * at some point we should find a better one
5382 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5383 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5384 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5385 rc = obd_get_info(env, mdt->mdt_child_exp,
5386 sizeof(KEY_OSP_CONNECTED),
5387 KEY_OSP_CONNECTED, NULL, NULL);
5390 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5393 rc = class_connect(&conn, obd, cluuid);
5397 lexp = class_conn2export(&conn);
5398 LASSERT(lexp != NULL);
5400 rc = nodemap_add_member(*client_nid, lexp);
5401 if (rc != 0 && rc != -EEXIST)
5404 rc = mdt_connect_internal(lexp, mdt, data);
5406 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5409 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5410 rc = tgt_client_new(env, lexp);
5412 mdt_export_stats_init(obd, lexp, localdata);
5416 class_disconnect(lexp);
5417 nodemap_del_member(lexp);
5421 /* Because we do not want this export to be evicted by pinger,
5422 * let's not add this export to the timed chain list. */
5423 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5424 spin_lock(&lexp->exp_obd->obd_dev_lock);
5425 list_del_init(&lexp->exp_obd_chain_timed);
5426 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5433 static int mdt_obd_reconnect(const struct lu_env *env,
5434 struct obd_export *exp, struct obd_device *obd,
5435 struct obd_uuid *cluuid,
5436 struct obd_connect_data *data,
5439 lnet_nid_t *client_nid = localdata;
5443 if (exp == NULL || obd == NULL || cluuid == NULL)
5446 rc = nodemap_add_member(*client_nid, exp);
5447 if (rc != 0 && rc != -EEXIST)
5450 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5452 mdt_export_stats_init(obd, exp, localdata);
5454 nodemap_del_member(exp);
5459 /* FIXME: Can we avoid using these two interfaces? */
5460 static int mdt_init_export(struct obd_export *exp)
5462 struct mdt_export_data *med = &exp->exp_mdt_data;
5466 INIT_LIST_HEAD(&med->med_open_head);
5467 spin_lock_init(&med->med_open_lock);
5468 spin_lock(&exp->exp_lock);
5469 exp->exp_connecting = 1;
5470 spin_unlock(&exp->exp_lock);
5472 /* self-export doesn't need client data and ldlm initialization */
5473 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5474 &exp->exp_client_uuid)))
5477 rc = tgt_client_alloc(exp);
5481 rc = ldlm_init_export(exp);
5488 tgt_client_free(exp);
5490 CERROR("%s: Failed to initialize export: rc = %d\n",
5491 exp->exp_obd->obd_name, rc);
5495 static int mdt_destroy_export(struct obd_export *exp)
5499 target_destroy_export(exp);
5500 /* destroy can be called from failed obd_setup, so
5501 * checking uuid is safer than obd_self_export */
5502 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5503 &exp->exp_client_uuid)))
5506 ldlm_destroy_export(exp);
5507 tgt_client_free(exp);
5509 LASSERT(list_empty(&exp->exp_outstanding_replies));
5510 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5515 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5516 struct linkea_data *ldata)
5520 LASSERT(ldata->ld_buf->lb_buf != NULL);
5522 if (!mdt_object_exists(mdt_obj))
5525 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5526 ldata->ld_buf, XATTR_NAME_LINK);
5527 if (rc == -ERANGE) {
5528 /* Buf was too small, figure out what we need. */
5529 lu_buf_free(ldata->ld_buf);
5530 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5531 ldata->ld_buf, XATTR_NAME_LINK);
5534 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5535 if (ldata->ld_buf->lb_buf == NULL)
5537 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5538 ldata->ld_buf, XATTR_NAME_LINK);
5543 return linkea_init_with_rec(ldata);
5547 * Given an MDT object, try to look up the full path to the object.
5548 * Part of the MDT layer implementation of lfs fid2path.
5550 * \param[in] info Per-thread common data shared by MDT level handlers.
5551 * \param[in] obj Object to do path lookup of
5552 * \param[in,out] fp User-provided struct to store path information
5553 * \param[in] root_fid Root FID of current path should reach
5555 * \retval 0 Lookup successful, path information stored in fp
5556 * \retval -EAGAIN Lookup failed, usually because object is being moved
5557 * \retval negative errno if there was a problem
5559 static int mdt_path_current(struct mdt_thread_info *info,
5560 struct mdt_object *obj,
5561 struct getinfo_fid2path *fp,
5562 struct lu_fid *root_fid)
5564 struct mdt_device *mdt = info->mti_mdt;
5565 struct mdt_object *mdt_obj;
5566 struct link_ea_header *leh;
5567 struct link_ea_entry *lee;
5568 struct lu_name *tmpname = &info->mti_name;
5569 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5570 struct lu_buf *buf = &info->mti_big_buf;
5573 struct linkea_data ldata = { NULL };
5578 /* temp buffer for path element, the buffer will be finally freed
5579 * in mdt_thread_info_fini */
5580 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5581 if (buf->lb_buf == NULL)
5585 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5588 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5590 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5591 struct lu_buf lmv_buf;
5593 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5594 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5595 GOTO(out, rc = -ENOENT);
5597 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5599 mdt_object_get(info->mti_env, mdt_obj);
5601 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5602 if (IS_ERR(mdt_obj))
5603 GOTO(out, rc = PTR_ERR(mdt_obj));
5606 if (!mdt_object_exists(mdt_obj)) {
5607 mdt_object_put(info->mti_env, mdt_obj);
5608 GOTO(out, rc = -ENOENT);
5611 if (mdt_object_remote(mdt_obj)) {
5612 mdt_object_put(info->mti_env, mdt_obj);
5613 GOTO(remote_out, rc = -EREMOTE);
5616 rc = mdt_links_read(info, mdt_obj, &ldata);
5618 mdt_object_put(info->mti_env, mdt_obj);
5623 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5624 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5625 /* If set, use link #linkno for path lookup, otherwise use
5626 link #0. Only do this for the final path element. */
5627 if (first && fp->gf_linkno < leh->leh_reccount) {
5629 for (count = 0; count < fp->gf_linkno; count++) {
5630 lee = (struct link_ea_entry *)
5631 ((char *)lee + reclen);
5632 linkea_entry_unpack(lee, &reclen, tmpname,
5635 if (fp->gf_linkno < leh->leh_reccount - 1)
5636 /* indicate to user there are more links */
5640 lmv_buf.lb_buf = info->mti_xattr_buf;
5641 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5642 /* Check if it is slave stripes */
5643 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5644 &lmv_buf, XATTR_NAME_LMV);
5645 mdt_object_put(info->mti_env, mdt_obj);
5647 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5649 /* For slave stripes, get its master */
5650 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5651 fp->gf_fid = *tmpfid;
5654 } else if (rc < 0 && rc != -ENODATA) {
5660 /* Pack the name in the end of the buffer */
5661 ptr -= tmpname->ln_namelen;
5662 if (ptr - 1 <= fp->gf_u.gf_path)
5663 GOTO(out, rc = -EOVERFLOW);
5664 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5667 /* keep the last resolved fid to the client, so the
5668 * client will build the left path on another MDT for
5670 fp->gf_fid = *tmpfid;
5676 ptr++; /* skip leading / */
5677 memmove(fp->gf_u.gf_path, ptr,
5678 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5685 * Given an MDT object, use mdt_path_current to get the path.
5686 * Essentially a wrapper to retry mdt_path_current a set number of times
5687 * if -EAGAIN is returned (usually because an object is being moved).
5689 * Part of the MDT layer implementation of lfs fid2path.
5691 * \param[in] info Per-thread common data shared by mdt level handlers.
5692 * \param[in] obj Object to do path lookup of
5693 * \param[in,out] fp User-provided struct for arguments and to store path
5696 * \retval 0 Lookup successful, path information stored in fp
5697 * \retval negative errno if there was a problem
5699 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5700 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5702 struct mdt_device *mdt = info->mti_mdt;
5707 if (fp->gf_pathlen < 3)
5710 if (root_fid == NULL)
5711 root_fid = &mdt->mdt_md_root_fid;
5713 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5714 fp->gf_u.gf_path[0] = '\0';
5718 /* Retry multiple times in case file is being moved */
5719 while (tries-- && rc == -EAGAIN)
5720 rc = mdt_path_current(info, obj, fp, root_fid);
5726 * Get the full path of the provided FID, as of changelog record recno.
5728 * This checks sanity and looks up object for user provided FID
5729 * before calling the actual path lookup code.
5731 * Part of the MDT layer implementation of lfs fid2path.
5733 * \param[in] info Per-thread common data shared by mdt level handlers.
5734 * \param[in,out] fp User-provided struct for arguments and to store path
5737 * \retval 0 Lookup successful, path information and recno stored in fp
5738 * \retval -ENOENT, object does not exist
5739 * \retval negative errno if there was a problem
5741 static int mdt_fid2path(struct mdt_thread_info *info,
5742 struct lu_fid *root_fid,
5743 struct getinfo_fid2path *fp)
5745 struct mdt_device *mdt = info->mti_mdt;
5746 struct mdt_object *obj;
5750 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5751 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5753 if (!fid_is_sane(&fp->gf_fid))
5756 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5757 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5758 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5759 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5763 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5766 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5767 PFID(&fp->gf_fid), rc);
5771 if (mdt_object_remote(obj))
5773 else if (!mdt_object_exists(obj))
5779 mdt_object_put(info->mti_env, obj);
5780 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5781 PFID(&fp->gf_fid), rc);
5785 rc = mdt_path(info, obj, fp, root_fid);
5787 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5788 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5789 fp->gf_recno, fp->gf_linkno);
5791 mdt_object_put(info->mti_env, obj);
5796 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5797 void *val, int vallen)
5799 struct getinfo_fid2path *fpout, *fpin;
5800 struct lu_fid *root_fid = NULL;
5803 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5806 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5807 lustre_swab_fid2path(fpin);
5809 memcpy(fpout, fpin, sizeof(*fpin));
5810 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5813 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5814 sizeof(struct lu_fid)) {
5815 /* client sent its root FID, which is normally fileset FID */
5816 root_fid = fpin->gf_u.gf_root_fid;
5817 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5818 lustre_swab_lu_fid(root_fid);
5820 if (root_fid != NULL && !fid_is_sane(root_fid))
5824 rc = mdt_fid2path(info, root_fid, fpout);
5828 int mdt_get_info(struct tgt_session_info *tsi)
5838 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5840 CDEBUG(D_IOCTL, "No GETINFO key\n");
5841 RETURN(err_serious(-EFAULT));
5843 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5846 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5847 if (vallen == NULL) {
5848 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5849 tgt_name(tsi->tsi_tgt));
5850 RETURN(err_serious(-EFAULT));
5853 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5855 rc = req_capsule_server_pack(tsi->tsi_pill);
5857 RETURN(err_serious(rc));
5859 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5860 if (valout == NULL) {
5861 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5862 tgt_name(tsi->tsi_tgt));
5863 RETURN(err_serious(-EFAULT));
5866 if (KEY_IS(KEY_FID2PATH)) {
5867 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5869 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5870 mdt_thread_info_fini(info);
5877 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5879 struct obd_ioctl_data *data = karg;
5882 struct mdt_object *obj;
5883 struct mdt_lock_handle *lh;
5887 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5888 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5891 fid = (struct lu_fid *)data->ioc_inlbuf1;
5893 if (!fid_is_sane(fid))
5896 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5898 lh = &mti->mti_lh[MDT_LH_PARENT];
5899 mdt_lock_reg_init(lh, LCK_CR);
5901 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5903 RETURN(PTR_ERR(obj));
5905 if (mdt_object_remote(obj)) {
5908 * before calling version get the correct MDS should be
5909 * fid, this is error to find remote object here
5911 CERROR("nonlocal object "DFID"\n", PFID(fid));
5912 } else if (!mdt_object_exists(obj)) {
5913 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5916 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5917 *(__u64 *)data->ioc_inlbuf2 = version;
5920 mdt_object_unlock_put(mti, obj, lh, 1);
5924 /* ioctls on obd dev */
5925 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5926 void *karg, void __user *uarg)
5929 struct obd_device *obd = exp->exp_obd;
5930 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5931 struct dt_device *dt = mdt->mdt_bottom;
5935 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5936 rc = lu_env_init(&env, LCT_MD_THREAD);
5942 rc = mdt_device_sync(&env, mdt);
5944 case OBD_IOC_SET_READONLY:
5945 rc = dt_sync(&env, dt);
5947 rc = dt_ro(&env, dt);
5949 case OBD_IOC_ABORT_RECOVERY:
5950 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5951 obd->obd_abort_recovery = 1;
5952 target_stop_recovery_thread(obd);
5955 case OBD_IOC_CHANGELOG_REG:
5956 case OBD_IOC_CHANGELOG_DEREG:
5957 case OBD_IOC_CHANGELOG_CLEAR:
5958 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
5962 case OBD_IOC_START_LFSCK: {
5963 struct md_device *next = mdt->mdt_child;
5964 struct obd_ioctl_data *data = karg;
5965 struct lfsck_start_param lsp;
5967 if (unlikely(data == NULL)) {
5972 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5973 lsp.lsp_index_valid = 0;
5974 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5977 case OBD_IOC_STOP_LFSCK: {
5978 struct md_device *next = mdt->mdt_child;
5979 struct obd_ioctl_data *data = karg;
5980 struct lfsck_stop stop;
5982 stop.ls_status = LS_STOPPED;
5983 /* Old lfsck utils may pass NULL @stop. */
5984 if (data->ioc_inlbuf1 == NULL)
5988 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5990 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5993 case OBD_IOC_QUERY_LFSCK: {
5994 struct md_device *next = mdt->mdt_child;
5995 struct obd_ioctl_data *data = karg;
5997 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6001 case OBD_IOC_GET_OBJ_VERSION: {
6002 struct mdt_thread_info *mti;
6003 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6004 memset(mti, 0, sizeof *mti);
6005 mti->mti_env = &env;
6009 rc = mdt_ioc_version_get(mti, karg);
6012 case OBD_IOC_CATLOGLIST: {
6013 struct mdt_thread_info *mti;
6015 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6016 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6017 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6018 &mti->mti_tmp_fid1);
6023 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6024 mdt_obd_name(mdt), cmd, rc);
6031 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6033 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6037 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6038 struct lfsck_start_param lsp;
6040 lsp.lsp_start = NULL;
6041 lsp.lsp_index_valid = 0;
6042 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6043 OBD_IOC_START_LFSCK,
6045 if (rc != 0 && rc != -EALREADY)
6046 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6047 mdt_obd_name(mdt), rc);
6050 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6054 static int mdt_obd_postrecov(struct obd_device *obd)
6059 rc = lu_env_init(&env, LCT_MD_THREAD);
6062 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6067 static struct obd_ops mdt_obd_device_ops = {
6068 .o_owner = THIS_MODULE,
6069 .o_set_info_async = mdt_obd_set_info_async,
6070 .o_connect = mdt_obd_connect,
6071 .o_reconnect = mdt_obd_reconnect,
6072 .o_disconnect = mdt_obd_disconnect,
6073 .o_init_export = mdt_init_export,
6074 .o_destroy_export = mdt_destroy_export,
6075 .o_iocontrol = mdt_iocontrol,
6076 .o_postrecov = mdt_obd_postrecov,
6079 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6080 struct lu_device *d)
6082 struct mdt_device *m = mdt_dev(d);
6089 static struct lu_device *mdt_device_free(const struct lu_env *env,
6090 struct lu_device *d)
6092 struct mdt_device *m = mdt_dev(d);
6095 lu_device_fini(&m->mdt_lu_dev);
6101 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6102 struct lu_device_type *t,
6103 struct lustre_cfg *cfg)
6105 struct lu_device *l;
6106 struct mdt_device *m;
6113 rc = mdt_init0(env, m, t, cfg);
6115 mdt_device_free(env, l);
6120 l = ERR_PTR(-ENOMEM);
6124 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6125 LU_KEY_INIT(mdt, struct mdt_thread_info);
6127 static void mdt_key_fini(const struct lu_context *ctx,
6128 struct lu_context_key *key, void* data)
6130 struct mdt_thread_info *info = data;
6132 if (info->mti_big_lmm) {
6133 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6134 info->mti_big_lmm = NULL;
6135 info->mti_big_lmmsize = 0;
6140 /* context key: mdt_thread_key */
6141 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6143 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6145 return lu_ucred(info->mti_env);
6148 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6150 return lu_ucred_check(info->mti_env);
6154 * Enable/disable COS (Commit On Sharing).
6156 * Set/Clear the COS flag in mdt options.
6158 * \param mdt mdt device
6159 * \param val 0 disables COS, other values enable COS
6161 void mdt_enable_cos(struct mdt_device *mdt, int val)
6166 mdt->mdt_opts.mo_cos = !!val;
6167 rc = lu_env_init(&env, LCT_LOCAL);
6168 if (unlikely(rc != 0)) {
6169 CWARN("%s: lu_env initialization failed, cannot "
6170 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6173 mdt_device_sync(&env, mdt);
6178 * Check COS (Commit On Sharing) status.
6180 * Return COS flag status.
6182 * \param mdt mdt device
6184 int mdt_cos_is_enabled(struct mdt_device *mdt)
6186 return mdt->mdt_opts.mo_cos != 0;
6189 static struct lu_device_type_operations mdt_device_type_ops = {
6190 .ldto_device_alloc = mdt_device_alloc,
6191 .ldto_device_free = mdt_device_free,
6192 .ldto_device_fini = mdt_device_fini
6195 static struct lu_device_type mdt_device_type = {
6196 .ldt_tags = LU_DEVICE_MD,
6197 .ldt_name = LUSTRE_MDT_NAME,
6198 .ldt_ops = &mdt_device_type_ops,
6199 .ldt_ctx_tags = LCT_MD_THREAD
6202 static int __init mdt_init(void)
6206 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6207 FID_NOBRACE_LEN + 1);
6208 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6210 rc = lu_kmem_init(mdt_caches);
6214 rc = mds_mod_init();
6218 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6219 LUSTRE_MDT_NAME, &mdt_device_type);
6224 lu_kmem_fini(mdt_caches);
6231 static void __exit mdt_exit(void)
6233 class_unregister_type(LUSTRE_MDT_NAME);
6235 lu_kmem_fini(mdt_caches);
6238 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6239 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6240 MODULE_VERSION(LUSTRE_VERSION_STRING);
6241 MODULE_LICENSE("GPL");
6243 module_init(mdt_init);
6244 module_exit(mdt_exit);