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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_handler.c
38 * Lustre Metadata Target (mdt) request handler
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Andreas Dilger <adilger@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
43 * Author: Mike Shaver <shaver@clusterfs.com>
44 * Author: Nikita Danilov <nikita@clusterfs.com>
45 * Author: Huang Hua <huanghua@clusterfs.com>
46 * Author: Yury Umanets <umka@clusterfs.com>
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
52 #include <linux/pagemap.h>
54 #include <dt_object.h>
55 #include <lustre_acl.h>
56 #include <lustre_export.h>
57 #include <lustre_ioctl.h>
58 #include <lustre_lfsck.h>
59 #include <lustre_log.h>
60 #include <lustre_net.h>
61 #include <lustre_nodemap.h>
62 #include <lustre_mds.h>
63 #include <lustre_param.h>
64 #include <lustre_quota.h>
65 #include <lustre_swab.h>
67 #include <obd_support.h>
69 #include <llog_swab.h>
71 #include "mdt_internal.h"
74 static unsigned int max_mod_rpcs_per_client = 8;
75 module_param(max_mod_rpcs_per_client, uint, 0644);
76 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
78 mdl_mode_t mdt_mdl_lock_modes[] = {
79 [LCK_MINMODE] = MDL_MINMODE,
86 [LCK_GROUP] = MDL_GROUP
89 enum ldlm_mode mdt_dlm_lock_modes[] = {
90 [MDL_MINMODE] = LCK_MINMODE,
97 [MDL_GROUP] = LCK_GROUP
100 static struct mdt_device *mdt_dev(struct lu_device *d);
101 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
103 static const struct lu_object_operations mdt_obj_ops;
105 /* Slab for MDT object allocation */
106 static struct kmem_cache *mdt_object_kmem;
108 /* For HSM restore handles */
109 struct kmem_cache *mdt_hsm_cdt_kmem;
111 /* For HSM request handles */
112 struct kmem_cache *mdt_hsm_car_kmem;
114 static struct lu_kmem_descr mdt_caches[] = {
116 .ckd_cache = &mdt_object_kmem,
117 .ckd_name = "mdt_obj",
118 .ckd_size = sizeof(struct mdt_object)
121 .ckd_cache = &mdt_hsm_cdt_kmem,
122 .ckd_name = "mdt_cdt_restore_handle",
123 .ckd_size = sizeof(struct cdt_restore_handle)
126 .ckd_cache = &mdt_hsm_car_kmem,
127 .ckd_name = "mdt_cdt_agent_req",
128 .ckd_size = sizeof(struct cdt_agent_req)
135 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
139 return rep->lock_policy_res1 & op_flag;
142 void mdt_clear_disposition(struct mdt_thread_info *info,
143 struct ldlm_reply *rep, __u64 op_flag)
146 info->mti_opdata &= ~op_flag;
147 tgt_opdata_clear(info->mti_env, op_flag);
150 rep->lock_policy_res1 &= ~op_flag;
153 void mdt_set_disposition(struct mdt_thread_info *info,
154 struct ldlm_reply *rep, __u64 op_flag)
157 info->mti_opdata |= op_flag;
158 tgt_opdata_set(info->mti_env, op_flag);
161 rep->lock_policy_res1 |= op_flag;
164 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
166 lh->mlh_pdo_hash = 0;
167 lh->mlh_reg_mode = lm;
168 lh->mlh_rreg_mode = lm;
169 lh->mlh_type = MDT_REG_LOCK;
172 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
173 const struct lu_name *lname)
175 lh->mlh_reg_mode = lock_mode;
176 lh->mlh_pdo_mode = LCK_MINMODE;
177 lh->mlh_rreg_mode = lock_mode;
178 lh->mlh_type = MDT_PDO_LOCK;
180 if (lu_name_is_valid(lname)) {
181 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
183 /* XXX Workaround for LU-2856
185 * Zero is a valid return value of full_name_hash, but
186 * several users of mlh_pdo_hash assume a non-zero
187 * hash value. We therefore map zero onto an
188 * arbitrary, but consistent value (1) to avoid
189 * problems further down the road. */
190 if (unlikely(lh->mlh_pdo_hash == 0))
191 lh->mlh_pdo_hash = 1;
193 lh->mlh_pdo_hash = 0;
197 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
198 struct mdt_lock_handle *lh)
204 * Any dir access needs couple of locks:
206 * 1) on part of dir we gonna take lookup/modify;
208 * 2) on whole dir to protect it from concurrent splitting and/or to
209 * flush client's cache for readdir().
211 * so, for a given mode and object this routine decides what lock mode
212 * to use for lock #2:
214 * 1) if caller's gonna lookup in dir then we need to protect dir from
215 * being splitted only - LCK_CR
217 * 2) if caller's gonna modify dir then we need to protect dir from
218 * being splitted and to flush cache - LCK_CW
220 * 3) if caller's gonna modify dir and that dir seems ready for
221 * splitting then we need to protect it from any type of access
222 * (lookup/modify/split) - LCK_EX --bzzz
225 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
226 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
229 * Ask underlaying level its opinion about preferable PDO lock mode
230 * having access type passed as regular lock mode:
232 * - MDL_MINMODE means that lower layer does not want to specify lock
235 * - MDL_NL means that no PDO lock should be taken. This is used in some
236 * cases. Say, for non-splittable directories no need to use PDO locks
239 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
240 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
242 if (mode != MDL_MINMODE) {
243 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
246 * Lower layer does not want to specify locking mode. We do it
247 * our selves. No special protection is needed, just flush
248 * client's cache on modification and allow concurrent
251 switch (lh->mlh_reg_mode) {
253 lh->mlh_pdo_mode = LCK_EX;
256 lh->mlh_pdo_mode = LCK_CR;
259 lh->mlh_pdo_mode = LCK_CW;
262 CERROR("Not expected lock type (0x%x)\n",
263 (int)lh->mlh_reg_mode);
268 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
272 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
275 struct mdt_device *mdt = info->mti_mdt;
276 struct lu_name *lname = &info->mti_name;
278 struct mdt_object *parent;
282 LASSERT(!info->mti_cross_ref);
284 OBD_ALLOC(name, NAME_MAX + 1);
287 lname->ln_name = name;
290 * We may want to allow this to mount a completely separate
291 * fileset from the MDT in the future, but keeping it to
292 * ROOT/ only for now avoid potential security issues.
294 *fid = mdt->mdt_md_root_fid;
296 while (rc == 0 && fileset != NULL && *fileset != '\0') {
297 const char *s1 = fileset;
303 while (*s2 != '/' && *s2 != '\0')
311 lname->ln_namelen = s2 - s1;
312 if (lname->ln_namelen > NAME_MAX) {
317 /* reject .. as a path component */
318 if (lname->ln_namelen == 2 &&
319 strncmp(s1, "..", 2) == 0) {
324 strncpy(name, s1, lname->ln_namelen);
325 name[lname->ln_namelen] = '\0';
327 parent = mdt_object_find(info->mti_env, mdt, fid);
328 if (IS_ERR(parent)) {
329 rc = PTR_ERR(parent);
332 /* Only got the fid of this obj by name */
334 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
335 fid, &info->mti_spec);
336 mdt_object_put(info->mti_env, parent);
339 parent = mdt_object_find(info->mti_env, mdt, fid);
341 rc = PTR_ERR(parent);
343 mode = lu_object_attr(&parent->mot_obj);
344 mdt_object_put(info->mti_env, parent);
350 OBD_FREE(name, NAME_MAX + 1);
355 static int mdt_get_root(struct tgt_session_info *tsi)
357 struct mdt_thread_info *info = tsi2mdt_info(tsi);
358 struct mdt_device *mdt = info->mti_mdt;
359 struct mdt_body *repbody;
360 char *fileset = NULL, *buffer = NULL;
362 struct obd_export *exp = info->mti_exp;
363 char *nodemap_fileset;
367 rc = mdt_check_ucred(info);
369 GOTO(out, rc = err_serious(rc));
371 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
372 GOTO(out, rc = err_serious(-ENOMEM));
374 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
375 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
376 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
378 GOTO(out, rc = err_serious(-EFAULT));
381 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
382 if (nodemap_fileset && nodemap_fileset[0]) {
383 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
385 /* consider fileset from client as a sub-fileset
386 * of the nodemap one */
387 OBD_ALLOC(buffer, PATH_MAX + 1);
389 GOTO(out, rc = err_serious(-ENOMEM));
390 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
391 nodemap_fileset, fileset) >= PATH_MAX + 1)
392 GOTO(out, rc = err_serious(-EINVAL));
395 /* enforce fileset as specified in the nodemap */
396 fileset = nodemap_fileset;
401 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
402 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
404 GOTO(out, rc = err_serious(rc));
406 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
408 repbody->mbo_valid |= OBD_MD_FLID;
412 mdt_thread_info_fini(info);
414 OBD_FREE(buffer, PATH_MAX+1);
418 static int mdt_statfs(struct tgt_session_info *tsi)
420 struct ptlrpc_request *req = tgt_ses_req(tsi);
421 struct mdt_thread_info *info = tsi2mdt_info(tsi);
422 struct md_device *next = info->mti_mdt->mdt_child;
423 struct ptlrpc_service_part *svcpt;
424 struct obd_statfs *osfs;
429 svcpt = req->rq_rqbd->rqbd_svcpt;
431 /* This will trigger a watchdog timeout */
432 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
433 (MDT_SERVICE_WATCHDOG_FACTOR *
434 at_get(&svcpt->scp_at_estimate)) + 1);
436 rc = mdt_check_ucred(info);
438 GOTO(out, rc = err_serious(rc));
440 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
441 GOTO(out, rc = err_serious(-ENOMEM));
443 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
445 GOTO(out, rc = -EPROTO);
447 /** statfs information are cached in the mdt_device */
448 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
449 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
450 /** statfs data is too old, get up-to-date one */
451 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
454 spin_lock(&info->mti_mdt->mdt_lock);
455 info->mti_mdt->mdt_osfs = *osfs;
456 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
457 spin_unlock(&info->mti_mdt->mdt_lock);
459 /** use cached statfs data */
460 spin_lock(&info->mti_mdt->mdt_lock);
461 *osfs = info->mti_mdt->mdt_osfs;
462 spin_unlock(&info->mti_mdt->mdt_lock);
466 mdt_counter_incr(req, LPROC_MDT_STATFS);
468 mdt_thread_info_fini(info);
472 #ifdef CONFIG_FS_POSIX_ACL
474 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
476 * \param info thread info object
477 * \param repbody reply to pack ACLs into
478 * \param o mdt object of file to examine
479 * \param nodemap nodemap of client to reply to
481 * \retval -errno error getting or parsing ACL from disk
483 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
484 struct mdt_object *o, struct lu_nodemap *nodemap)
486 const struct lu_env *env = info->mti_env;
487 struct md_object *next = mdt_object_child(o);
488 struct lu_buf *buf = &info->mti_buf;
491 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
492 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
494 if (buf->lb_len == 0)
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) {
506 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
507 mdt_obd_name(info->mti_mdt),
508 PFID(mdt_object_fid(o)), rc);
511 rc = nodemap_map_acl(nodemap, buf->lb_buf,
512 rc, NODEMAP_FS_TO_CLIENT);
513 /* if all ACLs mapped out, rc is still >= 0 */
515 CERROR("%s: nodemap_map_acl unable to parse "DFID
516 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
517 PFID(mdt_object_fid(o)), rc);
519 repbody->mbo_aclsize = rc;
520 repbody->mbo_valid |= OBD_MD_FLACL;
528 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
529 const struct lu_attr *attr, const struct lu_fid *fid)
531 struct md_attr *ma = &info->mti_attr;
532 struct obd_export *exp = info->mti_exp;
533 struct lu_nodemap *nodemap = NULL;
535 LASSERT(ma->ma_valid & MA_INODE);
537 if (attr->la_valid & LA_ATIME) {
538 b->mbo_atime = attr->la_atime;
539 b->mbo_valid |= OBD_MD_FLATIME;
541 if (attr->la_valid & LA_MTIME) {
542 b->mbo_mtime = attr->la_mtime;
543 b->mbo_valid |= OBD_MD_FLMTIME;
545 if (attr->la_valid & LA_CTIME) {
546 b->mbo_ctime = attr->la_ctime;
547 b->mbo_valid |= OBD_MD_FLCTIME;
549 if (attr->la_valid & LA_FLAGS) {
550 b->mbo_flags = attr->la_flags;
551 b->mbo_valid |= OBD_MD_FLFLAGS;
553 if (attr->la_valid & LA_NLINK) {
554 b->mbo_nlink = attr->la_nlink;
555 b->mbo_valid |= OBD_MD_FLNLINK;
557 if (attr->la_valid & (LA_UID|LA_GID)) {
558 nodemap = nodemap_get_from_exp(exp);
562 if (attr->la_valid & LA_UID) {
563 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
564 NODEMAP_FS_TO_CLIENT,
566 b->mbo_valid |= OBD_MD_FLUID;
568 if (attr->la_valid & LA_GID) {
569 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
570 NODEMAP_FS_TO_CLIENT,
572 b->mbo_valid |= OBD_MD_FLGID;
575 b->mbo_mode = attr->la_mode;
576 if (attr->la_valid & LA_MODE)
577 b->mbo_valid |= OBD_MD_FLMODE;
578 if (attr->la_valid & LA_TYPE)
579 b->mbo_valid |= OBD_MD_FLTYPE;
583 b->mbo_valid |= OBD_MD_FLID;
584 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid="LPX64"\n",
585 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
588 if (!(attr->la_valid & LA_TYPE))
591 b->mbo_rdev = attr->la_rdev;
592 b->mbo_size = attr->la_size;
593 b->mbo_blocks = attr->la_blocks;
595 if (!S_ISREG(attr->la_mode)) {
596 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
597 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
598 /* means no objects are allocated on osts. */
599 LASSERT(!(ma->ma_valid & MA_LOV));
600 /* just ignore blocks occupied by extend attributes on MDS */
602 /* if no object is allocated on osts, the size on mds is valid.
604 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
605 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
606 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
607 /* A released file stores its size on MDS. */
608 /* But return 1 block for released file, unless tools like tar
609 * will consider it fully sparse. (LU-3864)
611 if (unlikely(b->mbo_size == 0))
615 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
618 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
619 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
620 PFID(fid), (unsigned long long)b->mbo_size);
623 if (!IS_ERR_OR_NULL(nodemap))
624 nodemap_putref(nodemap);
627 static inline int mdt_body_has_lov(const struct lu_attr *la,
628 const struct mdt_body *body)
630 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
631 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
634 void mdt_client_compatibility(struct mdt_thread_info *info)
636 struct mdt_body *body;
637 struct ptlrpc_request *req = mdt_info_req(info);
638 struct obd_export *exp = req->rq_export;
639 struct md_attr *ma = &info->mti_attr;
640 struct lu_attr *la = &ma->ma_attr;
643 if (exp_connect_layout(exp))
644 /* the client can deal with 16-bit lmm_stripe_count */
647 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
649 if (!mdt_body_has_lov(la, body))
652 /* now we have a reply with a lov for a client not compatible with the
653 * layout lock so we have to clean the layout generation number */
654 if (S_ISREG(la->la_mode))
655 ma->ma_lmm->lmm_layout_gen = 0;
659 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
660 struct mdt_object *o)
662 const struct lu_env *env = info->mti_env;
665 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
674 /* Is it a directory? Let's check for the LMV as well */
675 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
676 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
680 rc2 = mo_xattr_get(env, mdt_object_child(o),
682 XATTR_NAME_DEFAULT_LMV);
684 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
692 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
695 const struct lu_env *env = info->mti_env;
699 LASSERT(info->mti_big_lmm_used == 0);
700 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
704 /* big_lmm may need to be grown */
705 if (info->mti_big_lmmsize < rc) {
706 int size = size_roundup_power2(rc);
708 if (info->mti_big_lmmsize > 0) {
709 /* free old buffer */
710 LASSERT(info->mti_big_lmm);
711 OBD_FREE_LARGE(info->mti_big_lmm,
712 info->mti_big_lmmsize);
713 info->mti_big_lmm = NULL;
714 info->mti_big_lmmsize = 0;
717 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
718 if (info->mti_big_lmm == NULL)
720 info->mti_big_lmmsize = size;
722 LASSERT(info->mti_big_lmmsize >= rc);
724 info->mti_buf.lb_buf = info->mti_big_lmm;
725 info->mti_buf.lb_len = info->mti_big_lmmsize;
726 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
731 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
732 struct md_attr *ma, const char *name)
734 struct md_object *next = mdt_object_child(o);
735 struct lu_buf *buf = &info->mti_buf;
738 if (strcmp(name, XATTR_NAME_LOV) == 0) {
739 buf->lb_buf = ma->ma_lmm;
740 buf->lb_len = ma->ma_lmm_size;
741 LASSERT(!(ma->ma_valid & MA_LOV));
742 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
743 buf->lb_buf = ma->ma_lmv;
744 buf->lb_len = ma->ma_lmv_size;
745 LASSERT(!(ma->ma_valid & MA_LMV));
746 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
747 buf->lb_buf = ma->ma_lmv;
748 buf->lb_len = ma->ma_lmv_size;
749 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
754 rc = mo_xattr_get(info->mti_env, next, buf, name);
758 if (strcmp(name, XATTR_NAME_LOV) == 0) {
759 if (info->mti_big_lmm_used)
760 ma->ma_lmm = info->mti_big_lmm;
762 /* NOT return LOV EA with hole to old client. */
763 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
764 LOV_PATTERN_F_HOLE) &&
765 !(exp_connect_flags(info->mti_exp) &
766 OBD_CONNECT_LFSCK)) {
769 ma->ma_lmm_size = rc;
770 ma->ma_valid |= MA_LOV;
772 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
773 if (info->mti_big_lmm_used)
774 ma->ma_lmv = info->mti_big_lmm;
776 ma->ma_lmv_size = rc;
777 ma->ma_valid |= MA_LMV;
778 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
779 ma->ma_lmv_size = rc;
780 ma->ma_valid |= MA_LMV_DEF;
783 /* Update mdt_max_mdsize so all clients will be aware that */
784 if (info->mti_mdt->mdt_max_mdsize < rc)
785 info->mti_mdt->mdt_max_mdsize = rc;
788 } else if (rc == -ENODATA) {
791 } else if (rc == -ERANGE) {
792 /* Default LMV has fixed size, so it must be able to fit
793 * in the original buffer */
794 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
796 rc = mdt_big_xattr_get(info, o, name);
798 info->mti_big_lmm_used = 1;
806 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
807 struct mdt_object *o, struct lu_fid *pfid)
809 struct lu_buf *buf = &info->mti_buf;
810 struct link_ea_header *leh;
811 struct link_ea_entry *lee;
815 buf->lb_buf = info->mti_big_lmm;
816 buf->lb_len = info->mti_big_lmmsize;
817 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
818 buf, XATTR_NAME_LINK);
819 /* ignore errors, MA_PFID won't be set and it is
820 * up to the caller to treat this as an error */
821 if (rc == -ERANGE || buf->lb_len == 0) {
822 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
823 buf->lb_buf = info->mti_big_lmm;
824 buf->lb_len = info->mti_big_lmmsize;
829 if (rc < sizeof(*leh)) {
830 CERROR("short LinkEA on "DFID": rc = %d\n",
831 PFID(mdt_object_fid(o)), rc);
835 leh = (struct link_ea_header *) buf->lb_buf;
836 lee = (struct link_ea_entry *)(leh + 1);
837 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
838 leh->leh_magic = LINK_EA_MAGIC;
839 leh->leh_reccount = __swab32(leh->leh_reccount);
840 leh->leh_len = __swab64(leh->leh_len);
842 if (leh->leh_magic != LINK_EA_MAGIC)
844 if (leh->leh_reccount == 0)
847 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
848 fid_be_to_cpu(pfid, pfid);
853 int mdt_attr_get_complex(struct mdt_thread_info *info,
854 struct mdt_object *o, struct md_attr *ma)
856 const struct lu_env *env = info->mti_env;
857 struct md_object *next = mdt_object_child(o);
858 struct lu_buf *buf = &info->mti_buf;
859 int need = ma->ma_need;
866 if (mdt_object_exists(o) == 0)
867 GOTO(out, rc = -ENOENT);
868 mode = lu_object_attr(&next->mo_lu);
870 if (need & MA_INODE) {
871 ma->ma_need = MA_INODE;
872 rc = mo_attr_get(env, next, ma);
875 ma->ma_valid |= MA_INODE;
878 if (need & MA_PFID) {
879 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
881 ma->ma_valid |= MA_PFID;
882 /* ignore this error, parent fid is not mandatory */
886 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
887 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
892 if (need & MA_LMV && S_ISDIR(mode)) {
893 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
898 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
899 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
904 if (need & MA_HSM && S_ISREG(mode)) {
905 buf->lb_buf = info->mti_xattr_buf;
906 buf->lb_len = sizeof(info->mti_xattr_buf);
907 CLASSERT(sizeof(struct hsm_attrs) <=
908 sizeof(info->mti_xattr_buf));
909 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
910 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
912 ma->ma_valid |= MA_HSM;
913 else if (rc2 < 0 && rc2 != -ENODATA)
917 #ifdef CONFIG_FS_POSIX_ACL
918 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
919 buf->lb_buf = ma->ma_acl;
920 buf->lb_len = ma->ma_acl_size;
921 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
923 ma->ma_acl_size = rc2;
924 ma->ma_valid |= MA_ACL_DEF;
925 } else if (rc2 == -ENODATA) {
934 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
935 rc, ma->ma_valid, ma->ma_lmm);
939 static int mdt_getattr_internal(struct mdt_thread_info *info,
940 struct mdt_object *o, int ma_need)
942 struct md_object *next = mdt_object_child(o);
943 const struct mdt_body *reqbody = info->mti_body;
944 struct ptlrpc_request *req = mdt_info_req(info);
945 struct md_attr *ma = &info->mti_attr;
946 struct lu_attr *la = &ma->ma_attr;
947 struct req_capsule *pill = info->mti_pill;
948 const struct lu_env *env = info->mti_env;
949 struct mdt_body *repbody;
950 struct lu_buf *buffer = &info->mti_buf;
951 struct obd_export *exp = info->mti_exp;
956 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
957 RETURN(err_serious(-ENOMEM));
959 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
963 if (mdt_object_remote(o)) {
964 /* This object is located on remote node.*/
965 /* Return -ENOTSUPP for old client */
966 if (!mdt_is_dne_client(req->rq_export))
967 GOTO(out, rc = -ENOTSUPP);
969 repbody->mbo_fid1 = *mdt_object_fid(o);
970 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
974 if (reqbody->mbo_eadatasize > 0) {
975 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
976 if (buffer->lb_buf == NULL)
977 GOTO(out, rc = -EPROTO);
978 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
981 buffer->lb_buf = NULL;
983 ma_need &= ~(MA_LOV | MA_LMV);
984 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
985 mdt_obd_name(info->mti_mdt),
986 req->rq_export->exp_client_uuid.uuid);
989 /* If it is dir object and client require MEA, then we got MEA */
990 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
991 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
992 /* Assumption: MDT_MD size is enough for lmv size. */
993 ma->ma_lmv = buffer->lb_buf;
994 ma->ma_lmv_size = buffer->lb_len;
995 ma->ma_need = MA_INODE;
996 if (ma->ma_lmv_size > 0) {
997 if (reqbody->mbo_valid & OBD_MD_MEA)
998 ma->ma_need |= MA_LMV;
999 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
1000 ma->ma_need |= MA_LMV_DEF;
1003 ma->ma_lmm = buffer->lb_buf;
1004 ma->ma_lmm_size = buffer->lb_len;
1005 ma->ma_need = MA_INODE | MA_HSM;
1006 if (ma->ma_lmm_size > 0)
1007 ma->ma_need |= MA_LOV;
1010 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1011 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1012 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1013 /* get default stripe info for this dir. */
1014 ma->ma_need |= MA_LOV_DEF;
1016 ma->ma_need |= ma_need;
1018 rc = mdt_attr_get_complex(info, o, ma);
1020 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1021 "%s: getattr error for "DFID": rc = %d\n",
1022 mdt_obd_name(info->mti_mdt),
1023 PFID(mdt_object_fid(o)), rc);
1027 /* if file is released, check if a restore is running */
1028 if (ma->ma_valid & MA_HSM) {
1029 repbody->mbo_valid |= OBD_MD_TSTATE;
1030 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1031 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1032 repbody->mbo_t_state = MS_RESTORE;
1035 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
1037 /* the Lustre protocol supposes to return default striping
1038 * on the user-visible root if explicitly requested */
1039 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
1040 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
1041 struct lu_fid rootfid;
1042 struct mdt_object *root;
1043 struct mdt_device *mdt = info->mti_mdt;
1045 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
1048 root = mdt_object_find(env, mdt, &rootfid);
1050 RETURN(PTR_ERR(root));
1051 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
1052 mdt_object_put(info->mti_env, root);
1054 CERROR("%s: getattr error for "DFID": rc = %d\n",
1055 mdt_obd_name(info->mti_mdt),
1056 PFID(mdt_object_fid(o)), rc);
1061 if (likely(ma->ma_valid & MA_INODE))
1062 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1066 if (mdt_body_has_lov(la, reqbody)) {
1067 if (ma->ma_valid & MA_LOV) {
1068 LASSERT(ma->ma_lmm_size);
1069 repbody->mbo_eadatasize = ma->ma_lmm_size;
1070 if (S_ISDIR(la->la_mode))
1071 repbody->mbo_valid |= OBD_MD_FLDIREA;
1073 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1074 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1076 if (ma->ma_valid & MA_LMV) {
1077 /* Return -ENOTSUPP for old client */
1078 if (!mdt_is_striped_client(req->rq_export))
1081 LASSERT(S_ISDIR(la->la_mode));
1082 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1083 repbody->mbo_eadatasize = ma->ma_lmv_size;
1084 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1086 if (ma->ma_valid & MA_LMV_DEF) {
1087 /* Return -ENOTSUPP for old client */
1088 if (!mdt_is_striped_client(req->rq_export))
1090 LASSERT(S_ISDIR(la->la_mode));
1091 repbody->mbo_eadatasize = ma->ma_lmv_size;
1092 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1093 OBD_MD_DEFAULT_MEA);
1095 } else if (S_ISLNK(la->la_mode) &&
1096 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1097 buffer->lb_buf = ma->ma_lmm;
1098 /* eadatasize from client includes NULL-terminator, so
1099 * there is no need to read it */
1100 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1101 rc = mo_readlink(env, next, buffer);
1102 if (unlikely(rc <= 0)) {
1103 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1104 mdt_obd_name(info->mti_mdt),
1105 PFID(mdt_object_fid(o)), rc);
1108 int print_limit = min_t(int, PAGE_CACHE_SIZE - 128, rc);
1110 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1112 repbody->mbo_valid |= OBD_MD_LINKNAME;
1113 /* we need to report back size with NULL-terminator
1114 * because client expects that */
1115 repbody->mbo_eadatasize = rc + 1;
1116 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1117 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1118 "on "DFID ", expected %d\n",
1119 mdt_obd_name(info->mti_mdt),
1120 rc, PFID(mdt_object_fid(o)),
1121 reqbody->mbo_eadatasize - 1);
1122 /* NULL terminate */
1123 ((char *)ma->ma_lmm)[rc] = 0;
1125 /* If the total CDEBUG() size is larger than a page, it
1126 * will print a warning to the console, avoid this by
1127 * printing just the last part of the symlink. */
1128 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1129 print_limit < rc ? "..." : "", print_limit,
1130 (char *)ma->ma_lmm + rc - print_limit, rc);
1135 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1136 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1137 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1138 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1139 repbody->mbo_max_mdsize);
1142 #ifdef CONFIG_FS_POSIX_ACL
1143 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1144 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1145 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1146 if (IS_ERR(nodemap))
1147 RETURN(PTR_ERR(nodemap));
1149 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1150 nodemap_putref(nodemap);
1156 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1161 static int mdt_getattr(struct tgt_session_info *tsi)
1163 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1164 struct mdt_object *obj = info->mti_object;
1165 struct req_capsule *pill = info->mti_pill;
1166 struct mdt_body *reqbody;
1167 struct mdt_body *repbody;
1171 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1173 LASSERT(obj != NULL);
1174 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1176 /* Unlike intent case where we need to pre-fill out buffers early on
1177 * in intent policy for ldlm reasons, here we can have a much better
1178 * guess at EA size by just reading it from disk.
1179 * Exceptions are readdir and (missing) directory striping */
1181 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1182 /* No easy way to know how long is the symlink, but it cannot
1183 * be more than PATH_MAX, so we allocate +1 */
1186 /* A special case for fs ROOT: getattr there might fetch
1187 * default EA for entire fs, not just for this dir!
1189 } else if (lu_fid_eq(mdt_object_fid(obj),
1190 &info->mti_mdt->mdt_md_root_fid) &&
1191 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1192 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1194 /* Should the default strping be bigger, mdt_fix_reply
1195 * will reallocate */
1196 rc = DEF_REP_MD_SIZE;
1198 /* Read the actual EA size from disk */
1199 rc = mdt_attr_get_eabuf_size(info, obj);
1203 GOTO(out, rc = err_serious(rc));
1205 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1207 rc = req_capsule_server_pack(pill);
1208 if (unlikely(rc != 0))
1209 GOTO(out, rc = err_serious(rc));
1211 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1212 LASSERT(repbody != NULL);
1213 repbody->mbo_eadatasize = 0;
1214 repbody->mbo_aclsize = 0;
1216 rc = mdt_check_ucred(info);
1218 GOTO(out_shrink, rc);
1220 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1222 rc = mdt_getattr_internal(info, obj, 0);
1225 mdt_client_compatibility(info);
1226 rc2 = mdt_fix_reply(info);
1230 mdt_thread_info_fini(info);
1235 * Exchange MOF_LOV_CREATED flags between two objects after a
1236 * layout swap. No assumption is made on whether o1 or o2 have
1237 * created objects or not.
1239 * \param[in,out] o1 First swap layout object
1240 * \param[in,out] o2 Second swap layout object
1242 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1244 unsigned int o1_lov_created = o1->mot_lov_created;
1246 mutex_lock(&o1->mot_lov_mutex);
1247 mutex_lock(&o2->mot_lov_mutex);
1249 o1->mot_lov_created = o2->mot_lov_created;
1250 o2->mot_lov_created = o1_lov_created;
1252 mutex_unlock(&o2->mot_lov_mutex);
1253 mutex_unlock(&o1->mot_lov_mutex);
1256 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1258 struct mdt_thread_info *info;
1259 struct ptlrpc_request *req = tgt_ses_req(tsi);
1260 struct obd_export *exp = req->rq_export;
1261 struct mdt_object *o1, *o2, *o;
1262 struct mdt_lock_handle *lh1, *lh2;
1263 struct mdc_swap_layouts *msl;
1267 /* client does not support layout lock, so layout swaping
1269 * FIXME: there is a problem for old clients which don't support
1270 * layout lock yet. If those clients have already opened the file
1271 * they won't be notified at all so that old layout may still be
1272 * used to do IO. This can be fixed after file release is landed by
1273 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1274 if (!exp_connect_layout(exp))
1275 RETURN(-EOPNOTSUPP);
1277 info = tsi2mdt_info(tsi);
1279 if (info->mti_dlm_req != NULL)
1280 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1282 o1 = info->mti_object;
1283 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1284 &info->mti_body->mbo_fid2);
1286 GOTO(out, rc = PTR_ERR(o));
1288 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1289 GOTO(put, rc = -ENOENT);
1291 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1292 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1298 /* permission check. Make sure the calling process having permission
1299 * to write both files. */
1300 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1305 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1310 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1312 GOTO(put, rc = -EPROTO);
1314 lh1 = &info->mti_lh[MDT_LH_NEW];
1315 mdt_lock_reg_init(lh1, LCK_EX);
1316 lh2 = &info->mti_lh[MDT_LH_OLD];
1317 mdt_lock_reg_init(lh2, LCK_EX);
1319 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1320 MDS_INODELOCK_XATTR);
1324 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1325 MDS_INODELOCK_XATTR);
1329 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1330 mdt_object_child(o2), msl->msl_flags);
1334 mdt_swap_lov_flag(o1, o2);
1337 mdt_object_unlock(info, o2, lh2, rc);
1339 mdt_object_unlock(info, o1, lh1, rc);
1341 mdt_object_put(info->mti_env, o);
1343 mdt_thread_info_fini(info);
1347 static int mdt_raw_lookup(struct mdt_thread_info *info,
1348 struct mdt_object *parent,
1349 const struct lu_name *lname,
1350 struct ldlm_reply *ldlm_rep)
1352 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1356 LASSERT(!info->mti_cross_ref);
1358 /* Only got the fid of this obj by name */
1359 fid_zero(child_fid);
1360 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1361 lname, child_fid, &info->mti_spec);
1363 struct mdt_body *repbody;
1365 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1366 repbody->mbo_fid1 = *child_fid;
1367 repbody->mbo_valid = OBD_MD_FLID;
1368 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1369 } else if (rc == -ENOENT) {
1370 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1377 * UPDATE lock should be taken against parent, and be released before exit;
1378 * child_bits lock should be taken against child, and be returned back:
1379 * (1)normal request should release the child lock;
1380 * (2)intent request will grant the lock to client.
1382 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1383 struct mdt_lock_handle *lhc,
1385 struct ldlm_reply *ldlm_rep)
1387 struct ptlrpc_request *req = mdt_info_req(info);
1388 struct mdt_body *reqbody = NULL;
1389 struct mdt_object *parent = info->mti_object;
1390 struct mdt_object *child;
1391 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1392 struct lu_name *lname = NULL;
1393 struct mdt_lock_handle *lhp = NULL;
1394 struct ldlm_lock *lock;
1401 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1402 LASSERT(ergo(is_resent,
1403 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1408 if (info->mti_cross_ref) {
1409 /* Only getattr on the child. Parent is on another node. */
1410 mdt_set_disposition(info, ldlm_rep,
1411 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1413 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1415 PFID(mdt_object_fid(child)), ldlm_rep);
1417 rc = mdt_check_resent_lock(info, child, lhc);
1420 } else if (rc > 0) {
1421 mdt_lock_handle_init(lhc);
1422 mdt_lock_reg_init(lhc, LCK_PR);
1425 * Object's name is on another MDS, no lookup or layout
1426 * lock is needed here but update lock is.
1428 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1429 MDS_INODELOCK_LAYOUT);
1430 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1432 rc = mdt_object_lock(info, child, lhc, child_bits);
1437 /* Finally, we can get attr for child. */
1438 if (!mdt_object_exists(child)) {
1439 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1441 "remote object doesn't exist.");
1442 mdt_object_unlock(info, child, lhc, 1);
1446 rc = mdt_getattr_internal(info, child, 0);
1447 if (unlikely(rc != 0))
1448 mdt_object_unlock(info, child, lhc, 1);
1453 lname = &info->mti_name;
1454 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1456 if (lu_name_is_valid(lname)) {
1457 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1458 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1459 PNAME(lname), ldlm_rep);
1461 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1462 if (unlikely(reqbody == NULL))
1463 RETURN(err_serious(-EPROTO));
1465 *child_fid = reqbody->mbo_fid2;
1467 if (unlikely(!fid_is_sane(child_fid)))
1468 RETURN(err_serious(-EINVAL));
1470 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1472 PFID(mdt_object_fid(parent)),
1473 PFID(&reqbody->mbo_fid2), ldlm_rep);
1476 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1478 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1479 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1481 "Parent doesn't exist!");
1485 if (mdt_object_remote(parent)) {
1486 CERROR("%s: parent "DFID" is on remote target\n",
1487 mdt_obd_name(info->mti_mdt),
1488 PFID(mdt_object_fid(parent)));
1492 if (lu_name_is_valid(lname)) {
1493 /* Always allow to lookup ".." */
1494 if (unlikely(lname->ln_namelen == 2 &&
1495 lname->ln_name[0] == '.' &&
1496 lname->ln_name[1] == '.'))
1497 info->mti_spec.sp_permitted = 1;
1499 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1500 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1505 /* step 1: lock parent only if parent is a directory */
1506 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1507 lhp = &info->mti_lh[MDT_LH_PARENT];
1508 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1509 rc = mdt_object_lock(info, parent, lhp,
1510 MDS_INODELOCK_UPDATE);
1511 if (unlikely(rc != 0))
1515 /* step 2: lookup child's fid by name */
1516 fid_zero(child_fid);
1517 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1518 child_fid, &info->mti_spec);
1520 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1523 GOTO(out_parent, rc);
1526 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1529 *step 3: find the child object by fid & lock it.
1530 * regardless if it is local or remote.
1532 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1533 * set parent dir fid the same as child fid in getattr by fid case
1534 * we should not lu_object_find() the object again, could lead
1535 * to hung if there is a concurrent unlink destroyed the object.
1537 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1538 mdt_object_get(info->mti_env, parent);
1541 child = mdt_object_find(info->mti_env, info->mti_mdt,
1545 if (unlikely(IS_ERR(child)))
1546 GOTO(out_parent, rc = PTR_ERR(child));
1548 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1549 if (!mdt_object_exists(child)) {
1550 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1552 "Object doesn't exist!");
1553 GOTO(out_child, rc = -ENOENT);
1556 rc = mdt_check_resent_lock(info, child, lhc);
1558 GOTO(out_child, rc);
1559 } else if (rc > 0) {
1560 mdt_lock_handle_init(lhc);
1561 mdt_lock_reg_init(lhc, LCK_PR);
1564 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1565 mdt_object_exists(child) && !mdt_object_remote(child)) {
1566 struct md_attr *ma = &info->mti_attr;
1569 ma->ma_need = MA_INODE;
1570 rc = mdt_attr_get_complex(info, child, ma);
1571 if (unlikely(rc != 0))
1572 GOTO(out_child, rc);
1574 /* If the file has not been changed for some time, we
1575 * return not only a LOOKUP lock, but also an UPDATE
1576 * lock and this might save us RPC on later STAT. For
1577 * directories, it also let negative dentry cache start
1578 * working for this dir. */
1579 if (ma->ma_valid & MA_INODE &&
1580 ma->ma_attr.la_valid & LA_CTIME &&
1581 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1582 ma->ma_attr.la_ctime < cfs_time_current_sec())
1583 child_bits |= MDS_INODELOCK_UPDATE;
1586 /* layout lock must be granted in a best-effort way
1587 * for IT operations */
1588 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1589 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1590 exp_connect_layout(info->mti_exp) &&
1591 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1592 !mdt_object_remote(child) && ldlm_rep != NULL) {
1593 /* try to grant layout lock for regular file. */
1599 child_bits |= MDS_INODELOCK_LAYOUT;
1600 /* try layout lock, it may fail to be granted due to
1601 * contention at LOOKUP or UPDATE */
1602 if (!mdt_object_lock_try(info, child, lhc,
1604 child_bits &= ~MDS_INODELOCK_LAYOUT;
1605 LASSERT(child_bits != 0);
1606 rc = mdt_object_lock(info, child, lhc,
1612 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1613 * client will enqueue the lock to the remote MDT */
1614 if (mdt_object_remote(child))
1615 child_bits &= ~MDS_INODELOCK_UPDATE;
1616 rc = mdt_object_lock(info, child, lhc, child_bits);
1618 if (unlikely(rc != 0))
1619 GOTO(out_child, rc);
1622 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1624 /* finally, we can get attr for child. */
1625 rc = mdt_getattr_internal(info, child, ma_need);
1626 if (unlikely(rc != 0)) {
1627 mdt_object_unlock(info, child, lhc, 1);
1629 /* Debugging code. */
1630 LDLM_DEBUG(lock, "Returning lock to client");
1631 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1632 &lock->l_resource->lr_name),
1633 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1634 PLDLMRES(lock->l_resource),
1635 PFID(mdt_object_fid(child)));
1638 LDLM_LOCK_PUT(lock);
1642 mdt_object_put(info->mti_env, child);
1645 mdt_object_unlock(info, parent, lhp, 1);
1649 /* normal handler: should release the child lock */
1650 static int mdt_getattr_name(struct tgt_session_info *tsi)
1652 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1653 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1654 struct mdt_body *reqbody;
1655 struct mdt_body *repbody;
1659 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1660 LASSERT(reqbody != NULL);
1661 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1662 LASSERT(repbody != NULL);
1664 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1665 repbody->mbo_eadatasize = 0;
1666 repbody->mbo_aclsize = 0;
1668 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1670 GOTO(out_shrink, rc);
1672 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1673 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1674 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1675 lhc->mlh_reg_lh.cookie = 0;
1677 mdt_exit_ucred(info);
1680 mdt_client_compatibility(info);
1681 rc2 = mdt_fix_reply(info);
1684 mdt_thread_info_fini(info);
1688 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1689 void *karg, void __user *uarg);
1691 static int mdt_set_info(struct tgt_session_info *tsi)
1693 struct ptlrpc_request *req = tgt_ses_req(tsi);
1696 int keylen, vallen, rc = 0;
1700 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1702 DEBUG_REQ(D_HA, req, "no set_info key");
1703 RETURN(err_serious(-EFAULT));
1706 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1709 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1711 DEBUG_REQ(D_HA, req, "no set_info val");
1712 RETURN(err_serious(-EFAULT));
1715 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1718 /* Swab any part of val you need to here */
1719 if (KEY_IS(KEY_READ_ONLY)) {
1720 spin_lock(&req->rq_export->exp_lock);
1722 *exp_connect_flags_ptr(req->rq_export) |=
1725 *exp_connect_flags_ptr(req->rq_export) &=
1726 ~OBD_CONNECT_RDONLY;
1727 spin_unlock(&req->rq_export->exp_lock);
1728 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1729 struct changelog_setinfo *cs = val;
1731 if (vallen != sizeof(*cs)) {
1732 CERROR("%s: bad changelog_clear setinfo size %d\n",
1733 tgt_name(tsi->tsi_tgt), vallen);
1736 if (ptlrpc_req_need_swab(req)) {
1737 __swab64s(&cs->cs_recno);
1738 __swab32s(&cs->cs_id);
1741 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1743 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1745 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1752 static int mdt_readpage(struct tgt_session_info *tsi)
1754 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1755 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1756 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1757 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1758 struct mdt_body *repbody;
1764 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1765 RETURN(err_serious(-ENOMEM));
1767 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1768 if (repbody == NULL || reqbody == NULL)
1769 RETURN(err_serious(-EFAULT));
1772 * prepare @rdpg before calling lower layers and transfer itself. Here
1773 * reqbody->size contains offset of where to start to read and
1774 * reqbody->nlink contains number bytes to read.
1776 rdpg->rp_hash = reqbody->mbo_size;
1777 if (rdpg->rp_hash != reqbody->mbo_size) {
1778 CERROR("Invalid hash: "LPX64" != "LPX64"\n",
1779 rdpg->rp_hash, reqbody->mbo_size);
1783 rdpg->rp_attrs = reqbody->mbo_mode;
1784 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1785 rdpg->rp_attrs |= LUDA_64BITHASH;
1786 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1787 exp_max_brw_size(tsi->tsi_exp));
1788 rdpg->rp_npages = (rdpg->rp_count + PAGE_CACHE_SIZE - 1) >>
1790 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1791 if (rdpg->rp_pages == NULL)
1794 for (i = 0; i < rdpg->rp_npages; ++i) {
1795 rdpg->rp_pages[i] = alloc_page(GFP_IOFS);
1796 if (rdpg->rp_pages[i] == NULL)
1797 GOTO(free_rdpg, rc = -ENOMEM);
1800 /* call lower layers to fill allocated pages with directory data */
1801 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1803 GOTO(free_rdpg, rc);
1805 /* send pages to client */
1806 rc = tgt_sendpage(tsi, rdpg, rc);
1811 for (i = 0; i < rdpg->rp_npages; i++)
1812 if (rdpg->rp_pages[i] != NULL)
1813 __free_page(rdpg->rp_pages[i]);
1814 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1816 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1822 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1824 struct lu_ucred *uc = mdt_ucred_check(info);
1825 struct lu_attr *attr = &info->mti_attr.ma_attr;
1830 if (op != REINT_SETATTR) {
1831 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1832 attr->la_uid = uc->uc_fsuid;
1833 /* for S_ISGID, inherit gid from his parent, such work will be
1834 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1835 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1836 attr->la_gid = uc->uc_fsgid;
1842 static int mdt_reint_internal(struct mdt_thread_info *info,
1843 struct mdt_lock_handle *lhc,
1846 struct req_capsule *pill = info->mti_pill;
1847 struct mdt_body *repbody;
1852 rc = mdt_reint_unpack(info, op);
1854 CERROR("Can't unpack reint, rc %d\n", rc);
1855 RETURN(err_serious(rc));
1858 /* for replay (no_create) lmm is not needed, client has it already */
1859 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1860 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1863 /* llog cookies are always 0, the field is kept for compatibility */
1864 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1865 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1867 rc = req_capsule_server_pack(pill);
1869 CERROR("Can't pack response, rc %d\n", rc);
1870 RETURN(err_serious(rc));
1873 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1874 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1876 repbody->mbo_eadatasize = 0;
1877 repbody->mbo_aclsize = 0;
1880 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1882 /* for replay no cookkie / lmm need, because client have this already */
1883 if (info->mti_spec.no_create)
1884 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1885 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1887 rc = mdt_init_ucred_reint(info);
1889 GOTO(out_shrink, rc);
1891 rc = mdt_fix_attr_ucred(info, op);
1893 GOTO(out_ucred, rc = err_serious(rc));
1895 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1897 GOTO(out_ucred, rc);
1898 } else if (rc == 1) {
1899 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1900 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1901 GOTO(out_ucred, rc);
1903 rc = mdt_reint_rec(info, lhc);
1906 mdt_exit_ucred(info);
1908 mdt_client_compatibility(info);
1909 rc2 = mdt_fix_reply(info);
1915 static long mdt_reint_opcode(struct ptlrpc_request *req,
1916 const struct req_format **fmt)
1918 struct mdt_device *mdt;
1919 struct mdt_rec_reint *rec;
1922 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1924 opc = rec->rr_opcode;
1925 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1926 if (opc < REINT_MAX && fmt[opc] != NULL)
1927 req_capsule_extend(&req->rq_pill, fmt[opc]);
1929 mdt = mdt_exp2dev(req->rq_export);
1930 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1931 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1932 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1933 opc = err_serious(-EFAULT);
1936 opc = err_serious(-EFAULT);
1941 static int mdt_reint(struct tgt_session_info *tsi)
1945 static const struct req_format *reint_fmts[REINT_MAX] = {
1946 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1947 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1948 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1949 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1950 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1951 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1952 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1953 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1954 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1959 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1961 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1963 * No lock possible here from client to pass it to reint code
1966 rc = mdt_reint_internal(info, NULL, opc);
1967 mdt_thread_info_fini(info);
1972 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1976 /* this should sync the whole device */
1977 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1979 struct dt_device *dt = mdt->mdt_bottom;
1983 rc = dt->dd_ops->dt_sync(env, dt);
1987 /* this should sync this object */
1988 static int mdt_object_sync(struct mdt_thread_info *info)
1990 struct md_object *next;
1994 if (!mdt_object_exists(info->mti_object)) {
1995 CWARN("%s: non existing object "DFID": rc = %d\n",
1996 mdt_obd_name(info->mti_mdt),
1997 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
2000 next = mdt_object_child(info->mti_object);
2001 rc = mo_object_sync(info->mti_env, next);
2006 static int mdt_sync(struct tgt_session_info *tsi)
2008 struct ptlrpc_request *req = tgt_ses_req(tsi);
2009 struct req_capsule *pill = tsi->tsi_pill;
2010 struct mdt_body *body;
2015 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2016 RETURN(err_serious(-ENOMEM));
2018 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2019 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2021 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2023 /* sync an object */
2024 rc = mdt_object_sync(info);
2026 const struct lu_fid *fid;
2027 struct lu_attr *la = &info->mti_attr.ma_attr;
2029 info->mti_attr.ma_need = MA_INODE;
2030 info->mti_attr.ma_valid = 0;
2031 rc = mdt_attr_get_complex(info, info->mti_object,
2034 body = req_capsule_server_get(pill,
2036 fid = mdt_object_fid(info->mti_object);
2037 mdt_pack_attr2body(info, body, la, fid);
2040 mdt_thread_info_fini(info);
2043 mdt_counter_incr(req, LPROC_MDT_SYNC);
2049 * Handle quota control requests to consult current usage/limit, but also
2050 * to configure quota enforcement
2052 static int mdt_quotactl(struct tgt_session_info *tsi)
2054 struct obd_export *exp = tsi->tsi_exp;
2055 struct req_capsule *pill = tsi->tsi_pill;
2056 struct obd_quotactl *oqctl, *repoqc;
2058 struct mdt_device *mdt = mdt_exp2dev(exp);
2059 struct lu_device *qmt = mdt->mdt_qmt_dev;
2060 struct lu_nodemap *nodemap;
2063 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2065 RETURN(err_serious(-EPROTO));
2067 rc = req_capsule_server_pack(pill);
2069 RETURN(err_serious(rc));
2071 nodemap = nodemap_get_from_exp(exp);
2072 if (IS_ERR(nodemap))
2073 RETURN(PTR_ERR(nodemap));
2075 switch (oqctl->qc_cmd) {
2076 /* master quotactl */
2079 if (!nodemap_can_setquota(nodemap))
2080 GOTO(out_nodemap, rc = -EPERM);
2084 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2085 /* slave quotactl */
2090 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2091 GOTO(out_nodemap, rc = -EFAULT);
2095 if (oqctl->qc_type == USRQUOTA)
2096 id = nodemap_map_id(nodemap, NODEMAP_UID,
2097 NODEMAP_CLIENT_TO_FS, id);
2098 else if (oqctl->qc_type == GRPQUOTA)
2099 id = nodemap_map_id(nodemap, NODEMAP_UID,
2100 NODEMAP_CLIENT_TO_FS, id);
2102 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2104 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2106 if (oqctl->qc_id != id)
2107 swap(oqctl->qc_id, id);
2109 switch (oqctl->qc_cmd) {
2115 /* forward quotactl request to QMT */
2116 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2121 /* slave quotactl */
2122 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2127 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2128 GOTO(out_nodemap, rc = -EFAULT);
2131 if (oqctl->qc_id != id)
2132 swap(oqctl->qc_id, id);
2139 nodemap_putref(nodemap);
2144 /** clone llog ctxt from child (mdd)
2145 * This allows remote llog (replicator) access.
2146 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2147 * context was originally set up, or we can handle them directly.
2148 * I choose the latter, but that means I need any llog
2149 * contexts set up by child to be accessable by the mdt. So we clone the
2150 * context into our context list here.
2152 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2155 struct md_device *next = mdt->mdt_child;
2156 struct llog_ctxt *ctxt;
2159 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2162 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2163 if (rc || ctxt == NULL) {
2167 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2169 CERROR("Can't set mdt ctxt %d\n", rc);
2174 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2175 struct mdt_device *mdt, int idx)
2177 struct llog_ctxt *ctxt;
2179 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2182 /* Put once for the get we just did, and once for the clone */
2183 llog_ctxt_put(ctxt);
2184 llog_ctxt_put(ctxt);
2189 * sec context handlers
2191 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2193 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2199 * quota request handlers
2201 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2203 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2204 struct lu_device *qmt = mdt->mdt_qmt_dev;
2209 RETURN(err_serious(-EOPNOTSUPP));
2211 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2215 struct mdt_object *mdt_object_new(const struct lu_env *env,
2216 struct mdt_device *d,
2217 const struct lu_fid *f)
2219 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2220 struct lu_object *o;
2221 struct mdt_object *m;
2224 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2225 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2226 if (unlikely(IS_ERR(o)))
2227 m = (struct mdt_object *)o;
2233 struct mdt_object *mdt_object_find(const struct lu_env *env,
2234 struct mdt_device *d,
2235 const struct lu_fid *f)
2237 struct lu_object *o;
2238 struct mdt_object *m;
2241 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2242 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2243 if (unlikely(IS_ERR(o)))
2244 m = (struct mdt_object *)o;
2252 * Asyncronous commit for mdt device.
2254 * Pass asynchonous commit call down the MDS stack.
2256 * \param env environment
2257 * \param mdt the mdt device
2259 static void mdt_device_commit_async(const struct lu_env *env,
2260 struct mdt_device *mdt)
2262 struct dt_device *dt = mdt->mdt_bottom;
2266 rc = dt->dd_ops->dt_commit_async(env, dt);
2267 if (unlikely(rc != 0))
2268 CWARN("%s: async commit start failed: rc = %d\n",
2269 mdt_obd_name(mdt), rc);
2270 atomic_inc(&mdt->mdt_async_commit_count);
2275 * Mark the lock as "synchonous".
2277 * Mark the lock to deffer transaction commit to the unlock time.
2279 * \param lock the lock to mark as "synchonous"
2281 * \see mdt_is_lock_sync
2282 * \see mdt_save_lock
2284 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2286 lock->l_ast_data = (void*)1;
2290 * Check whehter the lock "synchonous" or not.
2292 * \param lock the lock to check
2293 * \retval 1 the lock is "synchonous"
2294 * \retval 0 the lock isn't "synchronous"
2296 * \see mdt_set_lock_sync
2297 * \see mdt_save_lock
2299 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2301 return lock->l_ast_data != NULL;
2305 * Blocking AST for mdt locks.
2307 * Starts transaction commit if in case of COS lock conflict or
2308 * deffers such a commit to the mdt_save_lock.
2310 * \param lock the lock which blocks a request or cancelling lock
2311 * \param desc unused
2312 * \param data unused
2313 * \param flag indicates whether this cancelling or blocking callback
2315 * \see ldlm_blocking_ast_nocheck
2317 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2318 void *data, int flag)
2320 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2321 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2325 if (flag == LDLM_CB_CANCELING)
2328 lock_res_and_lock(lock);
2329 if (lock->l_blocking_ast != mdt_blocking_ast) {
2330 unlock_res_and_lock(lock);
2333 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2334 lock->l_blocking_lock != NULL) {
2335 if (mdt_cos_is_enabled(mdt) &&
2336 lock->l_client_cookie !=
2337 lock->l_blocking_lock->l_client_cookie)
2338 mdt_set_lock_sync(lock);
2339 else if (mdt_slc_is_enabled(mdt) &&
2340 ldlm_is_cos_incompat(lock->l_blocking_lock))
2341 mdt_set_lock_sync(lock);
2343 rc = ldlm_blocking_ast_nocheck(lock);
2345 /* There is no lock conflict if l_blocking_lock == NULL,
2346 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2347 * when the last reference to a local lock was released */
2348 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2351 rc = lu_env_init(&env, LCT_LOCAL);
2352 if (unlikely(rc != 0))
2353 CWARN("%s: lu_env initialization failed, cannot "
2354 "start asynchronous commit: rc = %d\n",
2357 mdt_device_commit_async(&env, mdt);
2364 * Blocking AST for cross-MDT lock
2366 * Discard lock from uncommitted_slc_locks and cancel it.
2368 * \param lock the lock which blocks a request or cancelling lock
2369 * \param desc unused
2370 * \param data unused
2371 * \param flag indicates whether this cancelling or blocking callback
2372 * \retval 0 on success
2373 * \retval negative number on error
2375 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2376 void *data, int flag)
2382 case LDLM_CB_BLOCKING: {
2383 struct lustre_handle lockh;
2385 ldlm_lock2handle(lock, &lockh);
2386 rc = ldlm_cli_cancel(&lockh,
2387 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2389 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2394 case LDLM_CB_CANCELING: {
2395 LDLM_DEBUG(lock, "Revoke remote lock\n");
2397 /* discard slc lock here so that it can be cleaned anytime,
2398 * especially for cleanup_resource() */
2399 tgt_discard_slc_lock(lock);
2401 /* once we cache lock, l_ast_data is set to mdt_object */
2402 if (lock->l_ast_data != NULL) {
2403 struct mdt_object *mo = lock->l_ast_data;
2406 rc = lu_env_init(&env, LCT_MD_THREAD);
2407 if (unlikely(rc != 0)) {
2408 struct obd_device *obd;
2410 obd = ldlm_lock_to_ns(lock)->ns_obd;
2411 CWARN("%s: lu_env initialization failed, object"
2412 "%p "DFID" is leaked!\n",
2414 PFID(mdt_object_fid(mo)));
2418 if (lock->l_policy_data.l_inodebits.bits &
2419 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2420 rc = mo_invalidate(&env, mdt_object_child(mo));
2421 mo->mot_cache_attr = 0;
2423 mdt_object_put(&env, mo);
2435 int mdt_check_resent_lock(struct mdt_thread_info *info,
2436 struct mdt_object *mo,
2437 struct mdt_lock_handle *lhc)
2439 /* the lock might already be gotten in ldlm_handle_enqueue() */
2440 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2441 struct ptlrpc_request *req = mdt_info_req(info);
2442 struct ldlm_lock *lock;
2444 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2445 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2447 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2448 * a resend case, however, it could be already destroyed
2449 * due to client eviction or a raced cancel RPC. */
2450 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64,
2451 lhc->mlh_reg_lh.cookie);
2455 if (!fid_res_name_eq(mdt_object_fid(mo),
2456 &lock->l_resource->lr_name)) {
2457 CWARN("%s: Although resent, but still not "
2458 "get child lock:"DFID"\n",
2459 info->mti_exp->exp_obd->obd_name,
2460 PFID(mdt_object_fid(mo)));
2461 LDLM_LOCK_PUT(lock);
2464 LDLM_LOCK_PUT(lock);
2470 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2471 const struct lu_fid *fid, struct lustre_handle *lh,
2472 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2475 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2476 union ldlm_policy_data *policy = &mti->mti_policy;
2477 struct ldlm_res_id *res_id = &mti->mti_res_id;
2481 LASSERT(mdt_object_remote(o));
2483 fid_build_reg_res_name(fid, res_id);
2485 memset(einfo, 0, sizeof(*einfo));
2486 einfo->ei_type = LDLM_IBITS;
2487 einfo->ei_mode = mode;
2488 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2489 einfo->ei_cb_cp = ldlm_completion_ast;
2490 einfo->ei_enq_slave = 0;
2491 einfo->ei_res_id = res_id;
2493 einfo->ei_nonblock = 1;
2496 * if we cache lock, couple lock with mdt_object, so that object
2497 * can be easily found in lock ASTs.
2499 mdt_object_get(mti->mti_env, o);
2500 einfo->ei_cbdata = o;
2503 memset(policy, 0, sizeof(*policy));
2504 policy->l_inodebits.bits = ibits;
2506 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2508 if (rc < 0 && cache) {
2509 mdt_object_put(mti->mti_env, o);
2510 einfo->ei_cbdata = NULL;
2515 static int mdt_object_local_lock(struct mdt_thread_info *info,
2516 struct mdt_object *o,
2517 struct mdt_lock_handle *lh, __u64 ibits,
2518 bool nonblock, bool cos_incompat)
2520 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2521 union ldlm_policy_data *policy = &info->mti_policy;
2522 struct ldlm_res_id *res_id = &info->mti_res_id;
2527 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2528 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2529 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2530 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2533 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2534 lh->mlh_reg_mode == LCK_EX);
2535 dlmflags |= LDLM_FL_COS_INCOMPAT;
2536 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2537 dlmflags |= LDLM_FL_COS_ENABLED;
2540 /* Only enqueue LOOKUP lock for remote object */
2541 if (mdt_object_remote(o))
2542 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2544 if (lh->mlh_type == MDT_PDO_LOCK) {
2545 /* check for exists after object is locked */
2546 if (mdt_object_exists(o) == 0) {
2547 /* Non-existent object shouldn't have PDO lock */
2550 /* Non-dir object shouldn't have PDO lock */
2551 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2556 memset(policy, 0, sizeof(*policy));
2557 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2559 dlmflags |= LDLM_FL_ATOMIC_CB;
2561 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2564 * Take PDO lock on whole directory and build correct @res_id for lock
2565 * on part of directory.
2567 if (lh->mlh_pdo_hash != 0) {
2568 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2569 mdt_lock_pdo_mode(info, o, lh);
2570 if (lh->mlh_pdo_mode != LCK_NL) {
2572 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2573 * is never going to be sent to client and we do not
2574 * want it slowed down due to possible cancels.
2576 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2577 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2578 policy, res_id, dlmflags,
2579 info->mti_exp == NULL ? NULL :
2580 &info->mti_exp->exp_handle.h_cookie);
2581 if (unlikely(rc != 0))
2582 GOTO(out_unlock, rc);
2586 * Finish res_id initializing by name hash marking part of
2587 * directory which is taking modification.
2589 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2592 policy->l_inodebits.bits = ibits;
2595 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2596 * going to be sent to client. If it is - mdt_intent_policy() path will
2597 * fix it up and turn FL_LOCAL flag off.
2599 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2600 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2601 info->mti_exp == NULL ? NULL :
2602 &info->mti_exp->exp_handle.h_cookie);
2605 mdt_object_unlock(info, o, lh, 1);
2606 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2607 lh->mlh_pdo_hash != 0 &&
2608 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2609 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2615 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2616 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2619 struct mdt_lock_handle *local_lh = NULL;
2623 if (!mdt_object_remote(o)) {
2624 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2629 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2630 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2631 MDS_INODELOCK_XATTR);
2633 /* Only enqueue LOOKUP lock for remote object */
2634 if (ibits & MDS_INODELOCK_LOOKUP) {
2635 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2636 nonblock, cos_incompat);
2643 if (ibits & MDS_INODELOCK_UPDATE) {
2644 /* Sigh, PDO needs to enqueue 2 locks right now, but
2645 * enqueue RPC can only request 1 lock, to avoid extra
2646 * RPC, so it will instead enqueue EX lock for remote
2647 * object anyway XXX*/
2648 if (lh->mlh_type == MDT_PDO_LOCK &&
2649 lh->mlh_pdo_hash != 0) {
2650 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2651 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2652 PFID(mdt_object_fid(o)));
2653 lh->mlh_pdo_hash = 0;
2654 lh->mlh_rreg_mode = LCK_EX;
2655 lh->mlh_type = MDT_REG_LOCK;
2657 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2660 MDS_INODELOCK_UPDATE, nonblock,
2662 if (rc != ELDLM_OK) {
2663 if (local_lh != NULL)
2664 mdt_object_unlock(info, o, local_lh, rc);
2672 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2673 struct mdt_lock_handle *lh, __u64 ibits)
2675 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2678 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2679 struct mdt_lock_handle *lh, __u64 ibits,
2682 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2683 return mdt_object_lock_internal(info, o, lh, ibits, false,
2687 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2688 struct mdt_lock_handle *lh, __u64 ibits)
2690 struct mdt_lock_handle tmp = *lh;
2693 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2700 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2701 struct mdt_object *o, struct mdt_lock_handle *lh,
2702 __u64 ibits, bool cos_incompat)
2704 struct mdt_lock_handle tmp = *lh;
2707 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2708 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2716 * Save a lock within request object.
2718 * Keep the lock referenced until whether client ACK or transaction
2719 * commit happens or release the lock immediately depending on input
2720 * parameters. If COS is ON, a write lock is converted to COS lock
2723 * \param info thead info object
2724 * \param h lock handle
2725 * \param mode lock mode
2726 * \param decref force immediate lock releasing
2728 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2729 enum ldlm_mode mode, int decref)
2733 if (lustre_handle_is_used(h)) {
2734 if (decref || !info->mti_has_trans ||
2735 !(mode & (LCK_PW | LCK_EX))) {
2736 mdt_fid_unlock(h, mode);
2738 struct mdt_device *mdt = info->mti_mdt;
2739 struct ldlm_lock *lock = ldlm_handle2lock(h);
2740 struct ptlrpc_request *req = mdt_info_req(info);
2743 cos = (mdt_cos_is_enabled(mdt) ||
2744 mdt_slc_is_enabled(mdt));
2746 LASSERTF(lock != NULL, "no lock for cookie "LPX64"\n",
2749 /* there is no request if mdt_object_unlock() is called
2750 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2751 if (likely(req != NULL)) {
2752 CDEBUG(D_HA, "request = %p reply state = %p"
2753 " transno = "LPD64"\n", req,
2754 req->rq_reply_state, req->rq_transno);
2756 ldlm_lock_downgrade(lock, LCK_COS);
2759 ptlrpc_save_lock(req, h, mode, cos);
2761 mdt_fid_unlock(h, mode);
2763 if (mdt_is_lock_sync(lock)) {
2764 CDEBUG(D_HA, "found sync-lock,"
2765 " async commit started\n");
2766 mdt_device_commit_async(info->mti_env,
2769 LDLM_LOCK_PUT(lock);
2778 * Save cross-MDT lock in uncommitted_slc_locks
2780 * Keep the lock referenced until transaction commit happens or release the lock
2781 * immediately depending on input parameters.
2783 * \param info thead info object
2784 * \param h lock handle
2785 * \param mode lock mode
2786 * \param decref force immediate lock releasing
2788 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2789 struct mdt_object *o, struct lustre_handle *h,
2790 enum ldlm_mode mode, int decref)
2794 if (lustre_handle_is_used(h)) {
2795 struct ldlm_lock *lock = ldlm_handle2lock(h);
2798 (lock->l_policy_data.l_inodebits.bits &
2799 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2800 mo_invalidate(info->mti_env, mdt_object_child(o));
2802 if (decref || !info->mti_has_trans ||
2803 !(mode & (LCK_PW | LCK_EX))) {
2804 ldlm_lock_decref_and_cancel(h, mode);
2805 LDLM_LOCK_PUT(lock);
2807 struct ptlrpc_request *req = mdt_info_req(info);
2809 LASSERT(req != NULL);
2810 tgt_save_slc_lock(lock, req->rq_transno);
2811 ldlm_lock_decref(h, mode);
2820 * Unlock mdt object.
2822 * Immeditely release the regular lock and the PDO lock or save the
2823 * lock in request and keep them referenced until client ACK or
2824 * transaction commit.
2826 * \param info thread info object
2827 * \param o mdt object
2828 * \param lh mdt lock handle referencing regular and PDO locks
2829 * \param decref force immediate lock releasing
2831 void mdt_object_unlock(struct mdt_thread_info *info, struct mdt_object *o,
2832 struct mdt_lock_handle *lh, int decref)
2836 mdt_save_lock(info, &lh->mlh_pdo_lh, lh->mlh_pdo_mode, decref);
2837 mdt_save_lock(info, &lh->mlh_reg_lh, lh->mlh_reg_mode, decref);
2838 mdt_save_remote_lock(info, o, &lh->mlh_rreg_lh, lh->mlh_rreg_mode,
2844 struct mdt_object *mdt_object_find_lock(struct mdt_thread_info *info,
2845 const struct lu_fid *f,
2846 struct mdt_lock_handle *lh,
2849 struct mdt_object *o;
2851 o = mdt_object_find(info->mti_env, info->mti_mdt, f);
2855 rc = mdt_object_lock(info, o, lh, ibits);
2857 mdt_object_put(info->mti_env, o);
2864 void mdt_object_unlock_put(struct mdt_thread_info * info,
2865 struct mdt_object * o,
2866 struct mdt_lock_handle *lh,
2869 mdt_object_unlock(info, o, lh, decref);
2870 mdt_object_put(info->mti_env, o);
2874 * Generic code handling requests that have struct mdt_body passed in:
2876 * - extract mdt_body from request and save it in @info, if present;
2878 * - create lu_object, corresponding to the fid in mdt_body, and save it in
2881 * - if HABEO_CORPUS flag is set for this request type check whether object
2882 * actually exists on storage (lu_object_exists()).
2885 static int mdt_body_unpack(struct mdt_thread_info *info, __u32 flags)
2887 const struct mdt_body *body;
2888 struct mdt_object *obj;
2889 const struct lu_env *env;
2890 struct req_capsule *pill;
2894 env = info->mti_env;
2895 pill = info->mti_pill;
2897 body = info->mti_body = req_capsule_client_get(pill, &RMF_MDT_BODY);
2901 if (!(body->mbo_valid & OBD_MD_FLID))
2904 if (!fid_is_sane(&body->mbo_fid1)) {
2905 CERROR("Invalid fid: "DFID"\n", PFID(&body->mbo_fid1));
2909 obj = mdt_object_find(env, info->mti_mdt, &body->mbo_fid1);
2911 if ((flags & HABEO_CORPUS) && !mdt_object_exists(obj)) {
2912 mdt_object_put(env, obj);
2915 info->mti_object = obj;
2924 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags)
2926 struct req_capsule *pill = info->mti_pill;
2930 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_CLIENT))
2931 rc = mdt_body_unpack(info, flags);
2935 if (rc == 0 && (flags & HABEO_REFERO)) {
2937 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
2938 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
2940 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
2941 req_capsule_set_size(pill, &RMF_LOGCOOKIES,
2944 rc = req_capsule_server_pack(pill);
2949 void mdt_lock_handle_init(struct mdt_lock_handle *lh)
2951 lh->mlh_type = MDT_NUL_LOCK;
2952 lh->mlh_reg_lh.cookie = 0ull;
2953 lh->mlh_reg_mode = LCK_MINMODE;
2954 lh->mlh_pdo_lh.cookie = 0ull;
2955 lh->mlh_pdo_mode = LCK_MINMODE;
2956 lh->mlh_rreg_lh.cookie = 0ull;
2957 lh->mlh_rreg_mode = LCK_MINMODE;
2960 void mdt_lock_handle_fini(struct mdt_lock_handle *lh)
2962 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2963 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2967 * Initialize fields of struct mdt_thread_info. Other fields are left in
2968 * uninitialized state, because it's too expensive to zero out whole
2969 * mdt_thread_info (> 1K) on each request arrival.
2971 void mdt_thread_info_init(struct ptlrpc_request *req,
2972 struct mdt_thread_info *info)
2976 info->mti_pill = &req->rq_pill;
2979 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
2980 mdt_lock_handle_init(&info->mti_lh[i]);
2982 /* mdt device: it can be NULL while CONNECT */
2983 if (req->rq_export) {
2984 info->mti_mdt = mdt_dev(req->rq_export->exp_obd->obd_lu_dev);
2985 info->mti_exp = req->rq_export;
2987 info->mti_mdt = NULL;
2988 info->mti_env = req->rq_svc_thread->t_env;
2989 info->mti_transno = lustre_msg_get_transno(req->rq_reqmsg);
2991 memset(&info->mti_attr, 0, sizeof(info->mti_attr));
2992 info->mti_big_buf = LU_BUF_NULL;
2993 info->mti_body = NULL;
2994 info->mti_object = NULL;
2995 info->mti_dlm_req = NULL;
2996 info->mti_has_trans = 0;
2997 info->mti_cross_ref = 0;
2998 info->mti_opdata = 0;
2999 info->mti_big_lmm_used = 0;
3001 info->mti_spec.no_create = 0;
3002 info->mti_spec.sp_rm_entry = 0;
3003 info->mti_spec.sp_permitted = 0;
3004 info->mti_spec.sp_migrate_close = 0;
3006 info->mti_spec.u.sp_ea.eadata = NULL;
3007 info->mti_spec.u.sp_ea.eadatalen = 0;
3010 void mdt_thread_info_fini(struct mdt_thread_info *info)
3014 if (info->mti_object != NULL) {
3015 mdt_object_put(info->mti_env, info->mti_object);
3016 info->mti_object = NULL;
3019 for (i = 0; i < ARRAY_SIZE(info->mti_lh); i++)
3020 mdt_lock_handle_fini(&info->mti_lh[i]);
3021 info->mti_env = NULL;
3022 info->mti_pill = NULL;
3023 info->mti_exp = NULL;
3025 if (unlikely(info->mti_big_buf.lb_buf != NULL))
3026 lu_buf_free(&info->mti_big_buf);
3029 struct mdt_thread_info *tsi2mdt_info(struct tgt_session_info *tsi)
3031 struct mdt_thread_info *mti;
3033 mti = mdt_th_info(tsi->tsi_env);
3034 LASSERT(mti != NULL);
3036 mdt_thread_info_init(tgt_ses_req(tsi), mti);
3037 if (tsi->tsi_corpus != NULL) {
3038 mti->mti_object = mdt_obj(tsi->tsi_corpus);
3039 lu_object_get(tsi->tsi_corpus);
3041 mti->mti_body = tsi->tsi_mdt_body;
3042 mti->mti_dlm_req = tsi->tsi_dlm_req;
3047 static int mdt_tgt_connect(struct tgt_session_info *tsi)
3049 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_DELAY_CONDITIONAL) &&
3051 tsi2mdt_info(tsi)->mti_mdt->mdt_seq_site.ss_node_id) {
3052 set_current_state(TASK_UNINTERRUPTIBLE);
3053 schedule_timeout(msecs_to_jiffies(3 * MSEC_PER_SEC));
3056 return tgt_connect(tsi);
3074 static int mdt_intent_getattr(enum mdt_it_code opcode,
3075 struct mdt_thread_info *info,
3076 struct ldlm_lock **,
3079 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3080 struct mdt_thread_info *info,
3081 struct ldlm_lock **lockp,
3084 static int mdt_intent_layout(enum mdt_it_code opcode,
3085 struct mdt_thread_info *info,
3086 struct ldlm_lock **,
3088 static int mdt_intent_reint(enum mdt_it_code opcode,
3089 struct mdt_thread_info *info,
3090 struct ldlm_lock **,
3093 static struct mdt_it_flavor {
3094 const struct req_format *it_fmt;
3096 int (*it_act)(enum mdt_it_code ,
3097 struct mdt_thread_info *,
3098 struct ldlm_lock **,
3101 } mdt_it_flavor[] = {
3103 .it_fmt = &RQF_LDLM_INTENT,
3104 /*.it_flags = HABEO_REFERO,*/
3106 .it_act = mdt_intent_reint,
3107 .it_reint = REINT_OPEN
3110 .it_fmt = &RQF_LDLM_INTENT,
3112 * OCREAT is not a MUTABOR request as if the file
3114 * We do the extra check of OBD_CONNECT_RDONLY in
3115 * mdt_reint_open() when we really need to create
3119 .it_act = mdt_intent_reint,
3120 .it_reint = REINT_OPEN
3123 .it_fmt = &RQF_LDLM_INTENT,
3124 .it_flags = MUTABOR,
3125 .it_act = mdt_intent_reint,
3126 .it_reint = REINT_CREATE
3128 [MDT_IT_GETATTR] = {
3129 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3130 .it_flags = HABEO_REFERO,
3131 .it_act = mdt_intent_getattr
3133 [MDT_IT_READDIR] = {
3139 .it_fmt = &RQF_LDLM_INTENT_GETATTR,
3140 .it_flags = HABEO_REFERO,
3141 .it_act = mdt_intent_getattr
3144 .it_fmt = &RQF_LDLM_INTENT_UNLINK,
3145 .it_flags = MUTABOR,
3147 .it_reint = REINT_UNLINK
3151 .it_flags = MUTABOR,
3154 [MDT_IT_GETXATTR] = {
3155 .it_fmt = &RQF_LDLM_INTENT_GETXATTR,
3156 .it_flags = HABEO_CORPUS,
3157 .it_act = mdt_intent_getxattr
3160 .it_fmt = &RQF_LDLM_INTENT_LAYOUT,
3162 .it_act = mdt_intent_layout
3167 mdt_intent_lock_replace(struct mdt_thread_info *info,
3168 struct ldlm_lock **lockp,
3169 struct mdt_lock_handle *lh,
3170 __u64 flags, int result)
3172 struct ptlrpc_request *req = mdt_info_req(info);
3173 struct ldlm_lock *lock = *lockp;
3174 struct ldlm_lock *new_lock;
3176 /* If possible resent found a lock, @lh is set to its handle */
3177 new_lock = ldlm_handle2lock_long(&lh->mlh_reg_lh, 0);
3179 if (new_lock == NULL && (flags & LDLM_FL_INTENT_ONLY)) {
3180 lh->mlh_reg_lh.cookie = 0;
3184 if (new_lock == NULL && (flags & LDLM_FL_RESENT)) {
3185 /* Lock is pinned by ldlm_handle_enqueue0() as it is
3186 * a resend case, however, it could be already destroyed
3187 * due to client eviction or a raced cancel RPC. */
3188 LDLM_DEBUG_NOLOCK("Invalid lock handle "LPX64"\n",
3189 lh->mlh_reg_lh.cookie);
3190 lh->mlh_reg_lh.cookie = 0;
3194 LASSERTF(new_lock != NULL,
3195 "lockh "LPX64" flags "LPX64" rc %d\n",
3196 lh->mlh_reg_lh.cookie, flags, result);
3199 * If we've already given this lock to a client once, then we should
3200 * have no readers or writers. Otherwise, we should have one reader
3201 * _or_ writer ref (which will be zeroed below) before returning the
3204 if (new_lock->l_export == req->rq_export) {
3205 LASSERT(new_lock->l_readers + new_lock->l_writers == 0);
3207 LASSERT(new_lock->l_export == NULL);
3208 LASSERT(new_lock->l_readers + new_lock->l_writers == 1);
3213 if (new_lock->l_export == req->rq_export) {
3215 * Already gave this to the client, which means that we
3216 * reconstructed a reply.
3218 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) &
3221 LDLM_LOCK_RELEASE(new_lock);
3222 lh->mlh_reg_lh.cookie = 0;
3223 RETURN(ELDLM_LOCK_REPLACED);
3227 * Fixup the lock to be given to the client.
3229 lock_res_and_lock(new_lock);
3230 /* Zero new_lock->l_readers and new_lock->l_writers without triggering
3231 * possible blocking AST. */
3232 while (new_lock->l_readers > 0) {
3233 lu_ref_del(&new_lock->l_reference, "reader", new_lock);
3234 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3235 new_lock->l_readers--;
3237 while (new_lock->l_writers > 0) {
3238 lu_ref_del(&new_lock->l_reference, "writer", new_lock);
3239 lu_ref_del(&new_lock->l_reference, "user", new_lock);
3240 new_lock->l_writers--;
3243 new_lock->l_export = class_export_lock_get(req->rq_export, new_lock);
3244 new_lock->l_blocking_ast = lock->l_blocking_ast;
3245 new_lock->l_completion_ast = lock->l_completion_ast;
3246 new_lock->l_remote_handle = lock->l_remote_handle;
3247 new_lock->l_flags &= ~LDLM_FL_LOCAL;
3249 unlock_res_and_lock(new_lock);
3251 cfs_hash_add(new_lock->l_export->exp_lock_hash,
3252 &new_lock->l_remote_handle,
3253 &new_lock->l_exp_hash);
3255 LDLM_LOCK_RELEASE(new_lock);
3256 lh->mlh_reg_lh.cookie = 0;
3258 RETURN(ELDLM_LOCK_REPLACED);
3261 static void mdt_intent_fixup_resent(struct mdt_thread_info *info,
3262 struct ldlm_lock *new_lock,
3263 struct mdt_lock_handle *lh,
3266 struct ptlrpc_request *req = mdt_info_req(info);
3267 struct ldlm_request *dlmreq;
3269 if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT))
3272 dlmreq = req_capsule_client_get(info->mti_pill, &RMF_DLM_REQ);
3274 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
3275 * lock was found by ldlm_handle_enqueue(); if so @lh must be
3277 if (flags & LDLM_FL_RESENT) {
3278 lh->mlh_reg_lh.cookie = new_lock->l_handle.h_cookie;
3279 lh->mlh_reg_mode = new_lock->l_granted_mode;
3281 LDLM_DEBUG(new_lock, "Restoring lock cookie");
3282 DEBUG_REQ(D_DLMTRACE, req, "restoring lock cookie "LPX64,
3283 lh->mlh_reg_lh.cookie);
3288 * If the xid matches, then we know this is a resent request, and allow
3289 * it. (It's probably an OPEN, for which we don't send a lock.
3291 if (req_can_reconstruct(req, NULL))
3295 * This remote handle isn't enqueued, so we never received or processed
3296 * this request. Clear MSG_RESENT, because it can be handled like any
3297 * normal request now.
3299 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
3301 DEBUG_REQ(D_DLMTRACE, req, "no existing lock with rhandle "LPX64,
3302 dlmreq->lock_handle[0].cookie);
3305 static int mdt_intent_getxattr(enum mdt_it_code opcode,
3306 struct mdt_thread_info *info,
3307 struct ldlm_lock **lockp,
3310 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3311 struct ldlm_reply *ldlm_rep = NULL;
3315 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3316 * (for the resend case) or a new lock. Below we will use it to
3317 * replace the original lock.
3319 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3320 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3321 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3322 rc = mdt_object_lock(info, info->mti_object, lhc,
3323 MDS_INODELOCK_XATTR);
3328 grc = mdt_getxattr(info);
3330 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, 0);
3332 if (mdt_info_req(info)->rq_repmsg != NULL)
3333 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3334 if (ldlm_rep == NULL)
3335 RETURN(err_serious(-EFAULT));
3337 ldlm_rep->lock_policy_res2 = grc;
3342 static int mdt_intent_getattr(enum mdt_it_code opcode,
3343 struct mdt_thread_info *info,
3344 struct ldlm_lock **lockp,
3347 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3349 struct ldlm_reply *ldlm_rep;
3350 struct mdt_body *reqbody;
3351 struct mdt_body *repbody;
3355 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3358 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3361 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3362 repbody->mbo_eadatasize = 0;
3363 repbody->mbo_aclsize = 0;
3367 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3369 case MDT_IT_GETATTR:
3370 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3374 CERROR("Unsupported intent (%d)\n", opcode);
3375 GOTO(out_shrink, rc = -EINVAL);
3378 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3380 GOTO(out_shrink, rc);
3382 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3383 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3385 /* Get lock from request for possible resent case. */
3386 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3388 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3389 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3391 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3392 ldlm_rep->lock_policy_res2 = 0;
3393 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3394 ldlm_rep->lock_policy_res2) {
3395 lhc->mlh_reg_lh.cookie = 0ull;
3396 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3399 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3402 mdt_exit_ucred(info);
3404 mdt_client_compatibility(info);
3405 rc2 = mdt_fix_reply(info);
3411 static int mdt_intent_layout(enum mdt_it_code opcode,
3412 struct mdt_thread_info *info,
3413 struct ldlm_lock **lockp,
3416 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3417 struct layout_intent *layout;
3419 struct mdt_object *obj = NULL;
3420 int layout_size = 0;
3424 if (opcode != MDT_IT_LAYOUT) {
3425 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3430 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3434 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3435 CERROR("%s: Unsupported layout intent opc %d\n",
3436 mdt_obd_name(info->mti_mdt), layout->li_opc);
3440 fid = &info->mti_tmp_fid2;
3441 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3443 /* Get lock from request for possible resent case. */
3444 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3446 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3448 GOTO(out, rc = PTR_ERR(obj));
3450 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3451 layout_size = mdt_attr_get_eabuf_size(info, obj);
3452 if (layout_size < 0)
3453 GOTO(out_obj, rc = layout_size);
3455 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3456 info->mti_mdt->mdt_max_mdsize = layout_size;
3459 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3460 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3462 rc = req_capsule_server_pack(info->mti_pill);
3466 mdt_object_put(info->mti_env, obj);
3468 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3469 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3472 lhc->mlh_reg_lh.cookie = 0;
3477 static int mdt_intent_reint(enum mdt_it_code opcode,
3478 struct mdt_thread_info *info,
3479 struct ldlm_lock **lockp,
3482 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3483 struct ldlm_reply *rep = NULL;
3487 static const struct req_format *intent_fmts[REINT_MAX] = {
3488 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3489 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3494 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3498 if (mdt_it_flavor[opcode].it_reint != opc) {
3499 CERROR("Reint code %ld doesn't match intent: %d\n",
3501 RETURN(err_serious(-EPROTO));
3504 /* Get lock from request for possible resent case. */
3505 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3507 rc = mdt_reint_internal(info, lhc, opc);
3509 /* Check whether the reply has been packed successfully. */
3510 if (mdt_info_req(info)->rq_repmsg != NULL)
3511 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3513 RETURN(err_serious(-EFAULT));
3515 /* MDC expects this in any case */
3517 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3519 /* the open lock or the lock for cross-ref object should be
3520 * returned to the client */
3521 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3522 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3523 rep->lock_policy_res2 = 0;
3524 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3528 rep->lock_policy_res2 = clear_serious(rc);
3530 if (rep->lock_policy_res2 == -ENOENT &&
3531 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3532 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3533 rep->lock_policy_res2 = 0;
3535 lhc->mlh_reg_lh.cookie = 0ull;
3536 if (rc == -ENOTCONN || rc == -ENODEV ||
3537 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3539 * If it is the disconnect error (ENODEV & ENOCONN), the error
3540 * will be returned by rq_status, and client at ptlrpc layer
3541 * will detect this, then disconnect, reconnect the import
3542 * immediately, instead of impacting the following the rpc.
3547 * For other cases, the error will be returned by intent, and client
3548 * will retrieve the result from intent.
3550 RETURN(ELDLM_LOCK_ABORTED);
3553 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3561 case IT_OPEN|IT_CREAT:
3568 rc = MDT_IT_READDIR;
3571 rc = MDT_IT_GETATTR;
3583 rc = MDT_IT_GETXATTR;
3588 case IT_QUOTA_DQACQ:
3593 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3600 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3601 struct mdt_thread_info *info,
3602 struct ldlm_lock **lockp, __u64 flags)
3604 struct req_capsule *pill = info->mti_pill;
3605 struct ptlrpc_request *req = mdt_info_req(info);
3606 struct mdt_it_flavor *flv;
3611 opc = mdt_intent_code(itopc);
3615 if (opc == MDT_IT_QUOTA) {
3616 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3619 RETURN(-EOPNOTSUPP);
3621 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3622 /* pass the request to quota master */
3623 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3624 mdt_info_req(info), lockp,
3629 flv = &mdt_it_flavor[opc];
3630 if (flv->it_fmt != NULL)
3631 req_capsule_extend(pill, flv->it_fmt);
3633 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3637 if (flv->it_flags & MUTABOR &&
3638 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3641 if (flv->it_act != NULL) {
3642 struct ldlm_reply *rep;
3644 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3646 /* execute policy */
3647 rc = flv->it_act(opc, info, lockp, flags);
3649 /* Check whether the reply has been packed successfully. */
3650 if (req->rq_repmsg != NULL) {
3651 rep = req_capsule_server_get(info->mti_pill,
3653 rep->lock_policy_res2 =
3654 ptlrpc_status_hton(rep->lock_policy_res2);
3661 static int mdt_intent_policy(struct ldlm_namespace *ns,
3662 struct ldlm_lock **lockp, void *req_cookie,
3663 enum ldlm_mode mode, __u64 flags, void *data)
3665 struct tgt_session_info *tsi;
3666 struct mdt_thread_info *info;
3667 struct ptlrpc_request *req = req_cookie;
3668 struct ldlm_intent *it;
3669 struct req_capsule *pill;
3674 LASSERT(req != NULL);
3676 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3678 info = tsi2mdt_info(tsi);
3679 LASSERT(info != NULL);
3680 pill = info->mti_pill;
3681 LASSERT(pill->rc_req == req);
3683 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3684 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3685 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3687 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3691 /* Lock without inodebits makes no sense and will oops
3692 * later in ldlm. Let's check it now to see if we have
3693 * ibits corrupted somewhere in mdt_intent_opc().
3694 * The case for client miss to set ibits has been
3695 * processed by others. */
3696 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3697 lr_type == LDLM_IBITS,
3698 info->mti_dlm_req->lock_desc.\
3699 l_policy_data.l_inodebits.bits != 0));
3701 rc = err_serious(-EFAULT);
3703 /* No intent was provided */
3704 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3705 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3706 rc = req_capsule_server_pack(pill);
3708 rc = err_serious(rc);
3710 mdt_thread_info_fini(info);
3714 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3716 struct seq_server_site *ss = mdt_seq_site(mdt);
3718 if (ss->ss_node_id == 0)
3721 if (ss->ss_client_seq != NULL) {
3722 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3723 ss->ss_client_seq->lcs_exp = NULL;
3726 if (ss->ss_server_fld != NULL) {
3727 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3728 ss->ss_server_fld->lsf_control_exp = NULL;
3732 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3734 struct seq_server_site *ss = mdt_seq_site(mdt);
3739 if (ss->ss_server_seq != NULL)
3740 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3743 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3745 mdt_seq_fini_cli(mdt);
3746 mdt_deregister_seq_exp(mdt);
3748 return seq_site_fini(env, mdt_seq_site(mdt));
3752 * It will retrieve its FLDB entries from MDT0, and it only happens
3753 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3754 * and it needs to refresh FLDB from the MDT0.
3756 static int mdt_register_lwp_callback(void *data)
3759 struct mdt_device *mdt = data;
3760 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3764 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3766 rc = lu_env_init(&env, LCT_MD_THREAD);
3768 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3772 /* Allocate new sequence now to avoid creating local transaction
3773 * in the normal transaction process */
3774 rc = seq_server_check_and_alloc_super(&env,
3775 mdt_seq_site(mdt)->ss_server_seq);
3780 rc = fld_update_from_controller(&env, fld);
3782 CERROR("%s: cannot update controller: rc = %d\n",
3783 mdt_obd_name(mdt), rc);
3792 static int mdt_register_seq_exp(struct mdt_device *mdt)
3794 struct seq_server_site *ss = mdt_seq_site(mdt);
3795 char *lwp_name = NULL;
3798 if (ss->ss_node_id == 0)
3801 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3802 if (lwp_name == NULL)
3803 GOTO(out_free, rc = -ENOMEM);
3805 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3809 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3814 rc = lustre_register_lwp_item(lwp_name,
3815 &ss->ss_server_fld->lsf_control_exp,
3816 mdt_register_lwp_callback, mdt);
3818 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3819 ss->ss_client_seq->lcs_exp = NULL;
3823 if (lwp_name != NULL)
3824 OBD_FREE(lwp_name, MAX_OBD_NAME);
3830 * Init client sequence manager which is used by local MDS to talk to sequence
3831 * controller on remote node.
3833 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3835 struct seq_server_site *ss = mdt_seq_site(mdt);
3840 /* check if this is adding the first MDC and controller is not yet
3842 OBD_ALLOC_PTR(ss->ss_client_seq);
3843 if (ss->ss_client_seq == NULL)
3846 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3847 if (prefix == NULL) {
3848 OBD_FREE_PTR(ss->ss_client_seq);
3849 ss->ss_client_seq = NULL;
3853 /* Note: seq_client_fini will be called in seq_site_fini */
3854 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3855 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3856 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3858 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3860 OBD_FREE_PTR(ss->ss_client_seq);
3861 ss->ss_client_seq = NULL;
3865 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3870 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3872 struct seq_server_site *ss;
3876 ss = mdt_seq_site(mdt);
3877 /* init sequence controller server(MDT0) */
3878 if (ss->ss_node_id == 0) {
3879 OBD_ALLOC_PTR(ss->ss_control_seq);
3880 if (ss->ss_control_seq == NULL)
3883 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3884 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3887 GOTO(out_seq_fini, rc);
3890 /* Init normal sequence server */
3891 OBD_ALLOC_PTR(ss->ss_server_seq);
3892 if (ss->ss_server_seq == NULL)
3893 GOTO(out_seq_fini, rc = -ENOMEM);
3895 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3896 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3898 GOTO(out_seq_fini, rc);
3900 /* init seq client for seq server to talk to seq controller(MDT0) */
3901 rc = mdt_seq_init_cli(env, mdt);
3903 GOTO(out_seq_fini, rc);
3905 if (ss->ss_node_id != 0)
3906 /* register controller export through lwp */
3907 rc = mdt_register_seq_exp(mdt);
3912 mdt_seq_fini(env, mdt);
3920 static int mdt_fld_fini(const struct lu_env *env,
3921 struct mdt_device *m)
3923 struct seq_server_site *ss = mdt_seq_site(m);
3926 if (ss && ss->ss_server_fld) {
3927 fld_server_fini(env, ss->ss_server_fld);
3928 OBD_FREE_PTR(ss->ss_server_fld);
3929 ss->ss_server_fld = NULL;
3935 static int mdt_fld_init(const struct lu_env *env,
3937 struct mdt_device *m)
3939 struct seq_server_site *ss;
3943 ss = mdt_seq_site(m);
3945 OBD_ALLOC_PTR(ss->ss_server_fld);
3946 if (ss->ss_server_fld == NULL)
3947 RETURN(rc = -ENOMEM);
3949 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3952 OBD_FREE_PTR(ss->ss_server_fld);
3953 ss->ss_server_fld = NULL;
3960 static void mdt_stack_pre_fini(const struct lu_env *env,
3961 struct mdt_device *m, struct lu_device *top)
3963 struct lustre_cfg_bufs *bufs;
3964 struct lustre_cfg *lcfg;
3965 struct mdt_thread_info *info;
3970 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3971 LASSERT(info != NULL);
3973 bufs = &info->mti_u.bufs;
3975 LASSERT(m->mdt_child_exp);
3976 LASSERT(m->mdt_child_exp->exp_obd);
3978 /* process cleanup, pass mdt obd name to get obd umount flags */
3979 /* XXX: this is needed because all layers are referenced by
3980 * objects (some of them are pinned by osd, for example *
3981 * the proper solution should be a model where object used
3982 * by osd only doesn't have mdt/mdd slices -bzzz */
3983 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3984 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3985 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
3989 top->ld_ops->ldo_process_config(env, top, lcfg);
3990 lustre_cfg_free(lcfg);
3994 static void mdt_stack_fini(const struct lu_env *env,
3995 struct mdt_device *m, struct lu_device *top)
3997 struct obd_device *obd = mdt2obd_dev(m);
3998 struct lustre_cfg_bufs *bufs;
3999 struct lustre_cfg *lcfg;
4000 struct mdt_thread_info *info;
4004 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4005 LASSERT(info != NULL);
4007 lu_dev_del_linkage(top->ld_site, top);
4009 lu_site_purge(env, top->ld_site, -1);
4011 bufs = &info->mti_u.bufs;
4012 /* process cleanup, pass mdt obd name to get obd umount flags */
4013 /* another purpose is to let all layers to release their objects */
4014 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4019 lustre_cfg_bufs_set_string(bufs, 1, flags);
4020 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4025 top->ld_ops->ldo_process_config(env, top, lcfg);
4026 lustre_cfg_free(lcfg);
4028 lu_site_purge(env, top->ld_site, -1);
4030 m->mdt_child = NULL;
4031 m->mdt_bottom = NULL;
4033 obd_disconnect(m->mdt_child_exp);
4034 m->mdt_child_exp = NULL;
4036 obd_disconnect(m->mdt_bottom_exp);
4037 m->mdt_child_exp = NULL;
4040 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4041 const char *next, struct obd_export **exp)
4043 struct obd_connect_data *data = NULL;
4044 struct obd_device *obd;
4048 OBD_ALLOC_PTR(data);
4050 GOTO(out, rc = -ENOMEM);
4052 obd = class_name2obd(next);
4054 CERROR("%s: can't locate next device: %s\n",
4055 mdt_obd_name(m), next);
4056 GOTO(out, rc = -ENOTCONN);
4059 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4060 data->ocd_version = LUSTRE_VERSION_CODE;
4062 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4064 CERROR("%s: cannot connect to next dev %s (%d)\n",
4065 mdt_obd_name(m), next, rc);
4075 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4076 struct lustre_cfg *cfg)
4078 char *dev = lustre_cfg_string(cfg, 0);
4079 int rc, name_size, uuid_size;
4080 char *name, *uuid, *p;
4081 struct lustre_cfg_bufs *bufs;
4082 struct lustre_cfg *lcfg;
4083 struct obd_device *obd;
4084 struct lustre_profile *lprof;
4085 struct lu_site *site;
4088 /* in 1.8 we had the only device in the stack - MDS.
4089 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4090 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4091 * to generate names and setup MDT, MDD. MDT will be using
4092 * generated name to connect to MDD. for MDD the next device
4093 * will be LOD with name taken from so called "profile" which
4094 * is generated by mount_option line
4096 * 1.8 MGS generates config. commands like this:
4097 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4098 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4099 * 2.0 MGS generates config. commands like this:
4100 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4101 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4102 * 3:lustre-MDT0000-mdtlov 4:f
4104 * we generate MDD name from MDT one, just replacing T with D
4106 * after all the preparations, the logical equivalent will be
4107 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4108 * 3:lustre-MDT0000-mdtlov 4:f
4109 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4110 * 3:lustre-MDD0000 4:f
4112 * notice we build the stack from down to top: MDD first, then MDT */
4114 name_size = MAX_OBD_NAME;
4115 uuid_size = MAX_OBD_NAME;
4117 OBD_ALLOC(name, name_size);
4118 OBD_ALLOC(uuid, uuid_size);
4119 if (name == NULL || uuid == NULL)
4120 GOTO(cleanup_mem, rc = -ENOMEM);
4122 OBD_ALLOC_PTR(bufs);
4124 GOTO(cleanup_mem, rc = -ENOMEM);
4127 p = strstr(name, "-MDT");
4129 GOTO(free_bufs, rc = -ENOMEM);
4132 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4134 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4135 if (lprof == NULL || lprof->lp_dt == NULL) {
4136 CERROR("can't find the profile: %s\n",
4137 lustre_cfg_string(cfg, 0));
4138 GOTO(free_bufs, rc = -EINVAL);
4141 lustre_cfg_bufs_reset(bufs, name);
4142 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4143 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4144 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4146 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4148 GOTO(put_profile, rc = -ENOMEM);
4150 rc = class_attach(lcfg);
4152 GOTO(lcfg_cleanup, rc);
4154 obd = class_name2obd(name);
4156 CERROR("Can not find obd %s (%s in config)\n",
4157 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4158 GOTO(lcfg_cleanup, rc = -EINVAL);
4161 lustre_cfg_free(lcfg);
4163 lustre_cfg_bufs_reset(bufs, name);
4164 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4165 lustre_cfg_bufs_set_string(bufs, 2, dev);
4166 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4168 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4170 GOTO(class_detach, rc = -ENOMEM);
4172 rc = class_setup(obd, lcfg);
4174 GOTO(class_detach, rc);
4176 /* connect to MDD we just setup */
4177 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4179 GOTO(class_detach, rc);
4181 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4183 LASSERT(mdt_lu_site(mdt) == NULL);
4184 mdt->mdt_lu_dev.ld_site = site;
4185 site->ls_top_dev = &mdt->mdt_lu_dev;
4186 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4188 /* now connect to bottom OSD */
4189 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4190 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4192 GOTO(class_detach, rc);
4194 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4196 rc = lu_env_refill((struct lu_env *)env);
4198 CERROR("Failure to refill session: '%d'\n", rc);
4200 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4205 class_detach(obd, lcfg);
4207 lustre_cfg_free(lcfg);
4209 class_put_profile(lprof);
4214 OBD_FREE(name, name_size);
4216 OBD_FREE(uuid, uuid_size);
4220 /* setup quota master target on MDT0 */
4221 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4222 struct lustre_cfg *cfg)
4224 struct obd_device *obd;
4225 char *dev = lustre_cfg_string(cfg, 0);
4226 char *qmtname, *uuid, *p;
4227 struct lustre_cfg_bufs *bufs;
4228 struct lustre_cfg *lcfg;
4229 struct lustre_profile *lprof;
4230 struct obd_connect_data *data;
4234 LASSERT(mdt->mdt_qmt_exp == NULL);
4235 LASSERT(mdt->mdt_qmt_dev == NULL);
4237 /* quota master is on MDT0 only for now */
4238 if (mdt->mdt_seq_site.ss_node_id != 0)
4241 /* MGS generates config commands which look as follows:
4242 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4243 * 3:lustre-MDT0000-mdtlov 4:f
4245 * We generate the QMT name from the MDT one, just replacing MD with QM
4246 * after all the preparations, the logical equivalent will be:
4247 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4248 * 3:lustre-MDT0000-osd 4:f */
4249 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4250 OBD_ALLOC(uuid, UUID_MAX);
4251 OBD_ALLOC_PTR(bufs);
4252 OBD_ALLOC_PTR(data);
4253 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4254 GOTO(cleanup_mem, rc = -ENOMEM);
4256 strcpy(qmtname, dev);
4257 p = strstr(qmtname, "-MDT");
4259 GOTO(cleanup_mem, rc = -ENOMEM);
4260 /* replace MD with QM */
4264 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4266 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4267 if (lprof == NULL || lprof->lp_dt == NULL) {
4268 CERROR("can't find profile for %s\n",
4269 lustre_cfg_string(cfg, 0));
4270 GOTO(cleanup_mem, rc = -EINVAL);
4273 lustre_cfg_bufs_reset(bufs, qmtname);
4274 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4275 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4276 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4278 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4280 GOTO(put_profile, rc = -ENOMEM);
4282 rc = class_attach(lcfg);
4284 GOTO(lcfg_cleanup, rc);
4286 obd = class_name2obd(qmtname);
4288 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4289 lustre_cfg_string(cfg, 0));
4290 GOTO(lcfg_cleanup, rc = -EINVAL);
4293 lustre_cfg_free(lcfg);
4295 lustre_cfg_bufs_reset(bufs, qmtname);
4296 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4297 lustre_cfg_bufs_set_string(bufs, 2, dev);
4299 /* for quota, the next device should be the OSD device */
4300 lustre_cfg_bufs_set_string(bufs, 3,
4301 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4303 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4305 GOTO(class_detach, rc = -ENOMEM);
4307 rc = class_setup(obd, lcfg);
4309 GOTO(class_detach, rc);
4311 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4313 /* configure local quota objects */
4314 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4318 GOTO(class_cleanup, rc);
4320 /* connect to quota master target */
4321 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4322 data->ocd_version = LUSTRE_VERSION_CODE;
4323 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4326 CERROR("cannot connect to quota master device %s (%d)\n",
4328 GOTO(class_cleanup, rc);
4334 class_manual_cleanup(obd);
4335 mdt->mdt_qmt_dev = NULL;
4339 class_detach(obd, lcfg);
4341 lustre_cfg_free(lcfg);
4343 class_put_profile(lprof);
4348 OBD_FREE(qmtname, MAX_OBD_NAME);
4350 OBD_FREE(uuid, UUID_MAX);
4356 /* Shutdown quota master target associated with mdt */
4357 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4361 if (mdt->mdt_qmt_exp == NULL)
4363 LASSERT(mdt->mdt_qmt_dev != NULL);
4365 /* the qmt automatically shuts down when the mdt disconnects */
4366 obd_disconnect(mdt->mdt_qmt_exp);
4367 mdt->mdt_qmt_exp = NULL;
4368 mdt->mdt_qmt_dev = NULL;
4372 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4373 * for now. This will be removed along with converting rest of MDT code
4374 * to use tgt_session_info */
4375 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4377 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4380 rc = mdt_getxattr(info);
4382 mdt_thread_info_fini(info);
4386 static struct tgt_handler mdt_tgt_handlers[] = {
4387 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4388 0, MDS_CONNECT, mdt_tgt_connect,
4389 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4390 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4391 0, MDS_DISCONNECT, tgt_disconnect,
4392 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4393 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4394 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4395 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4396 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4397 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4398 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4399 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4401 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4402 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4403 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4404 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4405 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4406 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4407 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4408 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4410 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4411 mdt_hsm_ct_register),
4412 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4413 mdt_hsm_ct_unregister),
4414 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4416 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4418 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4419 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4421 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4426 static struct tgt_handler mdt_sec_ctx_ops[] = {
4427 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4428 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4429 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4432 static struct tgt_handler mdt_quota_ops[] = {
4433 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4436 static struct tgt_opc_slice mdt_common_slice[] = {
4438 .tos_opc_start = MDS_FIRST_OPC,
4439 .tos_opc_end = MDS_LAST_OPC,
4440 .tos_hs = mdt_tgt_handlers
4443 .tos_opc_start = OBD_FIRST_OPC,
4444 .tos_opc_end = OBD_LAST_OPC,
4445 .tos_hs = tgt_obd_handlers
4448 .tos_opc_start = LDLM_FIRST_OPC,
4449 .tos_opc_end = LDLM_LAST_OPC,
4450 .tos_hs = tgt_dlm_handlers
4453 .tos_opc_start = SEC_FIRST_OPC,
4454 .tos_opc_end = SEC_LAST_OPC,
4455 .tos_hs = mdt_sec_ctx_ops
4458 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4459 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4460 .tos_hs = tgt_out_handlers
4463 .tos_opc_start = FLD_FIRST_OPC,
4464 .tos_opc_end = FLD_LAST_OPC,
4465 .tos_hs = fld_handlers
4468 .tos_opc_start = SEQ_FIRST_OPC,
4469 .tos_opc_end = SEQ_LAST_OPC,
4470 .tos_hs = seq_handlers
4473 .tos_opc_start = QUOTA_DQACQ,
4474 .tos_opc_end = QUOTA_LAST_OPC,
4475 .tos_hs = mdt_quota_ops
4478 .tos_opc_start = LLOG_FIRST_OPC,
4479 .tos_opc_end = LLOG_LAST_OPC,
4480 .tos_hs = tgt_llog_handlers
4483 .tos_opc_start = LFSCK_FIRST_OPC,
4484 .tos_opc_end = LFSCK_LAST_OPC,
4485 .tos_hs = tgt_lfsck_handlers
4493 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4495 struct md_device *next = m->mdt_child;
4496 struct lu_device *d = &m->mdt_lu_dev;
4497 struct obd_device *obd = mdt2obd_dev(m);
4498 struct lfsck_stop stop;
4501 if (m->mdt_md_root != NULL) {
4502 mdt_object_put(env, m->mdt_md_root);
4503 m->mdt_md_root = NULL;
4506 stop.ls_status = LS_PAUSED;
4508 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4510 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4511 target_recovery_fini(obd);
4512 ping_evictor_stop();
4514 if (m->mdt_opts.mo_coordinator)
4515 mdt_hsm_cdt_stop(m);
4517 mdt_hsm_cdt_fini(m);
4519 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4520 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4522 if (m->mdt_namespace != NULL)
4523 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4524 d->ld_obd->obd_force);
4526 obd_exports_barrier(obd);
4527 obd_zombie_barrier();
4531 tgt_fini(env, &m->mdt_lut);
4532 mdt_fs_cleanup(env, m);
4533 upcall_cache_cleanup(m->mdt_identity_cache);
4534 m->mdt_identity_cache = NULL;
4536 if (m->mdt_namespace != NULL) {
4537 ldlm_namespace_free_post(m->mdt_namespace);
4538 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4541 mdt_quota_fini(env, m);
4543 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4545 mdt_seq_fini(env, m);
4546 mdt_fld_fini(env, m);
4551 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4553 LASSERT(atomic_read(&d->ld_ref) == 0);
4555 server_put_mount(mdt_obd_name(m), true);
4560 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4562 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4563 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4565 struct mdt_thread_info *info;
4566 struct obd_device *obd;
4567 const char *dev = lustre_cfg_string(cfg, 0);
4568 const char *num = lustre_cfg_string(cfg, 2);
4569 struct lustre_mount_info *lmi = NULL;
4570 struct lustre_sb_info *lsi;
4572 struct seq_server_site *ss_site;
4573 const char *identity_upcall = "NONE";
4574 struct md_device *next;
4580 lu_device_init(&m->mdt_lu_dev, ldt);
4582 * Environment (env) might be missing mdt_thread_key values at that
4583 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4586 * Usually device allocation path doesn't use module key values, but
4587 * mdt has to do a lot of work here, so allocate key value.
4589 rc = lu_env_refill((struct lu_env *)env);
4593 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4594 LASSERT(info != NULL);
4596 obd = class_name2obd(dev);
4597 LASSERT(obd != NULL);
4599 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4600 m->mdt_opts.mo_evict_tgt_nids = 1;
4601 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4603 /* default is coordinator off, it is started through conf_param
4605 m->mdt_opts.mo_coordinator = 0;
4607 lmi = server_get_mount(dev);
4609 CERROR("Cannot get mount info for %s!\n", dev);
4612 lsi = s2lsi(lmi->lmi_sb);
4613 /* CMD is supported only in IAM mode */
4615 node_id = simple_strtol(num, NULL, 10);
4616 obd->u.obt.obt_magic = OBT_MAGIC;
4617 if (lsi->lsi_lmd != NULL &&
4618 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4619 m->mdt_skip_lfsck = 1;
4622 m->mdt_squash.rsi_uid = 0;
4623 m->mdt_squash.rsi_gid = 0;
4624 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4625 init_rwsem(&m->mdt_squash.rsi_sem);
4626 spin_lock_init(&m->mdt_lock);
4627 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4628 m->mdt_enable_remote_dir = 0;
4629 m->mdt_enable_remote_dir_gid = 0;
4631 atomic_set(&m->mdt_mds_mds_conns, 0);
4632 atomic_set(&m->mdt_async_commit_count, 0);
4634 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4635 m->mdt_lu_dev.ld_obd = obd;
4636 /* Set this lu_device to obd for error handling purposes. */
4637 obd->obd_lu_dev = &m->mdt_lu_dev;
4639 /* init the stack */
4640 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4642 CERROR("%s: Can't init device stack, rc %d\n",
4643 mdt_obd_name(m), rc);
4648 ss_site = mdt_seq_site(m);
4649 s->ld_seq_site = ss_site;
4652 /* set server index */
4653 ss_site->ss_node_id = node_id;
4655 /* failover is the default
4656 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4657 * assumed whose ss_node_id == 0 XXX
4659 obd->obd_replayable = 1;
4660 /* No connection accepted until configurations will finish */
4661 obd->obd_no_conn = 1;
4663 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4664 char *str = lustre_cfg_string(cfg, 4);
4665 if (strchr(str, 'n')) {
4666 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4667 obd->obd_replayable = 0;
4671 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4673 GOTO(err_fini_stack, rc);
4675 rc = mdt_seq_init(env, m);
4677 GOTO(err_fini_fld, rc);
4679 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4680 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4681 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4682 LDLM_NAMESPACE_SERVER,
4683 LDLM_NAMESPACE_GREEDY,
4685 if (m->mdt_namespace == NULL)
4686 GOTO(err_fini_seq, rc = -ENOMEM);
4688 m->mdt_namespace->ns_lvbp = m;
4689 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4691 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4692 /* set obd_namespace for compatibility with old code */
4693 obd->obd_namespace = m->mdt_namespace;
4695 rc = mdt_hsm_cdt_init(m);
4697 CERROR("%s: error initializing coordinator, rc %d\n",
4698 mdt_obd_name(m), rc);
4699 GOTO(err_free_ns, rc);
4702 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4703 OBD_FAIL_MDS_ALL_REQUEST_NET,
4704 OBD_FAIL_MDS_ALL_REPLY_NET);
4706 GOTO(err_free_hsm, rc);
4708 rc = mdt_fs_setup(env, m, obd, lsi);
4712 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4714 next = m->mdt_child;
4715 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4718 GOTO(err_fs_cleanup, rc);
4720 if (mntopts & MNTOPT_USERXATTR)
4721 m->mdt_opts.mo_user_xattr = 1;
4723 m->mdt_opts.mo_user_xattr = 0;
4725 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4727 GOTO(err_fs_cleanup, rc);
4729 if (mntopts & MNTOPT_ACL)
4730 m->mdt_opts.mo_acl = 1;
4732 m->mdt_opts.mo_acl = 0;
4734 /* XXX: to support suppgid for ACL, we enable identity_upcall
4735 * by default, otherwise, maybe got unexpected -EACCESS. */
4736 if (m->mdt_opts.mo_acl)
4737 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4739 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4741 &mdt_identity_upcall_cache_ops);
4742 if (IS_ERR(m->mdt_identity_cache)) {
4743 rc = PTR_ERR(m->mdt_identity_cache);
4744 m->mdt_identity_cache = NULL;
4745 GOTO(err_fs_cleanup, rc);
4748 rc = mdt_procfs_init(m, dev);
4750 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4751 GOTO(err_recovery, rc);
4754 rc = mdt_quota_init(env, m, cfg);
4756 GOTO(err_procfs, rc);
4758 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4759 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4760 "mdt_ldlm_client", m->mdt_ldlm_client);
4762 ping_evictor_start();
4764 /* recovery will be started upon mdt_prepare()
4765 * when the whole stack is complete and ready
4766 * to serve the requests */
4768 /* Reduce the initial timeout on an MDS because it doesn't need such
4769 * a long timeout as an OST does. Adaptive timeouts will adjust this
4770 * value appropriately. */
4771 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4772 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4778 target_recovery_fini(obd);
4779 upcall_cache_cleanup(m->mdt_identity_cache);
4780 m->mdt_identity_cache = NULL;
4782 mdt_fs_cleanup(env, m);
4784 tgt_fini(env, &m->mdt_lut);
4786 mdt_hsm_cdt_fini(m);
4788 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4789 obd->obd_namespace = m->mdt_namespace = NULL;
4791 mdt_seq_fini(env, m);
4793 mdt_fld_fini(env, m);
4795 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4798 server_put_mount(dev, true);
4802 /* For interoperability, the left element is old parameter, the right one
4803 * is the new version of the parameter, if some parameter is deprecated,
4804 * the new version should be set as NULL. */
4805 static struct cfg_interop_param mdt_interop_param[] = {
4806 { "mdt.group_upcall", NULL },
4807 { "mdt.quota_type", NULL },
4808 { "mdd.quota_type", NULL },
4809 { "mdt.som", NULL },
4810 { "mdt.rootsquash", "mdt.root_squash" },
4811 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4815 /* used by MGS to process specific configurations */
4816 static int mdt_process_config(const struct lu_env *env,
4817 struct lu_device *d, struct lustre_cfg *cfg)
4819 struct mdt_device *m = mdt_dev(d);
4820 struct md_device *md_next = m->mdt_child;
4821 struct lu_device *next = md2lu_dev(md_next);
4825 switch (cfg->lcfg_command) {
4827 struct obd_device *obd = d->ld_obd;
4829 /* For interoperability */
4830 struct cfg_interop_param *ptr = NULL;
4831 struct lustre_cfg *old_cfg = NULL;
4834 param = lustre_cfg_string(cfg, 1);
4835 if (param == NULL) {
4836 CERROR("param is empty\n");
4841 ptr = class_find_old_param(param, mdt_interop_param);
4843 if (ptr->new_param == NULL) {
4845 CWARN("For interoperability, skip this %s."
4846 " It is obsolete.\n", ptr->old_param);
4850 CWARN("Found old param %s, changed it to %s.\n",
4851 ptr->old_param, ptr->new_param);
4854 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4861 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4863 if (rc > 0 || rc == -ENOSYS) {
4864 /* is it an HSM var ? */
4865 rc = class_process_proc_param(PARAM_HSM,
4866 hsm_cdt_get_proc_vars(),
4868 if (rc > 0 || rc == -ENOSYS)
4869 /* we don't understand; pass it on */
4870 rc = next->ld_ops->ldo_process_config(env, next,
4874 if (old_cfg != NULL)
4875 lustre_cfg_free(cfg);
4880 /* others are passed further */
4881 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4887 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4888 const struct lu_object_header *hdr,
4889 struct lu_device *d)
4891 struct mdt_object *mo;
4895 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4897 struct lu_object *o;
4898 struct lu_object_header *h;
4901 h = &mo->mot_header;
4902 lu_object_header_init(h);
4903 lu_object_init(o, h, d);
4904 lu_object_add_top(h, o);
4905 o->lo_ops = &mdt_obj_ops;
4906 spin_lock_init(&mo->mot_write_lock);
4907 mutex_init(&mo->mot_lov_mutex);
4908 init_rwsem(&mo->mot_open_sem);
4914 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4915 const struct lu_object_conf *unused)
4917 struct mdt_device *d = mdt_dev(o->lo_dev);
4918 struct lu_device *under;
4919 struct lu_object *below;
4923 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4924 PFID(lu_object_fid(o)));
4926 under = &d->mdt_child->md_lu_dev;
4927 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4928 if (below != NULL) {
4929 lu_object_add(o, below);
4936 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4938 struct mdt_object *mo = mdt_obj(o);
4939 struct lu_object_header *h;
4943 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4944 PFID(lu_object_fid(o)));
4946 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4947 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4950 lu_object_header_fini(h);
4951 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4956 static int mdt_object_print(const struct lu_env *env, void *cookie,
4957 lu_printer_t p, const struct lu_object *o)
4959 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4961 return (*p)(env, cookie,
4962 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
4963 mdto, mdto->mot_lov_created ? "lov_created" : "",
4964 mdto->mot_cache_attr ? "cache_attr" : "",
4965 mdto->mot_write_count);
4968 static int mdt_prepare(const struct lu_env *env,
4969 struct lu_device *pdev,
4970 struct lu_device *cdev)
4972 struct mdt_device *mdt = mdt_dev(cdev);
4973 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4974 struct obd_device *obd = cdev->ld_obd;
4981 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4985 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
4989 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
4993 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
4994 /* The LFSCK instance is registered just now, so it must be there when
4995 * register the namespace to such instance. */
4996 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
4998 if (mdt->mdt_seq_site.ss_node_id == 0) {
4999 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5000 &mdt->mdt_md_root_fid);
5005 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5007 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5008 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5009 LASSERT(obd->obd_no_conn);
5010 spin_lock(&obd->obd_dev_lock);
5011 obd->obd_no_conn = 0;
5012 spin_unlock(&obd->obd_dev_lock);
5014 if (obd->obd_recovering == 0)
5015 mdt_postrecov(env, mdt);
5020 const struct lu_device_operations mdt_lu_ops = {
5021 .ldo_object_alloc = mdt_object_alloc,
5022 .ldo_process_config = mdt_process_config,
5023 .ldo_prepare = mdt_prepare,
5026 static const struct lu_object_operations mdt_obj_ops = {
5027 .loo_object_init = mdt_object_init,
5028 .loo_object_free = mdt_object_free,
5029 .loo_object_print = mdt_object_print
5032 static int mdt_obd_set_info_async(const struct lu_env *env,
5033 struct obd_export *exp,
5034 __u32 keylen, void *key,
5035 __u32 vallen, void *val,
5036 struct ptlrpc_request_set *set)
5042 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5043 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
5051 * Match client and server connection feature flags.
5053 * Compute the compatibility flags for a connection request based on
5054 * features mutually supported by client and server.
5056 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5057 * must not be updated here, otherwise a partially initialized value may
5058 * be exposed. After the connection request is successfully processed,
5059 * the top-level MDT connect request handler atomically updates the export
5060 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5061 * reply. \see mdt_connect().
5063 * \param exp the obd_export associated with this client/target pair
5064 * \param mdt the target device for the connection
5065 * \param data stores data for this connect request
5068 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5069 * \retval -EBADE client and server feature requirements are incompatible
5071 static int mdt_connect_internal(struct obd_export *exp,
5072 struct mdt_device *mdt,
5073 struct obd_connect_data *data)
5075 LASSERT(data != NULL);
5077 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5078 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5079 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5081 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5082 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5083 CWARN("%s: client %s does not support ibits lock, either "
5084 "very old or an invalid client: flags "LPX64"\n",
5085 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5086 data->ocd_connect_flags);
5090 if (!mdt->mdt_opts.mo_acl)
5091 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5093 if (!mdt->mdt_opts.mo_user_xattr)
5094 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5096 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5097 data->ocd_brw_size = min(data->ocd_brw_size,
5098 (__u32)MD_MAX_BRW_SIZE);
5099 if (data->ocd_brw_size == 0) {
5100 CERROR("%s: cli %s/%p ocd_connect_flags: "LPX64
5101 " ocd_version: %x ocd_grant: %d "
5102 "ocd_index: %u ocd_brw_size is "
5103 "unexpectedly zero, network data "
5104 "corruption? Refusing connection of this"
5107 exp->exp_client_uuid.uuid,
5108 exp, data->ocd_connect_flags, data->ocd_version,
5109 data->ocd_grant, data->ocd_index);
5114 /* NB: Disregard the rule against updating
5115 * exp_connect_data.ocd_connect_flags in this case, since
5116 * tgt_client_new() needs to know if this is a lightweight
5117 * connection, and it is safe to expose this flag before
5118 * connection processing completes. */
5119 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5120 spin_lock(&exp->exp_lock);
5121 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5122 spin_unlock(&exp->exp_lock);
5125 data->ocd_version = LUSTRE_VERSION_CODE;
5127 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5128 CWARN("%s: MDS requires FID support, but client not\n",
5133 if (OCD_HAS_FLAG(data, PINGLESS)) {
5134 if (ptlrpc_pinger_suppress_pings()) {
5135 spin_lock(&exp->exp_obd->obd_dev_lock);
5136 list_del_init(&exp->exp_obd_chain_timed);
5137 spin_unlock(&exp->exp_obd->obd_dev_lock);
5139 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5143 data->ocd_max_easize = mdt->mdt_max_ea_size;
5145 /* NB: Disregard the rule against updating
5146 * exp_connect_data.ocd_connect_flags in this case, since
5147 * tgt_client_new() needs to know if this is client supports
5148 * multiple modify RPCs, and it is safe to expose this flag before
5149 * connection processing completes. */
5150 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5151 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5152 spin_lock(&exp->exp_lock);
5153 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5154 spin_unlock(&exp->exp_lock);
5160 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5161 struct mdt_thread_info *info,
5162 struct mdt_file_data *mfd)
5164 struct lu_context ses;
5168 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5173 lu_context_enter(&ses);
5175 mdt_ucred(info)->uc_valid = UCRED_OLD;
5176 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5178 lu_context_exit(&ses);
5179 lu_context_fini(&ses);
5185 static int mdt_export_cleanup(struct obd_export *exp)
5187 struct list_head closing_list;
5188 struct mdt_export_data *med = &exp->exp_mdt_data;
5189 struct obd_device *obd = exp->exp_obd;
5190 struct mdt_device *mdt;
5191 struct mdt_thread_info *info;
5193 struct mdt_file_data *mfd, *n;
5197 INIT_LIST_HEAD(&closing_list);
5198 spin_lock(&med->med_open_lock);
5199 while (!list_empty(&med->med_open_head)) {
5200 struct list_head *tmp = med->med_open_head.next;
5201 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5203 /* Remove mfd handle so it can't be found again.
5204 * We are consuming the mfd_list reference here. */
5205 class_handle_unhash(&mfd->mfd_handle);
5206 list_move_tail(&mfd->mfd_list, &closing_list);
5208 spin_unlock(&med->med_open_lock);
5209 mdt = mdt_dev(obd->obd_lu_dev);
5210 LASSERT(mdt != NULL);
5212 rc = lu_env_init(&env, LCT_MD_THREAD);
5216 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5217 LASSERT(info != NULL);
5218 memset(info, 0, sizeof *info);
5219 info->mti_env = &env;
5220 info->mti_mdt = mdt;
5221 info->mti_exp = exp;
5223 if (!list_empty(&closing_list)) {
5224 struct md_attr *ma = &info->mti_attr;
5226 /* Close any open files (which may also cause orphan
5228 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5229 list_del_init(&mfd->mfd_list);
5230 ma->ma_need = ma->ma_valid = 0;
5232 /* This file is being closed due to an eviction, it
5233 * could have been modified and now dirty regarding to
5234 * HSM archive, check this!
5235 * The logic here is to mark a file dirty if there's a
5236 * chance it was dirtied before the client was evicted,
5237 * so that we don't have to wait for a release attempt
5238 * before finding out the file was actually dirty and
5239 * fail the release. Aggressively marking it dirty here
5240 * will cause the policy engine to attempt to
5241 * re-archive it; when rearchiving, we can compare the
5242 * current version to the HSM data_version and make the
5243 * archive request into a noop if it's not actually
5246 if (mfd->mfd_mode & FMODE_WRITE)
5247 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5249 /* Don't unlink orphan on failover umount, LU-184 */
5250 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5251 ma->ma_valid = MA_FLAGS;
5252 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5254 mdt_mfd_close(info, mfd);
5257 info->mti_mdt = NULL;
5258 /* cleanup client slot early */
5259 /* Do not erase record for recoverable client. */
5260 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5261 tgt_client_del(&env, exp);
5267 static inline void mdt_enable_slc(struct mdt_device *mdt)
5269 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5270 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5273 static inline void mdt_disable_slc(struct mdt_device *mdt)
5275 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5276 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5279 static int mdt_obd_disconnect(struct obd_export *exp)
5285 class_export_get(exp);
5287 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5288 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5289 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5291 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5292 mdt_disable_slc(mdt);
5295 rc = server_disconnect_export(exp);
5297 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5299 rc = mdt_export_cleanup(exp);
5300 nodemap_del_member(exp);
5301 class_export_put(exp);
5305 /* mds_connect copy */
5306 static int mdt_obd_connect(const struct lu_env *env,
5307 struct obd_export **exp, struct obd_device *obd,
5308 struct obd_uuid *cluuid,
5309 struct obd_connect_data *data,
5312 struct obd_export *lexp;
5313 struct lustre_handle conn = { 0 };
5314 struct mdt_device *mdt;
5316 lnet_nid_t *client_nid = localdata;
5319 LASSERT(env != NULL);
5320 LASSERT(data != NULL);
5322 if (!exp || !obd || !cluuid)
5325 mdt = mdt_dev(obd->obd_lu_dev);
5327 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5328 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5329 atomic_inc(&mdt->mdt_mds_mds_conns);
5330 mdt_enable_slc(mdt);
5334 * first, check whether the stack is ready to handle requests
5335 * XXX: probably not very appropriate method is used now
5336 * at some point we should find a better one
5338 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5339 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5340 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5341 rc = obd_get_info(env, mdt->mdt_child_exp,
5342 sizeof(KEY_OSP_CONNECTED),
5343 KEY_OSP_CONNECTED, NULL, NULL);
5346 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5349 rc = class_connect(&conn, obd, cluuid);
5353 lexp = class_conn2export(&conn);
5354 LASSERT(lexp != NULL);
5356 rc = nodemap_add_member(*client_nid, lexp);
5357 if (rc != 0 && rc != -EEXIST)
5360 rc = mdt_connect_internal(lexp, mdt, data);
5362 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5365 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5366 rc = tgt_client_new(env, lexp);
5368 mdt_export_stats_init(obd, lexp, localdata);
5372 class_disconnect(lexp);
5373 nodemap_del_member(lexp);
5377 /* Because we do not want this export to be evicted by pinger,
5378 * let's not add this export to the timed chain list. */
5379 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5380 spin_lock(&lexp->exp_obd->obd_dev_lock);
5381 list_del_init(&lexp->exp_obd_chain_timed);
5382 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5389 static int mdt_obd_reconnect(const struct lu_env *env,
5390 struct obd_export *exp, struct obd_device *obd,
5391 struct obd_uuid *cluuid,
5392 struct obd_connect_data *data,
5395 lnet_nid_t *client_nid = localdata;
5399 if (exp == NULL || obd == NULL || cluuid == NULL)
5402 rc = nodemap_add_member(*client_nid, exp);
5403 if (rc != 0 && rc != -EEXIST)
5406 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5408 mdt_export_stats_init(obd, exp, localdata);
5410 nodemap_del_member(exp);
5415 /* FIXME: Can we avoid using these two interfaces? */
5416 static int mdt_init_export(struct obd_export *exp)
5418 struct mdt_export_data *med = &exp->exp_mdt_data;
5422 INIT_LIST_HEAD(&med->med_open_head);
5423 spin_lock_init(&med->med_open_lock);
5424 spin_lock(&exp->exp_lock);
5425 exp->exp_connecting = 1;
5426 spin_unlock(&exp->exp_lock);
5428 /* self-export doesn't need client data and ldlm initialization */
5429 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5430 &exp->exp_client_uuid)))
5433 rc = tgt_client_alloc(exp);
5437 rc = ldlm_init_export(exp);
5444 tgt_client_free(exp);
5446 CERROR("%s: Failed to initialize export: rc = %d\n",
5447 exp->exp_obd->obd_name, rc);
5451 static int mdt_destroy_export(struct obd_export *exp)
5455 target_destroy_export(exp);
5456 /* destroy can be called from failed obd_setup, so
5457 * checking uuid is safer than obd_self_export */
5458 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5459 &exp->exp_client_uuid)))
5462 ldlm_destroy_export(exp);
5463 tgt_client_free(exp);
5465 LASSERT(list_empty(&exp->exp_outstanding_replies));
5466 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5471 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5472 struct linkea_data *ldata)
5476 LASSERT(ldata->ld_buf->lb_buf != NULL);
5478 if (!mdt_object_exists(mdt_obj))
5481 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5482 ldata->ld_buf, XATTR_NAME_LINK);
5483 if (rc == -ERANGE) {
5484 /* Buf was too small, figure out what we need. */
5485 lu_buf_free(ldata->ld_buf);
5486 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5487 ldata->ld_buf, XATTR_NAME_LINK);
5490 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5491 if (ldata->ld_buf->lb_buf == NULL)
5493 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5494 ldata->ld_buf, XATTR_NAME_LINK);
5499 return linkea_init(ldata);
5503 * Given an MDT object, try to look up the full path to the object.
5504 * Part of the MDT layer implementation of lfs fid2path.
5506 * \param[in] info Per-thread common data shared by MDT level handlers.
5507 * \param[in] obj Object to do path lookup of
5508 * \param[in,out] fp User-provided struct to store path information
5509 * \param[in] root_fid Root FID of current path should reach
5511 * \retval 0 Lookup successful, path information stored in fp
5512 * \retval -EAGAIN Lookup failed, usually because object is being moved
5513 * \retval negative errno if there was a problem
5515 static int mdt_path_current(struct mdt_thread_info *info,
5516 struct mdt_object *obj,
5517 struct getinfo_fid2path *fp,
5518 struct lu_fid *root_fid)
5520 struct mdt_device *mdt = info->mti_mdt;
5521 struct mdt_object *mdt_obj;
5522 struct link_ea_header *leh;
5523 struct link_ea_entry *lee;
5524 struct lu_name *tmpname = &info->mti_name;
5525 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5526 struct lu_buf *buf = &info->mti_big_buf;
5529 struct linkea_data ldata = { NULL };
5534 /* temp buffer for path element, the buffer will be finally freed
5535 * in mdt_thread_info_fini */
5536 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5537 if (buf->lb_buf == NULL)
5541 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5544 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5546 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5547 struct lu_buf lmv_buf;
5549 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5550 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5551 GOTO(out, rc = -ENOENT);
5553 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5554 if (IS_ERR(mdt_obj))
5555 GOTO(out, rc = PTR_ERR(mdt_obj));
5557 if (!mdt_object_exists(mdt_obj)) {
5558 mdt_object_put(info->mti_env, mdt_obj);
5559 GOTO(out, rc = -ENOENT);
5562 if (mdt_object_remote(mdt_obj)) {
5563 mdt_object_put(info->mti_env, mdt_obj);
5564 GOTO(remote_out, rc = -EREMOTE);
5567 rc = mdt_links_read(info, mdt_obj, &ldata);
5569 mdt_object_put(info->mti_env, mdt_obj);
5574 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5575 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5576 /* If set, use link #linkno for path lookup, otherwise use
5577 link #0. Only do this for the final path element. */
5578 if (first && fp->gf_linkno < leh->leh_reccount) {
5580 for (count = 0; count < fp->gf_linkno; count++) {
5581 lee = (struct link_ea_entry *)
5582 ((char *)lee + reclen);
5583 linkea_entry_unpack(lee, &reclen, tmpname,
5586 if (fp->gf_linkno < leh->leh_reccount - 1)
5587 /* indicate to user there are more links */
5591 lmv_buf.lb_buf = info->mti_xattr_buf;
5592 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5593 /* Check if it is slave stripes */
5594 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5595 &lmv_buf, XATTR_NAME_LMV);
5596 mdt_object_put(info->mti_env, mdt_obj);
5598 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5600 /* For slave stripes, get its master */
5601 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5602 fp->gf_fid = *tmpfid;
5605 } else if (rc < 0 && rc != -ENODATA) {
5611 /* Pack the name in the end of the buffer */
5612 ptr -= tmpname->ln_namelen;
5613 if (ptr - 1 <= fp->gf_u.gf_path)
5614 GOTO(out, rc = -EOVERFLOW);
5615 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5618 /* keep the last resolved fid to the client, so the
5619 * client will build the left path on another MDT for
5621 fp->gf_fid = *tmpfid;
5627 ptr++; /* skip leading / */
5628 memmove(fp->gf_u.gf_path, ptr,
5629 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5636 * Given an MDT object, use mdt_path_current to get the path.
5637 * Essentially a wrapper to retry mdt_path_current a set number of times
5638 * if -EAGAIN is returned (usually because an object is being moved).
5640 * Part of the MDT layer implementation of lfs fid2path.
5642 * \param[in] info Per-thread common data shared by mdt level handlers.
5643 * \param[in] obj Object to do path lookup of
5644 * \param[in,out] fp User-provided struct for arguments and to store path
5647 * \retval 0 Lookup successful, path information stored in fp
5648 * \retval negative errno if there was a problem
5650 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5651 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5653 struct mdt_device *mdt = info->mti_mdt;
5658 if (fp->gf_pathlen < 3)
5661 if (root_fid == NULL)
5662 root_fid = &mdt->mdt_md_root_fid;
5664 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5665 fp->gf_u.gf_path[0] = '\0';
5669 /* Retry multiple times in case file is being moved */
5670 while (tries-- && rc == -EAGAIN)
5671 rc = mdt_path_current(info, obj, fp, root_fid);
5677 * Get the full path of the provided FID, as of changelog record recno.
5679 * This checks sanity and looks up object for user provided FID
5680 * before calling the actual path lookup code.
5682 * Part of the MDT layer implementation of lfs fid2path.
5684 * \param[in] info Per-thread common data shared by mdt level handlers.
5685 * \param[in,out] fp User-provided struct for arguments and to store path
5688 * \retval 0 Lookup successful, path information and recno stored in fp
5689 * \retval -ENOENT, object does not exist
5690 * \retval negative errno if there was a problem
5692 static int mdt_fid2path(struct mdt_thread_info *info,
5693 struct lu_fid *root_fid,
5694 struct getinfo_fid2path *fp)
5696 struct mdt_device *mdt = info->mti_mdt;
5697 struct mdt_object *obj;
5701 CDEBUG(D_IOCTL, "path get "DFID" from "LPU64" #%d\n",
5702 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5704 if (!fid_is_sane(&fp->gf_fid))
5707 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5708 CWARN("%s: "DFID" is invalid, sequence should be "
5709 ">= "LPX64"\n", mdt_obd_name(mdt),
5710 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5714 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5717 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5718 PFID(&fp->gf_fid), rc);
5722 if (mdt_object_remote(obj))
5724 else if (!mdt_object_exists(obj))
5730 mdt_object_put(info->mti_env, obj);
5731 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5732 PFID(&fp->gf_fid), rc);
5736 rc = mdt_path(info, obj, fp, root_fid);
5738 CDEBUG(D_INFO, "fid "DFID", path %s recno "LPX64" linkno %u\n",
5739 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5740 fp->gf_recno, fp->gf_linkno);
5742 mdt_object_put(info->mti_env, obj);
5747 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5748 void *val, int vallen)
5750 struct getinfo_fid2path *fpout, *fpin;
5751 struct lu_fid *root_fid = NULL;
5754 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5757 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5758 lustre_swab_fid2path(fpin);
5760 memcpy(fpout, fpin, sizeof(*fpin));
5761 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5764 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5765 sizeof(struct lu_fid)) {
5766 /* client sent its root FID, which is normally fileset FID */
5767 root_fid = fpin->gf_u.gf_root_fid;
5768 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5769 lustre_swab_lu_fid(root_fid);
5771 if (root_fid != NULL && !fid_is_sane(root_fid))
5775 rc = mdt_fid2path(info, root_fid, fpout);
5779 int mdt_get_info(struct tgt_session_info *tsi)
5789 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5791 CDEBUG(D_IOCTL, "No GETINFO key\n");
5792 RETURN(err_serious(-EFAULT));
5794 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5797 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5798 if (vallen == NULL) {
5799 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5800 tgt_name(tsi->tsi_tgt));
5801 RETURN(err_serious(-EFAULT));
5804 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5806 rc = req_capsule_server_pack(tsi->tsi_pill);
5808 RETURN(err_serious(rc));
5810 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5811 if (valout == NULL) {
5812 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5813 tgt_name(tsi->tsi_tgt));
5814 RETURN(err_serious(-EFAULT));
5817 if (KEY_IS(KEY_FID2PATH)) {
5818 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5820 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5821 mdt_thread_info_fini(info);
5828 /* Pass the ioc down */
5829 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5830 unsigned int cmd, int len, void *data)
5832 struct lu_context ioctl_session;
5833 struct md_device *next = mdt->mdt_child;
5837 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5840 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5841 lu_context_enter(&ioctl_session);
5842 env->le_ses = &ioctl_session;
5844 LASSERT(next->md_ops->mdo_iocontrol);
5845 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5847 lu_context_exit(&ioctl_session);
5848 lu_context_fini(&ioctl_session);
5852 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5854 struct obd_ioctl_data *data = karg;
5857 struct mdt_object *obj;
5858 struct mdt_lock_handle *lh;
5862 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5863 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5866 fid = (struct lu_fid *)data->ioc_inlbuf1;
5868 if (!fid_is_sane(fid))
5871 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5873 lh = &mti->mti_lh[MDT_LH_PARENT];
5874 mdt_lock_reg_init(lh, LCK_CR);
5876 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5878 RETURN(PTR_ERR(obj));
5880 if (mdt_object_remote(obj)) {
5883 * before calling version get the correct MDS should be
5884 * fid, this is error to find remote object here
5886 CERROR("nonlocal object "DFID"\n", PFID(fid));
5887 } else if (!mdt_object_exists(obj)) {
5888 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5891 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5892 *(__u64 *)data->ioc_inlbuf2 = version;
5895 mdt_object_unlock_put(mti, obj, lh, 1);
5899 /* ioctls on obd dev */
5900 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5901 void *karg, void __user *uarg)
5904 struct obd_device *obd = exp->exp_obd;
5905 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5906 struct dt_device *dt = mdt->mdt_bottom;
5910 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5911 rc = lu_env_init(&env, LCT_MD_THREAD);
5917 rc = mdt_device_sync(&env, mdt);
5919 case OBD_IOC_SET_READONLY:
5920 rc = dt_sync(&env, dt);
5922 rc = dt_ro(&env, dt);
5924 case OBD_IOC_ABORT_RECOVERY:
5925 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5926 obd->obd_abort_recovery = 1;
5927 target_stop_recovery_thread(obd);
5930 case OBD_IOC_CHANGELOG_REG:
5931 case OBD_IOC_CHANGELOG_DEREG:
5932 case OBD_IOC_CHANGELOG_CLEAR:
5933 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5935 case OBD_IOC_START_LFSCK: {
5936 struct md_device *next = mdt->mdt_child;
5937 struct obd_ioctl_data *data = karg;
5938 struct lfsck_start_param lsp;
5940 if (unlikely(data == NULL)) {
5945 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5946 lsp.lsp_index_valid = 0;
5947 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5950 case OBD_IOC_STOP_LFSCK: {
5951 struct md_device *next = mdt->mdt_child;
5952 struct obd_ioctl_data *data = karg;
5953 struct lfsck_stop stop;
5955 stop.ls_status = LS_STOPPED;
5956 /* Old lfsck utils may pass NULL @stop. */
5957 if (data->ioc_inlbuf1 == NULL)
5961 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5963 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5966 case OBD_IOC_QUERY_LFSCK: {
5967 struct md_device *next = mdt->mdt_child;
5968 struct obd_ioctl_data *data = karg;
5970 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
5974 case OBD_IOC_GET_OBJ_VERSION: {
5975 struct mdt_thread_info *mti;
5976 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5977 memset(mti, 0, sizeof *mti);
5978 mti->mti_env = &env;
5982 rc = mdt_ioc_version_get(mti, karg);
5985 case OBD_IOC_CATLOGLIST: {
5986 struct mdt_thread_info *mti;
5988 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5989 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
5990 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
5991 &mti->mti_tmp_fid1);
5996 CERROR("%s: Not supported cmd = %d, rc = %d\n",
5997 mdt_obd_name(mdt), cmd, rc);
6004 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6006 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6010 if (!mdt->mdt_skip_lfsck) {
6011 struct lfsck_start_param lsp;
6013 lsp.lsp_start = NULL;
6014 lsp.lsp_index_valid = 0;
6015 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6016 OBD_IOC_START_LFSCK,
6018 if (rc != 0 && rc != -EALREADY)
6019 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6020 mdt_obd_name(mdt), rc);
6023 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6027 static int mdt_obd_postrecov(struct obd_device *obd)
6032 rc = lu_env_init(&env, LCT_MD_THREAD);
6035 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6040 static struct obd_ops mdt_obd_device_ops = {
6041 .o_owner = THIS_MODULE,
6042 .o_set_info_async = mdt_obd_set_info_async,
6043 .o_connect = mdt_obd_connect,
6044 .o_reconnect = mdt_obd_reconnect,
6045 .o_disconnect = mdt_obd_disconnect,
6046 .o_init_export = mdt_init_export,
6047 .o_destroy_export = mdt_destroy_export,
6048 .o_iocontrol = mdt_iocontrol,
6049 .o_postrecov = mdt_obd_postrecov,
6052 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6053 struct lu_device *d)
6055 struct mdt_device *m = mdt_dev(d);
6062 static struct lu_device *mdt_device_free(const struct lu_env *env,
6063 struct lu_device *d)
6065 struct mdt_device *m = mdt_dev(d);
6068 lu_device_fini(&m->mdt_lu_dev);
6074 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6075 struct lu_device_type *t,
6076 struct lustre_cfg *cfg)
6078 struct lu_device *l;
6079 struct mdt_device *m;
6086 rc = mdt_init0(env, m, t, cfg);
6088 mdt_device_free(env, l);
6093 l = ERR_PTR(-ENOMEM);
6097 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6098 LU_KEY_INIT(mdt, struct mdt_thread_info);
6100 static void mdt_key_fini(const struct lu_context *ctx,
6101 struct lu_context_key *key, void* data)
6103 struct mdt_thread_info *info = data;
6105 if (info->mti_big_lmm) {
6106 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6107 info->mti_big_lmm = NULL;
6108 info->mti_big_lmmsize = 0;
6113 /* context key: mdt_thread_key */
6114 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6116 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6118 return lu_ucred(info->mti_env);
6121 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6123 return lu_ucred_check(info->mti_env);
6127 * Enable/disable COS (Commit On Sharing).
6129 * Set/Clear the COS flag in mdt options.
6131 * \param mdt mdt device
6132 * \param val 0 disables COS, other values enable COS
6134 void mdt_enable_cos(struct mdt_device *mdt, int val)
6139 mdt->mdt_opts.mo_cos = !!val;
6140 rc = lu_env_init(&env, LCT_LOCAL);
6141 if (unlikely(rc != 0)) {
6142 CWARN("%s: lu_env initialization failed, cannot "
6143 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6146 mdt_device_sync(&env, mdt);
6151 * Check COS (Commit On Sharing) status.
6153 * Return COS flag status.
6155 * \param mdt mdt device
6157 int mdt_cos_is_enabled(struct mdt_device *mdt)
6159 return mdt->mdt_opts.mo_cos != 0;
6162 static struct lu_device_type_operations mdt_device_type_ops = {
6163 .ldto_device_alloc = mdt_device_alloc,
6164 .ldto_device_free = mdt_device_free,
6165 .ldto_device_fini = mdt_device_fini
6168 static struct lu_device_type mdt_device_type = {
6169 .ldt_tags = LU_DEVICE_MD,
6170 .ldt_name = LUSTRE_MDT_NAME,
6171 .ldt_ops = &mdt_device_type_ops,
6172 .ldt_ctx_tags = LCT_MD_THREAD
6175 static int __init mdt_init(void)
6179 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6180 FID_NOBRACE_LEN + 1);
6181 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6183 rc = lu_kmem_init(mdt_caches);
6187 rc = mds_mod_init();
6191 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6192 LUSTRE_MDT_NAME, &mdt_device_type);
6197 lu_kmem_fini(mdt_caches);
6204 static void __exit mdt_exit(void)
6206 class_unregister_type(LUSTRE_MDT_NAME);
6208 lu_kmem_fini(mdt_caches);
6211 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6212 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6213 MODULE_VERSION(LUSTRE_VERSION_STRING);
6214 MODULE_LICENSE("GPL");
6216 module_init(mdt_init);
6217 module_exit(mdt_exit);