4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/mdt/mdt_handler.c
34 * Lustre Metadata Target (mdt) request handler
36 * Author: Peter Braam <braam@clusterfs.com>
37 * Author: Andreas Dilger <adilger@clusterfs.com>
38 * Author: Phil Schwan <phil@clusterfs.com>
39 * Author: Mike Shaver <shaver@clusterfs.com>
40 * Author: Nikita Danilov <nikita@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
42 * Author: Yury Umanets <umka@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #include <linux/module.h>
48 #include <linux/pagemap.h>
50 #include <dt_object.h>
51 #include <lustre_acl.h>
52 #include <lustre_export.h>
53 #include <lustre_ioctl.h>
54 #include <lustre_lfsck.h>
55 #include <lustre_log.h>
56 #include <lustre_net.h>
57 #include <lustre_nodemap.h>
58 #include <lustre_mds.h>
59 #include <lustre_param.h>
60 #include <lustre_quota.h>
61 #include <lustre_swab.h>
63 #include <obd_support.h>
65 #include <llog_swab.h>
67 #include "mdt_internal.h"
70 static unsigned int max_mod_rpcs_per_client = 8;
71 module_param(max_mod_rpcs_per_client, uint, 0644);
72 MODULE_PARM_DESC(max_mod_rpcs_per_client, "maximum number of modify RPCs in flight allowed per client");
74 mdl_mode_t mdt_mdl_lock_modes[] = {
75 [LCK_MINMODE] = MDL_MINMODE,
82 [LCK_GROUP] = MDL_GROUP
85 enum ldlm_mode mdt_dlm_lock_modes[] = {
86 [MDL_MINMODE] = LCK_MINMODE,
93 [MDL_GROUP] = LCK_GROUP
96 static struct mdt_device *mdt_dev(struct lu_device *d);
97 static int mdt_unpack_req_pack_rep(struct mdt_thread_info *info, __u32 flags);
99 static const struct lu_object_operations mdt_obj_ops;
101 /* Slab for MDT object allocation */
102 static struct kmem_cache *mdt_object_kmem;
104 /* For HSM restore handles */
105 struct kmem_cache *mdt_hsm_cdt_kmem;
107 /* For HSM request handles */
108 struct kmem_cache *mdt_hsm_car_kmem;
110 static struct lu_kmem_descr mdt_caches[] = {
112 .ckd_cache = &mdt_object_kmem,
113 .ckd_name = "mdt_obj",
114 .ckd_size = sizeof(struct mdt_object)
117 .ckd_cache = &mdt_hsm_cdt_kmem,
118 .ckd_name = "mdt_cdt_restore_handle",
119 .ckd_size = sizeof(struct cdt_restore_handle)
122 .ckd_cache = &mdt_hsm_car_kmem,
123 .ckd_name = "mdt_cdt_agent_req",
124 .ckd_size = sizeof(struct cdt_agent_req)
131 __u64 mdt_get_disposition(struct ldlm_reply *rep, __u64 op_flag)
135 return rep->lock_policy_res1 & op_flag;
138 void mdt_clear_disposition(struct mdt_thread_info *info,
139 struct ldlm_reply *rep, __u64 op_flag)
142 info->mti_opdata &= ~op_flag;
143 tgt_opdata_clear(info->mti_env, op_flag);
146 rep->lock_policy_res1 &= ~op_flag;
149 void mdt_set_disposition(struct mdt_thread_info *info,
150 struct ldlm_reply *rep, __u64 op_flag)
153 info->mti_opdata |= op_flag;
154 tgt_opdata_set(info->mti_env, op_flag);
157 rep->lock_policy_res1 |= op_flag;
160 void mdt_lock_reg_init(struct mdt_lock_handle *lh, enum ldlm_mode lm)
162 lh->mlh_pdo_hash = 0;
163 lh->mlh_reg_mode = lm;
164 lh->mlh_rreg_mode = lm;
165 lh->mlh_type = MDT_REG_LOCK;
168 void mdt_lock_pdo_init(struct mdt_lock_handle *lh, enum ldlm_mode lock_mode,
169 const struct lu_name *lname)
171 lh->mlh_reg_mode = lock_mode;
172 lh->mlh_pdo_mode = LCK_MINMODE;
173 lh->mlh_rreg_mode = lock_mode;
174 lh->mlh_type = MDT_PDO_LOCK;
176 if (lu_name_is_valid(lname)) {
177 lh->mlh_pdo_hash = full_name_hash(lname->ln_name,
179 /* XXX Workaround for LU-2856
181 * Zero is a valid return value of full_name_hash, but
182 * several users of mlh_pdo_hash assume a non-zero
183 * hash value. We therefore map zero onto an
184 * arbitrary, but consistent value (1) to avoid
185 * problems further down the road. */
186 if (unlikely(lh->mlh_pdo_hash == 0))
187 lh->mlh_pdo_hash = 1;
189 lh->mlh_pdo_hash = 0;
193 static void mdt_lock_pdo_mode(struct mdt_thread_info *info, struct mdt_object *o,
194 struct mdt_lock_handle *lh)
200 * Any dir access needs couple of locks:
202 * 1) on part of dir we gonna take lookup/modify;
204 * 2) on whole dir to protect it from concurrent splitting and/or to
205 * flush client's cache for readdir().
207 * so, for a given mode and object this routine decides what lock mode
208 * to use for lock #2:
210 * 1) if caller's gonna lookup in dir then we need to protect dir from
211 * being splitted only - LCK_CR
213 * 2) if caller's gonna modify dir then we need to protect dir from
214 * being splitted and to flush cache - LCK_CW
216 * 3) if caller's gonna modify dir and that dir seems ready for
217 * splitting then we need to protect it from any type of access
218 * (lookup/modify/split) - LCK_EX --bzzz
221 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
222 LASSERT(lh->mlh_pdo_mode == LCK_MINMODE);
225 * Ask underlaying level its opinion about preferable PDO lock mode
226 * having access type passed as regular lock mode:
228 * - MDL_MINMODE means that lower layer does not want to specify lock
231 * - MDL_NL means that no PDO lock should be taken. This is used in some
232 * cases. Say, for non-splittable directories no need to use PDO locks
235 mode = mdo_lock_mode(info->mti_env, mdt_object_child(o),
236 mdt_dlm_mode2mdl_mode(lh->mlh_reg_mode));
238 if (mode != MDL_MINMODE) {
239 lh->mlh_pdo_mode = mdt_mdl_mode2dlm_mode(mode);
242 * Lower layer does not want to specify locking mode. We do it
243 * our selves. No special protection is needed, just flush
244 * client's cache on modification and allow concurrent
247 switch (lh->mlh_reg_mode) {
249 lh->mlh_pdo_mode = LCK_EX;
252 lh->mlh_pdo_mode = LCK_CR;
255 lh->mlh_pdo_mode = LCK_CW;
258 CERROR("Not expected lock type (0x%x)\n",
259 (int)lh->mlh_reg_mode);
264 LASSERT(lh->mlh_pdo_mode != LCK_MINMODE);
268 static int mdt_lookup_fileset(struct mdt_thread_info *info, const char *fileset,
271 struct mdt_device *mdt = info->mti_mdt;
272 struct lu_name *lname = &info->mti_name;
274 struct mdt_object *parent;
278 LASSERT(!info->mti_cross_ref);
280 OBD_ALLOC(name, NAME_MAX + 1);
283 lname->ln_name = name;
286 * We may want to allow this to mount a completely separate
287 * fileset from the MDT in the future, but keeping it to
288 * ROOT/ only for now avoid potential security issues.
290 *fid = mdt->mdt_md_root_fid;
292 while (rc == 0 && fileset != NULL && *fileset != '\0') {
293 const char *s1 = fileset;
299 while (*s2 != '/' && *s2 != '\0')
307 lname->ln_namelen = s2 - s1;
308 if (lname->ln_namelen > NAME_MAX) {
313 /* reject .. as a path component */
314 if (lname->ln_namelen == 2 &&
315 strncmp(s1, "..", 2) == 0) {
320 strncpy(name, s1, lname->ln_namelen);
321 name[lname->ln_namelen] = '\0';
323 parent = mdt_object_find(info->mti_env, mdt, fid);
324 if (IS_ERR(parent)) {
325 rc = PTR_ERR(parent);
328 /* Only got the fid of this obj by name */
330 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
331 fid, &info->mti_spec);
332 mdt_object_put(info->mti_env, parent);
335 parent = mdt_object_find(info->mti_env, mdt, fid);
337 rc = PTR_ERR(parent);
339 mode = lu_object_attr(&parent->mot_obj);
340 mdt_object_put(info->mti_env, parent);
346 OBD_FREE(name, NAME_MAX + 1);
351 static int mdt_get_root(struct tgt_session_info *tsi)
353 struct mdt_thread_info *info = tsi2mdt_info(tsi);
354 struct mdt_device *mdt = info->mti_mdt;
355 struct mdt_body *repbody;
356 char *fileset = NULL, *buffer = NULL;
358 struct obd_export *exp = info->mti_exp;
359 char *nodemap_fileset;
363 rc = mdt_check_ucred(info);
365 GOTO(out, rc = err_serious(rc));
367 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GET_ROOT_PACK))
368 GOTO(out, rc = err_serious(-ENOMEM));
370 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
371 if (req_capsule_get_size(info->mti_pill, &RMF_NAME, RCL_CLIENT) > 0) {
372 fileset = req_capsule_client_get(info->mti_pill, &RMF_NAME);
374 GOTO(out, rc = err_serious(-EFAULT));
377 nodemap_fileset = nodemap_get_fileset(exp->exp_target_data.ted_nodemap);
378 if (nodemap_fileset && nodemap_fileset[0]) {
379 CDEBUG(D_INFO, "nodemap fileset is %s\n", nodemap_fileset);
381 /* consider fileset from client as a sub-fileset
382 * of the nodemap one */
383 OBD_ALLOC(buffer, PATH_MAX + 1);
385 GOTO(out, rc = err_serious(-ENOMEM));
386 if (snprintf(buffer, PATH_MAX + 1, "%s/%s",
387 nodemap_fileset, fileset) >= PATH_MAX + 1)
388 GOTO(out, rc = err_serious(-EINVAL));
391 /* enforce fileset as specified in the nodemap */
392 fileset = nodemap_fileset;
397 CDEBUG(D_INFO, "Getting fileset %s\n", fileset);
398 rc = mdt_lookup_fileset(info, fileset, &repbody->mbo_fid1);
400 GOTO(out, rc = err_serious(rc));
402 repbody->mbo_fid1 = mdt->mdt_md_root_fid;
404 repbody->mbo_valid |= OBD_MD_FLID;
408 mdt_thread_info_fini(info);
410 OBD_FREE(buffer, PATH_MAX+1);
414 static int mdt_statfs(struct tgt_session_info *tsi)
416 struct ptlrpc_request *req = tgt_ses_req(tsi);
417 struct mdt_thread_info *info = tsi2mdt_info(tsi);
418 struct md_device *next = info->mti_mdt->mdt_child;
419 struct ptlrpc_service_part *svcpt;
420 struct obd_statfs *osfs;
425 svcpt = req->rq_rqbd->rqbd_svcpt;
427 /* This will trigger a watchdog timeout */
428 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_STATFS_LCW_SLEEP,
429 (MDT_SERVICE_WATCHDOG_FACTOR *
430 at_get(&svcpt->scp_at_estimate)) + 1);
432 rc = mdt_check_ucred(info);
434 GOTO(out, rc = err_serious(rc));
436 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STATFS_PACK))
437 GOTO(out, rc = err_serious(-ENOMEM));
439 osfs = req_capsule_server_get(info->mti_pill, &RMF_OBD_STATFS);
441 GOTO(out, rc = -EPROTO);
443 /** statfs information are cached in the mdt_device */
444 if (cfs_time_before_64(info->mti_mdt->mdt_osfs_age,
445 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS))) {
446 /** statfs data is too old, get up-to-date one */
447 rc = next->md_ops->mdo_statfs(info->mti_env, next, osfs);
450 spin_lock(&info->mti_mdt->mdt_lock);
451 info->mti_mdt->mdt_osfs = *osfs;
452 info->mti_mdt->mdt_osfs_age = cfs_time_current_64();
453 spin_unlock(&info->mti_mdt->mdt_lock);
455 /** use cached statfs data */
456 spin_lock(&info->mti_mdt->mdt_lock);
457 *osfs = info->mti_mdt->mdt_osfs;
458 spin_unlock(&info->mti_mdt->mdt_lock);
462 mdt_counter_incr(req, LPROC_MDT_STATFS);
464 mdt_thread_info_fini(info);
468 #ifdef CONFIG_FS_POSIX_ACL
470 * Pack ACL data into the reply. UIDs/GIDs are mapped and filtered by nodemap.
472 * \param info thread info object
473 * \param repbody reply to pack ACLs into
474 * \param o mdt object of file to examine
475 * \param nodemap nodemap of client to reply to
477 * \retval -errno error getting or parsing ACL from disk
479 int mdt_pack_acl2body(struct mdt_thread_info *info, struct mdt_body *repbody,
480 struct mdt_object *o, struct lu_nodemap *nodemap)
482 const struct lu_env *env = info->mti_env;
483 struct md_object *next = mdt_object_child(o);
484 struct lu_buf *buf = &info->mti_buf;
487 buf->lb_buf = req_capsule_server_get(info->mti_pill, &RMF_ACL);
488 buf->lb_len = req_capsule_get_size(info->mti_pill, &RMF_ACL,
490 if (buf->lb_len == 0)
493 rc = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_ACCESS);
495 if (rc == -ENODATA) {
496 repbody->mbo_aclsize = 0;
497 repbody->mbo_valid |= OBD_MD_FLACL;
499 } else if (rc == -EOPNOTSUPP) {
502 CERROR("%s: unable to read "DFID" ACL: rc = %d\n",
503 mdt_obd_name(info->mti_mdt),
504 PFID(mdt_object_fid(o)), rc);
507 rc = nodemap_map_acl(nodemap, buf->lb_buf,
508 rc, NODEMAP_FS_TO_CLIENT);
509 /* if all ACLs mapped out, rc is still >= 0 */
511 CERROR("%s: nodemap_map_acl unable to parse "DFID
512 " ACL: rc = %d\n", mdt_obd_name(info->mti_mdt),
513 PFID(mdt_object_fid(o)), rc);
515 repbody->mbo_aclsize = rc;
516 repbody->mbo_valid |= OBD_MD_FLACL;
524 void mdt_pack_attr2body(struct mdt_thread_info *info, struct mdt_body *b,
525 const struct lu_attr *attr, const struct lu_fid *fid)
527 struct md_attr *ma = &info->mti_attr;
528 struct obd_export *exp = info->mti_exp;
529 struct lu_nodemap *nodemap = NULL;
531 LASSERT(ma->ma_valid & MA_INODE);
533 if (attr->la_valid & LA_ATIME) {
534 b->mbo_atime = attr->la_atime;
535 b->mbo_valid |= OBD_MD_FLATIME;
537 if (attr->la_valid & LA_MTIME) {
538 b->mbo_mtime = attr->la_mtime;
539 b->mbo_valid |= OBD_MD_FLMTIME;
541 if (attr->la_valid & LA_CTIME) {
542 b->mbo_ctime = attr->la_ctime;
543 b->mbo_valid |= OBD_MD_FLCTIME;
545 if (attr->la_valid & LA_FLAGS) {
546 b->mbo_flags = attr->la_flags;
547 b->mbo_valid |= OBD_MD_FLFLAGS;
549 if (attr->la_valid & LA_NLINK) {
550 b->mbo_nlink = attr->la_nlink;
551 b->mbo_valid |= OBD_MD_FLNLINK;
553 if (attr->la_valid & (LA_UID|LA_GID)) {
554 nodemap = nodemap_get_from_exp(exp);
558 if (attr->la_valid & LA_UID) {
559 b->mbo_uid = nodemap_map_id(nodemap, NODEMAP_UID,
560 NODEMAP_FS_TO_CLIENT,
562 b->mbo_valid |= OBD_MD_FLUID;
564 if (attr->la_valid & LA_GID) {
565 b->mbo_gid = nodemap_map_id(nodemap, NODEMAP_GID,
566 NODEMAP_FS_TO_CLIENT,
568 b->mbo_valid |= OBD_MD_FLGID;
571 b->mbo_mode = attr->la_mode;
572 if (attr->la_valid & LA_MODE)
573 b->mbo_valid |= OBD_MD_FLMODE;
574 if (attr->la_valid & LA_TYPE)
575 b->mbo_valid |= OBD_MD_FLTYPE;
579 b->mbo_valid |= OBD_MD_FLID;
580 CDEBUG(D_INODE, DFID": nlink=%d, mode=%o, valid=%#llx\n",
581 PFID(fid), b->mbo_nlink, b->mbo_mode, b->mbo_valid);
584 if (!(attr->la_valid & LA_TYPE))
587 b->mbo_rdev = attr->la_rdev;
588 b->mbo_size = attr->la_size;
589 b->mbo_blocks = attr->la_blocks;
591 if (!S_ISREG(attr->la_mode)) {
592 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS | OBD_MD_FLRDEV;
593 } else if (ma->ma_need & MA_LOV && !(ma->ma_valid & MA_LOV)) {
594 /* means no objects are allocated on osts. */
595 LASSERT(!(ma->ma_valid & MA_LOV));
596 /* just ignore blocks occupied by extend attributes on MDS */
598 /* if no object is allocated on osts, the size on mds is valid.
600 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
601 } else if ((ma->ma_valid & MA_LOV) && ma->ma_lmm != NULL &&
602 ma->ma_lmm->lmm_pattern & LOV_PATTERN_F_RELEASED) {
603 /* A released file stores its size on MDS. */
604 /* But return 1 block for released file, unless tools like tar
605 * will consider it fully sparse. (LU-3864)
607 if (unlikely(b->mbo_size == 0))
611 b->mbo_valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
614 if (fid != NULL && (b->mbo_valid & OBD_MD_FLSIZE))
615 CDEBUG(D_VFSTRACE, DFID": returning size %llu\n",
616 PFID(fid), (unsigned long long)b->mbo_size);
619 if (!IS_ERR_OR_NULL(nodemap))
620 nodemap_putref(nodemap);
623 static inline int mdt_body_has_lov(const struct lu_attr *la,
624 const struct mdt_body *body)
626 return (S_ISREG(la->la_mode) && (body->mbo_valid & OBD_MD_FLEASIZE)) ||
627 (S_ISDIR(la->la_mode) && (body->mbo_valid & OBD_MD_FLDIREA));
630 void mdt_client_compatibility(struct mdt_thread_info *info)
632 struct mdt_body *body;
633 struct ptlrpc_request *req = mdt_info_req(info);
634 struct obd_export *exp = req->rq_export;
635 struct md_attr *ma = &info->mti_attr;
636 struct lu_attr *la = &ma->ma_attr;
639 if (exp_connect_layout(exp))
640 /* the client can deal with 16-bit lmm_stripe_count */
643 body = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
645 if (!mdt_body_has_lov(la, body))
648 /* now we have a reply with a lov for a client not compatible with the
649 * layout lock so we have to clean the layout generation number */
650 if (S_ISREG(la->la_mode))
651 ma->ma_lmm->lmm_layout_gen = 0;
655 static int mdt_attr_get_eabuf_size(struct mdt_thread_info *info,
656 struct mdt_object *o)
658 const struct lu_env *env = info->mti_env;
661 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
670 /* Is it a directory? Let's check for the LMV as well */
671 if (S_ISDIR(lu_object_attr(&mdt_object_child(o)->mo_lu))) {
672 rc2 = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL,
676 rc2 = mo_xattr_get(env, mdt_object_child(o),
678 XATTR_NAME_DEFAULT_LMV);
680 if ((rc2 < 0 && rc2 != -ENODATA) || (rc2 > rc))
688 int mdt_big_xattr_get(struct mdt_thread_info *info, struct mdt_object *o,
691 const struct lu_env *env = info->mti_env;
695 LASSERT(info->mti_big_lmm_used == 0);
696 rc = mo_xattr_get(env, mdt_object_child(o), &LU_BUF_NULL, name);
700 /* big_lmm may need to be grown */
701 if (info->mti_big_lmmsize < rc) {
702 int size = size_roundup_power2(rc);
704 if (info->mti_big_lmmsize > 0) {
705 /* free old buffer */
706 LASSERT(info->mti_big_lmm);
707 OBD_FREE_LARGE(info->mti_big_lmm,
708 info->mti_big_lmmsize);
709 info->mti_big_lmm = NULL;
710 info->mti_big_lmmsize = 0;
713 OBD_ALLOC_LARGE(info->mti_big_lmm, size);
714 if (info->mti_big_lmm == NULL)
716 info->mti_big_lmmsize = size;
718 LASSERT(info->mti_big_lmmsize >= rc);
720 info->mti_buf.lb_buf = info->mti_big_lmm;
721 info->mti_buf.lb_len = info->mti_big_lmmsize;
722 rc = mo_xattr_get(env, mdt_object_child(o), &info->mti_buf, name);
727 int mdt_stripe_get(struct mdt_thread_info *info, struct mdt_object *o,
728 struct md_attr *ma, const char *name)
730 struct md_object *next = mdt_object_child(o);
731 struct lu_buf *buf = &info->mti_buf;
734 if (strcmp(name, XATTR_NAME_LOV) == 0) {
735 buf->lb_buf = ma->ma_lmm;
736 buf->lb_len = ma->ma_lmm_size;
737 LASSERT(!(ma->ma_valid & MA_LOV));
738 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
739 buf->lb_buf = ma->ma_lmv;
740 buf->lb_len = ma->ma_lmv_size;
741 LASSERT(!(ma->ma_valid & MA_LMV));
742 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
743 buf->lb_buf = ma->ma_lmv;
744 buf->lb_len = ma->ma_lmv_size;
745 LASSERT(!(ma->ma_valid & MA_LMV_DEF));
750 rc = mo_xattr_get(info->mti_env, next, buf, name);
754 if (strcmp(name, XATTR_NAME_LOV) == 0) {
755 if (info->mti_big_lmm_used)
756 ma->ma_lmm = info->mti_big_lmm;
758 /* NOT return LOV EA with hole to old client. */
759 if (unlikely(le32_to_cpu(ma->ma_lmm->lmm_pattern) &
760 LOV_PATTERN_F_HOLE) &&
761 !(exp_connect_flags(info->mti_exp) &
762 OBD_CONNECT_LFSCK)) {
765 ma->ma_lmm_size = rc;
766 ma->ma_valid |= MA_LOV;
768 } else if (strcmp(name, XATTR_NAME_LMV) == 0) {
769 if (info->mti_big_lmm_used)
770 ma->ma_lmv = info->mti_big_lmm;
772 ma->ma_lmv_size = rc;
773 ma->ma_valid |= MA_LMV;
774 } else if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
775 ma->ma_lmv_size = rc;
776 ma->ma_valid |= MA_LMV_DEF;
779 /* Update mdt_max_mdsize so all clients will be aware that */
780 if (info->mti_mdt->mdt_max_mdsize < rc)
781 info->mti_mdt->mdt_max_mdsize = rc;
784 } else if (rc == -ENODATA) {
787 } else if (rc == -ERANGE) {
788 /* Default LMV has fixed size, so it must be able to fit
789 * in the original buffer */
790 if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0)
792 rc = mdt_big_xattr_get(info, o, name);
794 info->mti_big_lmm_used = 1;
802 static int mdt_attr_get_pfid(struct mdt_thread_info *info,
803 struct mdt_object *o, struct lu_fid *pfid)
805 struct lu_buf *buf = &info->mti_buf;
806 struct link_ea_header *leh;
807 struct link_ea_entry *lee;
811 buf->lb_buf = info->mti_big_lmm;
812 buf->lb_len = info->mti_big_lmmsize;
813 rc = mo_xattr_get(info->mti_env, mdt_object_child(o),
814 buf, XATTR_NAME_LINK);
815 /* ignore errors, MA_PFID won't be set and it is
816 * up to the caller to treat this as an error */
817 if (rc == -ERANGE || buf->lb_len == 0) {
818 rc = mdt_big_xattr_get(info, o, XATTR_NAME_LINK);
819 buf->lb_buf = info->mti_big_lmm;
820 buf->lb_len = info->mti_big_lmmsize;
825 if (rc < sizeof(*leh)) {
826 CERROR("short LinkEA on "DFID": rc = %d\n",
827 PFID(mdt_object_fid(o)), rc);
831 leh = (struct link_ea_header *) buf->lb_buf;
832 lee = (struct link_ea_entry *)(leh + 1);
833 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
834 leh->leh_magic = LINK_EA_MAGIC;
835 leh->leh_reccount = __swab32(leh->leh_reccount);
836 leh->leh_len = __swab64(leh->leh_len);
838 if (leh->leh_magic != LINK_EA_MAGIC)
840 if (leh->leh_reccount == 0)
843 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
844 fid_be_to_cpu(pfid, pfid);
849 int mdt_attr_get_complex(struct mdt_thread_info *info,
850 struct mdt_object *o, struct md_attr *ma)
852 const struct lu_env *env = info->mti_env;
853 struct md_object *next = mdt_object_child(o);
854 struct lu_buf *buf = &info->mti_buf;
855 int need = ma->ma_need;
862 if (mdt_object_exists(o) == 0)
863 GOTO(out, rc = -ENOENT);
864 mode = lu_object_attr(&next->mo_lu);
866 if (need & MA_INODE) {
867 ma->ma_need = MA_INODE;
868 rc = mo_attr_get(env, next, ma);
871 ma->ma_valid |= MA_INODE;
874 if (need & MA_PFID) {
875 rc = mdt_attr_get_pfid(info, o, &ma->ma_pfid);
877 ma->ma_valid |= MA_PFID;
878 /* ignore this error, parent fid is not mandatory */
882 if (need & MA_LOV && (S_ISREG(mode) || S_ISDIR(mode))) {
883 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LOV);
888 if (need & MA_LMV && S_ISDIR(mode)) {
889 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_LMV);
894 if (need & MA_LMV_DEF && S_ISDIR(mode)) {
895 rc = mdt_stripe_get(info, o, ma, XATTR_NAME_DEFAULT_LMV);
900 if (need & MA_HSM && S_ISREG(mode)) {
901 buf->lb_buf = info->mti_xattr_buf;
902 buf->lb_len = sizeof(info->mti_xattr_buf);
903 CLASSERT(sizeof(struct hsm_attrs) <=
904 sizeof(info->mti_xattr_buf));
905 rc2 = mo_xattr_get(info->mti_env, next, buf, XATTR_NAME_HSM);
906 rc2 = lustre_buf2hsm(info->mti_xattr_buf, rc2, &ma->ma_hsm);
908 ma->ma_valid |= MA_HSM;
909 else if (rc2 < 0 && rc2 != -ENODATA)
913 #ifdef CONFIG_FS_POSIX_ACL
914 if (need & MA_ACL_DEF && S_ISDIR(mode)) {
915 buf->lb_buf = ma->ma_acl;
916 buf->lb_len = ma->ma_acl_size;
917 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
919 ma->ma_acl_size = rc2;
920 ma->ma_valid |= MA_ACL_DEF;
921 } else if (rc2 == -ENODATA) {
930 CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
931 rc, ma->ma_valid, ma->ma_lmm);
935 static int mdt_getattr_internal(struct mdt_thread_info *info,
936 struct mdt_object *o, int ma_need)
938 struct md_object *next = mdt_object_child(o);
939 const struct mdt_body *reqbody = info->mti_body;
940 struct ptlrpc_request *req = mdt_info_req(info);
941 struct md_attr *ma = &info->mti_attr;
942 struct lu_attr *la = &ma->ma_attr;
943 struct req_capsule *pill = info->mti_pill;
944 const struct lu_env *env = info->mti_env;
945 struct mdt_body *repbody;
946 struct lu_buf *buffer = &info->mti_buf;
947 struct obd_export *exp = info->mti_exp;
952 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_GETATTR_PACK))
953 RETURN(err_serious(-ENOMEM));
955 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
959 if (mdt_object_remote(o)) {
960 /* This object is located on remote node.*/
961 /* Return -ENOTSUPP for old client */
962 if (!mdt_is_dne_client(req->rq_export))
963 GOTO(out, rc = -ENOTSUPP);
965 repbody->mbo_fid1 = *mdt_object_fid(o);
966 repbody->mbo_valid = OBD_MD_FLID | OBD_MD_MDS;
970 if (reqbody->mbo_eadatasize > 0) {
971 buffer->lb_buf = req_capsule_server_get(pill, &RMF_MDT_MD);
972 if (buffer->lb_buf == NULL)
973 GOTO(out, rc = -EPROTO);
974 buffer->lb_len = req_capsule_get_size(pill, &RMF_MDT_MD,
977 buffer->lb_buf = NULL;
979 ma_need &= ~(MA_LOV | MA_LMV);
980 CDEBUG(D_INFO, "%s: RPC from %s: does not need LOVEA.\n",
981 mdt_obd_name(info->mti_mdt),
982 req->rq_export->exp_client_uuid.uuid);
985 /* If it is dir object and client require MEA, then we got MEA */
986 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
987 (reqbody->mbo_valid & (OBD_MD_MEA | OBD_MD_DEFAULT_MEA))) {
988 /* Assumption: MDT_MD size is enough for lmv size. */
989 ma->ma_lmv = buffer->lb_buf;
990 ma->ma_lmv_size = buffer->lb_len;
991 ma->ma_need = MA_INODE;
992 if (ma->ma_lmv_size > 0) {
993 if (reqbody->mbo_valid & OBD_MD_MEA)
994 ma->ma_need |= MA_LMV;
995 else if (reqbody->mbo_valid & OBD_MD_DEFAULT_MEA)
996 ma->ma_need |= MA_LMV_DEF;
999 ma->ma_lmm = buffer->lb_buf;
1000 ma->ma_lmm_size = buffer->lb_len;
1001 ma->ma_need = MA_INODE | MA_HSM;
1002 if (ma->ma_lmm_size > 0)
1003 ma->ma_need |= MA_LOV;
1006 if (S_ISDIR(lu_object_attr(&next->mo_lu)) &&
1007 reqbody->mbo_valid & OBD_MD_FLDIREA &&
1008 lustre_msg_get_opc(req->rq_reqmsg) == MDS_GETATTR) {
1009 /* get default stripe info for this dir. */
1010 ma->ma_need |= MA_LOV_DEF;
1012 ma->ma_need |= ma_need;
1014 rc = mdt_attr_get_complex(info, o, ma);
1016 CDEBUG(rc == -ENOENT ? D_OTHER : D_ERROR,
1017 "%s: getattr error for "DFID": rc = %d\n",
1018 mdt_obd_name(info->mti_mdt),
1019 PFID(mdt_object_fid(o)), rc);
1023 /* if file is released, check if a restore is running */
1024 if (ma->ma_valid & MA_HSM) {
1025 repbody->mbo_valid |= OBD_MD_TSTATE;
1026 if ((ma->ma_hsm.mh_flags & HS_RELEASED) &&
1027 mdt_hsm_restore_is_running(info, mdt_object_fid(o)))
1028 repbody->mbo_t_state = MS_RESTORE;
1031 is_root = lu_fid_eq(mdt_object_fid(o), &info->mti_mdt->mdt_md_root_fid);
1033 /* the Lustre protocol supposes to return default striping
1034 * on the user-visible root if explicitly requested */
1035 if ((ma->ma_valid & MA_LOV) == 0 && S_ISDIR(la->la_mode) &&
1036 (ma->ma_need & MA_LOV_DEF && is_root) && ma->ma_need & MA_LOV) {
1037 struct lu_fid rootfid;
1038 struct mdt_object *root;
1039 struct mdt_device *mdt = info->mti_mdt;
1041 rc = dt_root_get(env, mdt->mdt_bottom, &rootfid);
1044 root = mdt_object_find(env, mdt, &rootfid);
1046 RETURN(PTR_ERR(root));
1047 rc = mdt_stripe_get(info, root, ma, XATTR_NAME_LOV);
1048 mdt_object_put(info->mti_env, root);
1050 CERROR("%s: getattr error for "DFID": rc = %d\n",
1051 mdt_obd_name(info->mti_mdt),
1052 PFID(mdt_object_fid(o)), rc);
1057 if (likely(ma->ma_valid & MA_INODE))
1058 mdt_pack_attr2body(info, repbody, la, mdt_object_fid(o));
1062 if (mdt_body_has_lov(la, reqbody)) {
1063 if (ma->ma_valid & MA_LOV) {
1064 LASSERT(ma->ma_lmm_size);
1065 repbody->mbo_eadatasize = ma->ma_lmm_size;
1066 if (S_ISDIR(la->la_mode))
1067 repbody->mbo_valid |= OBD_MD_FLDIREA;
1069 repbody->mbo_valid |= OBD_MD_FLEASIZE;
1070 mdt_dump_lmm(D_INFO, ma->ma_lmm, repbody->mbo_valid);
1072 if (ma->ma_valid & MA_LMV) {
1073 /* Return -ENOTSUPP for old client */
1074 if (!mdt_is_striped_client(req->rq_export))
1077 LASSERT(S_ISDIR(la->la_mode));
1078 mdt_dump_lmv(D_INFO, ma->ma_lmv);
1079 repbody->mbo_eadatasize = ma->ma_lmv_size;
1080 repbody->mbo_valid |= (OBD_MD_FLDIREA|OBD_MD_MEA);
1082 if (ma->ma_valid & MA_LMV_DEF) {
1083 /* Return -ENOTSUPP for old client */
1084 if (!mdt_is_striped_client(req->rq_export))
1086 LASSERT(S_ISDIR(la->la_mode));
1087 repbody->mbo_eadatasize = ma->ma_lmv_size;
1088 repbody->mbo_valid |= (OBD_MD_FLDIREA |
1089 OBD_MD_DEFAULT_MEA);
1091 } else if (S_ISLNK(la->la_mode) &&
1092 reqbody->mbo_valid & OBD_MD_LINKNAME) {
1093 buffer->lb_buf = ma->ma_lmm;
1094 /* eadatasize from client includes NULL-terminator, so
1095 * there is no need to read it */
1096 buffer->lb_len = reqbody->mbo_eadatasize - 1;
1097 rc = mo_readlink(env, next, buffer);
1098 if (unlikely(rc <= 0)) {
1099 CERROR("%s: readlink failed for "DFID": rc = %d\n",
1100 mdt_obd_name(info->mti_mdt),
1101 PFID(mdt_object_fid(o)), rc);
1104 int print_limit = min_t(int, PAGE_SIZE - 128, rc);
1106 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READLINK_EPROTO))
1108 repbody->mbo_valid |= OBD_MD_LINKNAME;
1109 /* we need to report back size with NULL-terminator
1110 * because client expects that */
1111 repbody->mbo_eadatasize = rc + 1;
1112 if (repbody->mbo_eadatasize != reqbody->mbo_eadatasize)
1113 CDEBUG(D_INODE, "%s: Read shorter symlink %d "
1114 "on "DFID ", expected %d\n",
1115 mdt_obd_name(info->mti_mdt),
1116 rc, PFID(mdt_object_fid(o)),
1117 reqbody->mbo_eadatasize - 1);
1118 /* NULL terminate */
1119 ((char *)ma->ma_lmm)[rc] = 0;
1121 /* If the total CDEBUG() size is larger than a page, it
1122 * will print a warning to the console, avoid this by
1123 * printing just the last part of the symlink. */
1124 CDEBUG(D_INODE, "symlink dest %s%.*s, len = %d\n",
1125 print_limit < rc ? "..." : "", print_limit,
1126 (char *)ma->ma_lmm + rc - print_limit, rc);
1131 if (reqbody->mbo_valid & OBD_MD_FLMODEASIZE) {
1132 repbody->mbo_max_mdsize = info->mti_mdt->mdt_max_mdsize;
1133 repbody->mbo_valid |= OBD_MD_FLMODEASIZE;
1134 CDEBUG(D_INODE, "changing the max MD size to %u\n",
1135 repbody->mbo_max_mdsize);
1138 #ifdef CONFIG_FS_POSIX_ACL
1139 if ((exp_connect_flags(req->rq_export) & OBD_CONNECT_ACL) &&
1140 (reqbody->mbo_valid & OBD_MD_FLACL)) {
1141 struct lu_nodemap *nodemap = nodemap_get_from_exp(exp);
1142 if (IS_ERR(nodemap))
1143 RETURN(PTR_ERR(nodemap));
1145 rc = mdt_pack_acl2body(info, repbody, o, nodemap);
1146 nodemap_putref(nodemap);
1152 mdt_counter_incr(req, LPROC_MDT_GETATTR);
1157 static int mdt_getattr(struct tgt_session_info *tsi)
1159 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1160 struct mdt_object *obj = info->mti_object;
1161 struct req_capsule *pill = info->mti_pill;
1162 struct mdt_body *reqbody;
1163 struct mdt_body *repbody;
1167 reqbody = req_capsule_client_get(pill, &RMF_MDT_BODY);
1169 LASSERT(obj != NULL);
1170 LASSERT(lu_object_assert_exists(&obj->mot_obj));
1172 /* Unlike intent case where we need to pre-fill out buffers early on
1173 * in intent policy for ldlm reasons, here we can have a much better
1174 * guess at EA size by just reading it from disk.
1175 * Exceptions are readdir and (missing) directory striping */
1177 if (reqbody->mbo_valid & OBD_MD_LINKNAME) {
1178 /* No easy way to know how long is the symlink, but it cannot
1179 * be more than PATH_MAX, so we allocate +1 */
1182 /* A special case for fs ROOT: getattr there might fetch
1183 * default EA for entire fs, not just for this dir!
1185 } else if (lu_fid_eq(mdt_object_fid(obj),
1186 &info->mti_mdt->mdt_md_root_fid) &&
1187 (reqbody->mbo_valid & OBD_MD_FLDIREA) &&
1188 (lustre_msg_get_opc(mdt_info_req(info)->rq_reqmsg) ==
1190 /* Should the default strping be bigger, mdt_fix_reply
1191 * will reallocate */
1192 rc = DEF_REP_MD_SIZE;
1194 /* Read the actual EA size from disk */
1195 rc = mdt_attr_get_eabuf_size(info, obj);
1199 GOTO(out, rc = err_serious(rc));
1201 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, rc);
1203 rc = req_capsule_server_pack(pill);
1204 if (unlikely(rc != 0))
1205 GOTO(out, rc = err_serious(rc));
1207 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1208 LASSERT(repbody != NULL);
1209 repbody->mbo_eadatasize = 0;
1210 repbody->mbo_aclsize = 0;
1212 rc = mdt_check_ucred(info);
1214 GOTO(out_shrink, rc);
1216 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1218 rc = mdt_getattr_internal(info, obj, 0);
1221 mdt_client_compatibility(info);
1222 rc2 = mdt_fix_reply(info);
1226 mdt_thread_info_fini(info);
1231 * Exchange MOF_LOV_CREATED flags between two objects after a
1232 * layout swap. No assumption is made on whether o1 or o2 have
1233 * created objects or not.
1235 * \param[in,out] o1 First swap layout object
1236 * \param[in,out] o2 Second swap layout object
1238 static void mdt_swap_lov_flag(struct mdt_object *o1, struct mdt_object *o2)
1240 unsigned int o1_lov_created = o1->mot_lov_created;
1242 mutex_lock(&o1->mot_lov_mutex);
1243 mutex_lock(&o2->mot_lov_mutex);
1245 o1->mot_lov_created = o2->mot_lov_created;
1246 o2->mot_lov_created = o1_lov_created;
1248 mutex_unlock(&o2->mot_lov_mutex);
1249 mutex_unlock(&o1->mot_lov_mutex);
1252 static int mdt_swap_layouts(struct tgt_session_info *tsi)
1254 struct mdt_thread_info *info;
1255 struct ptlrpc_request *req = tgt_ses_req(tsi);
1256 struct obd_export *exp = req->rq_export;
1257 struct mdt_object *o1, *o2, *o;
1258 struct mdt_lock_handle *lh1, *lh2;
1259 struct mdc_swap_layouts *msl;
1263 /* client does not support layout lock, so layout swaping
1265 * FIXME: there is a problem for old clients which don't support
1266 * layout lock yet. If those clients have already opened the file
1267 * they won't be notified at all so that old layout may still be
1268 * used to do IO. This can be fixed after file release is landed by
1269 * doing exclusive open and taking full EX ibits lock. - Jinshan */
1270 if (!exp_connect_layout(exp))
1271 RETURN(-EOPNOTSUPP);
1273 info = tsi2mdt_info(tsi);
1275 if (info->mti_dlm_req != NULL)
1276 ldlm_request_cancel(req, info->mti_dlm_req, 0, LATF_SKIP);
1278 o1 = info->mti_object;
1279 o = o2 = mdt_object_find(info->mti_env, info->mti_mdt,
1280 &info->mti_body->mbo_fid2);
1282 GOTO(out, rc = PTR_ERR(o));
1284 if (mdt_object_remote(o) || !mdt_object_exists(o)) /* remote object */
1285 GOTO(put, rc = -ENOENT);
1287 rc = lu_fid_cmp(&info->mti_body->mbo_fid1, &info->mti_body->mbo_fid2);
1288 if (unlikely(rc == 0)) /* same file, you kidding me? no-op. */
1294 /* permission check. Make sure the calling process having permission
1295 * to write both files. */
1296 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o1), NULL,
1301 rc = mo_permission(info->mti_env, NULL, mdt_object_child(o2), NULL,
1306 msl = req_capsule_client_get(info->mti_pill, &RMF_SWAP_LAYOUTS);
1308 GOTO(put, rc = -EPROTO);
1310 lh1 = &info->mti_lh[MDT_LH_NEW];
1311 mdt_lock_reg_init(lh1, LCK_EX);
1312 lh2 = &info->mti_lh[MDT_LH_OLD];
1313 mdt_lock_reg_init(lh2, LCK_EX);
1315 rc = mdt_object_lock(info, o1, lh1, MDS_INODELOCK_LAYOUT |
1316 MDS_INODELOCK_XATTR);
1320 rc = mdt_object_lock(info, o2, lh2, MDS_INODELOCK_LAYOUT |
1321 MDS_INODELOCK_XATTR);
1325 rc = mo_swap_layouts(info->mti_env, mdt_object_child(o1),
1326 mdt_object_child(o2), msl->msl_flags);
1330 mdt_swap_lov_flag(o1, o2);
1333 mdt_object_unlock(info, o2, lh2, rc);
1335 mdt_object_unlock(info, o1, lh1, rc);
1337 mdt_object_put(info->mti_env, o);
1339 mdt_thread_info_fini(info);
1343 static int mdt_raw_lookup(struct mdt_thread_info *info,
1344 struct mdt_object *parent,
1345 const struct lu_name *lname,
1346 struct ldlm_reply *ldlm_rep)
1348 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1352 LASSERT(!info->mti_cross_ref);
1354 /* Only got the fid of this obj by name */
1355 fid_zero(child_fid);
1356 rc = mdo_lookup(info->mti_env, mdt_object_child(info->mti_object),
1357 lname, child_fid, &info->mti_spec);
1359 struct mdt_body *repbody;
1361 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1362 repbody->mbo_fid1 = *child_fid;
1363 repbody->mbo_valid = OBD_MD_FLID;
1364 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1365 } else if (rc == -ENOENT) {
1366 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1373 * UPDATE lock should be taken against parent, and be released before exit;
1374 * child_bits lock should be taken against child, and be returned back:
1375 * (1)normal request should release the child lock;
1376 * (2)intent request will grant the lock to client.
1378 static int mdt_getattr_name_lock(struct mdt_thread_info *info,
1379 struct mdt_lock_handle *lhc,
1381 struct ldlm_reply *ldlm_rep)
1383 struct ptlrpc_request *req = mdt_info_req(info);
1384 struct mdt_body *reqbody = NULL;
1385 struct mdt_object *parent = info->mti_object;
1386 struct mdt_object *child;
1387 struct lu_fid *child_fid = &info->mti_tmp_fid1;
1388 struct lu_name *lname = NULL;
1389 struct mdt_lock_handle *lhp = NULL;
1390 struct ldlm_lock *lock;
1397 is_resent = lustre_handle_is_used(&lhc->mlh_reg_lh);
1398 LASSERT(ergo(is_resent,
1399 lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT));
1404 if (info->mti_cross_ref) {
1405 /* Only getattr on the child. Parent is on another node. */
1406 mdt_set_disposition(info, ldlm_rep,
1407 DISP_LOOKUP_EXECD | DISP_LOOKUP_POS);
1409 CDEBUG(D_INODE, "partial getattr_name child_fid = "DFID", "
1411 PFID(mdt_object_fid(child)), ldlm_rep);
1413 rc = mdt_check_resent_lock(info, child, lhc);
1416 } else if (rc > 0) {
1417 mdt_lock_handle_init(lhc);
1418 mdt_lock_reg_init(lhc, LCK_PR);
1421 * Object's name is on another MDS, no lookup or layout
1422 * lock is needed here but update lock is.
1424 child_bits &= ~(MDS_INODELOCK_LOOKUP |
1425 MDS_INODELOCK_LAYOUT);
1426 child_bits |= MDS_INODELOCK_PERM | MDS_INODELOCK_UPDATE;
1428 rc = mdt_object_lock(info, child, lhc, child_bits);
1433 /* Finally, we can get attr for child. */
1434 if (!mdt_object_exists(child)) {
1435 LU_OBJECT_DEBUG(D_INFO, info->mti_env,
1437 "remote object doesn't exist.");
1438 mdt_object_unlock(info, child, lhc, 1);
1442 rc = mdt_getattr_internal(info, child, 0);
1443 if (unlikely(rc != 0))
1444 mdt_object_unlock(info, child, lhc, 1);
1449 lname = &info->mti_name;
1450 mdt_name_unpack(info->mti_pill, &RMF_NAME, lname, MNF_FIX_ANON);
1452 if (lu_name_is_valid(lname)) {
1453 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DNAME", "
1454 "ldlm_rep = %p\n", PFID(mdt_object_fid(parent)),
1455 PNAME(lname), ldlm_rep);
1457 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1458 if (unlikely(reqbody == NULL))
1459 RETURN(err_serious(-EPROTO));
1461 *child_fid = reqbody->mbo_fid2;
1463 if (unlikely(!fid_is_sane(child_fid)))
1464 RETURN(err_serious(-EINVAL));
1466 CDEBUG(D_INODE, "getattr with lock for "DFID"/"DFID", "
1468 PFID(mdt_object_fid(parent)),
1469 PFID(&reqbody->mbo_fid2), ldlm_rep);
1472 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_EXECD);
1474 if (unlikely(!mdt_object_exists(parent)) && lu_name_is_valid(lname)) {
1475 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1477 "Parent doesn't exist!");
1481 if (mdt_object_remote(parent)) {
1482 CERROR("%s: parent "DFID" is on remote target\n",
1483 mdt_obd_name(info->mti_mdt),
1484 PFID(mdt_object_fid(parent)));
1488 if (lu_name_is_valid(lname)) {
1489 /* Always allow to lookup ".." */
1490 if (unlikely(lname->ln_namelen == 2 &&
1491 lname->ln_name[0] == '.' &&
1492 lname->ln_name[1] == '.'))
1493 info->mti_spec.sp_permitted = 1;
1495 if (info->mti_body->mbo_valid == OBD_MD_FLID) {
1496 rc = mdt_raw_lookup(info, parent, lname, ldlm_rep);
1501 /* step 1: lock parent only if parent is a directory */
1502 if (S_ISDIR(lu_object_attr(&parent->mot_obj))) {
1503 lhp = &info->mti_lh[MDT_LH_PARENT];
1504 mdt_lock_pdo_init(lhp, LCK_PR, lname);
1505 rc = mdt_object_lock(info, parent, lhp,
1506 MDS_INODELOCK_UPDATE);
1507 if (unlikely(rc != 0))
1511 /* step 2: lookup child's fid by name */
1512 fid_zero(child_fid);
1513 rc = mdo_lookup(info->mti_env, mdt_object_child(parent), lname,
1514 child_fid, &info->mti_spec);
1516 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_NEG);
1519 GOTO(out_parent, rc);
1522 mdt_set_disposition(info, ldlm_rep, DISP_LOOKUP_POS);
1525 *step 3: find the child object by fid & lock it.
1526 * regardless if it is local or remote.
1528 *Note: LU-3240 (commit 762f2114d282a98ebfa4dbbeea9298a8088ad24e)
1529 * set parent dir fid the same as child fid in getattr by fid case
1530 * we should not lu_object_find() the object again, could lead
1531 * to hung if there is a concurrent unlink destroyed the object.
1533 if (lu_fid_eq(mdt_object_fid(parent), child_fid)) {
1534 mdt_object_get(info->mti_env, parent);
1537 child = mdt_object_find(info->mti_env, info->mti_mdt,
1541 if (unlikely(IS_ERR(child)))
1542 GOTO(out_parent, rc = PTR_ERR(child));
1544 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_RESEND, obd_timeout * 2);
1545 if (!mdt_object_exists(child)) {
1546 LU_OBJECT_DEBUG(D_INODE, info->mti_env,
1548 "Object doesn't exist!");
1549 GOTO(out_child, rc = -ENOENT);
1552 rc = mdt_check_resent_lock(info, child, lhc);
1554 GOTO(out_child, rc);
1555 } else if (rc > 0) {
1556 mdt_lock_handle_init(lhc);
1557 mdt_lock_reg_init(lhc, LCK_PR);
1560 if (!(child_bits & MDS_INODELOCK_UPDATE) &&
1561 mdt_object_exists(child) && !mdt_object_remote(child)) {
1562 struct md_attr *ma = &info->mti_attr;
1565 ma->ma_need = MA_INODE;
1566 rc = mdt_attr_get_complex(info, child, ma);
1567 if (unlikely(rc != 0))
1568 GOTO(out_child, rc);
1570 /* If the file has not been changed for some time, we
1571 * return not only a LOOKUP lock, but also an UPDATE
1572 * lock and this might save us RPC on later STAT. For
1573 * directories, it also let negative dentry cache start
1574 * working for this dir. */
1575 if (ma->ma_valid & MA_INODE &&
1576 ma->ma_attr.la_valid & LA_CTIME &&
1577 info->mti_mdt->mdt_namespace->ns_ctime_age_limit +
1578 ma->ma_attr.la_ctime < cfs_time_current_sec())
1579 child_bits |= MDS_INODELOCK_UPDATE;
1582 /* layout lock must be granted in a best-effort way
1583 * for IT operations */
1584 LASSERT(!(child_bits & MDS_INODELOCK_LAYOUT));
1585 if (!OBD_FAIL_CHECK(OBD_FAIL_MDS_NO_LL_GETATTR) &&
1586 exp_connect_layout(info->mti_exp) &&
1587 S_ISREG(lu_object_attr(&child->mot_obj)) &&
1588 !mdt_object_remote(child) && ldlm_rep != NULL) {
1589 /* try to grant layout lock for regular file. */
1595 child_bits |= MDS_INODELOCK_LAYOUT;
1596 /* try layout lock, it may fail to be granted due to
1597 * contention at LOOKUP or UPDATE */
1598 if (!mdt_object_lock_try(info, child, lhc,
1600 child_bits &= ~MDS_INODELOCK_LAYOUT;
1601 LASSERT(child_bits != 0);
1602 rc = mdt_object_lock(info, child, lhc,
1608 /* Do not enqueue the UPDATE lock from MDT(cross-MDT),
1609 * client will enqueue the lock to the remote MDT */
1610 if (mdt_object_remote(child))
1611 child_bits &= ~MDS_INODELOCK_UPDATE;
1612 rc = mdt_object_lock(info, child, lhc, child_bits);
1614 if (unlikely(rc != 0))
1615 GOTO(out_child, rc);
1618 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
1620 /* finally, we can get attr for child. */
1621 rc = mdt_getattr_internal(info, child, ma_need);
1622 if (unlikely(rc != 0)) {
1623 mdt_object_unlock(info, child, lhc, 1);
1625 /* Debugging code. */
1626 LDLM_DEBUG(lock, "Returning lock to client");
1627 LASSERTF(fid_res_name_eq(mdt_object_fid(child),
1628 &lock->l_resource->lr_name),
1629 "Lock res_id: "DLDLMRES", fid: "DFID"\n",
1630 PLDLMRES(lock->l_resource),
1631 PFID(mdt_object_fid(child)));
1634 LDLM_LOCK_PUT(lock);
1638 mdt_object_put(info->mti_env, child);
1641 mdt_object_unlock(info, parent, lhp, 1);
1645 /* normal handler: should release the child lock */
1646 static int mdt_getattr_name(struct tgt_session_info *tsi)
1648 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1649 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_CHILD];
1650 struct mdt_body *reqbody;
1651 struct mdt_body *repbody;
1655 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
1656 LASSERT(reqbody != NULL);
1657 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
1658 LASSERT(repbody != NULL);
1660 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
1661 repbody->mbo_eadatasize = 0;
1662 repbody->mbo_aclsize = 0;
1664 rc = mdt_init_ucred_intent_getattr(info, reqbody);
1666 GOTO(out_shrink, rc);
1668 rc = mdt_getattr_name_lock(info, lhc, MDS_INODELOCK_UPDATE, NULL);
1669 if (lustre_handle_is_used(&lhc->mlh_reg_lh)) {
1670 ldlm_lock_decref(&lhc->mlh_reg_lh, lhc->mlh_reg_mode);
1671 lhc->mlh_reg_lh.cookie = 0;
1673 mdt_exit_ucred(info);
1676 mdt_client_compatibility(info);
1677 rc2 = mdt_fix_reply(info);
1680 mdt_thread_info_fini(info);
1684 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1685 void *karg, void __user *uarg);
1687 static int mdt_set_info(struct tgt_session_info *tsi)
1689 struct ptlrpc_request *req = tgt_ses_req(tsi);
1692 int keylen, vallen, rc = 0;
1696 key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
1698 DEBUG_REQ(D_HA, req, "no set_info key");
1699 RETURN(err_serious(-EFAULT));
1702 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
1705 val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
1707 DEBUG_REQ(D_HA, req, "no set_info val");
1708 RETURN(err_serious(-EFAULT));
1711 vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
1714 /* Swab any part of val you need to here */
1715 if (KEY_IS(KEY_READ_ONLY)) {
1716 spin_lock(&req->rq_export->exp_lock);
1718 *exp_connect_flags_ptr(req->rq_export) |=
1721 *exp_connect_flags_ptr(req->rq_export) &=
1722 ~OBD_CONNECT_RDONLY;
1723 spin_unlock(&req->rq_export->exp_lock);
1724 } else if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
1725 struct changelog_setinfo *cs = val;
1727 if (vallen != sizeof(*cs)) {
1728 CERROR("%s: bad changelog_clear setinfo size %d\n",
1729 tgt_name(tsi->tsi_tgt), vallen);
1732 if (ptlrpc_req_need_swab(req)) {
1733 __swab64s(&cs->cs_recno);
1734 __swab32s(&cs->cs_id);
1737 rc = mdt_iocontrol(OBD_IOC_CHANGELOG_CLEAR, req->rq_export,
1739 } else if (KEY_IS(KEY_EVICT_BY_NID)) {
1741 obd_export_evict_by_nid(req->rq_export->exp_obd, val);
1748 static int mdt_readpage(struct tgt_session_info *tsi)
1750 struct mdt_thread_info *info = mdt_th_info(tsi->tsi_env);
1751 struct mdt_object *object = mdt_obj(tsi->tsi_corpus);
1752 struct lu_rdpg *rdpg = &info->mti_u.rdpg.mti_rdpg;
1753 const struct mdt_body *reqbody = tsi->tsi_mdt_body;
1754 struct mdt_body *repbody;
1760 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_READPAGE_PACK))
1761 RETURN(err_serious(-ENOMEM));
1763 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_MDT_BODY);
1764 if (repbody == NULL || reqbody == NULL)
1765 RETURN(err_serious(-EFAULT));
1768 * prepare @rdpg before calling lower layers and transfer itself. Here
1769 * reqbody->size contains offset of where to start to read and
1770 * reqbody->nlink contains number bytes to read.
1772 rdpg->rp_hash = reqbody->mbo_size;
1773 if (rdpg->rp_hash != reqbody->mbo_size) {
1774 CERROR("Invalid hash: %#llx != %#llx\n",
1775 rdpg->rp_hash, reqbody->mbo_size);
1779 rdpg->rp_attrs = reqbody->mbo_mode;
1780 if (exp_connect_flags(tsi->tsi_exp) & OBD_CONNECT_64BITHASH)
1781 rdpg->rp_attrs |= LUDA_64BITHASH;
1782 rdpg->rp_count = min_t(unsigned int, reqbody->mbo_nlink,
1783 exp_max_brw_size(tsi->tsi_exp));
1784 rdpg->rp_npages = (rdpg->rp_count + PAGE_SIZE - 1) >>
1786 OBD_ALLOC(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1787 if (rdpg->rp_pages == NULL)
1790 for (i = 0; i < rdpg->rp_npages; ++i) {
1791 rdpg->rp_pages[i] = alloc_page(GFP_NOFS);
1792 if (rdpg->rp_pages[i] == NULL)
1793 GOTO(free_rdpg, rc = -ENOMEM);
1796 /* call lower layers to fill allocated pages with directory data */
1797 rc = mo_readpage(tsi->tsi_env, mdt_object_child(object), rdpg);
1799 GOTO(free_rdpg, rc);
1801 /* send pages to client */
1802 rc = tgt_sendpage(tsi, rdpg, rc);
1807 for (i = 0; i < rdpg->rp_npages; i++)
1808 if (rdpg->rp_pages[i] != NULL)
1809 __free_page(rdpg->rp_pages[i]);
1810 OBD_FREE(rdpg->rp_pages, rdpg->rp_npages * sizeof rdpg->rp_pages[0]);
1812 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE))
1818 static int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
1820 struct lu_ucred *uc = mdt_ucred_check(info);
1821 struct lu_attr *attr = &info->mti_attr.ma_attr;
1826 if (op != REINT_SETATTR) {
1827 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
1828 attr->la_uid = uc->uc_fsuid;
1829 /* for S_ISGID, inherit gid from his parent, such work will be
1830 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
1831 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
1832 attr->la_gid = uc->uc_fsgid;
1838 static int mdt_reint_internal(struct mdt_thread_info *info,
1839 struct mdt_lock_handle *lhc,
1842 struct req_capsule *pill = info->mti_pill;
1843 struct mdt_body *repbody;
1848 rc = mdt_reint_unpack(info, op);
1850 CERROR("Can't unpack reint, rc %d\n", rc);
1851 RETURN(err_serious(rc));
1854 /* for replay (no_create) lmm is not needed, client has it already */
1855 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1856 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER,
1859 /* llog cookies are always 0, the field is kept for compatibility */
1860 if (req_capsule_has_field(pill, &RMF_LOGCOOKIES, RCL_SERVER))
1861 req_capsule_set_size(pill, &RMF_LOGCOOKIES, RCL_SERVER, 0);
1863 rc = req_capsule_server_pack(pill);
1865 CERROR("Can't pack response, rc %d\n", rc);
1866 RETURN(err_serious(rc));
1869 if (req_capsule_has_field(pill, &RMF_MDT_BODY, RCL_SERVER)) {
1870 repbody = req_capsule_server_get(pill, &RMF_MDT_BODY);
1872 repbody->mbo_eadatasize = 0;
1873 repbody->mbo_aclsize = 0;
1876 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_REINT_DELAY, 10);
1878 /* for replay no cookkie / lmm need, because client have this already */
1879 if (info->mti_spec.no_create)
1880 if (req_capsule_has_field(pill, &RMF_MDT_MD, RCL_SERVER))
1881 req_capsule_set_size(pill, &RMF_MDT_MD, RCL_SERVER, 0);
1883 rc = mdt_init_ucred_reint(info);
1885 GOTO(out_shrink, rc);
1887 rc = mdt_fix_attr_ucred(info, op);
1889 GOTO(out_ucred, rc = err_serious(rc));
1891 rc = mdt_check_resent(info, mdt_reconstruct, lhc);
1893 GOTO(out_ucred, rc);
1894 } else if (rc == 1) {
1895 DEBUG_REQ(D_INODE, mdt_info_req(info), "resent opt.");
1896 rc = lustre_msg_get_status(mdt_info_req(info)->rq_repmsg);
1897 GOTO(out_ucred, rc);
1899 rc = mdt_reint_rec(info, lhc);
1902 mdt_exit_ucred(info);
1904 mdt_client_compatibility(info);
1905 rc2 = mdt_fix_reply(info);
1911 static long mdt_reint_opcode(struct ptlrpc_request *req,
1912 const struct req_format **fmt)
1914 struct mdt_device *mdt;
1915 struct mdt_rec_reint *rec;
1918 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
1920 opc = rec->rr_opcode;
1921 DEBUG_REQ(D_INODE, req, "reint opt = %ld", opc);
1922 if (opc < REINT_MAX && fmt[opc] != NULL)
1923 req_capsule_extend(&req->rq_pill, fmt[opc]);
1925 mdt = mdt_exp2dev(req->rq_export);
1926 CERROR("%s: Unsupported opcode '%ld' from client '%s':"
1927 " rc = %d\n", req->rq_export->exp_obd->obd_name,
1928 opc, mdt->mdt_ldlm_client->cli_name, -EFAULT);
1929 opc = err_serious(-EFAULT);
1932 opc = err_serious(-EFAULT);
1937 static int mdt_reint(struct tgt_session_info *tsi)
1941 static const struct req_format *reint_fmts[REINT_MAX] = {
1942 [REINT_SETATTR] = &RQF_MDS_REINT_SETATTR,
1943 [REINT_CREATE] = &RQF_MDS_REINT_CREATE,
1944 [REINT_LINK] = &RQF_MDS_REINT_LINK,
1945 [REINT_UNLINK] = &RQF_MDS_REINT_UNLINK,
1946 [REINT_RENAME] = &RQF_MDS_REINT_RENAME,
1947 [REINT_OPEN] = &RQF_MDS_REINT_OPEN,
1948 [REINT_SETXATTR] = &RQF_MDS_REINT_SETXATTR,
1949 [REINT_RMENTRY] = &RQF_MDS_REINT_UNLINK,
1950 [REINT_MIGRATE] = &RQF_MDS_REINT_RENAME
1955 opc = mdt_reint_opcode(tgt_ses_req(tsi), reint_fmts);
1957 struct mdt_thread_info *info = tsi2mdt_info(tsi);
1959 * No lock possible here from client to pass it to reint code
1962 rc = mdt_reint_internal(info, NULL, opc);
1963 mdt_thread_info_fini(info);
1968 tsi->tsi_reply_fail_id = OBD_FAIL_MDS_REINT_NET_REP;
1972 /* this should sync the whole device */
1973 static int mdt_device_sync(const struct lu_env *env, struct mdt_device *mdt)
1975 struct dt_device *dt = mdt->mdt_bottom;
1979 rc = dt->dd_ops->dt_sync(env, dt);
1983 /* this should sync this object */
1984 static int mdt_object_sync(struct mdt_thread_info *info)
1986 struct md_object *next;
1990 if (!mdt_object_exists(info->mti_object)) {
1991 CWARN("%s: non existing object "DFID": rc = %d\n",
1992 mdt_obd_name(info->mti_mdt),
1993 PFID(mdt_object_fid(info->mti_object)), -ESTALE);
1996 next = mdt_object_child(info->mti_object);
1997 rc = mo_object_sync(info->mti_env, next);
2002 static int mdt_sync(struct tgt_session_info *tsi)
2004 struct ptlrpc_request *req = tgt_ses_req(tsi);
2005 struct req_capsule *pill = tsi->tsi_pill;
2006 struct mdt_body *body;
2011 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SYNC_PACK))
2012 RETURN(err_serious(-ENOMEM));
2014 if (fid_seq(&tsi->tsi_mdt_body->mbo_fid1) == 0) {
2015 rc = mdt_device_sync(tsi->tsi_env, mdt_exp2dev(tsi->tsi_exp));
2017 struct mdt_thread_info *info = tsi2mdt_info(tsi);
2019 /* sync an object */
2020 rc = mdt_object_sync(info);
2022 const struct lu_fid *fid;
2023 struct lu_attr *la = &info->mti_attr.ma_attr;
2025 info->mti_attr.ma_need = MA_INODE;
2026 info->mti_attr.ma_valid = 0;
2027 rc = mdt_attr_get_complex(info, info->mti_object,
2030 body = req_capsule_server_get(pill,
2032 fid = mdt_object_fid(info->mti_object);
2033 mdt_pack_attr2body(info, body, la, fid);
2036 mdt_thread_info_fini(info);
2039 mdt_counter_incr(req, LPROC_MDT_SYNC);
2045 * Handle quota control requests to consult current usage/limit, but also
2046 * to configure quota enforcement
2048 static int mdt_quotactl(struct tgt_session_info *tsi)
2050 struct obd_export *exp = tsi->tsi_exp;
2051 struct req_capsule *pill = tsi->tsi_pill;
2052 struct obd_quotactl *oqctl, *repoqc;
2054 struct mdt_device *mdt = mdt_exp2dev(exp);
2055 struct lu_device *qmt = mdt->mdt_qmt_dev;
2056 struct lu_nodemap *nodemap;
2059 oqctl = req_capsule_client_get(pill, &RMF_OBD_QUOTACTL);
2061 RETURN(err_serious(-EPROTO));
2063 rc = req_capsule_server_pack(pill);
2065 RETURN(err_serious(rc));
2067 nodemap = nodemap_get_from_exp(exp);
2068 if (IS_ERR(nodemap))
2069 RETURN(PTR_ERR(nodemap));
2071 switch (oqctl->qc_cmd) {
2072 /* master quotactl */
2075 if (!nodemap_can_setquota(nodemap))
2076 GOTO(out_nodemap, rc = -EPERM);
2080 GOTO(out_nodemap, rc = -EOPNOTSUPP);
2081 /* slave quotactl */
2086 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2087 GOTO(out_nodemap, rc = -EFAULT);
2091 if (oqctl->qc_type == USRQUOTA)
2092 id = nodemap_map_id(nodemap, NODEMAP_UID,
2093 NODEMAP_CLIENT_TO_FS, id);
2094 else if (oqctl->qc_type == GRPQUOTA)
2095 id = nodemap_map_id(nodemap, NODEMAP_UID,
2096 NODEMAP_CLIENT_TO_FS, id);
2098 repoqc = req_capsule_server_get(pill, &RMF_OBD_QUOTACTL);
2100 GOTO(out_nodemap, rc = err_serious(-EFAULT));
2102 if (oqctl->qc_id != id)
2103 swap(oqctl->qc_id, id);
2105 switch (oqctl->qc_cmd) {
2111 /* forward quotactl request to QMT */
2112 rc = qmt_hdls.qmth_quotactl(tsi->tsi_env, qmt, oqctl);
2117 /* slave quotactl */
2118 rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom,
2123 CERROR("Unsupported quotactl command: %d\n", oqctl->qc_cmd);
2124 GOTO(out_nodemap, rc = -EFAULT);
2127 if (oqctl->qc_id != id)
2128 swap(oqctl->qc_id, id);
2135 nodemap_putref(nodemap);
2140 /** clone llog ctxt from child (mdd)
2141 * This allows remote llog (replicator) access.
2142 * We can either pass all llog RPCs (eg mdt_llog_create) on to child where the
2143 * context was originally set up, or we can handle them directly.
2144 * I choose the latter, but that means I need any llog
2145 * contexts set up by child to be accessable by the mdt. So we clone the
2146 * context into our context list here.
2148 static int mdt_llog_ctxt_clone(const struct lu_env *env, struct mdt_device *mdt,
2151 struct md_device *next = mdt->mdt_child;
2152 struct llog_ctxt *ctxt;
2155 if (!llog_ctxt_null(mdt2obd_dev(mdt), idx))
2158 rc = next->md_ops->mdo_llog_ctxt_get(env, next, idx, (void **)&ctxt);
2159 if (rc || ctxt == NULL) {
2163 rc = llog_group_set_ctxt(&mdt2obd_dev(mdt)->obd_olg, ctxt, idx);
2165 CERROR("Can't set mdt ctxt %d\n", rc);
2170 static int mdt_llog_ctxt_unclone(const struct lu_env *env,
2171 struct mdt_device *mdt, int idx)
2173 struct llog_ctxt *ctxt;
2175 ctxt = llog_get_context(mdt2obd_dev(mdt), idx);
2178 /* Put once for the get we just did, and once for the clone */
2179 llog_ctxt_put(ctxt);
2180 llog_ctxt_put(ctxt);
2185 * sec context handlers
2187 static int mdt_sec_ctx_handle(struct tgt_session_info *tsi)
2189 CFS_FAIL_TIMEOUT(OBD_FAIL_SEC_CTX_HDL_PAUSE, cfs_fail_val);
2195 * quota request handlers
2197 static int mdt_quota_dqacq(struct tgt_session_info *tsi)
2199 struct mdt_device *mdt = mdt_exp2dev(tsi->tsi_exp);
2200 struct lu_device *qmt = mdt->mdt_qmt_dev;
2205 RETURN(err_serious(-EOPNOTSUPP));
2207 rc = qmt_hdls.qmth_dqacq(tsi->tsi_env, qmt, tgt_ses_req(tsi));
2211 struct mdt_object *mdt_object_new(const struct lu_env *env,
2212 struct mdt_device *d,
2213 const struct lu_fid *f)
2215 struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
2216 struct lu_object *o;
2217 struct mdt_object *m;
2220 CDEBUG(D_INFO, "Allocate object for "DFID"\n", PFID(f));
2221 o = lu_object_find(env, &d->mdt_lu_dev, f, &conf);
2222 if (unlikely(IS_ERR(o)))
2223 m = (struct mdt_object *)o;
2229 struct mdt_object *mdt_object_find(const struct lu_env *env,
2230 struct mdt_device *d,
2231 const struct lu_fid *f)
2233 struct lu_object *o;
2234 struct mdt_object *m;
2237 CDEBUG(D_INFO, "Find object for "DFID"\n", PFID(f));
2238 o = lu_object_find(env, &d->mdt_lu_dev, f, NULL);
2239 if (unlikely(IS_ERR(o)))
2240 m = (struct mdt_object *)o;
2248 * Asyncronous commit for mdt device.
2250 * Pass asynchonous commit call down the MDS stack.
2252 * \param env environment
2253 * \param mdt the mdt device
2255 static void mdt_device_commit_async(const struct lu_env *env,
2256 struct mdt_device *mdt)
2258 struct dt_device *dt = mdt->mdt_bottom;
2262 rc = dt->dd_ops->dt_commit_async(env, dt);
2263 if (unlikely(rc != 0))
2264 CWARN("%s: async commit start failed: rc = %d\n",
2265 mdt_obd_name(mdt), rc);
2266 atomic_inc(&mdt->mdt_async_commit_count);
2271 * Mark the lock as "synchonous".
2273 * Mark the lock to deffer transaction commit to the unlock time.
2275 * \param lock the lock to mark as "synchonous"
2277 * \see mdt_is_lock_sync
2278 * \see mdt_save_lock
2280 static inline void mdt_set_lock_sync(struct ldlm_lock *lock)
2282 lock->l_ast_data = (void*)1;
2286 * Check whehter the lock "synchonous" or not.
2288 * \param lock the lock to check
2289 * \retval 1 the lock is "synchonous"
2290 * \retval 0 the lock isn't "synchronous"
2292 * \see mdt_set_lock_sync
2293 * \see mdt_save_lock
2295 static inline int mdt_is_lock_sync(struct ldlm_lock *lock)
2297 return lock->l_ast_data != NULL;
2301 * Blocking AST for mdt locks.
2303 * Starts transaction commit if in case of COS lock conflict or
2304 * deffers such a commit to the mdt_save_lock.
2306 * \param lock the lock which blocks a request or cancelling lock
2307 * \param desc unused
2308 * \param data unused
2309 * \param flag indicates whether this cancelling or blocking callback
2311 * \see ldlm_blocking_ast_nocheck
2313 int mdt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2314 void *data, int flag)
2316 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2317 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2321 if (flag == LDLM_CB_CANCELING)
2324 lock_res_and_lock(lock);
2325 if (lock->l_blocking_ast != mdt_blocking_ast) {
2326 unlock_res_and_lock(lock);
2329 if (lock->l_req_mode & (LCK_PW | LCK_EX) &&
2330 lock->l_blocking_lock != NULL) {
2331 if (mdt_cos_is_enabled(mdt) &&
2332 lock->l_client_cookie !=
2333 lock->l_blocking_lock->l_client_cookie)
2334 mdt_set_lock_sync(lock);
2335 else if (mdt_slc_is_enabled(mdt) &&
2336 ldlm_is_cos_incompat(lock->l_blocking_lock))
2337 mdt_set_lock_sync(lock);
2339 rc = ldlm_blocking_ast_nocheck(lock);
2341 /* There is no lock conflict if l_blocking_lock == NULL,
2342 * it indicates a blocking ast sent from ldlm_lock_decref_internal
2343 * when the last reference to a local lock was released */
2344 if (lock->l_req_mode == LCK_COS && lock->l_blocking_lock != NULL) {
2347 rc = lu_env_init(&env, LCT_LOCAL);
2348 if (unlikely(rc != 0))
2349 CWARN("%s: lu_env initialization failed, cannot "
2350 "start asynchronous commit: rc = %d\n",
2353 mdt_device_commit_async(&env, mdt);
2360 * Blocking AST for cross-MDT lock
2362 * Discard lock from uncommitted_slc_locks and cancel it.
2364 * \param lock the lock which blocks a request or cancelling lock
2365 * \param desc unused
2366 * \param data unused
2367 * \param flag indicates whether this cancelling or blocking callback
2368 * \retval 0 on success
2369 * \retval negative number on error
2371 int mdt_remote_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2372 void *data, int flag)
2378 case LDLM_CB_BLOCKING: {
2379 struct lustre_handle lockh;
2381 ldlm_lock2handle(lock, &lockh);
2382 rc = ldlm_cli_cancel(&lockh,
2383 ldlm_is_atomic_cb(lock) ? 0 : LCF_ASYNC);
2385 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2390 case LDLM_CB_CANCELING: {
2391 struct obd_device *obd = ldlm_lock_to_ns(lock)->ns_obd;
2392 struct mdt_device *mdt =
2393 mdt_dev(obd->obd_lu_dev->ld_site->ls_top_dev);
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(&mdt->mdt_lut, 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 CWARN("%s: lu_env initialization failed, object"
2409 "%p "DFID" is leaked!\n",
2411 PFID(mdt_object_fid(mo)));
2415 if (lock->l_policy_data.l_inodebits.bits &
2416 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)) {
2417 rc = mo_invalidate(&env, mdt_object_child(mo));
2418 mo->mot_cache_attr = 0;
2420 mdt_object_put(&env, mo);
2432 int mdt_check_resent_lock(struct mdt_thread_info *info,
2433 struct mdt_object *mo,
2434 struct mdt_lock_handle *lhc)
2436 /* the lock might already be gotten in ldlm_handle_enqueue() */
2437 if (unlikely(lustre_handle_is_used(&lhc->mlh_reg_lh))) {
2438 struct ptlrpc_request *req = mdt_info_req(info);
2439 struct ldlm_lock *lock;
2441 lock = ldlm_handle2lock(&lhc->mlh_reg_lh);
2442 LASSERT(lustre_msg_get_flags(req->rq_reqmsg) & MSG_RESENT);
2444 /* Lock is pinned by ldlm_handle_enqueue0() as it is
2445 * a resend case, however, it could be already destroyed
2446 * due to client eviction or a raced cancel RPC. */
2447 LDLM_DEBUG_NOLOCK("Invalid lock handle %#llx",
2448 lhc->mlh_reg_lh.cookie);
2452 if (!fid_res_name_eq(mdt_object_fid(mo),
2453 &lock->l_resource->lr_name)) {
2454 CWARN("%s: Although resent, but still not "
2455 "get child lock:"DFID"\n",
2456 info->mti_exp->exp_obd->obd_name,
2457 PFID(mdt_object_fid(mo)));
2458 LDLM_LOCK_PUT(lock);
2461 LDLM_LOCK_PUT(lock);
2467 int mdt_remote_object_lock(struct mdt_thread_info *mti, struct mdt_object *o,
2468 const struct lu_fid *fid, struct lustre_handle *lh,
2469 enum ldlm_mode mode, __u64 ibits, bool nonblock,
2472 struct ldlm_enqueue_info *einfo = &mti->mti_einfo;
2473 union ldlm_policy_data *policy = &mti->mti_policy;
2474 struct ldlm_res_id *res_id = &mti->mti_res_id;
2478 LASSERT(mdt_object_remote(o));
2480 fid_build_reg_res_name(fid, res_id);
2482 memset(einfo, 0, sizeof(*einfo));
2483 einfo->ei_type = LDLM_IBITS;
2484 einfo->ei_mode = mode;
2485 einfo->ei_cb_bl = mdt_remote_blocking_ast;
2486 einfo->ei_cb_cp = ldlm_completion_ast;
2487 einfo->ei_enq_slave = 0;
2488 einfo->ei_res_id = res_id;
2490 einfo->ei_nonblock = 1;
2493 * if we cache lock, couple lock with mdt_object, so that object
2494 * can be easily found in lock ASTs.
2496 mdt_object_get(mti->mti_env, o);
2497 einfo->ei_cbdata = o;
2500 memset(policy, 0, sizeof(*policy));
2501 policy->l_inodebits.bits = ibits;
2503 rc = mo_object_lock(mti->mti_env, mdt_object_child(o), lh, einfo,
2505 if (rc < 0 && cache) {
2506 mdt_object_put(mti->mti_env, o);
2507 einfo->ei_cbdata = NULL;
2512 static int mdt_object_local_lock(struct mdt_thread_info *info,
2513 struct mdt_object *o,
2514 struct mdt_lock_handle *lh, __u64 ibits,
2515 bool nonblock, bool cos_incompat)
2517 struct ldlm_namespace *ns = info->mti_mdt->mdt_namespace;
2518 union ldlm_policy_data *policy = &info->mti_policy;
2519 struct ldlm_res_id *res_id = &info->mti_res_id;
2524 LASSERT(!lustre_handle_is_used(&lh->mlh_reg_lh));
2525 LASSERT(!lustre_handle_is_used(&lh->mlh_pdo_lh));
2526 LASSERT(lh->mlh_reg_mode != LCK_MINMODE);
2527 LASSERT(lh->mlh_type != MDT_NUL_LOCK);
2530 LASSERT(lh->mlh_reg_mode == LCK_PW ||
2531 lh->mlh_reg_mode == LCK_EX);
2532 dlmflags |= LDLM_FL_COS_INCOMPAT;
2533 } else if (mdt_cos_is_enabled(info->mti_mdt)) {
2534 dlmflags |= LDLM_FL_COS_ENABLED;
2537 /* Only enqueue LOOKUP lock for remote object */
2538 if (mdt_object_remote(o))
2539 LASSERT(ibits == MDS_INODELOCK_LOOKUP);
2541 if (lh->mlh_type == MDT_PDO_LOCK) {
2542 /* check for exists after object is locked */
2543 if (mdt_object_exists(o) == 0) {
2544 /* Non-existent object shouldn't have PDO lock */
2547 /* Non-dir object shouldn't have PDO lock */
2548 if (!S_ISDIR(lu_object_attr(&o->mot_obj)))
2553 memset(policy, 0, sizeof(*policy));
2554 fid_build_reg_res_name(mdt_object_fid(o), res_id);
2556 dlmflags |= LDLM_FL_ATOMIC_CB;
2558 dlmflags |= LDLM_FL_BLOCK_NOWAIT;
2561 * Take PDO lock on whole directory and build correct @res_id for lock
2562 * on part of directory.
2564 if (lh->mlh_pdo_hash != 0) {
2565 LASSERT(lh->mlh_type == MDT_PDO_LOCK);
2566 mdt_lock_pdo_mode(info, o, lh);
2567 if (lh->mlh_pdo_mode != LCK_NL) {
2569 * Do not use LDLM_FL_LOCAL_ONLY for parallel lock, it
2570 * is never going to be sent to client and we do not
2571 * want it slowed down due to possible cancels.
2573 policy->l_inodebits.bits = MDS_INODELOCK_UPDATE;
2574 rc = mdt_fid_lock(ns, &lh->mlh_pdo_lh, lh->mlh_pdo_mode,
2575 policy, res_id, dlmflags,
2576 info->mti_exp == NULL ? NULL :
2577 &info->mti_exp->exp_handle.h_cookie);
2578 if (unlikely(rc != 0))
2579 GOTO(out_unlock, rc);
2583 * Finish res_id initializing by name hash marking part of
2584 * directory which is taking modification.
2586 res_id->name[LUSTRE_RES_ID_HSH_OFF] = lh->mlh_pdo_hash;
2589 policy->l_inodebits.bits = ibits;
2592 * Use LDLM_FL_LOCAL_ONLY for this lock. We do not know yet if it is
2593 * going to be sent to client. If it is - mdt_intent_policy() path will
2594 * fix it up and turn FL_LOCAL flag off.
2596 rc = mdt_fid_lock(ns, &lh->mlh_reg_lh, lh->mlh_reg_mode, policy,
2597 res_id, LDLM_FL_LOCAL_ONLY | dlmflags,
2598 info->mti_exp == NULL ? NULL :
2599 &info->mti_exp->exp_handle.h_cookie);
2602 mdt_object_unlock(info, o, lh, 1);
2603 else if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_MDS_PDO_LOCK)) &&
2604 lh->mlh_pdo_hash != 0 &&
2605 (lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX))
2606 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_PDO_LOCK, 15);
2612 mdt_object_lock_internal(struct mdt_thread_info *info, struct mdt_object *o,
2613 struct mdt_lock_handle *lh, __u64 ibits, bool nonblock,
2616 struct mdt_lock_handle *local_lh = NULL;
2620 if (!mdt_object_remote(o)) {
2621 rc = mdt_object_local_lock(info, o, lh, ibits, nonblock,
2626 /* XXX do not support PERM/LAYOUT/XATTR lock for remote object yet */
2627 ibits &= ~(MDS_INODELOCK_PERM | MDS_INODELOCK_LAYOUT |
2628 MDS_INODELOCK_XATTR);
2630 /* Only enqueue LOOKUP lock for remote object */
2631 if (ibits & MDS_INODELOCK_LOOKUP) {
2632 rc = mdt_object_local_lock(info, o, lh, MDS_INODELOCK_LOOKUP,
2633 nonblock, cos_incompat);
2640 if (ibits & MDS_INODELOCK_UPDATE) {
2641 /* Sigh, PDO needs to enqueue 2 locks right now, but
2642 * enqueue RPC can only request 1 lock, to avoid extra
2643 * RPC, so it will instead enqueue EX lock for remote
2644 * object anyway XXX*/
2645 if (lh->mlh_type == MDT_PDO_LOCK &&
2646 lh->mlh_pdo_hash != 0) {
2647 CDEBUG(D_INFO, "%s: "DFID" convert PDO lock to"
2648 "EX lock.\n", mdt_obd_name(info->mti_mdt),
2649 PFID(mdt_object_fid(o)));
2650 lh->mlh_pdo_hash = 0;
2651 lh->mlh_rreg_mode = LCK_EX;
2652 lh->mlh_type = MDT_REG_LOCK;
2654 rc = mdt_remote_object_lock(info, o, mdt_object_fid(o),
2657 MDS_INODELOCK_UPDATE, nonblock,
2659 if (rc != ELDLM_OK) {
2660 if (local_lh != NULL)
2661 mdt_object_unlock(info, o, local_lh, rc);
2669 int mdt_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2670 struct mdt_lock_handle *lh, __u64 ibits)
2672 return mdt_object_lock_internal(info, o, lh, ibits, false, false);
2675 int mdt_reint_object_lock(struct mdt_thread_info *info, struct mdt_object *o,
2676 struct mdt_lock_handle *lh, __u64 ibits,
2679 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2680 return mdt_object_lock_internal(info, o, lh, ibits, false,
2684 int mdt_object_lock_try(struct mdt_thread_info *info, struct mdt_object *o,
2685 struct mdt_lock_handle *lh, __u64 ibits)
2687 struct mdt_lock_handle tmp = *lh;
2690 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, false);
2697 int mdt_reint_object_lock_try(struct mdt_thread_info *info,
2698 struct mdt_object *o, struct mdt_lock_handle *lh,
2699 __u64 ibits, bool cos_incompat)
2701 struct mdt_lock_handle tmp = *lh;
2704 LASSERT(lh->mlh_reg_mode == LCK_PW || lh->mlh_reg_mode == LCK_EX);
2705 rc = mdt_object_lock_internal(info, o, &tmp, ibits, true, cos_incompat);
2713 * Save a lock within request object.
2715 * Keep the lock referenced until whether client ACK or transaction
2716 * commit happens or release the lock immediately depending on input
2717 * parameters. If COS is ON, a write lock is converted to COS lock
2720 * \param info thead info object
2721 * \param h lock handle
2722 * \param mode lock mode
2723 * \param decref force immediate lock releasing
2725 static void mdt_save_lock(struct mdt_thread_info *info, struct lustre_handle *h,
2726 enum ldlm_mode mode, int decref)
2730 if (lustre_handle_is_used(h)) {
2731 if (decref || !info->mti_has_trans ||
2732 !(mode & (LCK_PW | LCK_EX))) {
2733 mdt_fid_unlock(h, mode);
2735 struct mdt_device *mdt = info->mti_mdt;
2736 struct ldlm_lock *lock = ldlm_handle2lock(h);
2737 struct ptlrpc_request *req = mdt_info_req(info);
2740 cos = (mdt_cos_is_enabled(mdt) ||
2741 mdt_slc_is_enabled(mdt));
2743 LASSERTF(lock != NULL, "no lock for cookie %#llx\n",
2746 /* there is no request if mdt_object_unlock() is called
2747 * from mdt_export_cleanup()->mdt_add_dirty_flag() */
2748 if (likely(req != NULL)) {
2749 CDEBUG(D_HA, "request = %p reply state = %p"
2750 " transno = %lld\n", req,
2751 req->rq_reply_state, req->rq_transno);
2753 ldlm_lock_downgrade(lock, LCK_COS);
2756 ptlrpc_save_lock(req, h, mode, cos);
2758 mdt_fid_unlock(h, mode);
2760 if (mdt_is_lock_sync(lock)) {
2761 CDEBUG(D_HA, "found sync-lock,"
2762 " async commit started\n");
2763 mdt_device_commit_async(info->mti_env,
2766 LDLM_LOCK_PUT(lock);
2775 * Save cross-MDT lock in uncommitted_slc_locks
2777 * Keep the lock referenced until transaction commit happens or release the lock
2778 * immediately depending on input parameters.
2780 * \param info thead info object
2781 * \param h lock handle
2782 * \param mode lock mode
2783 * \param decref force immediate lock releasing
2785 static void mdt_save_remote_lock(struct mdt_thread_info *info,
2786 struct mdt_object *o, struct lustre_handle *h,
2787 enum ldlm_mode mode, int decref)
2791 if (lustre_handle_is_used(h)) {
2792 struct ldlm_lock *lock = ldlm_handle2lock(h);
2795 (lock->l_policy_data.l_inodebits.bits &
2796 (MDS_INODELOCK_XATTR | MDS_INODELOCK_UPDATE)))
2797 mo_invalidate(info->mti_env, mdt_object_child(o));
2799 if (decref || !info->mti_has_trans ||
2800 !(mode & (LCK_PW | LCK_EX))) {
2801 ldlm_lock_decref_and_cancel(h, mode);
2802 LDLM_LOCK_PUT(lock);
2804 struct ptlrpc_request *req = mdt_info_req(info);
2806 LASSERT(req != NULL);
2807 tgt_save_slc_lock(&info->mti_mdt->mdt_lut, lock,
2809 ldlm_lock_decref(h, mode);
2818 * Unlock mdt object.
2820 * Immeditely release the regular lock and the PDO lock or save the
2821 * lock in request and keep them referenced until client ACK or
2822 * transaction commit.
2824 * \param info thread info object
2825 * \param o mdt object
2826 * \param lh mdt lock handle referencing regular and PDO locks
2827 * \param decref force immediate lock releasing
2829 * XXX o is not used and may be NULL, see hsm_cdt_request_completed().
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 %#llx\n",
3189 lh->mlh_reg_lh.cookie);
3190 lh->mlh_reg_lh.cookie = 0;
3194 LASSERTF(new_lock != NULL,
3195 "lockh %#llx flags %#llx : 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 %#llx",
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 %#llx",
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;
3316 * Initialize lhc->mlh_reg_lh either from a previously granted lock
3317 * (for the resend case) or a new lock. Below we will use it to
3318 * replace the original lock.
3320 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3321 if (!lustre_handle_is_used(&lhc->mlh_reg_lh)) {
3322 mdt_lock_reg_init(lhc, (*lockp)->l_req_mode);
3323 rc = mdt_object_lock(info, info->mti_object, lhc,
3324 MDS_INODELOCK_XATTR);
3329 rc = mdt_getxattr(info);
3331 if (mdt_info_req(info)->rq_repmsg != NULL)
3332 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3334 if (ldlm_rep == NULL ||
3335 OBD_FAIL_CHECK(OBD_FAIL_MDS_XATTR_REP)) {
3336 mdt_object_unlock(info, info->mti_object, lhc, 1);
3337 RETURN(err_serious(-EFAULT));
3340 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3342 /* This is left for interop instead of adding a new interop flag.
3344 #if LUSTRE_VERSION_CODE > OBD_OCD_VERSION(3, 0, 0, 0)
3345 if (ldlm_rep->lock_policy_res2) {
3346 mdt_object_unlock(info, info->mti_object, lhc, 1);
3347 RETURN(ELDLM_LOCK_ABORTED);
3351 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3355 static int mdt_intent_getattr(enum mdt_it_code opcode,
3356 struct mdt_thread_info *info,
3357 struct ldlm_lock **lockp,
3360 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3362 struct ldlm_reply *ldlm_rep;
3363 struct mdt_body *reqbody;
3364 struct mdt_body *repbody;
3368 reqbody = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
3371 repbody = req_capsule_server_get(info->mti_pill, &RMF_MDT_BODY);
3374 info->mti_cross_ref = !!(reqbody->mbo_valid & OBD_MD_FLCROSSREF);
3375 repbody->mbo_eadatasize = 0;
3376 repbody->mbo_aclsize = 0;
3380 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM;
3382 case MDT_IT_GETATTR:
3383 child_bits = MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
3387 CERROR("Unsupported intent (%d)\n", opcode);
3388 GOTO(out_shrink, rc = -EINVAL);
3391 rc = mdt_init_ucred_intent_getattr(info, reqbody);
3393 GOTO(out_shrink, rc);
3395 ldlm_rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3396 mdt_set_disposition(info, ldlm_rep, DISP_IT_EXECD);
3398 /* Get lock from request for possible resent case. */
3399 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3401 rc = mdt_getattr_name_lock(info, lhc, child_bits, ldlm_rep);
3402 ldlm_rep->lock_policy_res2 = clear_serious(rc);
3404 if (mdt_get_disposition(ldlm_rep, DISP_LOOKUP_NEG))
3405 ldlm_rep->lock_policy_res2 = 0;
3406 if (!mdt_get_disposition(ldlm_rep, DISP_LOOKUP_POS) ||
3407 ldlm_rep->lock_policy_res2) {
3408 lhc->mlh_reg_lh.cookie = 0ull;
3409 GOTO(out_ucred, rc = ELDLM_LOCK_ABORTED);
3412 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3415 mdt_exit_ucred(info);
3417 mdt_client_compatibility(info);
3418 rc2 = mdt_fix_reply(info);
3424 static int mdt_intent_layout(enum mdt_it_code opcode,
3425 struct mdt_thread_info *info,
3426 struct ldlm_lock **lockp,
3429 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_LAYOUT];
3430 struct layout_intent *layout;
3432 struct mdt_object *obj = NULL;
3433 int layout_size = 0;
3437 if (opcode != MDT_IT_LAYOUT) {
3438 CERROR("%s: Unknown intent (%d)\n", mdt_obd_name(info->mti_mdt),
3443 layout = req_capsule_client_get(info->mti_pill, &RMF_LAYOUT_INTENT);
3447 if (layout->li_opc != LAYOUT_INTENT_ACCESS) {
3448 CERROR("%s: Unsupported layout intent opc %d\n",
3449 mdt_obd_name(info->mti_mdt), layout->li_opc);
3453 fid = &info->mti_tmp_fid2;
3454 fid_extract_from_res_name(fid, &(*lockp)->l_resource->lr_name);
3456 /* Get lock from request for possible resent case. */
3457 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3459 obj = mdt_object_find(info->mti_env, info->mti_mdt, fid);
3461 GOTO(out, rc = PTR_ERR(obj));
3463 if (mdt_object_exists(obj) && !mdt_object_remote(obj)) {
3464 layout_size = mdt_attr_get_eabuf_size(info, obj);
3465 if (layout_size < 0)
3466 GOTO(out_obj, rc = layout_size);
3468 if (layout_size > info->mti_mdt->mdt_max_mdsize)
3469 info->mti_mdt->mdt_max_mdsize = layout_size;
3472 (*lockp)->l_lvb_type = LVB_T_LAYOUT;
3473 req_capsule_set_size(info->mti_pill, &RMF_DLM_LVB, RCL_SERVER,
3475 rc = req_capsule_server_pack(info->mti_pill);
3479 mdt_object_put(info->mti_env, obj);
3481 if (rc == 0 && lustre_handle_is_used(&lhc->mlh_reg_lh))
3482 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3485 lhc->mlh_reg_lh.cookie = 0;
3490 static int mdt_intent_reint(enum mdt_it_code opcode,
3491 struct mdt_thread_info *info,
3492 struct ldlm_lock **lockp,
3495 struct mdt_lock_handle *lhc = &info->mti_lh[MDT_LH_RMT];
3496 struct ldlm_reply *rep = NULL;
3500 static const struct req_format *intent_fmts[REINT_MAX] = {
3501 [REINT_CREATE] = &RQF_LDLM_INTENT_CREATE,
3502 [REINT_OPEN] = &RQF_LDLM_INTENT_OPEN
3507 opc = mdt_reint_opcode(mdt_info_req(info), intent_fmts);
3511 if (mdt_it_flavor[opcode].it_reint != opc) {
3512 CERROR("Reint code %ld doesn't match intent: %d\n",
3514 RETURN(err_serious(-EPROTO));
3517 /* Get lock from request for possible resent case. */
3518 mdt_intent_fixup_resent(info, *lockp, lhc, flags);
3520 rc = mdt_reint_internal(info, lhc, opc);
3522 /* Check whether the reply has been packed successfully. */
3523 if (mdt_info_req(info)->rq_repmsg != NULL)
3524 rep = req_capsule_server_get(info->mti_pill, &RMF_DLM_REP);
3526 RETURN(err_serious(-EFAULT));
3528 /* MDC expects this in any case */
3530 mdt_set_disposition(info, rep, DISP_LOOKUP_EXECD);
3532 /* the open lock or the lock for cross-ref object should be
3533 * returned to the client */
3534 if (lustre_handle_is_used(&lhc->mlh_reg_lh) &&
3535 (rc == 0 || rc == -MDT_EREMOTE_OPEN)) {
3536 rep->lock_policy_res2 = 0;
3537 rc = mdt_intent_lock_replace(info, lockp, lhc, flags, rc);
3541 rep->lock_policy_res2 = clear_serious(rc);
3543 if (rep->lock_policy_res2 == -ENOENT &&
3544 mdt_get_disposition(rep, DISP_LOOKUP_NEG) &&
3545 !mdt_get_disposition(rep, DISP_OPEN_CREATE))
3546 rep->lock_policy_res2 = 0;
3548 lhc->mlh_reg_lh.cookie = 0ull;
3549 if (rc == -ENOTCONN || rc == -ENODEV ||
3550 rc == -EOVERFLOW) { /**< if VBR failure then return error */
3552 * If it is the disconnect error (ENODEV & ENOCONN), the error
3553 * will be returned by rq_status, and client at ptlrpc layer
3554 * will detect this, then disconnect, reconnect the import
3555 * immediately, instead of impacting the following the rpc.
3560 * For other cases, the error will be returned by intent, and client
3561 * will retrieve the result from intent.
3563 RETURN(ELDLM_LOCK_ABORTED);
3566 static int mdt_intent_code(enum ldlm_intent_flags itcode)
3574 case IT_OPEN|IT_CREAT:
3581 rc = MDT_IT_READDIR;
3584 rc = MDT_IT_GETATTR;
3596 rc = MDT_IT_GETXATTR;
3601 case IT_QUOTA_DQACQ:
3606 CERROR("Unknown intent opcode: 0x%08x\n", itcode);
3613 static int mdt_intent_opc(enum ldlm_intent_flags itopc,
3614 struct mdt_thread_info *info,
3615 struct ldlm_lock **lockp, __u64 flags)
3617 struct req_capsule *pill = info->mti_pill;
3618 struct ptlrpc_request *req = mdt_info_req(info);
3619 struct mdt_it_flavor *flv;
3624 opc = mdt_intent_code(itopc);
3628 if (opc == MDT_IT_QUOTA) {
3629 struct lu_device *qmt = info->mti_mdt->mdt_qmt_dev;
3632 RETURN(-EOPNOTSUPP);
3634 (*lockp)->l_lvb_type = LVB_T_LQUOTA;
3635 /* pass the request to quota master */
3636 rc = qmt_hdls.qmth_intent_policy(info->mti_env, qmt,
3637 mdt_info_req(info), lockp,
3642 flv = &mdt_it_flavor[opc];
3643 if (flv->it_fmt != NULL)
3644 req_capsule_extend(pill, flv->it_fmt);
3646 rc = mdt_unpack_req_pack_rep(info, flv->it_flags);
3650 if (flv->it_flags & MUTABOR &&
3651 exp_connect_flags(req->rq_export) & OBD_CONNECT_RDONLY)
3654 if (flv->it_act != NULL) {
3655 struct ldlm_reply *rep;
3657 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_INTENT_DELAY, 10);
3659 /* execute policy */
3660 rc = flv->it_act(opc, info, lockp, flags);
3662 /* Check whether the reply has been packed successfully. */
3663 if (req->rq_repmsg != NULL) {
3664 rep = req_capsule_server_get(info->mti_pill,
3666 rep->lock_policy_res2 =
3667 ptlrpc_status_hton(rep->lock_policy_res2);
3674 static int mdt_intent_policy(struct ldlm_namespace *ns,
3675 struct ldlm_lock **lockp, void *req_cookie,
3676 enum ldlm_mode mode, __u64 flags, void *data)
3678 struct tgt_session_info *tsi;
3679 struct mdt_thread_info *info;
3680 struct ptlrpc_request *req = req_cookie;
3681 struct ldlm_intent *it;
3682 struct req_capsule *pill;
3687 LASSERT(req != NULL);
3689 tsi = tgt_ses_info(req->rq_svc_thread->t_env);
3691 info = tsi2mdt_info(tsi);
3692 LASSERT(info != NULL);
3693 pill = info->mti_pill;
3694 LASSERT(pill->rc_req == req);
3696 if (req->rq_reqmsg->lm_bufcount > DLM_INTENT_IT_OFF) {
3697 req_capsule_extend(pill, &RQF_LDLM_INTENT_BASIC);
3698 it = req_capsule_client_get(pill, &RMF_LDLM_INTENT);
3700 rc = mdt_intent_opc(it->opc, info, lockp, flags);
3704 /* Lock without inodebits makes no sense and will oops
3705 * later in ldlm. Let's check it now to see if we have
3706 * ibits corrupted somewhere in mdt_intent_opc().
3707 * The case for client miss to set ibits has been
3708 * processed by others. */
3709 LASSERT(ergo(info->mti_dlm_req->lock_desc.l_resource.\
3710 lr_type == LDLM_IBITS,
3711 info->mti_dlm_req->lock_desc.\
3712 l_policy_data.l_inodebits.bits != 0));
3714 rc = err_serious(-EFAULT);
3716 /* No intent was provided */
3717 LASSERT(pill->rc_fmt == &RQF_LDLM_ENQUEUE);
3718 req_capsule_set_size(pill, &RMF_DLM_LVB, RCL_SERVER, 0);
3719 rc = req_capsule_server_pack(pill);
3721 rc = err_serious(rc);
3723 mdt_thread_info_fini(info);
3727 static void mdt_deregister_seq_exp(struct mdt_device *mdt)
3729 struct seq_server_site *ss = mdt_seq_site(mdt);
3731 if (ss->ss_node_id == 0)
3734 if (ss->ss_client_seq != NULL) {
3735 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3736 ss->ss_client_seq->lcs_exp = NULL;
3739 if (ss->ss_server_fld != NULL) {
3740 lustre_deregister_lwp_item(&ss->ss_server_fld->lsf_control_exp);
3741 ss->ss_server_fld->lsf_control_exp = NULL;
3745 static void mdt_seq_fini_cli(struct mdt_device *mdt)
3747 struct seq_server_site *ss = mdt_seq_site(mdt);
3752 if (ss->ss_server_seq != NULL)
3753 seq_server_set_cli(NULL, ss->ss_server_seq, NULL);
3756 static int mdt_seq_fini(const struct lu_env *env, struct mdt_device *mdt)
3758 mdt_seq_fini_cli(mdt);
3759 mdt_deregister_seq_exp(mdt);
3761 return seq_site_fini(env, mdt_seq_site(mdt));
3765 * It will retrieve its FLDB entries from MDT0, and it only happens
3766 * when upgrading existent FS to 2.6 or when local FLDB is corrupted,
3767 * and it needs to refresh FLDB from the MDT0.
3769 static int mdt_register_lwp_callback(void *data)
3772 struct mdt_device *mdt = data;
3773 struct lu_server_fld *fld = mdt_seq_site(mdt)->ss_server_fld;
3777 LASSERT(mdt_seq_site(mdt)->ss_node_id != 0);
3779 rc = lu_env_init(&env, LCT_MD_THREAD);
3781 CERROR("%s: cannot init env: rc = %d\n", mdt_obd_name(mdt), rc);
3785 /* Allocate new sequence now to avoid creating local transaction
3786 * in the normal transaction process */
3787 rc = seq_server_check_and_alloc_super(&env,
3788 mdt_seq_site(mdt)->ss_server_seq);
3793 rc = fld_update_from_controller(&env, fld);
3795 CERROR("%s: cannot update controller: rc = %d\n",
3796 mdt_obd_name(mdt), rc);
3805 static int mdt_register_seq_exp(struct mdt_device *mdt)
3807 struct seq_server_site *ss = mdt_seq_site(mdt);
3808 char *lwp_name = NULL;
3811 if (ss->ss_node_id == 0)
3814 OBD_ALLOC(lwp_name, MAX_OBD_NAME);
3815 if (lwp_name == NULL)
3816 GOTO(out_free, rc = -ENOMEM);
3818 rc = tgt_name2lwp_name(mdt_obd_name(mdt), lwp_name, MAX_OBD_NAME, 0);
3822 rc = lustre_register_lwp_item(lwp_name, &ss->ss_client_seq->lcs_exp,
3827 rc = lustre_register_lwp_item(lwp_name,
3828 &ss->ss_server_fld->lsf_control_exp,
3829 mdt_register_lwp_callback, mdt);
3831 lustre_deregister_lwp_item(&ss->ss_client_seq->lcs_exp);
3832 ss->ss_client_seq->lcs_exp = NULL;
3836 if (lwp_name != NULL)
3837 OBD_FREE(lwp_name, MAX_OBD_NAME);
3843 * Init client sequence manager which is used by local MDS to talk to sequence
3844 * controller on remote node.
3846 static int mdt_seq_init_cli(const struct lu_env *env, struct mdt_device *mdt)
3848 struct seq_server_site *ss = mdt_seq_site(mdt);
3853 /* check if this is adding the first MDC and controller is not yet
3855 OBD_ALLOC_PTR(ss->ss_client_seq);
3856 if (ss->ss_client_seq == NULL)
3859 OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
3860 if (prefix == NULL) {
3861 OBD_FREE_PTR(ss->ss_client_seq);
3862 ss->ss_client_seq = NULL;
3866 /* Note: seq_client_fini will be called in seq_site_fini */
3867 snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", mdt_obd_name(mdt));
3868 rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_METADATA,
3869 prefix, ss->ss_node_id == 0 ? ss->ss_control_seq :
3871 OBD_FREE(prefix, MAX_OBD_NAME + 5);
3873 OBD_FREE_PTR(ss->ss_client_seq);
3874 ss->ss_client_seq = NULL;
3878 rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
3883 static int mdt_seq_init(const struct lu_env *env, struct mdt_device *mdt)
3885 struct seq_server_site *ss;
3889 ss = mdt_seq_site(mdt);
3890 /* init sequence controller server(MDT0) */
3891 if (ss->ss_node_id == 0) {
3892 OBD_ALLOC_PTR(ss->ss_control_seq);
3893 if (ss->ss_control_seq == NULL)
3896 rc = seq_server_init(env, ss->ss_control_seq, mdt->mdt_bottom,
3897 mdt_obd_name(mdt), LUSTRE_SEQ_CONTROLLER,
3900 GOTO(out_seq_fini, rc);
3903 /* Init normal sequence server */
3904 OBD_ALLOC_PTR(ss->ss_server_seq);
3905 if (ss->ss_server_seq == NULL)
3906 GOTO(out_seq_fini, rc = -ENOMEM);
3908 rc = seq_server_init(env, ss->ss_server_seq, mdt->mdt_bottom,
3909 mdt_obd_name(mdt), LUSTRE_SEQ_SERVER, ss);
3911 GOTO(out_seq_fini, rc);
3913 /* init seq client for seq server to talk to seq controller(MDT0) */
3914 rc = mdt_seq_init_cli(env, mdt);
3916 GOTO(out_seq_fini, rc);
3918 if (ss->ss_node_id != 0)
3919 /* register controller export through lwp */
3920 rc = mdt_register_seq_exp(mdt);
3925 mdt_seq_fini(env, mdt);
3933 static int mdt_fld_fini(const struct lu_env *env,
3934 struct mdt_device *m)
3936 struct seq_server_site *ss = mdt_seq_site(m);
3939 if (ss && ss->ss_server_fld) {
3940 fld_server_fini(env, ss->ss_server_fld);
3941 OBD_FREE_PTR(ss->ss_server_fld);
3942 ss->ss_server_fld = NULL;
3948 static int mdt_fld_init(const struct lu_env *env,
3950 struct mdt_device *m)
3952 struct seq_server_site *ss;
3956 ss = mdt_seq_site(m);
3958 OBD_ALLOC_PTR(ss->ss_server_fld);
3959 if (ss->ss_server_fld == NULL)
3960 RETURN(rc = -ENOMEM);
3962 rc = fld_server_init(env, ss->ss_server_fld, m->mdt_bottom, uuid,
3965 OBD_FREE_PTR(ss->ss_server_fld);
3966 ss->ss_server_fld = NULL;
3973 static void mdt_stack_pre_fini(const struct lu_env *env,
3974 struct mdt_device *m, struct lu_device *top)
3976 struct lustre_cfg_bufs *bufs;
3977 struct lustre_cfg *lcfg;
3978 struct mdt_thread_info *info;
3983 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
3984 LASSERT(info != NULL);
3986 bufs = &info->mti_u.bufs;
3988 LASSERT(m->mdt_child_exp);
3989 LASSERT(m->mdt_child_exp->exp_obd);
3991 /* process cleanup, pass mdt obd name to get obd umount flags */
3992 /* XXX: this is needed because all layers are referenced by
3993 * objects (some of them are pinned by osd, for example *
3994 * the proper solution should be a model where object used
3995 * by osd only doesn't have mdt/mdd slices -bzzz */
3996 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
3997 lustre_cfg_bufs_set_string(bufs, 1, NULL);
3998 lcfg = lustre_cfg_new(LCFG_PRE_CLEANUP, bufs);
4002 top->ld_ops->ldo_process_config(env, top, lcfg);
4003 lustre_cfg_free(lcfg);
4007 static void mdt_stack_fini(const struct lu_env *env,
4008 struct mdt_device *m, struct lu_device *top)
4010 struct obd_device *obd = mdt2obd_dev(m);
4011 struct lustre_cfg_bufs *bufs;
4012 struct lustre_cfg *lcfg;
4013 struct mdt_thread_info *info;
4017 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4018 LASSERT(info != NULL);
4020 lu_dev_del_linkage(top->ld_site, top);
4022 lu_site_purge(env, top->ld_site, -1);
4024 bufs = &info->mti_u.bufs;
4025 /* process cleanup, pass mdt obd name to get obd umount flags */
4026 /* another purpose is to let all layers to release their objects */
4027 lustre_cfg_bufs_reset(bufs, mdt_obd_name(m));
4032 lustre_cfg_bufs_set_string(bufs, 1, flags);
4033 lcfg = lustre_cfg_new(LCFG_CLEANUP, bufs);
4038 top->ld_ops->ldo_process_config(env, top, lcfg);
4039 lustre_cfg_free(lcfg);
4041 lu_site_purge(env, top->ld_site, -1);
4043 m->mdt_child = NULL;
4044 m->mdt_bottom = NULL;
4046 obd_disconnect(m->mdt_child_exp);
4047 m->mdt_child_exp = NULL;
4049 obd_disconnect(m->mdt_bottom_exp);
4050 m->mdt_child_exp = NULL;
4053 static int mdt_connect_to_next(const struct lu_env *env, struct mdt_device *m,
4054 const char *next, struct obd_export **exp)
4056 struct obd_connect_data *data = NULL;
4057 struct obd_device *obd;
4061 OBD_ALLOC_PTR(data);
4063 GOTO(out, rc = -ENOMEM);
4065 obd = class_name2obd(next);
4067 CERROR("%s: can't locate next device: %s\n",
4068 mdt_obd_name(m), next);
4069 GOTO(out, rc = -ENOTCONN);
4072 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4073 data->ocd_version = LUSTRE_VERSION_CODE;
4075 rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
4077 CERROR("%s: cannot connect to next dev %s (%d)\n",
4078 mdt_obd_name(m), next, rc);
4088 static int mdt_stack_init(const struct lu_env *env, struct mdt_device *mdt,
4089 struct lustre_cfg *cfg)
4091 char *dev = lustre_cfg_string(cfg, 0);
4092 int rc, name_size, uuid_size;
4093 char *name, *uuid, *p;
4094 struct lustre_cfg_bufs *bufs;
4095 struct lustre_cfg *lcfg;
4096 struct obd_device *obd;
4097 struct lustre_profile *lprof;
4098 struct lu_site *site;
4101 /* in 1.8 we had the only device in the stack - MDS.
4102 * 2.0 introduces MDT, MDD, OSD; MDT starts others internally.
4103 * in 2.3 OSD is instantiated by obd_mount.c, so we need
4104 * to generate names and setup MDT, MDD. MDT will be using
4105 * generated name to connect to MDD. for MDD the next device
4106 * will be LOD with name taken from so called "profile" which
4107 * is generated by mount_option line
4109 * 1.8 MGS generates config. commands like this:
4110 * #06 (104)mount_option 0: 1:lustre-MDT0000 2:lustre-mdtlov
4111 * #08 (120)setup 0:lustre-MDT0000 1:dev 2:type 3:lustre-MDT0000
4112 * 2.0 MGS generates config. commands like this:
4113 * #07 (112)mount_option 0: 1:lustre-MDT0000 2:lustre-MDT0000-mdtlov
4114 * #08 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4115 * 3:lustre-MDT0000-mdtlov 4:f
4117 * we generate MDD name from MDT one, just replacing T with D
4119 * after all the preparations, the logical equivalent will be
4120 * #01 (160)setup 0:lustre-MDD0000 1:lustre-MDD0000_UUID 2:0
4121 * 3:lustre-MDT0000-mdtlov 4:f
4122 * #02 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4123 * 3:lustre-MDD0000 4:f
4125 * notice we build the stack from down to top: MDD first, then MDT */
4127 name_size = MAX_OBD_NAME;
4128 uuid_size = MAX_OBD_NAME;
4130 OBD_ALLOC(name, name_size);
4131 OBD_ALLOC(uuid, uuid_size);
4132 if (name == NULL || uuid == NULL)
4133 GOTO(cleanup_mem, rc = -ENOMEM);
4135 OBD_ALLOC_PTR(bufs);
4137 GOTO(cleanup_mem, rc = -ENOMEM);
4140 p = strstr(name, "-MDT");
4142 GOTO(free_bufs, rc = -ENOMEM);
4145 snprintf(uuid, MAX_OBD_NAME, "%s_UUID", name);
4147 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4148 if (lprof == NULL || lprof->lp_dt == NULL) {
4149 CERROR("can't find the profile: %s\n",
4150 lustre_cfg_string(cfg, 0));
4151 GOTO(free_bufs, rc = -EINVAL);
4154 lustre_cfg_bufs_reset(bufs, name);
4155 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_MDD_NAME);
4156 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4157 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4159 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4161 GOTO(put_profile, rc = -ENOMEM);
4163 rc = class_attach(lcfg);
4165 GOTO(lcfg_cleanup, rc);
4167 obd = class_name2obd(name);
4169 CERROR("Can not find obd %s (%s in config)\n",
4170 MDD_OBD_NAME, lustre_cfg_string(cfg, 0));
4171 GOTO(lcfg_cleanup, rc = -EINVAL);
4174 lustre_cfg_free(lcfg);
4176 lustre_cfg_bufs_reset(bufs, name);
4177 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4178 lustre_cfg_bufs_set_string(bufs, 2, dev);
4179 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4181 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4183 GOTO(class_detach, rc = -ENOMEM);
4185 rc = class_setup(obd, lcfg);
4187 GOTO(class_detach, rc);
4189 /* connect to MDD we just setup */
4190 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_child_exp);
4192 GOTO(class_detach, rc);
4194 site = mdt->mdt_child_exp->exp_obd->obd_lu_dev->ld_site;
4196 LASSERT(mdt_lu_site(mdt) == NULL);
4197 mdt->mdt_lu_dev.ld_site = site;
4198 site->ls_top_dev = &mdt->mdt_lu_dev;
4199 mdt->mdt_child = lu2md_dev(mdt->mdt_child_exp->exp_obd->obd_lu_dev);
4201 /* now connect to bottom OSD */
4202 snprintf(name, MAX_OBD_NAME, "%s-osd", dev);
4203 rc = mdt_connect_to_next(env, mdt, name, &mdt->mdt_bottom_exp);
4205 GOTO(class_detach, rc);
4207 lu2dt_dev(mdt->mdt_bottom_exp->exp_obd->obd_lu_dev);
4209 rc = lu_env_refill((struct lu_env *)env);
4211 CERROR("Failure to refill session: '%d'\n", rc);
4213 lu_dev_add_linkage(site, &mdt->mdt_lu_dev);
4218 class_detach(obd, lcfg);
4220 lustre_cfg_free(lcfg);
4222 class_put_profile(lprof);
4227 OBD_FREE(name, name_size);
4229 OBD_FREE(uuid, uuid_size);
4233 /* setup quota master target on MDT0 */
4234 static int mdt_quota_init(const struct lu_env *env, struct mdt_device *mdt,
4235 struct lustre_cfg *cfg)
4237 struct obd_device *obd;
4238 char *dev = lustre_cfg_string(cfg, 0);
4239 char *qmtname, *uuid, *p;
4240 struct lustre_cfg_bufs *bufs;
4241 struct lustre_cfg *lcfg;
4242 struct lustre_profile *lprof;
4243 struct obd_connect_data *data;
4247 LASSERT(mdt->mdt_qmt_exp == NULL);
4248 LASSERT(mdt->mdt_qmt_dev == NULL);
4250 /* quota master is on MDT0 only for now */
4251 if (mdt->mdt_seq_site.ss_node_id != 0)
4254 /* MGS generates config commands which look as follows:
4255 * #01 (160)setup 0:lustre-MDT0000 1:lustre-MDT0000_UUID 2:0
4256 * 3:lustre-MDT0000-mdtlov 4:f
4258 * We generate the QMT name from the MDT one, just replacing MD with QM
4259 * after all the preparations, the logical equivalent will be:
4260 * #01 (160)setup 0:lustre-QMT0000 1:lustre-QMT0000_UUID 2:0
4261 * 3:lustre-MDT0000-osd 4:f */
4262 OBD_ALLOC(qmtname, MAX_OBD_NAME);
4263 OBD_ALLOC(uuid, UUID_MAX);
4264 OBD_ALLOC_PTR(bufs);
4265 OBD_ALLOC_PTR(data);
4266 if (qmtname == NULL || uuid == NULL || bufs == NULL || data == NULL)
4267 GOTO(cleanup_mem, rc = -ENOMEM);
4269 strcpy(qmtname, dev);
4270 p = strstr(qmtname, "-MDT");
4272 GOTO(cleanup_mem, rc = -ENOMEM);
4273 /* replace MD with QM */
4277 snprintf(uuid, UUID_MAX, "%s_UUID", qmtname);
4279 lprof = class_get_profile(lustre_cfg_string(cfg, 0));
4280 if (lprof == NULL || lprof->lp_dt == NULL) {
4281 CERROR("can't find profile for %s\n",
4282 lustre_cfg_string(cfg, 0));
4283 GOTO(cleanup_mem, rc = -EINVAL);
4286 lustre_cfg_bufs_reset(bufs, qmtname);
4287 lustre_cfg_bufs_set_string(bufs, 1, LUSTRE_QMT_NAME);
4288 lustre_cfg_bufs_set_string(bufs, 2, uuid);
4289 lustre_cfg_bufs_set_string(bufs, 3, lprof->lp_dt);
4291 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
4293 GOTO(put_profile, rc = -ENOMEM);
4295 rc = class_attach(lcfg);
4297 GOTO(lcfg_cleanup, rc);
4299 obd = class_name2obd(qmtname);
4301 CERROR("Can not find obd %s (%s in config)\n", qmtname,
4302 lustre_cfg_string(cfg, 0));
4303 GOTO(lcfg_cleanup, rc = -EINVAL);
4306 lustre_cfg_free(lcfg);
4308 lustre_cfg_bufs_reset(bufs, qmtname);
4309 lustre_cfg_bufs_set_string(bufs, 1, uuid);
4310 lustre_cfg_bufs_set_string(bufs, 2, dev);
4312 /* for quota, the next device should be the OSD device */
4313 lustre_cfg_bufs_set_string(bufs, 3,
4314 mdt->mdt_bottom->dd_lu_dev.ld_obd->obd_name);
4316 lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
4318 GOTO(class_detach, rc = -ENOMEM);
4320 rc = class_setup(obd, lcfg);
4322 GOTO(class_detach, rc);
4324 mdt->mdt_qmt_dev = obd->obd_lu_dev;
4326 /* configure local quota objects */
4327 rc = mdt->mdt_qmt_dev->ld_ops->ldo_prepare(env,
4331 GOTO(class_cleanup, rc);
4333 /* connect to quota master target */
4334 data->ocd_connect_flags = OBD_CONNECT_VERSION;
4335 data->ocd_version = LUSTRE_VERSION_CODE;
4336 rc = obd_connect(NULL, &mdt->mdt_qmt_exp, obd, &obd->obd_uuid,
4339 CERROR("cannot connect to quota master device %s (%d)\n",
4341 GOTO(class_cleanup, rc);
4347 class_manual_cleanup(obd);
4348 mdt->mdt_qmt_dev = NULL;
4352 class_detach(obd, lcfg);
4354 lustre_cfg_free(lcfg);
4356 class_put_profile(lprof);
4361 OBD_FREE(qmtname, MAX_OBD_NAME);
4363 OBD_FREE(uuid, UUID_MAX);
4369 /* Shutdown quota master target associated with mdt */
4370 static void mdt_quota_fini(const struct lu_env *env, struct mdt_device *mdt)
4374 if (mdt->mdt_qmt_exp == NULL)
4376 LASSERT(mdt->mdt_qmt_dev != NULL);
4378 /* the qmt automatically shuts down when the mdt disconnects */
4379 obd_disconnect(mdt->mdt_qmt_exp);
4380 mdt->mdt_qmt_exp = NULL;
4381 mdt->mdt_qmt_dev = NULL;
4385 /* mdt_getxattr() is used from mdt_intent_getxattr(), use this wrapper
4386 * for now. This will be removed along with converting rest of MDT code
4387 * to use tgt_session_info */
4388 static int mdt_tgt_getxattr(struct tgt_session_info *tsi)
4390 struct mdt_thread_info *info = tsi2mdt_info(tsi);
4393 rc = mdt_getxattr(info);
4395 mdt_thread_info_fini(info);
4399 static struct tgt_handler mdt_tgt_handlers[] = {
4400 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4401 0, MDS_CONNECT, mdt_tgt_connect,
4402 &RQF_CONNECT, LUSTRE_OBD_VERSION),
4403 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4404 0, MDS_DISCONNECT, tgt_disconnect,
4405 &RQF_MDS_DISCONNECT, LUSTRE_OBD_VERSION),
4406 TGT_RPC_HANDLER(MDS_FIRST_OPC,
4407 HABEO_REFERO, MDS_SET_INFO, mdt_set_info,
4408 &RQF_OBD_SET_INFO, LUSTRE_MDS_VERSION),
4409 TGT_MDT_HDL(0, MDS_GET_INFO, mdt_get_info),
4410 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_GET_ROOT, mdt_get_root),
4411 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETATTR, mdt_getattr),
4412 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_GETATTR_NAME,
4414 TGT_MDT_HDL(HABEO_CORPUS, MDS_GETXATTR, mdt_tgt_getxattr),
4415 TGT_MDT_HDL(0 | HABEO_REFERO, MDS_STATFS, mdt_statfs),
4416 TGT_MDT_HDL(0 | MUTABOR, MDS_REINT, mdt_reint),
4417 TGT_MDT_HDL(HABEO_CORPUS, MDS_CLOSE, mdt_close),
4418 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_READPAGE, mdt_readpage),
4419 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_SYNC, mdt_sync),
4420 TGT_MDT_HDL(0, MDS_QUOTACTL, mdt_quotactl),
4421 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_PROGRESS,
4423 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_REGISTER,
4424 mdt_hsm_ct_register),
4425 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_CT_UNREGISTER,
4426 mdt_hsm_ct_unregister),
4427 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_STATE_GET,
4429 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR, MDS_HSM_STATE_SET,
4431 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_ACTION, mdt_hsm_action),
4432 TGT_MDT_HDL(HABEO_CORPUS| HABEO_REFERO, MDS_HSM_REQUEST,
4434 TGT_MDT_HDL(HABEO_CLAVIS | HABEO_CORPUS | HABEO_REFERO | MUTABOR,
4439 static struct tgt_handler mdt_sec_ctx_ops[] = {
4440 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT, mdt_sec_ctx_handle),
4441 TGT_SEC_HDL_VAR(0, SEC_CTX_INIT_CONT,mdt_sec_ctx_handle),
4442 TGT_SEC_HDL_VAR(0, SEC_CTX_FINI, mdt_sec_ctx_handle)
4445 static struct tgt_handler mdt_quota_ops[] = {
4446 TGT_QUOTA_HDL(HABEO_REFERO, QUOTA_DQACQ, mdt_quota_dqacq),
4449 static struct tgt_opc_slice mdt_common_slice[] = {
4451 .tos_opc_start = MDS_FIRST_OPC,
4452 .tos_opc_end = MDS_LAST_OPC,
4453 .tos_hs = mdt_tgt_handlers
4456 .tos_opc_start = OBD_FIRST_OPC,
4457 .tos_opc_end = OBD_LAST_OPC,
4458 .tos_hs = tgt_obd_handlers
4461 .tos_opc_start = LDLM_FIRST_OPC,
4462 .tos_opc_end = LDLM_LAST_OPC,
4463 .tos_hs = tgt_dlm_handlers
4466 .tos_opc_start = SEC_FIRST_OPC,
4467 .tos_opc_end = SEC_LAST_OPC,
4468 .tos_hs = mdt_sec_ctx_ops
4471 .tos_opc_start = OUT_UPDATE_FIRST_OPC,
4472 .tos_opc_end = OUT_UPDATE_LAST_OPC,
4473 .tos_hs = tgt_out_handlers
4476 .tos_opc_start = FLD_FIRST_OPC,
4477 .tos_opc_end = FLD_LAST_OPC,
4478 .tos_hs = fld_handlers
4481 .tos_opc_start = SEQ_FIRST_OPC,
4482 .tos_opc_end = SEQ_LAST_OPC,
4483 .tos_hs = seq_handlers
4486 .tos_opc_start = QUOTA_DQACQ,
4487 .tos_opc_end = QUOTA_LAST_OPC,
4488 .tos_hs = mdt_quota_ops
4491 .tos_opc_start = LLOG_FIRST_OPC,
4492 .tos_opc_end = LLOG_LAST_OPC,
4493 .tos_hs = tgt_llog_handlers
4496 .tos_opc_start = LFSCK_FIRST_OPC,
4497 .tos_opc_end = LFSCK_LAST_OPC,
4498 .tos_hs = tgt_lfsck_handlers
4506 static void mdt_fini(const struct lu_env *env, struct mdt_device *m)
4508 struct md_device *next = m->mdt_child;
4509 struct lu_device *d = &m->mdt_lu_dev;
4510 struct obd_device *obd = mdt2obd_dev(m);
4511 struct lfsck_stop stop;
4514 stop.ls_status = LS_PAUSED;
4516 next->md_ops->mdo_iocontrol(env, next, OBD_IOC_STOP_LFSCK, 0, &stop);
4518 mdt_stack_pre_fini(env, m, md2lu_dev(m->mdt_child));
4519 ping_evictor_stop();
4521 if (m->mdt_opts.mo_coordinator)
4522 mdt_hsm_cdt_stop(m);
4524 mdt_hsm_cdt_fini(m);
4526 mdt_llog_ctxt_unclone(env, m, LLOG_AGENT_ORIG_CTXT);
4527 mdt_llog_ctxt_unclone(env, m, LLOG_CHANGELOG_ORIG_CTXT);
4529 if (m->mdt_namespace != NULL)
4530 ldlm_namespace_free_prior(m->mdt_namespace, NULL,
4531 d->ld_obd->obd_force);
4533 obd_exports_barrier(obd);
4534 obd_zombie_barrier();
4538 tgt_fini(env, &m->mdt_lut);
4539 mdt_fs_cleanup(env, m);
4540 upcall_cache_cleanup(m->mdt_identity_cache);
4541 m->mdt_identity_cache = NULL;
4543 if (m->mdt_namespace != NULL) {
4544 ldlm_namespace_free_post(m->mdt_namespace);
4545 d->ld_obd->obd_namespace = m->mdt_namespace = NULL;
4548 if (m->mdt_md_root != NULL) {
4549 mdt_object_put(env, m->mdt_md_root);
4550 m->mdt_md_root = NULL;
4553 mdt_quota_fini(env, m);
4555 cfs_free_nidlist(&m->mdt_squash.rsi_nosquash_nids);
4557 mdt_seq_fini(env, m);
4558 mdt_fld_fini(env, m);
4563 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4565 LASSERT(atomic_read(&d->ld_ref) == 0);
4567 server_put_mount(mdt_obd_name(m), true);
4572 static int mdt_postrecov(const struct lu_env *, struct mdt_device *);
4574 static int mdt_init0(const struct lu_env *env, struct mdt_device *m,
4575 struct lu_device_type *ldt, struct lustre_cfg *cfg)
4577 struct mdt_thread_info *info;
4578 struct obd_device *obd;
4579 const char *dev = lustre_cfg_string(cfg, 0);
4580 const char *num = lustre_cfg_string(cfg, 2);
4581 struct lustre_mount_info *lmi = NULL;
4582 struct lustre_sb_info *lsi;
4584 struct seq_server_site *ss_site;
4585 const char *identity_upcall = "NONE";
4586 struct md_device *next;
4592 lu_device_init(&m->mdt_lu_dev, ldt);
4594 * Environment (env) might be missing mdt_thread_key values at that
4595 * point, if device is allocated when mdt_thread_key is in QUIESCENT
4598 * Usually device allocation path doesn't use module key values, but
4599 * mdt has to do a lot of work here, so allocate key value.
4601 rc = lu_env_refill((struct lu_env *)env);
4605 info = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
4606 LASSERT(info != NULL);
4608 obd = class_name2obd(dev);
4609 LASSERT(obd != NULL);
4611 m->mdt_max_mdsize = MAX_MD_SIZE; /* 4 stripes */
4612 m->mdt_opts.mo_evict_tgt_nids = 1;
4613 m->mdt_opts.mo_cos = MDT_COS_DEFAULT;
4615 /* default is coordinator off, it is started through conf_param
4617 m->mdt_opts.mo_coordinator = 0;
4619 lmi = server_get_mount(dev);
4621 CERROR("Cannot get mount info for %s!\n", dev);
4624 lsi = s2lsi(lmi->lmi_sb);
4625 /* CMD is supported only in IAM mode */
4627 node_id = simple_strtol(num, NULL, 10);
4628 obd->u.obt.obt_magic = OBT_MAGIC;
4629 if (lsi->lsi_lmd != NULL &&
4630 lsi->lsi_lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
4631 m->mdt_skip_lfsck = 1;
4634 m->mdt_squash.rsi_uid = 0;
4635 m->mdt_squash.rsi_gid = 0;
4636 INIT_LIST_HEAD(&m->mdt_squash.rsi_nosquash_nids);
4637 init_rwsem(&m->mdt_squash.rsi_sem);
4638 spin_lock_init(&m->mdt_lock);
4639 m->mdt_osfs_age = cfs_time_shift_64(-1000);
4640 m->mdt_enable_remote_dir = 0;
4641 m->mdt_enable_remote_dir_gid = 0;
4643 atomic_set(&m->mdt_mds_mds_conns, 0);
4644 atomic_set(&m->mdt_async_commit_count, 0);
4646 m->mdt_lu_dev.ld_ops = &mdt_lu_ops;
4647 m->mdt_lu_dev.ld_obd = obd;
4648 /* Set this lu_device to obd for error handling purposes. */
4649 obd->obd_lu_dev = &m->mdt_lu_dev;
4651 /* init the stack */
4652 rc = mdt_stack_init((struct lu_env *)env, m, cfg);
4654 CERROR("%s: Can't init device stack, rc %d\n",
4655 mdt_obd_name(m), rc);
4660 ss_site = mdt_seq_site(m);
4661 s->ld_seq_site = ss_site;
4664 /* set server index */
4665 ss_site->ss_node_id = node_id;
4667 /* failover is the default
4668 * FIXME: we do not failout mds0/mgs, which may cause some problems.
4669 * assumed whose ss_node_id == 0 XXX
4671 obd->obd_replayable = 1;
4672 /* No connection accepted until configurations will finish */
4673 obd->obd_no_conn = 1;
4675 if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
4676 char *str = lustre_cfg_string(cfg, 4);
4677 if (strchr(str, 'n')) {
4678 CWARN("%s: recovery disabled\n", mdt_obd_name(m));
4679 obd->obd_replayable = 0;
4683 rc = mdt_fld_init(env, mdt_obd_name(m), m);
4685 GOTO(err_fini_stack, rc);
4687 rc = mdt_seq_init(env, m);
4689 GOTO(err_fini_fld, rc);
4691 snprintf(info->mti_u.ns_name, sizeof(info->mti_u.ns_name), "%s-%s",
4692 LUSTRE_MDT_NAME, obd->obd_uuid.uuid);
4693 m->mdt_namespace = ldlm_namespace_new(obd, info->mti_u.ns_name,
4694 LDLM_NAMESPACE_SERVER,
4695 LDLM_NAMESPACE_GREEDY,
4697 if (m->mdt_namespace == NULL)
4698 GOTO(err_fini_seq, rc = -ENOMEM);
4700 m->mdt_namespace->ns_lvbp = m;
4701 m->mdt_namespace->ns_lvbo = &mdt_lvbo;
4703 ldlm_register_intent(m->mdt_namespace, mdt_intent_policy);
4704 /* set obd_namespace for compatibility with old code */
4705 obd->obd_namespace = m->mdt_namespace;
4707 rc = mdt_hsm_cdt_init(m);
4709 CERROR("%s: error initializing coordinator, rc %d\n",
4710 mdt_obd_name(m), rc);
4711 GOTO(err_free_ns, rc);
4714 rc = tgt_init(env, &m->mdt_lut, obd, m->mdt_bottom, mdt_common_slice,
4715 OBD_FAIL_MDS_ALL_REQUEST_NET,
4716 OBD_FAIL_MDS_ALL_REPLY_NET);
4718 GOTO(err_free_hsm, rc);
4720 rc = mdt_fs_setup(env, m, obd, lsi);
4724 tgt_adapt_sptlrpc_conf(&m->mdt_lut, 1);
4726 next = m->mdt_child;
4727 rc = next->md_ops->mdo_iocontrol(env, next, OBD_IOC_GET_MNTOPT, 0,
4730 GOTO(err_fs_cleanup, rc);
4732 if (mntopts & MNTOPT_USERXATTR)
4733 m->mdt_opts.mo_user_xattr = 1;
4735 m->mdt_opts.mo_user_xattr = 0;
4737 rc = next->md_ops->mdo_maxeasize_get(env, next, &m->mdt_max_ea_size);
4739 GOTO(err_fs_cleanup, rc);
4741 if (mntopts & MNTOPT_ACL)
4742 m->mdt_opts.mo_acl = 1;
4744 m->mdt_opts.mo_acl = 0;
4746 /* XXX: to support suppgid for ACL, we enable identity_upcall
4747 * by default, otherwise, maybe got unexpected -EACCESS. */
4748 if (m->mdt_opts.mo_acl)
4749 identity_upcall = MDT_IDENTITY_UPCALL_PATH;
4751 m->mdt_identity_cache = upcall_cache_init(mdt_obd_name(m),
4753 &mdt_identity_upcall_cache_ops);
4754 if (IS_ERR(m->mdt_identity_cache)) {
4755 rc = PTR_ERR(m->mdt_identity_cache);
4756 m->mdt_identity_cache = NULL;
4757 GOTO(err_fs_cleanup, rc);
4760 rc = mdt_procfs_init(m, dev);
4762 CERROR("Can't init MDT lprocfs, rc %d\n", rc);
4763 GOTO(err_recovery, rc);
4766 rc = mdt_quota_init(env, m, cfg);
4768 GOTO(err_procfs, rc);
4770 m->mdt_ldlm_client = &mdt2obd_dev(m)->obd_ldlm_client;
4771 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
4772 "mdt_ldlm_client", m->mdt_ldlm_client);
4774 ping_evictor_start();
4776 /* recovery will be started upon mdt_prepare()
4777 * when the whole stack is complete and ready
4778 * to serve the requests */
4780 /* Reduce the initial timeout on an MDS because it doesn't need such
4781 * a long timeout as an OST does. Adaptive timeouts will adjust this
4782 * value appropriately. */
4783 if (ldlm_timeout == LDLM_TIMEOUT_DEFAULT)
4784 ldlm_timeout = MDS_LDLM_TIMEOUT_DEFAULT;
4790 target_recovery_fini(obd);
4791 upcall_cache_cleanup(m->mdt_identity_cache);
4792 m->mdt_identity_cache = NULL;
4794 mdt_fs_cleanup(env, m);
4796 tgt_fini(env, &m->mdt_lut);
4798 mdt_hsm_cdt_fini(m);
4800 ldlm_namespace_free(m->mdt_namespace, NULL, 0);
4801 obd->obd_namespace = m->mdt_namespace = NULL;
4803 mdt_seq_fini(env, m);
4805 mdt_fld_fini(env, m);
4807 mdt_stack_fini(env, m, md2lu_dev(m->mdt_child));
4810 server_put_mount(dev, true);
4814 /* For interoperability, the left element is old parameter, the right one
4815 * is the new version of the parameter, if some parameter is deprecated,
4816 * the new version should be set as NULL. */
4817 static struct cfg_interop_param mdt_interop_param[] = {
4818 { "mdt.group_upcall", NULL },
4819 { "mdt.quota_type", NULL },
4820 { "mdd.quota_type", NULL },
4821 { "mdt.som", NULL },
4822 { "mdt.rootsquash", "mdt.root_squash" },
4823 { "mdt.nosquash_nid", "mdt.nosquash_nids" },
4827 /* used by MGS to process specific configurations */
4828 static int mdt_process_config(const struct lu_env *env,
4829 struct lu_device *d, struct lustre_cfg *cfg)
4831 struct mdt_device *m = mdt_dev(d);
4832 struct md_device *md_next = m->mdt_child;
4833 struct lu_device *next = md2lu_dev(md_next);
4837 switch (cfg->lcfg_command) {
4839 struct obd_device *obd = d->ld_obd;
4841 /* For interoperability */
4842 struct cfg_interop_param *ptr = NULL;
4843 struct lustre_cfg *old_cfg = NULL;
4846 param = lustre_cfg_string(cfg, 1);
4847 if (param == NULL) {
4848 CERROR("param is empty\n");
4853 ptr = class_find_old_param(param, mdt_interop_param);
4855 if (ptr->new_param == NULL) {
4857 CWARN("For interoperability, skip this %s."
4858 " It is obsolete.\n", ptr->old_param);
4862 CWARN("Found old param %s, changed it to %s.\n",
4863 ptr->old_param, ptr->new_param);
4866 cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
4873 rc = class_process_proc_param(PARAM_MDT, obd->obd_vars,
4875 if (rc > 0 || rc == -ENOSYS) {
4876 /* is it an HSM var ? */
4877 rc = class_process_proc_param(PARAM_HSM,
4878 hsm_cdt_get_proc_vars(),
4880 if (rc > 0 || rc == -ENOSYS)
4881 /* we don't understand; pass it on */
4882 rc = next->ld_ops->ldo_process_config(env, next,
4886 if (old_cfg != NULL)
4887 lustre_cfg_free(cfg);
4892 /* others are passed further */
4893 rc = next->ld_ops->ldo_process_config(env, next, cfg);
4899 static struct lu_object *mdt_object_alloc(const struct lu_env *env,
4900 const struct lu_object_header *hdr,
4901 struct lu_device *d)
4903 struct mdt_object *mo;
4907 OBD_SLAB_ALLOC_PTR_GFP(mo, mdt_object_kmem, GFP_NOFS);
4909 struct lu_object *o;
4910 struct lu_object_header *h;
4913 h = &mo->mot_header;
4914 lu_object_header_init(h);
4915 lu_object_init(o, h, d);
4916 lu_object_add_top(h, o);
4917 o->lo_ops = &mdt_obj_ops;
4918 spin_lock_init(&mo->mot_write_lock);
4919 mutex_init(&mo->mot_lov_mutex);
4920 init_rwsem(&mo->mot_open_sem);
4926 static int mdt_object_init(const struct lu_env *env, struct lu_object *o,
4927 const struct lu_object_conf *unused)
4929 struct mdt_device *d = mdt_dev(o->lo_dev);
4930 struct lu_device *under;
4931 struct lu_object *below;
4935 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
4936 PFID(lu_object_fid(o)));
4938 under = &d->mdt_child->md_lu_dev;
4939 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
4940 if (below != NULL) {
4941 lu_object_add(o, below);
4948 static void mdt_object_free(const struct lu_env *env, struct lu_object *o)
4950 struct mdt_object *mo = mdt_obj(o);
4951 struct lu_object_header *h;
4955 CDEBUG(D_INFO, "object free, fid = "DFID"\n",
4956 PFID(lu_object_fid(o)));
4958 LASSERT(atomic_read(&mo->mot_open_count) == 0);
4959 LASSERT(atomic_read(&mo->mot_lease_count) == 0);
4962 lu_object_header_fini(h);
4963 OBD_SLAB_FREE_PTR(mo, mdt_object_kmem);
4968 static int mdt_object_print(const struct lu_env *env, void *cookie,
4969 lu_printer_t p, const struct lu_object *o)
4971 struct mdt_object *mdto = mdt_obj((struct lu_object *)o);
4973 return (*p)(env, cookie,
4974 LUSTRE_MDT_NAME"-object@%p(%s %s, writecount=%d)",
4975 mdto, mdto->mot_lov_created ? "lov_created" : "",
4976 mdto->mot_cache_attr ? "cache_attr" : "",
4977 mdto->mot_write_count);
4980 static int mdt_prepare(const struct lu_env *env,
4981 struct lu_device *pdev,
4982 struct lu_device *cdev)
4984 struct mdt_device *mdt = mdt_dev(cdev);
4985 struct lu_device *next = &mdt->mdt_child->md_lu_dev;
4986 struct obd_device *obd = cdev->ld_obd;
4993 rc = next->ld_ops->ldo_prepare(env, cdev, next);
4997 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_CHANGELOG_ORIG_CTXT);
5001 rc = mdt_llog_ctxt_clone(env, mdt, LLOG_AGENT_ORIG_CTXT);
5005 rc = lfsck_register_namespace(env, mdt->mdt_bottom, mdt->mdt_namespace);
5006 /* The LFSCK instance is registered just now, so it must be there when
5007 * register the namespace to such instance. */
5008 LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
5010 if (mdt->mdt_seq_site.ss_node_id == 0) {
5011 rc = mdt->mdt_child->md_ops->mdo_root_get(env, mdt->mdt_child,
5012 &mdt->mdt_md_root_fid);
5017 LASSERT(!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state));
5019 target_recovery_init(&mdt->mdt_lut, tgt_request_handle);
5020 set_bit(MDT_FL_CFGLOG, &mdt->mdt_state);
5021 LASSERT(obd->obd_no_conn);
5022 spin_lock(&obd->obd_dev_lock);
5023 obd->obd_no_conn = 0;
5024 spin_unlock(&obd->obd_dev_lock);
5026 if (obd->obd_recovering == 0)
5027 mdt_postrecov(env, mdt);
5032 const struct lu_device_operations mdt_lu_ops = {
5033 .ldo_object_alloc = mdt_object_alloc,
5034 .ldo_process_config = mdt_process_config,
5035 .ldo_prepare = mdt_prepare,
5038 static const struct lu_object_operations mdt_obj_ops = {
5039 .loo_object_init = mdt_object_init,
5040 .loo_object_free = mdt_object_free,
5041 .loo_object_print = mdt_object_print
5044 static int mdt_obd_set_info_async(const struct lu_env *env,
5045 struct obd_export *exp,
5046 __u32 keylen, void *key,
5047 __u32 vallen, void *val,
5048 struct ptlrpc_request_set *set)
5054 if (KEY_IS(KEY_SPTLRPC_CONF)) {
5055 rc = tgt_adapt_sptlrpc_conf(class_exp2tgt(exp), 0);
5063 * Match client and server connection feature flags.
5065 * Compute the compatibility flags for a connection request based on
5066 * features mutually supported by client and server.
5068 * The obd_export::exp_connect_data.ocd_connect_flags field in \a exp
5069 * must not be updated here, otherwise a partially initialized value may
5070 * be exposed. After the connection request is successfully processed,
5071 * the top-level MDT connect request handler atomically updates the export
5072 * connect flags from the obd_connect_data::ocd_connect_flags field of the
5073 * reply. \see mdt_connect().
5075 * Before 2.7.50 clients will send a struct obd_connect_data_v1 rather than a
5076 * full struct obd_connect_data. So care must be taken when accessing fields
5077 * that are not present in struct obd_connect_data_v1. See LU-16.
5079 * \param exp the obd_export associated with this client/target pair
5080 * \param mdt the target device for the connection
5081 * \param data stores data for this connect request
5084 * \retval -EPROTO \a data unexpectedly has zero obd_connect_data::ocd_brw_size
5085 * \retval -EBADE client and server feature requirements are incompatible
5087 static int mdt_connect_internal(struct obd_export *exp,
5088 struct mdt_device *mdt,
5089 struct obd_connect_data *data)
5091 LASSERT(data != NULL);
5093 data->ocd_connect_flags &= MDT_CONNECT_SUPPORTED;
5095 if (data->ocd_connect_flags & OBD_CONNECT_FLAGS2)
5096 data->ocd_connect_flags2 &= MDT_CONNECT_SUPPORTED2;
5098 data->ocd_ibits_known &= MDS_INODELOCK_FULL;
5100 if (!(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5101 !(data->ocd_connect_flags & OBD_CONNECT_IBITS)) {
5102 CWARN("%s: client %s does not support ibits lock, either "
5103 "very old or an invalid client: flags %#llx\n",
5104 mdt_obd_name(mdt), exp->exp_client_uuid.uuid,
5105 data->ocd_connect_flags);
5109 if (!mdt->mdt_opts.mo_acl)
5110 data->ocd_connect_flags &= ~OBD_CONNECT_ACL;
5112 if (!mdt->mdt_opts.mo_user_xattr)
5113 data->ocd_connect_flags &= ~OBD_CONNECT_XATTR;
5115 if (data->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
5116 data->ocd_brw_size = min(data->ocd_brw_size,
5117 (__u32)MD_MAX_BRW_SIZE);
5118 if (data->ocd_brw_size == 0) {
5119 CERROR("%s: cli %s/%p ocd_connect_flags: %#llx"
5120 " ocd_version: %x ocd_grant: %d "
5121 "ocd_index: %u ocd_brw_size is "
5122 "unexpectedly zero, network data "
5123 "corruption? Refusing connection of this"
5126 exp->exp_client_uuid.uuid,
5127 exp, data->ocd_connect_flags, data->ocd_version,
5128 data->ocd_grant, data->ocd_index);
5133 /* NB: Disregard the rule against updating
5134 * exp_connect_data.ocd_connect_flags in this case, since
5135 * tgt_client_new() needs to know if this is a lightweight
5136 * connection, and it is safe to expose this flag before
5137 * connection processing completes. */
5138 if (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) {
5139 spin_lock(&exp->exp_lock);
5140 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_LIGHTWEIGHT;
5141 spin_unlock(&exp->exp_lock);
5144 data->ocd_version = LUSTRE_VERSION_CODE;
5146 if ((data->ocd_connect_flags & OBD_CONNECT_FID) == 0) {
5147 CWARN("%s: MDS requires FID support, but client not\n",
5152 if (OCD_HAS_FLAG(data, PINGLESS)) {
5153 if (ptlrpc_pinger_suppress_pings()) {
5154 spin_lock(&exp->exp_obd->obd_dev_lock);
5155 list_del_init(&exp->exp_obd_chain_timed);
5156 spin_unlock(&exp->exp_obd->obd_dev_lock);
5158 data->ocd_connect_flags &= ~OBD_CONNECT_PINGLESS;
5162 data->ocd_max_easize = mdt->mdt_max_ea_size;
5164 /* NB: Disregard the rule against updating
5165 * exp_connect_data.ocd_connect_flags in this case, since
5166 * tgt_client_new() needs to know if this is client supports
5167 * multiple modify RPCs, and it is safe to expose this flag before
5168 * connection processing completes. */
5169 if (data->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS) {
5170 data->ocd_maxmodrpcs = max_mod_rpcs_per_client;
5171 spin_lock(&exp->exp_lock);
5172 *exp_connect_flags_ptr(exp) |= OBD_CONNECT_MULTIMODRPCS;
5173 spin_unlock(&exp->exp_lock);
5179 static int mdt_ctxt_add_dirty_flag(struct lu_env *env,
5180 struct mdt_thread_info *info,
5181 struct mdt_file_data *mfd)
5183 struct lu_context ses;
5187 rc = lu_context_init(&ses, LCT_SERVER_SESSION);
5192 lu_context_enter(&ses);
5194 mdt_ucred(info)->uc_valid = UCRED_OLD;
5195 rc = mdt_add_dirty_flag(info, mfd->mfd_object, &info->mti_attr);
5197 lu_context_exit(&ses);
5198 lu_context_fini(&ses);
5204 static int mdt_export_cleanup(struct obd_export *exp)
5206 struct list_head closing_list;
5207 struct mdt_export_data *med = &exp->exp_mdt_data;
5208 struct obd_device *obd = exp->exp_obd;
5209 struct mdt_device *mdt;
5210 struct mdt_thread_info *info;
5212 struct mdt_file_data *mfd, *n;
5216 INIT_LIST_HEAD(&closing_list);
5217 spin_lock(&med->med_open_lock);
5218 while (!list_empty(&med->med_open_head)) {
5219 struct list_head *tmp = med->med_open_head.next;
5220 mfd = list_entry(tmp, struct mdt_file_data, mfd_list);
5222 /* Remove mfd handle so it can't be found again.
5223 * We are consuming the mfd_list reference here. */
5224 class_handle_unhash(&mfd->mfd_handle);
5225 list_move_tail(&mfd->mfd_list, &closing_list);
5227 spin_unlock(&med->med_open_lock);
5228 mdt = mdt_dev(obd->obd_lu_dev);
5229 LASSERT(mdt != NULL);
5231 rc = lu_env_init(&env, LCT_MD_THREAD);
5235 info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5236 LASSERT(info != NULL);
5237 memset(info, 0, sizeof *info);
5238 info->mti_env = &env;
5239 info->mti_mdt = mdt;
5240 info->mti_exp = exp;
5242 if (!list_empty(&closing_list)) {
5243 struct md_attr *ma = &info->mti_attr;
5245 /* Close any open files (which may also cause orphan
5247 list_for_each_entry_safe(mfd, n, &closing_list, mfd_list) {
5248 list_del_init(&mfd->mfd_list);
5249 ma->ma_need = ma->ma_valid = 0;
5251 /* This file is being closed due to an eviction, it
5252 * could have been modified and now dirty regarding to
5253 * HSM archive, check this!
5254 * The logic here is to mark a file dirty if there's a
5255 * chance it was dirtied before the client was evicted,
5256 * so that we don't have to wait for a release attempt
5257 * before finding out the file was actually dirty and
5258 * fail the release. Aggressively marking it dirty here
5259 * will cause the policy engine to attempt to
5260 * re-archive it; when rearchiving, we can compare the
5261 * current version to the HSM data_version and make the
5262 * archive request into a noop if it's not actually
5265 if (mfd->mfd_mode & FMODE_WRITE)
5266 rc = mdt_ctxt_add_dirty_flag(&env, info, mfd);
5268 /* Don't unlink orphan on failover umount, LU-184 */
5269 if (exp->exp_flags & OBD_OPT_FAILOVER) {
5270 ma->ma_valid = MA_FLAGS;
5271 ma->ma_attr_flags |= MDS_KEEP_ORPHAN;
5273 mdt_mfd_close(info, mfd);
5276 info->mti_mdt = NULL;
5277 /* cleanup client slot early */
5278 /* Do not erase record for recoverable client. */
5279 if (!(exp->exp_flags & OBD_OPT_FAILOVER) || exp->exp_failed)
5280 tgt_client_del(&env, exp);
5286 static inline void mdt_enable_slc(struct mdt_device *mdt)
5288 if (mdt->mdt_lut.lut_sync_lock_cancel == NEVER_SYNC_ON_CANCEL)
5289 mdt->mdt_lut.lut_sync_lock_cancel = BLOCKING_SYNC_ON_CANCEL;
5292 static inline void mdt_disable_slc(struct mdt_device *mdt)
5294 if (mdt->mdt_lut.lut_sync_lock_cancel == BLOCKING_SYNC_ON_CANCEL)
5295 mdt->mdt_lut.lut_sync_lock_cancel = NEVER_SYNC_ON_CANCEL;
5298 static int mdt_obd_disconnect(struct obd_export *exp)
5304 class_export_get(exp);
5306 if ((exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) &&
5307 !(exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)) {
5308 struct mdt_device *mdt = mdt_dev(exp->exp_obd->obd_lu_dev);
5310 if (atomic_dec_and_test(&mdt->mdt_mds_mds_conns))
5311 mdt_disable_slc(mdt);
5314 rc = server_disconnect_export(exp);
5316 CDEBUG(D_IOCTL, "server disconnect error: rc = %d\n", rc);
5318 rc = mdt_export_cleanup(exp);
5319 nodemap_del_member(exp);
5320 class_export_put(exp);
5324 /* mds_connect copy */
5325 static int mdt_obd_connect(const struct lu_env *env,
5326 struct obd_export **exp, struct obd_device *obd,
5327 struct obd_uuid *cluuid,
5328 struct obd_connect_data *data,
5331 struct obd_export *lexp;
5332 struct lustre_handle conn = { 0 };
5333 struct mdt_device *mdt;
5335 lnet_nid_t *client_nid = localdata;
5338 LASSERT(env != NULL);
5339 LASSERT(data != NULL);
5341 if (!exp || !obd || !cluuid)
5344 mdt = mdt_dev(obd->obd_lu_dev);
5346 if ((data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
5347 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT)) {
5348 atomic_inc(&mdt->mdt_mds_mds_conns);
5349 mdt_enable_slc(mdt);
5353 * first, check whether the stack is ready to handle requests
5354 * XXX: probably not very appropriate method is used now
5355 * at some point we should find a better one
5357 if (!test_bit(MDT_FL_SYNCED, &mdt->mdt_state) &&
5358 !(data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) &&
5359 !(data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
5360 rc = obd_get_info(env, mdt->mdt_child_exp,
5361 sizeof(KEY_OSP_CONNECTED),
5362 KEY_OSP_CONNECTED, NULL, NULL);
5365 set_bit(MDT_FL_SYNCED, &mdt->mdt_state);
5368 rc = class_connect(&conn, obd, cluuid);
5372 lexp = class_conn2export(&conn);
5373 LASSERT(lexp != NULL);
5375 rc = nodemap_add_member(*client_nid, lexp);
5376 if (rc != 0 && rc != -EEXIST)
5379 rc = mdt_connect_internal(lexp, mdt, data);
5381 struct lsd_client_data *lcd = lexp->exp_target_data.ted_lcd;
5384 memcpy(lcd->lcd_uuid, cluuid, sizeof lcd->lcd_uuid);
5385 rc = tgt_client_new(env, lexp);
5387 mdt_export_stats_init(obd, lexp, localdata);
5391 class_disconnect(lexp);
5392 nodemap_del_member(lexp);
5396 /* Because we do not want this export to be evicted by pinger,
5397 * let's not add this export to the timed chain list. */
5398 if (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
5399 spin_lock(&lexp->exp_obd->obd_dev_lock);
5400 list_del_init(&lexp->exp_obd_chain_timed);
5401 spin_unlock(&lexp->exp_obd->obd_dev_lock);
5408 static int mdt_obd_reconnect(const struct lu_env *env,
5409 struct obd_export *exp, struct obd_device *obd,
5410 struct obd_uuid *cluuid,
5411 struct obd_connect_data *data,
5414 lnet_nid_t *client_nid = localdata;
5418 if (exp == NULL || obd == NULL || cluuid == NULL)
5421 rc = nodemap_add_member(*client_nid, exp);
5422 if (rc != 0 && rc != -EEXIST)
5425 rc = mdt_connect_internal(exp, mdt_dev(obd->obd_lu_dev), data);
5427 mdt_export_stats_init(obd, exp, localdata);
5429 nodemap_del_member(exp);
5434 /* FIXME: Can we avoid using these two interfaces? */
5435 static int mdt_init_export(struct obd_export *exp)
5437 struct mdt_export_data *med = &exp->exp_mdt_data;
5441 INIT_LIST_HEAD(&med->med_open_head);
5442 spin_lock_init(&med->med_open_lock);
5443 spin_lock(&exp->exp_lock);
5444 exp->exp_connecting = 1;
5445 spin_unlock(&exp->exp_lock);
5447 /* self-export doesn't need client data and ldlm initialization */
5448 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5449 &exp->exp_client_uuid)))
5452 rc = tgt_client_alloc(exp);
5456 rc = ldlm_init_export(exp);
5463 tgt_client_free(exp);
5465 CERROR("%s: Failed to initialize export: rc = %d\n",
5466 exp->exp_obd->obd_name, rc);
5470 static int mdt_destroy_export(struct obd_export *exp)
5474 target_destroy_export(exp);
5475 /* destroy can be called from failed obd_setup, so
5476 * checking uuid is safer than obd_self_export */
5477 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
5478 &exp->exp_client_uuid)))
5481 ldlm_destroy_export(exp);
5482 tgt_client_free(exp);
5484 LASSERT(list_empty(&exp->exp_outstanding_replies));
5485 LASSERT(list_empty(&exp->exp_mdt_data.med_open_head));
5490 int mdt_links_read(struct mdt_thread_info *info, struct mdt_object *mdt_obj,
5491 struct linkea_data *ldata)
5495 LASSERT(ldata->ld_buf->lb_buf != NULL);
5497 if (!mdt_object_exists(mdt_obj))
5500 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5501 ldata->ld_buf, XATTR_NAME_LINK);
5502 if (rc == -ERANGE) {
5503 /* Buf was too small, figure out what we need. */
5504 lu_buf_free(ldata->ld_buf);
5505 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5506 ldata->ld_buf, XATTR_NAME_LINK);
5509 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
5510 if (ldata->ld_buf->lb_buf == NULL)
5512 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5513 ldata->ld_buf, XATTR_NAME_LINK);
5518 return linkea_init_with_rec(ldata);
5522 * Given an MDT object, try to look up the full path to the object.
5523 * Part of the MDT layer implementation of lfs fid2path.
5525 * \param[in] info Per-thread common data shared by MDT level handlers.
5526 * \param[in] obj Object to do path lookup of
5527 * \param[in,out] fp User-provided struct to store path information
5528 * \param[in] root_fid Root FID of current path should reach
5530 * \retval 0 Lookup successful, path information stored in fp
5531 * \retval -EAGAIN Lookup failed, usually because object is being moved
5532 * \retval negative errno if there was a problem
5534 static int mdt_path_current(struct mdt_thread_info *info,
5535 struct mdt_object *obj,
5536 struct getinfo_fid2path *fp,
5537 struct lu_fid *root_fid)
5539 struct mdt_device *mdt = info->mti_mdt;
5540 struct mdt_object *mdt_obj;
5541 struct link_ea_header *leh;
5542 struct link_ea_entry *lee;
5543 struct lu_name *tmpname = &info->mti_name;
5544 struct lu_fid *tmpfid = &info->mti_tmp_fid1;
5545 struct lu_buf *buf = &info->mti_big_buf;
5548 struct linkea_data ldata = { NULL };
5553 /* temp buffer for path element, the buffer will be finally freed
5554 * in mdt_thread_info_fini */
5555 buf = lu_buf_check_and_alloc(buf, PATH_MAX);
5556 if (buf->lb_buf == NULL)
5560 ptr = fp->gf_u.gf_path + fp->gf_pathlen - 1;
5563 *tmpfid = fp->gf_fid = *mdt_object_fid(obj);
5565 while (!lu_fid_eq(root_fid, &fp->gf_fid)) {
5566 struct lu_buf lmv_buf;
5568 if (!lu_fid_eq(root_fid, &mdt->mdt_md_root_fid) &&
5569 lu_fid_eq(&mdt->mdt_md_root_fid, &fp->gf_fid))
5570 GOTO(out, rc = -ENOENT);
5572 mdt_obj = mdt_object_find(info->mti_env, mdt, tmpfid);
5573 if (IS_ERR(mdt_obj))
5574 GOTO(out, rc = PTR_ERR(mdt_obj));
5576 if (!mdt_object_exists(mdt_obj)) {
5577 mdt_object_put(info->mti_env, mdt_obj);
5578 GOTO(out, rc = -ENOENT);
5581 if (mdt_object_remote(mdt_obj)) {
5582 mdt_object_put(info->mti_env, mdt_obj);
5583 GOTO(remote_out, rc = -EREMOTE);
5586 rc = mdt_links_read(info, mdt_obj, &ldata);
5588 mdt_object_put(info->mti_env, mdt_obj);
5593 lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
5594 linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
5595 /* If set, use link #linkno for path lookup, otherwise use
5596 link #0. Only do this for the final path element. */
5597 if (first && fp->gf_linkno < leh->leh_reccount) {
5599 for (count = 0; count < fp->gf_linkno; count++) {
5600 lee = (struct link_ea_entry *)
5601 ((char *)lee + reclen);
5602 linkea_entry_unpack(lee, &reclen, tmpname,
5605 if (fp->gf_linkno < leh->leh_reccount - 1)
5606 /* indicate to user there are more links */
5610 lmv_buf.lb_buf = info->mti_xattr_buf;
5611 lmv_buf.lb_len = sizeof(info->mti_xattr_buf);
5612 /* Check if it is slave stripes */
5613 rc = mo_xattr_get(info->mti_env, mdt_object_child(mdt_obj),
5614 &lmv_buf, XATTR_NAME_LMV);
5615 mdt_object_put(info->mti_env, mdt_obj);
5617 union lmv_mds_md *lmm = lmv_buf.lb_buf;
5619 /* For slave stripes, get its master */
5620 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE) {
5621 fp->gf_fid = *tmpfid;
5624 } else if (rc < 0 && rc != -ENODATA) {
5630 /* Pack the name in the end of the buffer */
5631 ptr -= tmpname->ln_namelen;
5632 if (ptr - 1 <= fp->gf_u.gf_path)
5633 GOTO(out, rc = -EOVERFLOW);
5634 strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
5637 /* keep the last resolved fid to the client, so the
5638 * client will build the left path on another MDT for
5640 fp->gf_fid = *tmpfid;
5646 ptr++; /* skip leading / */
5647 memmove(fp->gf_u.gf_path, ptr,
5648 fp->gf_u.gf_path + fp->gf_pathlen - ptr);
5655 * Given an MDT object, use mdt_path_current to get the path.
5656 * Essentially a wrapper to retry mdt_path_current a set number of times
5657 * if -EAGAIN is returned (usually because an object is being moved).
5659 * Part of the MDT layer implementation of lfs fid2path.
5661 * \param[in] info Per-thread common data shared by mdt level handlers.
5662 * \param[in] obj Object to do path lookup of
5663 * \param[in,out] fp User-provided struct for arguments and to store path
5666 * \retval 0 Lookup successful, path information stored in fp
5667 * \retval negative errno if there was a problem
5669 static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
5670 struct getinfo_fid2path *fp, struct lu_fid *root_fid)
5672 struct mdt_device *mdt = info->mti_mdt;
5677 if (fp->gf_pathlen < 3)
5680 if (root_fid == NULL)
5681 root_fid = &mdt->mdt_md_root_fid;
5683 if (lu_fid_eq(root_fid, mdt_object_fid(obj))) {
5684 fp->gf_u.gf_path[0] = '\0';
5688 /* Retry multiple times in case file is being moved */
5689 while (tries-- && rc == -EAGAIN)
5690 rc = mdt_path_current(info, obj, fp, root_fid);
5696 * Get the full path of the provided FID, as of changelog record recno.
5698 * This checks sanity and looks up object for user provided FID
5699 * before calling the actual path lookup code.
5701 * Part of the MDT layer implementation of lfs fid2path.
5703 * \param[in] info Per-thread common data shared by mdt level handlers.
5704 * \param[in,out] fp User-provided struct for arguments and to store path
5707 * \retval 0 Lookup successful, path information and recno stored in fp
5708 * \retval -ENOENT, object does not exist
5709 * \retval negative errno if there was a problem
5711 static int mdt_fid2path(struct mdt_thread_info *info,
5712 struct lu_fid *root_fid,
5713 struct getinfo_fid2path *fp)
5715 struct mdt_device *mdt = info->mti_mdt;
5716 struct mdt_object *obj;
5720 CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
5721 PFID(&fp->gf_fid), fp->gf_recno, fp->gf_linkno);
5723 if (!fid_is_sane(&fp->gf_fid))
5726 if (!fid_is_namespace_visible(&fp->gf_fid)) {
5727 CDEBUG(D_INFO, "%s: "DFID" is invalid, f_seq should be >= %#llx"
5728 ", or f_oid != 0, or f_ver == 0\n", mdt_obd_name(mdt),
5729 PFID(&fp->gf_fid), (__u64)FID_SEQ_NORMAL);
5733 obj = mdt_object_find(info->mti_env, mdt, &fp->gf_fid);
5736 CDEBUG(D_IOCTL, "cannot find "DFID": rc = %d\n",
5737 PFID(&fp->gf_fid), rc);
5741 if (mdt_object_remote(obj))
5743 else if (!mdt_object_exists(obj))
5749 mdt_object_put(info->mti_env, obj);
5750 CDEBUG(D_IOCTL, "nonlocal object "DFID": rc = %d\n",
5751 PFID(&fp->gf_fid), rc);
5755 rc = mdt_path(info, obj, fp, root_fid);
5757 CDEBUG(D_INFO, "fid "DFID", path %s recno %#llx linkno %u\n",
5758 PFID(&fp->gf_fid), fp->gf_u.gf_path,
5759 fp->gf_recno, fp->gf_linkno);
5761 mdt_object_put(info->mti_env, obj);
5766 static int mdt_rpc_fid2path(struct mdt_thread_info *info, void *key, int keylen,
5767 void *val, int vallen)
5769 struct getinfo_fid2path *fpout, *fpin;
5770 struct lu_fid *root_fid = NULL;
5773 fpin = key + cfs_size_round(sizeof(KEY_FID2PATH));
5776 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5777 lustre_swab_fid2path(fpin);
5779 memcpy(fpout, fpin, sizeof(*fpin));
5780 if (fpout->gf_pathlen != vallen - sizeof(*fpin))
5783 if (keylen >= cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*fpin) +
5784 sizeof(struct lu_fid)) {
5785 /* client sent its root FID, which is normally fileset FID */
5786 root_fid = fpin->gf_u.gf_root_fid;
5787 if (ptlrpc_req_need_swab(info->mti_pill->rc_req))
5788 lustre_swab_lu_fid(root_fid);
5790 if (root_fid != NULL && !fid_is_sane(root_fid))
5794 rc = mdt_fid2path(info, root_fid, fpout);
5798 int mdt_get_info(struct tgt_session_info *tsi)
5808 key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
5810 CDEBUG(D_IOCTL, "No GETINFO key\n");
5811 RETURN(err_serious(-EFAULT));
5813 keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
5816 vallen = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_VALLEN);
5817 if (vallen == NULL) {
5818 CDEBUG(D_IOCTL, "%s: cannot get RMF_GETINFO_VALLEN buffer\n",
5819 tgt_name(tsi->tsi_tgt));
5820 RETURN(err_serious(-EFAULT));
5823 req_capsule_set_size(tsi->tsi_pill, &RMF_GETINFO_VAL, RCL_SERVER,
5825 rc = req_capsule_server_pack(tsi->tsi_pill);
5827 RETURN(err_serious(rc));
5829 valout = req_capsule_server_get(tsi->tsi_pill, &RMF_GETINFO_VAL);
5830 if (valout == NULL) {
5831 CDEBUG(D_IOCTL, "%s: cannot get get-info RPC out buffer\n",
5832 tgt_name(tsi->tsi_tgt));
5833 RETURN(err_serious(-EFAULT));
5836 if (KEY_IS(KEY_FID2PATH)) {
5837 struct mdt_thread_info *info = tsi2mdt_info(tsi);
5839 rc = mdt_rpc_fid2path(info, key, keylen, valout, *vallen);
5840 mdt_thread_info_fini(info);
5847 /* Pass the ioc down */
5848 static int mdt_ioc_child(struct lu_env *env, struct mdt_device *mdt,
5849 unsigned int cmd, int len, void *data)
5851 struct lu_context ioctl_session;
5852 struct md_device *next = mdt->mdt_child;
5856 rc = lu_context_init(&ioctl_session, LCT_SERVER_SESSION);
5859 ioctl_session.lc_thread = (struct ptlrpc_thread *)current;
5860 lu_context_enter(&ioctl_session);
5861 env->le_ses = &ioctl_session;
5863 LASSERT(next->md_ops->mdo_iocontrol);
5864 rc = next->md_ops->mdo_iocontrol(env, next, cmd, len, data);
5866 lu_context_exit(&ioctl_session);
5867 lu_context_fini(&ioctl_session);
5871 static int mdt_ioc_version_get(struct mdt_thread_info *mti, void *karg)
5873 struct obd_ioctl_data *data = karg;
5876 struct mdt_object *obj;
5877 struct mdt_lock_handle *lh;
5881 if (data->ioc_inlbuf1 == NULL || data->ioc_inllen1 != sizeof(*fid) ||
5882 data->ioc_inlbuf2 == NULL || data->ioc_inllen2 != sizeof(version))
5885 fid = (struct lu_fid *)data->ioc_inlbuf1;
5887 if (!fid_is_sane(fid))
5890 CDEBUG(D_IOCTL, "getting version for "DFID"\n", PFID(fid));
5892 lh = &mti->mti_lh[MDT_LH_PARENT];
5893 mdt_lock_reg_init(lh, LCK_CR);
5895 obj = mdt_object_find_lock(mti, fid, lh, MDS_INODELOCK_UPDATE);
5897 RETURN(PTR_ERR(obj));
5899 if (mdt_object_remote(obj)) {
5902 * before calling version get the correct MDS should be
5903 * fid, this is error to find remote object here
5905 CERROR("nonlocal object "DFID"\n", PFID(fid));
5906 } else if (!mdt_object_exists(obj)) {
5907 *(__u64 *)data->ioc_inlbuf2 = ENOENT_VERSION;
5910 version = dt_version_get(mti->mti_env, mdt_obj2dt(obj));
5911 *(__u64 *)data->ioc_inlbuf2 = version;
5914 mdt_object_unlock_put(mti, obj, lh, 1);
5918 /* ioctls on obd dev */
5919 static int mdt_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
5920 void *karg, void __user *uarg)
5923 struct obd_device *obd = exp->exp_obd;
5924 struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
5925 struct dt_device *dt = mdt->mdt_bottom;
5929 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
5930 rc = lu_env_init(&env, LCT_MD_THREAD);
5936 rc = mdt_device_sync(&env, mdt);
5938 case OBD_IOC_SET_READONLY:
5939 rc = dt_sync(&env, dt);
5941 rc = dt_ro(&env, dt);
5943 case OBD_IOC_ABORT_RECOVERY:
5944 CERROR("%s: Aborting recovery for device\n", mdt_obd_name(mdt));
5945 obd->obd_abort_recovery = 1;
5946 target_stop_recovery_thread(obd);
5949 case OBD_IOC_CHANGELOG_REG:
5950 case OBD_IOC_CHANGELOG_DEREG:
5951 case OBD_IOC_CHANGELOG_CLEAR:
5952 rc = mdt_ioc_child(&env, mdt, cmd, len, karg);
5954 case OBD_IOC_START_LFSCK: {
5955 struct md_device *next = mdt->mdt_child;
5956 struct obd_ioctl_data *data = karg;
5957 struct lfsck_start_param lsp;
5959 if (unlikely(data == NULL)) {
5964 lsp.lsp_start = (struct lfsck_start *)(data->ioc_inlbuf1);
5965 lsp.lsp_index_valid = 0;
5966 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &lsp);
5969 case OBD_IOC_STOP_LFSCK: {
5970 struct md_device *next = mdt->mdt_child;
5971 struct obd_ioctl_data *data = karg;
5972 struct lfsck_stop stop;
5974 stop.ls_status = LS_STOPPED;
5975 /* Old lfsck utils may pass NULL @stop. */
5976 if (data->ioc_inlbuf1 == NULL)
5980 ((struct lfsck_stop *)(data->ioc_inlbuf1))->ls_flags;
5982 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0, &stop);
5985 case OBD_IOC_QUERY_LFSCK: {
5986 struct md_device *next = mdt->mdt_child;
5987 struct obd_ioctl_data *data = karg;
5989 rc = next->md_ops->mdo_iocontrol(&env, next, cmd, 0,
5993 case OBD_IOC_GET_OBJ_VERSION: {
5994 struct mdt_thread_info *mti;
5995 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
5996 memset(mti, 0, sizeof *mti);
5997 mti->mti_env = &env;
6001 rc = mdt_ioc_version_get(mti, karg);
6004 case OBD_IOC_CATLOGLIST: {
6005 struct mdt_thread_info *mti;
6007 mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
6008 lu_local_obj_fid(&mti->mti_tmp_fid1, LLOG_CATALOGS_OID);
6009 rc = llog_catalog_list(&env, mdt->mdt_bottom, 0, karg,
6010 &mti->mti_tmp_fid1);
6015 CERROR("%s: Not supported cmd = %d, rc = %d\n",
6016 mdt_obd_name(mdt), cmd, rc);
6023 static int mdt_postrecov(const struct lu_env *env, struct mdt_device *mdt)
6025 struct lu_device *ld = md2lu_dev(mdt->mdt_child);
6029 if (!mdt->mdt_skip_lfsck) {
6030 struct lfsck_start_param lsp;
6032 lsp.lsp_start = NULL;
6033 lsp.lsp_index_valid = 0;
6034 rc = mdt->mdt_child->md_ops->mdo_iocontrol(env, mdt->mdt_child,
6035 OBD_IOC_START_LFSCK,
6037 if (rc != 0 && rc != -EALREADY)
6038 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
6039 mdt_obd_name(mdt), rc);
6042 rc = ld->ld_ops->ldo_recovery_complete(env, ld);
6046 static int mdt_obd_postrecov(struct obd_device *obd)
6051 rc = lu_env_init(&env, LCT_MD_THREAD);
6054 rc = mdt_postrecov(&env, mdt_dev(obd->obd_lu_dev));
6059 static struct obd_ops mdt_obd_device_ops = {
6060 .o_owner = THIS_MODULE,
6061 .o_set_info_async = mdt_obd_set_info_async,
6062 .o_connect = mdt_obd_connect,
6063 .o_reconnect = mdt_obd_reconnect,
6064 .o_disconnect = mdt_obd_disconnect,
6065 .o_init_export = mdt_init_export,
6066 .o_destroy_export = mdt_destroy_export,
6067 .o_iocontrol = mdt_iocontrol,
6068 .o_postrecov = mdt_obd_postrecov,
6071 static struct lu_device* mdt_device_fini(const struct lu_env *env,
6072 struct lu_device *d)
6074 struct mdt_device *m = mdt_dev(d);
6081 static struct lu_device *mdt_device_free(const struct lu_env *env,
6082 struct lu_device *d)
6084 struct mdt_device *m = mdt_dev(d);
6087 lu_device_fini(&m->mdt_lu_dev);
6093 static struct lu_device *mdt_device_alloc(const struct lu_env *env,
6094 struct lu_device_type *t,
6095 struct lustre_cfg *cfg)
6097 struct lu_device *l;
6098 struct mdt_device *m;
6105 rc = mdt_init0(env, m, t, cfg);
6107 mdt_device_free(env, l);
6112 l = ERR_PTR(-ENOMEM);
6116 /* context key constructor/destructor: mdt_key_init, mdt_key_fini */
6117 LU_KEY_INIT(mdt, struct mdt_thread_info);
6119 static void mdt_key_fini(const struct lu_context *ctx,
6120 struct lu_context_key *key, void* data)
6122 struct mdt_thread_info *info = data;
6124 if (info->mti_big_lmm) {
6125 OBD_FREE_LARGE(info->mti_big_lmm, info->mti_big_lmmsize);
6126 info->mti_big_lmm = NULL;
6127 info->mti_big_lmmsize = 0;
6132 /* context key: mdt_thread_key */
6133 LU_CONTEXT_KEY_DEFINE(mdt, LCT_MD_THREAD);
6135 struct lu_ucred *mdt_ucred(const struct mdt_thread_info *info)
6137 return lu_ucred(info->mti_env);
6140 struct lu_ucred *mdt_ucred_check(const struct mdt_thread_info *info)
6142 return lu_ucred_check(info->mti_env);
6146 * Enable/disable COS (Commit On Sharing).
6148 * Set/Clear the COS flag in mdt options.
6150 * \param mdt mdt device
6151 * \param val 0 disables COS, other values enable COS
6153 void mdt_enable_cos(struct mdt_device *mdt, int val)
6158 mdt->mdt_opts.mo_cos = !!val;
6159 rc = lu_env_init(&env, LCT_LOCAL);
6160 if (unlikely(rc != 0)) {
6161 CWARN("%s: lu_env initialization failed, cannot "
6162 "sync: rc = %d\n", mdt_obd_name(mdt), rc);
6165 mdt_device_sync(&env, mdt);
6170 * Check COS (Commit On Sharing) status.
6172 * Return COS flag status.
6174 * \param mdt mdt device
6176 int mdt_cos_is_enabled(struct mdt_device *mdt)
6178 return mdt->mdt_opts.mo_cos != 0;
6181 static struct lu_device_type_operations mdt_device_type_ops = {
6182 .ldto_device_alloc = mdt_device_alloc,
6183 .ldto_device_free = mdt_device_free,
6184 .ldto_device_fini = mdt_device_fini
6187 static struct lu_device_type mdt_device_type = {
6188 .ldt_tags = LU_DEVICE_MD,
6189 .ldt_name = LUSTRE_MDT_NAME,
6190 .ldt_ops = &mdt_device_type_ops,
6191 .ldt_ctx_tags = LCT_MD_THREAD
6194 static int __init mdt_init(void)
6198 CLASSERT(sizeof("0x0123456789ABCDEF:0x01234567:0x01234567") ==
6199 FID_NOBRACE_LEN + 1);
6200 CLASSERT(sizeof("[0x0123456789ABCDEF:0x01234567:0x01234567]") ==
6202 rc = lu_kmem_init(mdt_caches);
6206 rc = mds_mod_init();
6210 rc = class_register_type(&mdt_obd_device_ops, NULL, true, NULL,
6211 LUSTRE_MDT_NAME, &mdt_device_type);
6216 lu_kmem_fini(mdt_caches);
6223 static void __exit mdt_exit(void)
6225 class_unregister_type(LUSTRE_MDT_NAME);
6227 lu_kmem_fini(mdt_caches);
6230 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
6231 MODULE_DESCRIPTION("Lustre Metadata Target ("LUSTRE_MDT_NAME")");
6232 MODULE_VERSION(LUSTRE_VERSION_STRING);
6233 MODULE_LICENSE("GPL");
6235 module_init(mdt_init);
6236 module_exit(mdt_exit);