4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <uapi/linux/lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_nodemap.h>
57 #include <lustre_mds.h>
58 #include <uapi/linux/lustre_param.h>
59 #include <lustre_quota.h>
60 #include <lustre_swab.h>
62 #include <obd_support.h>
63 #include <lustre_barrier.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 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 like tar
672 * will consider it fully sparse. (LU-3864)
674 if (unlikely(b->mbo_size == 0))
678 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
681 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
682 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
683 PFID(fid), (unsigned long long)b->mbo_size);
686 if (!IS_ERR_OR_NULL(nodemap))
687 nodemap_putref(nodemap);
690 static inline int mdt_body_has_lov(const struct lu_attr *la,
691 const struct mdt_body *body)
693 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
694 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
697 void mdt_client_compatibility(struct mdt_thread_info *info)
699 struct mdt_body *body;
700 struct ptlrpc_request *req = mdt_info_req(info);
701 struct obd_export *exp = req->rq_export;
702 struct md_attr *ma = &info->mti_attr;
703 struct lu_attr *la = &ma->ma_attr;
706 if (exp_connect_layout(exp))
707 /* the client can deal with 16-bit lmm_stripe_count */
710 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
712 if (!mdt_body_has_lov(la, body))
715 /* now we have a reply with a lov for a client not compatible with the
716 * layout lock so we have to clean the layout generation number */
717 if (S_ISREG(la->la_mode))
718 ma->ma_lmm->lmm_layout_gen = 0;
722 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
723 struct mdt_object *o)
725 const struct lu_env *env = info->mti_env;
728 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
737 /* Is it a directory? Let's check for the LMV as well */
738 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
739 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
743 rc2 = mo_xattr_get(env, mdt_object_child(o),
745 XATTR_NAME_DEFAULT_LMV);
747 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
755 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
758 const struct lu_env *env = info->mti_env;
762 LASSERT(info->mti_big_lmm_used == 0);
763 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
767 /* big_lmm may need to be grown */
768 if (info->mti_big_lmmsize < rc) {
769 int size = size_roundup_power2(rc);
771 if (info->mti_big_lmmsize > 0) {
772 /* free old buffer */
773 LASSERT(info->mti_big_lmm);
774 OBD_FREE_LARGE(info->mti_big_lmm,
775 info->mti_big_lmmsize);
776 info->mti_big_lmm = NULL;
777 info->mti_big_lmmsize = 0;
780 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
781 if (info->mti_big_lmm == NULL)
783 info->mti_big_lmmsize = size;
785 LASSERT(info->mti_big_lmmsize >= rc);
787 info->mti_buf.lb_buf = info->mti_big_lmm;
788 info->mti_buf.lb_len = info->mti_big_lmmsize;
789 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
794 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
795 struct md_attr *ma, const char *name)
797 struct md_object *next = mdt_object_child(o);
798 struct lu_buf *buf = &info->mti_buf;
801 if (strcmp(name, XATTR_NAME_LOV) == 0) {
802 buf->lb_buf = ma->ma_lmm;
803 buf->lb_len = ma->ma_lmm_size;
804 LASSERT(!(ma->ma_valid & MA_LOV));
805 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
806 buf->lb_buf = ma->ma_lmv;
807 buf->lb_len = ma->ma_lmv_size;
808 LASSERT(!(ma->ma_valid & MA_LMV));
809 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
810 buf->lb_buf = ma->ma_lmv;
811 buf->lb_len = ma->ma_lmv_size;
812 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
817 rc = mo_xattr_get(info->mti_env, next, buf, name);
821 if (strcmp(name, XATTR_NAME_LOV) == 0) {
822 if (info->mti_big_lmm_used)
823 ma->ma_lmm = info->mti_big_lmm;
825 /* NOT return LOV EA with hole to old client. */
826 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
827 LOV_PATTERN_F_HOLE) &&
828 !(exp_connect_flags(info->mti_exp) &
829 OBD_CONNECT_LFSCK)) {
832 ma->ma_lmm_size = rc;
833 ma->ma_valid |= MA_LOV;
835 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
836 if (info->mti_big_lmm_used)
837 ma->ma_lmv = info->mti_big_lmm;
839 ma->ma_lmv_size = rc;
840 ma->ma_valid |= MA_LMV;
841 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
842 ma->ma_lmv_size = rc;
843 ma->ma_valid |= MA_LMV_DEF;
846 /* Update mdt_max_mdsize so all clients will be aware that */
847 if (info->mti_mdt->mdt_max_mdsize < rc)
848 info->mti_mdt->mdt_max_mdsize = rc;
851 } else if (rc == -ENODATA) {
854 } else if (rc == -ERANGE) {
855 /* Default LMV has fixed size, so it must be able to fit
856 * in the original buffer */
857 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
859 rc = mdt_big_xattr_get(info, o, name);
861 info->mti_big_lmm_used = 1;
869 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
870 struct mdt_object *o, struct lu_fid *pfid)
872 struct lu_buf *buf = &info->mti_buf;
873 struct link_ea_header *leh;
874 struct link_ea_entry *lee;
878 buf->lb_buf = info->mti_big_lmm;
879 buf->lb_len = info->mti_big_lmmsize;
880 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
881 buf, XATTR_NAME_LINK);
882 /* ignore errors, MA_PFID won't be set and it is
883 * up to the caller to treat this as an error */
884 if (rc == -ERANGE || buf->lb_len == 0) {
885 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
886 buf->lb_buf = info->mti_big_lmm;
887 buf->lb_len = info->mti_big_lmmsize;
892 if (rc < sizeof(*leh)) {
893 CERROR("short LinkEA on "DFID": rc = %d\n",
894 PFID(mdt_object_fid(o)), rc);
898 leh = (struct link_ea_header *) buf->lb_buf;
899 lee = (struct link_ea_entry *)(leh + 1);
900 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
901 leh->leh_magic = LINK_EA_MAGIC;
902 leh->leh_reccount = __swab32(leh->leh_reccount);
903 leh->leh_len = __swab64(leh->leh_len);
905 if (leh->leh_magic != LINK_EA_MAGIC)
907 if (leh->leh_reccount == 0)
910 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
911 fid_be_to_cpu(pfid, pfid);
916 int mdt_attr_get_complex(struct mdt_thread_info *info,
917 struct mdt_object *o, struct md_attr *ma)
919 const struct lu_env *env = info->mti_env;
920 struct md_object *next = mdt_object_child(o);
921 struct lu_buf *buf = &info->mti_buf;
922 int need = ma->ma_need;
929 if (mdt_object_exists(o) == 0)
930 GOTO(out, rc = -ENOENT);
931 mode = lu_object_attr(&next->mo_lu);
933 if (need & MA_INODE) {
934 ma->ma_need = MA_INODE;
935 rc = mo_attr_get(env, next, ma);
938 ma->ma_valid |= MA_INODE;
941 if (need & MA_PFID) {
942 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
944 ma->ma_valid |= MA_PFID;
945 /* ignore this error, parent fid is not mandatory */
949 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
950 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
955 if (need & MA_LMV && S_ISDIR(mode)) {
956 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
961 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
962 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
967 if (need & MA_HSM && S_ISREG(mode)) {
968 buf->lb_buf = info->mti_xattr_buf;
969 buf->lb_len = sizeof(info->mti_xattr_buf);
970 CLASSERT(sizeof(struct hsm_attrs) <=
971 sizeof(info->mti_xattr_buf));
972 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
973 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
975 ma->ma_valid |= MA_HSM;
976 else if (rc2 < 0 && rc2 != -ENODATA)
980 #ifdef CONFIG_FS_POSIX_ACL
981 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
982 buf->lb_buf = ma->ma_acl;
983 buf->lb_len = ma->ma_acl_size;
984 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
986 ma->ma_acl_size = rc2;
987 ma->ma_valid |= MA_ACL_DEF;
988 } else if (rc2 == -ENODATA) {
997 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
998 rc, ma->ma_valid, ma->ma_lmm);
1002 static int mdt_getattr_internal(struct mdt_thread_info *info,
1003 struct mdt_object *o, int ma_need)
1005 struct md_object *next = mdt_object_child(o);
1006 const struct mdt_body *reqbody = info->mti_body;
1007 struct ptlrpc_request *req = mdt_info_req(info);
1008 struct md_attr *ma = &info->mti_attr;
1009 struct lu_attr *la = &ma->ma_attr;
1010 struct req_capsule *pill = info->mti_pill;
1011 const struct lu_env *env = info->mti_env;
1012 struct mdt_body *repbody;
1013 struct lu_buf *buffer = &info->mti_buf;
1014 struct obd_export *exp = info->mti_exp;
1018 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
1019 RETURN(err_serious(-ENOMEM));
1021 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1025 if (mdt_object_remote(o)) {
1026 /* This object is located on remote node.*/
1027 /* Return -ENOTSUPP for old client */
1028 if (!mdt_is_dne_client(req->rq_export))
1029 GOTO(out, rc = -ENOTSUPP);
1031 repbody->mbo_fid1 = *mdt_object_fid(o);
1032 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
1036 if (reqbody->mbo_eadatasize > 0) {
1037 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
1038 if (buffer->lb_buf == NULL)
1039 GOTO(out, rc = -EPROTO);
1040 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
1043 buffer->lb_buf = NULL;
1045 ma_need &= ~(MA_LOV | MA_LMV);
1046 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
1047 mdt_obd_name(info->mti_mdt),
1048 req->rq_export->exp_client_uuid.uuid);
1051 /* If it is dir object and client require MEA, then we got MEA */
1052 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1053 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
1054 /* Assumption: MDT_MD size is enough for lmv size. */
1055 ma->ma_lmv = buffer->lb_buf;
1056 ma->ma_lmv_size = buffer->lb_len;
1057 ma->ma_need = MA_INODE;
1058 if (ma->ma_lmv_size > 0) {
1059 if (reqbody->mbo_valid & OBD_MD_MEA)
1060 ma->ma_need |= MA_LMV;
1061 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1062 ma->ma_need |= MA_LMV_DEF;
1065 ma->ma_lmm = buffer->lb_buf;
1066 ma->ma_lmm_size = buffer->lb_len;
1067 ma->ma_need = MA_INODE | MA_HSM;
1068 if (ma->ma_lmm_size > 0)
1069 ma->ma_need |= MA_LOV;
1072 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1073 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1074 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1075 /* get default stripe info for this dir. */
1076 ma->ma_need |= MA_LOV_DEF;
1078 ma->ma_need |= ma_need;
1080 rc = mdt_attr_get_complex(info, o, ma);
1082 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1083 "%s: getattr error for "DFID": rc = %d\n",
1084 mdt_obd_name(info->mti_mdt),
1085 PFID(mdt_object_fid(o)), rc);
1089 /* if file is released, check if a restore is running */
1090 if (ma->ma_valid & MA_HSM) {
1091 repbody->mbo_valid |= OBD_MD_TSTATE;
1092 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1093 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1094 repbody->mbo_t_state = MS_RESTORE;
1097 if (likely(ma->ma_valid & MA_INODE))
1098 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1102 if (mdt_body_has_lov(la, reqbody)) {
1103 if (ma->ma_valid & MA_LOV) {
1104 LASSERT(ma->ma_lmm_size);
1105 repbody->mbo_eadatasize = ma->ma_lmm_size;
1106 if (S_ISDIR(la->la_mode))
1107 repbody->mbo_valid |= OBD_MD_FLDIREA;
1109 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1110 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1112 if (ma->ma_valid & MA_LMV) {
1113 /* Return -ENOTSUPP for old client */
1114 if (!mdt_is_striped_client(req->rq_export))
1117 LASSERT(S_ISDIR(la->la_mode));
1118 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1119 repbody->mbo_eadatasize = ma->ma_lmv_size;
1120 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1122 if (ma->ma_valid & MA_LMV_DEF) {
1123 /* Return -ENOTSUPP for old client */
1124 if (!mdt_is_striped_client(req->rq_export))
1126 LASSERT(S_ISDIR(la->la_mode));
1127 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1128 repbody->mbo_eadatasize = ma->ma_lmv_size;
1129 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1130 OBD_MD_DEFAULT_MEA);
1132 } else if (S_ISLNK(la->la_mode) &&
1133 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1134 buffer->lb_buf = ma->ma_lmm;
1135 /* eadatasize from client includes NULL-terminator, so
1136 * there is no need to read it */
1137 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1138 rc = mo_readlink(env, next, buffer);
1139 if (unlikely(rc <= 0)) {
1140 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1141 mdt_obd_name(info->mti_mdt),
1142 PFID(mdt_object_fid(o)), rc);
1145 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1147 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1149 repbody->mbo_valid |= OBD_MD_LINKNAME;
1150 /* we need to report back size with NULL-terminator
1151 * because client expects that */
1152 repbody->mbo_eadatasize = rc + 1;
1153 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1154 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1155 "on "DFID ", expected %d\n",
1156 mdt_obd_name(info->mti_mdt),
1157 rc, PFID(mdt_object_fid(o)),
1158 reqbody->mbo_eadatasize - 1);
1159 /* NULL terminate */
1160 ((char *)ma->ma_lmm)[rc] = 0;
1162 /* If the total CDEBUG() size is larger than a page, it
1163 * will print a warning to the console, avoid this by
1164 * printing just the last part of the symlink. */
1165 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1166 print_limit < rc ? "..." : "", print_limit,
1167 (char *)ma->ma_lmm + rc - print_limit, rc);
1172 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1173 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1174 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1175 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1176 repbody->mbo_max_mdsize);
1179 #ifdef CONFIG_FS_POSIX_ACL
1180 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1181 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1182 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1183 if (IS_ERR(nodemap))
1184 RETURN(PTR_ERR(nodemap));
1186 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1187 nodemap_putref(nodemap);
1193 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1198 static int mdt_getattr(struct tgt_session_info *tsi)
1200 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1201 struct mdt_object *obj = info->mti_object;
1202 struct req_capsule *pill = info->mti_pill;
1203 struct mdt_body *reqbody;
1204 struct mdt_body *repbody;
1208 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1210 LASSERT(obj != NULL);
1211 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1213 /* Unlike intent case where we need to pre-fill out buffers early on
1214 * in intent policy for ldlm reasons, here we can have a much better
1215 * guess at EA size by just reading it from disk.
1216 * Exceptions are readdir and (missing) directory striping */
1218 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1219 /* No easy way to know how long is the symlink, but it cannot
1220 * be more than PATH_MAX, so we allocate +1 */
1223 /* A special case for fs ROOT: getattr there might fetch
1224 * default EA for entire fs, not just for this dir!
1226 } else if (lu_fid_eq(mdt_object_fid(obj),
1227 &info->mti_mdt->mdt_md_root_fid) &&
1228 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1229 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1231 /* Should the default strping be bigger, mdt_fix_reply
1232 * will reallocate */
1233 rc = DEF_REP_MD_SIZE;
1235 /* Read the actual EA size from disk */
1236 rc = mdt_attr_get_eabuf_size(info, obj);
1240 GOTO(out, rc = err_serious(rc));
1242 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1244 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1245 * by default. If the target object has more ACL entries, then
1246 * enlarge the buffer when necessary. */
1247 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1248 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1250 rc = req_capsule_server_pack(pill);
1251 if (unlikely(rc != 0))
1252 GOTO(out, rc = err_serious(rc));
1254 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1255 LASSERT(repbody != NULL);
1256 repbody->mbo_eadatasize = 0;
1257 repbody->mbo_aclsize = 0;
1259 rc = mdt_check_ucred(info);
1261 GOTO(out_shrink, rc);
1263 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1265 rc = mdt_getattr_internal(info, obj, 0);
1268 mdt_client_compatibility(info);
1269 rc2 = mdt_fix_reply(info);
1273 mdt_thread_info_fini(info);
1278 * Handler of layout intent RPC requiring the layout modification
1280 * \param[in] info thread environment
1281 * \param[in] obj object
1282 * \param[in] layout layout intent
1283 * \param[in] buf buffer containing client's lovea, could be empty
1285 * \retval 0 on success
1286 * \retval < 0 error code
1288 static int mdt_layout_change(struct mdt_thread_info *info,
1289 struct mdt_object *obj,
1290 struct layout_intent *layout,
1291 const struct lu_buf *buf)
1293 struct mdt_lock_handle *lh = &info->mti_lh[MDT_LH_LOCAL];
1297 CDEBUG(D_INFO, "got layout change request from client: "
1298 "opc:%u flags:%#x extent[%#llx,%#llx)\n",
1299 layout->li_opc, layout->li_flags,
1300 layout->li_start, layout->li_end);
1301 if (layout->li_start >= layout->li_end) {
1302 CERROR("Recieved an invalid layout change range [%llu, %llu) "
1303 "for "DFID"\n", layout->li_start, layout->li_end,
1304 PFID(mdt_object_fid(obj)));
1308 if (!S_ISREG(lu_object_attr(&obj->mot_obj)))
1309 GOTO(out, rc = -EINVAL);
1311 rc = mo_permission(info->mti_env, NULL, mdt_object_child(obj), NULL,
1316 /* take layout lock to prepare layout change */
1317 mdt_lock_reg_init(lh, LCK_EX);
1318 rc = mdt_object_lock(info, obj, lh,
1319 MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
1323 rc = mo_layout_change(info->mti_env, mdt_object_child(obj), layout,
1326 mdt_object_unlock(info, obj, lh, 1);
1332 * Exchange MOF_LOV_CREATED flags between two objects after a
1333 * layout swap. No assumption is made on whether o1 or o2 have
1334 * created objects or not.
1336 * \param[in,out] o1 First swap layout object
1337 * \param[in,out] o2 Second swap layout object
1339 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1341 unsigned int o1_lov_created = o1->mot_lov_created;
1343 mutex_lock(&o1->mot_lov_mutex);
1344 mutex_lock(&o2->mot_lov_mutex);
1346 o1->mot_lov_created = o2->mot_lov_created;
1347 o2->mot_lov_created = o1_lov_created;
1349 mutex_unlock(&o2->mot_lov_mutex);
1350 mutex_unlock(&o1->mot_lov_mutex);
1353 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1355 struct mdt_thread_info *info;
1356 struct ptlrpc_request *req = tgt_ses_req(tsi);
1357 struct obd_export *exp = req->rq_export;
1358 struct mdt_object *o1, *o2, *o;
1359 struct mdt_lock_handle *lh1, *lh2;
1360 struct mdc_swap_layouts *msl;
1364 /* client does not support layout lock, so layout swaping
1366 * FIXME: there is a problem for old clients which don't support
1367 * layout lock yet. If those clients have already opened the file
1368 * they won't be notified at all so that old layout may still be
1369 * used to do IO. This can be fixed after file release is landed by
1370 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1371 if (!exp_connect_layout(exp))
1372 RETURN(-EOPNOTSUPP);
1374 info = tsi2mdt_info(tsi);
1376 if (info->mti_dlm_req != NULL)
1377 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1379 o1 = info->mti_object;
1380 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1381 &info->mti_body->mbo_fid2);
1383 GOTO(out, rc = PTR_ERR(o));
1385 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1386 GOTO(put, rc = -ENOENT);
1388 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1389 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1395 /* permission check. Make sure the calling process having permission
1396 * to write both files. */
1397 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1402 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1407 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1409 GOTO(put, rc = -EPROTO);
1411 lh1 = &info->mti_lh[MDT_LH_NEW];
1412 mdt_lock_reg_init(lh1, LCK_EX);
1413 lh2 = &info->mti_lh[MDT_LH_OLD];
1414 mdt_lock_reg_init(lh2, LCK_EX);
1416 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1417 MDS_INODELOCK_XATTR);
1421 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1422 MDS_INODELOCK_XATTR);
1426 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1427 mdt_object_child(o2), msl->msl_flags);
1431 mdt_swap_lov_flag(o1, o2);
1434 mdt_object_unlock(info, o2, lh2, rc);
1436 mdt_object_unlock(info, o1, lh1, rc);
1438 mdt_object_put(info->mti_env, o);
1440 mdt_thread_info_fini(info);
1444 static int mdt_raw_lookup(struct mdt_thread_info *info,
1445 struct mdt_object *parent,
1446 const struct lu_name *lname,
1447 struct ldlm_reply *ldlm_rep)
1449 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1453 LASSERT(!info->mti_cross_ref);
1455 /* Only got the fid of this obj by name */
1456 fid_zero(child_fid);
1457 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1458 lname, child_fid, &info->mti_spec);
1460 struct mdt_body *repbody;
1462 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1463 repbody->mbo_fid1 = *child_fid;
1464 repbody->mbo_valid = OBD_MD_FLID;
1465 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1466 } else if (rc == -ENOENT) {
1467 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1474 * UPDATE lock should be taken against parent, and be released before exit;
1475 * child_bits lock should be taken against child, and be returned back:
1476 * (1)normal request should release the child lock;
1477 * (2)intent request will grant the lock to client.
1479 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1480 struct mdt_lock_handle *lhc,
1482 struct ldlm_reply *ldlm_rep)
1484 struct ptlrpc_request *req = mdt_info_req(info);
1485 struct mdt_body *reqbody = NULL;
1486 struct mdt_object *parent = info->mti_object;
1487 struct mdt_object *child;
1488 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1489 struct lu_name *lname = NULL;
1490 struct mdt_lock_handle *lhp = NULL;
1491 struct ldlm_lock *lock;
1498 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1499 LASSERT(ergo(is_resent,
1500 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1505 if (info->mti_cross_ref) {
1506 /* Only getattr on the child. Parent is on another node. */
1507 mdt_set_disposition(info, ldlm_rep,
1508 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1510 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1512 PFID(mdt_object_fid(child)), ldlm_rep);
1514 rc = mdt_check_resent_lock(info, child, lhc);
1517 } else if (rc > 0) {
1518 mdt_lock_handle_init(lhc);
1519 mdt_lock_reg_init(lhc, LCK_PR);
1522 * Object's name is on another MDS, no lookup or layout
1523 * lock is needed here but update lock is.
1525 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1526 MDS_INODELOCK_LAYOUT);
1527 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1529 rc = mdt_object_lock(info, child, lhc, child_bits);
1534 /* Finally, we can get attr for child. */
1535 if (!mdt_object_exists(child)) {
1536 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1538 "remote object doesn't exist.");
1539 mdt_object_unlock(info, child, lhc, 1);
1543 rc = mdt_getattr_internal(info, child, 0);
1544 if (unlikely(rc != 0))
1545 mdt_object_unlock(info, child, lhc, 1);
1550 lname = &info->mti_name;
1551 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1553 if (lu_name_is_valid(lname)) {
1554 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1555 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1556 PNAME(lname), ldlm_rep);
1558 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1559 if (unlikely(reqbody == NULL))
1560 RETURN(err_serious(-EPROTO));
1562 *child_fid = reqbody->mbo_fid2;
1564 if (unlikely(!fid_is_sane(child_fid)))
1565 RETURN(err_serious(-EINVAL));
1567 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1569 PFID(mdt_object_fid(parent)),
1570 PFID(&reqbody->mbo_fid2), ldlm_rep);
1573 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1575 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1576 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1578 "Parent doesn't exist!");
1582 if (mdt_object_remote(parent)) {
1583 CERROR("%s: parent "DFID" is on remote target\n",
1584 mdt_obd_name(info->mti_mdt),
1585 PFID(mdt_object_fid(parent)));
1589 if (lu_name_is_valid(lname)) {
1590 /* Always allow to lookup ".." */
1591 if (unlikely(lname->ln_namelen == 2 &&
1592 lname->ln_name[0] == '.' &&
1593 lname->ln_name[1] == '.'))
1594 info->mti_spec.sp_permitted = 1;
1596 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1597 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1602 /* step 1: lock parent only if parent is a directory */
1603 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1604 lhp = &info->mti_lh[MDT_LH_PARENT];
1605 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1606 rc = mdt_object_lock(info, parent, lhp,
1607 MDS_INODELOCK_UPDATE);
1608 if (unlikely(rc != 0))
1612 /* step 2: lookup child's fid by name */
1613 fid_zero(child_fid);
1614 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1615 child_fid, &info->mti_spec);
1617 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1620 GOTO(out_parent, rc);
1623 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1626 *step 3: find the child object by fid & lock it.
1627 * regardless if it is local or remote.
1629 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1630 * set parent dir fid the same as child fid in getattr by fid case
1631 * we should not lu_object_find() the object again, could lead
1632 * to hung if there is a concurrent unlink destroyed the object.
1634 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1635 mdt_object_get(info->mti_env, parent);
1638 child = mdt_object_find(info->mti_env, info->mti_mdt,
1642 if (unlikely(IS_ERR(child)))
1643 GOTO(out_parent, rc = PTR_ERR(child));
1645 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1646 if (!mdt_object_exists(child)) {
1647 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1649 "Object doesn't exist!");
1650 GOTO(out_child, rc = -ENOENT);
1653 rc = mdt_check_resent_lock(info, child, lhc);
1655 GOTO(out_child, rc);
1656 } else if (rc > 0) {
1657 mdt_lock_handle_init(lhc);
1658 mdt_lock_reg_init(lhc, LCK_PR);
1661 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1662 mdt_object_exists(child) && !mdt_object_remote(child)) {
1663 struct md_attr *ma = &info->mti_attr;
1666 ma->ma_need = MA_INODE;
1667 rc = mdt_attr_get_complex(info, child, ma);
1668 if (unlikely(rc != 0))
1669 GOTO(out_child, rc);
1671 /* If the file has not been changed for some time, we
1672 * return not only a LOOKUP lock, but also an UPDATE
1673 * lock and this might save us RPC on later STAT. For
1674 * directories, it also let negative dentry cache start
1675 * working for this dir. */
1676 if (ma->ma_valid & MA_INODE &&
1677 ma->ma_attr.la_valid & LA_CTIME &&
1678 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1679 ma->ma_attr.la_ctime < cfs_time_current_sec())
1680 child_bits |= MDS_INODELOCK_UPDATE;
1683 /* layout lock must be granted in a best-effort way
1684 * for IT operations */
1685 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1686 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1687 exp_connect_layout(info->mti_exp) &&
1688 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1689 !mdt_object_remote(child) && ldlm_rep != NULL) {
1690 /* try to grant layout lock for regular file. */
1696 child_bits |= MDS_INODELOCK_LAYOUT;
1697 /* try layout lock, it may fail to be granted due to
1698 * contention at LOOKUP or UPDATE */
1699 if (!mdt_object_lock_try(info, child, lhc,
1701 child_bits &= ~MDS_INODELOCK_LAYOUT;
1702 LASSERT(child_bits != 0);
1703 rc = mdt_object_lock(info, child, lhc,
1709 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1710 * client will enqueue the lock to the remote MDT */
1711 if (mdt_object_remote(child))
1712 child_bits &= ~MDS_INODELOCK_UPDATE;
1713 rc = mdt_object_lock(info, child, lhc, child_bits);
1715 if (unlikely(rc != 0))
1716 GOTO(out_child, rc);
1719 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1721 /* finally, we can get attr for child. */
1722 rc = mdt_getattr_internal(info, child, ma_need);
1723 if (unlikely(rc != 0)) {
1724 mdt_object_unlock(info, child, lhc, 1);
1726 /* Debugging code. */
1727 LDLM_DEBUG(lock, "Returning lock to client");
1728 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1729 &lock->l_resource->lr_name),
1730 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1731 PLDLMRES(lock->l_resource),
1732 PFID(mdt_object_fid(child)));
1735 LDLM_LOCK_PUT(lock);
1739 mdt_object_put(info->mti_env, child);
1742 mdt_object_unlock(info, parent, lhp, 1);
1746 /* normal handler: should release the child lock */
1747 static int mdt_getattr_name(struct tgt_session_info *tsi)
1749 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1750 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1751 struct mdt_body *reqbody;
1752 struct mdt_body *repbody;
1756 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1757 LASSERT(reqbody != NULL);
1758 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1759 LASSERT(repbody != NULL);
1761 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1762 repbody->mbo_eadatasize = 0;
1763 repbody->mbo_aclsize = 0;
1765 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1767 GOTO(out_shrink, rc);
1769 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1770 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1771 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1772 lhc->mlh_reg_lh.cookie = 0;
1774 mdt_exit_ucred(info);
1777 mdt_client_compatibility(info);
1778 rc2 = mdt_fix_reply(info);
1781 mdt_thread_info_fini(info);
1785 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1786 void *karg, void __user *uarg);
1788 static int mdt_set_info(struct tgt_session_info *tsi)
1790 struct ptlrpc_request *req = tgt_ses_req(tsi);
1793 int keylen, vallen, rc = 0;
1797 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1799 DEBUG_REQ(D_HA, req, "no set_info key");
1800 RETURN(err_serious(-EFAULT));
1803 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1806 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1808 DEBUG_REQ(D_HA, req, "no set_info val");
1809 RETURN(err_serious(-EFAULT));
1812 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1815 /* Swab any part of val you need to here */
1816 if (KEY_IS(KEY_READ_ONLY)) {
1817 spin_lock(&req->rq_export->exp_lock);
1819 *exp_connect_flags_ptr(req->rq_export) |=
1822 *exp_connect_flags_ptr(req->rq_export) &=
1823 ~OBD_CONNECT_RDONLY;
1824 spin_unlock(&req->rq_export->exp_lock);
1825 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1826 struct changelog_setinfo *cs = val;
1828 if (vallen != sizeof(*cs)) {
1829 CERROR("%s: bad changelog_clear setinfo size %d\n",
1830 tgt_name(tsi->tsi_tgt), vallen);
1833 if (ptlrpc_req_need_swab(req)) {
1834 __swab64s(&cs->cs_recno);
1835 __swab32s(&cs->cs_id);
1838 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1840 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1842 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1849 static int mdt_readpage(struct tgt_session_info *tsi)
1851 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1852 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1853 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1854 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1855 struct mdt_body *repbody;
1861 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1862 RETURN(err_serious(-ENOMEM));
1864 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1865 if (repbody == NULL || reqbody == NULL)
1866 RETURN(err_serious(-EFAULT));
1869 * prepare @rdpg before calling lower layers and transfer itself. Here
1870 * reqbody->size contains offset of where to start to read and
1871 * reqbody->nlink contains number bytes to read.
1873 rdpg->rp_hash = reqbody->mbo_size;
1874 if (rdpg->rp_hash != reqbody->mbo_size) {
1875 CERROR("Invalid hash: %#llx != %#llx\n",
1876 rdpg->rp_hash, reqbody->mbo_size);
1880 rdpg->rp_attrs = reqbody->mbo_mode;
1881 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1882 rdpg->rp_attrs |= LUDA_64BITHASH;
1883 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1884 exp_max_brw_size(tsi->tsi_exp));
1885 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1887 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1888 if (rdpg->rp_pages == NULL)
1891 for (i = 0; i < rdpg->rp_npages; ++i) {
1892 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1893 if (rdpg->rp_pages[i] == NULL)
1894 GOTO(free_rdpg, rc = -ENOMEM);
1897 /* call lower layers to fill allocated pages with directory data */
1898 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1900 GOTO(free_rdpg, rc);
1902 /* send pages to client */
1903 rc = tgt_sendpage(tsi, rdpg, rc);
1908 for (i = 0; i < rdpg->rp_npages; i++)
1909 if (rdpg->rp_pages[i] != NULL)
1910 __free_page(rdpg->rp_pages[i]);
1911 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1913 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1919 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1921 struct lu_ucred *uc = mdt_ucred_check(info);
1922 struct lu_attr *attr = &info->mti_attr.ma_attr;
1927 if (op != REINT_SETATTR) {
1928 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1929 attr->la_uid = uc->uc_fsuid;
1930 /* for S_ISGID, inherit gid from his parent, such work will be
1931 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1932 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1933 attr->la_gid = uc->uc_fsgid;
1939 static int mdt_reint_internal(struct mdt_thread_info *info,
1940 struct mdt_lock_handle *lhc,
1943 struct req_capsule *pill = info->mti_pill;
1944 struct mdt_body *repbody;
1949 rc = mdt_reint_unpack(info, op);
1951 CERROR("Can't unpack reint, rc %d\n", rc);
1952 RETURN(err_serious(rc));
1955 /* for replay (no_create) lmm is not needed, client has it already */
1956 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1957 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1960 /* llog cookies are always 0, the field is kept for compatibility */
1961 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1962 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1964 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
1965 * by default. If the target object has more ACL entries, then
1966 * enlarge the buffer when necessary. */
1967 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
1968 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
1969 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
1971 rc = req_capsule_server_pack(pill);
1973 CERROR("Can't pack response, rc %d\n", rc);
1974 RETURN(err_serious(rc));
1977 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1978 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1980 repbody->mbo_eadatasize = 0;
1981 repbody->mbo_aclsize = 0;
1984 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1986 /* for replay no cookkie / lmm need, because client have this already */
1987 if (info->mti_spec.no_create)
1988 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1989 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1991 rc = mdt_init_ucred_reint(info);
1993 GOTO(out_shrink, rc);
1995 rc = mdt_fix_attr_ucred(info, op);
1997 GOTO(out_ucred, rc = err_serious(rc));
1999 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
2001 GOTO(out_ucred, rc);
2002 } else if (rc == 1) {
2003 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
2004 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
2005 GOTO(out_ucred, rc);
2007 rc = mdt_reint_rec(info, lhc);
2010 mdt_exit_ucred(info);
2012 mdt_client_compatibility(info);
2013 rc2 = mdt_fix_reply(info);
2019 static long mdt_reint_opcode(struct ptlrpc_request *req,
2020 const struct req_format **fmt)
2022 struct mdt_device *mdt;
2023 struct mdt_rec_reint *rec;
2026 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
2028 opc = rec->rr_opcode;
2029 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
2030 if (opc < REINT_MAX && fmt[opc] != NULL)
2031 req_capsule_extend(&req->rq_pill, fmt[opc]);
2033 mdt = mdt_exp2dev(req->rq_export);
2034 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
2035 " rc = %d\n", req->rq_export->exp_obd->obd_name,
2036 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
2037 opc = err_serious(-EFAULT);
2040 opc = err_serious(-EFAULT);
2045 static int mdt_reint(struct tgt_session_info *tsi)
2049 static const struct req_format *reint_fmts[REINT_MAX] = {
2050 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
2051 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
2052 [REINT_LINK] = &RQF_MDS_REINT_LINK,
2053 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
2054 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
2055 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
2056 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
2057 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
2058 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
2063 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
2065 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2067 * No lock possible here from client to pass it to reint code
2070 rc = mdt_reint_internal(info, NULL, opc);
2071 mdt_thread_info_fini(info);
2076 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
2080 /* this should sync the whole device */
2081 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
2083 struct dt_device *dt = mdt->mdt_bottom;
2087 rc = dt->dd_ops->dt_sync(env, dt);
2091 /* this should sync this object */
2092 static int mdt_object_sync(struct mdt_thread_info *info)
2094 struct md_object *next;
2098 if (!mdt_object_exists(info->mti_object)) {
2099 CWARN("%s: non existing object "DFID": rc = %d\n",
2100 mdt_obd_name(info->mti_mdt),
2101 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2104 next = mdt_object_child(info->mti_object);
2105 rc = mo_object_sync(info->mti_env, next);
2110 static int mdt_sync(struct tgt_session_info *tsi)
2112 struct ptlrpc_request *req = tgt_ses_req(tsi);
2113 struct req_capsule *pill = tsi->tsi_pill;
2114 struct mdt_body *body;
2119 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2120 RETURN(err_serious(-ENOMEM));
2122 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2123 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2125 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2127 /* sync an object */
2128 rc = mdt_object_sync(info);
2130 const struct lu_fid *fid;
2131 struct lu_attr *la = &info->mti_attr.ma_attr;
2133 info->mti_attr.ma_need = MA_INODE;
2134 info->mti_attr.ma_valid = 0;
2135 rc = mdt_attr_get_complex(info, info->mti_object,
2138 body = req_capsule_server_get(pill,
2140 fid = mdt_object_fid(info->mti_object);
2141 mdt_pack_attr2body(info, body, la, fid);
2144 mdt_thread_info_fini(info);
2147 mdt_counter_incr(req, LPROC_MDT_SYNC);
2153 * Handle quota control requests to consult current usage/limit, but also
2154 * to configure quota enforcement
2156 static int mdt_quotactl(struct tgt_session_info *tsi)
2158 struct obd_export *exp = tsi->tsi_exp;
2159 struct req_capsule *pill = tsi->tsi_pill;
2160 struct obd_quotactl *oqctl, *repoqc;
2162 struct mdt_device *mdt = mdt_exp2dev(exp);
2163 struct lu_device *qmt = mdt->mdt_qmt_dev;
2164 struct lu_nodemap *nodemap;
2167 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2169 RETURN(err_serious(-EPROTO));
2171 rc = req_capsule_server_pack(pill);
2173 RETURN(err_serious(rc));
2175 nodemap = nodemap_get_from_exp(exp);
2176 if (IS_ERR(nodemap))
2177 RETURN(PTR_ERR(nodemap));
2179 switch (oqctl->qc_cmd) {
2180 /* master quotactl */
2183 if (!nodemap_can_setquota(nodemap))
2184 GOTO(out_nodemap, rc = -EPERM);
2188 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2189 /* slave quotactl */
2194 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2195 GOTO(out_nodemap, rc = -EFAULT);
2199 switch (oqctl->qc_type) {
2201 id = nodemap_map_id(nodemap, NODEMAP_UID,
2202 NODEMAP_CLIENT_TO_FS, id);
2205 id = nodemap_map_id(nodemap, NODEMAP_GID,
2206 NODEMAP_CLIENT_TO_FS, id);
2209 /* todo: check/map project id */
2213 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2215 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2217 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2219 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA)
2220 barrier_exit(tsi->tsi_tgt->lut_bottom);
2222 if (oqctl->qc_id != id)
2223 swap(oqctl->qc_id, id);
2225 if (oqctl->qc_cmd == Q_SETINFO || oqctl->qc_cmd == Q_SETQUOTA) {
2226 if (unlikely(!barrier_entry(tsi->tsi_tgt->lut_bottom)))
2227 RETURN(-EINPROGRESS);
2230 switch (oqctl->qc_cmd) {
2236 /* forward quotactl request to QMT */
2237 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2242 /* slave quotactl */
2243 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2248 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2249 GOTO(out_nodemap, rc = -EFAULT);
2252 if (oqctl->qc_id != id)
2253 swap(oqctl->qc_id, id);
2260 nodemap_putref(nodemap);
2265 /** clone llog ctxt from child (mdd)
2266 * This allows remote llog (replicator) access.
2267 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2268 * context was originally set up, or we can handle them directly.
2269 * I choose the latter, but that means I need any llog
2270 * contexts set up by child to be accessable by the mdt. So we clone the
2271 * context into our context list here.
2273 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2276 struct md_device *next = mdt->mdt_child;
2277 struct llog_ctxt *ctxt;
2280 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2283 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2284 if (rc || ctxt == NULL) {
2288 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2290 CERROR("Can't set mdt ctxt %d\n", rc);
2295 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2296 struct mdt_device *mdt, int idx)
2298 struct llog_ctxt *ctxt;
2300 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2303 /* Put once for the get we just did, and once for the clone */
2304 llog_ctxt_put(ctxt);
2305 llog_ctxt_put(ctxt);
2310 * sec context handlers
2312 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2314 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2320 * quota request handlers
2322 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2324 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2325 struct lu_device *qmt = mdt->mdt_qmt_dev;
2330 RETURN(err_serious(-EOPNOTSUPP));
2332 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2336 struct mdt_object *mdt_object_new(const struct lu_env *env,
2337 struct mdt_device *d,
2338 const struct lu_fid *f)
2340 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2341 struct lu_object *o;
2342 struct mdt_object *m;
2345 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2346 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2347 if (unlikely(IS_ERR(o)))
2348 m = (struct mdt_object *)o;
2354 struct mdt_object *mdt_object_find(const struct lu_env *env,
2355 struct mdt_device *d,
2356 const struct lu_fid *f)
2358 struct lu_object *o;
2359 struct mdt_object *m;
2362 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2363 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2364 if (unlikely(IS_ERR(o)))
2365 m = (struct mdt_object *)o;
2373 * Asyncronous commit for mdt device.
2375 * Pass asynchonous commit call down the MDS stack.
2377 * \param env environment
2378 * \param mdt the mdt device
2380 static void mdt_device_commit_async(const struct lu_env *env,
2381 struct mdt_device *mdt)
2383 struct dt_device *dt = mdt->mdt_bottom;
2387 rc = dt->dd_ops->dt_commit_async(env, dt);
2388 if (unlikely(rc != 0))
2389 CWARN("%s: async commit start failed: rc = %d\n",
2390 mdt_obd_name(mdt), rc);
2391 atomic_inc(&mdt->mdt_async_commit_count);
2396 * Mark the lock as "synchonous".
2398 * Mark the lock to deffer transaction commit to the unlock time.
2400 * \param lock the lock to mark as "synchonous"
2402 * \see mdt_is_lock_sync
2403 * \see mdt_save_lock
2405 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2407 lock->l_ast_data = (void*)1;
2411 * Check whehter the lock "synchonous" or not.
2413 * \param lock the lock to check
2414 * \retval 1 the lock is "synchonous"
2415 * \retval 0 the lock isn't "synchronous"
2417 * \see mdt_set_lock_sync
2418 * \see mdt_save_lock
2420 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2422 return lock->l_ast_data != NULL;
2426 * Blocking AST for mdt locks.
2428 * Starts transaction commit if in case of COS lock conflict or
2429 * deffers such a commit to the mdt_save_lock.
2431 * \param lock the lock which blocks a request or cancelling lock
2432 * \param desc unused
2433 * \param data unused
2434 * \param flag indicates whether this cancelling or blocking callback
2436 * \see ldlm_blocking_ast_nocheck
2438 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2439 void *data, int flag)
2441 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2442 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2443 bool commit_async = false;
2447 if (flag == LDLM_CB_CANCELING)
2450 lock_res_and_lock(lock);
2451 if (lock->l_blocking_ast != mdt_blocking_ast) {
2452 unlock_res_and_lock(lock);
2455 /* There is no lock conflict if l_blocking_lock == NULL,
2456 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2457 * when the last reference to a local lock was released */
2458 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2459 lock->l_blocking_lock != NULL) {
2460 if (mdt_cos_is_enabled(mdt)) {
2461 if (lock->l_client_cookie !=
2462 lock->l_blocking_lock->l_client_cookie)
2463 mdt_set_lock_sync(lock);
2464 } else if (mdt_slc_is_enabled(mdt) &&
2465 ldlm_is_cos_incompat(lock->l_blocking_lock)) {
2466 mdt_set_lock_sync(lock);
2468 * we may do extra commit here, but there is a small
2469 * window to miss a commit: lock was unlocked (saved),
2470 * then a conflict lock queued and we come here, but
2471 * REP-ACK not received, so lock was not converted to
2473 * Fortunately this window is quite small, so the
2474 * extra commit should be rare (not to say distributed
2475 * operation is rare too).
2477 commit_async = true;
2479 } else if (lock->l_req_mode == LCK_COS &&
2480 lock->l_blocking_lock != NULL) {
2481 commit_async = true;
2484 rc = ldlm_blocking_ast_nocheck(lock);
2489 rc = lu_env_init(&env, LCT_LOCAL);
2490 if (unlikely(rc != 0))
2491 CWARN("%s: lu_env initialization failed, cannot "
2492 "start asynchronous commit: rc = %d\n",
2495 mdt_device_commit_async(&env, mdt);
2502 * Blocking AST for cross-MDT lock
2504 * Discard lock from uncommitted_slc_locks and cancel it.
2506 * \param lock the lock which blocks a request or cancelling lock
2507 * \param desc unused
2508 * \param data unused
2509 * \param flag indicates whether this cancelling or blocking callback
2510 * \retval 0 on success
2511 * \retval negative number on error
2513 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2514 void *data, int flag)
2520 case LDLM_CB_BLOCKING: {
2521 struct lustre_handle lockh;
2523 ldlm_lock2handle(lock, &lockh);
2524 rc = ldlm_cli_cancel(&lockh,
2525 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2527 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2532 case LDLM_CB_CANCELING: {
2533 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2534 struct mdt_device *mdt =
2535 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
2537 LDLM_DEBUG(lock, "Revoke remote lock\n");
2539 /* discard slc lock here so that it can be cleaned anytime,
2540 * especially for cleanup_resource() */
2541 tgt_discard_slc_lock(&mdt->mdt_lut, lock);
2543 /* once we cache lock, l_ast_data is set to mdt_object */
2544 if (lock->l_ast_data != NULL) {
2545 struct mdt_object *mo = lock->l_ast_data;
2548 rc = lu_env_init(&env, LCT_MD_THREAD);
2549 if (unlikely(rc != 0)) {
2550 CWARN("%s: lu_env initialization failed, object"
2551 "%p "DFID" is leaked!\n",
2553 PFID(mdt_object_fid(mo)));
2557 if (lock->l_policy_data.l_inodebits.bits &
2558 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2559 rc = mo_invalidate(&env, mdt_object_child(mo));
2560 mo->mot_cache_attr = 0;
2562 mdt_object_put(&env, mo);
2574 int mdt_check_resent_lock(struct mdt_thread_info *info,
2575 struct mdt_object *mo,
2576 struct mdt_lock_handle *lhc)
2578 /* the lock might already be gotten in ldlm_handle_enqueue() */
2579 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2580 struct ptlrpc_request *req = mdt_info_req(info);
2581 struct ldlm_lock *lock;
2583 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2584 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2586 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2587 * a resend case, however, it could be already destroyed
2588 * due to client eviction or a raced cancel RPC. */
2589 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2590 lhc->mlh_reg_lh.cookie);
2594 if (!fid_res_name_eq(mdt_object_fid(mo),
2595 &lock->l_resource->lr_name)) {
2596 CWARN("%s: Although resent, but still not "
2597 "get child lock:"DFID"\n",
2598 info->mti_exp->exp_obd->obd_name,
2599 PFID(mdt_object_fid(mo)));
2600 LDLM_LOCK_PUT(lock);
2603 LDLM_LOCK_PUT(lock);
2609 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2610 const struct lu_fid *fid, struct lustre_handle *lh,
2611 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2614 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2615 union ldlm_policy_data *policy = &mti->mti_policy;
2616 struct ldlm_res_id *res_id = &mti->mti_res_id;
2620 LASSERT(mdt_object_remote(o));
2622 fid_build_reg_res_name(fid, res_id);
2624 memset(einfo, 0, sizeof(*einfo));
2625 einfo->ei_type = LDLM_IBITS;
2626 einfo->ei_mode = mode;
2627 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2628 einfo->ei_cb_cp = ldlm_completion_ast;
2629 einfo->ei_enq_slave = 0;
2630 einfo->ei_res_id = res_id;
2632 einfo->ei_nonblock = 1;
2635 * if we cache lock, couple lock with mdt_object, so that object
2636 * can be easily found in lock ASTs.
2638 mdt_object_get(mti->mti_env, o);
2639 einfo->ei_cbdata = o;
2642 memset(policy, 0, sizeof(*policy));
2643 policy->l_inodebits.bits = ibits;
2645 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2647 if (rc < 0 && cache) {
2648 mdt_object_put(mti->mti_env, o);
2649 einfo->ei_cbdata = NULL;
2654 static int mdt_object_local_lock(struct mdt_thread_info *info,
2655 struct mdt_object *o,
2656 struct mdt_lock_handle *lh, __u64 ibits,
2657 bool nonblock, bool cos_incompat)
2659 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2660 union ldlm_policy_data *policy = &info->mti_policy;
2661 struct ldlm_res_id *res_id = &info->mti_res_id;
2666 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2667 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2668 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2669 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2672 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2673 lh->mlh_reg_mode == LCK_EX);
2674 dlmflags |= LDLM_FL_COS_INCOMPAT;
2675 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2676 dlmflags |= LDLM_FL_COS_ENABLED;
2679 /* Only enqueue LOOKUP lock for remote object */
2680 if (mdt_object_remote(o))
2681 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2683 if (lh->mlh_type == MDT_PDO_LOCK) {
2684 /* check for exists after object is locked */
2685 if (mdt_object_exists(o) == 0) {
2686 /* Non-existent object shouldn't have PDO lock */
2689 /* Non-dir object shouldn't have PDO lock */
2690 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2695 memset(policy, 0, sizeof(*policy));
2696 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2698 dlmflags |= LDLM_FL_ATOMIC_CB;
2700 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2703 * Take PDO lock on whole directory and build correct @res_id for lock
2704 * on part of directory.
2706 if (lh->mlh_pdo_hash != 0) {
2707 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2708 mdt_lock_pdo_mode(info, o, lh);
2709 if (lh->mlh_pdo_mode != LCK_NL) {
2711 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2712 * is never going to be sent to client and we do not
2713 * want it slowed down due to possible cancels.
2715 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2716 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2717 policy, res_id, dlmflags,
2718 info->mti_exp == NULL ? NULL :
2719 &info->mti_exp->exp_handle.h_cookie);
2720 if (unlikely(rc != 0))
2721 GOTO(out_unlock, rc);
2725 * Finish res_id initializing by name hash marking part of
2726 * directory which is taking modification.
2728 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2731 policy->l_inodebits.bits = ibits;
2734 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2735 * going to be sent to client. If it is - mdt_intent_policy() path will
2736 * fix it up and turn FL_LOCAL flag off.
2738 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2739 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2740 info->mti_exp == NULL ? NULL :
2741 &info->mti_exp->exp_handle.h_cookie);
2744 mdt_object_unlock(info, o, lh, 1);
2745 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2746 lh->mlh_pdo_hash != 0 &&
2747 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2748 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2754 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2755 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2758 struct mdt_lock_handle *local_lh = NULL;
2762 if (!mdt_object_remote(o)) {
2763 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2768 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2769 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2770 MDS_INODELOCK_XATTR);
2772 /* Only enqueue LOOKUP lock for remote object */
2773 if (ibits & MDS_INODELOCK_LOOKUP) {
2774 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2775 nonblock, cos_incompat);
2782 if (ibits & MDS_INODELOCK_UPDATE) {
2783 /* Sigh, PDO needs to enqueue 2 locks right now, but
2784 * enqueue RPC can only request 1 lock, to avoid extra
2785 * RPC, so it will instead enqueue EX lock for remote
2786 * object anyway XXX*/
2787 if (lh->mlh_type == MDT_PDO_LOCK &&
2788 lh->mlh_pdo_hash != 0) {
2789 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2790 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2791 PFID(mdt_object_fid(o)));
2792 lh->mlh_pdo_hash = 0;
2793 lh->mlh_rreg_mode = LCK_EX;
2794 lh->mlh_type = MDT_REG_LOCK;
2796 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2799 MDS_INODELOCK_UPDATE, nonblock,
2801 if (rc != ELDLM_OK) {
2802 if (local_lh != NULL)
2803 mdt_object_unlock(info, o, local_lh, rc);
2811 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2812 struct mdt_lock_handle *lh, __u64 ibits)
2814 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2817 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2818 struct mdt_lock_handle *lh, __u64 ibits,
2821 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2822 return mdt_object_lock_internal(info, o, lh, ibits, false,
2826 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2827 struct mdt_lock_handle *lh, __u64 ibits)
2829 struct mdt_lock_handle tmp = *lh;
2832 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2839 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2840 struct mdt_object *o, struct mdt_lock_handle *lh,
2841 __u64 ibits, bool cos_incompat)
2843 struct mdt_lock_handle tmp = *lh;
2846 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2847 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2855 * Save a lock within request object.
2857 * Keep the lock referenced until whether client ACK or transaction
2858 * commit happens or release the lock immediately depending on input
2859 * parameters. If COS is ON, a write lock is converted to COS lock
2862 * \param info thead info object
2863 * \param h lock handle
2864 * \param mode lock mode
2865 * \param decref force immediate lock releasing
2867 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2868 enum ldlm_mode mode, int decref)
2872 if (lustre_handle_is_used(h)) {
2873 if (decref || !info->mti_has_trans ||
2874 !(mode & (LCK_PW | LCK_EX))) {
2875 mdt_fid_unlock(h, mode);
2877 struct mdt_device *mdt = info->mti_mdt;
2878 struct ldlm_lock *lock = ldlm_handle2lock(h);
2879 struct ptlrpc_request *req = mdt_info_req(info);
2880 bool cos = mdt_cos_is_enabled(mdt);
2881 bool convert_lock = !cos && mdt_slc_is_enabled(mdt);
2883 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2886 /* there is no request if mdt_object_unlock() is called
2887 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2888 if (likely(req != NULL)) {
2889 LDLM_DEBUG(lock, "save lock request %p reply "
2890 "state %p transno %lld\n", req,
2891 req->rq_reply_state, req->rq_transno);
2893 ldlm_lock_downgrade(lock, LCK_COS);
2896 if (req->rq_export->exp_disconnected)
2897 mdt_fid_unlock(h, mode);
2899 ptlrpc_save_lock(req, h, mode, cos,
2902 mdt_fid_unlock(h, mode);
2904 if (mdt_is_lock_sync(lock)) {
2905 CDEBUG(D_HA, "found sync-lock,"
2906 " async commit started\n");
2907 mdt_device_commit_async(info->mti_env,
2910 LDLM_LOCK_PUT(lock);
2919 * Save cross-MDT lock in uncommitted_slc_locks
2921 * Keep the lock referenced until transaction commit happens or release the lock
2922 * immediately depending on input parameters.
2924 * \param info thead info object
2925 * \param h lock handle
2926 * \param mode lock mode
2927 * \param decref force immediate lock releasing
2929 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2930 struct mdt_object *o, struct lustre_handle *h,
2931 enum ldlm_mode mode, int decref)
2935 if (lustre_handle_is_used(h)) {
2936 struct ldlm_lock *lock = ldlm_handle2lock(h);
2939 (lock->l_policy_data.l_inodebits.bits &
2940 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2941 mo_invalidate(info->mti_env, mdt_object_child(o));
2943 if (decref || !info->mti_has_trans ||
2944 !(mode & (LCK_PW | LCK_EX))) {
2945 ldlm_lock_decref_and_cancel(h, mode);
2946 LDLM_LOCK_PUT(lock);
2948 struct ptlrpc_request *req = mdt_info_req(info);
2950 LASSERT(req != NULL);
2951 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2953 ldlm_lock_decref(h, mode);
2962 * Unlock mdt object.
2964 * Immeditely release the regular lock and the PDO lock or save the
2965 * lock in request and keep them referenced until client ACK or
2966 * transaction commit.
2968 * \param info thread info object
2969 * \param o mdt object
2970 * \param lh mdt lock handle referencing regular and PDO locks
2971 * \param decref force immediate lock releasing
2973 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
2975 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2976 struct mdt_lock_handle *lh, int decref)
2980 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2981 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2982 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2988 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2989 const struct lu_fid *f,
2990 struct mdt_lock_handle *lh,
2993 struct mdt_object *o;
2995 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2999 rc = mdt_object_lock(info, o, lh, ibits);
3001 mdt_object_put(info->mti_env, o);
3008 void mdt_object_unlock_put(struct mdt_thread_info * info,
3009 struct mdt_object * o,
3010 struct mdt_lock_handle *lh,
3013 mdt_object_unlock(info, o, lh, decref);
3014 mdt_object_put(info->mti_env, o);
3018 * Generic code handling requests that have struct mdt_body passed in:
3020 * - extract mdt_body from request and save it in @info, if present;
3022 * - create lu_object, corresponding to the fid in mdt_body, and save it in
3025 * - if HABEO_CORPUS flag is set for this request type check whether object
3026 * actually exists on storage (lu_object_exists()).
3029 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
3031 const struct mdt_body *body;
3032 struct mdt_object *obj;
3033 const struct lu_env *env;
3034 struct req_capsule *pill;
3038 env = info->mti_env;
3039 pill = info->mti_pill;
3041 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
3045 if (!(body->mbo_valid & OBD_MD_FLID))
3048 if (!fid_is_sane(&body->mbo_fid1)) {
3049 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
3053 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
3055 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
3056 mdt_object_put(env, obj);
3059 info->mti_object = obj;
3068 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
3070 struct req_capsule *pill = info->mti_pill;
3074 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
3075 rc = mdt_body_unpack(info, flags);
3079 if (rc == 0 && (flags & HABEO_REFERO)) {
3081 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
3082 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
3084 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
3085 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
3088 /* Set ACL reply buffer size as LUSTRE_POSIX_ACL_MAX_SIZE_OLD
3089 * by default. If the target object has more ACL entries, then
3090 * enlarge the buffer when necessary. */
3091 if (req_capsule_has_field(pill, &RMF_ACL, RCL_SERVER))
3092 req_capsule_set_size(pill, &RMF_ACL, RCL_SERVER,
3093 LUSTRE_POSIX_ACL_MAX_SIZE_OLD);
3095 rc = req_capsule_server_pack(pill);
3100 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
3102 lh->mlh_type = MDT_NUL_LOCK;
3103 lh->mlh_reg_lh.cookie = 0ull;
3104 lh->mlh_reg_mode = LCK_MINMODE;
3105 lh->mlh_pdo_lh.cookie = 0ull;
3106 lh->mlh_pdo_mode = LCK_MINMODE;
3107 lh->mlh_rreg_lh.cookie = 0ull;
3108 lh->mlh_rreg_mode = LCK_MINMODE;
3111 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
3113 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
3114 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
3118 * Initialize fields of struct mdt_thread_info. Other fields are left in
3119 * uninitialized state, because it's too expensive to zero out whole
3120 * mdt_thread_info (> 1K) on each request arrival.
3122 void mdt_thread_info_init(struct ptlrpc_request *req,
3123 struct mdt_thread_info *info)
3127 info->mti_pill = &req->rq_pill;
3130 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3131 mdt_lock_handle_init(&info->mti_lh[i]);
3133 /* mdt device: it can be NULL while CONNECT */
3134 if (req->rq_export) {
3135 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
3136 info->mti_exp = req->rq_export;
3138 info->mti_mdt = NULL;
3139 info->mti_env = req->rq_svc_thread->t_env;
3140 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
3142 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
3143 info->mti_big_buf = LU_BUF_NULL;
3144 info->mti_body = NULL;
3145 info->mti_object = NULL;
3146 info->mti_dlm_req = NULL;
3147 info->mti_has_trans = 0;
3148 info->mti_cross_ref = 0;
3149 info->mti_opdata = 0;
3150 info->mti_big_lmm_used = 0;
3151 info->mti_big_acl_used = 0;
3153 info->mti_spec.no_create = 0;
3154 info->mti_spec.sp_rm_entry = 0;
3155 info->mti_spec.sp_permitted = 0;
3156 info->mti_spec.sp_migrate_close = 0;
3158 info->mti_spec.u.sp_ea.eadata = NULL;
3159 info->mti_spec.u.sp_ea.eadatalen = 0;
3162 void mdt_thread_info_fini(struct mdt_thread_info *info)
3166 if (info->mti_object != NULL) {
3167 mdt_object_put(info->mti_env, info->mti_object);
3168 info->mti_object = NULL;
3171 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3172 mdt_lock_handle_fini(&info->mti_lh[i]);
3173 info->mti_env = NULL;
3174 info->mti_pill = NULL;
3175 info->mti_exp = NULL;
3177 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3178 lu_buf_free(&info->mti_big_buf);
3181 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3183 struct mdt_thread_info *mti;
3185 mti = mdt_th_info(tsi->tsi_env);
3186 LASSERT(mti != NULL);
3188 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3189 if (tsi->tsi_corpus != NULL) {
3190 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3191 lu_object_get(tsi->tsi_corpus);
3193 mti->mti_body = tsi->tsi_mdt_body;
3194 mti->mti_dlm_req = tsi->tsi_dlm_req;
3199 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3201 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3203 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3204 set_current_state(TASK_UNINTERRUPTIBLE);
3205 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3208 return tgt_connect(tsi);
3226 static int mdt_intent_getattr(enum mdt_it_code opcode,
3227 struct mdt_thread_info *info,
3228 struct ldlm_lock **,
3231 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3232 struct mdt_thread_info *info,
3233 struct ldlm_lock **lockp,
3236 static int mdt_intent_layout(enum mdt_it_code opcode,
3237 struct mdt_thread_info *info,
3238 struct ldlm_lock **,
3240 static int mdt_intent_reint(enum mdt_it_code opcode,
3241 struct mdt_thread_info *info,
3242 struct ldlm_lock **,
3245 static struct mdt_it_flavor {
3246 const struct req_format *it_fmt;
3248 int (*it_act)(enum mdt_it_code ,
3249 struct mdt_thread_info *,
3250 struct ldlm_lock **,
3253 } mdt_it_flavor[] = {
3255 .it_fmt = &RQF_LDLM_INTENT,
3256 /*.it_flags = HABEO_REFERO,*/
3258 .it_act = mdt_intent_reint,
3259 .it_reint = REINT_OPEN
3262 .it_fmt = &RQF_LDLM_INTENT,
3264 * OCREAT is not a MUTABOR request as if the file
3266 * We do the extra check of OBD_CONNECT_RDONLY in
3267 * mdt_reint_open() when we really need to create
3271 .it_act = mdt_intent_reint,
3272 .it_reint = REINT_OPEN
3275 .it_fmt = &RQF_LDLM_INTENT,
3276 .it_flags = MUTABOR,
3277 .it_act = mdt_intent_reint,
3278 .it_reint = REINT_CREATE
3280 [MDT_IT_GETATTR] = {
3281 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3282 .it_flags = HABEO_REFERO,
3283 .it_act = mdt_intent_getattr
3285 [MDT_IT_READDIR] = {
3291 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3292 .it_flags = HABEO_REFERO,
3293 .it_act = mdt_intent_getattr
3296 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3297 .it_flags = MUTABOR,
3299 .it_reint = REINT_UNLINK
3303 .it_flags = MUTABOR,
3306 [MDT_IT_GETXATTR] = {
3307 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3308 .it_flags = HABEO_CORPUS,
3309 .it_act = mdt_intent_getxattr
3312 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3314 .it_act = mdt_intent_layout
3319 mdt_intent_lock_replace(struct mdt_thread_info *info,
3320 struct ldlm_lock **lockp,
3321 struct mdt_lock_handle *lh,
3322 __u64 flags, int result)
3324 struct ptlrpc_request *req = mdt_info_req(info);
3325 struct ldlm_lock *lock = *lockp;
3326 struct ldlm_lock *new_lock;
3328 /* If possible resent found a lock, @lh is set to its handle */
3329 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3331 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3332 lh->mlh_reg_lh.cookie = 0;
3336 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3337 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3338 * a resend case, however, it could be already destroyed
3339 * due to client eviction or a raced cancel RPC. */
3340 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx\n",
3341 lh->mlh_reg_lh.cookie);
3342 lh->mlh_reg_lh.cookie = 0;
3346 LASSERTF(new_lock != NULL,
3347 "lockh %#llx flags %#llx : rc = %d\n",
3348 lh->mlh_reg_lh.cookie, flags, result);
3351 * If we've already given this lock to a client once, then we should
3352 * have no readers or writers. Otherwise, we should have one reader
3353 * _or_ writer ref (which will be zeroed below) before returning the
3356 if (new_lock->l_export == req->rq_export) {
3357 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3359 LASSERT(new_lock->l_export == NULL);
3360 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3365 if (new_lock->l_export == req->rq_export) {
3367 * Already gave this to the client, which means that we
3368 * reconstructed a reply.
3370 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3373 LDLM_LOCK_RELEASE(new_lock);
3374 lh->mlh_reg_lh.cookie = 0;
3375 RETURN(ELDLM_LOCK_REPLACED);
3379 * Fixup the lock to be given to the client.
3381 lock_res_and_lock(new_lock);
3382 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3383 * possible blocking AST. */
3384 while (new_lock->l_readers > 0) {
3385 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3386 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3387 new_lock->l_readers--;
3389 while (new_lock->l_writers > 0) {
3390 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3391 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3392 new_lock->l_writers--;
3395 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3396 new_lock->l_blocking_ast = lock->l_blocking_ast;
3397 new_lock->l_completion_ast = lock->l_completion_ast;
3398 new_lock->l_remote_handle = lock->l_remote_handle;
3399 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3401 unlock_res_and_lock(new_lock);
3403 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3404 &new_lock->l_remote_handle,
3405 &new_lock->l_exp_hash);
3407 LDLM_LOCK_RELEASE(new_lock);
3408 lh->mlh_reg_lh.cookie = 0;
3410 RETURN(ELDLM_LOCK_REPLACED);
3413 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3414 struct ldlm_lock *new_lock,
3415 struct mdt_lock_handle *lh,
3418 struct ptlrpc_request *req = mdt_info_req(info);
3419 struct ldlm_request *dlmreq;
3421 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3424 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3426 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3427 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3429 if (flags & LDLM_FL_RESENT) {
3430 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3431 lh->mlh_reg_mode = new_lock->l_granted_mode;
3433 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3434 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie %#llx",
3435 lh->mlh_reg_lh.cookie);
3440 * If the xid matches, then we know this is a resent request, and allow
3441 * it. (It's probably an OPEN, for which we don't send a lock.
3443 if (req_can_reconstruct(req, NULL))
3447 * This remote handle isn't enqueued, so we never received or processed
3448 * this request. Clear MSG_RESENT, because it can be handled like any
3449 * normal request now.
3451 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3453 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle %#llx",
3454 dlmreq->lock_handle[0].cookie);
3457 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3458 struct mdt_thread_info *info,
3459 struct ldlm_lock **lockp,
3462 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3463 struct ldlm_reply *ldlm_rep = NULL;
3468 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3469 * (for the resend case) or a new lock. Below we will use it to
3470 * replace the original lock.
3472 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3473 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3474 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3475 rc = mdt_object_lock(info, info->mti_object, lhc,
3476 MDS_INODELOCK_XATTR);
3481 rc = mdt_getxattr(info);
3483 if (mdt_info_req(info)->rq_repmsg != NULL)
3484 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3486 if (ldlm_rep == NULL ||
3487 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3488 mdt_object_unlock(info, info->mti_object, lhc, 1);
3489 RETURN(err_serious(-EFAULT));
3492 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3494 /* This is left for interop instead of adding a new interop flag.
3496 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3497 if (ldlm_rep->lock_policy_res2) {
3498 mdt_object_unlock(info, info->mti_object, lhc, 1);
3499 RETURN(ELDLM_LOCK_ABORTED);
3503 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3507 static int mdt_intent_getattr(enum mdt_it_code opcode,
3508 struct mdt_thread_info *info,
3509 struct ldlm_lock **lockp,
3512 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3514 struct ldlm_reply *ldlm_rep;
3515 struct mdt_body *reqbody;
3516 struct mdt_body *repbody;
3520 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3523 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3526 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3527 repbody->mbo_eadatasize = 0;
3528 repbody->mbo_aclsize = 0;
3532 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3534 case MDT_IT_GETATTR:
3535 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3539 CERROR("Unsupported intent (%d)\n", opcode);
3540 GOTO(out_shrink, rc = -EINVAL);
3543 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3545 GOTO(out_shrink, rc);
3547 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3548 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3550 /* Get lock from request for possible resent case. */
3551 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3553 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3554 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3556 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3557 ldlm_rep->lock_policy_res2 = 0;
3558 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3559 ldlm_rep->lock_policy_res2) {
3560 lhc->mlh_reg_lh.cookie = 0ull;
3561 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3564 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3567 mdt_exit_ucred(info);
3569 mdt_client_compatibility(info);
3570 rc2 = mdt_fix_reply(info);
3576 static int mdt_intent_layout(enum mdt_it_code opcode,
3577 struct mdt_thread_info *info,
3578 struct ldlm_lock **lockp,
3581 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3582 struct layout_intent *layout;
3584 struct mdt_object *obj = NULL;
3585 bool layout_change = false;
3586 int layout_size = 0;
3590 if (opcode != MDT_IT_LAYOUT) {
3591 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3596 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3600 switch (layout->li_opc) {
3601 case LAYOUT_INTENT_TRUNC:
3602 case LAYOUT_INTENT_WRITE:
3603 layout_change = true;
3605 case LAYOUT_INTENT_ACCESS:
3607 case LAYOUT_INTENT_READ:
3608 case LAYOUT_INTENT_GLIMPSE:
3609 case LAYOUT_INTENT_RELEASE:
3610 case LAYOUT_INTENT_RESTORE:
3611 CERROR("%s: Unsupported layout intent opc %d\n",
3612 mdt_obd_name(info->mti_mdt), layout->li_opc);
3616 CERROR("%s: Unknown layout intent opc %d\n",
3617 mdt_obd_name(info->mti_mdt), layout->li_opc);
3624 fid = &info->mti_tmp_fid2;
3625 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3627 /* Get lock from request for possible resent case. */
3628 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3630 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3632 GOTO(out, rc = PTR_ERR(obj));
3634 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3635 layout_size = mdt_attr_get_eabuf_size(info, obj);
3636 if (layout_size < 0)
3637 GOTO(out_obj, rc = layout_size);
3639 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3640 info->mti_mdt->mdt_max_mdsize = layout_size;
3644 * set reply buffer size, so that ldlm_handle_enqueue0()->
3645 * ldlm_lvbo_fill() will fill the reply buffer with lovea.
3647 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3648 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3650 rc = req_capsule_server_pack(info->mti_pill);
3655 if (layout_change) {
3656 struct lu_buf *buf = &info->mti_buf;
3659 * mdt_layout_change is a reint operation, when the request
3660 * is resent, layout write shouldn't reprocess it again.
3662 rc = mdt_check_resent(info, mdt_reconstruct_generic, lhc);
3664 GOTO(out_obj, rc = rc < 0 ? rc : 0);
3667 * There is another resent case: the client's job has been
3668 * done by another client, referring lod_declare_layout_change
3669 * -EALREADY case, and it became a operation w/o transaction,
3670 * so we should not do the layout change, otherwise
3671 * mdt_layout_change() will try to cancel the granted server
3672 * CR lock whose remote counterpart is still in hold on the
3673 * client, and a deadlock ensues.
3675 rc = mdt_check_resent_lock(info, obj, lhc);
3681 if (unlikely(req_is_replay(mdt_info_req(info)))) {
3682 buf->lb_buf = req_capsule_client_get(info->mti_pill,
3684 buf->lb_len = req_capsule_get_size(info->mti_pill,
3685 &RMF_EADATA, RCL_CLIENT);
3687 * If it's a replay of layout write intent RPC, the
3688 * client has saved the extended lovea when
3689 * it get reply then.
3691 if (buf->lb_len > 0)
3692 mdt_fix_lov_magic(info, buf->lb_buf);
3696 * Instantiate some layout components, if @buf contains
3697 * lovea, then it's a replay of the layout intent write
3700 rc = mdt_layout_change(info, obj, layout, buf);
3705 mdt_object_put(info->mti_env, obj);
3707 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3708 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3711 lhc->mlh_reg_lh.cookie = 0;
3716 static int mdt_intent_reint(enum mdt_it_code opcode,
3717 struct mdt_thread_info *info,
3718 struct ldlm_lock **lockp,
3721 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3722 struct ldlm_reply *rep = NULL;
3726 static const struct req_format *intent_fmts[REINT_MAX] = {
3727 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3728 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3733 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3737 if (mdt_it_flavor[opcode].it_reint != opc) {
3738 CERROR("Reint code %ld doesn't match intent: %d\n",
3740 RETURN(err_serious(-EPROTO));
3743 /* Get lock from request for possible resent case. */
3744 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3746 rc = mdt_reint_internal(info, lhc, opc);
3748 /* Check whether the reply has been packed successfully. */
3749 if (mdt_info_req(info)->rq_repmsg != NULL)
3750 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3752 RETURN(err_serious(-EFAULT));
3754 /* MDC expects this in any case */
3756 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3758 /* the open lock or the lock for cross-ref object should be
3759 * returned to the client */
3760 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3761 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3762 rep->lock_policy_res2 = 0;
3763 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3767 rep->lock_policy_res2 = clear_serious(rc);
3769 if (rep->lock_policy_res2 == -ENOENT &&
3770 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3771 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3772 rep->lock_policy_res2 = 0;
3774 lhc->mlh_reg_lh.cookie = 0ull;
3775 if (rc == -ENOTCONN || rc == -ENODEV ||
3776 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3778 * If it is the disconnect error (ENODEV & ENOCONN), the error
3779 * will be returned by rq_status, and client at ptlrpc layer
3780 * will detect this, then disconnect, reconnect the import
3781 * immediately, instead of impacting the following the rpc.
3786 * For other cases, the error will be returned by intent, and client
3787 * will retrieve the result from intent.
3789 RETURN(ELDLM_LOCK_ABORTED);
3792 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3800 case IT_OPEN|IT_CREAT:
3807 rc = MDT_IT_READDIR;
3810 rc = MDT_IT_GETATTR;
3822 rc = MDT_IT_GETXATTR;
3827 case IT_QUOTA_DQACQ:
3832 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3839 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3840 struct mdt_thread_info *info,
3841 struct ldlm_lock **lockp, __u64 flags)
3843 struct req_capsule *pill = info->mti_pill;
3844 struct ptlrpc_request *req = mdt_info_req(info);
3845 struct mdt_it_flavor *flv;
3850 opc = mdt_intent_code(itopc);
3854 if (opc == MDT_IT_QUOTA) {
3855 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3858 RETURN(-EOPNOTSUPP);
3860 if (mdt_rdonly(req->rq_export))
3863 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3864 /* pass the request to quota master */
3865 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3866 mdt_info_req(info), lockp,
3871 flv = &mdt_it_flavor[opc];
3872 if (flv->it_fmt != NULL)
3873 req_capsule_extend(pill, flv->it_fmt);
3875 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3879 if (flv->it_flags & MUTABOR && mdt_rdonly(req->rq_export))
3882 if (flv->it_act != NULL) {
3883 struct ldlm_reply *rep;
3885 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3887 /* execute policy */
3888 rc = flv->it_act(opc, info, lockp, flags);
3890 /* Check whether the reply has been packed successfully. */
3891 if (req->rq_repmsg != NULL) {
3892 rep = req_capsule_server_get(info->mti_pill,
3894 rep->lock_policy_res2 =
3895 ptlrpc_status_hton(rep->lock_policy_res2);
3902 static int mdt_intent_policy(struct ldlm_namespace *ns,
3903 struct ldlm_lock **lockp, void *req_cookie,
3904 enum ldlm_mode mode, __u64 flags, void *data)
3906 struct tgt_session_info *tsi;
3907 struct mdt_thread_info *info;
3908 struct ptlrpc_request *req = req_cookie;
3909 struct ldlm_intent *it;
3910 struct req_capsule *pill;
3915 LASSERT(req != NULL);
3917 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3919 info = tsi2mdt_info(tsi);
3920 LASSERT(info != NULL);
3921 pill = info->mti_pill;
3922 LASSERT(pill->rc_req == req);
3924 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3925 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3926 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3928 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3932 /* Lock without inodebits makes no sense and will oops
3933 * later in ldlm. Let's check it now to see if we have
3934 * ibits corrupted somewhere in mdt_intent_opc().
3935 * The case for client miss to set ibits has been
3936 * processed by others. */
3937 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3938 lr_type == LDLM_IBITS,
3939 info->mti_dlm_req->lock_desc.\
3940 l_policy_data.l_inodebits.bits != 0));
3942 rc = err_serious(-EFAULT);
3944 /* No intent was provided */
3945 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3946 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3947 rc = req_capsule_server_pack(pill);
3949 rc = err_serious(rc);
3951 mdt_thread_info_fini(info);
3955 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3957 struct seq_server_site *ss = mdt_seq_site(mdt);
3959 if (ss->ss_node_id == 0)
3962 if (ss->ss_client_seq != NULL) {
3963 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3964 ss->ss_client_seq->lcs_exp = NULL;
3967 if (ss->ss_server_fld != NULL) {
3968 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3969 ss->ss_server_fld->lsf_control_exp = NULL;
3973 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3975 struct seq_server_site *ss = mdt_seq_site(mdt);
3980 if (ss->ss_server_seq != NULL)
3981 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3984 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3986 mdt_seq_fini_cli(mdt);
3987 mdt_deregister_seq_exp(mdt);
3989 return seq_site_fini(env, mdt_seq_site(mdt));
3993 * It will retrieve its FLDB entries from MDT0, and it only happens
3994 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3995 * and it needs to refresh FLDB from the MDT0.
3997 static int mdt_register_lwp_callback(void *data)
4000 struct mdt_device *mdt = data;
4001 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
4005 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
4007 rc = lu_env_init(&env, LCT_MD_THREAD);
4009 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
4013 /* Allocate new sequence now to avoid creating local transaction
4014 * in the normal transaction process */
4015 rc = seq_server_check_and_alloc_super(&env,
4016 mdt_seq_site(mdt)->ss_server_seq);
4021 rc = fld_update_from_controller(&env, fld);
4023 CERROR("%s: cannot update controller: rc = %d\n",
4024 mdt_obd_name(mdt), rc);
4033 static int mdt_register_seq_exp(struct mdt_device *mdt)
4035 struct seq_server_site *ss = mdt_seq_site(mdt);
4036 char *lwp_name = NULL;
4039 if (ss->ss_node_id == 0)
4042 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
4043 if (lwp_name == NULL)
4044 GOTO(out_free, rc = -ENOMEM);
4046 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
4050 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
4055 rc = lustre_register_lwp_item(lwp_name,
4056 &ss->ss_server_fld->lsf_control_exp,
4057 mdt_register_lwp_callback, mdt);
4059 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
4060 ss->ss_client_seq->lcs_exp = NULL;
4064 if (lwp_name != NULL)
4065 OBD_FREE(lwp_name, MAX_OBD_NAME);
4071 * Init client sequence manager which is used by local MDS to talk to sequence
4072 * controller on remote node.
4074 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
4076 struct seq_server_site *ss = mdt_seq_site(mdt);
4081 /* check if this is adding the first MDC and controller is not yet
4083 OBD_ALLOC_PTR(ss->ss_client_seq);
4084 if (ss->ss_client_seq == NULL)
4087 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
4088 if (prefix == NULL) {
4089 OBD_FREE_PTR(ss->ss_client_seq);
4090 ss->ss_client_seq = NULL;
4094 /* Note: seq_client_fini will be called in seq_site_fini */
4095 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
4096 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
4097 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
4099 OBD_FREE(prefix, MAX_OBD_NAME + 5);
4101 OBD_FREE_PTR(ss->ss_client_seq);
4102 ss->ss_client_seq = NULL;
4106 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
4111 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
4113 struct seq_server_site *ss;
4117 ss = mdt_seq_site(mdt);
4118 /* init sequence controller server(MDT0) */
4119 if (ss->ss_node_id == 0) {
4120 OBD_ALLOC_PTR(ss->ss_control_seq);
4121 if (ss->ss_control_seq == NULL)
4124 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
4125 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
4128 GOTO(out_seq_fini, rc);
4131 /* Init normal sequence server */
4132 OBD_ALLOC_PTR(ss->ss_server_seq);
4133 if (ss->ss_server_seq == NULL)
4134 GOTO(out_seq_fini, rc = -ENOMEM);
4136 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
4137 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
4139 GOTO(out_seq_fini, rc);
4141 /* init seq client for seq server to talk to seq controller(MDT0) */
4142 rc = mdt_seq_init_cli(env, mdt);
4144 GOTO(out_seq_fini, rc);
4146 if (ss->ss_node_id != 0)
4147 /* register controller export through lwp */
4148 rc = mdt_register_seq_exp(mdt);
4153 mdt_seq_fini(env, mdt);
4161 static int mdt_fld_fini(const struct lu_env *env,
4162 struct mdt_device *m)
4164 struct seq_server_site *ss = mdt_seq_site(m);
4167 if (ss && ss->ss_server_fld) {
4168 fld_server_fini(env, ss->ss_server_fld);
4169 OBD_FREE_PTR(ss->ss_server_fld);
4170 ss->ss_server_fld = NULL;
4176 static int mdt_fld_init(const struct lu_env *env,
4178 struct mdt_device *m)
4180 struct seq_server_site *ss;
4184 ss = mdt_seq_site(m);
4186 OBD_ALLOC_PTR(ss->ss_server_fld);
4187 if (ss->ss_server_fld == NULL)
4188 RETURN(rc = -ENOMEM);
4190 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
4193 OBD_FREE_PTR(ss->ss_server_fld);
4194 ss->ss_server_fld = NULL;
4201 static void mdt_stack_pre_fini(const struct lu_env *env,
4202 struct mdt_device *m, struct lu_device *top)
4204 struct lustre_cfg_bufs *bufs;
4205 struct lustre_cfg *lcfg;
4206 struct mdt_thread_info *info;
4211 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4212 LASSERT(info != NULL);
4214 bufs = &info->mti_u.bufs;
4216 LASSERT(m->mdt_child_exp);
4217 LASSERT(m->mdt_child_exp->exp_obd);
4219 /* process cleanup, pass mdt obd name to get obd umount flags */
4220 /* XXX: this is needed because all layers are referenced by
4221 * objects (some of them are pinned by osd, for example *
4222 * the proper solution should be a model where object used
4223 * by osd only doesn't have mdt/mdd slices -bzzz */
4224 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4225 lustre_cfg_bufs_set_string(bufs, 1, NULL);
4226 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4229 lustre_cfg_init(lcfg, LCFG_PRE_CLEANUP, bufs);
4231 top->ld_ops->ldo_process_config(env, top, lcfg);
4232 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4236 static void mdt_stack_fini(const struct lu_env *env,
4237 struct mdt_device *m, struct lu_device *top)
4239 struct obd_device *obd = mdt2obd_dev(m);
4240 struct lustre_cfg_bufs *bufs;
4241 struct lustre_cfg *lcfg;
4242 struct mdt_thread_info *info;
4246 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4247 LASSERT(info != NULL);
4249 lu_dev_del_linkage(top->ld_site, top);
4251 lu_site_purge(env, top->ld_site, -1);
4253 bufs = &info->mti_u.bufs;
4254 /* process cleanup, pass mdt obd name to get obd umount flags */
4255 /* another purpose is to let all layers to release their objects */
4256 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4261 lustre_cfg_bufs_set_string(bufs, 1, flags);
4262 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4265 lustre_cfg_init(lcfg, LCFG_CLEANUP, bufs);
4268 top->ld_ops->ldo_process_config(env, top, lcfg);
4269 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4271 lu_site_purge(env, top->ld_site, -1);
4273 m->mdt_child = NULL;
4274 m->mdt_bottom = NULL;
4276 obd_disconnect(m->mdt_child_exp);
4277 m->mdt_child_exp = NULL;
4279 obd_disconnect(m->mdt_bottom_exp);
4280 m->mdt_child_exp = NULL;
4283 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4284 const char *next, struct obd_export **exp)
4286 struct obd_connect_data *data = NULL;
4287 struct obd_device *obd;
4291 OBD_ALLOC_PTR(data);
4293 GOTO(out, rc = -ENOMEM);
4295 obd = class_name2obd(next);
4297 CERROR("%s: can't locate next device: %s\n",
4298 mdt_obd_name(m), next);
4299 GOTO(out, rc = -ENOTCONN);
4302 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4303 data->ocd_version = LUSTRE_VERSION_CODE;
4305 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4307 CERROR("%s: cannot connect to next dev %s (%d)\n",
4308 mdt_obd_name(m), next, rc);
4318 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4319 struct lustre_cfg *cfg)
4321 char *dev = lustre_cfg_string(cfg, 0);
4322 int rc, name_size, uuid_size;
4323 char *name, *uuid, *p;
4324 struct lustre_cfg_bufs *bufs;
4325 struct lustre_cfg *lcfg;
4326 struct obd_device *obd;
4327 struct lustre_profile *lprof;
4328 struct lu_site *site;
4331 /* in 1.8 we had the only device in the stack - MDS.
4332 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4333 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4334 * to generate names and setup MDT, MDD. MDT will be using
4335 * generated name to connect to MDD. for MDD the next device
4336 * will be LOD with name taken from so called "profile" which
4337 * is generated by mount_option line
4339 * 1.8 MGS generates config. commands like this:
4340 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4341 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4342 * 2.0 MGS generates config. commands like this:
4343 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4344 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4345 * 3:lustre-MDT0000-mdtlov 4:f
4347 * we generate MDD name from MDT one, just replacing T with D
4349 * after all the preparations, the logical equivalent will be
4350 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4351 * 3:lustre-MDT0000-mdtlov 4:f
4352 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4353 * 3:lustre-MDD0000 4:f
4355 * notice we build the stack from down to top: MDD first, then MDT */
4357 name_size = MAX_OBD_NAME;
4358 uuid_size = MAX_OBD_NAME;
4360 OBD_ALLOC(name, name_size);
4361 OBD_ALLOC(uuid, uuid_size);
4362 if (name == NULL || uuid == NULL)
4363 GOTO(cleanup_mem, rc = -ENOMEM);
4365 OBD_ALLOC_PTR(bufs);
4367 GOTO(cleanup_mem, rc = -ENOMEM);
4370 p = strstr(name, "-MDT");
4372 GOTO(free_bufs, rc = -ENOMEM);
4375 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4377 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4378 if (lprof == NULL || lprof->lp_dt == NULL) {
4379 CERROR("can't find the profile: %s\n",
4380 lustre_cfg_string(cfg, 0));
4381 GOTO(free_bufs, rc = -EINVAL);
4384 lustre_cfg_bufs_reset(bufs, name);
4385 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4386 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4387 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4389 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4391 GOTO(put_profile, rc = -ENOMEM);
4392 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4394 rc = class_attach(lcfg);
4396 GOTO(lcfg_cleanup, rc);
4398 obd = class_name2obd(name);
4400 CERROR("Can not find obd %s (%s in config)\n",
4401 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4402 GOTO(lcfg_cleanup, rc = -EINVAL);
4405 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4407 lustre_cfg_bufs_reset(bufs, name);
4408 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4409 lustre_cfg_bufs_set_string(bufs, 2, dev);
4410 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4412 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4414 GOTO(class_detach, rc = -ENOMEM);
4415 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4417 rc = class_setup(obd, lcfg);
4419 GOTO(class_detach, rc);
4421 /* connect to MDD we just setup */
4422 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4424 GOTO(class_detach, rc);
4426 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4428 LASSERT(mdt_lu_site(mdt) == NULL);
4429 mdt->mdt_lu_dev.ld_site = site;
4430 site->ls_top_dev = &mdt->mdt_lu_dev;
4431 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4433 /* now connect to bottom OSD */
4434 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4435 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4437 GOTO(class_detach, rc);
4439 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4441 rc = lu_env_refill((struct lu_env *)env);
4443 CERROR("Failure to refill session: '%d'\n", rc);
4445 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4450 class_detach(obd, lcfg);
4452 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4454 class_put_profile(lprof);
4459 OBD_FREE(name, name_size);
4461 OBD_FREE(uuid, uuid_size);
4465 /* setup quota master target on MDT0 */
4466 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4467 struct lustre_cfg *cfg)
4469 struct obd_device *obd;
4470 char *dev = lustre_cfg_string(cfg, 0);
4471 char *qmtname, *uuid, *p;
4472 struct lustre_cfg_bufs *bufs;
4473 struct lustre_cfg *lcfg;
4474 struct lustre_profile *lprof;
4475 struct obd_connect_data *data;
4479 LASSERT(mdt->mdt_qmt_exp == NULL);
4480 LASSERT(mdt->mdt_qmt_dev == NULL);
4482 /* quota master is on MDT0 only for now */
4483 if (mdt->mdt_seq_site.ss_node_id != 0)
4486 /* MGS generates config commands which look as follows:
4487 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4488 * 3:lustre-MDT0000-mdtlov 4:f
4490 * We generate the QMT name from the MDT one, just replacing MD with QM
4491 * after all the preparations, the logical equivalent will be:
4492 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4493 * 3:lustre-MDT0000-osd 4:f */
4494 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4495 OBD_ALLOC(uuid, UUID_MAX);
4496 OBD_ALLOC_PTR(bufs);
4497 OBD_ALLOC_PTR(data);
4498 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4499 GOTO(cleanup_mem, rc = -ENOMEM);
4501 strcpy(qmtname, dev);
4502 p = strstr(qmtname, "-MDT");
4504 GOTO(cleanup_mem, rc = -ENOMEM);
4505 /* replace MD with QM */
4509 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4511 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4512 if (lprof == NULL || lprof->lp_dt == NULL) {
4513 CERROR("can't find profile for %s\n",
4514 lustre_cfg_string(cfg, 0));
4515 GOTO(cleanup_mem, rc = -EINVAL);
4518 lustre_cfg_bufs_reset(bufs, qmtname);
4519 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4520 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4521 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4523 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4525 GOTO(put_profile, rc = -ENOMEM);
4526 lustre_cfg_init(lcfg, LCFG_ATTACH, bufs);
4528 rc = class_attach(lcfg);
4530 GOTO(lcfg_cleanup, rc);
4532 obd = class_name2obd(qmtname);
4534 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4535 lustre_cfg_string(cfg, 0));
4536 GOTO(lcfg_cleanup, rc = -EINVAL);
4539 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4541 lustre_cfg_bufs_reset(bufs, qmtname);
4542 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4543 lustre_cfg_bufs_set_string(bufs, 2, dev);
4545 /* for quota, the next device should be the OSD device */
4546 lustre_cfg_bufs_set_string(bufs, 3,
4547 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4549 OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
4551 GOTO(class_detach, rc = -ENOMEM);
4552 lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
4554 rc = class_setup(obd, lcfg);
4556 GOTO(class_detach, rc);
4558 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4560 /* configure local quota objects */
4561 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4565 GOTO(class_cleanup, rc);
4567 /* connect to quota master target */
4568 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4569 data->ocd_version = LUSTRE_VERSION_CODE;
4570 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4573 CERROR("cannot connect to quota master device %s (%d)\n",
4575 GOTO(class_cleanup, rc);
4581 class_manual_cleanup(obd);
4582 mdt->mdt_qmt_dev = NULL;
4586 class_detach(obd, lcfg);
4588 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
4590 class_put_profile(lprof);
4595 OBD_FREE(qmtname, MAX_OBD_NAME);
4597 OBD_FREE(uuid, UUID_MAX);
4603 /* Shutdown quota master target associated with mdt */
4604 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4608 if (mdt->mdt_qmt_exp == NULL)
4610 LASSERT(mdt->mdt_qmt_dev != NULL);
4612 /* the qmt automatically shuts down when the mdt disconnects */
4613 obd_disconnect(mdt->mdt_qmt_exp);
4614 mdt->mdt_qmt_exp = NULL;
4615 mdt->mdt_qmt_dev = NULL;
4619 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4620 * for now. This will be removed along with converting rest of MDT code
4621 * to use tgt_session_info */
4622 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4624 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4627 rc = mdt_getxattr(info);
4629 mdt_thread_info_fini(info);
4633 static struct tgt_handler mdt_tgt_handlers[] = {
4634 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4635 0, MDS_CONNECT, mdt_tgt_connect,
4636 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4637 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4638 0, MDS_DISCONNECT, tgt_disconnect,
4639 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4640 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4641 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4642 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4643 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4644 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4645 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4646 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4648 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4649 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4650 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4651 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4652 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4653 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4654 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4655 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4657 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4658 mdt_hsm_ct_register),
4659 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4660 mdt_hsm_ct_unregister),
4661 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4663 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4665 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4666 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4668 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4673 static struct tgt_handler mdt_sec_ctx_ops[] = {
4674 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4675 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4676 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4679 static struct tgt_handler mdt_quota_ops[] = {
4680 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4683 static struct tgt_opc_slice mdt_common_slice[] = {
4685 .tos_opc_start = MDS_FIRST_OPC,
4686 .tos_opc_end = MDS_LAST_OPC,
4687 .tos_hs = mdt_tgt_handlers
4690 .tos_opc_start = OBD_FIRST_OPC,
4691 .tos_opc_end = OBD_LAST_OPC,
4692 .tos_hs = tgt_obd_handlers
4695 .tos_opc_start = LDLM_FIRST_OPC,
4696 .tos_opc_end = LDLM_LAST_OPC,
4697 .tos_hs = tgt_dlm_handlers
4700 .tos_opc_start = SEC_FIRST_OPC,
4701 .tos_opc_end = SEC_LAST_OPC,
4702 .tos_hs = mdt_sec_ctx_ops
4705 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4706 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4707 .tos_hs = tgt_out_handlers
4710 .tos_opc_start = FLD_FIRST_OPC,
4711 .tos_opc_end = FLD_LAST_OPC,
4712 .tos_hs = fld_handlers
4715 .tos_opc_start = SEQ_FIRST_OPC,
4716 .tos_opc_end = SEQ_LAST_OPC,
4717 .tos_hs = seq_handlers
4720 .tos_opc_start = QUOTA_DQACQ,
4721 .tos_opc_end = QUOTA_LAST_OPC,
4722 .tos_hs = mdt_quota_ops
4725 .tos_opc_start = LLOG_FIRST_OPC,
4726 .tos_opc_end = LLOG_LAST_OPC,
4727 .tos_hs = tgt_llog_handlers
4730 .tos_opc_start = LFSCK_FIRST_OPC,
4731 .tos_opc_end = LFSCK_LAST_OPC,
4732 .tos_hs = tgt_lfsck_handlers
4740 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4742 struct md_device *next = m->mdt_child;
4743 struct lu_device *d = &m->mdt_lu_dev;
4744 struct obd_device *obd = mdt2obd_dev(m);
4745 struct lfsck_stop stop;
4748 stop.ls_status = LS_PAUSED;
4750 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4752 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4753 ping_evictor_stop();
4755 /* Remove the HSM /proc entry so the coordinator cannot be
4756 * restarted by a user while it's shutting down. */
4757 hsm_cdt_procfs_fini(m);
4758 mdt_hsm_cdt_stop(m);
4760 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4761 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4763 if (m->mdt_namespace != NULL)
4764 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4765 d->ld_obd->obd_force);
4767 obd_exports_barrier(obd);
4768 obd_zombie_barrier();
4770 mdt_quota_fini(env, m);
4772 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4774 /* Calling the cleanup functions in the same order as in the mdt_init0
4779 target_recovery_fini(obd);
4780 upcall_cache_cleanup(m->mdt_identity_cache);
4781 m->mdt_identity_cache = NULL;
4783 mdt_fs_cleanup(env, m);
4785 tgt_fini(env, &m->mdt_lut);
4787 mdt_hsm_cdt_fini(m);
4789 if (m->mdt_namespace != NULL) {
4790 ldlm_namespace_free_post(m->mdt_namespace);
4791 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4794 if (m->mdt_md_root != NULL) {
4795 mdt_object_put(env, m->mdt_md_root);
4796 m->mdt_md_root = NULL;
4799 mdt_seq_fini(env, m);
4801 mdt_fld_fini(env, m);
4806 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4808 LASSERT(atomic_read(&d->ld_ref) == 0);
4810 server_put_mount(mdt_obd_name(m), true);
4815 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4817 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4818 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4820 struct mdt_thread_info *info;
4821 struct obd_device *obd;
4822 const char *dev = lustre_cfg_string(cfg, 0);
4823 const char *num = lustre_cfg_string(cfg, 2);
4824 struct lustre_mount_info *lmi = NULL;
4825 struct lustre_sb_info *lsi;
4827 struct seq_server_site *ss_site;
4828 const char *identity_upcall = "NONE";
4829 struct md_device *next;
4835 lu_device_init(&m->mdt_lu_dev, ldt);
4837 * Environment (env) might be missing mdt_thread_key values at that
4838 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4841 * Usually device allocation path doesn't use module key values, but
4842 * mdt has to do a lot of work here, so allocate key value.
4844 rc = lu_env_refill((struct lu_env *)env);
4848 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4849 LASSERT(info != NULL);
4851 obd = class_name2obd(dev);
4852 LASSERT(obd != NULL);
4854 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4855 m->mdt_opts.mo_evict_tgt_nids = 1;
4856 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4858 lmi = server_get_mount(dev);
4860 CERROR("Cannot get mount info for %s!\n", dev);
4863 lsi = s2lsi(lmi->lmi_sb);
4864 /* CMD is supported only in IAM mode */
4866 node_id = simple_strtol(num, NULL, 10);
4867 obd->u.obt.obt_magic = OBT_MAGIC;
4868 if (lsi->lsi_lmd != NULL &&
4869 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4870 m->mdt_skip_lfsck = 1;
4873 m->mdt_squash.rsi_uid = 0;
4874 m->mdt_squash.rsi_gid = 0;
4875 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4876 init_rwsem(&m->mdt_squash.rsi_sem);
4877 spin_lock_init(&m->mdt_lock);
4878 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4879 m->mdt_enable_remote_dir = 0;
4880 m->mdt_enable_remote_dir_gid = 0;
4882 atomic_set(&m->mdt_mds_mds_conns, 0);
4883 atomic_set(&m->mdt_async_commit_count, 0);
4885 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4886 m->mdt_lu_dev.ld_obd = obd;
4887 /* Set this lu_device to obd for error handling purposes. */
4888 obd->obd_lu_dev = &m->mdt_lu_dev;
4890 /* init the stack */
4891 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4893 CERROR("%s: Can't init device stack, rc %d\n",
4894 mdt_obd_name(m), rc);
4899 ss_site = mdt_seq_site(m);
4900 s->ld_seq_site = ss_site;
4903 /* set server index */
4904 ss_site->ss_node_id = node_id;
4906 /* failover is the default
4907 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4908 * assumed whose ss_node_id == 0 XXX
4910 obd->obd_replayable = 1;
4911 /* No connection accepted until configurations will finish */
4912 obd->obd_no_conn = 1;
4914 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4915 char *str = lustre_cfg_string(cfg, 4);
4916 if (strchr(str, 'n')) {
4917 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4918 obd->obd_replayable = 0;
4922 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4924 GOTO(err_fini_stack, rc);
4926 rc = mdt_seq_init(env, m);
4928 GOTO(err_fini_fld, rc);
4930 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4931 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4932 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4933 LDLM_NAMESPACE_SERVER,
4934 LDLM_NAMESPACE_GREEDY,
4936 if (m->mdt_namespace == NULL)
4937 GOTO(err_fini_seq, rc = -ENOMEM);
4939 m->mdt_namespace->ns_lvbp = m;
4940 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4942 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4943 /* set obd_namespace for compatibility with old code */
4944 obd->obd_namespace = m->mdt_namespace;
4946 rc = mdt_hsm_cdt_init(m);
4948 CERROR("%s: error initializing coordinator, rc %d\n",
4949 mdt_obd_name(m), rc);
4950 GOTO(err_free_ns, rc);
4953 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4954 OBD_FAIL_MDS_ALL_REQUEST_NET,
4955 OBD_FAIL_MDS_ALL_REPLY_NET);
4957 GOTO(err_free_hsm, rc);
4959 rc = mdt_fs_setup(env, m, obd, lsi);
4963 tgt_adapt_sptlrpc_conf(&m->mdt_lut);
4965 next = m->mdt_child;
4966 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4969 GOTO(err_fs_cleanup, rc);
4971 if (mntopts & MNTOPT_USERXATTR)
4972 m->mdt_opts.mo_user_xattr = 1;
4974 m->mdt_opts.mo_user_xattr = 0;
4976 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4978 GOTO(err_fs_cleanup, rc);
4980 if (mntopts & MNTOPT_ACL)
4981 m->mdt_opts.mo_acl = 1;
4983 m->mdt_opts.mo_acl = 0;
4985 /* XXX: to support suppgid for ACL, we enable identity_upcall
4986 * by default, otherwise, maybe got unexpected -EACCESS. */
4987 if (m->mdt_opts.mo_acl)
4988 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4990 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4992 &mdt_identity_upcall_cache_ops);
4993 if (IS_ERR(m->mdt_identity_cache)) {
4994 rc = PTR_ERR(m->mdt_identity_cache);
4995 m->mdt_identity_cache = NULL;
4996 GOTO(err_fs_cleanup, rc);
4999 rc = mdt_procfs_init(m, dev);
5001 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
5002 GOTO(err_recovery, rc);
5005 rc = mdt_quota_init(env, m, cfg);
5007 GOTO(err_procfs, rc);
5009 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
5010 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
5011 "mdt_ldlm_client", m->mdt_ldlm_client);
5013 ping_evictor_start();
5015 /* recovery will be started upon mdt_prepare()
5016 * when the whole stack is complete and ready
5017 * to serve the requests */
5019 /* Reduce the initial timeout on an MDS because it doesn't need such
5020 * a long timeout as an OST does. Adaptive timeouts will adjust this
5021 * value appropriately. */
5022 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
5023 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
5029 target_recovery_fini(obd);
5030 upcall_cache_cleanup(m->mdt_identity_cache);
5031 m->mdt_identity_cache = NULL;
5033 mdt_fs_cleanup(env, m);
5035 tgt_fini(env, &m->mdt_lut);
5037 mdt_hsm_cdt_fini(m);
5039 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
5040 obd->obd_namespace = m->mdt_namespace = NULL;
5042 mdt_seq_fini(env, m);
5044 mdt_fld_fini(env, m);
5046 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
5049 server_put_mount(dev, true);
5053 /* For interoperability, the left element is old parameter, the right one
5054 * is the new version of the parameter, if some parameter is deprecated,
5055 * the new version should be set as NULL. */
5056 static struct cfg_interop_param mdt_interop_param[] = {
5057 { "mdt.group_upcall", NULL },
5058 { "mdt.quota_type", NULL },
5059 { "mdd.quota_type", NULL },
5060 { "mdt.som", NULL },
5061 { "mdt.rootsquash", "mdt.root_squash" },
5062 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
5066 /* used by MGS to process specific configurations */
5067 static int mdt_process_config(const struct lu_env *env,
5068 struct lu_device *d, struct lustre_cfg *cfg)
5070 struct mdt_device *m = mdt_dev(d);
5071 struct md_device *md_next = m->mdt_child;
5072 struct lu_device *next = md2lu_dev(md_next);
5076 switch (cfg->lcfg_command) {
5078 struct obd_device *obd = d->ld_obd;
5080 /* For interoperability */
5081 struct cfg_interop_param *ptr = NULL;
5082 struct lustre_cfg *old_cfg = NULL;
5085 param = lustre_cfg_string(cfg, 1);
5086 if (param == NULL) {
5087 CERROR("param is empty\n");
5092 ptr = class_find_old_param(param, mdt_interop_param);
5094 if (ptr->new_param == NULL) {
5096 CWARN("For interoperability, skip this %s."
5097 " It is obsolete.\n", ptr->old_param);
5101 CWARN("Found old param %s, changed it to %s.\n",
5102 ptr->old_param, ptr->new_param);
5105 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
5112 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
5114 if (rc > 0 || rc == -ENOSYS) {
5115 /* is it an HSM var ? */
5116 rc = class_process_proc_param(PARAM_HSM,
5117 hsm_cdt_get_proc_vars(),
5119 if (rc > 0 || rc == -ENOSYS)
5120 /* we don't understand; pass it on */
5121 rc = next->ld_ops->ldo_process_config(env, next,
5126 OBD_FREE(cfg, lustre_cfg_len(cfg->lcfg_bufcount,
5127 cfg->lcfg_buflens));
5131 /* others are passed further */
5132 rc = next->ld_ops->ldo_process_config(env, next, cfg);
5138 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
5139 const struct lu_object_header *hdr,
5140 struct lu_device *d)
5142 struct mdt_object *mo;
5146 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
5148 struct lu_object *o;
5149 struct lu_object_header *h;
5152 h = &mo->mot_header;
5153 lu_object_header_init(h);
5154 lu_object_init(o, h, d);
5155 lu_object_add_top(h, o);
5156 o->lo_ops = &mdt_obj_ops;
5157 spin_lock_init(&mo->mot_write_lock);
5158 mutex_init(&mo->mot_lov_mutex);
5159 init_rwsem(&mo->mot_open_sem);
5160 atomic_set(&mo->mot_open_count, 0);
5166 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
5167 const struct lu_object_conf *unused)
5169 struct mdt_device *d = mdt_dev(o->lo_dev);
5170 struct lu_device *under;
5171 struct lu_object *below;
5175 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
5176 PFID(lu_object_fid(o)));
5178 under = &d->mdt_child->md_lu_dev;
5179 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
5180 if (below != NULL) {
5181 lu_object_add(o, below);
5188 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
5190 struct mdt_object *mo = mdt_obj(o);
5191 struct lu_object_header *h;
5195 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
5196 PFID(lu_object_fid(o)));
5198 LASSERT(atomic_read(&mo->mot_open_count) == 0);
5199 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
5202 lu_object_header_fini(h);
5203 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
5208 static int mdt_object_print(const struct lu_env *env, void *cookie,
5209 lu_printer_t p, const struct lu_object *o)
5211 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
5213 return (*p)(env, cookie,
5214 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
5215 mdto, mdto->mot_lov_created ? "lov_created" : "",
5216 mdto->mot_cache_attr ? "cache_attr" : "",
5217 mdto->mot_write_count);
5220 static int mdt_prepare(const struct lu_env *env,
5221 struct lu_device *pdev,
5222 struct lu_device *cdev)
5224 struct mdt_device *mdt = mdt_dev(cdev);
5225 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
5226 struct obd_device *obd = cdev->ld_obd;
5233 rc = next->ld_ops->ldo_prepare(env, cdev, next);
5237 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5241 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5245 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5246 /* The LFSCK instance is registered just now, so it must be there when
5247 * register the namespace to such instance. */
5248 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5250 if (mdt->mdt_seq_site.ss_node_id == 0) {
5251 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5252 &mdt->mdt_md_root_fid);
5257 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5259 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5260 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5261 LASSERT(obd->obd_no_conn);
5262 spin_lock(&obd->obd_dev_lock);
5263 obd->obd_no_conn = 0;
5264 spin_unlock(&obd->obd_dev_lock);
5266 if (obd->obd_recovering == 0)
5267 mdt_postrecov(env, mdt);
5272 const struct lu_device_operations mdt_lu_ops = {
5273 .ldo_object_alloc = mdt_object_alloc,
5274 .ldo_process_config = mdt_process_config,
5275 .ldo_prepare = mdt_prepare,
5278 static const struct lu_object_operations mdt_obj_ops = {
5279 .loo_object_init = mdt_object_init,
5280 .loo_object_free = mdt_object_free,
5281 .loo_object_print = mdt_object_print
5284 static int mdt_obd_set_info_async(const struct lu_env *env,
5285 struct obd_export *exp,
5286 __u32 keylen, void *key,
5287 __u32 vallen, void *val,
5288 struct ptlrpc_request_set *set)
5294 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5295 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp));
5303 * Match client and server connection feature flags.
5305 * Compute the compatibility flags for a connection request based on
5306 * features mutually supported by client and server.
5308 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5309 * must not be updated here, otherwise a partially initialized value may
5310 * be exposed. After the connection request is successfully processed,
5311 * the top-level MDT connect request handler atomically updates the export
5312 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5313 * reply. \see mdt_connect().
5315 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5316 * full struct obd_connect_data. So care must be taken when accessing fields
5317 * that are not present in struct obd_connect_data_v1. See LU-16.
5319 * \param exp the obd_export associated with this client/target pair
5320 * \param mdt the target device for the connection
5321 * \param data stores data for this connect request
5324 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5325 * \retval -EBADE client and server feature requirements are incompatible
5327 static int mdt_connect_internal(struct obd_export *exp,
5328 struct mdt_device *mdt,
5329 struct obd_connect_data *data)
5331 LASSERT(data != NULL);
5333 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5335 if (mdt->mdt_bottom->dd_rdonly &&
5336 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5337 !(data->ocd_connect_flags & OBD_CONNECT_RDONLY))
5340 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5341 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5343 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5345 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5346 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5347 CWARN("%s: client %s does not support ibits lock, either "
5348 "very old or an invalid client: flags %#llx\n",
5349 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5350 data->ocd_connect_flags);
5354 if (!mdt->mdt_opts.mo_acl)
5355 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5357 if (!mdt->mdt_opts.mo_user_xattr)
5358 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5360 if (OCD_HAS_FLAG(data, BRW_SIZE)) {
5361 data->ocd_brw_size = min(data->ocd_brw_size, MD_MAX_BRW_SIZE);
5362 if (data->ocd_brw_size == 0) {
5363 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx "
5364 "ocd_version: %x ocd_grant: %d ocd_index: %u "
5365 "ocd_brw_size unexpectedly zero, network data "
5366 "corruption? Refusing to connect this client\n",
5368 exp->exp_client_uuid.uuid,
5369 exp, data->ocd_connect_flags, data->ocd_version,
5370 data->ocd_grant, data->ocd_index);
5375 /* NB: Disregard the rule against updating
5376 * exp_connect_data.ocd_connect_flags in this case, since
5377 * tgt_client_new() needs to know if this is a lightweight
5378 * connection, and it is safe to expose this flag before
5379 * connection processing completes. */
5380 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5381 spin_lock(&exp->exp_lock);
5382 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5383 spin_unlock(&exp->exp_lock);
5386 data->ocd_version = LUSTRE_VERSION_CODE;
5388 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5389 CWARN("%s: MDS requires FID support, but client not\n",
5394 if (OCD_HAS_FLAG(data, PINGLESS)) {
5395 if (ptlrpc_pinger_suppress_pings()) {
5396 spin_lock(&exp->exp_obd->obd_dev_lock);
5397 list_del_init(&exp->exp_obd_chain_timed);
5398 spin_unlock(&exp->exp_obd->obd_dev_lock);
5400 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5404 data->ocd_max_easize = mdt->mdt_max_ea_size;
5406 /* NB: Disregard the rule against updating
5407 * exp_connect_data.ocd_connect_flags in this case, since
5408 * tgt_client_new() needs to know if this is client supports
5409 * multiple modify RPCs, and it is safe to expose this flag before
5410 * connection processing completes. */
5411 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5412 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5413 spin_lock(&exp->exp_lock);
5414 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5415 spin_unlock(&exp->exp_lock);
5421 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5422 struct mdt_thread_info *info,
5423 struct mdt_file_data *mfd)
5425 struct lu_context ses;
5429 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5434 lu_context_enter(&ses);
5436 mdt_ucred(info)->uc_valid = UCRED_OLD;
5437 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5439 lu_context_exit(&ses);
5440 lu_context_fini(&ses);
5446 static int mdt_export_cleanup(struct obd_export *exp)
5448 struct list_head closing_list;
5449 struct mdt_export_data *med = &exp->exp_mdt_data;
5450 struct obd_device *obd = exp->exp_obd;
5451 struct mdt_device *mdt;
5452 struct mdt_thread_info *info;
5454 struct mdt_file_data *mfd, *n;
5458 INIT_LIST_HEAD(&closing_list);
5459 spin_lock(&med->med_open_lock);
5460 while (!list_empty(&med->med_open_head)) {
5461 struct list_head *tmp = med->med_open_head.next;
5462 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5464 /* Remove mfd handle so it can't be found again.
5465 * We are consuming the mfd_list reference here. */
5466 class_handle_unhash(&mfd->mfd_handle);
5467 list_move_tail(&mfd->mfd_list, &closing_list);
5469 spin_unlock(&med->med_open_lock);
5470 mdt = mdt_dev(obd->obd_lu_dev);
5471 LASSERT(mdt != NULL);
5473 rc = lu_env_init(&env, LCT_MD_THREAD);
5477 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5478 LASSERT(info != NULL);
5479 memset(info, 0, sizeof *info);
5480 info->mti_env = &env;
5481 info->mti_mdt = mdt;
5482 info->mti_exp = exp;
5484 if (!list_empty(&closing_list)) {
5485 struct md_attr *ma = &info->mti_attr;
5487 /* Close any open files (which may also cause orphan
5489 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5490 list_del_init(&mfd->mfd_list);
5491 ma->ma_need = ma->ma_valid = 0;
5493 /* This file is being closed due to an eviction, it
5494 * could have been modified and now dirty regarding to
5495 * HSM archive, check this!
5496 * The logic here is to mark a file dirty if there's a
5497 * chance it was dirtied before the client was evicted,
5498 * so that we don't have to wait for a release attempt
5499 * before finding out the file was actually dirty and
5500 * fail the release. Aggressively marking it dirty here
5501 * will cause the policy engine to attempt to
5502 * re-archive it; when rearchiving, we can compare the
5503 * current version to the HSM data_version and make the
5504 * archive request into a noop if it's not actually
5507 if (mfd->mfd_mode & FMODE_WRITE)
5508 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5510 /* Don't unlink orphan on failover umount, LU-184 */
5511 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5512 ma->ma_valid = MA_FLAGS;
5513 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5515 mdt_mfd_close(info, mfd);
5518 info->mti_mdt = NULL;
5519 /* cleanup client slot early */
5520 /* Do not erase record for recoverable client. */
5521 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5522 tgt_client_del(&env, exp);
5528 static inline void mdt_enable_slc(struct mdt_device *mdt)
5530 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5531 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5534 static inline void mdt_disable_slc(struct mdt_device *mdt)
5536 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5537 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5540 static int mdt_obd_disconnect(struct obd_export *exp)
5546 class_export_get(exp);
5548 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5549 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5550 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5552 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5553 mdt_disable_slc(mdt);
5556 rc = server_disconnect_export(exp);
5558 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5560 rc = mdt_export_cleanup(exp);
5561 nodemap_del_member(exp);
5562 class_export_put(exp);
5566 /* mds_connect copy */
5567 static int mdt_obd_connect(const struct lu_env *env,
5568 struct obd_export **exp, struct obd_device *obd,
5569 struct obd_uuid *cluuid,
5570 struct obd_connect_data *data,
5573 struct obd_export *lexp;
5574 struct lustre_handle conn = { 0 };
5575 struct mdt_device *mdt;
5577 lnet_nid_t *client_nid = localdata;
5580 LASSERT(env != NULL);
5581 LASSERT(data != NULL);
5583 if (!exp || !obd || !cluuid)
5586 mdt = mdt_dev(obd->obd_lu_dev);
5588 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5589 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5590 atomic_inc(&mdt->mdt_mds_mds_conns);
5591 mdt_enable_slc(mdt);
5595 * first, check whether the stack is ready to handle requests
5596 * XXX: probably not very appropriate method is used now
5597 * at some point we should find a better one
5599 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5600 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5601 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5602 rc = obd_get_info(env, mdt->mdt_child_exp,
5603 sizeof(KEY_OSP_CONNECTED),
5604 KEY_OSP_CONNECTED, NULL, NULL);
5607 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5610 rc = class_connect(&conn, obd, cluuid);
5614 lexp = class_conn2export(&conn);
5615 LASSERT(lexp != NULL);
5617 rc = nodemap_add_member(*client_nid, lexp);
5618 if (rc != 0 && rc != -EEXIST)
5621 rc = mdt_connect_internal(lexp, mdt, data);
5623 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5626 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5627 rc = tgt_client_new(env, lexp);
5629 mdt_export_stats_init(obd, lexp, localdata);
5633 class_disconnect(lexp);
5634 nodemap_del_member(lexp);
5638 /* Because we do not want this export to be evicted by pinger,
5639 * let's not add this export to the timed chain list. */
5640 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5641 spin_lock(&lexp->exp_obd->obd_dev_lock);
5642 list_del_init(&lexp->exp_obd_chain_timed);
5643 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5650 static int mdt_obd_reconnect(const struct lu_env *env,
5651 struct obd_export *exp, struct obd_device *obd,
5652 struct obd_uuid *cluuid,
5653 struct obd_connect_data *data,
5656 lnet_nid_t *client_nid = localdata;
5660 if (exp == NULL || obd == NULL || cluuid == NULL)
5663 rc = nodemap_add_member(*client_nid, exp);
5664 if (rc != 0 && rc != -EEXIST)
5667 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5669 mdt_export_stats_init(obd, exp, localdata);
5671 nodemap_del_member(exp);
5676 /* FIXME: Can we avoid using these two interfaces? */
5677 static int mdt_init_export(struct obd_export *exp)
5679 struct mdt_export_data *med = &exp->exp_mdt_data;
5683 INIT_LIST_HEAD(&med->med_open_head);
5684 spin_lock_init(&med->med_open_lock);
5685 spin_lock(&exp->exp_lock);
5686 exp->exp_connecting = 1;
5687 spin_unlock(&exp->exp_lock);
5689 /* self-export doesn't need client data and ldlm initialization */
5690 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5691 &exp->exp_client_uuid)))
5694 rc = tgt_client_alloc(exp);
5698 rc = ldlm_init_export(exp);
5705 tgt_client_free(exp);
5707 CERROR("%s: Failed to initialize export: rc = %d\n",
5708 exp->exp_obd->obd_name, rc);
5712 static int mdt_destroy_export(struct obd_export *exp)
5716 target_destroy_export(exp);
5717 /* destroy can be called from failed obd_setup, so
5718 * checking uuid is safer than obd_self_export */
5719 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5720 &exp->exp_client_uuid)))
5723 ldlm_destroy_export(exp);
5724 tgt_client_free(exp);
5726 LASSERT(list_empty(&exp->exp_outstanding_replies));
5727 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5732 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5733 struct linkea_data *ldata)
5737 LASSERT(ldata->ld_buf->lb_buf != NULL);
5739 if (!mdt_object_exists(mdt_obj))
5742 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5743 ldata->ld_buf, XATTR_NAME_LINK);
5744 if (rc == -ERANGE) {
5745 /* Buf was too small, figure out what we need. */
5746 lu_buf_free(ldata->ld_buf);
5747 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5748 ldata->ld_buf, XATTR_NAME_LINK);
5751 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5752 if (ldata->ld_buf->lb_buf == NULL)
5754 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5755 ldata->ld_buf, XATTR_NAME_LINK);
5760 return linkea_init_with_rec(ldata);
5764 * Given an MDT object, try to look up the full path to the object.
5765 * Part of the MDT layer implementation of lfs fid2path.
5767 * \param[in] info Per-thread common data shared by MDT level handlers.
5768 * \param[in] obj Object to do path lookup of
5769 * \param[in,out] fp User-provided struct to store path information
5770 * \param[in] root_fid Root FID of current path should reach
5772 * \retval 0 Lookup successful, path information stored in fp
5773 * \retval -EAGAIN Lookup failed, usually because object is being moved
5774 * \retval negative errno if there was a problem
5776 static int mdt_path_current(struct mdt_thread_info *info,
5777 struct mdt_object *obj,
5778 struct getinfo_fid2path *fp,
5779 struct lu_fid *root_fid)
5781 struct mdt_device *mdt = info->mti_mdt;
5782 struct mdt_object *mdt_obj;
5783 struct link_ea_header *leh;
5784 struct link_ea_entry *lee;
5785 struct lu_name *tmpname = &info->mti_name;
5786 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5787 struct lu_buf *buf = &info->mti_big_buf;
5790 struct linkea_data ldata = { NULL };
5795 /* temp buffer for path element, the buffer will be finally freed
5796 * in mdt_thread_info_fini */
5797 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5798 if (buf->lb_buf == NULL)
5802 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5805 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5807 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5808 struct lu_buf lmv_buf;
5810 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5811 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5812 GOTO(out, rc = -ENOENT);
5814 if (lu_fid_eq(mdt_object_fid(obj), tmpfid)) {
5816 mdt_object_get(info->mti_env, mdt_obj);
5818 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5819 if (IS_ERR(mdt_obj))
5820 GOTO(out, rc = PTR_ERR(mdt_obj));
5823 if (!mdt_object_exists(mdt_obj)) {
5824 mdt_object_put(info->mti_env, mdt_obj);
5825 GOTO(out, rc = -ENOENT);
5828 if (mdt_object_remote(mdt_obj)) {
5829 mdt_object_put(info->mti_env, mdt_obj);
5830 GOTO(remote_out, rc = -EREMOTE);
5833 rc = mdt_links_read(info, mdt_obj, &ldata);
5835 mdt_object_put(info->mti_env, mdt_obj);
5840 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5841 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5842 /* If set, use link #linkno for path lookup, otherwise use
5843 link #0. Only do this for the final path element. */
5844 if (first && fp->gf_linkno < leh->leh_reccount) {
5846 for (count = 0; count < fp->gf_linkno; count++) {
5847 lee = (struct link_ea_entry *)
5848 ((char *)lee + reclen);
5849 linkea_entry_unpack(lee, &reclen, tmpname,
5852 if (fp->gf_linkno < leh->leh_reccount - 1)
5853 /* indicate to user there are more links */
5857 lmv_buf.lb_buf = info->mti_xattr_buf;
5858 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5859 /* Check if it is slave stripes */
5860 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5861 &lmv_buf, XATTR_NAME_LMV);
5862 mdt_object_put(info->mti_env, mdt_obj);
5864 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5866 /* For slave stripes, get its master */
5867 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5868 fp->gf_fid = *tmpfid;
5871 } else if (rc < 0 && rc != -ENODATA) {
5877 /* Pack the name in the end of the buffer */
5878 ptr -= tmpname->ln_namelen;
5879 if (ptr - 1 <= fp->gf_u.gf_path)
5880 GOTO(out, rc = -EOVERFLOW);
5881 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5884 /* keep the last resolved fid to the client, so the
5885 * client will build the left path on another MDT for
5887 fp->gf_fid = *tmpfid;
5893 ptr++; /* skip leading / */
5894 memmove(fp->gf_u.gf_path, ptr,
5895 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5902 * Given an MDT object, use mdt_path_current to get the path.
5903 * Essentially a wrapper to retry mdt_path_current a set number of times
5904 * if -EAGAIN is returned (usually because an object is being moved).
5906 * Part of the MDT layer implementation of lfs fid2path.
5908 * \param[in] info Per-thread common data shared by mdt level handlers.
5909 * \param[in] obj Object to do path lookup of
5910 * \param[in,out] fp User-provided struct for arguments and to store path
5913 * \retval 0 Lookup successful, path information stored in fp
5914 * \retval negative errno if there was a problem
5916 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5917 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5919 struct mdt_device *mdt = info->mti_mdt;
5924 if (fp->gf_pathlen < 3)
5927 if (root_fid == NULL)
5928 root_fid = &mdt->mdt_md_root_fid;
5930 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5931 fp->gf_u.gf_path[0] = '\0';
5935 /* Retry multiple times in case file is being moved */
5936 while (tries-- && rc == -EAGAIN)
5937 rc = mdt_path_current(info, obj, fp, root_fid);
5943 * Get the full path of the provided FID, as of changelog record recno.
5945 * This checks sanity and looks up object for user provided FID
5946 * before calling the actual path lookup code.
5948 * Part of the MDT layer implementation of lfs fid2path.
5950 * \param[in] info Per-thread common data shared by mdt level handlers.
5951 * \param[in,out] fp User-provided struct for arguments and to store path
5954 * \retval 0 Lookup successful, path information and recno stored in fp
5955 * \retval -ENOENT, object does not exist
5956 * \retval negative errno if there was a problem
5958 static int mdt_fid2path(struct mdt_thread_info *info,
5959 struct lu_fid *root_fid,
5960 struct getinfo_fid2path *fp)
5962 struct mdt_device *mdt = info->mti_mdt;
5963 struct mdt_object *obj;
5967 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5968 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5970 if (!fid_is_sane(&fp->gf_fid))
5973 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5974 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5975 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5976 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5980 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5983 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5984 PFID(&fp->gf_fid), rc);
5988 if (mdt_object_remote(obj))
5990 else if (!mdt_object_exists(obj))
5996 mdt_object_put(info->mti_env, obj);
5997 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5998 PFID(&fp->gf_fid), rc);
6002 rc = mdt_path(info, obj, fp, root_fid);
6004 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
6005 PFID(&fp->gf_fid), fp->gf_u.gf_path,
6006 fp->gf_recno, fp->gf_linkno);
6008 mdt_object_put(info->mti_env, obj);
6013 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
6014 void *val, int vallen)
6016 struct getinfo_fid2path *fpout, *fpin;
6017 struct lu_fid *root_fid = NULL;
6020 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
6023 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6024 lustre_swab_fid2path(fpin);
6026 memcpy(fpout, fpin, sizeof(*fpin));
6027 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
6030 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
6031 sizeof(struct lu_fid)) {
6032 /* client sent its root FID, which is normally fileset FID */
6033 root_fid = fpin->gf_u.gf_root_fid;
6034 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
6035 lustre_swab_lu_fid(root_fid);
6037 if (root_fid != NULL && !fid_is_sane(root_fid))
6041 rc = mdt_fid2path(info, root_fid, fpout);
6045 int mdt_get_info(struct tgt_session_info *tsi)
6055 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
6057 CDEBUG(D_IOCTL, "No GETINFO key\n");
6058 RETURN(err_serious(-EFAULT));
6060 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
6063 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
6064 if (vallen == NULL) {
6065 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
6066 tgt_name(tsi->tsi_tgt));
6067 RETURN(err_serious(-EFAULT));
6070 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
6072 rc = req_capsule_server_pack(tsi->tsi_pill);
6074 RETURN(err_serious(rc));
6076 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
6077 if (valout == NULL) {
6078 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
6079 tgt_name(tsi->tsi_tgt));
6080 RETURN(err_serious(-EFAULT));
6083 if (KEY_IS(KEY_FID2PATH)) {
6084 struct mdt_thread_info *info = tsi2mdt_info(tsi);
6086 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
6087 mdt_thread_info_fini(info);
6094 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
6096 struct obd_ioctl_data *data = karg;
6099 struct mdt_object *obj;
6100 struct mdt_lock_handle *lh;
6104 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
6105 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
6108 fid = (struct lu_fid *)data->ioc_inlbuf1;
6110 if (!fid_is_sane(fid))
6113 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
6115 lh = &mti->mti_lh[MDT_LH_PARENT];
6116 mdt_lock_reg_init(lh, LCK_CR);
6118 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
6120 RETURN(PTR_ERR(obj));
6122 if (mdt_object_remote(obj)) {
6125 * before calling version get the correct MDS should be
6126 * fid, this is error to find remote object here
6128 CERROR("nonlocal object "DFID"\n", PFID(fid));
6129 } else if (!mdt_object_exists(obj)) {
6130 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
6133 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
6134 *(__u64 *)data->ioc_inlbuf2 = version;
6137 mdt_object_unlock_put(mti, obj, lh, 1);
6141 /* ioctls on obd dev */
6142 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
6143 void *karg, void __user *uarg)
6146 struct obd_device *obd = exp->exp_obd;
6147 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
6148 struct dt_device *dt = mdt->mdt_bottom;
6152 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
6153 rc = lu_env_init(&env, LCT_MD_THREAD);
6159 rc = mdt_device_sync(&env, mdt);
6161 case OBD_IOC_SET_READONLY:
6162 rc = dt_sync(&env, dt);
6164 rc = dt_ro(&env, dt);
6166 case OBD_IOC_ABORT_RECOVERY:
6167 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
6168 obd->obd_abort_recovery = 1;
6169 target_stop_recovery_thread(obd);
6172 case OBD_IOC_CHANGELOG_REG:
6173 case OBD_IOC_CHANGELOG_DEREG:
6174 case OBD_IOC_CHANGELOG_CLEAR:
6175 rc = mdt->mdt_child->md_ops->mdo_iocontrol(&env,
6179 case OBD_IOC_START_LFSCK: {
6180 struct md_device *next = mdt->mdt_child;
6181 struct obd_ioctl_data *data = karg;
6182 struct lfsck_start_param lsp;
6184 if (unlikely(data == NULL)) {
6189 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
6190 lsp.lsp_index_valid = 0;
6191 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
6194 case OBD_IOC_STOP_LFSCK: {
6195 struct md_device *next = mdt->mdt_child;
6196 struct obd_ioctl_data *data = karg;
6197 struct lfsck_stop stop;
6199 stop.ls_status = LS_STOPPED;
6200 /* Old lfsck utils may pass NULL @stop. */
6201 if (data->ioc_inlbuf1 == NULL)
6205 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
6207 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
6210 case OBD_IOC_QUERY_LFSCK: {
6211 struct md_device *next = mdt->mdt_child;
6212 struct obd_ioctl_data *data = karg;
6214 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
6218 case OBD_IOC_GET_OBJ_VERSION: {
6219 struct mdt_thread_info *mti;
6220 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6221 memset(mti, 0, sizeof *mti);
6222 mti->mti_env = &env;
6226 rc = mdt_ioc_version_get(mti, karg);
6229 case OBD_IOC_CATLOGLIST: {
6230 struct mdt_thread_info *mti;
6232 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6233 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6234 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6235 &mti->mti_tmp_fid1);
6240 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6241 mdt_obd_name(mdt), cmd, rc);
6248 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6250 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6254 if (!mdt->mdt_skip_lfsck && !mdt->mdt_bottom->dd_rdonly) {
6255 struct lfsck_start_param lsp;
6257 lsp.lsp_start = NULL;
6258 lsp.lsp_index_valid = 0;
6259 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6260 OBD_IOC_START_LFSCK,
6262 if (rc != 0 && rc != -EALREADY)
6263 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6264 mdt_obd_name(mdt), rc);
6267 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6271 static int mdt_obd_postrecov(struct obd_device *obd)
6276 rc = lu_env_init(&env, LCT_MD_THREAD);
6279 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6284 static struct obd_ops mdt_obd_device_ops = {
6285 .o_owner = THIS_MODULE,
6286 .o_set_info_async = mdt_obd_set_info_async,
6287 .o_connect = mdt_obd_connect,
6288 .o_reconnect = mdt_obd_reconnect,
6289 .o_disconnect = mdt_obd_disconnect,
6290 .o_init_export = mdt_init_export,
6291 .o_destroy_export = mdt_destroy_export,
6292 .o_iocontrol = mdt_iocontrol,
6293 .o_postrecov = mdt_obd_postrecov,
6296 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6297 struct lu_device *d)
6299 struct mdt_device *m = mdt_dev(d);
6306 static struct lu_device *mdt_device_free(const struct lu_env *env,
6307 struct lu_device *d)
6309 struct mdt_device *m = mdt_dev(d);
6312 lu_device_fini(&m->mdt_lu_dev);
6318 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6319 struct lu_device_type *t,
6320 struct lustre_cfg *cfg)
6322 struct lu_device *l;
6323 struct mdt_device *m;
6330 rc = mdt_init0(env, m, t, cfg);
6332 mdt_device_free(env, l);
6337 l = ERR_PTR(-ENOMEM);
6341 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6342 LU_KEY_INIT(mdt, struct mdt_thread_info);
6344 static void mdt_key_fini(const struct lu_context *ctx,
6345 struct lu_context_key *key, void* data)
6347 struct mdt_thread_info *info = data;
6349 if (info->mti_big_lmm) {
6350 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6351 info->mti_big_lmm = NULL;
6352 info->mti_big_lmmsize = 0;
6355 if (info->mti_big_acl) {
6356 OBD_FREE_LARGE(info->mti_big_acl, info->mti_big_aclsize);
6357 info->mti_big_acl = NULL;
6358 info->mti_big_aclsize = 0;
6364 /* context key: mdt_thread_key */
6365 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6367 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6369 return lu_ucred(info->mti_env);
6372 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6374 return lu_ucred_check(info->mti_env);
6378 * Enable/disable COS (Commit On Sharing).
6380 * Set/Clear the COS flag in mdt options.
6382 * \param mdt mdt device
6383 * \param val 0 disables COS, other values enable COS
6385 void mdt_enable_cos(struct mdt_device *mdt, int val)
6390 mdt->mdt_opts.mo_cos = !!val;
6391 rc = lu_env_init(&env, LCT_LOCAL);
6392 if (unlikely(rc != 0)) {
6393 CWARN("%s: lu_env initialization failed, cannot "
6394 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6397 mdt_device_sync(&env, mdt);
6402 * Check COS (Commit On Sharing) status.
6404 * Return COS flag status.
6406 * \param mdt mdt device
6408 int mdt_cos_is_enabled(struct mdt_device *mdt)
6410 return mdt->mdt_opts.mo_cos != 0;
6413 static struct lu_device_type_operations mdt_device_type_ops = {
6414 .ldto_device_alloc = mdt_device_alloc,
6415 .ldto_device_free = mdt_device_free,
6416 .ldto_device_fini = mdt_device_fini
6419 static struct lu_device_type mdt_device_type = {
6420 .ldt_tags = LU_DEVICE_MD,
6421 .ldt_name = LUSTRE_MDT_NAME,
6422 .ldt_ops = &mdt_device_type_ops,
6423 .ldt_ctx_tags = LCT_MD_THREAD
6426 static int __init mdt_init(void)
6430 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6431 FID_NOBRACE_LEN + 1);
6432 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6434 rc = lu_kmem_init(mdt_caches);
6438 rc = mds_mod_init();
6442 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6443 LUSTRE_MDT_NAME, &mdt_device_type);
6448 lu_kmem_fini(mdt_caches);
6455 static void __exit mdt_exit(void)
6457 class_unregister_type(LUSTRE_MDT_NAME);
6459 lu_kmem_fini(mdt_caches);
6462 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6463 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6464 MODULE_VERSION(LUSTRE_VERSION_STRING);
6465 MODULE_LICENSE("GPL");
6467 module_init(mdt_init);
6468 module_exit(mdt_exit);