4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.h>
64 #include <obd_cksum.h>
65 #include <llog_swab.h>
67 #include "mdt_internal.h"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
110 static struct lu_kmem_descr mdt_caches[] = {
112 .ckd_cache = &mdt_object_kmem,
113 .ckd_name = "mdt_obj",
114 .ckd_size = sizeof(struct mdt_object)
117 .ckd_cache = &mdt_hsm_cdt_kmem,
118 .ckd_name = "mdt_cdt_restore_handle",
119 .ckd_size = sizeof(struct cdt_restore_handle)
122 .ckd_cache = &mdt_hsm_car_kmem,
123 .ckd_name = "mdt_cdt_agent_req",
124 .ckd_size = sizeof(struct cdt_agent_req)
131 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
135 return rep->lock_policy_res1 & op_flag;
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139 struct ldlm_reply *rep, __u64 op_flag)
142 info->mti_opdata &= ~op_flag;
143 tgt_opdata_clear(info->mti_env, op_flag);
146 rep->lock_policy_res1 &= ~op_flag;
149 void mdt_set_disposition(struct mdt_thread_info *info,
150 struct ldlm_reply *rep, __u64 op_flag)
153 info->mti_opdata |= op_flag;
154 tgt_opdata_set(info->mti_env, op_flag);
157 rep->lock_policy_res1 |= op_flag;
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
162 lh->mlh_pdo_hash = 0;
163 lh->mlh_reg_mode = lm;
164 lh->mlh_rreg_mode = lm;
165 lh->mlh_type = MDT_REG_LOCK;
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
169 const struct lu_name *lname)
171 lh->mlh_reg_mode = lock_mode;
172 lh->mlh_pdo_mode = LCK_MINMODE;
173 lh->mlh_rreg_mode = lock_mode;
174 lh->mlh_type = MDT_PDO_LOCK;
176 if (lu_name_is_valid(lname)) {
177 lh->mlh_pdo_hash = ll_full_name_hash(NULL, lname->ln_name,
179 /* XXX Workaround for LU-2856
181 * Zero is a valid return value of full_name_hash, but
182 * several users of mlh_pdo_hash assume a non-zero
183 * hash value. We therefore map zero onto an
184 * arbitrary, but consistent value (1) to avoid
185 * problems further down the road. */
186 if (unlikely(lh->mlh_pdo_hash == 0))
187 lh->mlh_pdo_hash = 1;
189 lh->mlh_pdo_hash = 0;
193 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
194 struct mdt_lock_handle *lh)
200 * Any dir access needs couple of locks:
202 * 1) on part of dir we gonna take lookup/modify;
204 * 2) on whole dir to protect it from concurrent splitting and/or to
205 * flush client's cache for readdir().
207 * so, for a given mode and object this routine decides what lock mode
208 * to use for lock #2:
210 * 1) if caller's gonna lookup in dir then we need to protect dir from
211 * being splitted only - LCK_CR
213 * 2) if caller's gonna modify dir then we need to protect dir from
214 * being splitted and to flush cache - LCK_CW
216 * 3) if caller's gonna modify dir and that dir seems ready for
217 * splitting then we need to protect it from any type of access
218 * (lookup/modify/split) - LCK_EX --bzzz
221 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
222 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
225 * Ask underlaying level its opinion about preferable PDO lock mode
226 * having access type passed as regular lock mode:
228 * - MDL_MINMODE means that lower layer does not want to specify lock
231 * - MDL_NL means that no PDO lock should be taken. This is used in some
232 * cases. Say, for non-splittable directories no need to use PDO locks
235 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
236 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
238 if (mode != MDL_MINMODE) {
239 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
242 * Lower layer does not want to specify locking mode. We do it
243 * our selves. No special protection is needed, just flush
244 * client's cache on modification and allow concurrent
247 switch (lh->mlh_reg_mode) {
249 lh->mlh_pdo_mode = LCK_EX;
252 lh->mlh_pdo_mode = LCK_CR;
255 lh->mlh_pdo_mode = LCK_CW;
258 CERROR("Not expected lock type (0x%x)\n",
259 (int)lh->mlh_reg_mode);
264 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
268 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
271 struct mdt_device *mdt = info->mti_mdt;
272 struct lu_name *lname = &info->mti_name;
274 struct mdt_object *parent;
278 LASSERT(!info->mti_cross_ref);
280 OBD_ALLOC(name, NAME_MAX + 1);
283 lname->ln_name = name;
286 * We may want to allow this to mount a completely separate
287 * fileset from the MDT in the future, but keeping it to
288 * ROOT/ only for now avoid potential security issues.
290 *fid = mdt->mdt_md_root_fid;
292 while (rc == 0 && fileset != NULL && *fileset != '\0') {
293 const char *s1 = fileset;
299 while (*s2 != '/' && *s2 != '\0')
307 lname->ln_namelen = s2 - s1;
308 if (lname->ln_namelen > NAME_MAX) {
313 /* reject .. as a path component */
314 if (lname->ln_namelen == 2 &&
315 strncmp(s1, "..", 2) == 0) {
320 strncpy(name, s1, lname->ln_namelen);
321 name[lname->ln_namelen] = '\0';
323 parent = mdt_object_find(info->mti_env, mdt, fid);
324 if (IS_ERR(parent)) {
325 rc = PTR_ERR(parent);
328 /* Only got the fid of this obj by name */
330 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
331 fid, &info->mti_spec);
332 mdt_object_put(info->mti_env, parent);
335 parent = mdt_object_find(info->mti_env, mdt, fid);
337 rc = PTR_ERR(parent);
339 mode = lu_object_attr(&parent->mot_obj);
340 mdt_object_put(info->mti_env, parent);
346 OBD_FREE(name, NAME_MAX + 1);
351 static int mdt_get_root(struct tgt_session_info *tsi)
353 struct mdt_thread_info *info = tsi2mdt_info(tsi);
354 struct mdt_device *mdt = info->mti_mdt;
355 struct mdt_body *repbody;
356 char *fileset = NULL, *buffer = NULL;
358 struct obd_export *exp = info->mti_exp;
359 char *nodemap_fileset;
363 rc = mdt_check_ucred(info);
365 GOTO(out, rc = err_serious(rc));
367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
368 GOTO(out, rc = err_serious(-ENOMEM));
370 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
371 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
372 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
374 GOTO(out, rc = err_serious(-EFAULT));
377 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
378 if (nodemap_fileset && nodemap_fileset[0]) {
379 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
381 /* consider fileset from client as a sub-fileset
382 * of the nodemap one */
383 OBD_ALLOC(buffer, PATH_MAX + 1);
385 GOTO(out, rc = err_serious(-ENOMEM));
386 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
387 nodemap_fileset, fileset) >= PATH_MAX + 1)
388 GOTO(out, rc = err_serious(-EINVAL));
391 /* enforce fileset as specified in the nodemap */
392 fileset = nodemap_fileset;
397 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
398 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
400 GOTO(out, rc = err_serious(rc));
402 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
404 repbody->mbo_valid |= OBD_MD_FLID;
408 mdt_thread_info_fini(info);
410 OBD_FREE(buffer, PATH_MAX+1);
414 static int mdt_statfs(struct tgt_session_info *tsi)
416 struct ptlrpc_request *req = tgt_ses_req(tsi);
417 struct mdt_thread_info *info = tsi2mdt_info(tsi);
418 struct md_device *next = info->mti_mdt->mdt_child;
419 struct ptlrpc_service_part *svcpt;
420 struct obd_statfs *osfs;
425 svcpt = req->rq_rqbd->rqbd_svcpt;
427 /* This will trigger a watchdog timeout */
428 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
429 (MDT_SERVICE_WATCHDOG_FACTOR *
430 at_get(&svcpt->scp_at_estimate)) + 1);
432 rc = mdt_check_ucred(info);
434 GOTO(out, rc = err_serious(rc));
436 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
437 GOTO(out, rc = err_serious(-ENOMEM));
439 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
441 GOTO(out, rc = -EPROTO);
443 /** statfs information are cached in the mdt_device */
444 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
445 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
446 /** statfs data is too old, get up-to-date one */
447 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
450 spin_lock(&info->mti_mdt->mdt_lock);
451 info->mti_mdt->mdt_osfs = *osfs;
452 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
453 spin_unlock(&info->mti_mdt->mdt_lock);
455 /** use cached statfs data */
456 spin_lock(&info->mti_mdt->mdt_lock);
457 *osfs = info->mti_mdt->mdt_osfs;
458 spin_unlock(&info->mti_mdt->mdt_lock);
462 mdt_counter_incr(req, LPROC_MDT_STATFS);
464 mdt_thread_info_fini(info);
468 #ifdef CONFIG_FS_POSIX_ACL
470 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
472 * \param info thread info object
473 * \param repbody reply to pack ACLs into
474 * \param o mdt object of file to examine
475 * \param nodemap nodemap of client to reply to
477 * \retval -errno error getting or parsing ACL from disk
479 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
480 struct mdt_object *o, struct lu_nodemap *nodemap)
482 const struct lu_env *env = info->mti_env;
483 struct md_object *next = mdt_object_child(o);
484 struct lu_buf *buf = &info->mti_buf;
485 struct mdt_device *mdt = info->mti_mdt;
490 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
491 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
493 if (buf->lb_len == 0)
497 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
499 if (rc == -ENODATA) {
500 repbody->mbo_aclsize = 0;
501 repbody->mbo_valid |= OBD_MD_FLACL;
503 } else if (rc == -EOPNOTSUPP) {
507 exp_connect_large_acl(info->mti_exp) &&
508 buf->lb_buf != info->mti_big_acl) {
509 if (info->mti_big_acl == NULL) {
510 OBD_ALLOC_LARGE(info->mti_big_acl,
511 mdt->mdt_max_ea_size);
512 if (info->mti_big_acl == NULL) {
513 CERROR("%s: unable to grow "
516 PFID(mdt_object_fid(o)));
520 info->mti_big_aclsize =
521 mdt->mdt_max_ea_size;
524 CDEBUG(D_INODE, "%s: grow the "DFID
525 " ACL buffer to size %d\n",
527 PFID(mdt_object_fid(o)),
528 mdt->mdt_max_ea_size);
530 buf->lb_buf = info->mti_big_acl;
531 buf->lb_len = info->mti_big_aclsize;
536 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
537 mdt_obd_name(mdt), PFID(mdt_object_fid(o)), rc);
540 if (buf->lb_buf == info->mti_big_acl)
541 info->mti_big_acl_used = 1;
543 rc = nodemap_map_acl(nodemap, buf->lb_buf,
544 rc, NODEMAP_FS_TO_CLIENT);
545 /* if all ACLs mapped out, rc is still >= 0 */
547 CERROR("%s: nodemap_map_acl unable to parse "DFID
548 " ACL: rc = %d\n", mdt_obd_name(mdt),
549 PFID(mdt_object_fid(o)), rc);
551 repbody->mbo_aclsize = rc;
552 repbody->mbo_valid |= OBD_MD_FLACL;
561 /* XXX Look into layout in MDT layer. */
562 static inline bool mdt_hsm_is_released(struct lov_mds_md *lmm)
564 struct lov_comp_md_v1 *comp_v1;
565 struct lov_mds_md *v1;
568 if (lmm->lmm_magic == LOV_MAGIC_COMP_V1) {
569 comp_v1 = (struct lov_comp_md_v1 *)lmm;
571 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
572 v1 = (struct lov_mds_md *)((char *)comp_v1 +
573 comp_v1->lcm_entries[i].lcme_offset);
574 /* We don't support partial release for now */
575 if (!(v1->lmm_pattern & LOV_PATTERN_F_RELEASED))
580 return (lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) ?
585 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
586 const struct lu_attr *attr, const struct lu_fid *fid)
588 struct md_attr *ma = &info->mti_attr;
589 struct obd_export *exp = info->mti_exp;
590 struct lu_nodemap *nodemap = NULL;
592 LASSERT(ma->ma_valid & MA_INODE);
594 if (attr->la_valid & LA_ATIME) {
595 b->mbo_atime = attr->la_atime;
596 b->mbo_valid |= OBD_MD_FLATIME;
598 if (attr->la_valid & LA_MTIME) {
599 b->mbo_mtime = attr->la_mtime;
600 b->mbo_valid |= OBD_MD_FLMTIME;
602 if (attr->la_valid & LA_CTIME) {
603 b->mbo_ctime = attr->la_ctime;
604 b->mbo_valid |= OBD_MD_FLCTIME;
606 if (attr->la_valid & LA_FLAGS) {
607 b->mbo_flags = attr->la_flags;
608 b->mbo_valid |= OBD_MD_FLFLAGS;
610 if (attr->la_valid & LA_NLINK) {
611 b->mbo_nlink = attr->la_nlink;
612 b->mbo_valid |= OBD_MD_FLNLINK;
614 if (attr->la_valid & (LA_UID|LA_GID)) {
615 nodemap = nodemap_get_from_exp(exp);
619 if (attr->la_valid & LA_UID) {
620 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
621 NODEMAP_FS_TO_CLIENT,
623 b->mbo_valid |= OBD_MD_FLUID;
625 if (attr->la_valid & LA_GID) {
626 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
627 NODEMAP_FS_TO_CLIENT,
629 b->mbo_valid |= OBD_MD_FLGID;
632 if (attr->la_valid & LA_PROJID) {
633 /* TODO, nodemap for project id */
634 b->mbo_projid = attr->la_projid;
635 b->mbo_valid |= OBD_MD_FLPROJID;
638 b->mbo_mode = attr->la_mode;
639 if (attr->la_valid & LA_MODE)
640 b->mbo_valid |= OBD_MD_FLMODE;
641 if (attr->la_valid & LA_TYPE)
642 b->mbo_valid |= OBD_MD_FLTYPE;
646 b->mbo_valid |= OBD_MD_FLID;
647 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
648 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
651 if (!(attr->la_valid & LA_TYPE))
654 b->mbo_rdev = attr->la_rdev;
655 b->mbo_size = attr->la_size;
656 b->mbo_blocks = attr->la_blocks;
658 if (!S_ISREG(attr->la_mode)) {
659 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
660 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
661 /* means no objects are allocated on osts. */
662 LASSERT(!(ma->ma_valid & MA_LOV));
663 /* just ignore blocks occupied by extend attributes on MDS */
665 /* if no object is allocated on osts, the size on mds is valid.
667 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
668 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL) {
669 if (mdt_hsm_is_released(ma->ma_lmm)) {
670 /* A released file stores its size on MDS. */
671 /* But return 1 block for released file, unless tools
672 * like tar will consider it fully sparse. (LU-3864)
674 if (unlikely(b->mbo_size == 0))
678 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
679 } else if (lov_pattern(ma->ma_lmm->lmm_pattern) ==
681 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
685 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
686 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
687 PFID(fid), (unsigned long long)b->mbo_size);
690 if (!IS_ERR_OR_NULL(nodemap))
691 nodemap_putref(nodemap);
694 static inline int mdt_body_has_lov(const struct lu_attr *la,
695 const struct mdt_body *body)
697 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
698 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
701 void mdt_client_compatibility(struct mdt_thread_info *info)
703 struct mdt_body *body;
704 struct ptlrpc_request *req = mdt_info_req(info);
705 struct obd_export *exp = req->rq_export;
706 struct md_attr *ma = &info->mti_attr;
707 struct lu_attr *la = &ma->ma_attr;
710 if (exp_connect_layout(exp))
711 /* the client can deal with 16-bit lmm_stripe_count */
714 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
716 if (!mdt_body_has_lov(la, body))
719 /* now we have a reply with a lov for a client not compatible with the
720 * layout lock so we have to clean the layout generation number */
721 if (S_ISREG(la->la_mode))
722 ma->ma_lmm->lmm_layout_gen = 0;
726 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
727 struct mdt_object *o)
729 const struct lu_env *env = info->mti_env;
732 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
741 /* Is it a directory? Let's check for the LMV as well */
742 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
743 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
747 rc2 = mo_xattr_get(env, mdt_object_child(o),
749 XATTR_NAME_DEFAULT_LMV);
751 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
759 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
762 const struct lu_env *env = info->mti_env;
766 LASSERT(info->mti_big_lmm_used == 0);
767 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
771 /* big_lmm may need to be grown */
772 if (info->mti_big_lmmsize < rc) {
773 int size = size_roundup_power2(rc);
775 if (info->mti_big_lmmsize > 0) {
776 /* free old buffer */
777 LASSERT(info->mti_big_lmm);
778 OBD_FREE_LARGE(info->mti_big_lmm,
779 info->mti_big_lmmsize);
780 info->mti_big_lmm = NULL;
781 info->mti_big_lmmsize = 0;
784 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
785 if (info->mti_big_lmm == NULL)
787 info->mti_big_lmmsize = size;
789 LASSERT(info->mti_big_lmmsize >= rc);
791 info->mti_buf.lb_buf = info->mti_big_lmm;
792 info->mti_buf.lb_len = info->mti_big_lmmsize;
793 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
798 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
799 struct md_attr *ma, const char *name)
801 struct md_object *next = mdt_object_child(o);
802 struct lu_buf *buf = &info->mti_buf;
805 if (strcmp(name, XATTR_NAME_LOV) == 0) {
806 buf->lb_buf = ma->ma_lmm;
807 buf->lb_len = ma->ma_lmm_size;
808 LASSERT(!(ma->ma_valid & MA_LOV));
809 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
810 buf->lb_buf = ma->ma_lmv;
811 buf->lb_len = ma->ma_lmv_size;
812 LASSERT(!(ma->ma_valid & MA_LMV));
813 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
814 buf->lb_buf = ma->ma_lmv;
815 buf->lb_len = ma->ma_lmv_size;
816 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
821 rc = mo_xattr_get(info->mti_env, next, buf, name);
825 if (strcmp(name, XATTR_NAME_LOV) == 0) {
826 if (info->mti_big_lmm_used)
827 ma->ma_lmm = info->mti_big_lmm;
829 /* NOT return LOV EA with hole to old client. */
830 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
831 LOV_PATTERN_F_HOLE) &&
832 !(exp_connect_flags(info->mti_exp) &
833 OBD_CONNECT_LFSCK)) {
836 ma->ma_lmm_size = rc;
837 ma->ma_valid |= MA_LOV;
839 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
840 if (info->mti_big_lmm_used)
841 ma->ma_lmv = info->mti_big_lmm;
843 ma->ma_lmv_size = rc;
844 ma->ma_valid |= MA_LMV;
845 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
846 ma->ma_lmv_size = rc;
847 ma->ma_valid |= MA_LMV_DEF;
850 /* Update mdt_max_mdsize so all clients will be aware that */
851 if (info->mti_mdt->mdt_max_mdsize < rc)
852 info->mti_mdt->mdt_max_mdsize = rc;
855 } else if (rc == -ENODATA) {
858 } else if (rc == -ERANGE) {
859 /* Default LMV has fixed size, so it must be able to fit
860 * in the original buffer */
861 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
863 rc = mdt_big_xattr_get(info, o, name);
865 info->mti_big_lmm_used = 1;
873 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
874 struct mdt_object *o, struct lu_fid *pfid)
876 struct lu_buf *buf = &info->mti_buf;
877 struct link_ea_header *leh;
878 struct link_ea_entry *lee;
882 buf->lb_buf = info->mti_big_lmm;
883 buf->lb_len = info->mti_big_lmmsize;
884 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
885 buf, XATTR_NAME_LINK);
886 /* ignore errors, MA_PFID won't be set and it is
887 * up to the caller to treat this as an error */
888 if (rc == -ERANGE || buf->lb_len == 0) {
889 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
890 buf->lb_buf = info->mti_big_lmm;
891 buf->lb_len = info->mti_big_lmmsize;
896 if (rc < sizeof(*leh)) {
897 CERROR("short LinkEA on "DFID": rc = %d\n",
898 PFID(mdt_object_fid(o)), rc);
902 leh = (struct link_ea_header *) buf->lb_buf;
903 lee = (struct link_ea_entry *)(leh + 1);
904 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
905 leh->leh_magic = LINK_EA_MAGIC;
906 leh->leh_reccount = __swab32(leh->leh_reccount);
907 leh->leh_len = __swab64(leh->leh_len);
909 if (leh->leh_magic != LINK_EA_MAGIC)
911 if (leh->leh_reccount == 0)
914 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
915 fid_be_to_cpu(pfid, pfid);
920 int mdt_attr_get_complex(struct mdt_thread_info *info,
921 struct mdt_object *o, struct md_attr *ma)
923 const struct lu_env *env = info->mti_env;
924 struct md_object *next = mdt_object_child(o);
925 struct lu_buf *buf = &info->mti_buf;
926 int need = ma->ma_need;
933 if (mdt_object_exists(o) == 0)
934 GOTO(out, rc = -ENOENT);
935 mode = lu_object_attr(&next->mo_lu);
937 if (need & MA_INODE) {
938 ma->ma_need = MA_INODE;
939 rc = mo_attr_get(env, next, ma);
942 ma->ma_valid |= MA_INODE;
945 if (need & MA_PFID) {
946 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
948 ma->ma_valid |= MA_PFID;
949 /* ignore this error, parent fid is not mandatory */
953 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
954 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
959 if (need & MA_LMV && S_ISDIR(mode)) {
960 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
965 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
966 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
971 if (need & MA_HSM && S_ISREG(mode)) {
972 buf->lb_buf = info->mti_xattr_buf;
973 buf->lb_len = sizeof(info->mti_xattr_buf);
974 CLASSERT(sizeof(struct hsm_attrs) <=
975 sizeof(info->mti_xattr_buf));
976 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
977 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
979 ma->ma_valid |= MA_HSM;
980 else if (rc2 < 0 && rc2 != -ENODATA)
984 #ifdef CONFIG_FS_POSIX_ACL
985 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
986 buf->lb_buf = ma->ma_acl;
987 buf->lb_len = ma->ma_acl_size;
988 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
990 ma->ma_acl_size = rc2;
991 ma->ma_valid |= MA_ACL_DEF;
992 } else if (rc2 == -ENODATA) {
1001 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1002 rc, ma->ma_valid, ma->ma_lmm);
1006 static int mdt_getattr_internal(struct mdt_thread_info *info,
1007 struct mdt_object *o, int ma_need)
1009 struct md_object *next = mdt_object_child(o);
1010 const struct mdt_body *reqbody = info->mti_body;
1011 struct ptlrpc_request *req = mdt_info_req(info);
1012 struct md_attr *ma = &info->mti_attr;
1013 struct lu_attr *la = &ma->ma_attr;
1014 struct req_capsule *pill = info->mti_pill;
1015 const struct lu_env *env = info->mti_env;
1016 struct mdt_body *repbody;
1017 struct lu_buf *buffer = &info->mti_buf;
1018 struct obd_export *exp = info->mti_exp;
1022 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1023 RETURN(err_serious(-ENOMEM));
1025 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1029 if (mdt_object_remote(o)) {
1030 /* This object is located on remote node.*/
1031 /* Return -ENOTSUPP for old client */
1032 if (!mdt_is_dne_client(req->rq_export))
1033 GOTO(out, rc = -ENOTSUPP);
1035 repbody->mbo_fid1 = *mdt_object_fid(o);
1036 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1040 if (reqbody->mbo_eadatasize > 0) {
1041 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1042 if (buffer->lb_buf == NULL)
1043 GOTO(out, rc = -EPROTO);
1044 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1047 buffer->lb_buf = NULL;
1049 ma_need &= ~(MA_LOV | MA_LMV);
1050 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1051 mdt_obd_name(info->mti_mdt),
1052 req->rq_export->exp_client_uuid.uuid);
1055 /* If it is dir object and client require MEA, then we got MEA */
1056 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1057 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1058 /* Assumption: MDT_MD size is enough for lmv size. */
1059 ma->ma_lmv = buffer->lb_buf;
1060 ma->ma_lmv_size = buffer->lb_len;
1061 ma->ma_need = MA_INODE;
1062 if (ma->ma_lmv_size > 0) {
1063 if (reqbody->mbo_valid & OBD_MD_MEA)
1064 ma->ma_need |= MA_LMV;
1065 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1066 ma->ma_need |= MA_LMV_DEF;
1069 ma->ma_lmm = buffer->lb_buf;
1070 ma->ma_lmm_size = buffer->lb_len;
1071 ma->ma_need = MA_INODE | MA_HSM;
1072 if (ma->ma_lmm_size > 0)
1073 ma->ma_need |= MA_LOV;
1076 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1077 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1078 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1079 /* get default stripe info for this dir. */
1080 ma->ma_need |= MA_LOV_DEF;
1082 ma->ma_need |= ma_need;
1084 rc = mdt_attr_get_complex(info, o, ma);
1086 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1087 "%s: getattr error for "DFID": rc = %d\n",
1088 mdt_obd_name(info->mti_mdt),
1089 PFID(mdt_object_fid(o)), rc);
1093 /* if file is released, check if a restore is running */
1094 if (ma->ma_valid & MA_HSM) {
1095 repbody->mbo_valid |= OBD_MD_TSTATE;
1096 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1097 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1098 repbody->mbo_t_state = MS_RESTORE;
1101 if (likely(ma->ma_valid & MA_INODE))
1102 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1106 if (mdt_body_has_lov(la, reqbody)) {
1107 if (ma->ma_valid & MA_LOV) {
1108 LASSERT(ma->ma_lmm_size);
1109 repbody->mbo_eadatasize = ma->ma_lmm_size;
1110 if (S_ISDIR(la->la_mode))
1111 repbody->mbo_valid |= OBD_MD_FLDIREA;
1113 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1114 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1116 if (ma->ma_valid & MA_LMV) {
1117 /* Return -ENOTSUPP for old client */
1118 if (!mdt_is_striped_client(req->rq_export))
1121 LASSERT(S_ISDIR(la->la_mode));
1122 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1123 repbody->mbo_eadatasize = ma->ma_lmv_size;
1124 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1126 if (ma->ma_valid & MA_LMV_DEF) {
1127 /* Return -ENOTSUPP for old client */
1128 if (!mdt_is_striped_client(req->rq_export))
1130 LASSERT(S_ISDIR(la->la_mode));
1131 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1132 repbody->mbo_eadatasize = ma->ma_lmv_size;
1133 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1134 OBD_MD_DEFAULT_MEA);
1136 } else if (S_ISLNK(la->la_mode) &&
1137 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1138 buffer->lb_buf = ma->ma_lmm;
1139 /* eadatasize from client includes NULL-terminator, so
1140 * there is no need to read it */
1141 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1142 rc = mo_readlink(env, next, buffer);
1143 if (unlikely(rc <= 0)) {
1144 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1145 mdt_obd_name(info->mti_mdt),
1146 PFID(mdt_object_fid(o)), rc);
1149 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1151 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1153 repbody->mbo_valid |= OBD_MD_LINKNAME;
1154 /* we need to report back size with NULL-terminator
1155 * because client expects that */
1156 repbody->mbo_eadatasize = rc + 1;
1157 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1158 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1159 "on "DFID ", expected %d\n",
1160 mdt_obd_name(info->mti_mdt),
1161 rc, PFID(mdt_object_fid(o)),
1162 reqbody->mbo_eadatasize - 1);
1163 /* NULL terminate */
1164 ((char *)ma->ma_lmm)[rc] = 0;
1166 /* If the total CDEBUG() size is larger than a page, it
1167 * will print a warning to the console, avoid this by
1168 * printing just the last part of the symlink. */
1169 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1170 print_limit < rc ? "..." : "", print_limit,
1171 (char *)ma->ma_lmm + rc - print_limit, rc);
1176 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1177 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1178 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1179 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1180 repbody->mbo_max_mdsize);
1183 #ifdef CONFIG_FS_POSIX_ACL
1184 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1185 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1186 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1187 if (IS_ERR(nodemap))
1188 RETURN(PTR_ERR(nodemap));
1190 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1191 nodemap_putref(nodemap);
1197 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1202 static int mdt_getattr(struct tgt_session_info *tsi)
1204 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1205 struct mdt_object *obj = info->mti_object;
1206 struct req_capsule *pill = info->mti_pill;
1207 struct mdt_body *reqbody;
1208 struct mdt_body *repbody;
1212 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1214 LASSERT(obj != NULL);
1215 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1217 /* Unlike intent case where we need to pre-fill out buffers early on
1218 * in intent policy for ldlm reasons, here we can have a much better
1219 * guess at EA size by just reading it from disk.
1220 * Exceptions are readdir and (missing) directory striping */
1222 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1223 /* No easy way to know how long is the symlink, but it cannot
1224 * be more than PATH_MAX, so we allocate +1 */
1227 /* A special case for fs ROOT: getattr there might fetch
1228 * default EA for entire fs, not just for this dir!
1230 } else if (lu_fid_eq(mdt_object_fid(obj),
1231 &info->mti_mdt->mdt_md_root_fid) &&
1232 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1233 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1235 /* Should the default strping be bigger, mdt_fix_reply
1236 * will reallocate */
1237 rc = DEF_REP_MD_SIZE;
1239 /* Read the actual EA size from disk */
1240 rc = mdt_attr_get_eabuf_size(info, obj);
1244 GOTO(out, rc = err_serious(rc));
1246 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1248 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1249 * by default. If the target object has more ACL entries, then
1250 * enlarge the buffer when necessary. */
1251 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1252 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1254 rc = req_capsule_server_pack(pill);
1255 if (unlikely(rc != 0))
1256 GOTO(out, rc = err_serious(rc));
1258 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1259 LASSERT(repbody != NULL);
1260 repbody->mbo_eadatasize = 0;
1261 repbody->mbo_aclsize = 0;
1263 rc = mdt_check_ucred(info);
1265 GOTO(out_shrink, rc);
1267 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1269 rc = mdt_getattr_internal(info, obj, 0);
1272 mdt_client_compatibility(info);
1273 rc2 = mdt_fix_reply(info);
1277 mdt_thread_info_fini(info);
1282 * Handler of layout intent RPC requiring the layout modification
1284 * \param[in] info thread environment
1285 * \param[in] obj object
1286 * \param[in] layout layout intent
1287 * \param[in] buf buffer containing client's lovea, could be empty
1289 * \retval 0 on success
1290 * \retval < 0 error code
1292 static int mdt_layout_change(struct mdt_thread_info *info,
1293 struct mdt_object *obj,
1294 struct layout_intent *layout,
1295 const struct lu_buf *buf)
1297 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1301 CDEBUG(D_INFO, "got layout change request from client: "
1302 "opc:%u flags:%#x extent[%#llx,%#llx)\n",
1303 layout->li_opc, layout->li_flags,
1304 layout->li_start, layout->li_end);
1305 if (layout->li_start >= layout->li_end) {
1306 CERROR("Recieved an invalid layout change range [%llu, %llu) "
1307 "for "DFID"\n", layout->li_start, layout->li_end,
1308 PFID(mdt_object_fid(obj)));
1312 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1313 GOTO(out, rc = -EINVAL);
1315 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1320 /* take layout lock to prepare layout change */
1321 mdt_lock_reg_init(lh, LCK_EX);
1322 rc = mdt_object_lock(info, obj, lh,
1323 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1327 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1330 mdt_object_unlock(info, obj, lh, 1);
1336 * Exchange MOF_LOV_CREATED flags between two objects after a
1337 * layout swap. No assumption is made on whether o1 or o2 have
1338 * created objects or not.
1340 * \param[in,out] o1 First swap layout object
1341 * \param[in,out] o2 Second swap layout object
1343 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1345 unsigned int o1_lov_created = o1->mot_lov_created;
1347 mutex_lock(&o1->mot_lov_mutex);
1348 mutex_lock(&o2->mot_lov_mutex);
1350 o1->mot_lov_created = o2->mot_lov_created;
1351 o2->mot_lov_created = o1_lov_created;
1353 mutex_unlock(&o2->mot_lov_mutex);
1354 mutex_unlock(&o1->mot_lov_mutex);
1357 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1359 struct mdt_thread_info *info;
1360 struct ptlrpc_request *req = tgt_ses_req(tsi);
1361 struct obd_export *exp = req->rq_export;
1362 struct mdt_object *o1, *o2, *o;
1363 struct mdt_lock_handle *lh1, *lh2;
1364 struct mdc_swap_layouts *msl;
1368 /* client does not support layout lock, so layout swaping
1370 * FIXME: there is a problem for old clients which don't support
1371 * layout lock yet. If those clients have already opened the file
1372 * they won't be notified at all so that old layout may still be
1373 * used to do IO. This can be fixed after file release is landed by
1374 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1375 if (!exp_connect_layout(exp))
1376 RETURN(-EOPNOTSUPP);
1378 info = tsi2mdt_info(tsi);
1380 if (info->mti_dlm_req != NULL)
1381 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1383 o1 = info->mti_object;
1384 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1385 &info->mti_body->mbo_fid2);
1387 GOTO(out, rc = PTR_ERR(o));
1389 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1390 GOTO(put, rc = -ENOENT);
1392 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1393 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1399 /* permission check. Make sure the calling process having permission
1400 * to write both files. */
1401 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1406 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1411 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1413 GOTO(put, rc = -EPROTO);
1415 lh1 = &info->mti_lh[MDT_LH_NEW];
1416 mdt_lock_reg_init(lh1, LCK_EX);
1417 lh2 = &info->mti_lh[MDT_LH_OLD];
1418 mdt_lock_reg_init(lh2, LCK_EX);
1420 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1421 MDS_INODELOCK_XATTR);
1425 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1426 MDS_INODELOCK_XATTR);
1430 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1431 mdt_object_child(o2), msl->msl_flags);
1435 mdt_swap_lov_flag(o1, o2);
1438 mdt_object_unlock(info, o2, lh2, rc);
1440 mdt_object_unlock(info, o1, lh1, rc);
1442 mdt_object_put(info->mti_env, o);
1444 mdt_thread_info_fini(info);
1448 static int mdt_raw_lookup(struct mdt_thread_info *info,
1449 struct mdt_object *parent,
1450 const struct lu_name *lname,
1451 struct ldlm_reply *ldlm_rep)
1453 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1457 LASSERT(!info->mti_cross_ref);
1459 /* Only got the fid of this obj by name */
1460 fid_zero(child_fid);
1461 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1462 lname, child_fid, &info->mti_spec);
1464 struct mdt_body *repbody;
1466 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1467 repbody->mbo_fid1 = *child_fid;
1468 repbody->mbo_valid = OBD_MD_FLID;
1469 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1470 } else if (rc == -ENOENT) {
1471 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1478 * UPDATE lock should be taken against parent, and be released before exit;
1479 * child_bits lock should be taken against child, and be returned back:
1480 * (1)normal request should release the child lock;
1481 * (2)intent request will grant the lock to client.
1483 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1484 struct mdt_lock_handle *lhc,
1486 struct ldlm_reply *ldlm_rep)
1488 struct ptlrpc_request *req = mdt_info_req(info);
1489 struct mdt_body *reqbody = NULL;
1490 struct mdt_object *parent = info->mti_object;
1491 struct mdt_object *child;
1492 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1493 struct lu_name *lname = NULL;
1494 struct mdt_lock_handle *lhp = NULL;
1495 struct ldlm_lock *lock;
1503 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1504 LASSERT(ergo(is_resent,
1505 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1510 if (info->mti_cross_ref) {
1511 /* Only getattr on the child. Parent is on another node. */
1512 mdt_set_disposition(info, ldlm_rep,
1513 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1515 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1517 PFID(mdt_object_fid(child)), ldlm_rep);
1519 rc = mdt_check_resent_lock(info, child, lhc);
1522 } else if (rc > 0) {
1523 mdt_lock_handle_init(lhc);
1524 mdt_lock_reg_init(lhc, LCK_PR);
1527 * Object's name is on another MDS, no lookup or layout
1528 * lock is needed here but update lock is.
1530 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1531 MDS_INODELOCK_LAYOUT);
1532 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1534 rc = mdt_object_lock(info, child, lhc, child_bits);
1539 /* Finally, we can get attr for child. */
1540 if (!mdt_object_exists(child)) {
1541 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1543 "remote object doesn't exist.");
1544 mdt_object_unlock(info, child, lhc, 1);
1548 rc = mdt_getattr_internal(info, child, 0);
1549 if (unlikely(rc != 0))
1550 mdt_object_unlock(info, child, lhc, 1);
1555 lname = &info->mti_name;
1556 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1558 if (lu_name_is_valid(lname)) {
1559 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1560 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1561 PNAME(lname), ldlm_rep);
1563 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1564 if (unlikely(reqbody == NULL))
1565 RETURN(err_serious(-EPROTO));
1567 *child_fid = reqbody->mbo_fid2;
1569 if (unlikely(!fid_is_sane(child_fid)))
1570 RETURN(err_serious(-EINVAL));
1572 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1574 PFID(mdt_object_fid(parent)),
1575 PFID(&reqbody->mbo_fid2), ldlm_rep);
1578 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1580 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1581 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1583 "Parent doesn't exist!");
1587 if (mdt_object_remote(parent)) {
1588 CERROR("%s: parent "DFID" is on remote target\n",
1589 mdt_obd_name(info->mti_mdt),
1590 PFID(mdt_object_fid(parent)));
1594 if (lu_name_is_valid(lname)) {
1595 /* Always allow to lookup ".." */
1596 if (unlikely(lname->ln_namelen == 2 &&
1597 lname->ln_name[0] == '.' &&
1598 lname->ln_name[1] == '.'))
1599 info->mti_spec.sp_permitted = 1;
1601 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1602 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1607 /* step 1: lock parent only if parent is a directory */
1608 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1609 lhp = &info->mti_lh[MDT_LH_PARENT];
1610 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1611 rc = mdt_object_lock(info, parent, lhp,
1612 MDS_INODELOCK_UPDATE);
1613 if (unlikely(rc != 0))
1617 /* step 2: lookup child's fid by name */
1618 fid_zero(child_fid);
1619 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1620 child_fid, &info->mti_spec);
1622 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1625 GOTO(out_parent, rc);
1628 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1631 *step 3: find the child object by fid & lock it.
1632 * regardless if it is local or remote.
1634 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1635 * set parent dir fid the same as child fid in getattr by fid case
1636 * we should not lu_object_find() the object again, could lead
1637 * to hung if there is a concurrent unlink destroyed the object.
1639 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1640 mdt_object_get(info->mti_env, parent);
1643 child = mdt_object_find(info->mti_env, info->mti_mdt,
1647 if (unlikely(IS_ERR(child)))
1648 GOTO(out_parent, rc = PTR_ERR(child));
1650 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1651 if (!mdt_object_exists(child)) {
1652 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1654 "Object doesn't exist!");
1655 GOTO(out_child, rc = -ENOENT);
1658 rc = mdt_check_resent_lock(info, child, lhc);
1660 GOTO(out_child, rc);
1661 } else if (rc > 0) {
1662 mdt_lock_handle_init(lhc);
1663 mdt_lock_reg_init(lhc, LCK_PR);
1665 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1666 mdt_object_exists(child) && !mdt_object_remote(child)) {
1667 struct md_attr *ma = &info->mti_attr;
1670 ma->ma_need = MA_INODE;
1671 rc = mdt_attr_get_complex(info, child, ma);
1672 if (unlikely(rc != 0))
1673 GOTO(out_child, rc);
1675 /* If the file has not been changed for some time, we
1676 * return not only a LOOKUP lock, but also an UPDATE
1677 * lock and this might save us RPC on later STAT. For
1678 * directories, it also let negative dentry cache start
1679 * working for this dir. */
1680 if (ma->ma_valid & MA_INODE &&
1681 ma->ma_attr.la_valid & LA_CTIME &&
1682 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1683 ma->ma_attr.la_ctime < cfs_time_current_sec())
1684 child_bits |= MDS_INODELOCK_UPDATE;
1687 /* layout lock must be granted in a best-effort way
1688 * for IT operations */
1689 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1690 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1691 exp_connect_layout(info->mti_exp) &&
1692 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1693 !mdt_object_remote(child) && ldlm_rep != NULL) {
1694 /* try to grant layout lock for regular file. */
1695 try_bits = MDS_INODELOCK_LAYOUT;
1698 if (try_bits != 0) {
1699 /* try layout lock, it may fail to be granted due to
1700 * contention at LOOKUP or UPDATE */
1701 rc = mdt_object_lock_try(info, child, lhc, &child_bits,
1703 if (child_bits & MDS_INODELOCK_LAYOUT)
1706 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1707 * client will enqueue the lock to the remote MDT */
1708 if (mdt_object_remote(child))
1709 child_bits &= ~MDS_INODELOCK_UPDATE;
1710 rc = mdt_object_lock(info, child, lhc, child_bits);
1712 if (unlikely(rc != 0))
1713 GOTO(out_child, rc);
1716 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1718 /* finally, we can get attr for child. */
1719 rc = mdt_getattr_internal(info, child, ma_need);
1720 if (unlikely(rc != 0)) {
1721 mdt_object_unlock(info, child, lhc, 1);
1723 /* Debugging code. */
1724 LDLM_DEBUG(lock, "Returning lock to client");
1725 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1726 &lock->l_resource->lr_name),
1727 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1728 PLDLMRES(lock->l_resource),
1729 PFID(mdt_object_fid(child)));
1732 LDLM_LOCK_PUT(lock);
1736 mdt_object_put(info->mti_env, child);
1739 mdt_object_unlock(info, parent, lhp, 1);
1743 /* normal handler: should release the child lock */
1744 static int mdt_getattr_name(struct tgt_session_info *tsi)
1746 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1747 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1748 struct mdt_body *reqbody;
1749 struct mdt_body *repbody;
1753 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1754 LASSERT(reqbody != NULL);
1755 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1756 LASSERT(repbody != NULL);
1758 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1759 repbody->mbo_eadatasize = 0;
1760 repbody->mbo_aclsize = 0;
1762 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1764 GOTO(out_shrink, rc);
1766 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1767 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1768 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1769 lhc->mlh_reg_lh.cookie = 0;
1771 mdt_exit_ucred(info);
1774 mdt_client_compatibility(info);
1775 rc2 = mdt_fix_reply(info);
1778 mdt_thread_info_fini(info);
1782 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1783 void *karg, void __user *uarg);
1785 static int mdt_set_info(struct tgt_session_info *tsi)
1787 struct ptlrpc_request *req = tgt_ses_req(tsi);
1790 int keylen, vallen, rc = 0;
1794 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1796 DEBUG_REQ(D_HA, req, "no set_info key");
1797 RETURN(err_serious(-EFAULT));
1800 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1803 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1805 DEBUG_REQ(D_HA, req, "no set_info val");
1806 RETURN(err_serious(-EFAULT));
1809 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1812 /* Swab any part of val you need to here */
1813 if (KEY_IS(KEY_READ_ONLY)) {
1814 spin_lock(&req->rq_export->exp_lock);
1816 *exp_connect_flags_ptr(req->rq_export) |=
1819 *exp_connect_flags_ptr(req->rq_export) &=
1820 ~OBD_CONNECT_RDONLY;
1821 spin_unlock(&req->rq_export->exp_lock);
1822 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1823 struct changelog_setinfo *cs = val;
1825 if (vallen != sizeof(*cs)) {
1826 CERROR("%s: bad changelog_clear setinfo size %d\n",
1827 tgt_name(tsi->tsi_tgt), vallen);
1830 if (ptlrpc_req_need_swab(req)) {
1831 __swab64s(&cs->cs_recno);
1832 __swab32s(&cs->cs_id);
1835 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1837 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1839 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1846 static int mdt_readpage(struct tgt_session_info *tsi)
1848 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1849 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1850 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1851 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1852 struct mdt_body *repbody;
1858 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1859 RETURN(err_serious(-ENOMEM));
1861 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1862 if (repbody == NULL || reqbody == NULL)
1863 RETURN(err_serious(-EFAULT));
1866 * prepare @rdpg before calling lower layers and transfer itself. Here
1867 * reqbody->size contains offset of where to start to read and
1868 * reqbody->nlink contains number bytes to read.
1870 rdpg->rp_hash = reqbody->mbo_size;
1871 if (rdpg->rp_hash != reqbody->mbo_size) {
1872 CERROR("Invalid hash: %#llx != %#llx\n",
1873 rdpg->rp_hash, reqbody->mbo_size);
1877 rdpg->rp_attrs = reqbody->mbo_mode;
1878 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1879 rdpg->rp_attrs |= LUDA_64BITHASH;
1880 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1881 exp_max_brw_size(tsi->tsi_exp));
1882 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1884 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1885 if (rdpg->rp_pages == NULL)
1888 for (i = 0; i < rdpg->rp_npages; ++i) {
1889 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1890 if (rdpg->rp_pages[i] == NULL)
1891 GOTO(free_rdpg, rc = -ENOMEM);
1894 /* call lower layers to fill allocated pages with directory data */
1895 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1897 GOTO(free_rdpg, rc);
1899 /* send pages to client */
1900 rc = tgt_sendpage(tsi, rdpg, rc);
1905 for (i = 0; i < rdpg->rp_npages; i++)
1906 if (rdpg->rp_pages[i] != NULL)
1907 __free_page(rdpg->rp_pages[i]);
1908 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1910 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1916 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1918 struct lu_ucred *uc = mdt_ucred_check(info);
1919 struct lu_attr *attr = &info->mti_attr.ma_attr;
1924 if (op != REINT_SETATTR) {
1925 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1926 attr->la_uid = uc->uc_fsuid;
1927 /* for S_ISGID, inherit gid from his parent, such work will be
1928 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1929 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1930 attr->la_gid = uc->uc_fsgid;
1936 static int mdt_reint_internal(struct mdt_thread_info *info,
1937 struct mdt_lock_handle *lhc,
1940 struct req_capsule *pill = info->mti_pill;
1941 struct mdt_body *repbody;
1946 rc = mdt_reint_unpack(info, op);
1948 CERROR("Can't unpack reint, rc %d\n", rc);
1949 RETURN(err_serious(rc));
1952 /* for replay (no_create) lmm is not needed, client has it already */
1953 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1954 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1957 /* llog cookies are always 0, the field is kept for compatibility */
1958 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1959 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1961 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1962 * by default. If the target object has more ACL entries, then
1963 * enlarge the buffer when necessary. */
1964 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
1965 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1966 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1968 rc = req_capsule_server_pack(pill);
1970 CERROR("Can't pack response, rc %d\n", rc);
1971 RETURN(err_serious(rc));
1974 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1975 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1977 repbody->mbo_eadatasize = 0;
1978 repbody->mbo_aclsize = 0;
1981 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1983 /* for replay no cookkie / lmm need, because client have this already */
1984 if (info->mti_spec.no_create)
1985 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1986 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1988 rc = mdt_init_ucred_reint(info);
1990 GOTO(out_shrink, rc);
1992 rc = mdt_fix_attr_ucred(info, op);
1994 GOTO(out_ucred, rc = err_serious(rc));
1996 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1998 GOTO(out_ucred, rc);
1999 } else if (rc == 1) {
2000 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
2001 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2002 GOTO(out_ucred, rc);
2004 rc = mdt_reint_rec(info, lhc);
2007 mdt_exit_ucred(info);
2009 mdt_client_compatibility(info);
2010 rc2 = mdt_fix_reply(info);
2016 static long mdt_reint_opcode(struct ptlrpc_request *req,
2017 const struct req_format **fmt)
2019 struct mdt_device *mdt;
2020 struct mdt_rec_reint *rec;
2023 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2025 opc = rec->rr_opcode;
2026 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2027 if (opc < REINT_MAX && fmt[opc] != NULL)
2028 req_capsule_extend(&req->rq_pill, fmt[opc]);
2030 mdt = mdt_exp2dev(req->rq_export);
2031 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2032 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2033 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2034 opc = err_serious(-EFAULT);
2037 opc = err_serious(-EFAULT);
2042 static int mdt_reint(struct tgt_session_info *tsi)
2046 static const struct req_format *reint_fmts[REINT_MAX] = {
2047 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2048 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2049 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2050 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2051 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2052 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2053 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2054 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2055 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
2060 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2062 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2064 * No lock possible here from client to pass it to reint code
2067 rc = mdt_reint_internal(info, NULL, opc);
2068 mdt_thread_info_fini(info);
2073 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2077 /* this should sync the whole device */
2078 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2080 struct dt_device *dt = mdt->mdt_bottom;
2084 rc = dt->dd_ops->dt_sync(env, dt);
2088 /* this should sync this object */
2089 static int mdt_object_sync(const struct lu_env *env, struct obd_export *exp,
2090 struct mdt_object *mo)
2096 if (!mdt_object_exists(mo)) {
2097 CWARN("%s: non existing object "DFID": rc = %d\n",
2098 exp->exp_obd->obd_name, PFID(mdt_object_fid(mo)),
2103 rc = mo_object_sync(env, mdt_object_child(mo));
2108 static int mdt_sync(struct tgt_session_info *tsi)
2110 struct ptlrpc_request *req = tgt_ses_req(tsi);
2111 struct req_capsule *pill = tsi->tsi_pill;
2112 struct mdt_body *body;
2117 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2118 RETURN(err_serious(-ENOMEM));
2120 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2121 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2123 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2125 /* sync an object */
2126 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp,
2129 const struct lu_fid *fid;
2130 struct lu_attr *la = &info->mti_attr.ma_attr;
2132 info->mti_attr.ma_need = MA_INODE;
2133 info->mti_attr.ma_valid = 0;
2134 rc = mdt_attr_get_complex(info, info->mti_object,
2137 body = req_capsule_server_get(pill,
2139 fid = mdt_object_fid(info->mti_object);
2140 mdt_pack_attr2body(info, body, la, fid);
2143 mdt_thread_info_fini(info);
2146 mdt_counter_incr(req, LPROC_MDT_SYNC);
2151 static int mdt_data_sync(struct tgt_session_info *tsi)
2153 struct mdt_thread_info *info;
2154 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2155 struct ost_body *body = tsi->tsi_ost_body;
2156 struct ost_body *repbody;
2157 struct mdt_object *mo = NULL;
2163 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2165 /* if no fid is specified then do nothing,
2166 * device sync is done via MDS_SYNC */
2167 if (fid_is_zero(&tsi->tsi_fid))
2170 mo = mdt_object_find(tsi->tsi_env, mdt, &tsi->tsi_fid);
2172 RETURN(PTR_ERR(mo));
2174 rc = mdt_object_sync(tsi->tsi_env, tsi->tsi_exp, mo);
2178 repbody->oa.o_oi = body->oa.o_oi;
2179 repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
2181 info = tsi2mdt_info(tsi);
2182 ma = &info->mti_attr;
2183 ma->ma_need = MA_INODE;
2185 rc = mdt_attr_get_complex(info, mo, ma);
2187 obdo_from_la(&repbody->oa, &ma->ma_attr, VALID_FLAGS);
2190 mdt_thread_info_fini(info);
2195 mdt_object_put(tsi->tsi_env, mo);
2200 * Handle quota control requests to consult current usage/limit, but also
2201 * to configure quota enforcement
2203 static int mdt_quotactl(struct tgt_session_info *tsi)
2205 struct obd_export *exp = tsi->tsi_exp;
2206 struct req_capsule *pill = tsi->tsi_pill;
2207 struct obd_quotactl *oqctl, *repoqc;
2209 struct mdt_device *mdt = mdt_exp2dev(exp);
2210 struct lu_device *qmt = mdt->mdt_qmt_dev;
2211 struct lu_nodemap *nodemap;
2214 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2216 RETURN(err_serious(-EPROTO));
2218 rc = req_capsule_server_pack(pill);
2220 RETURN(err_serious(rc));
2222 nodemap = nodemap_get_from_exp(exp);
2223 if (IS_ERR(nodemap))
2224 RETURN(PTR_ERR(nodemap));
2226 switch (oqctl->qc_cmd) {
2227 /* master quotactl */
2230 if (!nodemap_can_setquota(nodemap))
2231 GOTO(out_nodemap, rc = -EPERM);
2235 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2236 /* slave quotactl */
2241 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2242 GOTO(out_nodemap, rc = -EFAULT);
2246 switch (oqctl->qc_type) {
2248 id = nodemap_map_id(nodemap, NODEMAP_UID,
2249 NODEMAP_CLIENT_TO_FS, id);
2252 id = nodemap_map_id(nodemap, NODEMAP_GID,
2253 NODEMAP_CLIENT_TO_FS, id);
2256 /* todo: check/map project id */
2260 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2262 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2264 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2266 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2267 barrier_exit(tsi->tsi_tgt->lut_bottom);
2269 if (oqctl->qc_id != id)
2270 swap(oqctl->qc_id, id);
2272 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2273 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2274 RETURN(-EINPROGRESS);
2277 switch (oqctl->qc_cmd) {
2283 /* forward quotactl request to QMT */
2284 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2289 /* slave quotactl */
2290 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2295 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2296 GOTO(out_nodemap, rc = -EFAULT);
2299 if (oqctl->qc_id != id)
2300 swap(oqctl->qc_id, id);
2307 nodemap_putref(nodemap);
2312 /** clone llog ctxt from child (mdd)
2313 * This allows remote llog (replicator) access.
2314 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2315 * context was originally set up, or we can handle them directly.
2316 * I choose the latter, but that means I need any llog
2317 * contexts set up by child to be accessable by the mdt. So we clone the
2318 * context into our context list here.
2320 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2323 struct md_device *next = mdt->mdt_child;
2324 struct llog_ctxt *ctxt;
2327 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2330 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2331 if (rc || ctxt == NULL) {
2335 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2337 CERROR("Can't set mdt ctxt %d\n", rc);
2342 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2343 struct mdt_device *mdt, int idx)
2345 struct llog_ctxt *ctxt;
2347 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2350 /* Put once for the get we just did, and once for the clone */
2351 llog_ctxt_put(ctxt);
2352 llog_ctxt_put(ctxt);
2357 * sec context handlers
2359 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2361 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2367 * quota request handlers
2369 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2371 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2372 struct lu_device *qmt = mdt->mdt_qmt_dev;
2377 RETURN(err_serious(-EOPNOTSUPP));
2379 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2383 struct mdt_object *mdt_object_new(const struct lu_env *env,
2384 struct mdt_device *d,
2385 const struct lu_fid *f)
2387 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2388 struct lu_object *o;
2389 struct mdt_object *m;
2392 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2393 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2394 if (unlikely(IS_ERR(o)))
2395 m = (struct mdt_object *)o;
2401 struct mdt_object *mdt_object_find(const struct lu_env *env,
2402 struct mdt_device *d,
2403 const struct lu_fid *f)
2405 struct lu_object *o;
2406 struct mdt_object *m;
2409 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2410 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2411 if (unlikely(IS_ERR(o)))
2412 m = (struct mdt_object *)o;
2420 * Asyncronous commit for mdt device.
2422 * Pass asynchonous commit call down the MDS stack.
2424 * \param env environment
2425 * \param mdt the mdt device
2427 static void mdt_device_commit_async(const struct lu_env *env,
2428 struct mdt_device *mdt)
2430 struct dt_device *dt = mdt->mdt_bottom;
2434 rc = dt->dd_ops->dt_commit_async(env, dt);
2435 if (unlikely(rc != 0))
2436 CWARN("%s: async commit start failed: rc = %d\n",
2437 mdt_obd_name(mdt), rc);
2438 atomic_inc(&mdt->mdt_async_commit_count);
2443 * Mark the lock as "synchonous".
2445 * Mark the lock to deffer transaction commit to the unlock time.
2447 * \param lock the lock to mark as "synchonous"
2449 * \see mdt_is_lock_sync
2450 * \see mdt_save_lock
2452 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2454 lock->l_ast_data = (void*)1;
2458 * Check whehter the lock "synchonous" or not.
2460 * \param lock the lock to check
2461 * \retval 1 the lock is "synchonous"
2462 * \retval 0 the lock isn't "synchronous"
2464 * \see mdt_set_lock_sync
2465 * \see mdt_save_lock
2467 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2469 return lock->l_ast_data != NULL;
2473 * Blocking AST for mdt locks.
2475 * Starts transaction commit if in case of COS lock conflict or
2476 * deffers such a commit to the mdt_save_lock.
2478 * \param lock the lock which blocks a request or cancelling lock
2479 * \param desc unused
2480 * \param data unused
2481 * \param flag indicates whether this cancelling or blocking callback
2483 * \see ldlm_blocking_ast_nocheck
2485 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2486 void *data, int flag)
2488 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2489 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2490 bool commit_async = false;
2494 if (flag == LDLM_CB_CANCELING)
2497 lock_res_and_lock(lock);
2498 if (lock->l_blocking_ast != mdt_blocking_ast) {
2499 unlock_res_and_lock(lock);
2502 /* There is no lock conflict if l_blocking_lock == NULL,
2503 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2504 * when the last reference to a local lock was released */
2505 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2506 lock->l_blocking_lock != NULL) {
2507 if (mdt_cos_is_enabled(mdt)) {
2508 if (lock->l_client_cookie !=
2509 lock->l_blocking_lock->l_client_cookie)
2510 mdt_set_lock_sync(lock);
2511 } else if (mdt_slc_is_enabled(mdt) &&
2512 ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2513 mdt_set_lock_sync(lock);
2515 * we may do extra commit here, but there is a small
2516 * window to miss a commit: lock was unlocked (saved),
2517 * then a conflict lock queued and we come here, but
2518 * REP-ACK not received, so lock was not converted to
2520 * Fortunately this window is quite small, so the
2521 * extra commit should be rare (not to say distributed
2522 * operation is rare too).
2524 commit_async = true;
2526 } else if (lock->l_req_mode == LCK_COS &&
2527 lock->l_blocking_lock != NULL) {
2528 commit_async = true;
2531 rc = ldlm_blocking_ast_nocheck(lock);
2536 rc = lu_env_init(&env, LCT_LOCAL);
2537 if (unlikely(rc != 0))
2538 CWARN("%s: lu_env initialization failed, cannot "
2539 "start asynchronous commit: rc = %d\n",
2542 mdt_device_commit_async(&env, mdt);
2549 * Blocking AST for cross-MDT lock
2551 * Discard lock from uncommitted_slc_locks and cancel it.
2553 * \param lock the lock which blocks a request or cancelling lock
2554 * \param desc unused
2555 * \param data unused
2556 * \param flag indicates whether this cancelling or blocking callback
2557 * \retval 0 on success
2558 * \retval negative number on error
2560 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2561 void *data, int flag)
2567 case LDLM_CB_BLOCKING: {
2568 struct lustre_handle lockh;
2570 ldlm_lock2handle(lock, &lockh);
2571 rc = ldlm_cli_cancel(&lockh,
2572 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2574 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2579 case LDLM_CB_CANCELING: {
2580 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2581 struct mdt_device *mdt =
2582 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2584 LDLM_DEBUG(lock, "Revoke remote lock\n");
2586 /* discard slc lock here so that it can be cleaned anytime,
2587 * especially for cleanup_resource() */
2588 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2590 /* once we cache lock, l_ast_data is set to mdt_object */
2591 if (lock->l_ast_data != NULL) {
2592 struct mdt_object *mo = lock->l_ast_data;
2595 rc = lu_env_init(&env, LCT_MD_THREAD);
2596 if (unlikely(rc != 0)) {
2597 CWARN("%s: lu_env initialization failed, object"
2598 "%p "DFID" is leaked!\n",
2600 PFID(mdt_object_fid(mo)));
2604 if (lock->l_policy_data.l_inodebits.bits &
2605 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2606 rc = mo_invalidate(&env, mdt_object_child(mo));
2607 mo->mot_cache_attr = 0;
2609 mdt_object_put(&env, mo);
2621 int mdt_check_resent_lock(struct mdt_thread_info *info,
2622 struct mdt_object *mo,
2623 struct mdt_lock_handle *lhc)
2625 /* the lock might already be gotten in ldlm_handle_enqueue() */
2626 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2627 struct ptlrpc_request *req = mdt_info_req(info);
2628 struct ldlm_lock *lock;
2630 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2631 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2633 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2634 * a resend case, however, it could be already destroyed
2635 * due to client eviction or a raced cancel RPC. */
2636 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2637 lhc->mlh_reg_lh.cookie);
2641 if (!fid_res_name_eq(mdt_object_fid(mo),
2642 &lock->l_resource->lr_name)) {
2643 CWARN("%s: Although resent, but still not "
2644 "get child lock:"DFID"\n",
2645 info->mti_exp->exp_obd->obd_name,
2646 PFID(mdt_object_fid(mo)));
2647 LDLM_LOCK_PUT(lock);
2650 LDLM_LOCK_PUT(lock);
2656 int mdt_remote_object_lock_try(struct mdt_thread_info *mti,
2657 struct mdt_object *o, const struct lu_fid *fid,
2658 struct lustre_handle *lh, enum ldlm_mode mode,
2659 __u64 *ibits, __u64 trybits, bool cache)
2661 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2662 union ldlm_policy_data *policy = &mti->mti_policy;
2663 struct ldlm_res_id *res_id = &mti->mti_res_id;
2667 LASSERT(mdt_object_remote(o));
2669 fid_build_reg_res_name(fid, res_id);
2671 memset(einfo, 0, sizeof(*einfo));
2672 einfo->ei_type = LDLM_IBITS;
2673 einfo->ei_mode = mode;
2674 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2675 einfo->ei_cb_cp = ldlm_completion_ast;
2676 einfo->ei_enq_slave = 0;
2677 einfo->ei_res_id = res_id;
2681 * if we cache lock, couple lock with mdt_object, so that object
2682 * can be easily found in lock ASTs.
2684 mdt_object_get(mti->mti_env, o);
2685 einfo->ei_cbdata = o;
2689 memset(policy, 0, sizeof(*policy));
2690 policy->l_inodebits.bits = *ibits;
2691 policy->l_inodebits.try_bits = trybits;
2693 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2695 if (rc < 0 && cache) {
2696 mdt_object_put(mti->mti_env, o);
2697 einfo->ei_cbdata = NULL;
2700 /* Return successfully acquired bits to a caller */
2702 struct ldlm_lock *lock = ldlm_handle2lock(lh);
2705 *ibits = lock->l_policy_data.l_inodebits.bits;
2706 LDLM_LOCK_PUT(lock);
2711 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2712 const struct lu_fid *fid, struct lustre_handle *lh,
2713 enum ldlm_mode mode, __u64 ibits, bool cache)
2715 return mdt_remote_object_lock_try(mti, o, fid, lh, mode, &ibits, 0,
2719 static int mdt_object_local_lock(struct mdt_thread_info *info,
2720 struct mdt_object *o,
2721 struct mdt_lock_handle *lh, __u64 *ibits,
2722 __u64 trybits, bool cos_incompat)
2724 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2725 union ldlm_policy_data *policy = &info->mti_policy;
2726 struct ldlm_res_id *res_id = &info->mti_res_id;
2731 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2732 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2733 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2734 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2737 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2738 lh->mlh_reg_mode == LCK_EX);
2739 dlmflags |= LDLM_FL_COS_INCOMPAT;
2740 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2741 dlmflags |= LDLM_FL_COS_ENABLED;
2744 /* Only enqueue LOOKUP lock for remote object */
2745 if (mdt_object_remote(o)) {
2746 LASSERT(*ibits == MDS_INODELOCK_LOOKUP);
2749 if (lh->mlh_type == MDT_PDO_LOCK) {
2750 /* check for exists after object is locked */
2751 if (mdt_object_exists(o) == 0) {
2752 /* Non-existent object shouldn't have PDO lock */
2755 /* Non-dir object shouldn't have PDO lock */
2756 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2762 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2763 dlmflags |= LDLM_FL_ATOMIC_CB;
2766 * Take PDO lock on whole directory and build correct @res_id for lock
2767 * on part of directory.
2769 if (lh->mlh_pdo_hash != 0) {
2770 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2771 mdt_lock_pdo_mode(info, o, lh);
2772 if (lh->mlh_pdo_mode != LCK_NL) {
2774 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2775 * is never going to be sent to client and we do not
2776 * want it slowed down due to possible cancels.
2778 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2779 policy->l_inodebits.try_bits = 0;
2780 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2781 policy, res_id, dlmflags,
2782 info->mti_exp == NULL ? NULL :
2783 &info->mti_exp->exp_handle.h_cookie);
2784 if (unlikely(rc != 0))
2785 GOTO(out_unlock, rc);
2789 * Finish res_id initializing by name hash marking part of
2790 * directory which is taking modification.
2792 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2795 policy->l_inodebits.bits = *ibits;
2796 policy->l_inodebits.try_bits = trybits;
2799 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2800 * going to be sent to client. If it is - mdt_intent_policy() path will
2801 * fix it up and turn FL_LOCAL flag off.
2803 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2804 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2805 info->mti_exp == NULL ? NULL :
2806 &info->mti_exp->exp_handle.h_cookie);
2809 mdt_object_unlock(info, o, lh, 1);
2810 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2811 lh->mlh_pdo_hash != 0 &&
2812 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2813 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2815 /* Return successfully acquired bits to a caller */
2817 struct ldlm_lock *lock = ldlm_handle2lock(&lh->mlh_reg_lh);
2820 *ibits = lock->l_policy_data.l_inodebits.bits;
2821 LDLM_LOCK_PUT(lock);
2827 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2828 struct mdt_lock_handle *lh, __u64 *ibits,
2829 __u64 trybits, bool cos_incompat)
2831 struct mdt_lock_handle *local_lh = NULL;
2835 if (!mdt_object_remote(o)) {
2836 rc = mdt_object_local_lock(info, o, lh, ibits, trybits,
2841 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2842 *ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2843 MDS_INODELOCK_XATTR);
2845 /* Only enqueue LOOKUP lock for remote object */
2846 if (*ibits & MDS_INODELOCK_LOOKUP) {
2847 __u64 local = MDS_INODELOCK_LOOKUP;
2849 rc = mdt_object_local_lock(info, o, lh, &local, 0,
2857 if ((*ibits | trybits) & MDS_INODELOCK_UPDATE) {
2858 /* Sigh, PDO needs to enqueue 2 locks right now, but
2859 * enqueue RPC can only request 1 lock, to avoid extra
2860 * RPC, so it will instead enqueue EX lock for remote
2861 * object anyway XXX*/
2862 if (lh->mlh_type == MDT_PDO_LOCK &&
2863 lh->mlh_pdo_hash != 0) {
2864 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2865 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2866 PFID(mdt_object_fid(o)));
2867 lh->mlh_pdo_hash = 0;
2868 lh->mlh_rreg_mode = LCK_EX;
2869 lh->mlh_type = MDT_REG_LOCK;
2872 rc = mdt_remote_object_lock_try(info, o, mdt_object_fid(o),
2875 ibits, trybits, false);
2876 if (rc != ELDLM_OK) {
2877 if (local_lh != NULL)
2878 mdt_object_unlock(info, o, local_lh, rc);
2886 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2887 struct mdt_lock_handle *lh, __u64 ibits)
2889 return mdt_object_lock_internal(info, o, lh, &ibits, 0, false);
2892 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2893 struct mdt_lock_handle *lh, __u64 ibits,
2896 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2897 return mdt_object_lock_internal(info, o, lh, &ibits, 0,
2901 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2902 struct mdt_lock_handle *lh, __u64 *ibits,
2903 __u64 trybits, bool cos_incompat)
2905 return mdt_object_lock_internal(info, o, lh, ibits, trybits,
2910 * Save a lock within request object.
2912 * Keep the lock referenced until whether client ACK or transaction
2913 * commit happens or release the lock immediately depending on input
2914 * parameters. If COS is ON, a write lock is converted to COS lock
2917 * \param info thead info object
2918 * \param h lock handle
2919 * \param mode lock mode
2920 * \param decref force immediate lock releasing
2922 void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2923 enum ldlm_mode mode, int decref)
2927 if (lustre_handle_is_used(h)) {
2928 if (decref || !info->mti_has_trans ||
2929 !(mode & (LCK_PW | LCK_EX))) {
2930 mdt_fid_unlock(h, mode);
2932 struct mdt_device *mdt = info->mti_mdt;
2933 struct ldlm_lock *lock = ldlm_handle2lock(h);
2934 struct ptlrpc_request *req = mdt_info_req(info);
2935 bool cos = mdt_cos_is_enabled(mdt);
2936 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
2938 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2941 /* there is no request if mdt_object_unlock() is called
2942 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2943 if (likely(req != NULL)) {
2944 LDLM_DEBUG(lock, "save lock request %p reply "
2945 "state %p transno %lld\n", req,
2946 req->rq_reply_state, req->rq_transno);
2948 ldlm_lock_downgrade(lock, LCK_COS);
2951 if (req->rq_export->exp_disconnected)
2952 mdt_fid_unlock(h, mode);
2954 ptlrpc_save_lock(req, h, mode, cos,
2957 mdt_fid_unlock(h, mode);
2959 if (mdt_is_lock_sync(lock)) {
2960 CDEBUG(D_HA, "found sync-lock,"
2961 " async commit started\n");
2962 mdt_device_commit_async(info->mti_env,
2965 LDLM_LOCK_PUT(lock);
2974 * Save cross-MDT lock in uncommitted_slc_locks
2976 * Keep the lock referenced until transaction commit happens or release the lock
2977 * immediately depending on input parameters.
2979 * \param info thead info object
2980 * \param h lock handle
2981 * \param mode lock mode
2982 * \param decref force immediate lock releasing
2984 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2985 struct mdt_object *o, struct lustre_handle *h,
2986 enum ldlm_mode mode, int decref)
2990 if (lustre_handle_is_used(h)) {
2991 struct ldlm_lock *lock = ldlm_handle2lock(h);
2994 (lock->l_policy_data.l_inodebits.bits &
2995 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2996 mo_invalidate(info->mti_env, mdt_object_child(o));
2998 if (decref || !info->mti_has_trans ||
2999 !(mode & (LCK_PW | LCK_EX))) {
3000 ldlm_lock_decref_and_cancel(h, mode);
3001 LDLM_LOCK_PUT(lock);
3003 struct ptlrpc_request *req = mdt_info_req(info);
3005 LASSERT(req != NULL);
3006 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
3008 ldlm_lock_decref(h, mode);
3017 * Unlock mdt object.
3019 * Immeditely release the regular lock and the PDO lock or save the
3020 * lock in request and keep them referenced until client ACK or
3021 * transaction commit.
3023 * \param info thread info object
3024 * \param o mdt object
3025 * \param lh mdt lock handle referencing regular and PDO locks
3026 * \param decref force immediate lock releasing
3028 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
3030 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
3031 struct mdt_lock_handle *lh, int decref)
3035 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
3036 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
3037 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
3043 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
3044 const struct lu_fid *f,
3045 struct mdt_lock_handle *lh,
3048 struct mdt_object *o;
3050 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
3054 rc = mdt_object_lock(info, o, lh, ibits);
3056 mdt_object_put(info->mti_env, o);
3063 void mdt_object_unlock_put(struct mdt_thread_info * info,
3064 struct mdt_object * o,
3065 struct mdt_lock_handle *lh,
3068 mdt_object_unlock(info, o, lh, decref);
3069 mdt_object_put(info->mti_env, o);
3073 * Generic code handling requests that have struct mdt_body passed in:
3075 * - extract mdt_body from request and save it in @info, if present;
3077 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3080 * - if HABEO_CORPUS flag is set for this request type check whether object
3081 * actually exists on storage (lu_object_exists()).
3084 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3086 const struct mdt_body *body;
3087 struct mdt_object *obj;
3088 const struct lu_env *env;
3089 struct req_capsule *pill;
3093 env = info->mti_env;
3094 pill = info->mti_pill;
3096 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3100 if (!(body->mbo_valid & OBD_MD_FLID))
3103 if (!fid_is_sane(&body->mbo_fid1)) {
3104 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3108 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3110 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3111 mdt_object_put(env, obj);
3114 info->mti_object = obj;
3123 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3125 struct req_capsule *pill = info->mti_pill;
3129 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3130 rc = mdt_body_unpack(info, flags);
3134 if (rc == 0 && (flags & HABEO_REFERO)) {
3136 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3137 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3139 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3140 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3143 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3144 * by default. If the target object has more ACL entries, then
3145 * enlarge the buffer when necessary. */
3146 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3147 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3148 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3150 rc = req_capsule_server_pack(pill);
3155 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3157 lh->mlh_type = MDT_NUL_LOCK;
3158 lh->mlh_reg_lh.cookie = 0ull;
3159 lh->mlh_reg_mode = LCK_MINMODE;
3160 lh->mlh_pdo_lh.cookie = 0ull;
3161 lh->mlh_pdo_mode = LCK_MINMODE;
3162 lh->mlh_rreg_lh.cookie = 0ull;
3163 lh->mlh_rreg_mode = LCK_MINMODE;
3166 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3168 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3169 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3173 * Initialize fields of struct mdt_thread_info. Other fields are left in
3174 * uninitialized state, because it's too expensive to zero out whole
3175 * mdt_thread_info (> 1K) on each request arrival.
3177 void mdt_thread_info_init(struct ptlrpc_request *req,
3178 struct mdt_thread_info *info)
3182 info->mti_pill = &req->rq_pill;
3185 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3186 mdt_lock_handle_init(&info->mti_lh[i]);
3188 /* mdt device: it can be NULL while CONNECT */
3189 if (req->rq_export) {
3190 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3191 info->mti_exp = req->rq_export;
3193 info->mti_mdt = NULL;
3194 info->mti_env = req->rq_svc_thread->t_env;
3195 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3197 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3198 info->mti_big_buf = LU_BUF_NULL;
3199 info->mti_body = NULL;
3200 info->mti_object = NULL;
3201 info->mti_dlm_req = NULL;
3202 info->mti_has_trans = 0;
3203 info->mti_cross_ref = 0;
3204 info->mti_opdata = 0;
3205 info->mti_big_lmm_used = 0;
3206 info->mti_big_acl_used = 0;
3208 info->mti_spec.no_create = 0;
3209 info->mti_spec.sp_rm_entry = 0;
3210 info->mti_spec.sp_permitted = 0;
3211 info->mti_spec.sp_migrate_close = 0;
3213 info->mti_spec.u.sp_ea.eadata = NULL;
3214 info->mti_spec.u.sp_ea.eadatalen = 0;
3217 void mdt_thread_info_fini(struct mdt_thread_info *info)
3221 if (info->mti_object != NULL) {
3222 mdt_object_put(info->mti_env, info->mti_object);
3223 info->mti_object = NULL;
3226 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3227 mdt_lock_handle_fini(&info->mti_lh[i]);
3228 info->mti_env = NULL;
3229 info->mti_pill = NULL;
3230 info->mti_exp = NULL;
3232 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3233 lu_buf_free(&info->mti_big_buf);
3236 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3238 struct mdt_thread_info *mti;
3240 mti = mdt_th_info(tsi->tsi_env);
3241 LASSERT(mti != NULL);
3243 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3244 if (tsi->tsi_corpus != NULL) {
3245 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3246 lu_object_get(tsi->tsi_corpus);
3248 mti->mti_body = tsi->tsi_mdt_body;
3249 mti->mti_dlm_req = tsi->tsi_dlm_req;
3254 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3256 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3258 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3259 set_current_state(TASK_UNINTERRUPTIBLE);
3260 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3263 return tgt_connect(tsi);
3281 static int mdt_intent_getattr(enum mdt_it_code opcode,
3282 struct mdt_thread_info *info,
3283 struct ldlm_lock **,
3286 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3287 struct mdt_thread_info *info,
3288 struct ldlm_lock **lockp,
3291 static int mdt_intent_layout(enum mdt_it_code opcode,
3292 struct mdt_thread_info *info,
3293 struct ldlm_lock **,
3295 static int mdt_intent_reint(enum mdt_it_code opcode,
3296 struct mdt_thread_info *info,
3297 struct ldlm_lock **,
3300 static struct mdt_it_flavor {
3301 const struct req_format *it_fmt;
3303 int (*it_act)(enum mdt_it_code ,
3304 struct mdt_thread_info *,
3305 struct ldlm_lock **,
3308 } mdt_it_flavor[] = {
3310 .it_fmt = &RQF_LDLM_INTENT,
3311 /*.it_flags = HABEO_REFERO,*/
3313 .it_act = mdt_intent_reint,
3314 .it_reint = REINT_OPEN
3317 .it_fmt = &RQF_LDLM_INTENT,
3319 * OCREAT is not a MUTABOR request as if the file
3321 * We do the extra check of OBD_CONNECT_RDONLY in
3322 * mdt_reint_open() when we really need to create
3326 .it_act = mdt_intent_reint,
3327 .it_reint = REINT_OPEN
3330 .it_fmt = &RQF_LDLM_INTENT,
3331 .it_flags = MUTABOR,
3332 .it_act = mdt_intent_reint,
3333 .it_reint = REINT_CREATE
3335 [MDT_IT_GETATTR] = {
3336 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3337 .it_flags = HABEO_REFERO,
3338 .it_act = mdt_intent_getattr
3340 [MDT_IT_READDIR] = {
3346 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3347 .it_flags = HABEO_REFERO,
3348 .it_act = mdt_intent_getattr
3351 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3352 .it_flags = MUTABOR,
3354 .it_reint = REINT_UNLINK
3358 .it_flags = MUTABOR,
3361 [MDT_IT_GETXATTR] = {
3362 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3363 .it_flags = HABEO_CORPUS,
3364 .it_act = mdt_intent_getxattr
3367 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3369 .it_act = mdt_intent_layout
3374 mdt_intent_lock_replace(struct mdt_thread_info *info,
3375 struct ldlm_lock **lockp,
3376 struct mdt_lock_handle *lh,
3377 __u64 flags, int result)
3379 struct ptlrpc_request *req = mdt_info_req(info);
3380 struct ldlm_lock *lock = *lockp;
3381 struct ldlm_lock *new_lock;
3383 /* If possible resent found a lock, @lh is set to its handle */
3384 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3386 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3387 lh->mlh_reg_lh.cookie = 0;
3391 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3392 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3393 * a resend case, however, it could be already destroyed
3394 * due to client eviction or a raced cancel RPC. */
3395 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3396 lh->mlh_reg_lh.cookie);
3397 lh->mlh_reg_lh.cookie = 0;
3401 LASSERTF(new_lock != NULL,
3402 "lockh %#llx flags %#llx : rc = %d\n",
3403 lh->mlh_reg_lh.cookie, flags, result);
3406 * If we've already given this lock to a client once, then we should
3407 * have no readers or writers. Otherwise, we should have one reader
3408 * _or_ writer ref (which will be zeroed below) before returning the
3411 if (new_lock->l_export == req->rq_export) {
3412 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3414 LASSERT(new_lock->l_export == NULL);
3415 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3420 if (new_lock->l_export == req->rq_export) {
3422 * Already gave this to the client, which means that we
3423 * reconstructed a reply.
3425 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3428 LDLM_LOCK_RELEASE(new_lock);
3429 lh->mlh_reg_lh.cookie = 0;
3430 RETURN(ELDLM_LOCK_REPLACED);
3434 * Fixup the lock to be given to the client.
3436 lock_res_and_lock(new_lock);
3437 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3438 * possible blocking AST. */
3439 while (new_lock->l_readers > 0) {
3440 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3441 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3442 new_lock->l_readers--;
3444 while (new_lock->l_writers > 0) {
3445 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3446 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3447 new_lock->l_writers--;
3450 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3451 new_lock->l_blocking_ast = lock->l_blocking_ast;
3452 new_lock->l_completion_ast = lock->l_completion_ast;
3453 new_lock->l_remote_handle = lock->l_remote_handle;
3454 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3456 unlock_res_and_lock(new_lock);
3458 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3459 &new_lock->l_remote_handle,
3460 &new_lock->l_exp_hash);
3462 LDLM_LOCK_RELEASE(new_lock);
3463 lh->mlh_reg_lh.cookie = 0;
3465 RETURN(ELDLM_LOCK_REPLACED);
3468 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3469 struct ldlm_lock *new_lock,
3470 struct mdt_lock_handle *lh,
3473 struct ptlrpc_request *req = mdt_info_req(info);
3474 struct ldlm_request *dlmreq;
3476 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3479 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3481 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3482 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3484 if (flags & LDLM_FL_RESENT) {
3485 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3486 lh->mlh_reg_mode = new_lock->l_granted_mode;
3488 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3489 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3490 lh->mlh_reg_lh.cookie);
3495 * If the xid matches, then we know this is a resent request, and allow
3496 * it. (It's probably an OPEN, for which we don't send a lock.
3498 if (req_can_reconstruct(req, NULL))
3502 * This remote handle isn't enqueued, so we never received or processed
3503 * this request. Clear MSG_RESENT, because it can be handled like any
3504 * normal request now.
3506 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3508 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3509 dlmreq->lock_handle[0].cookie);
3512 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3513 struct mdt_thread_info *info,
3514 struct ldlm_lock **lockp,
3517 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3518 struct ldlm_reply *ldlm_rep = NULL;
3523 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3524 * (for the resend case) or a new lock. Below we will use it to
3525 * replace the original lock.
3527 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3528 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3529 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3530 rc = mdt_object_lock(info, info->mti_object, lhc,
3531 MDS_INODELOCK_XATTR);
3536 rc = mdt_getxattr(info);
3538 if (mdt_info_req(info)->rq_repmsg != NULL)
3539 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3541 if (ldlm_rep == NULL ||
3542 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3543 mdt_object_unlock(info, info->mti_object, lhc, 1);
3544 RETURN(err_serious(-EFAULT));
3547 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3549 /* This is left for interop instead of adding a new interop flag.
3551 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3552 if (ldlm_rep->lock_policy_res2) {
3553 mdt_object_unlock(info, info->mti_object, lhc, 1);
3554 RETURN(ELDLM_LOCK_ABORTED);
3558 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3562 static int mdt_intent_getattr(enum mdt_it_code opcode,
3563 struct mdt_thread_info *info,
3564 struct ldlm_lock **lockp,
3567 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3569 struct ldlm_reply *ldlm_rep;
3570 struct mdt_body *reqbody;
3571 struct mdt_body *repbody;
3575 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3578 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3581 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3582 repbody->mbo_eadatasize = 0;
3583 repbody->mbo_aclsize = 0;
3587 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3589 case MDT_IT_GETATTR:
3590 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3594 CERROR("Unsupported intent (%d)\n", opcode);
3595 GOTO(out_shrink, rc = -EINVAL);
3598 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3600 GOTO(out_shrink, rc);
3602 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3603 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3605 /* Get lock from request for possible resent case. */
3606 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3608 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3609 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3611 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3612 ldlm_rep->lock_policy_res2 = 0;
3613 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3614 ldlm_rep->lock_policy_res2) {
3615 lhc->mlh_reg_lh.cookie = 0ull;
3616 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3619 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3622 mdt_exit_ucred(info);
3624 mdt_client_compatibility(info);
3625 rc2 = mdt_fix_reply(info);
3631 static int mdt_intent_layout(enum mdt_it_code opcode,
3632 struct mdt_thread_info *info,
3633 struct ldlm_lock **lockp,
3636 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3637 struct layout_intent *layout;
3639 struct mdt_object *obj = NULL;
3640 bool layout_change = false;
3641 int layout_size = 0;
3645 if (opcode != MDT_IT_LAYOUT) {
3646 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3651 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3655 switch (layout->li_opc) {
3656 case LAYOUT_INTENT_TRUNC:
3657 case LAYOUT_INTENT_WRITE:
3658 layout_change = true;
3660 case LAYOUT_INTENT_ACCESS:
3662 case LAYOUT_INTENT_READ:
3663 case LAYOUT_INTENT_GLIMPSE:
3664 case LAYOUT_INTENT_RELEASE:
3665 case LAYOUT_INTENT_RESTORE:
3666 CERROR("%s: Unsupported layout intent opc %d\n",
3667 mdt_obd_name(info->mti_mdt), layout->li_opc);
3671 CERROR("%s: Unknown layout intent opc %d\n",
3672 mdt_obd_name(info->mti_mdt), layout->li_opc);
3679 fid = &info->mti_tmp_fid2;
3680 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3682 /* Get lock from request for possible resent case. */
3683 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3685 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3687 GOTO(out, rc = PTR_ERR(obj));
3689 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3690 layout_size = mdt_attr_get_eabuf_size(info, obj);
3691 if (layout_size < 0)
3692 GOTO(out_obj, rc = layout_size);
3694 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3695 info->mti_mdt->mdt_max_mdsize = layout_size;
3699 * set reply buffer size, so that ldlm_handle_enqueue0()->
3700 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3702 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3703 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3705 rc = req_capsule_server_pack(info->mti_pill);
3710 if (layout_change) {
3711 struct lu_buf *buf = &info->mti_buf;
3714 * mdt_layout_change is a reint operation, when the request
3715 * is resent, layout write shouldn't reprocess it again.
3717 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
3719 GOTO(out_obj, rc = rc < 0 ? rc : 0);
3722 * There is another resent case: the client's job has been
3723 * done by another client, referring lod_declare_layout_change
3724 * -EALREADY case, and it became a operation w/o transaction,
3725 * so we should not do the layout change, otherwise
3726 * mdt_layout_change() will try to cancel the granted server
3727 * CR lock whose remote counterpart is still in hold on the
3728 * client, and a deadlock ensues.
3730 rc = mdt_check_resent_lock(info, obj, lhc);
3736 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3737 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3739 buf->lb_len = req_capsule_get_size(info->mti_pill,
3740 &RMF_EADATA, RCL_CLIENT);
3742 * If it's a replay of layout write intent RPC, the
3743 * client has saved the extended lovea when
3744 * it get reply then.
3746 if (buf->lb_len > 0)
3747 mdt_fix_lov_magic(info, buf->lb_buf);
3751 * Instantiate some layout components, if @buf contains
3752 * lovea, then it's a replay of the layout intent write
3755 rc = mdt_layout_change(info, obj, layout, buf);
3760 mdt_object_put(info->mti_env, obj);
3762 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3763 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3766 lhc->mlh_reg_lh.cookie = 0;
3771 static int mdt_intent_reint(enum mdt_it_code opcode,
3772 struct mdt_thread_info *info,
3773 struct ldlm_lock **lockp,
3776 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3777 struct ldlm_reply *rep = NULL;
3781 static const struct req_format *intent_fmts[REINT_MAX] = {
3782 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3783 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3788 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3792 if (mdt_it_flavor[opcode].it_reint != opc) {
3793 CERROR("Reint code %ld doesn't match intent: %d\n",
3795 RETURN(err_serious(-EPROTO));
3798 /* Get lock from request for possible resent case. */
3799 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3801 rc = mdt_reint_internal(info, lhc, opc);
3803 /* Check whether the reply has been packed successfully. */
3804 if (mdt_info_req(info)->rq_repmsg != NULL)
3805 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3807 RETURN(err_serious(-EFAULT));
3809 /* MDC expects this in any case */
3811 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3813 /* the open lock or the lock for cross-ref object should be
3814 * returned to the client */
3815 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3816 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3817 rep->lock_policy_res2 = 0;
3818 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3822 rep->lock_policy_res2 = clear_serious(rc);
3824 if (rep->lock_policy_res2 == -ENOENT &&
3825 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3826 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3827 rep->lock_policy_res2 = 0;
3829 lhc->mlh_reg_lh.cookie = 0ull;
3830 if (rc == -ENOTCONN || rc == -ENODEV ||
3831 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3833 * If it is the disconnect error (ENODEV & ENOCONN), the error
3834 * will be returned by rq_status, and client at ptlrpc layer
3835 * will detect this, then disconnect, reconnect the import
3836 * immediately, instead of impacting the following the rpc.
3841 * For other cases, the error will be returned by intent, and client
3842 * will retrieve the result from intent.
3844 RETURN(ELDLM_LOCK_ABORTED);
3847 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3855 case IT_OPEN|IT_CREAT:
3862 rc = MDT_IT_READDIR;
3865 rc = MDT_IT_GETATTR;
3877 rc = MDT_IT_GETXATTR;
3882 case IT_QUOTA_DQACQ:
3887 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3894 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3895 struct mdt_thread_info *info,
3896 struct ldlm_lock **lockp, __u64 flags)
3898 struct req_capsule *pill = info->mti_pill;
3899 struct ptlrpc_request *req = mdt_info_req(info);
3900 struct mdt_it_flavor *flv;
3905 opc = mdt_intent_code(itopc);
3909 if (opc == MDT_IT_QUOTA) {
3910 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3913 RETURN(-EOPNOTSUPP);
3915 if (mdt_rdonly(req->rq_export))
3918 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3919 /* pass the request to quota master */
3920 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3921 mdt_info_req(info), lockp,
3926 flv = &mdt_it_flavor[opc];
3927 if (flv->it_fmt != NULL)
3928 req_capsule_extend(pill, flv->it_fmt);
3930 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3934 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3937 if (flv->it_act != NULL) {
3938 struct ldlm_reply *rep;
3940 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3942 /* execute policy */
3943 rc = flv->it_act(opc, info, lockp, flags);
3945 /* Check whether the reply has been packed successfully. */
3946 if (req->rq_repmsg != NULL) {
3947 rep = req_capsule_server_get(info->mti_pill,
3949 rep->lock_policy_res2 =
3950 ptlrpc_status_hton(rep->lock_policy_res2);
3957 static int mdt_intent_policy(struct ldlm_namespace *ns,
3958 struct ldlm_lock **lockp, void *req_cookie,
3959 enum ldlm_mode mode, __u64 flags, void *data)
3961 struct tgt_session_info *tsi;
3962 struct mdt_thread_info *info;
3963 struct ptlrpc_request *req = req_cookie;
3964 struct ldlm_intent *it;
3965 struct req_capsule *pill;
3970 LASSERT(req != NULL);
3972 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3974 info = tsi2mdt_info(tsi);
3975 LASSERT(info != NULL);
3976 pill = info->mti_pill;
3977 LASSERT(pill->rc_req == req);
3979 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3980 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3981 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3983 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3987 /* Lock without inodebits makes no sense and will oops
3988 * later in ldlm. Let's check it now to see if we have
3989 * ibits corrupted somewhere in mdt_intent_opc().
3990 * The case for client miss to set ibits has been
3991 * processed by others. */
3992 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3993 lr_type == LDLM_IBITS,
3994 info->mti_dlm_req->lock_desc.\
3995 l_policy_data.l_inodebits.bits != 0));
3997 rc = err_serious(-EFAULT);
3999 /* No intent was provided */
4000 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
4001 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
4002 rc = req_capsule_server_pack(pill);
4004 rc = err_serious(rc);
4006 mdt_thread_info_fini(info);
4010 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
4012 struct seq_server_site *ss = mdt_seq_site(mdt);
4014 if (ss->ss_node_id == 0)
4017 if (ss->ss_client_seq != NULL) {
4018 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4019 ss->ss_client_seq->lcs_exp = NULL;
4022 if (ss->ss_server_fld != NULL) {
4023 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
4024 ss->ss_server_fld->lsf_control_exp = NULL;
4028 static void mdt_seq_fini_cli(struct mdt_device *mdt)
4030 struct seq_server_site *ss = mdt_seq_site(mdt);
4035 if (ss->ss_server_seq != NULL)
4036 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
4039 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
4041 mdt_seq_fini_cli(mdt);
4042 mdt_deregister_seq_exp(mdt);
4044 return seq_site_fini(env, mdt_seq_site(mdt));
4048 * It will retrieve its FLDB entries from MDT0, and it only happens
4049 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
4050 * and it needs to refresh FLDB from the MDT0.
4052 static int mdt_register_lwp_callback(void *data)
4055 struct mdt_device *mdt = data;
4056 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4060 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4062 rc = lu_env_init(&env, LCT_MD_THREAD);
4064 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4068 /* Allocate new sequence now to avoid creating local transaction
4069 * in the normal transaction process */
4070 rc = seq_server_check_and_alloc_super(&env,
4071 mdt_seq_site(mdt)->ss_server_seq);
4076 rc = fld_update_from_controller(&env, fld);
4078 CERROR("%s: cannot update controller: rc = %d\n",
4079 mdt_obd_name(mdt), rc);
4088 static int mdt_register_seq_exp(struct mdt_device *mdt)
4090 struct seq_server_site *ss = mdt_seq_site(mdt);
4091 char *lwp_name = NULL;
4094 if (ss->ss_node_id == 0)
4097 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4098 if (lwp_name == NULL)
4099 GOTO(out_free, rc = -ENOMEM);
4101 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4105 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4110 rc = lustre_register_lwp_item(lwp_name,
4111 &ss->ss_server_fld->lsf_control_exp,
4112 mdt_register_lwp_callback, mdt);
4114 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4115 ss->ss_client_seq->lcs_exp = NULL;
4119 if (lwp_name != NULL)
4120 OBD_FREE(lwp_name, MAX_OBD_NAME);
4126 * Init client sequence manager which is used by local MDS to talk to sequence
4127 * controller on remote node.
4129 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4131 struct seq_server_site *ss = mdt_seq_site(mdt);
4136 /* check if this is adding the first MDC and controller is not yet
4138 OBD_ALLOC_PTR(ss->ss_client_seq);
4139 if (ss->ss_client_seq == NULL)
4142 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4143 if (prefix == NULL) {
4144 OBD_FREE_PTR(ss->ss_client_seq);
4145 ss->ss_client_seq = NULL;
4149 /* Note: seq_client_fini will be called in seq_site_fini */
4150 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4151 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4152 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4154 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4156 OBD_FREE_PTR(ss->ss_client_seq);
4157 ss->ss_client_seq = NULL;
4161 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4166 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4168 struct seq_server_site *ss;
4172 ss = mdt_seq_site(mdt);
4173 /* init sequence controller server(MDT0) */
4174 if (ss->ss_node_id == 0) {
4175 OBD_ALLOC_PTR(ss->ss_control_seq);
4176 if (ss->ss_control_seq == NULL)
4179 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4180 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4183 GOTO(out_seq_fini, rc);
4186 /* Init normal sequence server */
4187 OBD_ALLOC_PTR(ss->ss_server_seq);
4188 if (ss->ss_server_seq == NULL)
4189 GOTO(out_seq_fini, rc = -ENOMEM);
4191 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4192 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4194 GOTO(out_seq_fini, rc);
4196 /* init seq client for seq server to talk to seq controller(MDT0) */
4197 rc = mdt_seq_init_cli(env, mdt);
4199 GOTO(out_seq_fini, rc);
4201 if (ss->ss_node_id != 0)
4202 /* register controller export through lwp */
4203 rc = mdt_register_seq_exp(mdt);
4208 mdt_seq_fini(env, mdt);
4216 static int mdt_fld_fini(const struct lu_env *env,
4217 struct mdt_device *m)
4219 struct seq_server_site *ss = mdt_seq_site(m);
4222 if (ss && ss->ss_server_fld) {
4223 fld_server_fini(env, ss->ss_server_fld);
4224 OBD_FREE_PTR(ss->ss_server_fld);
4225 ss->ss_server_fld = NULL;
4231 static int mdt_fld_init(const struct lu_env *env,
4233 struct mdt_device *m)
4235 struct seq_server_site *ss;
4239 ss = mdt_seq_site(m);
4241 OBD_ALLOC_PTR(ss->ss_server_fld);
4242 if (ss->ss_server_fld == NULL)
4243 RETURN(rc = -ENOMEM);
4245 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4248 OBD_FREE_PTR(ss->ss_server_fld);
4249 ss->ss_server_fld = NULL;
4256 static void mdt_stack_pre_fini(const struct lu_env *env,
4257 struct mdt_device *m, struct lu_device *top)
4259 struct lustre_cfg_bufs *bufs;
4260 struct lustre_cfg *lcfg;
4261 struct mdt_thread_info *info;
4266 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4267 LASSERT(info != NULL);
4269 bufs = &info->mti_u.bufs;
4271 LASSERT(m->mdt_child_exp);
4272 LASSERT(m->mdt_child_exp->exp_obd);
4274 /* process cleanup, pass mdt obd name to get obd umount flags */
4275 /* XXX: this is needed because all layers are referenced by
4276 * objects (some of them are pinned by osd, for example *
4277 * the proper solution should be a model where object used
4278 * by osd only doesn't have mdt/mdd slices -bzzz */
4279 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4280 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4281 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4284 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4286 top->ld_ops->ldo_process_config(env, top, lcfg);
4287 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4291 static void mdt_stack_fini(const struct lu_env *env,
4292 struct mdt_device *m, struct lu_device *top)
4294 struct obd_device *obd = mdt2obd_dev(m);
4295 struct lustre_cfg_bufs *bufs;
4296 struct lustre_cfg *lcfg;
4297 struct mdt_thread_info *info;
4301 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4302 LASSERT(info != NULL);
4304 lu_dev_del_linkage(top->ld_site, top);
4306 lu_site_purge(env, top->ld_site, -1);
4308 bufs = &info->mti_u.bufs;
4309 /* process cleanup, pass mdt obd name to get obd umount flags */
4310 /* another purpose is to let all layers to release their objects */
4311 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4316 lustre_cfg_bufs_set_string(bufs, 1, flags);
4317 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4320 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
4323 top->ld_ops->ldo_process_config(env, top, lcfg);
4324 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4326 lu_site_purge(env, top->ld_site, -1);
4328 m->mdt_child = NULL;
4329 m->mdt_bottom = NULL;
4331 obd_disconnect(m->mdt_child_exp);
4332 m->mdt_child_exp = NULL;
4334 obd_disconnect(m->mdt_bottom_exp);
4335 m->mdt_child_exp = NULL;
4338 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4339 const char *next, struct obd_export **exp)
4341 struct obd_connect_data *data = NULL;
4342 struct obd_device *obd;
4346 OBD_ALLOC_PTR(data);
4348 GOTO(out, rc = -ENOMEM);
4350 obd = class_name2obd(next);
4352 CERROR("%s: can't locate next device: %s\n",
4353 mdt_obd_name(m), next);
4354 GOTO(out, rc = -ENOTCONN);
4357 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4358 data->ocd_version = LUSTRE_VERSION_CODE;
4360 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4362 CERROR("%s: cannot connect to next dev %s (%d)\n",
4363 mdt_obd_name(m), next, rc);
4373 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4374 struct lustre_cfg *cfg)
4376 char *dev = lustre_cfg_string(cfg, 0);
4377 int rc, name_size, uuid_size;
4378 char *name, *uuid, *p;
4379 struct lustre_cfg_bufs *bufs;
4380 struct lustre_cfg *lcfg;
4381 struct obd_device *obd;
4382 struct lustre_profile *lprof;
4383 struct lu_site *site;
4386 /* in 1.8 we had the only device in the stack - MDS.
4387 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4388 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4389 * to generate names and setup MDT, MDD. MDT will be using
4390 * generated name to connect to MDD. for MDD the next device
4391 * will be LOD with name taken from so called "profile" which
4392 * is generated by mount_option line
4394 * 1.8 MGS generates config. commands like this:
4395 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4396 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4397 * 2.0 MGS generates config. commands like this:
4398 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4399 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4400 * 3:lustre-MDT0000-mdtlov 4:f
4402 * we generate MDD name from MDT one, just replacing T with D
4404 * after all the preparations, the logical equivalent will be
4405 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4406 * 3:lustre-MDT0000-mdtlov 4:f
4407 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4408 * 3:lustre-MDD0000 4:f
4410 * notice we build the stack from down to top: MDD first, then MDT */
4412 name_size = MAX_OBD_NAME;
4413 uuid_size = MAX_OBD_NAME;
4415 OBD_ALLOC(name, name_size);
4416 OBD_ALLOC(uuid, uuid_size);
4417 if (name == NULL || uuid == NULL)
4418 GOTO(cleanup_mem, rc = -ENOMEM);
4420 OBD_ALLOC_PTR(bufs);
4422 GOTO(cleanup_mem, rc = -ENOMEM);
4425 p = strstr(name, "-MDT");
4427 GOTO(free_bufs, rc = -ENOMEM);
4430 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4432 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4433 if (lprof == NULL || lprof->lp_dt == NULL) {
4434 CERROR("can't find the profile: %s\n",
4435 lustre_cfg_string(cfg, 0));
4436 GOTO(free_bufs, rc = -EINVAL);
4439 lustre_cfg_bufs_reset(bufs, name);
4440 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4441 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4442 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4444 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4446 GOTO(put_profile, rc = -ENOMEM);
4447 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4449 rc = class_attach(lcfg);
4451 GOTO(lcfg_cleanup, rc);
4453 obd = class_name2obd(name);
4455 CERROR("Can not find obd %s (%s in config)\n",
4456 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4457 GOTO(lcfg_cleanup, rc = -EINVAL);
4460 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4462 lustre_cfg_bufs_reset(bufs, name);
4463 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4464 lustre_cfg_bufs_set_string(bufs, 2, dev);
4465 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4467 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4469 GOTO(class_detach, rc = -ENOMEM);
4470 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4472 rc = class_setup(obd, lcfg);
4474 GOTO(class_detach, rc);
4476 /* connect to MDD we just setup */
4477 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4479 GOTO(class_detach, rc);
4481 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4483 LASSERT(mdt_lu_site(mdt) == NULL);
4484 mdt->mdt_lu_dev.ld_site = site;
4485 site->ls_top_dev = &mdt->mdt_lu_dev;
4486 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4488 /* now connect to bottom OSD */
4489 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4490 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4492 GOTO(class_detach, rc);
4494 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4496 rc = lu_env_refill((struct lu_env *)env);
4498 CERROR("Failure to refill session: '%d'\n", rc);
4500 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4505 class_detach(obd, lcfg);
4507 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4509 class_put_profile(lprof);
4514 OBD_FREE(name, name_size);
4516 OBD_FREE(uuid, uuid_size);
4520 /* setup quota master target on MDT0 */
4521 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4522 struct lustre_cfg *cfg)
4524 struct obd_device *obd;
4525 char *dev = lustre_cfg_string(cfg, 0);
4526 char *qmtname, *uuid, *p;
4527 struct lustre_cfg_bufs *bufs;
4528 struct lustre_cfg *lcfg;
4529 struct lustre_profile *lprof;
4530 struct obd_connect_data *data;
4534 LASSERT(mdt->mdt_qmt_exp == NULL);
4535 LASSERT(mdt->mdt_qmt_dev == NULL);
4537 /* quota master is on MDT0 only for now */
4538 if (mdt->mdt_seq_site.ss_node_id != 0)
4541 /* MGS generates config commands which look as follows:
4542 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4543 * 3:lustre-MDT0000-mdtlov 4:f
4545 * We generate the QMT name from the MDT one, just replacing MD with QM
4546 * after all the preparations, the logical equivalent will be:
4547 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4548 * 3:lustre-MDT0000-osd 4:f */
4549 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4550 OBD_ALLOC(uuid, UUID_MAX);
4551 OBD_ALLOC_PTR(bufs);
4552 OBD_ALLOC_PTR(data);
4553 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4554 GOTO(cleanup_mem, rc = -ENOMEM);
4556 strcpy(qmtname, dev);
4557 p = strstr(qmtname, "-MDT");
4559 GOTO(cleanup_mem, rc = -ENOMEM);
4560 /* replace MD with QM */
4564 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4566 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4567 if (lprof == NULL || lprof->lp_dt == NULL) {
4568 CERROR("can't find profile for %s\n",
4569 lustre_cfg_string(cfg, 0));
4570 GOTO(cleanup_mem, rc = -EINVAL);
4573 lustre_cfg_bufs_reset(bufs, qmtname);
4574 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4575 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4576 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4578 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4580 GOTO(put_profile, rc = -ENOMEM);
4581 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4583 rc = class_attach(lcfg);
4585 GOTO(lcfg_cleanup, rc);
4587 obd = class_name2obd(qmtname);
4589 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4590 lustre_cfg_string(cfg, 0));
4591 GOTO(lcfg_cleanup, rc = -EINVAL);
4594 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4596 lustre_cfg_bufs_reset(bufs, qmtname);
4597 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4598 lustre_cfg_bufs_set_string(bufs, 2, dev);
4600 /* for quota, the next device should be the OSD device */
4601 lustre_cfg_bufs_set_string(bufs, 3,
4602 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4604 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4606 GOTO(class_detach, rc = -ENOMEM);
4607 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4609 rc = class_setup(obd, lcfg);
4611 GOTO(class_detach, rc);
4613 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4615 /* configure local quota objects */
4616 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4620 GOTO(class_cleanup, rc);
4622 /* connect to quota master target */
4623 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4624 data->ocd_version = LUSTRE_VERSION_CODE;
4625 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4628 CERROR("cannot connect to quota master device %s (%d)\n",
4630 GOTO(class_cleanup, rc);
4636 class_manual_cleanup(obd);
4637 mdt->mdt_qmt_dev = NULL;
4641 class_detach(obd, lcfg);
4643 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4645 class_put_profile(lprof);
4650 OBD_FREE(qmtname, MAX_OBD_NAME);
4652 OBD_FREE(uuid, UUID_MAX);
4658 /* Shutdown quota master target associated with mdt */
4659 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4663 if (mdt->mdt_qmt_exp == NULL)
4665 LASSERT(mdt->mdt_qmt_dev != NULL);
4667 /* the qmt automatically shuts down when the mdt disconnects */
4668 obd_disconnect(mdt->mdt_qmt_exp);
4669 mdt->mdt_qmt_exp = NULL;
4670 mdt->mdt_qmt_dev = NULL;
4674 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4675 * for now. This will be removed along with converting rest of MDT code
4676 * to use tgt_session_info */
4677 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4679 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4682 rc = mdt_getxattr(info);
4684 mdt_thread_info_fini(info);
4688 #define OBD_FAIL_OST_READ_NET OBD_FAIL_OST_BRW_NET
4689 #define OBD_FAIL_OST_WRITE_NET OBD_FAIL_OST_BRW_NET
4690 #define OST_BRW_READ OST_READ
4691 #define OST_BRW_WRITE OST_WRITE
4693 static struct tgt_handler mdt_tgt_handlers[] = {
4694 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4695 0, MDS_CONNECT, mdt_tgt_connect,
4696 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4697 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4698 0, MDS_DISCONNECT, tgt_disconnect,
4699 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4700 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4701 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4702 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4703 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4704 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4705 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4706 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4708 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4709 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4710 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4711 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4712 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4713 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4714 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4715 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4717 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4718 mdt_hsm_ct_register),
4719 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4720 mdt_hsm_ct_unregister),
4721 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4723 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4725 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4726 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4728 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4733 static struct tgt_handler mdt_io_ops[] = {
4734 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO, OST_BRW_READ, tgt_brw_read),
4735 TGT_OST_HDL(HABEO_CORPUS | MUTABOR, OST_BRW_WRITE, tgt_brw_write),
4736 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4737 OST_PUNCH, mdt_punch_hdl),
4738 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO, OST_SYNC, mdt_data_sync),
4741 static struct tgt_handler mdt_sec_ctx_ops[] = {
4742 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4743 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4744 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4747 static struct tgt_handler mdt_quota_ops[] = {
4748 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4751 static struct tgt_opc_slice mdt_common_slice[] = {
4753 .tos_opc_start = MDS_FIRST_OPC,
4754 .tos_opc_end = MDS_LAST_OPC,
4755 .tos_hs = mdt_tgt_handlers
4758 .tos_opc_start = OBD_FIRST_OPC,
4759 .tos_opc_end = OBD_LAST_OPC,
4760 .tos_hs = tgt_obd_handlers
4763 .tos_opc_start = LDLM_FIRST_OPC,
4764 .tos_opc_end = LDLM_LAST_OPC,
4765 .tos_hs = tgt_dlm_handlers
4768 .tos_opc_start = SEC_FIRST_OPC,
4769 .tos_opc_end = SEC_LAST_OPC,
4770 .tos_hs = mdt_sec_ctx_ops
4773 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4774 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4775 .tos_hs = tgt_out_handlers
4778 .tos_opc_start = FLD_FIRST_OPC,
4779 .tos_opc_end = FLD_LAST_OPC,
4780 .tos_hs = fld_handlers
4783 .tos_opc_start = SEQ_FIRST_OPC,
4784 .tos_opc_end = SEQ_LAST_OPC,
4785 .tos_hs = seq_handlers
4788 .tos_opc_start = QUOTA_DQACQ,
4789 .tos_opc_end = QUOTA_LAST_OPC,
4790 .tos_hs = mdt_quota_ops
4793 .tos_opc_start = LLOG_FIRST_OPC,
4794 .tos_opc_end = LLOG_LAST_OPC,
4795 .tos_hs = tgt_llog_handlers
4798 .tos_opc_start = LFSCK_FIRST_OPC,
4799 .tos_opc_end = LFSCK_LAST_OPC,
4800 .tos_hs = tgt_lfsck_handlers
4803 .tos_opc_start = OST_FIRST_OPC,
4804 .tos_opc_end = OST_LAST_OPC,
4805 .tos_hs = mdt_io_ops
4812 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4814 struct md_device *next = m->mdt_child;
4815 struct lu_device *d = &m->mdt_lu_dev;
4816 struct obd_device *obd = mdt2obd_dev(m);
4817 struct lfsck_stop stop;
4820 stop.ls_status = LS_PAUSED;
4822 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4824 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4825 ping_evictor_stop();
4827 /* Remove the HSM /proc entry so the coordinator cannot be
4828 * restarted by a user while it's shutting down. */
4829 hsm_cdt_procfs_fini(m);
4830 mdt_hsm_cdt_stop(m);
4832 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4833 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4835 if (m->mdt_namespace != NULL)
4836 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4837 d->ld_obd->obd_force);
4839 obd_exports_barrier(obd);
4840 obd_zombie_barrier();
4842 mdt_quota_fini(env, m);
4844 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4846 /* Calling the cleanup functions in the same order as in the mdt_init0
4851 target_recovery_fini(obd);
4852 upcall_cache_cleanup(m->mdt_identity_cache);
4853 m->mdt_identity_cache = NULL;
4855 mdt_fs_cleanup(env, m);
4857 tgt_fini(env, &m->mdt_lut);
4859 mdt_hsm_cdt_fini(m);
4861 if (m->mdt_namespace != NULL) {
4862 ldlm_namespace_free_post(m->mdt_namespace);
4863 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4866 if (m->mdt_md_root != NULL) {
4867 mdt_object_put(env, m->mdt_md_root);
4868 m->mdt_md_root = NULL;
4871 mdt_seq_fini(env, m);
4873 mdt_fld_fini(env, m);
4878 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4880 LASSERT(atomic_read(&d->ld_ref) == 0);
4882 server_put_mount(mdt_obd_name(m), true);
4887 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4889 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4890 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4892 struct mdt_thread_info *info;
4893 struct obd_device *obd;
4894 const char *dev = lustre_cfg_string(cfg, 0);
4895 const char *num = lustre_cfg_string(cfg, 2);
4896 struct lustre_mount_info *lmi = NULL;
4897 struct lustre_sb_info *lsi;
4899 struct seq_server_site *ss_site;
4900 const char *identity_upcall = "NONE";
4901 struct md_device *next;
4907 lu_device_init(&m->mdt_lu_dev, ldt);
4909 * Environment (env) might be missing mdt_thread_key values at that
4910 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4913 * Usually device allocation path doesn't use module key values, but
4914 * mdt has to do a lot of work here, so allocate key value.
4916 rc = lu_env_refill((struct lu_env *)env);
4920 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4921 LASSERT(info != NULL);
4923 obd = class_name2obd(dev);
4924 LASSERT(obd != NULL);
4926 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4927 m->mdt_opts.mo_evict_tgt_nids = 1;
4928 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4930 lmi = server_get_mount(dev);
4932 CERROR("Cannot get mount info for %s!\n", dev);
4935 lsi = s2lsi(lmi->lmi_sb);
4936 /* CMD is supported only in IAM mode */
4938 node_id = simple_strtol(num, NULL, 10);
4939 obd->u.obt.obt_magic = OBT_MAGIC;
4940 if (lsi->lsi_lmd != NULL &&
4941 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4942 m->mdt_skip_lfsck = 1;
4945 m->mdt_squash.rsi_uid = 0;
4946 m->mdt_squash.rsi_gid = 0;
4947 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4948 init_rwsem(&m->mdt_squash.rsi_sem);
4949 spin_lock_init(&m->mdt_lock);
4950 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4951 m->mdt_enable_remote_dir = 0;
4952 m->mdt_enable_remote_dir_gid = 0;
4954 atomic_set(&m->mdt_mds_mds_conns, 0);
4955 atomic_set(&m->mdt_async_commit_count, 0);
4957 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4958 m->mdt_lu_dev.ld_obd = obd;
4959 /* Set this lu_device to obd for error handling purposes. */
4960 obd->obd_lu_dev = &m->mdt_lu_dev;
4962 /* init the stack */
4963 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4965 CERROR("%s: Can't init device stack, rc %d\n",
4966 mdt_obd_name(m), rc);
4971 ss_site = mdt_seq_site(m);
4972 s->ld_seq_site = ss_site;
4975 /* set server index */
4976 ss_site->ss_node_id = node_id;
4978 /* failover is the default
4979 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4980 * assumed whose ss_node_id == 0 XXX
4982 obd->obd_replayable = 1;
4983 /* No connection accepted until configurations will finish */
4984 obd->obd_no_conn = 1;
4986 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4987 char *str = lustre_cfg_string(cfg, 4);
4988 if (strchr(str, 'n')) {
4989 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4990 obd->obd_replayable = 0;
4994 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4996 GOTO(err_fini_stack, rc);
4998 rc = mdt_seq_init(env, m);
5000 GOTO(err_fini_fld, rc);
5002 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
5003 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
5004 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
5005 LDLM_NAMESPACE_SERVER,
5006 LDLM_NAMESPACE_GREEDY,
5008 if (m->mdt_namespace == NULL)
5009 GOTO(err_fini_seq, rc = -ENOMEM);
5011 m->mdt_namespace->ns_lvbp = m;
5012 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
5014 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
5015 /* set obd_namespace for compatibility with old code */
5016 obd->obd_namespace = m->mdt_namespace;
5018 rc = mdt_hsm_cdt_init(m);
5020 CERROR("%s: error initializing coordinator, rc %d\n",
5021 mdt_obd_name(m), rc);
5022 GOTO(err_free_ns, rc);
5025 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
5026 OBD_FAIL_MDS_ALL_REQUEST_NET,
5027 OBD_FAIL_MDS_ALL_REPLY_NET);
5029 GOTO(err_free_hsm, rc);
5031 rc = mdt_fs_setup(env, m, obd, lsi);
5035 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
5037 next = m->mdt_child;
5038 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
5041 GOTO(err_fs_cleanup, rc);
5043 if (mntopts & MNTOPT_USERXATTR)
5044 m->mdt_opts.mo_user_xattr = 1;
5046 m->mdt_opts.mo_user_xattr = 0;
5048 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
5050 GOTO(err_fs_cleanup, rc);
5052 if (mntopts & MNTOPT_ACL)
5053 m->mdt_opts.mo_acl = 1;
5055 m->mdt_opts.mo_acl = 0;
5057 /* XXX: to support suppgid for ACL, we enable identity_upcall
5058 * by default, otherwise, maybe got unexpected -EACCESS. */
5059 if (m->mdt_opts.mo_acl)
5060 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
5062 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
5064 &mdt_identity_upcall_cache_ops);
5065 if (IS_ERR(m->mdt_identity_cache)) {
5066 rc = PTR_ERR(m->mdt_identity_cache);
5067 m->mdt_identity_cache = NULL;
5068 GOTO(err_fs_cleanup, rc);
5071 rc = mdt_procfs_init(m, dev);
5073 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5074 GOTO(err_recovery, rc);
5077 rc = mdt_quota_init(env, m, cfg);
5079 GOTO(err_procfs, rc);
5081 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5082 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5083 "mdt_ldlm_client", m->mdt_ldlm_client);
5085 ping_evictor_start();
5087 /* recovery will be started upon mdt_prepare()
5088 * when the whole stack is complete and ready
5089 * to serve the requests */
5091 /* Reduce the initial timeout on an MDS because it doesn't need such
5092 * a long timeout as an OST does. Adaptive timeouts will adjust this
5093 * value appropriately. */
5094 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5095 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5101 target_recovery_fini(obd);
5102 upcall_cache_cleanup(m->mdt_identity_cache);
5103 m->mdt_identity_cache = NULL;
5105 mdt_fs_cleanup(env, m);
5107 tgt_fini(env, &m->mdt_lut);
5109 mdt_hsm_cdt_fini(m);
5111 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5112 obd->obd_namespace = m->mdt_namespace = NULL;
5114 mdt_seq_fini(env, m);
5116 mdt_fld_fini(env, m);
5118 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5121 server_put_mount(dev, true);
5125 /* For interoperability, the left element is old parameter, the right one
5126 * is the new version of the parameter, if some parameter is deprecated,
5127 * the new version should be set as NULL. */
5128 static struct cfg_interop_param mdt_interop_param[] = {
5129 { "mdt.group_upcall", NULL },
5130 { "mdt.quota_type", NULL },
5131 { "mdd.quota_type", NULL },
5132 { "mdt.som", NULL },
5133 { "mdt.rootsquash", "mdt.root_squash" },
5134 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5138 /* used by MGS to process specific configurations */
5139 static int mdt_process_config(const struct lu_env *env,
5140 struct lu_device *d, struct lustre_cfg *cfg)
5142 struct mdt_device *m = mdt_dev(d);
5143 struct md_device *md_next = m->mdt_child;
5144 struct lu_device *next = md2lu_dev(md_next);
5148 switch (cfg->lcfg_command) {
5150 struct obd_device *obd = d->ld_obd;
5152 /* For interoperability */
5153 struct cfg_interop_param *ptr = NULL;
5154 struct lustre_cfg *old_cfg = NULL;
5157 param = lustre_cfg_string(cfg, 1);
5158 if (param == NULL) {
5159 CERROR("param is empty\n");
5164 ptr = class_find_old_param(param, mdt_interop_param);
5166 if (ptr->new_param == NULL) {
5168 CWARN("For interoperability, skip this %s."
5169 " It is obsolete.\n", ptr->old_param);
5173 CWARN("Found old param %s, changed it to %s.\n",
5174 ptr->old_param, ptr->new_param);
5177 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5184 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5186 if (rc > 0 || rc == -ENOSYS) {
5187 /* is it an HSM var ? */
5188 rc = class_process_proc_param(PARAM_HSM,
5189 hsm_cdt_get_proc_vars(),
5191 if (rc > 0 || rc == -ENOSYS)
5192 /* we don't understand; pass it on */
5193 rc = next->ld_ops->ldo_process_config(env, next,
5198 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5199 cfg->lcfg_buflens));
5203 /* others are passed further */
5204 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5210 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5211 const struct lu_object_header *hdr,
5212 struct lu_device *d)
5214 struct mdt_object *mo;
5218 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5220 struct lu_object *o;
5221 struct lu_object_header *h;
5224 h = &mo->mot_header;
5225 lu_object_header_init(h);
5226 lu_object_init(o, h, d);
5227 lu_object_add_top(h, o);
5228 o->lo_ops = &mdt_obj_ops;
5229 spin_lock_init(&mo->mot_write_lock);
5230 mutex_init(&mo->mot_lov_mutex);
5231 init_rwsem(&mo->mot_dom_sem);
5232 init_rwsem(&mo->mot_open_sem);
5233 atomic_set(&mo->mot_open_count, 0);
5239 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5240 const struct lu_object_conf *unused)
5242 struct mdt_device *d = mdt_dev(o->lo_dev);
5243 struct lu_device *under;
5244 struct lu_object *below;
5248 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5249 PFID(lu_object_fid(o)));
5251 under = &d->mdt_child->md_lu_dev;
5252 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5253 if (below != NULL) {
5254 lu_object_add(o, below);
5261 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5263 struct mdt_object *mo = mdt_obj(o);
5264 struct lu_object_header *h;
5268 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5269 PFID(lu_object_fid(o)));
5271 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5272 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5275 lu_object_header_fini(h);
5276 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5281 static int mdt_object_print(const struct lu_env *env, void *cookie,
5282 lu_printer_t p, const struct lu_object *o)
5284 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5286 return (*p)(env, cookie,
5287 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5288 mdto, mdto->mot_lov_created ? "lov_created" : "",
5289 mdto->mot_cache_attr ? "cache_attr" : "",
5290 mdto->mot_write_count);
5293 static int mdt_prepare(const struct lu_env *env,
5294 struct lu_device *pdev,
5295 struct lu_device *cdev)
5297 struct mdt_device *mdt = mdt_dev(cdev);
5298 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5299 struct obd_device *obd = cdev->ld_obd;
5306 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5310 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5314 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5318 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5319 /* The LFSCK instance is registered just now, so it must be there when
5320 * register the namespace to such instance. */
5321 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5323 if (mdt->mdt_seq_site.ss_node_id == 0) {
5324 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5325 &mdt->mdt_md_root_fid);
5330 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5332 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5333 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5334 LASSERT(obd->obd_no_conn);
5335 spin_lock(&obd->obd_dev_lock);
5336 obd->obd_no_conn = 0;
5337 spin_unlock(&obd->obd_dev_lock);
5339 if (obd->obd_recovering == 0)
5340 mdt_postrecov(env, mdt);
5345 const struct lu_device_operations mdt_lu_ops = {
5346 .ldo_object_alloc = mdt_object_alloc,
5347 .ldo_process_config = mdt_process_config,
5348 .ldo_prepare = mdt_prepare,
5351 static const struct lu_object_operations mdt_obj_ops = {
5352 .loo_object_init = mdt_object_init,
5353 .loo_object_free = mdt_object_free,
5354 .loo_object_print = mdt_object_print
5357 static int mdt_obd_set_info_async(const struct lu_env *env,
5358 struct obd_export *exp,
5359 __u32 keylen, void *key,
5360 __u32 vallen, void *val,
5361 struct ptlrpc_request_set *set)
5367 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5368 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5376 * Match client and server connection feature flags.
5378 * Compute the compatibility flags for a connection request based on
5379 * features mutually supported by client and server.
5381 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5382 * must not be updated here, otherwise a partially initialized value may
5383 * be exposed. After the connection request is successfully processed,
5384 * the top-level MDT connect request handler atomically updates the export
5385 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5386 * reply. \see mdt_connect().
5388 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5389 * full struct obd_connect_data. So care must be taken when accessing fields
5390 * that are not present in struct obd_connect_data_v1. See LU-16.
5392 * \param exp the obd_export associated with this client/target pair
5393 * \param mdt the target device for the connection
5394 * \param data stores data for this connect request
5397 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5398 * \retval -EBADE client and server feature requirements are incompatible
5400 static int mdt_connect_internal(const struct lu_env *env,
5401 struct obd_export *exp,
5402 struct mdt_device *mdt,
5403 struct obd_connect_data *data, bool reconnect)
5405 LASSERT(data != NULL);
5407 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5409 if (mdt->mdt_bottom->dd_rdonly &&
5410 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5411 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5414 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5415 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5417 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5419 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5420 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5421 CWARN("%s: client %s does not support ibits lock, either "
5422 "very old or an invalid client: flags %#llx\n",
5423 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5424 data->ocd_connect_flags);
5428 if (!mdt->mdt_opts.mo_acl)
5429 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5431 if (!mdt->mdt_opts.mo_user_xattr)
5432 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5434 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
5435 data->ocd_brw_size = min(data->ocd_brw_size, MD_MAX_BRW_SIZE);
5436 if (data->ocd_brw_size == 0) {
5437 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
5438 "ocd_version: %x ocd_grant: %d ocd_index: %u "
5439 "ocd_brw_size unexpectedly zero, network data "
5440 "corruption? Refusing to connect this client\n",
5442 exp->exp_client_uuid.uuid,
5443 exp, data->ocd_connect_flags, data->ocd_version,
5444 data->ocd_grant, data->ocd_index);
5449 if (OCD_HAS_FLAG(data, GRANT))
5450 data->ocd_grant = mdt_grant_connect(env, exp, data->ocd_grant,
5453 /* NB: Disregard the rule against updating
5454 * exp_connect_data.ocd_connect_flags in this case, since
5455 * tgt_client_new() needs to know if this is a lightweight
5456 * connection, and it is safe to expose this flag before
5457 * connection processing completes. */
5458 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5459 spin_lock(&exp->exp_lock);
5460 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5461 spin_unlock(&exp->exp_lock);
5464 data->ocd_version = LUSTRE_VERSION_CODE;
5466 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5467 CWARN("%s: MDS requires FID support, but client not\n",
5472 if (OCD_HAS_FLAG(data, PINGLESS)) {
5473 if (ptlrpc_pinger_suppress_pings()) {
5474 spin_lock(&exp->exp_obd->obd_dev_lock);
5475 list_del_init(&exp->exp_obd_chain_timed);
5476 spin_unlock(&exp->exp_obd->obd_dev_lock);
5478 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5482 data->ocd_max_easize = mdt->mdt_max_ea_size;
5484 /* NB: Disregard the rule against updating
5485 * exp_connect_data.ocd_connect_flags in this case, since
5486 * tgt_client_new() needs to know if this is client supports
5487 * multiple modify RPCs, and it is safe to expose this flag before
5488 * connection processing completes. */
5489 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5490 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5491 spin_lock(&exp->exp_lock);
5492 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5493 spin_unlock(&exp->exp_lock);
5496 if (OCD_HAS_FLAG(data, CKSUM)) {
5497 __u32 cksum_types = data->ocd_cksum_types;
5499 /* The client set in ocd_cksum_types the checksum types it
5500 * supports. We have to mask off the algorithms that we don't
5502 data->ocd_cksum_types &= cksum_types_supported_server();
5504 if (unlikely(data->ocd_cksum_types == 0)) {
5505 CERROR("%s: Connect with checksum support but no "
5506 "ocd_cksum_types is set\n",
5507 exp->exp_obd->obd_name);
5511 CDEBUG(D_RPCTRACE, "%s: cli %s supports cksum type %x, return "
5512 "%x\n", exp->exp_obd->obd_name, obd_export_nid2str(exp),
5513 cksum_types, data->ocd_cksum_types);
5515 /* This client does not support OBD_CONNECT_CKSUM
5516 * fall back to CRC32 */
5517 CDEBUG(D_RPCTRACE, "%s: cli %s does not support "
5518 "OBD_CONNECT_CKSUM, CRC32 will be used\n",
5519 exp->exp_obd->obd_name, obd_export_nid2str(exp));
5525 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5526 struct mdt_thread_info *info,
5527 struct mdt_file_data *mfd)
5529 struct lu_context ses;
5533 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5538 lu_context_enter(&ses);
5540 mdt_ucred(info)->uc_valid = UCRED_OLD;
5541 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5543 lu_context_exit(&ses);
5544 lu_context_fini(&ses);
5550 static int mdt_export_cleanup(struct obd_export *exp)
5552 struct list_head closing_list;
5553 struct mdt_export_data *med = &exp->exp_mdt_data;
5554 struct obd_device *obd = exp->exp_obd;
5555 struct mdt_device *mdt;
5556 struct mdt_thread_info *info;
5558 struct mdt_file_data *mfd, *n;
5562 INIT_LIST_HEAD(&closing_list);
5563 spin_lock(&med->med_open_lock);
5564 while (!list_empty(&med->med_open_head)) {
5565 struct list_head *tmp = med->med_open_head.next;
5566 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5568 /* Remove mfd handle so it can't be found again.
5569 * We are consuming the mfd_list reference here. */
5570 class_handle_unhash(&mfd->mfd_handle);
5571 list_move_tail(&mfd->mfd_list, &closing_list);
5573 spin_unlock(&med->med_open_lock);
5574 mdt = mdt_dev(obd->obd_lu_dev);
5575 LASSERT(mdt != NULL);
5577 rc = lu_env_init(&env, LCT_MD_THREAD);
5581 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5582 LASSERT(info != NULL);
5583 memset(info, 0, sizeof *info);
5584 info->mti_env = &env;
5585 info->mti_mdt = mdt;
5586 info->mti_exp = exp;
5588 if (!list_empty(&closing_list)) {
5589 struct md_attr *ma = &info->mti_attr;
5591 /* Close any open files (which may also cause orphan
5593 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5594 list_del_init(&mfd->mfd_list);
5595 ma->ma_need = ma->ma_valid = 0;
5597 /* This file is being closed due to an eviction, it
5598 * could have been modified and now dirty regarding to
5599 * HSM archive, check this!
5600 * The logic here is to mark a file dirty if there's a
5601 * chance it was dirtied before the client was evicted,
5602 * so that we don't have to wait for a release attempt
5603 * before finding out the file was actually dirty and
5604 * fail the release. Aggressively marking it dirty here
5605 * will cause the policy engine to attempt to
5606 * re-archive it; when rearchiving, we can compare the
5607 * current version to the HSM data_version and make the
5608 * archive request into a noop if it's not actually
5611 if (mfd->mfd_mode & FMODE_WRITE)
5612 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5614 /* Don't unlink orphan on failover umount, LU-184 */
5615 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5616 ma->ma_valid = MA_FLAGS;
5617 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5619 mdt_mfd_close(info, mfd);
5622 info->mti_mdt = NULL;
5623 /* cleanup client slot early */
5624 /* Do not erase record for recoverable client. */
5625 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5626 tgt_client_del(&env, exp);
5632 static inline void mdt_enable_slc(struct mdt_device *mdt)
5634 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5635 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5638 static inline void mdt_disable_slc(struct mdt_device *mdt)
5640 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5641 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5644 static int mdt_obd_disconnect(struct obd_export *exp)
5650 class_export_get(exp);
5652 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5653 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5654 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5656 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5657 mdt_disable_slc(mdt);
5660 rc = server_disconnect_export(exp);
5662 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5664 rc = mdt_export_cleanup(exp);
5665 nodemap_del_member(exp);
5666 class_export_put(exp);
5670 /* mds_connect copy */
5671 static int mdt_obd_connect(const struct lu_env *env,
5672 struct obd_export **exp, struct obd_device *obd,
5673 struct obd_uuid *cluuid,
5674 struct obd_connect_data *data,
5677 struct obd_export *lexp;
5678 struct lustre_handle conn = { 0 };
5679 struct mdt_device *mdt;
5681 lnet_nid_t *client_nid = localdata;
5684 LASSERT(env != NULL);
5685 LASSERT(data != NULL);
5687 if (!exp || !obd || !cluuid)
5690 mdt = mdt_dev(obd->obd_lu_dev);
5692 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5693 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5694 atomic_inc(&mdt->mdt_mds_mds_conns);
5695 mdt_enable_slc(mdt);
5699 * first, check whether the stack is ready to handle requests
5700 * XXX: probably not very appropriate method is used now
5701 * at some point we should find a better one
5703 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5704 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5705 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5706 rc = obd_get_info(env, mdt->mdt_child_exp,
5707 sizeof(KEY_OSP_CONNECTED),
5708 KEY_OSP_CONNECTED, NULL, NULL);
5711 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5714 rc = class_connect(&conn, obd, cluuid);
5718 lexp = class_conn2export(&conn);
5719 LASSERT(lexp != NULL);
5721 rc = nodemap_add_member(*client_nid, lexp);
5722 if (rc != 0 && rc != -EEXIST)
5725 rc = mdt_connect_internal(env, lexp, mdt, data, false);
5727 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5730 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5731 rc = tgt_client_new(env, lexp);
5733 mdt_export_stats_init(obd, lexp, localdata);
5737 class_disconnect(lexp);
5738 nodemap_del_member(lexp);
5742 /* Because we do not want this export to be evicted by pinger,
5743 * let's not add this export to the timed chain list. */
5744 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5745 spin_lock(&lexp->exp_obd->obd_dev_lock);
5746 list_del_init(&lexp->exp_obd_chain_timed);
5747 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5754 static int mdt_obd_reconnect(const struct lu_env *env,
5755 struct obd_export *exp, struct obd_device *obd,
5756 struct obd_uuid *cluuid,
5757 struct obd_connect_data *data,
5760 lnet_nid_t *client_nid = localdata;
5764 if (exp == NULL || obd == NULL || cluuid == NULL)
5767 rc = nodemap_add_member(*client_nid, exp);
5768 if (rc != 0 && rc != -EEXIST)
5771 rc = mdt_connect_internal(env, exp, mdt_dev(obd->obd_lu_dev), data,
5774 mdt_export_stats_init(obd, exp, localdata);
5776 nodemap_del_member(exp);
5781 /* FIXME: Can we avoid using these two interfaces? */
5782 static int mdt_init_export(struct obd_export *exp)
5784 struct mdt_export_data *med = &exp->exp_mdt_data;
5788 INIT_LIST_HEAD(&med->med_open_head);
5789 spin_lock_init(&med->med_open_lock);
5790 spin_lock(&exp->exp_lock);
5791 exp->exp_connecting = 1;
5792 spin_unlock(&exp->exp_lock);
5794 /* self-export doesn't need client data and ldlm initialization */
5795 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5796 &exp->exp_client_uuid)))
5799 rc = tgt_client_alloc(exp);
5803 rc = ldlm_init_export(exp);
5810 tgt_client_free(exp);
5812 CERROR("%s: Failed to initialize export: rc = %d\n",
5813 exp->exp_obd->obd_name, rc);
5817 static int mdt_destroy_export(struct obd_export *exp)
5821 target_destroy_export(exp);
5822 /* destroy can be called from failed obd_setup, so
5823 * checking uuid is safer than obd_self_export */
5824 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5825 &exp->exp_client_uuid)))
5828 ldlm_destroy_export(exp);
5829 tgt_client_free(exp);
5831 LASSERT(list_empty(&exp->exp_outstanding_replies));
5832 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5837 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5838 struct linkea_data *ldata)
5842 LASSERT(ldata->ld_buf->lb_buf != NULL);
5844 if (!mdt_object_exists(mdt_obj))
5847 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5848 ldata->ld_buf, XATTR_NAME_LINK);
5849 if (rc == -ERANGE) {
5850 /* Buf was too small, figure out what we need. */
5851 lu_buf_free(ldata->ld_buf);
5852 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5853 ldata->ld_buf, XATTR_NAME_LINK);
5856 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5857 if (ldata->ld_buf->lb_buf == NULL)
5859 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5860 ldata->ld_buf, XATTR_NAME_LINK);
5865 return linkea_init_with_rec(ldata);
5869 * Given an MDT object, try to look up the full path to the object.
5870 * Part of the MDT layer implementation of lfs fid2path.
5872 * \param[in] info Per-thread common data shared by MDT level handlers.
5873 * \param[in] obj Object to do path lookup of
5874 * \param[in,out] fp User-provided struct to store path information
5875 * \param[in] root_fid Root FID of current path should reach
5877 * \retval 0 Lookup successful, path information stored in fp
5878 * \retval -EAGAIN Lookup failed, usually because object is being moved
5879 * \retval negative errno if there was a problem
5881 static int mdt_path_current(struct mdt_thread_info *info,
5882 struct mdt_object *obj,
5883 struct getinfo_fid2path *fp,
5884 struct lu_fid *root_fid)
5886 struct mdt_device *mdt = info->mti_mdt;
5887 struct mdt_object *mdt_obj;
5888 struct link_ea_header *leh;
5889 struct link_ea_entry *lee;
5890 struct lu_name *tmpname = &info->mti_name;
5891 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5892 struct lu_buf *buf = &info->mti_big_buf;
5895 struct linkea_data ldata = { NULL };
5900 /* temp buffer for path element, the buffer will be finally freed
5901 * in mdt_thread_info_fini */
5902 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5903 if (buf->lb_buf == NULL)
5907 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5910 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5912 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5913 struct lu_buf lmv_buf;
5915 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5916 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5917 GOTO(out, rc = -ENOENT);
5919 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5921 mdt_object_get(info->mti_env, mdt_obj);
5923 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5924 if (IS_ERR(mdt_obj))
5925 GOTO(out, rc = PTR_ERR(mdt_obj));
5928 if (!mdt_object_exists(mdt_obj)) {
5929 mdt_object_put(info->mti_env, mdt_obj);
5930 GOTO(out, rc = -ENOENT);
5933 if (mdt_object_remote(mdt_obj)) {
5934 mdt_object_put(info->mti_env, mdt_obj);
5935 GOTO(remote_out, rc = -EREMOTE);
5938 rc = mdt_links_read(info, mdt_obj, &ldata);
5940 mdt_object_put(info->mti_env, mdt_obj);
5945 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5946 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5947 /* If set, use link #linkno for path lookup, otherwise use
5948 link #0. Only do this for the final path element. */
5949 if (first && fp->gf_linkno < leh->leh_reccount) {
5951 for (count = 0; count < fp->gf_linkno; count++) {
5952 lee = (struct link_ea_entry *)
5953 ((char *)lee + reclen);
5954 linkea_entry_unpack(lee, &reclen, tmpname,
5957 if (fp->gf_linkno < leh->leh_reccount - 1)
5958 /* indicate to user there are more links */
5962 lmv_buf.lb_buf = info->mti_xattr_buf;
5963 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5964 /* Check if it is slave stripes */
5965 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5966 &lmv_buf, XATTR_NAME_LMV);
5967 mdt_object_put(info->mti_env, mdt_obj);
5969 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5971 /* For slave stripes, get its master */
5972 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5973 fp->gf_fid = *tmpfid;
5976 } else if (rc < 0 && rc != -ENODATA) {
5982 /* Pack the name in the end of the buffer */
5983 ptr -= tmpname->ln_namelen;
5984 if (ptr - 1 <= fp->gf_u.gf_path)
5985 GOTO(out, rc = -EOVERFLOW);
5986 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5989 /* keep the last resolved fid to the client, so the
5990 * client will build the left path on another MDT for
5992 fp->gf_fid = *tmpfid;
5998 ptr++; /* skip leading / */
5999 memmove(fp->gf_u.gf_path, ptr,
6000 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
6007 * Given an MDT object, use mdt_path_current to get the path.
6008 * Essentially a wrapper to retry mdt_path_current a set number of times
6009 * if -EAGAIN is returned (usually because an object is being moved).
6011 * Part of the MDT layer implementation of lfs fid2path.
6013 * \param[in] info Per-thread common data shared by mdt level handlers.
6014 * \param[in] obj Object to do path lookup of
6015 * \param[in,out] fp User-provided struct for arguments and to store path
6018 * \retval 0 Lookup successful, path information stored in fp
6019 * \retval negative errno if there was a problem
6021 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
6022 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
6024 struct mdt_device *mdt = info->mti_mdt;
6029 if (fp->gf_pathlen < 3)
6032 if (root_fid == NULL)
6033 root_fid = &mdt->mdt_md_root_fid;
6035 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
6036 fp->gf_u.gf_path[0] = '\0';
6040 /* Retry multiple times in case file is being moved */
6041 while (tries-- && rc == -EAGAIN)
6042 rc = mdt_path_current(info, obj, fp, root_fid);
6048 * Get the full path of the provided FID, as of changelog record recno.
6050 * This checks sanity and looks up object for user provided FID
6051 * before calling the actual path lookup code.
6053 * Part of the MDT layer implementation of lfs fid2path.
6055 * \param[in] info Per-thread common data shared by mdt level handlers.
6056 * \param[in,out] fp User-provided struct for arguments and to store path
6059 * \retval 0 Lookup successful, path information and recno stored in fp
6060 * \retval -ENOENT, object does not exist
6061 * \retval negative errno if there was a problem
6063 static int mdt_fid2path(struct mdt_thread_info *info,
6064 struct lu_fid *root_fid,
6065 struct getinfo_fid2path *fp)
6067 struct mdt_device *mdt = info->mti_mdt;
6068 struct mdt_object *obj;
6072 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
6073 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
6075 if (!fid_is_sane(&fp->gf_fid))
6078 if (!fid_is_namespace_visible(&fp->gf_fid)) {
6079 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
6080 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
6081 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
6085 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
6088 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
6089 PFID(&fp->gf_fid), rc);
6093 if (mdt_object_remote(obj))
6095 else if (!mdt_object_exists(obj))
6101 mdt_object_put(info->mti_env, obj);
6102 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
6103 PFID(&fp->gf_fid), rc);
6107 rc = mdt_path(info, obj, fp, root_fid);
6109 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6110 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6111 fp->gf_recno, fp->gf_linkno);
6113 mdt_object_put(info->mti_env, obj);
6118 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6119 void *val, int vallen)
6121 struct getinfo_fid2path *fpout, *fpin;
6122 struct lu_fid *root_fid = NULL;
6125 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6128 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6129 lustre_swab_fid2path(fpin);
6131 memcpy(fpout, fpin, sizeof(*fpin));
6132 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6135 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6136 sizeof(struct lu_fid)) {
6137 /* client sent its root FID, which is normally fileset FID */
6138 root_fid = fpin->gf_u.gf_root_fid;
6139 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6140 lustre_swab_lu_fid(root_fid);
6142 if (root_fid != NULL && !fid_is_sane(root_fid))
6146 rc = mdt_fid2path(info, root_fid, fpout);
6150 int mdt_get_info(struct tgt_session_info *tsi)
6160 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6162 CDEBUG(D_IOCTL, "No GETINFO key\n");
6163 RETURN(err_serious(-EFAULT));
6165 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6168 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6169 if (vallen == NULL) {
6170 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6171 tgt_name(tsi->tsi_tgt));
6172 RETURN(err_serious(-EFAULT));
6175 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6177 rc = req_capsule_server_pack(tsi->tsi_pill);
6179 RETURN(err_serious(rc));
6181 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6182 if (valout == NULL) {
6183 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6184 tgt_name(tsi->tsi_tgt));
6185 RETURN(err_serious(-EFAULT));
6188 if (KEY_IS(KEY_FID2PATH)) {
6189 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6191 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6192 mdt_thread_info_fini(info);
6199 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6201 struct obd_ioctl_data *data = karg;
6204 struct mdt_object *obj;
6205 struct mdt_lock_handle *lh;
6209 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6210 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6213 fid = (struct lu_fid *)data->ioc_inlbuf1;
6215 if (!fid_is_sane(fid))
6218 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6220 lh = &mti->mti_lh[MDT_LH_PARENT];
6221 mdt_lock_reg_init(lh, LCK_CR);
6223 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6225 RETURN(PTR_ERR(obj));
6227 if (mdt_object_remote(obj)) {
6230 * before calling version get the correct MDS should be
6231 * fid, this is error to find remote object here
6233 CERROR("nonlocal object "DFID"\n", PFID(fid));
6234 } else if (!mdt_object_exists(obj)) {
6235 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6238 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6239 *(__u64 *)data->ioc_inlbuf2 = version;
6242 mdt_object_unlock_put(mti, obj, lh, 1);
6246 /* ioctls on obd dev */
6247 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6248 void *karg, void __user *uarg)
6251 struct obd_device *obd = exp->exp_obd;
6252 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6253 struct dt_device *dt = mdt->mdt_bottom;
6257 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6258 rc = lu_env_init(&env, LCT_MD_THREAD);
6264 rc = mdt_device_sync(&env, mdt);
6266 case OBD_IOC_SET_READONLY:
6267 rc = dt_sync(&env, dt);
6269 rc = dt_ro(&env, dt);
6271 case OBD_IOC_ABORT_RECOVERY:
6272 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6273 obd->obd_abort_recovery = 1;
6274 target_stop_recovery_thread(obd);
6277 case OBD_IOC_CHANGELOG_REG:
6278 case OBD_IOC_CHANGELOG_DEREG:
6279 case OBD_IOC_CHANGELOG_CLEAR:
6280 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6284 case OBD_IOC_START_LFSCK: {
6285 struct md_device *next = mdt->mdt_child;
6286 struct obd_ioctl_data *data = karg;
6287 struct lfsck_start_param lsp;
6289 if (unlikely(data == NULL)) {
6294 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6295 lsp.lsp_index_valid = 0;
6296 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6299 case OBD_IOC_STOP_LFSCK: {
6300 struct md_device *next = mdt->mdt_child;
6301 struct obd_ioctl_data *data = karg;
6302 struct lfsck_stop stop;
6304 stop.ls_status = LS_STOPPED;
6305 /* Old lfsck utils may pass NULL @stop. */
6306 if (data->ioc_inlbuf1 == NULL)
6310 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6312 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6315 case OBD_IOC_QUERY_LFSCK: {
6316 struct md_device *next = mdt->mdt_child;
6317 struct obd_ioctl_data *data = karg;
6319 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6323 case OBD_IOC_GET_OBJ_VERSION: {
6324 struct mdt_thread_info *mti;
6325 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6326 memset(mti, 0, sizeof *mti);
6327 mti->mti_env = &env;
6331 rc = mdt_ioc_version_get(mti, karg);
6334 case OBD_IOC_CATLOGLIST: {
6335 struct mdt_thread_info *mti;
6337 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6338 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6339 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6340 &mti->mti_tmp_fid1);
6345 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6346 mdt_obd_name(mdt), cmd, rc);
6353 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6355 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6359 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6360 struct lfsck_start_param lsp;
6362 lsp.lsp_start = NULL;
6363 lsp.lsp_index_valid = 0;
6364 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6365 OBD_IOC_START_LFSCK,
6367 if (rc != 0 && rc != -EALREADY)
6368 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6369 mdt_obd_name(mdt), rc);
6372 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6376 static int mdt_obd_postrecov(struct obd_device *obd)
6381 rc = lu_env_init(&env, LCT_MD_THREAD);
6384 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6389 static struct obd_ops mdt_obd_device_ops = {
6390 .o_owner = THIS_MODULE,
6391 .o_set_info_async = mdt_obd_set_info_async,
6392 .o_connect = mdt_obd_connect,
6393 .o_reconnect = mdt_obd_reconnect,
6394 .o_disconnect = mdt_obd_disconnect,
6395 .o_init_export = mdt_init_export,
6396 .o_destroy_export = mdt_destroy_export,
6397 .o_iocontrol = mdt_iocontrol,
6398 .o_postrecov = mdt_obd_postrecov,
6399 /* Data-on-MDT IO methods */
6400 .o_preprw = mdt_obd_preprw,
6401 .o_commitrw = mdt_obd_commitrw,
6404 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6405 struct lu_device *d)
6407 struct mdt_device *m = mdt_dev(d);
6414 static struct lu_device *mdt_device_free(const struct lu_env *env,
6415 struct lu_device *d)
6417 struct mdt_device *m = mdt_dev(d);
6420 lu_device_fini(&m->mdt_lu_dev);
6426 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6427 struct lu_device_type *t,
6428 struct lustre_cfg *cfg)
6430 struct lu_device *l;
6431 struct mdt_device *m;
6438 rc = mdt_init0(env, m, t, cfg);
6440 mdt_device_free(env, l);
6445 l = ERR_PTR(-ENOMEM);
6449 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6450 LU_KEY_INIT(mdt, struct mdt_thread_info);
6452 static void mdt_key_fini(const struct lu_context *ctx,
6453 struct lu_context_key *key, void* data)
6455 struct mdt_thread_info *info = data;
6457 if (info->mti_big_lmm) {
6458 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6459 info->mti_big_lmm = NULL;
6460 info->mti_big_lmmsize = 0;
6463 if (info->mti_big_acl) {
6464 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
6465 info->mti_big_acl = NULL;
6466 info->mti_big_aclsize = 0;
6472 /* context key: mdt_thread_key */
6473 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6475 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6477 return lu_ucred(info->mti_env);
6480 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6482 return lu_ucred_check(info->mti_env);
6486 * Enable/disable COS (Commit On Sharing).
6488 * Set/Clear the COS flag in mdt options.
6490 * \param mdt mdt device
6491 * \param val 0 disables COS, other values enable COS
6493 void mdt_enable_cos(struct mdt_device *mdt, int val)
6498 mdt->mdt_opts.mo_cos = !!val;
6499 rc = lu_env_init(&env, LCT_LOCAL);
6500 if (unlikely(rc != 0)) {
6501 CWARN("%s: lu_env initialization failed, cannot "
6502 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6505 mdt_device_sync(&env, mdt);
6510 * Check COS (Commit On Sharing) status.
6512 * Return COS flag status.
6514 * \param mdt mdt device
6516 int mdt_cos_is_enabled(struct mdt_device *mdt)
6518 return mdt->mdt_opts.mo_cos != 0;
6521 static struct lu_device_type_operations mdt_device_type_ops = {
6522 .ldto_device_alloc = mdt_device_alloc,
6523 .ldto_device_free = mdt_device_free,
6524 .ldto_device_fini = mdt_device_fini
6527 static struct lu_device_type mdt_device_type = {
6528 .ldt_tags = LU_DEVICE_MD,
6529 .ldt_name = LUSTRE_MDT_NAME,
6530 .ldt_ops = &mdt_device_type_ops,
6531 .ldt_ctx_tags = LCT_MD_THREAD
6534 static int __init mdt_init(void)
6538 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6539 FID_NOBRACE_LEN + 1);
6540 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6542 rc = lu_kmem_init(mdt_caches);
6546 rc = mds_mod_init();
6550 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6551 LUSTRE_MDT_NAME, &mdt_device_type);
6556 lu_kmem_fini(mdt_caches);
6563 static void __exit mdt_exit(void)
6565 class_unregister_type(LUSTRE_MDT_NAME);
6567 lu_kmem_fini(mdt_caches);
6570 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6571 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6572 MODULE_VERSION(LUSTRE_VERSION_STRING);
6573 MODULE_LICENSE("GPL");
6575 module_init(mdt_init);
6576 module_exit(mdt_exit);