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 /* XXX Look into layout in MDT layer. */
526 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
528 struct lov_comp_md_v1 *comp_v1;
529 struct lov_mds_md *v1;
532 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
533 comp_v1 = (struct lov_comp_md_v1 *)lmm;
535 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
536 v1 = (struct lov_mds_md *)((char *)comp_v1 +
537 comp_v1->lcm_entries[i].lcme_offset);
538 /* We don't support partial release for now */
539 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
544 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
549 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
550 const struct lu_attr *attr, const struct lu_fid *fid)
552 struct md_attr *ma = &info->mti_attr;
553 struct obd_export *exp = info->mti_exp;
554 struct lu_nodemap *nodemap = NULL;
556 LASSERT(ma->ma_valid & MA_INODE);
558 if (attr->la_valid & LA_ATIME) {
559 b->mbo_atime = attr->la_atime;
560 b->mbo_valid |= OBD_MD_FLATIME;
562 if (attr->la_valid & LA_MTIME) {
563 b->mbo_mtime = attr->la_mtime;
564 b->mbo_valid |= OBD_MD_FLMTIME;
566 if (attr->la_valid & LA_CTIME) {
567 b->mbo_ctime = attr->la_ctime;
568 b->mbo_valid |= OBD_MD_FLCTIME;
570 if (attr->la_valid & LA_FLAGS) {
571 b->mbo_flags = attr->la_flags;
572 b->mbo_valid |= OBD_MD_FLFLAGS;
574 if (attr->la_valid & LA_NLINK) {
575 b->mbo_nlink = attr->la_nlink;
576 b->mbo_valid |= OBD_MD_FLNLINK;
578 if (attr->la_valid & (LA_UID|LA_GID)) {
579 nodemap = nodemap_get_from_exp(exp);
583 if (attr->la_valid & LA_UID) {
584 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
585 NODEMAP_FS_TO_CLIENT,
587 b->mbo_valid |= OBD_MD_FLUID;
589 if (attr->la_valid & LA_GID) {
590 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
591 NODEMAP_FS_TO_CLIENT,
593 b->mbo_valid |= OBD_MD_FLGID;
596 b->mbo_mode = attr->la_mode;
597 if (attr->la_valid & LA_MODE)
598 b->mbo_valid |= OBD_MD_FLMODE;
599 if (attr->la_valid & LA_TYPE)
600 b->mbo_valid |= OBD_MD_FLTYPE;
604 b->mbo_valid |= OBD_MD_FLID;
605 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
606 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
609 if (!(attr->la_valid & LA_TYPE))
612 b->mbo_rdev = attr->la_rdev;
613 b->mbo_size = attr->la_size;
614 b->mbo_blocks = attr->la_blocks;
616 if (!S_ISREG(attr->la_mode)) {
617 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
618 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
619 /* means no objects are allocated on osts. */
620 LASSERT(!(ma->ma_valid & MA_LOV));
621 /* just ignore blocks occupied by extend attributes on MDS */
623 /* if no object is allocated on osts, the size on mds is valid.
625 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
626 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
627 mdt_hsm_is_released(ma->ma_lmm)) {
628 /* A released file stores its size on MDS. */
629 /* But return 1 block for released file, unless tools like tar
630 * will consider it fully sparse. (LU-3864)
632 if (unlikely(b->mbo_size == 0))
636 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
639 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
640 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
641 PFID(fid), (unsigned long long)b->mbo_size);
644 if (!IS_ERR_OR_NULL(nodemap))
645 nodemap_putref(nodemap);
648 static inline int mdt_body_has_lov(const struct lu_attr *la,
649 const struct mdt_body *body)
651 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
652 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
655 void mdt_client_compatibility(struct mdt_thread_info *info)
657 struct mdt_body *body;
658 struct ptlrpc_request *req = mdt_info_req(info);
659 struct obd_export *exp = req->rq_export;
660 struct md_attr *ma = &info->mti_attr;
661 struct lu_attr *la = &ma->ma_attr;
664 if (exp_connect_layout(exp))
665 /* the client can deal with 16-bit lmm_stripe_count */
668 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
670 if (!mdt_body_has_lov(la, body))
673 /* now we have a reply with a lov for a client not compatible with the
674 * layout lock so we have to clean the layout generation number */
675 if (S_ISREG(la->la_mode))
676 ma->ma_lmm->lmm_layout_gen = 0;
680 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
681 struct mdt_object *o)
683 const struct lu_env *env = info->mti_env;
686 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
695 /* Is it a directory? Let's check for the LMV as well */
696 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
697 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
701 rc2 = mo_xattr_get(env, mdt_object_child(o),
703 XATTR_NAME_DEFAULT_LMV);
705 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
713 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
716 const struct lu_env *env = info->mti_env;
720 LASSERT(info->mti_big_lmm_used == 0);
721 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
725 /* big_lmm may need to be grown */
726 if (info->mti_big_lmmsize < rc) {
727 int size = size_roundup_power2(rc);
729 if (info->mti_big_lmmsize > 0) {
730 /* free old buffer */
731 LASSERT(info->mti_big_lmm);
732 OBD_FREE_LARGE(info->mti_big_lmm,
733 info->mti_big_lmmsize);
734 info->mti_big_lmm = NULL;
735 info->mti_big_lmmsize = 0;
738 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
739 if (info->mti_big_lmm == NULL)
741 info->mti_big_lmmsize = size;
743 LASSERT(info->mti_big_lmmsize >= rc);
745 info->mti_buf.lb_buf = info->mti_big_lmm;
746 info->mti_buf.lb_len = info->mti_big_lmmsize;
747 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
752 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
753 struct md_attr *ma, const char *name)
755 struct md_object *next = mdt_object_child(o);
756 struct lu_buf *buf = &info->mti_buf;
759 if (strcmp(name, XATTR_NAME_LOV) == 0) {
760 buf->lb_buf = ma->ma_lmm;
761 buf->lb_len = ma->ma_lmm_size;
762 LASSERT(!(ma->ma_valid & MA_LOV));
763 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
764 buf->lb_buf = ma->ma_lmv;
765 buf->lb_len = ma->ma_lmv_size;
766 LASSERT(!(ma->ma_valid & MA_LMV));
767 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
768 buf->lb_buf = ma->ma_lmv;
769 buf->lb_len = ma->ma_lmv_size;
770 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
775 rc = mo_xattr_get(info->mti_env, next, buf, name);
779 if (strcmp(name, XATTR_NAME_LOV) == 0) {
780 if (info->mti_big_lmm_used)
781 ma->ma_lmm = info->mti_big_lmm;
783 /* NOT return LOV EA with hole to old client. */
784 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
785 LOV_PATTERN_F_HOLE) &&
786 !(exp_connect_flags(info->mti_exp) &
787 OBD_CONNECT_LFSCK)) {
790 ma->ma_lmm_size = rc;
791 ma->ma_valid |= MA_LOV;
793 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
794 if (info->mti_big_lmm_used)
795 ma->ma_lmv = info->mti_big_lmm;
797 ma->ma_lmv_size = rc;
798 ma->ma_valid |= MA_LMV;
799 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
800 ma->ma_lmv_size = rc;
801 ma->ma_valid |= MA_LMV_DEF;
804 /* Update mdt_max_mdsize so all clients will be aware that */
805 if (info->mti_mdt->mdt_max_mdsize < rc)
806 info->mti_mdt->mdt_max_mdsize = rc;
809 } else if (rc == -ENODATA) {
812 } else if (rc == -ERANGE) {
813 /* Default LMV has fixed size, so it must be able to fit
814 * in the original buffer */
815 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
817 rc = mdt_big_xattr_get(info, o, name);
819 info->mti_big_lmm_used = 1;
827 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
828 struct mdt_object *o, struct lu_fid *pfid)
830 struct lu_buf *buf = &info->mti_buf;
831 struct link_ea_header *leh;
832 struct link_ea_entry *lee;
836 buf->lb_buf = info->mti_big_lmm;
837 buf->lb_len = info->mti_big_lmmsize;
838 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
839 buf, XATTR_NAME_LINK);
840 /* ignore errors, MA_PFID won't be set and it is
841 * up to the caller to treat this as an error */
842 if (rc == -ERANGE || buf->lb_len == 0) {
843 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
844 buf->lb_buf = info->mti_big_lmm;
845 buf->lb_len = info->mti_big_lmmsize;
850 if (rc < sizeof(*leh)) {
851 CERROR("short LinkEA on "DFID": rc = %d\n",
852 PFID(mdt_object_fid(o)), rc);
856 leh = (struct link_ea_header *) buf->lb_buf;
857 lee = (struct link_ea_entry *)(leh + 1);
858 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
859 leh->leh_magic = LINK_EA_MAGIC;
860 leh->leh_reccount = __swab32(leh->leh_reccount);
861 leh->leh_len = __swab64(leh->leh_len);
863 if (leh->leh_magic != LINK_EA_MAGIC)
865 if (leh->leh_reccount == 0)
868 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
869 fid_be_to_cpu(pfid, pfid);
874 int mdt_attr_get_complex(struct mdt_thread_info *info,
875 struct mdt_object *o, struct md_attr *ma)
877 const struct lu_env *env = info->mti_env;
878 struct md_object *next = mdt_object_child(o);
879 struct lu_buf *buf = &info->mti_buf;
880 int need = ma->ma_need;
887 if (mdt_object_exists(o) == 0)
888 GOTO(out, rc = -ENOENT);
889 mode = lu_object_attr(&next->mo_lu);
891 if (need & MA_INODE) {
892 ma->ma_need = MA_INODE;
893 rc = mo_attr_get(env, next, ma);
896 ma->ma_valid |= MA_INODE;
899 if (need & MA_PFID) {
900 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
902 ma->ma_valid |= MA_PFID;
903 /* ignore this error, parent fid is not mandatory */
907 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
908 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
913 if (need & MA_LMV && S_ISDIR(mode)) {
914 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
919 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
920 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
925 if (need & MA_HSM && S_ISREG(mode)) {
926 buf->lb_buf = info->mti_xattr_buf;
927 buf->lb_len = sizeof(info->mti_xattr_buf);
928 CLASSERT(sizeof(struct hsm_attrs) <=
929 sizeof(info->mti_xattr_buf));
930 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
931 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
933 ma->ma_valid |= MA_HSM;
934 else if (rc2 < 0 && rc2 != -ENODATA)
938 #ifdef CONFIG_FS_POSIX_ACL
939 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
940 buf->lb_buf = ma->ma_acl;
941 buf->lb_len = ma->ma_acl_size;
942 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
944 ma->ma_acl_size = rc2;
945 ma->ma_valid |= MA_ACL_DEF;
946 } else if (rc2 == -ENODATA) {
955 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
956 rc, ma->ma_valid, ma->ma_lmm);
960 static int mdt_getattr_internal(struct mdt_thread_info *info,
961 struct mdt_object *o, int ma_need)
963 struct md_object *next = mdt_object_child(o);
964 const struct mdt_body *reqbody = info->mti_body;
965 struct ptlrpc_request *req = mdt_info_req(info);
966 struct md_attr *ma = &info->mti_attr;
967 struct lu_attr *la = &ma->ma_attr;
968 struct req_capsule *pill = info->mti_pill;
969 const struct lu_env *env = info->mti_env;
970 struct mdt_body *repbody;
971 struct lu_buf *buffer = &info->mti_buf;
972 struct obd_export *exp = info->mti_exp;
976 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
977 RETURN(err_serious(-ENOMEM));
979 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
983 if (mdt_object_remote(o)) {
984 /* This object is located on remote node.*/
985 /* Return -ENOTSUPP for old client */
986 if (!mdt_is_dne_client(req->rq_export))
987 GOTO(out, rc = -ENOTSUPP);
989 repbody->mbo_fid1 = *mdt_object_fid(o);
990 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
994 if (reqbody->mbo_eadatasize > 0) {
995 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
996 if (buffer->lb_buf == NULL)
997 GOTO(out, rc = -EPROTO);
998 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1001 buffer->lb_buf = NULL;
1003 ma_need &= ~(MA_LOV | MA_LMV);
1004 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1005 mdt_obd_name(info->mti_mdt),
1006 req->rq_export->exp_client_uuid.uuid);
1009 /* If it is dir object and client require MEA, then we got MEA */
1010 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1011 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1012 /* Assumption: MDT_MD size is enough for lmv size. */
1013 ma->ma_lmv = buffer->lb_buf;
1014 ma->ma_lmv_size = buffer->lb_len;
1015 ma->ma_need = MA_INODE;
1016 if (ma->ma_lmv_size > 0) {
1017 if (reqbody->mbo_valid & OBD_MD_MEA)
1018 ma->ma_need |= MA_LMV;
1019 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1020 ma->ma_need |= MA_LMV_DEF;
1023 ma->ma_lmm = buffer->lb_buf;
1024 ma->ma_lmm_size = buffer->lb_len;
1025 ma->ma_need = MA_INODE | MA_HSM;
1026 if (ma->ma_lmm_size > 0)
1027 ma->ma_need |= MA_LOV;
1030 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1031 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1032 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1033 /* get default stripe info for this dir. */
1034 ma->ma_need |= MA_LOV_DEF;
1036 ma->ma_need |= ma_need;
1038 rc = mdt_attr_get_complex(info, o, ma);
1040 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1041 "%s: getattr error for "DFID": rc = %d\n",
1042 mdt_obd_name(info->mti_mdt),
1043 PFID(mdt_object_fid(o)), rc);
1047 /* if file is released, check if a restore is running */
1048 if (ma->ma_valid & MA_HSM) {
1049 repbody->mbo_valid |= OBD_MD_TSTATE;
1050 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1051 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1052 repbody->mbo_t_state = MS_RESTORE;
1055 if (likely(ma->ma_valid & MA_INODE))
1056 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1060 if (mdt_body_has_lov(la, reqbody)) {
1061 if (ma->ma_valid & MA_LOV) {
1062 LASSERT(ma->ma_lmm_size);
1063 repbody->mbo_eadatasize = ma->ma_lmm_size;
1064 if (S_ISDIR(la->la_mode))
1065 repbody->mbo_valid |= OBD_MD_FLDIREA;
1067 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1068 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1070 if (ma->ma_valid & MA_LMV) {
1071 /* Return -ENOTSUPP for old client */
1072 if (!mdt_is_striped_client(req->rq_export))
1075 LASSERT(S_ISDIR(la->la_mode));
1076 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1077 repbody->mbo_eadatasize = ma->ma_lmv_size;
1078 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1080 if (ma->ma_valid & MA_LMV_DEF) {
1081 /* Return -ENOTSUPP for old client */
1082 if (!mdt_is_striped_client(req->rq_export))
1084 LASSERT(S_ISDIR(la->la_mode));
1085 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1086 repbody->mbo_eadatasize = ma->ma_lmv_size;
1087 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1088 OBD_MD_DEFAULT_MEA);
1090 } else if (S_ISLNK(la->la_mode) &&
1091 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1092 buffer->lb_buf = ma->ma_lmm;
1093 /* eadatasize from client includes NULL-terminator, so
1094 * there is no need to read it */
1095 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1096 rc = mo_readlink(env, next, buffer);
1097 if (unlikely(rc <= 0)) {
1098 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1099 mdt_obd_name(info->mti_mdt),
1100 PFID(mdt_object_fid(o)), rc);
1103 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1105 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1107 repbody->mbo_valid |= OBD_MD_LINKNAME;
1108 /* we need to report back size with NULL-terminator
1109 * because client expects that */
1110 repbody->mbo_eadatasize = rc + 1;
1111 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1112 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1113 "on "DFID ", expected %d\n",
1114 mdt_obd_name(info->mti_mdt),
1115 rc, PFID(mdt_object_fid(o)),
1116 reqbody->mbo_eadatasize - 1);
1117 /* NULL terminate */
1118 ((char *)ma->ma_lmm)[rc] = 0;
1120 /* If the total CDEBUG() size is larger than a page, it
1121 * will print a warning to the console, avoid this by
1122 * printing just the last part of the symlink. */
1123 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1124 print_limit < rc ? "..." : "", print_limit,
1125 (char *)ma->ma_lmm + rc - print_limit, rc);
1130 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1131 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1132 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1133 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1134 repbody->mbo_max_mdsize);
1137 #ifdef CONFIG_FS_POSIX_ACL
1138 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1139 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1140 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1141 if (IS_ERR(nodemap))
1142 RETURN(PTR_ERR(nodemap));
1144 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1145 nodemap_putref(nodemap);
1151 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1156 static int mdt_getattr(struct tgt_session_info *tsi)
1158 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1159 struct mdt_object *obj = info->mti_object;
1160 struct req_capsule *pill = info->mti_pill;
1161 struct mdt_body *reqbody;
1162 struct mdt_body *repbody;
1166 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1168 LASSERT(obj != NULL);
1169 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1171 /* Unlike intent case where we need to pre-fill out buffers early on
1172 * in intent policy for ldlm reasons, here we can have a much better
1173 * guess at EA size by just reading it from disk.
1174 * Exceptions are readdir and (missing) directory striping */
1176 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1177 /* No easy way to know how long is the symlink, but it cannot
1178 * be more than PATH_MAX, so we allocate +1 */
1181 /* A special case for fs ROOT: getattr there might fetch
1182 * default EA for entire fs, not just for this dir!
1184 } else if (lu_fid_eq(mdt_object_fid(obj),
1185 &info->mti_mdt->mdt_md_root_fid) &&
1186 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1187 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1189 /* Should the default strping be bigger, mdt_fix_reply
1190 * will reallocate */
1191 rc = DEF_REP_MD_SIZE;
1193 /* Read the actual EA size from disk */
1194 rc = mdt_attr_get_eabuf_size(info, obj);
1198 GOTO(out, rc = err_serious(rc));
1200 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1202 rc = req_capsule_server_pack(pill);
1203 if (unlikely(rc != 0))
1204 GOTO(out, rc = err_serious(rc));
1206 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1207 LASSERT(repbody != NULL);
1208 repbody->mbo_eadatasize = 0;
1209 repbody->mbo_aclsize = 0;
1211 rc = mdt_check_ucred(info);
1213 GOTO(out_shrink, rc);
1215 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1217 rc = mdt_getattr_internal(info, obj, 0);
1220 mdt_client_compatibility(info);
1221 rc2 = mdt_fix_reply(info);
1225 mdt_thread_info_fini(info);
1230 * Exchange MOF_LOV_CREATED flags between two objects after a
1231 * layout swap. No assumption is made on whether o1 or o2 have
1232 * created objects or not.
1234 * \param[in,out] o1 First swap layout object
1235 * \param[in,out] o2 Second swap layout object
1237 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1239 unsigned int o1_lov_created = o1->mot_lov_created;
1241 mutex_lock(&o1->mot_lov_mutex);
1242 mutex_lock(&o2->mot_lov_mutex);
1244 o1->mot_lov_created = o2->mot_lov_created;
1245 o2->mot_lov_created = o1_lov_created;
1247 mutex_unlock(&o2->mot_lov_mutex);
1248 mutex_unlock(&o1->mot_lov_mutex);
1251 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1253 struct mdt_thread_info *info;
1254 struct ptlrpc_request *req = tgt_ses_req(tsi);
1255 struct obd_export *exp = req->rq_export;
1256 struct mdt_object *o1, *o2, *o;
1257 struct mdt_lock_handle *lh1, *lh2;
1258 struct mdc_swap_layouts *msl;
1262 /* client does not support layout lock, so layout swaping
1264 * FIXME: there is a problem for old clients which don't support
1265 * layout lock yet. If those clients have already opened the file
1266 * they won't be notified at all so that old layout may still be
1267 * used to do IO. This can be fixed after file release is landed by
1268 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1269 if (!exp_connect_layout(exp))
1270 RETURN(-EOPNOTSUPP);
1272 info = tsi2mdt_info(tsi);
1274 if (info->mti_dlm_req != NULL)
1275 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1277 o1 = info->mti_object;
1278 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1279 &info->mti_body->mbo_fid2);
1281 GOTO(out, rc = PTR_ERR(o));
1283 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1284 GOTO(put, rc = -ENOENT);
1286 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1287 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1293 /* permission check. Make sure the calling process having permission
1294 * to write both files. */
1295 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1300 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1305 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1307 GOTO(put, rc = -EPROTO);
1309 lh1 = &info->mti_lh[MDT_LH_NEW];
1310 mdt_lock_reg_init(lh1, LCK_EX);
1311 lh2 = &info->mti_lh[MDT_LH_OLD];
1312 mdt_lock_reg_init(lh2, LCK_EX);
1314 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1315 MDS_INODELOCK_XATTR);
1319 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1320 MDS_INODELOCK_XATTR);
1324 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1325 mdt_object_child(o2), msl->msl_flags);
1329 mdt_swap_lov_flag(o1, o2);
1332 mdt_object_unlock(info, o2, lh2, rc);
1334 mdt_object_unlock(info, o1, lh1, rc);
1336 mdt_object_put(info->mti_env, o);
1338 mdt_thread_info_fini(info);
1342 static int mdt_raw_lookup(struct mdt_thread_info *info,
1343 struct mdt_object *parent,
1344 const struct lu_name *lname,
1345 struct ldlm_reply *ldlm_rep)
1347 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1351 LASSERT(!info->mti_cross_ref);
1353 /* Only got the fid of this obj by name */
1354 fid_zero(child_fid);
1355 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1356 lname, child_fid, &info->mti_spec);
1358 struct mdt_body *repbody;
1360 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1361 repbody->mbo_fid1 = *child_fid;
1362 repbody->mbo_valid = OBD_MD_FLID;
1363 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1364 } else if (rc == -ENOENT) {
1365 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1372 * UPDATE lock should be taken against parent, and be released before exit;
1373 * child_bits lock should be taken against child, and be returned back:
1374 * (1)normal request should release the child lock;
1375 * (2)intent request will grant the lock to client.
1377 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1378 struct mdt_lock_handle *lhc,
1380 struct ldlm_reply *ldlm_rep)
1382 struct ptlrpc_request *req = mdt_info_req(info);
1383 struct mdt_body *reqbody = NULL;
1384 struct mdt_object *parent = info->mti_object;
1385 struct mdt_object *child;
1386 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1387 struct lu_name *lname = NULL;
1388 struct mdt_lock_handle *lhp = NULL;
1389 struct ldlm_lock *lock;
1396 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1397 LASSERT(ergo(is_resent,
1398 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1403 if (info->mti_cross_ref) {
1404 /* Only getattr on the child. Parent is on another node. */
1405 mdt_set_disposition(info, ldlm_rep,
1406 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1408 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1410 PFID(mdt_object_fid(child)), ldlm_rep);
1412 rc = mdt_check_resent_lock(info, child, lhc);
1415 } else if (rc > 0) {
1416 mdt_lock_handle_init(lhc);
1417 mdt_lock_reg_init(lhc, LCK_PR);
1420 * Object's name is on another MDS, no lookup or layout
1421 * lock is needed here but update lock is.
1423 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1424 MDS_INODELOCK_LAYOUT);
1425 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1427 rc = mdt_object_lock(info, child, lhc, child_bits);
1432 /* Finally, we can get attr for child. */
1433 if (!mdt_object_exists(child)) {
1434 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1436 "remote object doesn't exist.");
1437 mdt_object_unlock(info, child, lhc, 1);
1441 rc = mdt_getattr_internal(info, child, 0);
1442 if (unlikely(rc != 0))
1443 mdt_object_unlock(info, child, lhc, 1);
1448 lname = &info->mti_name;
1449 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1451 if (lu_name_is_valid(lname)) {
1452 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1453 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1454 PNAME(lname), ldlm_rep);
1456 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1457 if (unlikely(reqbody == NULL))
1458 RETURN(err_serious(-EPROTO));
1460 *child_fid = reqbody->mbo_fid2;
1462 if (unlikely(!fid_is_sane(child_fid)))
1463 RETURN(err_serious(-EINVAL));
1465 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1467 PFID(mdt_object_fid(parent)),
1468 PFID(&reqbody->mbo_fid2), ldlm_rep);
1471 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1473 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1474 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1476 "Parent doesn't exist!");
1480 if (mdt_object_remote(parent)) {
1481 CERROR("%s: parent "DFID" is on remote target\n",
1482 mdt_obd_name(info->mti_mdt),
1483 PFID(mdt_object_fid(parent)));
1487 if (lu_name_is_valid(lname)) {
1488 /* Always allow to lookup ".." */
1489 if (unlikely(lname->ln_namelen == 2 &&
1490 lname->ln_name[0] == '.' &&
1491 lname->ln_name[1] == '.'))
1492 info->mti_spec.sp_permitted = 1;
1494 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1495 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1500 /* step 1: lock parent only if parent is a directory */
1501 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1502 lhp = &info->mti_lh[MDT_LH_PARENT];
1503 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1504 rc = mdt_object_lock(info, parent, lhp,
1505 MDS_INODELOCK_UPDATE);
1506 if (unlikely(rc != 0))
1510 /* step 2: lookup child's fid by name */
1511 fid_zero(child_fid);
1512 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1513 child_fid, &info->mti_spec);
1515 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1518 GOTO(out_parent, rc);
1521 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1524 *step 3: find the child object by fid & lock it.
1525 * regardless if it is local or remote.
1527 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1528 * set parent dir fid the same as child fid in getattr by fid case
1529 * we should not lu_object_find() the object again, could lead
1530 * to hung if there is a concurrent unlink destroyed the object.
1532 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1533 mdt_object_get(info->mti_env, parent);
1536 child = mdt_object_find(info->mti_env, info->mti_mdt,
1540 if (unlikely(IS_ERR(child)))
1541 GOTO(out_parent, rc = PTR_ERR(child));
1543 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1544 if (!mdt_object_exists(child)) {
1545 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1547 "Object doesn't exist!");
1548 GOTO(out_child, rc = -ENOENT);
1551 rc = mdt_check_resent_lock(info, child, lhc);
1553 GOTO(out_child, rc);
1554 } else if (rc > 0) {
1555 mdt_lock_handle_init(lhc);
1556 mdt_lock_reg_init(lhc, LCK_PR);
1559 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1560 mdt_object_exists(child) && !mdt_object_remote(child)) {
1561 struct md_attr *ma = &info->mti_attr;
1564 ma->ma_need = MA_INODE;
1565 rc = mdt_attr_get_complex(info, child, ma);
1566 if (unlikely(rc != 0))
1567 GOTO(out_child, rc);
1569 /* If the file has not been changed for some time, we
1570 * return not only a LOOKUP lock, but also an UPDATE
1571 * lock and this might save us RPC on later STAT. For
1572 * directories, it also let negative dentry cache start
1573 * working for this dir. */
1574 if (ma->ma_valid & MA_INODE &&
1575 ma->ma_attr.la_valid & LA_CTIME &&
1576 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1577 ma->ma_attr.la_ctime < cfs_time_current_sec())
1578 child_bits |= MDS_INODELOCK_UPDATE;
1581 /* layout lock must be granted in a best-effort way
1582 * for IT operations */
1583 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1584 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1585 exp_connect_layout(info->mti_exp) &&
1586 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1587 !mdt_object_remote(child) && ldlm_rep != NULL) {
1588 /* try to grant layout lock for regular file. */
1594 child_bits |= MDS_INODELOCK_LAYOUT;
1595 /* try layout lock, it may fail to be granted due to
1596 * contention at LOOKUP or UPDATE */
1597 if (!mdt_object_lock_try(info, child, lhc,
1599 child_bits &= ~MDS_INODELOCK_LAYOUT;
1600 LASSERT(child_bits != 0);
1601 rc = mdt_object_lock(info, child, lhc,
1607 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1608 * client will enqueue the lock to the remote MDT */
1609 if (mdt_object_remote(child))
1610 child_bits &= ~MDS_INODELOCK_UPDATE;
1611 rc = mdt_object_lock(info, child, lhc, child_bits);
1613 if (unlikely(rc != 0))
1614 GOTO(out_child, rc);
1617 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1619 /* finally, we can get attr for child. */
1620 rc = mdt_getattr_internal(info, child, ma_need);
1621 if (unlikely(rc != 0)) {
1622 mdt_object_unlock(info, child, lhc, 1);
1624 /* Debugging code. */
1625 LDLM_DEBUG(lock, "Returning lock to client");
1626 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1627 &lock->l_resource->lr_name),
1628 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1629 PLDLMRES(lock->l_resource),
1630 PFID(mdt_object_fid(child)));
1633 LDLM_LOCK_PUT(lock);
1637 mdt_object_put(info->mti_env, child);
1640 mdt_object_unlock(info, parent, lhp, 1);
1644 /* normal handler: should release the child lock */
1645 static int mdt_getattr_name(struct tgt_session_info *tsi)
1647 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1648 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1649 struct mdt_body *reqbody;
1650 struct mdt_body *repbody;
1654 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1655 LASSERT(reqbody != NULL);
1656 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1657 LASSERT(repbody != NULL);
1659 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1660 repbody->mbo_eadatasize = 0;
1661 repbody->mbo_aclsize = 0;
1663 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1665 GOTO(out_shrink, rc);
1667 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1668 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1669 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1670 lhc->mlh_reg_lh.cookie = 0;
1672 mdt_exit_ucred(info);
1675 mdt_client_compatibility(info);
1676 rc2 = mdt_fix_reply(info);
1679 mdt_thread_info_fini(info);
1683 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1684 void *karg, void __user *uarg);
1686 static int mdt_set_info(struct tgt_session_info *tsi)
1688 struct ptlrpc_request *req = tgt_ses_req(tsi);
1691 int keylen, vallen, rc = 0;
1695 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1697 DEBUG_REQ(D_HA, req, "no set_info key");
1698 RETURN(err_serious(-EFAULT));
1701 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1704 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1706 DEBUG_REQ(D_HA, req, "no set_info val");
1707 RETURN(err_serious(-EFAULT));
1710 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1713 /* Swab any part of val you need to here */
1714 if (KEY_IS(KEY_READ_ONLY)) {
1715 spin_lock(&req->rq_export->exp_lock);
1717 *exp_connect_flags_ptr(req->rq_export) |=
1720 *exp_connect_flags_ptr(req->rq_export) &=
1721 ~OBD_CONNECT_RDONLY;
1722 spin_unlock(&req->rq_export->exp_lock);
1723 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1724 struct changelog_setinfo *cs = val;
1726 if (vallen != sizeof(*cs)) {
1727 CERROR("%s: bad changelog_clear setinfo size %d\n",
1728 tgt_name(tsi->tsi_tgt), vallen);
1731 if (ptlrpc_req_need_swab(req)) {
1732 __swab64s(&cs->cs_recno);
1733 __swab32s(&cs->cs_id);
1736 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1738 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1740 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1747 static int mdt_readpage(struct tgt_session_info *tsi)
1749 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1750 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1751 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1752 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1753 struct mdt_body *repbody;
1759 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1760 RETURN(err_serious(-ENOMEM));
1762 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1763 if (repbody == NULL || reqbody == NULL)
1764 RETURN(err_serious(-EFAULT));
1767 * prepare @rdpg before calling lower layers and transfer itself. Here
1768 * reqbody->size contains offset of where to start to read and
1769 * reqbody->nlink contains number bytes to read.
1771 rdpg->rp_hash = reqbody->mbo_size;
1772 if (rdpg->rp_hash != reqbody->mbo_size) {
1773 CERROR("Invalid hash: %#llx != %#llx\n",
1774 rdpg->rp_hash, reqbody->mbo_size);
1778 rdpg->rp_attrs = reqbody->mbo_mode;
1779 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1780 rdpg->rp_attrs |= LUDA_64BITHASH;
1781 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1782 exp_max_brw_size(tsi->tsi_exp));
1783 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1785 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1786 if (rdpg->rp_pages == NULL)
1789 for (i = 0; i < rdpg->rp_npages; ++i) {
1790 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1791 if (rdpg->rp_pages[i] == NULL)
1792 GOTO(free_rdpg, rc = -ENOMEM);
1795 /* call lower layers to fill allocated pages with directory data */
1796 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1798 GOTO(free_rdpg, rc);
1800 /* send pages to client */
1801 rc = tgt_sendpage(tsi, rdpg, rc);
1806 for (i = 0; i < rdpg->rp_npages; i++)
1807 if (rdpg->rp_pages[i] != NULL)
1808 __free_page(rdpg->rp_pages[i]);
1809 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1811 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1817 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1819 struct lu_ucred *uc = mdt_ucred_check(info);
1820 struct lu_attr *attr = &info->mti_attr.ma_attr;
1825 if (op != REINT_SETATTR) {
1826 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1827 attr->la_uid = uc->uc_fsuid;
1828 /* for S_ISGID, inherit gid from his parent, such work will be
1829 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1830 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1831 attr->la_gid = uc->uc_fsgid;
1837 static int mdt_reint_internal(struct mdt_thread_info *info,
1838 struct mdt_lock_handle *lhc,
1841 struct req_capsule *pill = info->mti_pill;
1842 struct mdt_body *repbody;
1847 rc = mdt_reint_unpack(info, op);
1849 CERROR("Can't unpack reint, rc %d\n", rc);
1850 RETURN(err_serious(rc));
1853 /* for replay (no_create) lmm is not needed, client has it already */
1854 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1855 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1858 /* llog cookies are always 0, the field is kept for compatibility */
1859 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1860 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1862 rc = req_capsule_server_pack(pill);
1864 CERROR("Can't pack response, rc %d\n", rc);
1865 RETURN(err_serious(rc));
1868 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1869 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1871 repbody->mbo_eadatasize = 0;
1872 repbody->mbo_aclsize = 0;
1875 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1877 /* for replay no cookkie / lmm need, because client have this already */
1878 if (info->mti_spec.no_create)
1879 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1880 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1882 rc = mdt_init_ucred_reint(info);
1884 GOTO(out_shrink, rc);
1886 rc = mdt_fix_attr_ucred(info, op);
1888 GOTO(out_ucred, rc = err_serious(rc));
1890 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1892 GOTO(out_ucred, rc);
1893 } else if (rc == 1) {
1894 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1895 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1896 GOTO(out_ucred, rc);
1898 rc = mdt_reint_rec(info, lhc);
1901 mdt_exit_ucred(info);
1903 mdt_client_compatibility(info);
1904 rc2 = mdt_fix_reply(info);
1910 static long mdt_reint_opcode(struct ptlrpc_request *req,
1911 const struct req_format **fmt)
1913 struct mdt_device *mdt;
1914 struct mdt_rec_reint *rec;
1917 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1919 opc = rec->rr_opcode;
1920 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1921 if (opc < REINT_MAX && fmt[opc] != NULL)
1922 req_capsule_extend(&req->rq_pill, fmt[opc]);
1924 mdt = mdt_exp2dev(req->rq_export);
1925 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1926 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1927 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1928 opc = err_serious(-EFAULT);
1931 opc = err_serious(-EFAULT);
1936 static int mdt_reint(struct tgt_session_info *tsi)
1940 static const struct req_format *reint_fmts[REINT_MAX] = {
1941 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1942 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1943 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1944 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1945 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1946 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1947 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1948 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1949 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1954 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1956 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1958 * No lock possible here from client to pass it to reint code
1961 rc = mdt_reint_internal(info, NULL, opc);
1962 mdt_thread_info_fini(info);
1967 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1971 /* this should sync the whole device */
1972 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1974 struct dt_device *dt = mdt->mdt_bottom;
1978 rc = dt->dd_ops->dt_sync(env, dt);
1982 /* this should sync this object */
1983 static int mdt_object_sync(struct mdt_thread_info *info)
1985 struct md_object *next;
1989 if (!mdt_object_exists(info->mti_object)) {
1990 CWARN("%s: non existing object "DFID": rc = %d\n",
1991 mdt_obd_name(info->mti_mdt),
1992 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1995 next = mdt_object_child(info->mti_object);
1996 rc = mo_object_sync(info->mti_env, next);
2001 static int mdt_sync(struct tgt_session_info *tsi)
2003 struct ptlrpc_request *req = tgt_ses_req(tsi);
2004 struct req_capsule *pill = tsi->tsi_pill;
2005 struct mdt_body *body;
2010 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2011 RETURN(err_serious(-ENOMEM));
2013 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2014 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2016 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2018 /* sync an object */
2019 rc = mdt_object_sync(info);
2021 const struct lu_fid *fid;
2022 struct lu_attr *la = &info->mti_attr.ma_attr;
2024 info->mti_attr.ma_need = MA_INODE;
2025 info->mti_attr.ma_valid = 0;
2026 rc = mdt_attr_get_complex(info, info->mti_object,
2029 body = req_capsule_server_get(pill,
2031 fid = mdt_object_fid(info->mti_object);
2032 mdt_pack_attr2body(info, body, la, fid);
2035 mdt_thread_info_fini(info);
2038 mdt_counter_incr(req, LPROC_MDT_SYNC);
2044 * Handle quota control requests to consult current usage/limit, but also
2045 * to configure quota enforcement
2047 static int mdt_quotactl(struct tgt_session_info *tsi)
2049 struct obd_export *exp = tsi->tsi_exp;
2050 struct req_capsule *pill = tsi->tsi_pill;
2051 struct obd_quotactl *oqctl, *repoqc;
2053 struct mdt_device *mdt = mdt_exp2dev(exp);
2054 struct lu_device *qmt = mdt->mdt_qmt_dev;
2055 struct lu_nodemap *nodemap;
2058 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2060 RETURN(err_serious(-EPROTO));
2062 rc = req_capsule_server_pack(pill);
2064 RETURN(err_serious(rc));
2066 nodemap = nodemap_get_from_exp(exp);
2067 if (IS_ERR(nodemap))
2068 RETURN(PTR_ERR(nodemap));
2070 switch (oqctl->qc_cmd) {
2071 /* master quotactl */
2074 if (!nodemap_can_setquota(nodemap))
2075 GOTO(out_nodemap, rc = -EPERM);
2079 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2080 /* slave quotactl */
2085 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2086 GOTO(out_nodemap, rc = -EFAULT);
2090 if (oqctl->qc_type == USRQUOTA)
2091 id = nodemap_map_id(nodemap, NODEMAP_UID,
2092 NODEMAP_CLIENT_TO_FS, id);
2093 else if (oqctl->qc_type == GRPQUOTA)
2094 id = nodemap_map_id(nodemap, NODEMAP_UID,
2095 NODEMAP_CLIENT_TO_FS, id);
2097 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2099 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2101 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2102 barrier_exit(tsi->tsi_tgt->lut_bottom);
2104 if (oqctl->qc_id != id)
2105 swap(oqctl->qc_id, id);
2107 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2108 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2109 RETURN(-EINPROGRESS);
2112 switch (oqctl->qc_cmd) {
2118 /* forward quotactl request to QMT */
2119 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2124 /* slave quotactl */
2125 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2130 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2131 GOTO(out_nodemap, rc = -EFAULT);
2134 if (oqctl->qc_id != id)
2135 swap(oqctl->qc_id, id);
2142 nodemap_putref(nodemap);
2147 /** clone llog ctxt from child (mdd)
2148 * This allows remote llog (replicator) access.
2149 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2150 * context was originally set up, or we can handle them directly.
2151 * I choose the latter, but that means I need any llog
2152 * contexts set up by child to be accessable by the mdt. So we clone the
2153 * context into our context list here.
2155 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2158 struct md_device *next = mdt->mdt_child;
2159 struct llog_ctxt *ctxt;
2162 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2165 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2166 if (rc || ctxt == NULL) {
2170 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2172 CERROR("Can't set mdt ctxt %d\n", rc);
2177 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2178 struct mdt_device *mdt, int idx)
2180 struct llog_ctxt *ctxt;
2182 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2185 /* Put once for the get we just did, and once for the clone */
2186 llog_ctxt_put(ctxt);
2187 llog_ctxt_put(ctxt);
2192 * sec context handlers
2194 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2196 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2202 * quota request handlers
2204 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2206 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2207 struct lu_device *qmt = mdt->mdt_qmt_dev;
2212 RETURN(err_serious(-EOPNOTSUPP));
2214 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2218 struct mdt_object *mdt_object_new(const struct lu_env *env,
2219 struct mdt_device *d,
2220 const struct lu_fid *f)
2222 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2223 struct lu_object *o;
2224 struct mdt_object *m;
2227 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2228 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2229 if (unlikely(IS_ERR(o)))
2230 m = (struct mdt_object *)o;
2236 struct mdt_object *mdt_object_find(const struct lu_env *env,
2237 struct mdt_device *d,
2238 const struct lu_fid *f)
2240 struct lu_object *o;
2241 struct mdt_object *m;
2244 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2245 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2246 if (unlikely(IS_ERR(o)))
2247 m = (struct mdt_object *)o;
2255 * Asyncronous commit for mdt device.
2257 * Pass asynchonous commit call down the MDS stack.
2259 * \param env environment
2260 * \param mdt the mdt device
2262 static void mdt_device_commit_async(const struct lu_env *env,
2263 struct mdt_device *mdt)
2265 struct dt_device *dt = mdt->mdt_bottom;
2269 rc = dt->dd_ops->dt_commit_async(env, dt);
2270 if (unlikely(rc != 0))
2271 CWARN("%s: async commit start failed: rc = %d\n",
2272 mdt_obd_name(mdt), rc);
2273 atomic_inc(&mdt->mdt_async_commit_count);
2278 * Mark the lock as "synchonous".
2280 * Mark the lock to deffer transaction commit to the unlock time.
2282 * \param lock the lock to mark as "synchonous"
2284 * \see mdt_is_lock_sync
2285 * \see mdt_save_lock
2287 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2289 lock->l_ast_data = (void*)1;
2293 * Check whehter the lock "synchonous" or not.
2295 * \param lock the lock to check
2296 * \retval 1 the lock is "synchonous"
2297 * \retval 0 the lock isn't "synchronous"
2299 * \see mdt_set_lock_sync
2300 * \see mdt_save_lock
2302 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2304 return lock->l_ast_data != NULL;
2308 * Blocking AST for mdt locks.
2310 * Starts transaction commit if in case of COS lock conflict or
2311 * deffers such a commit to the mdt_save_lock.
2313 * \param lock the lock which blocks a request or cancelling lock
2314 * \param desc unused
2315 * \param data unused
2316 * \param flag indicates whether this cancelling or blocking callback
2318 * \see ldlm_blocking_ast_nocheck
2320 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2321 void *data, int flag)
2323 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2324 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2328 if (flag == LDLM_CB_CANCELING)
2331 lock_res_and_lock(lock);
2332 if (lock->l_blocking_ast != mdt_blocking_ast) {
2333 unlock_res_and_lock(lock);
2336 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2337 lock->l_blocking_lock != NULL) {
2338 if (mdt_cos_is_enabled(mdt) &&
2339 lock->l_client_cookie !=
2340 lock->l_blocking_lock->l_client_cookie)
2341 mdt_set_lock_sync(lock);
2342 else if (mdt_slc_is_enabled(mdt) &&
2343 ldlm_is_cos_incompat(lock->l_blocking_lock))
2344 mdt_set_lock_sync(lock);
2346 rc = ldlm_blocking_ast_nocheck(lock);
2348 /* There is no lock conflict if l_blocking_lock == NULL,
2349 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2350 * when the last reference to a local lock was released */
2351 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2354 rc = lu_env_init(&env, LCT_LOCAL);
2355 if (unlikely(rc != 0))
2356 CWARN("%s: lu_env initialization failed, cannot "
2357 "start asynchronous commit: rc = %d\n",
2360 mdt_device_commit_async(&env, mdt);
2367 * Blocking AST for cross-MDT lock
2369 * Discard lock from uncommitted_slc_locks and cancel it.
2371 * \param lock the lock which blocks a request or cancelling lock
2372 * \param desc unused
2373 * \param data unused
2374 * \param flag indicates whether this cancelling or blocking callback
2375 * \retval 0 on success
2376 * \retval negative number on error
2378 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2379 void *data, int flag)
2385 case LDLM_CB_BLOCKING: {
2386 struct lustre_handle lockh;
2388 ldlm_lock2handle(lock, &lockh);
2389 rc = ldlm_cli_cancel(&lockh,
2390 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2392 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2397 case LDLM_CB_CANCELING: {
2398 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2399 struct mdt_device *mdt =
2400 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2402 LDLM_DEBUG(lock, "Revoke remote lock\n");
2404 /* discard slc lock here so that it can be cleaned anytime,
2405 * especially for cleanup_resource() */
2406 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2408 /* once we cache lock, l_ast_data is set to mdt_object */
2409 if (lock->l_ast_data != NULL) {
2410 struct mdt_object *mo = lock->l_ast_data;
2413 rc = lu_env_init(&env, LCT_MD_THREAD);
2414 if (unlikely(rc != 0)) {
2415 CWARN("%s: lu_env initialization failed, object"
2416 "%p "DFID" is leaked!\n",
2418 PFID(mdt_object_fid(mo)));
2422 if (lock->l_policy_data.l_inodebits.bits &
2423 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2424 rc = mo_invalidate(&env, mdt_object_child(mo));
2425 mo->mot_cache_attr = 0;
2427 mdt_object_put(&env, mo);
2439 int mdt_check_resent_lock(struct mdt_thread_info *info,
2440 struct mdt_object *mo,
2441 struct mdt_lock_handle *lhc)
2443 /* the lock might already be gotten in ldlm_handle_enqueue() */
2444 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2445 struct ptlrpc_request *req = mdt_info_req(info);
2446 struct ldlm_lock *lock;
2448 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2449 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2451 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2452 * a resend case, however, it could be already destroyed
2453 * due to client eviction or a raced cancel RPC. */
2454 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2455 lhc->mlh_reg_lh.cookie);
2459 if (!fid_res_name_eq(mdt_object_fid(mo),
2460 &lock->l_resource->lr_name)) {
2461 CWARN("%s: Although resent, but still not "
2462 "get child lock:"DFID"\n",
2463 info->mti_exp->exp_obd->obd_name,
2464 PFID(mdt_object_fid(mo)));
2465 LDLM_LOCK_PUT(lock);
2468 LDLM_LOCK_PUT(lock);
2474 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2475 const struct lu_fid *fid, struct lustre_handle *lh,
2476 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2479 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2480 union ldlm_policy_data *policy = &mti->mti_policy;
2481 struct ldlm_res_id *res_id = &mti->mti_res_id;
2485 LASSERT(mdt_object_remote(o));
2487 fid_build_reg_res_name(fid, res_id);
2489 memset(einfo, 0, sizeof(*einfo));
2490 einfo->ei_type = LDLM_IBITS;
2491 einfo->ei_mode = mode;
2492 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2493 einfo->ei_cb_cp = ldlm_completion_ast;
2494 einfo->ei_enq_slave = 0;
2495 einfo->ei_res_id = res_id;
2497 einfo->ei_nonblock = 1;
2500 * if we cache lock, couple lock with mdt_object, so that object
2501 * can be easily found in lock ASTs.
2503 mdt_object_get(mti->mti_env, o);
2504 einfo->ei_cbdata = o;
2507 memset(policy, 0, sizeof(*policy));
2508 policy->l_inodebits.bits = ibits;
2510 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2512 if (rc < 0 && cache) {
2513 mdt_object_put(mti->mti_env, o);
2514 einfo->ei_cbdata = NULL;
2519 static int mdt_object_local_lock(struct mdt_thread_info *info,
2520 struct mdt_object *o,
2521 struct mdt_lock_handle *lh, __u64 ibits,
2522 bool nonblock, bool cos_incompat)
2524 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2525 union ldlm_policy_data *policy = &info->mti_policy;
2526 struct ldlm_res_id *res_id = &info->mti_res_id;
2531 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2532 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2533 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2534 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2537 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2538 lh->mlh_reg_mode == LCK_EX);
2539 dlmflags |= LDLM_FL_COS_INCOMPAT;
2540 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2541 dlmflags |= LDLM_FL_COS_ENABLED;
2544 /* Only enqueue LOOKUP lock for remote object */
2545 if (mdt_object_remote(o))
2546 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2548 if (lh->mlh_type == MDT_PDO_LOCK) {
2549 /* check for exists after object is locked */
2550 if (mdt_object_exists(o) == 0) {
2551 /* Non-existent object shouldn't have PDO lock */
2554 /* Non-dir object shouldn't have PDO lock */
2555 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2560 memset(policy, 0, sizeof(*policy));
2561 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2563 dlmflags |= LDLM_FL_ATOMIC_CB;
2565 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2568 * Take PDO lock on whole directory and build correct @res_id for lock
2569 * on part of directory.
2571 if (lh->mlh_pdo_hash != 0) {
2572 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2573 mdt_lock_pdo_mode(info, o, lh);
2574 if (lh->mlh_pdo_mode != LCK_NL) {
2576 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2577 * is never going to be sent to client and we do not
2578 * want it slowed down due to possible cancels.
2580 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2581 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2582 policy, res_id, dlmflags,
2583 info->mti_exp == NULL ? NULL :
2584 &info->mti_exp->exp_handle.h_cookie);
2585 if (unlikely(rc != 0))
2586 GOTO(out_unlock, rc);
2590 * Finish res_id initializing by name hash marking part of
2591 * directory which is taking modification.
2593 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2596 policy->l_inodebits.bits = ibits;
2599 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2600 * going to be sent to client. If it is - mdt_intent_policy() path will
2601 * fix it up and turn FL_LOCAL flag off.
2603 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2604 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2605 info->mti_exp == NULL ? NULL :
2606 &info->mti_exp->exp_handle.h_cookie);
2609 mdt_object_unlock(info, o, lh, 1);
2610 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2611 lh->mlh_pdo_hash != 0 &&
2612 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2613 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2619 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2620 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2623 struct mdt_lock_handle *local_lh = NULL;
2627 if (!mdt_object_remote(o)) {
2628 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2633 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2634 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2635 MDS_INODELOCK_XATTR);
2637 /* Only enqueue LOOKUP lock for remote object */
2638 if (ibits & MDS_INODELOCK_LOOKUP) {
2639 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2640 nonblock, cos_incompat);
2647 if (ibits & MDS_INODELOCK_UPDATE) {
2648 /* Sigh, PDO needs to enqueue 2 locks right now, but
2649 * enqueue RPC can only request 1 lock, to avoid extra
2650 * RPC, so it will instead enqueue EX lock for remote
2651 * object anyway XXX*/
2652 if (lh->mlh_type == MDT_PDO_LOCK &&
2653 lh->mlh_pdo_hash != 0) {
2654 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2655 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2656 PFID(mdt_object_fid(o)));
2657 lh->mlh_pdo_hash = 0;
2658 lh->mlh_rreg_mode = LCK_EX;
2659 lh->mlh_type = MDT_REG_LOCK;
2661 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2664 MDS_INODELOCK_UPDATE, nonblock,
2666 if (rc != ELDLM_OK) {
2667 if (local_lh != NULL)
2668 mdt_object_unlock(info, o, local_lh, rc);
2676 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2677 struct mdt_lock_handle *lh, __u64 ibits)
2679 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2682 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2683 struct mdt_lock_handle *lh, __u64 ibits,
2686 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2687 return mdt_object_lock_internal(info, o, lh, ibits, false,
2691 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2692 struct mdt_lock_handle *lh, __u64 ibits)
2694 struct mdt_lock_handle tmp = *lh;
2697 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2704 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2705 struct mdt_object *o, struct mdt_lock_handle *lh,
2706 __u64 ibits, bool cos_incompat)
2708 struct mdt_lock_handle tmp = *lh;
2711 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2712 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2720 * Save a lock within request object.
2722 * Keep the lock referenced until whether client ACK or transaction
2723 * commit happens or release the lock immediately depending on input
2724 * parameters. If COS is ON, a write lock is converted to COS lock
2727 * \param info thead info object
2728 * \param h lock handle
2729 * \param mode lock mode
2730 * \param decref force immediate lock releasing
2732 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2733 enum ldlm_mode mode, int decref)
2737 if (lustre_handle_is_used(h)) {
2738 if (decref || !info->mti_has_trans ||
2739 !(mode & (LCK_PW | LCK_EX))) {
2740 mdt_fid_unlock(h, mode);
2742 struct mdt_device *mdt = info->mti_mdt;
2743 struct ldlm_lock *lock = ldlm_handle2lock(h);
2744 struct ptlrpc_request *req = mdt_info_req(info);
2747 cos = (mdt_cos_is_enabled(mdt) ||
2748 mdt_slc_is_enabled(mdt));
2750 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2753 /* there is no request if mdt_object_unlock() is called
2754 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2755 if (likely(req != NULL)) {
2756 CDEBUG(D_HA, "request = %p reply state = %p"
2757 " transno = %lld\n", req,
2758 req->rq_reply_state, req->rq_transno);
2760 ldlm_lock_downgrade(lock, LCK_COS);
2763 ptlrpc_save_lock(req, h, mode, cos);
2765 mdt_fid_unlock(h, mode);
2767 if (mdt_is_lock_sync(lock)) {
2768 CDEBUG(D_HA, "found sync-lock,"
2769 " async commit started\n");
2770 mdt_device_commit_async(info->mti_env,
2773 LDLM_LOCK_PUT(lock);
2782 * Save cross-MDT lock in uncommitted_slc_locks
2784 * Keep the lock referenced until transaction commit happens or release the lock
2785 * immediately depending on input parameters.
2787 * \param info thead info object
2788 * \param h lock handle
2789 * \param mode lock mode
2790 * \param decref force immediate lock releasing
2792 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2793 struct mdt_object *o, struct lustre_handle *h,
2794 enum ldlm_mode mode, int decref)
2798 if (lustre_handle_is_used(h)) {
2799 struct ldlm_lock *lock = ldlm_handle2lock(h);
2802 (lock->l_policy_data.l_inodebits.bits &
2803 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2804 mo_invalidate(info->mti_env, mdt_object_child(o));
2806 if (decref || !info->mti_has_trans ||
2807 !(mode & (LCK_PW | LCK_EX))) {
2808 ldlm_lock_decref_and_cancel(h, mode);
2809 LDLM_LOCK_PUT(lock);
2811 struct ptlrpc_request *req = mdt_info_req(info);
2813 LASSERT(req != NULL);
2814 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2816 ldlm_lock_decref(h, mode);
2825 * Unlock mdt object.
2827 * Immeditely release the regular lock and the PDO lock or save the
2828 * lock in request and keep them referenced until client ACK or
2829 * transaction commit.
2831 * \param info thread info object
2832 * \param o mdt object
2833 * \param lh mdt lock handle referencing regular and PDO locks
2834 * \param decref force immediate lock releasing
2836 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2838 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2839 struct mdt_lock_handle *lh, int decref)
2843 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2844 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2845 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2851 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2852 const struct lu_fid *f,
2853 struct mdt_lock_handle *lh,
2856 struct mdt_object *o;
2858 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2862 rc = mdt_object_lock(info, o, lh, ibits);
2864 mdt_object_put(info->mti_env, o);
2871 void mdt_object_unlock_put(struct mdt_thread_info * info,
2872 struct mdt_object * o,
2873 struct mdt_lock_handle *lh,
2876 mdt_object_unlock(info, o, lh, decref);
2877 mdt_object_put(info->mti_env, o);
2881 * Generic code handling requests that have struct mdt_body passed in:
2883 * - extract mdt_body from request and save it in @info, if present;
2885 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2888 * - if HABEO_CORPUS flag is set for this request type check whether object
2889 * actually exists on storage (lu_object_exists()).
2892 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2894 const struct mdt_body *body;
2895 struct mdt_object *obj;
2896 const struct lu_env *env;
2897 struct req_capsule *pill;
2901 env = info->mti_env;
2902 pill = info->mti_pill;
2904 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2908 if (!(body->mbo_valid & OBD_MD_FLID))
2911 if (!fid_is_sane(&body->mbo_fid1)) {
2912 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2916 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2918 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2919 mdt_object_put(env, obj);
2922 info->mti_object = obj;
2931 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2933 struct req_capsule *pill = info->mti_pill;
2937 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2938 rc = mdt_body_unpack(info, flags);
2942 if (rc == 0 && (flags & HABEO_REFERO)) {
2944 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2945 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2947 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2948 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2951 rc = req_capsule_server_pack(pill);
2956 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2958 lh->mlh_type = MDT_NUL_LOCK;
2959 lh->mlh_reg_lh.cookie = 0ull;
2960 lh->mlh_reg_mode = LCK_MINMODE;
2961 lh->mlh_pdo_lh.cookie = 0ull;
2962 lh->mlh_pdo_mode = LCK_MINMODE;
2963 lh->mlh_rreg_lh.cookie = 0ull;
2964 lh->mlh_rreg_mode = LCK_MINMODE;
2967 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2969 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2970 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2974 * Initialize fields of struct mdt_thread_info. Other fields are left in
2975 * uninitialized state, because it's too expensive to zero out whole
2976 * mdt_thread_info (> 1K) on each request arrival.
2978 void mdt_thread_info_init(struct ptlrpc_request *req,
2979 struct mdt_thread_info *info)
2983 info->mti_pill = &req->rq_pill;
2986 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2987 mdt_lock_handle_init(&info->mti_lh[i]);
2989 /* mdt device: it can be NULL while CONNECT */
2990 if (req->rq_export) {
2991 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2992 info->mti_exp = req->rq_export;
2994 info->mti_mdt = NULL;
2995 info->mti_env = req->rq_svc_thread->t_env;
2996 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2998 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2999 info->mti_big_buf = LU_BUF_NULL;
3000 info->mti_body = NULL;
3001 info->mti_object = NULL;
3002 info->mti_dlm_req = NULL;
3003 info->mti_has_trans = 0;
3004 info->mti_cross_ref = 0;
3005 info->mti_opdata = 0;
3006 info->mti_big_lmm_used = 0;
3008 info->mti_spec.no_create = 0;
3009 info->mti_spec.sp_rm_entry = 0;
3010 info->mti_spec.sp_permitted = 0;
3011 info->mti_spec.sp_migrate_close = 0;
3013 info->mti_spec.u.sp_ea.eadata = NULL;
3014 info->mti_spec.u.sp_ea.eadatalen = 0;
3017 void mdt_thread_info_fini(struct mdt_thread_info *info)
3021 if (info->mti_object != NULL) {
3022 mdt_object_put(info->mti_env, info->mti_object);
3023 info->mti_object = NULL;
3026 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3027 mdt_lock_handle_fini(&info->mti_lh[i]);
3028 info->mti_env = NULL;
3029 info->mti_pill = NULL;
3030 info->mti_exp = NULL;
3032 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3033 lu_buf_free(&info->mti_big_buf);
3036 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3038 struct mdt_thread_info *mti;
3040 mti = mdt_th_info(tsi->tsi_env);
3041 LASSERT(mti != NULL);
3043 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3044 if (tsi->tsi_corpus != NULL) {
3045 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3046 lu_object_get(tsi->tsi_corpus);
3048 mti->mti_body = tsi->tsi_mdt_body;
3049 mti->mti_dlm_req = tsi->tsi_dlm_req;
3054 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3056 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3058 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3059 set_current_state(TASK_UNINTERRUPTIBLE);
3060 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3063 return tgt_connect(tsi);
3081 static int mdt_intent_getattr(enum mdt_it_code opcode,
3082 struct mdt_thread_info *info,
3083 struct ldlm_lock **,
3086 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3087 struct mdt_thread_info *info,
3088 struct ldlm_lock **lockp,
3091 static int mdt_intent_layout(enum mdt_it_code opcode,
3092 struct mdt_thread_info *info,
3093 struct ldlm_lock **,
3095 static int mdt_intent_reint(enum mdt_it_code opcode,
3096 struct mdt_thread_info *info,
3097 struct ldlm_lock **,
3100 static struct mdt_it_flavor {
3101 const struct req_format *it_fmt;
3103 int (*it_act)(enum mdt_it_code ,
3104 struct mdt_thread_info *,
3105 struct ldlm_lock **,
3108 } mdt_it_flavor[] = {
3110 .it_fmt = &RQF_LDLM_INTENT,
3111 /*.it_flags = HABEO_REFERO,*/
3113 .it_act = mdt_intent_reint,
3114 .it_reint = REINT_OPEN
3117 .it_fmt = &RQF_LDLM_INTENT,
3119 * OCREAT is not a MUTABOR request as if the file
3121 * We do the extra check of OBD_CONNECT_RDONLY in
3122 * mdt_reint_open() when we really need to create
3126 .it_act = mdt_intent_reint,
3127 .it_reint = REINT_OPEN
3130 .it_fmt = &RQF_LDLM_INTENT,
3131 .it_flags = MUTABOR,
3132 .it_act = mdt_intent_reint,
3133 .it_reint = REINT_CREATE
3135 [MDT_IT_GETATTR] = {
3136 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3137 .it_flags = HABEO_REFERO,
3138 .it_act = mdt_intent_getattr
3140 [MDT_IT_READDIR] = {
3146 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3147 .it_flags = HABEO_REFERO,
3148 .it_act = mdt_intent_getattr
3151 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3152 .it_flags = MUTABOR,
3154 .it_reint = REINT_UNLINK
3158 .it_flags = MUTABOR,
3161 [MDT_IT_GETXATTR] = {
3162 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3163 .it_flags = HABEO_CORPUS,
3164 .it_act = mdt_intent_getxattr
3167 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3169 .it_act = mdt_intent_layout
3174 mdt_intent_lock_replace(struct mdt_thread_info *info,
3175 struct ldlm_lock **lockp,
3176 struct mdt_lock_handle *lh,
3177 __u64 flags, int result)
3179 struct ptlrpc_request *req = mdt_info_req(info);
3180 struct ldlm_lock *lock = *lockp;
3181 struct ldlm_lock *new_lock;
3183 /* If possible resent found a lock, @lh is set to its handle */
3184 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3186 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3187 lh->mlh_reg_lh.cookie = 0;
3191 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3192 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3193 * a resend case, however, it could be already destroyed
3194 * due to client eviction or a raced cancel RPC. */
3195 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3196 lh->mlh_reg_lh.cookie);
3197 lh->mlh_reg_lh.cookie = 0;
3201 LASSERTF(new_lock != NULL,
3202 "lockh %#llx flags %#llx : rc = %d\n",
3203 lh->mlh_reg_lh.cookie, flags, result);
3206 * If we've already given this lock to a client once, then we should
3207 * have no readers or writers. Otherwise, we should have one reader
3208 * _or_ writer ref (which will be zeroed below) before returning the
3211 if (new_lock->l_export == req->rq_export) {
3212 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3214 LASSERT(new_lock->l_export == NULL);
3215 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3220 if (new_lock->l_export == req->rq_export) {
3222 * Already gave this to the client, which means that we
3223 * reconstructed a reply.
3225 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3228 LDLM_LOCK_RELEASE(new_lock);
3229 lh->mlh_reg_lh.cookie = 0;
3230 RETURN(ELDLM_LOCK_REPLACED);
3234 * Fixup the lock to be given to the client.
3236 lock_res_and_lock(new_lock);
3237 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3238 * possible blocking AST. */
3239 while (new_lock->l_readers > 0) {
3240 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3241 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3242 new_lock->l_readers--;
3244 while (new_lock->l_writers > 0) {
3245 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3246 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3247 new_lock->l_writers--;
3250 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3251 new_lock->l_blocking_ast = lock->l_blocking_ast;
3252 new_lock->l_completion_ast = lock->l_completion_ast;
3253 new_lock->l_remote_handle = lock->l_remote_handle;
3254 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3256 unlock_res_and_lock(new_lock);
3258 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3259 &new_lock->l_remote_handle,
3260 &new_lock->l_exp_hash);
3262 LDLM_LOCK_RELEASE(new_lock);
3263 lh->mlh_reg_lh.cookie = 0;
3265 RETURN(ELDLM_LOCK_REPLACED);
3268 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3269 struct ldlm_lock *new_lock,
3270 struct mdt_lock_handle *lh,
3273 struct ptlrpc_request *req = mdt_info_req(info);
3274 struct ldlm_request *dlmreq;
3276 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3279 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3281 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3282 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3284 if (flags & LDLM_FL_RESENT) {
3285 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3286 lh->mlh_reg_mode = new_lock->l_granted_mode;
3288 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3289 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3290 lh->mlh_reg_lh.cookie);
3295 * If the xid matches, then we know this is a resent request, and allow
3296 * it. (It's probably an OPEN, for which we don't send a lock.
3298 if (req_can_reconstruct(req, NULL))
3302 * This remote handle isn't enqueued, so we never received or processed
3303 * this request. Clear MSG_RESENT, because it can be handled like any
3304 * normal request now.
3306 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3308 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3309 dlmreq->lock_handle[0].cookie);
3312 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3313 struct mdt_thread_info *info,
3314 struct ldlm_lock **lockp,
3317 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3318 struct ldlm_reply *ldlm_rep = NULL;
3323 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3324 * (for the resend case) or a new lock. Below we will use it to
3325 * replace the original lock.
3327 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3328 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3329 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3330 rc = mdt_object_lock(info, info->mti_object, lhc,
3331 MDS_INODELOCK_XATTR);
3336 rc = mdt_getxattr(info);
3338 if (mdt_info_req(info)->rq_repmsg != NULL)
3339 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3341 if (ldlm_rep == NULL ||
3342 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3343 mdt_object_unlock(info, info->mti_object, lhc, 1);
3344 RETURN(err_serious(-EFAULT));
3347 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3349 /* This is left for interop instead of adding a new interop flag.
3351 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3352 if (ldlm_rep->lock_policy_res2) {
3353 mdt_object_unlock(info, info->mti_object, lhc, 1);
3354 RETURN(ELDLM_LOCK_ABORTED);
3358 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3362 static int mdt_intent_getattr(enum mdt_it_code opcode,
3363 struct mdt_thread_info *info,
3364 struct ldlm_lock **lockp,
3367 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3369 struct ldlm_reply *ldlm_rep;
3370 struct mdt_body *reqbody;
3371 struct mdt_body *repbody;
3375 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3378 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3381 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3382 repbody->mbo_eadatasize = 0;
3383 repbody->mbo_aclsize = 0;
3387 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3389 case MDT_IT_GETATTR:
3390 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3394 CERROR("Unsupported intent (%d)\n", opcode);
3395 GOTO(out_shrink, rc = -EINVAL);
3398 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3400 GOTO(out_shrink, rc);
3402 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3403 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3405 /* Get lock from request for possible resent case. */
3406 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3408 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3409 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3411 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3412 ldlm_rep->lock_policy_res2 = 0;
3413 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3414 ldlm_rep->lock_policy_res2) {
3415 lhc->mlh_reg_lh.cookie = 0ull;
3416 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3419 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3422 mdt_exit_ucred(info);
3424 mdt_client_compatibility(info);
3425 rc2 = mdt_fix_reply(info);
3431 static int mdt_intent_layout(enum mdt_it_code opcode,
3432 struct mdt_thread_info *info,
3433 struct ldlm_lock **lockp,
3436 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3437 struct layout_intent *layout;
3439 struct mdt_object *obj = NULL;
3440 int layout_size = 0;
3444 if (opcode != MDT_IT_LAYOUT) {
3445 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3450 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3454 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3455 CERROR("%s: Unsupported layout intent opc %d\n",
3456 mdt_obd_name(info->mti_mdt), layout->li_opc);
3460 fid = &info->mti_tmp_fid2;
3461 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3463 /* Get lock from request for possible resent case. */
3464 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3466 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3468 GOTO(out, rc = PTR_ERR(obj));
3470 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3471 layout_size = mdt_attr_get_eabuf_size(info, obj);
3472 if (layout_size < 0)
3473 GOTO(out_obj, rc = layout_size);
3475 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3476 info->mti_mdt->mdt_max_mdsize = layout_size;
3479 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3480 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3482 rc = req_capsule_server_pack(info->mti_pill);
3486 mdt_object_put(info->mti_env, obj);
3488 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3489 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3492 lhc->mlh_reg_lh.cookie = 0;
3497 static int mdt_intent_reint(enum mdt_it_code opcode,
3498 struct mdt_thread_info *info,
3499 struct ldlm_lock **lockp,
3502 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3503 struct ldlm_reply *rep = NULL;
3507 static const struct req_format *intent_fmts[REINT_MAX] = {
3508 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3509 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3514 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3518 if (mdt_it_flavor[opcode].it_reint != opc) {
3519 CERROR("Reint code %ld doesn't match intent: %d\n",
3521 RETURN(err_serious(-EPROTO));
3524 /* Get lock from request for possible resent case. */
3525 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3527 rc = mdt_reint_internal(info, lhc, opc);
3529 /* Check whether the reply has been packed successfully. */
3530 if (mdt_info_req(info)->rq_repmsg != NULL)
3531 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3533 RETURN(err_serious(-EFAULT));
3535 /* MDC expects this in any case */
3537 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3539 /* the open lock or the lock for cross-ref object should be
3540 * returned to the client */
3541 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3542 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3543 rep->lock_policy_res2 = 0;
3544 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3548 rep->lock_policy_res2 = clear_serious(rc);
3550 if (rep->lock_policy_res2 == -ENOENT &&
3551 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3552 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3553 rep->lock_policy_res2 = 0;
3555 lhc->mlh_reg_lh.cookie = 0ull;
3556 if (rc == -ENOTCONN || rc == -ENODEV ||
3557 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3559 * If it is the disconnect error (ENODEV & ENOCONN), the error
3560 * will be returned by rq_status, and client at ptlrpc layer
3561 * will detect this, then disconnect, reconnect the import
3562 * immediately, instead of impacting the following the rpc.
3567 * For other cases, the error will be returned by intent, and client
3568 * will retrieve the result from intent.
3570 RETURN(ELDLM_LOCK_ABORTED);
3573 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3581 case IT_OPEN|IT_CREAT:
3588 rc = MDT_IT_READDIR;
3591 rc = MDT_IT_GETATTR;
3603 rc = MDT_IT_GETXATTR;
3608 case IT_QUOTA_DQACQ:
3613 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3620 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3621 struct mdt_thread_info *info,
3622 struct ldlm_lock **lockp, __u64 flags)
3624 struct req_capsule *pill = info->mti_pill;
3625 struct ptlrpc_request *req = mdt_info_req(info);
3626 struct mdt_it_flavor *flv;
3631 opc = mdt_intent_code(itopc);
3635 if (opc == MDT_IT_QUOTA) {
3636 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3639 RETURN(-EOPNOTSUPP);
3641 if (mdt_rdonly(req->rq_export))
3644 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3645 /* pass the request to quota master */
3646 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3647 mdt_info_req(info), lockp,
3652 flv = &mdt_it_flavor[opc];
3653 if (flv->it_fmt != NULL)
3654 req_capsule_extend(pill, flv->it_fmt);
3656 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3660 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3663 if (flv->it_act != NULL) {
3664 struct ldlm_reply *rep;
3666 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3668 /* execute policy */
3669 rc = flv->it_act(opc, info, lockp, flags);
3671 /* Check whether the reply has been packed successfully. */
3672 if (req->rq_repmsg != NULL) {
3673 rep = req_capsule_server_get(info->mti_pill,
3675 rep->lock_policy_res2 =
3676 ptlrpc_status_hton(rep->lock_policy_res2);
3683 static int mdt_intent_policy(struct ldlm_namespace *ns,
3684 struct ldlm_lock **lockp, void *req_cookie,
3685 enum ldlm_mode mode, __u64 flags, void *data)
3687 struct tgt_session_info *tsi;
3688 struct mdt_thread_info *info;
3689 struct ptlrpc_request *req = req_cookie;
3690 struct ldlm_intent *it;
3691 struct req_capsule *pill;
3696 LASSERT(req != NULL);
3698 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3700 info = tsi2mdt_info(tsi);
3701 LASSERT(info != NULL);
3702 pill = info->mti_pill;
3703 LASSERT(pill->rc_req == req);
3705 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3706 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3707 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3709 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3713 /* Lock without inodebits makes no sense and will oops
3714 * later in ldlm. Let's check it now to see if we have
3715 * ibits corrupted somewhere in mdt_intent_opc().
3716 * The case for client miss to set ibits has been
3717 * processed by others. */
3718 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3719 lr_type == LDLM_IBITS,
3720 info->mti_dlm_req->lock_desc.\
3721 l_policy_data.l_inodebits.bits != 0));
3723 rc = err_serious(-EFAULT);
3725 /* No intent was provided */
3726 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3727 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3728 rc = req_capsule_server_pack(pill);
3730 rc = err_serious(rc);
3732 mdt_thread_info_fini(info);
3736 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3738 struct seq_server_site *ss = mdt_seq_site(mdt);
3740 if (ss->ss_node_id == 0)
3743 if (ss->ss_client_seq != NULL) {
3744 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3745 ss->ss_client_seq->lcs_exp = NULL;
3748 if (ss->ss_server_fld != NULL) {
3749 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3750 ss->ss_server_fld->lsf_control_exp = NULL;
3754 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3756 struct seq_server_site *ss = mdt_seq_site(mdt);
3761 if (ss->ss_server_seq != NULL)
3762 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3765 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3767 mdt_seq_fini_cli(mdt);
3768 mdt_deregister_seq_exp(mdt);
3770 return seq_site_fini(env, mdt_seq_site(mdt));
3774 * It will retrieve its FLDB entries from MDT0, and it only happens
3775 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3776 * and it needs to refresh FLDB from the MDT0.
3778 static int mdt_register_lwp_callback(void *data)
3781 struct mdt_device *mdt = data;
3782 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3786 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3788 rc = lu_env_init(&env, LCT_MD_THREAD);
3790 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3794 /* Allocate new sequence now to avoid creating local transaction
3795 * in the normal transaction process */
3796 rc = seq_server_check_and_alloc_super(&env,
3797 mdt_seq_site(mdt)->ss_server_seq);
3802 rc = fld_update_from_controller(&env, fld);
3804 CERROR("%s: cannot update controller: rc = %d\n",
3805 mdt_obd_name(mdt), rc);
3814 static int mdt_register_seq_exp(struct mdt_device *mdt)
3816 struct seq_server_site *ss = mdt_seq_site(mdt);
3817 char *lwp_name = NULL;
3820 if (ss->ss_node_id == 0)
3823 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3824 if (lwp_name == NULL)
3825 GOTO(out_free, rc = -ENOMEM);
3827 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3831 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3836 rc = lustre_register_lwp_item(lwp_name,
3837 &ss->ss_server_fld->lsf_control_exp,
3838 mdt_register_lwp_callback, mdt);
3840 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3841 ss->ss_client_seq->lcs_exp = NULL;
3845 if (lwp_name != NULL)
3846 OBD_FREE(lwp_name, MAX_OBD_NAME);
3852 * Init client sequence manager which is used by local MDS to talk to sequence
3853 * controller on remote node.
3855 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3857 struct seq_server_site *ss = mdt_seq_site(mdt);
3862 /* check if this is adding the first MDC and controller is not yet
3864 OBD_ALLOC_PTR(ss->ss_client_seq);
3865 if (ss->ss_client_seq == NULL)
3868 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3869 if (prefix == NULL) {
3870 OBD_FREE_PTR(ss->ss_client_seq);
3871 ss->ss_client_seq = NULL;
3875 /* Note: seq_client_fini will be called in seq_site_fini */
3876 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3877 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3878 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3880 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3882 OBD_FREE_PTR(ss->ss_client_seq);
3883 ss->ss_client_seq = NULL;
3887 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3892 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3894 struct seq_server_site *ss;
3898 ss = mdt_seq_site(mdt);
3899 /* init sequence controller server(MDT0) */
3900 if (ss->ss_node_id == 0) {
3901 OBD_ALLOC_PTR(ss->ss_control_seq);
3902 if (ss->ss_control_seq == NULL)
3905 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3906 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3909 GOTO(out_seq_fini, rc);
3912 /* Init normal sequence server */
3913 OBD_ALLOC_PTR(ss->ss_server_seq);
3914 if (ss->ss_server_seq == NULL)
3915 GOTO(out_seq_fini, rc = -ENOMEM);
3917 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3918 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3920 GOTO(out_seq_fini, rc);
3922 /* init seq client for seq server to talk to seq controller(MDT0) */
3923 rc = mdt_seq_init_cli(env, mdt);
3925 GOTO(out_seq_fini, rc);
3927 if (ss->ss_node_id != 0)
3928 /* register controller export through lwp */
3929 rc = mdt_register_seq_exp(mdt);
3934 mdt_seq_fini(env, mdt);
3942 static int mdt_fld_fini(const struct lu_env *env,
3943 struct mdt_device *m)
3945 struct seq_server_site *ss = mdt_seq_site(m);
3948 if (ss && ss->ss_server_fld) {
3949 fld_server_fini(env, ss->ss_server_fld);
3950 OBD_FREE_PTR(ss->ss_server_fld);
3951 ss->ss_server_fld = NULL;
3957 static int mdt_fld_init(const struct lu_env *env,
3959 struct mdt_device *m)
3961 struct seq_server_site *ss;
3965 ss = mdt_seq_site(m);
3967 OBD_ALLOC_PTR(ss->ss_server_fld);
3968 if (ss->ss_server_fld == NULL)
3969 RETURN(rc = -ENOMEM);
3971 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3974 OBD_FREE_PTR(ss->ss_server_fld);
3975 ss->ss_server_fld = NULL;
3982 static void mdt_stack_pre_fini(const struct lu_env *env,
3983 struct mdt_device *m, struct lu_device *top)
3985 struct lustre_cfg_bufs *bufs;
3986 struct lustre_cfg *lcfg;
3987 struct mdt_thread_info *info;
3992 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3993 LASSERT(info != NULL);
3995 bufs = &info->mti_u.bufs;
3997 LASSERT(m->mdt_child_exp);
3998 LASSERT(m->mdt_child_exp->exp_obd);
4000 /* process cleanup, pass mdt obd name to get obd umount flags */
4001 /* XXX: this is needed because all layers are referenced by
4002 * objects (some of them are pinned by osd, for example *
4003 * the proper solution should be a model where object used
4004 * by osd only doesn't have mdt/mdd slices -bzzz */
4005 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4006 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4007 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4011 top->ld_ops->ldo_process_config(env, top, lcfg);
4012 lustre_cfg_free(lcfg);
4016 static void mdt_stack_fini(const struct lu_env *env,
4017 struct mdt_device *m, struct lu_device *top)
4019 struct obd_device *obd = mdt2obd_dev(m);
4020 struct lustre_cfg_bufs *bufs;
4021 struct lustre_cfg *lcfg;
4022 struct mdt_thread_info *info;
4026 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4027 LASSERT(info != NULL);
4029 lu_dev_del_linkage(top->ld_site, top);
4031 lu_site_purge(env, top->ld_site, -1);
4033 bufs = &info->mti_u.bufs;
4034 /* process cleanup, pass mdt obd name to get obd umount flags */
4035 /* another purpose is to let all layers to release their objects */
4036 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4041 lustre_cfg_bufs_set_string(bufs, 1, flags);
4042 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4047 top->ld_ops->ldo_process_config(env, top, lcfg);
4048 lustre_cfg_free(lcfg);
4050 lu_site_purge(env, top->ld_site, -1);
4052 m->mdt_child = NULL;
4053 m->mdt_bottom = NULL;
4055 obd_disconnect(m->mdt_child_exp);
4056 m->mdt_child_exp = NULL;
4058 obd_disconnect(m->mdt_bottom_exp);
4059 m->mdt_child_exp = NULL;
4062 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4063 const char *next, struct obd_export **exp)
4065 struct obd_connect_data *data = NULL;
4066 struct obd_device *obd;
4070 OBD_ALLOC_PTR(data);
4072 GOTO(out, rc = -ENOMEM);
4074 obd = class_name2obd(next);
4076 CERROR("%s: can't locate next device: %s\n",
4077 mdt_obd_name(m), next);
4078 GOTO(out, rc = -ENOTCONN);
4081 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4082 data->ocd_version = LUSTRE_VERSION_CODE;
4084 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4086 CERROR("%s: cannot connect to next dev %s (%d)\n",
4087 mdt_obd_name(m), next, rc);
4097 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4098 struct lustre_cfg *cfg)
4100 char *dev = lustre_cfg_string(cfg, 0);
4101 int rc, name_size, uuid_size;
4102 char *name, *uuid, *p;
4103 struct lustre_cfg_bufs *bufs;
4104 struct lustre_cfg *lcfg;
4105 struct obd_device *obd;
4106 struct lustre_profile *lprof;
4107 struct lu_site *site;
4110 /* in 1.8 we had the only device in the stack - MDS.
4111 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4112 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4113 * to generate names and setup MDT, MDD. MDT will be using
4114 * generated name to connect to MDD. for MDD the next device
4115 * will be LOD with name taken from so called "profile" which
4116 * is generated by mount_option line
4118 * 1.8 MGS generates config. commands like this:
4119 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4120 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4121 * 2.0 MGS generates config. commands like this:
4122 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4123 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4124 * 3:lustre-MDT0000-mdtlov 4:f
4126 * we generate MDD name from MDT one, just replacing T with D
4128 * after all the preparations, the logical equivalent will be
4129 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4130 * 3:lustre-MDT0000-mdtlov 4:f
4131 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4132 * 3:lustre-MDD0000 4:f
4134 * notice we build the stack from down to top: MDD first, then MDT */
4136 name_size = MAX_OBD_NAME;
4137 uuid_size = MAX_OBD_NAME;
4139 OBD_ALLOC(name, name_size);
4140 OBD_ALLOC(uuid, uuid_size);
4141 if (name == NULL || uuid == NULL)
4142 GOTO(cleanup_mem, rc = -ENOMEM);
4144 OBD_ALLOC_PTR(bufs);
4146 GOTO(cleanup_mem, rc = -ENOMEM);
4149 p = strstr(name, "-MDT");
4151 GOTO(free_bufs, rc = -ENOMEM);
4154 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4156 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4157 if (lprof == NULL || lprof->lp_dt == NULL) {
4158 CERROR("can't find the profile: %s\n",
4159 lustre_cfg_string(cfg, 0));
4160 GOTO(free_bufs, rc = -EINVAL);
4163 lustre_cfg_bufs_reset(bufs, name);
4164 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4165 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4166 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4168 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4170 GOTO(put_profile, rc = -ENOMEM);
4172 rc = class_attach(lcfg);
4174 GOTO(lcfg_cleanup, rc);
4176 obd = class_name2obd(name);
4178 CERROR("Can not find obd %s (%s in config)\n",
4179 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4180 GOTO(lcfg_cleanup, rc = -EINVAL);
4183 lustre_cfg_free(lcfg);
4185 lustre_cfg_bufs_reset(bufs, name);
4186 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4187 lustre_cfg_bufs_set_string(bufs, 2, dev);
4188 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4190 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4192 GOTO(class_detach, rc = -ENOMEM);
4194 rc = class_setup(obd, lcfg);
4196 GOTO(class_detach, rc);
4198 /* connect to MDD we just setup */
4199 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4201 GOTO(class_detach, rc);
4203 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4205 LASSERT(mdt_lu_site(mdt) == NULL);
4206 mdt->mdt_lu_dev.ld_site = site;
4207 site->ls_top_dev = &mdt->mdt_lu_dev;
4208 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4210 /* now connect to bottom OSD */
4211 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4212 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4214 GOTO(class_detach, rc);
4216 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4218 rc = lu_env_refill((struct lu_env *)env);
4220 CERROR("Failure to refill session: '%d'\n", rc);
4222 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4227 class_detach(obd, lcfg);
4229 lustre_cfg_free(lcfg);
4231 class_put_profile(lprof);
4236 OBD_FREE(name, name_size);
4238 OBD_FREE(uuid, uuid_size);
4242 /* setup quota master target on MDT0 */
4243 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4244 struct lustre_cfg *cfg)
4246 struct obd_device *obd;
4247 char *dev = lustre_cfg_string(cfg, 0);
4248 char *qmtname, *uuid, *p;
4249 struct lustre_cfg_bufs *bufs;
4250 struct lustre_cfg *lcfg;
4251 struct lustre_profile *lprof;
4252 struct obd_connect_data *data;
4256 LASSERT(mdt->mdt_qmt_exp == NULL);
4257 LASSERT(mdt->mdt_qmt_dev == NULL);
4259 /* quota master is on MDT0 only for now */
4260 if (mdt->mdt_seq_site.ss_node_id != 0)
4263 /* MGS generates config commands which look as follows:
4264 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4265 * 3:lustre-MDT0000-mdtlov 4:f
4267 * We generate the QMT name from the MDT one, just replacing MD with QM
4268 * after all the preparations, the logical equivalent will be:
4269 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4270 * 3:lustre-MDT0000-osd 4:f */
4271 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4272 OBD_ALLOC(uuid, UUID_MAX);
4273 OBD_ALLOC_PTR(bufs);
4274 OBD_ALLOC_PTR(data);
4275 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4276 GOTO(cleanup_mem, rc = -ENOMEM);
4278 strcpy(qmtname, dev);
4279 p = strstr(qmtname, "-MDT");
4281 GOTO(cleanup_mem, rc = -ENOMEM);
4282 /* replace MD with QM */
4286 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4288 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4289 if (lprof == NULL || lprof->lp_dt == NULL) {
4290 CERROR("can't find profile for %s\n",
4291 lustre_cfg_string(cfg, 0));
4292 GOTO(cleanup_mem, rc = -EINVAL);
4295 lustre_cfg_bufs_reset(bufs, qmtname);
4296 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4297 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4298 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4300 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4302 GOTO(put_profile, rc = -ENOMEM);
4304 rc = class_attach(lcfg);
4306 GOTO(lcfg_cleanup, rc);
4308 obd = class_name2obd(qmtname);
4310 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4311 lustre_cfg_string(cfg, 0));
4312 GOTO(lcfg_cleanup, rc = -EINVAL);
4315 lustre_cfg_free(lcfg);
4317 lustre_cfg_bufs_reset(bufs, qmtname);
4318 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4319 lustre_cfg_bufs_set_string(bufs, 2, dev);
4321 /* for quota, the next device should be the OSD device */
4322 lustre_cfg_bufs_set_string(bufs, 3,
4323 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4325 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4327 GOTO(class_detach, rc = -ENOMEM);
4329 rc = class_setup(obd, lcfg);
4331 GOTO(class_detach, rc);
4333 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4335 /* configure local quota objects */
4336 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4340 GOTO(class_cleanup, rc);
4342 /* connect to quota master target */
4343 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4344 data->ocd_version = LUSTRE_VERSION_CODE;
4345 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4348 CERROR("cannot connect to quota master device %s (%d)\n",
4350 GOTO(class_cleanup, rc);
4356 class_manual_cleanup(obd);
4357 mdt->mdt_qmt_dev = NULL;
4361 class_detach(obd, lcfg);
4363 lustre_cfg_free(lcfg);
4365 class_put_profile(lprof);
4370 OBD_FREE(qmtname, MAX_OBD_NAME);
4372 OBD_FREE(uuid, UUID_MAX);
4378 /* Shutdown quota master target associated with mdt */
4379 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4383 if (mdt->mdt_qmt_exp == NULL)
4385 LASSERT(mdt->mdt_qmt_dev != NULL);
4387 /* the qmt automatically shuts down when the mdt disconnects */
4388 obd_disconnect(mdt->mdt_qmt_exp);
4389 mdt->mdt_qmt_exp = NULL;
4390 mdt->mdt_qmt_dev = NULL;
4394 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4395 * for now. This will be removed along with converting rest of MDT code
4396 * to use tgt_session_info */
4397 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4399 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4402 rc = mdt_getxattr(info);
4404 mdt_thread_info_fini(info);
4408 static struct tgt_handler mdt_tgt_handlers[] = {
4409 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4410 0, MDS_CONNECT, mdt_tgt_connect,
4411 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4412 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4413 0, MDS_DISCONNECT, tgt_disconnect,
4414 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4415 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4416 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4417 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4418 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4419 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4420 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4421 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4423 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4424 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4425 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4426 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4427 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4428 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4429 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4430 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4432 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4433 mdt_hsm_ct_register),
4434 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4435 mdt_hsm_ct_unregister),
4436 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4438 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4440 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4441 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4443 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4448 static struct tgt_handler mdt_sec_ctx_ops[] = {
4449 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4450 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4451 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4454 static struct tgt_handler mdt_quota_ops[] = {
4455 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4458 static struct tgt_opc_slice mdt_common_slice[] = {
4460 .tos_opc_start = MDS_FIRST_OPC,
4461 .tos_opc_end = MDS_LAST_OPC,
4462 .tos_hs = mdt_tgt_handlers
4465 .tos_opc_start = OBD_FIRST_OPC,
4466 .tos_opc_end = OBD_LAST_OPC,
4467 .tos_hs = tgt_obd_handlers
4470 .tos_opc_start = LDLM_FIRST_OPC,
4471 .tos_opc_end = LDLM_LAST_OPC,
4472 .tos_hs = tgt_dlm_handlers
4475 .tos_opc_start = SEC_FIRST_OPC,
4476 .tos_opc_end = SEC_LAST_OPC,
4477 .tos_hs = mdt_sec_ctx_ops
4480 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4481 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4482 .tos_hs = tgt_out_handlers
4485 .tos_opc_start = FLD_FIRST_OPC,
4486 .tos_opc_end = FLD_LAST_OPC,
4487 .tos_hs = fld_handlers
4490 .tos_opc_start = SEQ_FIRST_OPC,
4491 .tos_opc_end = SEQ_LAST_OPC,
4492 .tos_hs = seq_handlers
4495 .tos_opc_start = QUOTA_DQACQ,
4496 .tos_opc_end = QUOTA_LAST_OPC,
4497 .tos_hs = mdt_quota_ops
4500 .tos_opc_start = LLOG_FIRST_OPC,
4501 .tos_opc_end = LLOG_LAST_OPC,
4502 .tos_hs = tgt_llog_handlers
4505 .tos_opc_start = LFSCK_FIRST_OPC,
4506 .tos_opc_end = LFSCK_LAST_OPC,
4507 .tos_hs = tgt_lfsck_handlers
4515 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4517 struct md_device *next = m->mdt_child;
4518 struct lu_device *d = &m->mdt_lu_dev;
4519 struct obd_device *obd = mdt2obd_dev(m);
4520 struct lfsck_stop stop;
4523 stop.ls_status = LS_PAUSED;
4525 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4527 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4528 ping_evictor_stop();
4530 if (m->mdt_opts.mo_coordinator)
4531 mdt_hsm_cdt_stop(m);
4533 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4534 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4536 if (m->mdt_namespace != NULL)
4537 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4538 d->ld_obd->obd_force);
4540 obd_exports_barrier(obd);
4541 obd_zombie_barrier();
4543 mdt_quota_fini(env, m);
4545 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4547 /* Calling the cleanup functions in the same order as in the mdt_init0
4552 target_recovery_fini(obd);
4553 upcall_cache_cleanup(m->mdt_identity_cache);
4554 m->mdt_identity_cache = NULL;
4556 mdt_fs_cleanup(env, m);
4558 tgt_fini(env, &m->mdt_lut);
4560 mdt_hsm_cdt_fini(m);
4562 if (m->mdt_namespace != NULL) {
4563 ldlm_namespace_free_post(m->mdt_namespace);
4564 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4567 if (m->mdt_md_root != NULL) {
4568 mdt_object_put(env, m->mdt_md_root);
4569 m->mdt_md_root = NULL;
4572 mdt_seq_fini(env, m);
4574 mdt_fld_fini(env, m);
4579 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4581 LASSERT(atomic_read(&d->ld_ref) == 0);
4583 server_put_mount(mdt_obd_name(m), true);
4588 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4590 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4591 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4593 struct mdt_thread_info *info;
4594 struct obd_device *obd;
4595 const char *dev = lustre_cfg_string(cfg, 0);
4596 const char *num = lustre_cfg_string(cfg, 2);
4597 struct lustre_mount_info *lmi = NULL;
4598 struct lustre_sb_info *lsi;
4600 struct seq_server_site *ss_site;
4601 const char *identity_upcall = "NONE";
4602 struct md_device *next;
4608 lu_device_init(&m->mdt_lu_dev, ldt);
4610 * Environment (env) might be missing mdt_thread_key values at that
4611 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4614 * Usually device allocation path doesn't use module key values, but
4615 * mdt has to do a lot of work here, so allocate key value.
4617 rc = lu_env_refill((struct lu_env *)env);
4621 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4622 LASSERT(info != NULL);
4624 obd = class_name2obd(dev);
4625 LASSERT(obd != NULL);
4627 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4628 m->mdt_opts.mo_evict_tgt_nids = 1;
4629 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4631 /* default is coordinator off, it is started through conf_param
4633 m->mdt_opts.mo_coordinator = 0;
4635 lmi = server_get_mount(dev);
4637 CERROR("Cannot get mount info for %s!\n", dev);
4640 lsi = s2lsi(lmi->lmi_sb);
4641 /* CMD is supported only in IAM mode */
4643 node_id = simple_strtol(num, NULL, 10);
4644 obd->u.obt.obt_magic = OBT_MAGIC;
4645 if (lsi->lsi_lmd != NULL &&
4646 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4647 m->mdt_skip_lfsck = 1;
4650 m->mdt_squash.rsi_uid = 0;
4651 m->mdt_squash.rsi_gid = 0;
4652 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4653 init_rwsem(&m->mdt_squash.rsi_sem);
4654 spin_lock_init(&m->mdt_lock);
4655 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4656 m->mdt_enable_remote_dir = 0;
4657 m->mdt_enable_remote_dir_gid = 0;
4659 atomic_set(&m->mdt_mds_mds_conns, 0);
4660 atomic_set(&m->mdt_async_commit_count, 0);
4662 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4663 m->mdt_lu_dev.ld_obd = obd;
4664 /* Set this lu_device to obd for error handling purposes. */
4665 obd->obd_lu_dev = &m->mdt_lu_dev;
4667 /* init the stack */
4668 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4670 CERROR("%s: Can't init device stack, rc %d\n",
4671 mdt_obd_name(m), rc);
4676 ss_site = mdt_seq_site(m);
4677 s->ld_seq_site = ss_site;
4680 /* set server index */
4681 ss_site->ss_node_id = node_id;
4683 /* failover is the default
4684 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4685 * assumed whose ss_node_id == 0 XXX
4687 obd->obd_replayable = 1;
4688 /* No connection accepted until configurations will finish */
4689 obd->obd_no_conn = 1;
4691 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4692 char *str = lustre_cfg_string(cfg, 4);
4693 if (strchr(str, 'n')) {
4694 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4695 obd->obd_replayable = 0;
4699 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4701 GOTO(err_fini_stack, rc);
4703 rc = mdt_seq_init(env, m);
4705 GOTO(err_fini_fld, rc);
4707 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4708 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4709 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4710 LDLM_NAMESPACE_SERVER,
4711 LDLM_NAMESPACE_GREEDY,
4713 if (m->mdt_namespace == NULL)
4714 GOTO(err_fini_seq, rc = -ENOMEM);
4716 m->mdt_namespace->ns_lvbp = m;
4717 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4719 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4720 /* set obd_namespace for compatibility with old code */
4721 obd->obd_namespace = m->mdt_namespace;
4723 rc = mdt_hsm_cdt_init(m);
4725 CERROR("%s: error initializing coordinator, rc %d\n",
4726 mdt_obd_name(m), rc);
4727 GOTO(err_free_ns, rc);
4730 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4731 OBD_FAIL_MDS_ALL_REQUEST_NET,
4732 OBD_FAIL_MDS_ALL_REPLY_NET);
4734 GOTO(err_free_hsm, rc);
4736 rc = mdt_fs_setup(env, m, obd, lsi);
4740 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4742 next = m->mdt_child;
4743 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4746 GOTO(err_fs_cleanup, rc);
4748 if (mntopts & MNTOPT_USERXATTR)
4749 m->mdt_opts.mo_user_xattr = 1;
4751 m->mdt_opts.mo_user_xattr = 0;
4753 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4755 GOTO(err_fs_cleanup, rc);
4757 if (mntopts & MNTOPT_ACL)
4758 m->mdt_opts.mo_acl = 1;
4760 m->mdt_opts.mo_acl = 0;
4762 /* XXX: to support suppgid for ACL, we enable identity_upcall
4763 * by default, otherwise, maybe got unexpected -EACCESS. */
4764 if (m->mdt_opts.mo_acl)
4765 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4767 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4769 &mdt_identity_upcall_cache_ops);
4770 if (IS_ERR(m->mdt_identity_cache)) {
4771 rc = PTR_ERR(m->mdt_identity_cache);
4772 m->mdt_identity_cache = NULL;
4773 GOTO(err_fs_cleanup, rc);
4776 rc = mdt_procfs_init(m, dev);
4778 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4779 GOTO(err_recovery, rc);
4782 rc = mdt_quota_init(env, m, cfg);
4784 GOTO(err_procfs, rc);
4786 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4787 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4788 "mdt_ldlm_client", m->mdt_ldlm_client);
4790 ping_evictor_start();
4792 /* recovery will be started upon mdt_prepare()
4793 * when the whole stack is complete and ready
4794 * to serve the requests */
4796 /* Reduce the initial timeout on an MDS because it doesn't need such
4797 * a long timeout as an OST does. Adaptive timeouts will adjust this
4798 * value appropriately. */
4799 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4800 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4806 target_recovery_fini(obd);
4807 upcall_cache_cleanup(m->mdt_identity_cache);
4808 m->mdt_identity_cache = NULL;
4810 mdt_fs_cleanup(env, m);
4812 tgt_fini(env, &m->mdt_lut);
4814 mdt_hsm_cdt_fini(m);
4816 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4817 obd->obd_namespace = m->mdt_namespace = NULL;
4819 mdt_seq_fini(env, m);
4821 mdt_fld_fini(env, m);
4823 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4826 server_put_mount(dev, true);
4830 /* For interoperability, the left element is old parameter, the right one
4831 * is the new version of the parameter, if some parameter is deprecated,
4832 * the new version should be set as NULL. */
4833 static struct cfg_interop_param mdt_interop_param[] = {
4834 { "mdt.group_upcall", NULL },
4835 { "mdt.quota_type", NULL },
4836 { "mdd.quota_type", NULL },
4837 { "mdt.som", NULL },
4838 { "mdt.rootsquash", "mdt.root_squash" },
4839 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4843 /* used by MGS to process specific configurations */
4844 static int mdt_process_config(const struct lu_env *env,
4845 struct lu_device *d, struct lustre_cfg *cfg)
4847 struct mdt_device *m = mdt_dev(d);
4848 struct md_device *md_next = m->mdt_child;
4849 struct lu_device *next = md2lu_dev(md_next);
4853 switch (cfg->lcfg_command) {
4855 struct obd_device *obd = d->ld_obd;
4857 /* For interoperability */
4858 struct cfg_interop_param *ptr = NULL;
4859 struct lustre_cfg *old_cfg = NULL;
4862 param = lustre_cfg_string(cfg, 1);
4863 if (param == NULL) {
4864 CERROR("param is empty\n");
4869 ptr = class_find_old_param(param, mdt_interop_param);
4871 if (ptr->new_param == NULL) {
4873 CWARN("For interoperability, skip this %s."
4874 " It is obsolete.\n", ptr->old_param);
4878 CWARN("Found old param %s, changed it to %s.\n",
4879 ptr->old_param, ptr->new_param);
4882 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4889 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4891 if (rc > 0 || rc == -ENOSYS) {
4892 /* is it an HSM var ? */
4893 rc = class_process_proc_param(PARAM_HSM,
4894 hsm_cdt_get_proc_vars(),
4896 if (rc > 0 || rc == -ENOSYS)
4897 /* we don't understand; pass it on */
4898 rc = next->ld_ops->ldo_process_config(env, next,
4902 if (old_cfg != NULL)
4903 lustre_cfg_free(cfg);
4908 /* others are passed further */
4909 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4915 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4916 const struct lu_object_header *hdr,
4917 struct lu_device *d)
4919 struct mdt_object *mo;
4923 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4925 struct lu_object *o;
4926 struct lu_object_header *h;
4929 h = &mo->mot_header;
4930 lu_object_header_init(h);
4931 lu_object_init(o, h, d);
4932 lu_object_add_top(h, o);
4933 o->lo_ops = &mdt_obj_ops;
4934 spin_lock_init(&mo->mot_write_lock);
4935 mutex_init(&mo->mot_lov_mutex);
4936 init_rwsem(&mo->mot_open_sem);
4937 atomic_set(&mo->mot_open_count, 0);
4943 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4944 const struct lu_object_conf *unused)
4946 struct mdt_device *d = mdt_dev(o->lo_dev);
4947 struct lu_device *under;
4948 struct lu_object *below;
4952 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4953 PFID(lu_object_fid(o)));
4955 under = &d->mdt_child->md_lu_dev;
4956 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4957 if (below != NULL) {
4958 lu_object_add(o, below);
4965 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4967 struct mdt_object *mo = mdt_obj(o);
4968 struct lu_object_header *h;
4972 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4973 PFID(lu_object_fid(o)));
4975 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4976 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4979 lu_object_header_fini(h);
4980 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4985 static int mdt_object_print(const struct lu_env *env, void *cookie,
4986 lu_printer_t p, const struct lu_object *o)
4988 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4990 return (*p)(env, cookie,
4991 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
4992 mdto, mdto->mot_lov_created ? "lov_created" : "",
4993 mdto->mot_cache_attr ? "cache_attr" : "",
4994 mdto->mot_write_count);
4997 static int mdt_prepare(const struct lu_env *env,
4998 struct lu_device *pdev,
4999 struct lu_device *cdev)
5001 struct mdt_device *mdt = mdt_dev(cdev);
5002 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5003 struct obd_device *obd = cdev->ld_obd;
5010 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5014 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5018 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5022 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5023 /* The LFSCK instance is registered just now, so it must be there when
5024 * register the namespace to such instance. */
5025 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5027 if (mdt->mdt_seq_site.ss_node_id == 0) {
5028 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5029 &mdt->mdt_md_root_fid);
5034 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5036 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5037 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5038 LASSERT(obd->obd_no_conn);
5039 spin_lock(&obd->obd_dev_lock);
5040 obd->obd_no_conn = 0;
5041 spin_unlock(&obd->obd_dev_lock);
5043 if (obd->obd_recovering == 0)
5044 mdt_postrecov(env, mdt);
5049 const struct lu_device_operations mdt_lu_ops = {
5050 .ldo_object_alloc = mdt_object_alloc,
5051 .ldo_process_config = mdt_process_config,
5052 .ldo_prepare = mdt_prepare,
5055 static const struct lu_object_operations mdt_obj_ops = {
5056 .loo_object_init = mdt_object_init,
5057 .loo_object_free = mdt_object_free,
5058 .loo_object_print = mdt_object_print
5061 static int mdt_obd_set_info_async(const struct lu_env *env,
5062 struct obd_export *exp,
5063 __u32 keylen, void *key,
5064 __u32 vallen, void *val,
5065 struct ptlrpc_request_set *set)
5071 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5072 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5080 * Match client and server connection feature flags.
5082 * Compute the compatibility flags for a connection request based on
5083 * features mutually supported by client and server.
5085 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5086 * must not be updated here, otherwise a partially initialized value may
5087 * be exposed. After the connection request is successfully processed,
5088 * the top-level MDT connect request handler atomically updates the export
5089 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5090 * reply. \see mdt_connect().
5092 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5093 * full struct obd_connect_data. So care must be taken when accessing fields
5094 * that are not present in struct obd_connect_data_v1. See LU-16.
5096 * \param exp the obd_export associated with this client/target pair
5097 * \param mdt the target device for the connection
5098 * \param data stores data for this connect request
5101 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5102 * \retval -EBADE client and server feature requirements are incompatible
5104 static int mdt_connect_internal(struct obd_export *exp,
5105 struct mdt_device *mdt,
5106 struct obd_connect_data *data)
5108 LASSERT(data != NULL);
5110 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5112 if (mdt->mdt_bottom->dd_rdonly &&
5113 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5114 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5117 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5118 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5120 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5122 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5123 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5124 CWARN("%s: client %s does not support ibits lock, either "
5125 "very old or an invalid client: flags %#llx\n",
5126 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5127 data->ocd_connect_flags);
5131 if (!mdt->mdt_opts.mo_acl)
5132 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5134 if (!mdt->mdt_opts.mo_user_xattr)
5135 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5137 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5138 data->ocd_brw_size = min(data->ocd_brw_size,
5139 (__u32)MD_MAX_BRW_SIZE);
5140 if (data->ocd_brw_size == 0) {
5141 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx"
5142 " ocd_version: %x ocd_grant: %d "
5143 "ocd_index: %u ocd_brw_size is "
5144 "unexpectedly zero, network data "
5145 "corruption? Refusing connection of this"
5148 exp->exp_client_uuid.uuid,
5149 exp, data->ocd_connect_flags, data->ocd_version,
5150 data->ocd_grant, data->ocd_index);
5155 /* NB: Disregard the rule against updating
5156 * exp_connect_data.ocd_connect_flags in this case, since
5157 * tgt_client_new() needs to know if this is a lightweight
5158 * connection, and it is safe to expose this flag before
5159 * connection processing completes. */
5160 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5161 spin_lock(&exp->exp_lock);
5162 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5163 spin_unlock(&exp->exp_lock);
5166 data->ocd_version = LUSTRE_VERSION_CODE;
5168 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5169 CWARN("%s: MDS requires FID support, but client not\n",
5174 if (OCD_HAS_FLAG(data, PINGLESS)) {
5175 if (ptlrpc_pinger_suppress_pings()) {
5176 spin_lock(&exp->exp_obd->obd_dev_lock);
5177 list_del_init(&exp->exp_obd_chain_timed);
5178 spin_unlock(&exp->exp_obd->obd_dev_lock);
5180 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5184 data->ocd_max_easize = mdt->mdt_max_ea_size;
5186 /* NB: Disregard the rule against updating
5187 * exp_connect_data.ocd_connect_flags in this case, since
5188 * tgt_client_new() needs to know if this is client supports
5189 * multiple modify RPCs, and it is safe to expose this flag before
5190 * connection processing completes. */
5191 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5192 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5193 spin_lock(&exp->exp_lock);
5194 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5195 spin_unlock(&exp->exp_lock);
5201 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5202 struct mdt_thread_info *info,
5203 struct mdt_file_data *mfd)
5205 struct lu_context ses;
5209 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5214 lu_context_enter(&ses);
5216 mdt_ucred(info)->uc_valid = UCRED_OLD;
5217 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5219 lu_context_exit(&ses);
5220 lu_context_fini(&ses);
5226 static int mdt_export_cleanup(struct obd_export *exp)
5228 struct list_head closing_list;
5229 struct mdt_export_data *med = &exp->exp_mdt_data;
5230 struct obd_device *obd = exp->exp_obd;
5231 struct mdt_device *mdt;
5232 struct mdt_thread_info *info;
5234 struct mdt_file_data *mfd, *n;
5238 INIT_LIST_HEAD(&closing_list);
5239 spin_lock(&med->med_open_lock);
5240 while (!list_empty(&med->med_open_head)) {
5241 struct list_head *tmp = med->med_open_head.next;
5242 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5244 /* Remove mfd handle so it can't be found again.
5245 * We are consuming the mfd_list reference here. */
5246 class_handle_unhash(&mfd->mfd_handle);
5247 list_move_tail(&mfd->mfd_list, &closing_list);
5249 spin_unlock(&med->med_open_lock);
5250 mdt = mdt_dev(obd->obd_lu_dev);
5251 LASSERT(mdt != NULL);
5253 rc = lu_env_init(&env, LCT_MD_THREAD);
5257 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5258 LASSERT(info != NULL);
5259 memset(info, 0, sizeof *info);
5260 info->mti_env = &env;
5261 info->mti_mdt = mdt;
5262 info->mti_exp = exp;
5264 if (!list_empty(&closing_list)) {
5265 struct md_attr *ma = &info->mti_attr;
5267 /* Close any open files (which may also cause orphan
5269 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5270 list_del_init(&mfd->mfd_list);
5271 ma->ma_need = ma->ma_valid = 0;
5273 /* This file is being closed due to an eviction, it
5274 * could have been modified and now dirty regarding to
5275 * HSM archive, check this!
5276 * The logic here is to mark a file dirty if there's a
5277 * chance it was dirtied before the client was evicted,
5278 * so that we don't have to wait for a release attempt
5279 * before finding out the file was actually dirty and
5280 * fail the release. Aggressively marking it dirty here
5281 * will cause the policy engine to attempt to
5282 * re-archive it; when rearchiving, we can compare the
5283 * current version to the HSM data_version and make the
5284 * archive request into a noop if it's not actually
5287 if (mfd->mfd_mode & FMODE_WRITE)
5288 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5290 /* Don't unlink orphan on failover umount, LU-184 */
5291 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5292 ma->ma_valid = MA_FLAGS;
5293 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5295 mdt_mfd_close(info, mfd);
5298 info->mti_mdt = NULL;
5299 /* cleanup client slot early */
5300 /* Do not erase record for recoverable client. */
5301 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5302 tgt_client_del(&env, exp);
5308 static inline void mdt_enable_slc(struct mdt_device *mdt)
5310 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5311 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5314 static inline void mdt_disable_slc(struct mdt_device *mdt)
5316 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5317 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5320 static int mdt_obd_disconnect(struct obd_export *exp)
5326 class_export_get(exp);
5328 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5329 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5330 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5332 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5333 mdt_disable_slc(mdt);
5336 rc = server_disconnect_export(exp);
5338 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5340 rc = mdt_export_cleanup(exp);
5341 nodemap_del_member(exp);
5342 class_export_put(exp);
5346 /* mds_connect copy */
5347 static int mdt_obd_connect(const struct lu_env *env,
5348 struct obd_export **exp, struct obd_device *obd,
5349 struct obd_uuid *cluuid,
5350 struct obd_connect_data *data,
5353 struct obd_export *lexp;
5354 struct lustre_handle conn = { 0 };
5355 struct mdt_device *mdt;
5357 lnet_nid_t *client_nid = localdata;
5360 LASSERT(env != NULL);
5361 LASSERT(data != NULL);
5363 if (!exp || !obd || !cluuid)
5366 mdt = mdt_dev(obd->obd_lu_dev);
5368 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5369 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5370 atomic_inc(&mdt->mdt_mds_mds_conns);
5371 mdt_enable_slc(mdt);
5375 * first, check whether the stack is ready to handle requests
5376 * XXX: probably not very appropriate method is used now
5377 * at some point we should find a better one
5379 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5380 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5381 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5382 rc = obd_get_info(env, mdt->mdt_child_exp,
5383 sizeof(KEY_OSP_CONNECTED),
5384 KEY_OSP_CONNECTED, NULL, NULL);
5387 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5390 rc = class_connect(&conn, obd, cluuid);
5394 lexp = class_conn2export(&conn);
5395 LASSERT(lexp != NULL);
5397 rc = nodemap_add_member(*client_nid, lexp);
5398 if (rc != 0 && rc != -EEXIST)
5401 rc = mdt_connect_internal(lexp, mdt, data);
5403 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5406 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5407 rc = tgt_client_new(env, lexp);
5409 mdt_export_stats_init(obd, lexp, localdata);
5413 class_disconnect(lexp);
5414 nodemap_del_member(lexp);
5418 /* Because we do not want this export to be evicted by pinger,
5419 * let's not add this export to the timed chain list. */
5420 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5421 spin_lock(&lexp->exp_obd->obd_dev_lock);
5422 list_del_init(&lexp->exp_obd_chain_timed);
5423 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5430 static int mdt_obd_reconnect(const struct lu_env *env,
5431 struct obd_export *exp, struct obd_device *obd,
5432 struct obd_uuid *cluuid,
5433 struct obd_connect_data *data,
5436 lnet_nid_t *client_nid = localdata;
5440 if (exp == NULL || obd == NULL || cluuid == NULL)
5443 rc = nodemap_add_member(*client_nid, exp);
5444 if (rc != 0 && rc != -EEXIST)
5447 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5449 mdt_export_stats_init(obd, exp, localdata);
5451 nodemap_del_member(exp);
5456 /* FIXME: Can we avoid using these two interfaces? */
5457 static int mdt_init_export(struct obd_export *exp)
5459 struct mdt_export_data *med = &exp->exp_mdt_data;
5463 INIT_LIST_HEAD(&med->med_open_head);
5464 spin_lock_init(&med->med_open_lock);
5465 spin_lock(&exp->exp_lock);
5466 exp->exp_connecting = 1;
5467 spin_unlock(&exp->exp_lock);
5469 /* self-export doesn't need client data and ldlm initialization */
5470 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5471 &exp->exp_client_uuid)))
5474 rc = tgt_client_alloc(exp);
5478 rc = ldlm_init_export(exp);
5485 tgt_client_free(exp);
5487 CERROR("%s: Failed to initialize export: rc = %d\n",
5488 exp->exp_obd->obd_name, rc);
5492 static int mdt_destroy_export(struct obd_export *exp)
5496 target_destroy_export(exp);
5497 /* destroy can be called from failed obd_setup, so
5498 * checking uuid is safer than obd_self_export */
5499 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5500 &exp->exp_client_uuid)))
5503 ldlm_destroy_export(exp);
5504 tgt_client_free(exp);
5506 LASSERT(list_empty(&exp->exp_outstanding_replies));
5507 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5512 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5513 struct linkea_data *ldata)
5517 LASSERT(ldata->ld_buf->lb_buf != NULL);
5519 if (!mdt_object_exists(mdt_obj))
5522 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5523 ldata->ld_buf, XATTR_NAME_LINK);
5524 if (rc == -ERANGE) {
5525 /* Buf was too small, figure out what we need. */
5526 lu_buf_free(ldata->ld_buf);
5527 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5528 ldata->ld_buf, XATTR_NAME_LINK);
5531 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5532 if (ldata->ld_buf->lb_buf == NULL)
5534 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5535 ldata->ld_buf, XATTR_NAME_LINK);
5540 return linkea_init_with_rec(ldata);
5544 * Given an MDT object, try to look up the full path to the object.
5545 * Part of the MDT layer implementation of lfs fid2path.
5547 * \param[in] info Per-thread common data shared by MDT level handlers.
5548 * \param[in] obj Object to do path lookup of
5549 * \param[in,out] fp User-provided struct to store path information
5550 * \param[in] root_fid Root FID of current path should reach
5552 * \retval 0 Lookup successful, path information stored in fp
5553 * \retval -EAGAIN Lookup failed, usually because object is being moved
5554 * \retval negative errno if there was a problem
5556 static int mdt_path_current(struct mdt_thread_info *info,
5557 struct mdt_object *obj,
5558 struct getinfo_fid2path *fp,
5559 struct lu_fid *root_fid)
5561 struct mdt_device *mdt = info->mti_mdt;
5562 struct mdt_object *mdt_obj;
5563 struct link_ea_header *leh;
5564 struct link_ea_entry *lee;
5565 struct lu_name *tmpname = &info->mti_name;
5566 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5567 struct lu_buf *buf = &info->mti_big_buf;
5570 struct linkea_data ldata = { NULL };
5575 /* temp buffer for path element, the buffer will be finally freed
5576 * in mdt_thread_info_fini */
5577 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5578 if (buf->lb_buf == NULL)
5582 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5585 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5587 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5588 struct lu_buf lmv_buf;
5590 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5591 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5592 GOTO(out, rc = -ENOENT);
5594 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5596 mdt_object_get(info->mti_env, mdt_obj);
5598 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5599 if (IS_ERR(mdt_obj))
5600 GOTO(out, rc = PTR_ERR(mdt_obj));
5603 if (!mdt_object_exists(mdt_obj)) {
5604 mdt_object_put(info->mti_env, mdt_obj);
5605 GOTO(out, rc = -ENOENT);
5608 if (mdt_object_remote(mdt_obj)) {
5609 mdt_object_put(info->mti_env, mdt_obj);
5610 GOTO(remote_out, rc = -EREMOTE);
5613 rc = mdt_links_read(info, mdt_obj, &ldata);
5615 mdt_object_put(info->mti_env, mdt_obj);
5620 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5621 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5622 /* If set, use link #linkno for path lookup, otherwise use
5623 link #0. Only do this for the final path element. */
5624 if (first && fp->gf_linkno < leh->leh_reccount) {
5626 for (count = 0; count < fp->gf_linkno; count++) {
5627 lee = (struct link_ea_entry *)
5628 ((char *)lee + reclen);
5629 linkea_entry_unpack(lee, &reclen, tmpname,
5632 if (fp->gf_linkno < leh->leh_reccount - 1)
5633 /* indicate to user there are more links */
5637 lmv_buf.lb_buf = info->mti_xattr_buf;
5638 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5639 /* Check if it is slave stripes */
5640 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5641 &lmv_buf, XATTR_NAME_LMV);
5642 mdt_object_put(info->mti_env, mdt_obj);
5644 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5646 /* For slave stripes, get its master */
5647 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5648 fp->gf_fid = *tmpfid;
5651 } else if (rc < 0 && rc != -ENODATA) {
5657 /* Pack the name in the end of the buffer */
5658 ptr -= tmpname->ln_namelen;
5659 if (ptr - 1 <= fp->gf_u.gf_path)
5660 GOTO(out, rc = -EOVERFLOW);
5661 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5664 /* keep the last resolved fid to the client, so the
5665 * client will build the left path on another MDT for
5667 fp->gf_fid = *tmpfid;
5673 ptr++; /* skip leading / */
5674 memmove(fp->gf_u.gf_path, ptr,
5675 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5682 * Given an MDT object, use mdt_path_current to get the path.
5683 * Essentially a wrapper to retry mdt_path_current a set number of times
5684 * if -EAGAIN is returned (usually because an object is being moved).
5686 * Part of the MDT layer implementation of lfs fid2path.
5688 * \param[in] info Per-thread common data shared by mdt level handlers.
5689 * \param[in] obj Object to do path lookup of
5690 * \param[in,out] fp User-provided struct for arguments and to store path
5693 * \retval 0 Lookup successful, path information stored in fp
5694 * \retval negative errno if there was a problem
5696 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5697 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5699 struct mdt_device *mdt = info->mti_mdt;
5704 if (fp->gf_pathlen < 3)
5707 if (root_fid == NULL)
5708 root_fid = &mdt->mdt_md_root_fid;
5710 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5711 fp->gf_u.gf_path[0] = '\0';
5715 /* Retry multiple times in case file is being moved */
5716 while (tries-- && rc == -EAGAIN)
5717 rc = mdt_path_current(info, obj, fp, root_fid);
5723 * Get the full path of the provided FID, as of changelog record recno.
5725 * This checks sanity and looks up object for user provided FID
5726 * before calling the actual path lookup code.
5728 * Part of the MDT layer implementation of lfs fid2path.
5730 * \param[in] info Per-thread common data shared by mdt level handlers.
5731 * \param[in,out] fp User-provided struct for arguments and to store path
5734 * \retval 0 Lookup successful, path information and recno stored in fp
5735 * \retval -ENOENT, object does not exist
5736 * \retval negative errno if there was a problem
5738 static int mdt_fid2path(struct mdt_thread_info *info,
5739 struct lu_fid *root_fid,
5740 struct getinfo_fid2path *fp)
5742 struct mdt_device *mdt = info->mti_mdt;
5743 struct mdt_object *obj;
5747 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5748 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5750 if (!fid_is_sane(&fp->gf_fid))
5753 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5754 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5755 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5756 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5760 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5763 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5764 PFID(&fp->gf_fid), rc);
5768 if (mdt_object_remote(obj))
5770 else if (!mdt_object_exists(obj))
5776 mdt_object_put(info->mti_env, obj);
5777 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5778 PFID(&fp->gf_fid), rc);
5782 rc = mdt_path(info, obj, fp, root_fid);
5784 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5785 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5786 fp->gf_recno, fp->gf_linkno);
5788 mdt_object_put(info->mti_env, obj);
5793 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5794 void *val, int vallen)
5796 struct getinfo_fid2path *fpout, *fpin;
5797 struct lu_fid *root_fid = NULL;
5800 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5803 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5804 lustre_swab_fid2path(fpin);
5806 memcpy(fpout, fpin, sizeof(*fpin));
5807 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5810 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5811 sizeof(struct lu_fid)) {
5812 /* client sent its root FID, which is normally fileset FID */
5813 root_fid = fpin->gf_u.gf_root_fid;
5814 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5815 lustre_swab_lu_fid(root_fid);
5817 if (root_fid != NULL && !fid_is_sane(root_fid))
5821 rc = mdt_fid2path(info, root_fid, fpout);
5825 int mdt_get_info(struct tgt_session_info *tsi)
5835 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5837 CDEBUG(D_IOCTL, "No GETINFO key\n");
5838 RETURN(err_serious(-EFAULT));
5840 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5843 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5844 if (vallen == NULL) {
5845 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5846 tgt_name(tsi->tsi_tgt));
5847 RETURN(err_serious(-EFAULT));
5850 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5852 rc = req_capsule_server_pack(tsi->tsi_pill);
5854 RETURN(err_serious(rc));
5856 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5857 if (valout == NULL) {
5858 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5859 tgt_name(tsi->tsi_tgt));
5860 RETURN(err_serious(-EFAULT));
5863 if (KEY_IS(KEY_FID2PATH)) {
5864 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5866 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5867 mdt_thread_info_fini(info);
5874 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5876 struct obd_ioctl_data *data = karg;
5879 struct mdt_object *obj;
5880 struct mdt_lock_handle *lh;
5884 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5885 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5888 fid = (struct lu_fid *)data->ioc_inlbuf1;
5890 if (!fid_is_sane(fid))
5893 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5895 lh = &mti->mti_lh[MDT_LH_PARENT];
5896 mdt_lock_reg_init(lh, LCK_CR);
5898 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5900 RETURN(PTR_ERR(obj));
5902 if (mdt_object_remote(obj)) {
5905 * before calling version get the correct MDS should be
5906 * fid, this is error to find remote object here
5908 CERROR("nonlocal object "DFID"\n", PFID(fid));
5909 } else if (!mdt_object_exists(obj)) {
5910 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5913 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5914 *(__u64 *)data->ioc_inlbuf2 = version;
5917 mdt_object_unlock_put(mti, obj, lh, 1);
5921 /* ioctls on obd dev */
5922 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5923 void *karg, void __user *uarg)
5926 struct obd_device *obd = exp->exp_obd;
5927 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5928 struct dt_device *dt = mdt->mdt_bottom;
5932 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5933 rc = lu_env_init(&env, LCT_MD_THREAD);
5939 rc = mdt_device_sync(&env, mdt);
5941 case OBD_IOC_SET_READONLY:
5942 rc = dt_sync(&env, dt);
5944 rc = dt_ro(&env, dt);
5946 case OBD_IOC_ABORT_RECOVERY:
5947 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5948 obd->obd_abort_recovery = 1;
5949 target_stop_recovery_thread(obd);
5952 case OBD_IOC_CHANGELOG_REG:
5953 case OBD_IOC_CHANGELOG_DEREG:
5954 case OBD_IOC_CHANGELOG_CLEAR:
5955 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
5959 case OBD_IOC_START_LFSCK: {
5960 struct md_device *next = mdt->mdt_child;
5961 struct obd_ioctl_data *data = karg;
5962 struct lfsck_start_param lsp;
5964 if (unlikely(data == NULL)) {
5969 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5970 lsp.lsp_index_valid = 0;
5971 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5974 case OBD_IOC_STOP_LFSCK: {
5975 struct md_device *next = mdt->mdt_child;
5976 struct obd_ioctl_data *data = karg;
5977 struct lfsck_stop stop;
5979 stop.ls_status = LS_STOPPED;
5980 /* Old lfsck utils may pass NULL @stop. */
5981 if (data->ioc_inlbuf1 == NULL)
5985 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5987 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5990 case OBD_IOC_QUERY_LFSCK: {
5991 struct md_device *next = mdt->mdt_child;
5992 struct obd_ioctl_data *data = karg;
5994 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
5998 case OBD_IOC_GET_OBJ_VERSION: {
5999 struct mdt_thread_info *mti;
6000 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6001 memset(mti, 0, sizeof *mti);
6002 mti->mti_env = &env;
6006 rc = mdt_ioc_version_get(mti, karg);
6009 case OBD_IOC_CATLOGLIST: {
6010 struct mdt_thread_info *mti;
6012 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6013 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6014 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6015 &mti->mti_tmp_fid1);
6020 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6021 mdt_obd_name(mdt), cmd, rc);
6028 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6030 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6034 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6035 struct lfsck_start_param lsp;
6037 lsp.lsp_start = NULL;
6038 lsp.lsp_index_valid = 0;
6039 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6040 OBD_IOC_START_LFSCK,
6042 if (rc != 0 && rc != -EALREADY)
6043 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6044 mdt_obd_name(mdt), rc);
6047 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6051 static int mdt_obd_postrecov(struct obd_device *obd)
6056 rc = lu_env_init(&env, LCT_MD_THREAD);
6059 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6064 static struct obd_ops mdt_obd_device_ops = {
6065 .o_owner = THIS_MODULE,
6066 .o_set_info_async = mdt_obd_set_info_async,
6067 .o_connect = mdt_obd_connect,
6068 .o_reconnect = mdt_obd_reconnect,
6069 .o_disconnect = mdt_obd_disconnect,
6070 .o_init_export = mdt_init_export,
6071 .o_destroy_export = mdt_destroy_export,
6072 .o_iocontrol = mdt_iocontrol,
6073 .o_postrecov = mdt_obd_postrecov,
6076 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6077 struct lu_device *d)
6079 struct mdt_device *m = mdt_dev(d);
6086 static struct lu_device *mdt_device_free(const struct lu_env *env,
6087 struct lu_device *d)
6089 struct mdt_device *m = mdt_dev(d);
6092 lu_device_fini(&m->mdt_lu_dev);
6098 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6099 struct lu_device_type *t,
6100 struct lustre_cfg *cfg)
6102 struct lu_device *l;
6103 struct mdt_device *m;
6110 rc = mdt_init0(env, m, t, cfg);
6112 mdt_device_free(env, l);
6117 l = ERR_PTR(-ENOMEM);
6121 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6122 LU_KEY_INIT(mdt, struct mdt_thread_info);
6124 static void mdt_key_fini(const struct lu_context *ctx,
6125 struct lu_context_key *key, void* data)
6127 struct mdt_thread_info *info = data;
6129 if (info->mti_big_lmm) {
6130 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6131 info->mti_big_lmm = NULL;
6132 info->mti_big_lmmsize = 0;
6137 /* context key: mdt_thread_key */
6138 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6140 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6142 return lu_ucred(info->mti_env);
6145 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6147 return lu_ucred_check(info->mti_env);
6151 * Enable/disable COS (Commit On Sharing).
6153 * Set/Clear the COS flag in mdt options.
6155 * \param mdt mdt device
6156 * \param val 0 disables COS, other values enable COS
6158 void mdt_enable_cos(struct mdt_device *mdt, int val)
6163 mdt->mdt_opts.mo_cos = !!val;
6164 rc = lu_env_init(&env, LCT_LOCAL);
6165 if (unlikely(rc != 0)) {
6166 CWARN("%s: lu_env initialization failed, cannot "
6167 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6170 mdt_device_sync(&env, mdt);
6175 * Check COS (Commit On Sharing) status.
6177 * Return COS flag status.
6179 * \param mdt mdt device
6181 int mdt_cos_is_enabled(struct mdt_device *mdt)
6183 return mdt->mdt_opts.mo_cos != 0;
6186 static struct lu_device_type_operations mdt_device_type_ops = {
6187 .ldto_device_alloc = mdt_device_alloc,
6188 .ldto_device_free = mdt_device_free,
6189 .ldto_device_fini = mdt_device_fini
6192 static struct lu_device_type mdt_device_type = {
6193 .ldt_tags = LU_DEVICE_MD,
6194 .ldt_name = LUSTRE_MDT_NAME,
6195 .ldt_ops = &mdt_device_type_ops,
6196 .ldt_ctx_tags = LCT_MD_THREAD
6199 static int __init mdt_init(void)
6203 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6204 FID_NOBRACE_LEN + 1);
6205 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6207 rc = lu_kmem_init(mdt_caches);
6211 rc = mds_mod_init();
6215 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6216 LUSTRE_MDT_NAME, &mdt_device_type);
6221 lu_kmem_fini(mdt_caches);
6228 static void __exit mdt_exit(void)
6230 class_unregister_type(LUSTRE_MDT_NAME);
6232 lu_kmem_fini(mdt_caches);
6235 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6236 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6237 MODULE_VERSION(LUSTRE_VERSION_STRING);
6238 MODULE_LICENSE("GPL");
6240 module_init(mdt_init);
6241 module_exit(mdt_exit);